Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone Block Registration/Block Dependencies #10503

Merged
merged 4 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ public static function should_load_blocks() {
* Only enqueue block assets when needed.
*
* @param string $type slug of the block.
* @param array $script_dependencies An array of view-side Javascript dependencies to be enqueued.
*
* @return void
*/
public static function load_assets_as_required( $type ) {
public static function load_assets_as_required( $type, $script_dependencies = array() ) {
$type = sanitize_title_with_dashes( $type );
// Enqueue styles.
$style_relative_path = '_inc/blocks/' . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
Expand All @@ -124,7 +125,7 @@ public static function load_assets_as_required( $type ) {
if ( self::block_has_asset( $script_relative_path ) ) {
$script_version = self::get_asset_version( $script_relative_path );
$view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, array(), $script_version, false );
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
}
}

Expand Down
32 changes: 32 additions & 0 deletions modules/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Load code specific to Gutenberg blocks which are not tied to a module.
* This file is unusual, and is not an actual `module` as such.
* It is included in ./module-extras.php
kraftbj marked this conversation as resolved.
Show resolved Hide resolved
*
*/

jetpack_register_block(
kraftbj marked this conversation as resolved.
Show resolved Hide resolved
'map',
array(
'render_callback' => 'jetpack_map_block_load_assets',
)
jeffersonrabb marked this conversation as resolved.
Show resolved Hide resolved
);

/**
* Map block registration/dependency declaration.
*
* @param array $attr - Array containing the map block attributes.
* @param string $content - String containing the map block content.
*
* @return string
*/
function jetpack_map_block_load_assets( $attr, $content ) {
$dependencies = array(
'wp-element',
'wp-i18n',
'wp-api-fetch',
);
Jetpack_Gutenberg::load_assets_as_required( 'map', $dependencies );
return $content;
}
7 changes: 6 additions & 1 deletion modules/module-extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
);
}

/* If Gutenberg blocks are enabled, register blocks that aren't associated with modules */
if ( Jetpack_Gutenberg::should_load_blocks() ) {
$tools[] = 'blocks.php';
}

/**
* Filter extra tools (not modules) to include.
*
Expand All @@ -62,4 +67,4 @@
function jetpack_widgets_add_suffix( $widget_name ) {
return sprintf( __( '%s (Jetpack)', 'jetpack' ), $widget_name );
}
add_filter( 'jetpack_widget_name', 'jetpack_widgets_add_suffix' );
add_filter( 'jetpack_widget_name', 'jetpack_widgets_add_suffix' );