| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utilities to manage editor settings. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Extends the block editor with settings that are only in the plugin. |
| 10 |
* |
| 11 |
* This is a temporary solution until the Gutenberg plugin sets |
| 12 |
* the required WordPress version to 5.8. |
| 13 |
* |
| 14 |
* @see https://core.trac.wordpress.org/ticket/52920 |
| 15 |
* |
| 16 |
* @param array $settings Existing editor settings. |
| 17 |
* |
| 18 |
* @return array Filtered settings. |
| 19 |
*/ |
| 20 |
function gutenberg_extend_post_editor_settings( $settings ) { |
| 21 |
$image_default_size = get_option( 'image_default_size', 'large' ); |
| 22 |
$image_sizes = wp_list_pluck( $settings['imageSizes'], 'slug' ); |
| 23 |
|
| 24 |
$settings['imageDefaultSize'] = in_array( $image_default_size, $image_sizes, true ) ? $image_default_size : 'large'; |
| 25 |
$settings['__unstableEnableFullSiteEditingBlocks'] = gutenberg_supports_block_templates(); |
| 26 |
|
| 27 |
if ( gutenberg_is_fse_theme() ) { |
| 28 |
$settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas(); |
| 29 |
} |
| 30 |
|
| 31 |
return $settings; |
| 32 |
} |
| 33 |
// This can be removed when plugin support requires WordPress 5.8.0+. |
| 34 |
if ( function_exists( 'get_block_editor_settings' ) ) { |
| 35 |
add_filter( 'block_editor_settings_all', 'gutenberg_extend_post_editor_settings' ); |
| 36 |
} else { |
| 37 |
add_filter( 'block_editor_settings', 'gutenberg_extend_post_editor_settings' ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Initialize a block-based editor. |
| 42 |
* |
| 43 |
* @param string $editor_name Editor name. |
| 44 |
* @param string $editor_script_handle Editor script handle. |
| 45 |
* @param array $settings { |
| 46 |
* Elements to initialize a block-based editor. |
| 47 |
* |
| 48 |
* @type array $preload_paths Array of paths to preload. |
| 49 |
* @type string $initializer_name Editor initialization function name. |
| 50 |
* @type array $editor_settings Editor settings. |
| 51 |
* } |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $settings ) { |
| 55 |
|
| 56 |
$defaults = array( |
| 57 |
'preload_paths' => array(), |
| 58 |
'initializer_name' => 'initialize', |
| 59 |
'editor_settings' => array(), |
| 60 |
); |
| 61 |
|
| 62 |
$settings = wp_parse_args( $settings, $defaults ); |
| 63 |
|
| 64 |
/** |
| 65 |
* Preload common data by specifying an array of REST API paths that will be preloaded. |
| 66 |
* |
| 67 |
* Filters the array of paths that will be preloaded. |
| 68 |
* |
| 69 |
* @param string[] $preload_paths Array of paths to preload. |
| 70 |
*/ |
| 71 |
$preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] ); |
| 72 |
|
| 73 |
$preload_data = array_reduce( |
| 74 |
$preload_paths, |
| 75 |
'rest_preload_api_request', |
| 76 |
array() |
| 77 |
); |
| 78 |
|
| 79 |
wp_add_inline_script( |
| 80 |
'wp-api-fetch', |
| 81 |
sprintf( |
| 82 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 83 |
wp_json_encode( $preload_data ) |
| 84 |
), |
| 85 |
'after' |
| 86 |
); |
| 87 |
wp_add_inline_script( |
| 88 |
"wp-{$editor_script_handle}", |
| 89 |
sprintf( |
| 90 |
'wp.domReady( function() { |
| 91 |
wp.%s.%s( "%s", %s ); |
| 92 |
} );', |
| 93 |
lcfirst( str_replace( '-', '', ucwords( $editor_script_handle, '-' ) ) ), |
| 94 |
$settings['initializer_name'], |
| 95 |
str_replace( '_', '-', $editor_name ), |
| 96 |
wp_json_encode( $settings['editor_settings'] ) |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
// Preload server-registered block schemas. |
| 101 |
wp_add_inline_script( |
| 102 |
'wp-blocks', |
| 103 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
| 104 |
); |
| 105 |
|
| 106 |
} |
| 107 |
|