Skip to content
Merged
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
45 changes: 31 additions & 14 deletions class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Handles server-side registration and use of all blocks available in Jetpack for the block editor, aka Gutenberg.
* Works in tandem with client-side block registration via `_inc/blocks/block-manifest.json`
* Works in tandem with client-side block registration via `block-manifest.json`
*
* @package Jetpack
*/
Expand Down Expand Up @@ -84,30 +84,46 @@ public static function load_blocks() {
}

/**
* Checks for a given .json file in the `_inc/blocks` folder.
* Return the Gutenberg extensions (blocks and plugins) directory
*
* @return string The Gutenberg extensions directory
*/
public static function get_blocks_directory() {
/**
* Filter to select Gutenberg blocks directory
*
* @since 6.9
*
* @param string default: '_inc/blocks/'
*/
return apply_filters( 'jetpack_blocks_directory', '_inc/blocks/' );
}

/**
* Checks for a given .json file in the blocks folder.
*
* @param $preset The name of the .json file to look for.
*
* @return bool True if the file is found.
*/
public static function preset_exists( $preset ) {
return file_exists( JETPACK__PLUGIN_DIR . '/_inc/blocks/' . $preset . '.json' );
return file_exists( JETPACK__PLUGIN_DIR . self::get_blocks_directory() . $preset . '.json' );
}

/**
* Decodes JSON loaded from a preset file in `_inc/blocks`
* Decodes JSON loaded from a preset file in the blocks folder
*
* @param $preset The name of the .json file to load.
*
* @return mixed Returns an object if the file is present, or false if a valid .json file is not present.
*/
public static function get_preset( $preset ) {
return json_decode( file_get_contents( JETPACK__PLUGIN_DIR . '/_inc/blocks/' . $preset . '.json' ) );
return json_decode( file_get_contents( JETPACK__PLUGIN_DIR .self::get_blocks_directory() . $preset . '.json' ) );
}

/**
* Filters the results of `apply_filter( 'jetpack_set_available_blocks', array() )`
* using the merged contents of `_inc/blocks/blocks-manifest.json` ( $preset_blocks )
* using the merged contents of `blocks-manifest.json` ( $preset_blocks )
* and self::$jetpack_blocks ( $internal_blocks )
*
* @param $blocks The default list.
Expand Down Expand Up @@ -217,15 +233,15 @@ public static function load_assets_as_required( $type, $script_dependencies = ar

$type = sanitize_title_with_dashes( $type );
// Enqueue styles.
$style_relative_path = '_inc/blocks/' . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
$style_relative_path = self::get_blocks_directory() . $type . '/view' . ( is_rtl() ? '.rtl' : '' ) . '.css';
if ( self::block_has_asset( $style_relative_path ) ) {
$style_version = self::get_asset_version( $style_relative_path );
$view_style = plugins_url( $style_relative_path, JETPACK__PLUGIN_FILE );
wp_enqueue_style( 'jetpack-block-' . $type, $view_style, array(), $style_version );
}

// Enqueue script.
$script_relative_path = '_inc/blocks/' . $type . '/view.js';
$script_relative_path = self::get_blocks_directory() . $type . '/view.js';
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 );
Expand All @@ -235,7 +251,7 @@ public static function load_assets_as_required( $type, $script_dependencies = ar
wp_localize_script(
'jetpack-block-' . $type,
'Jetpack_Block_Assets_Base_Url',
plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE )
plugins_url( self::get_blocks_directory(), JETPACK__PLUGIN_FILE )
);
}

Expand Down Expand Up @@ -277,12 +293,13 @@ public static function enqueue_block_editor_assets() {

$rtl = is_rtl() ? '.rtl' : '';
$beta = Jetpack_Constants::is_true('JETPACK_BETA_BLOCKS' ) ? '-beta' : '';
$blocks_dir = self::get_blocks_directory();

$editor_script = plugins_url( "_inc/blocks/editor{$beta}.js", JETPACK__PLUGIN_FILE );
$editor_style = plugins_url( "_inc/blocks/editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE );
$editor_script = plugins_url( "{$blocks_dir}editor{$beta}.js", JETPACK__PLUGIN_FILE );
$editor_style = plugins_url( "{$blocks_dir}editor{$beta}{$rtl}.css", JETPACK__PLUGIN_FILE );

$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/editor.js' )
$version = Jetpack::is_development_version() && file_exists( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
? filemtime( JETPACK__PLUGIN_DIR . $blocks_dir . 'editor.js' )
: JETPACK__VERSION;

wp_enqueue_script(
Expand Down Expand Up @@ -313,7 +330,7 @@ public static function enqueue_block_editor_assets() {
wp_localize_script(
'jetpack-blocks-editor',
'Jetpack_Block_Assets_Base_Url',
plugins_url( '_inc/blocks/', JETPACK__PLUGIN_FILE )
plugins_url( $blocks_dir . '/', JETPACK__PLUGIN_FILE )
);

wp_localize_script(
Expand Down