| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utilities to manage editor settings. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns editor settings that are common to all editors: |
| 10 |
* post, site, widgets, and navigation. |
| 11 |
* |
| 12 |
* All these settings are already part of core, |
| 13 |
* see edit-form-blocks.php |
| 14 |
* |
| 15 |
* @return array Common editor settings. |
| 16 |
*/ |
| 17 |
function gutenberg_get_common_block_editor_settings() { |
| 18 |
$max_upload_size = wp_max_upload_size(); |
| 19 |
if ( ! $max_upload_size ) { |
| 20 |
$max_upload_size = 0; |
| 21 |
} |
| 22 |
|
| 23 |
$available_image_sizes = array(); |
| 24 |
// This filter is documented in wp-admin/includes/media.php. |
| 25 |
$image_size_names = apply_filters( |
| 26 |
'image_size_names_choose', |
| 27 |
array( |
| 28 |
'thumbnail' => __( 'Thumbnail', 'gutenberg' ), |
| 29 |
'medium' => __( 'Medium', 'gutenberg' ), |
| 30 |
'large' => __( 'Large', 'gutenberg' ), |
| 31 |
'full' => __( 'Full Size', 'gutenberg' ), |
| 32 |
) |
| 33 |
); |
| 34 |
foreach ( $image_size_names as $image_size_slug => $image_size_name ) { |
| 35 |
$available_image_sizes[] = array( |
| 36 |
'slug' => $image_size_slug, |
| 37 |
'name' => $image_size_name, |
| 38 |
); |
| 39 |
}; |
| 40 |
|
| 41 |
$settings = array( |
| 42 |
'__unstableEnableFullSiteEditingBlocks' => gutenberg_is_fse_theme(), |
| 43 |
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), |
| 44 |
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), |
| 45 |
'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), |
| 46 |
'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), |
| 47 |
'enableCustomUnits' => get_theme_support( 'custom-units' ), |
| 48 |
'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), |
| 49 |
'imageSizes' => $available_image_sizes, |
| 50 |
'isRTL' => is_rtl(), |
| 51 |
'maxUploadFileSize' => $max_upload_size, |
| 52 |
'allowedMimeTypes' => get_allowed_mime_types(), |
| 53 |
); |
| 54 |
|
| 55 |
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); |
| 56 |
if ( false !== $color_palette ) { |
| 57 |
$settings['colors'] = $color_palette; |
| 58 |
} |
| 59 |
|
| 60 |
$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); |
| 61 |
if ( false !== $font_sizes ) { |
| 62 |
$settings['fontSizes'] = $font_sizes; |
| 63 |
} |
| 64 |
|
| 65 |
$gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); |
| 66 |
if ( false !== $gradient_presets ) { |
| 67 |
$settings['gradients'] = $gradient_presets; |
| 68 |
} |
| 69 |
|
| 70 |
return $settings; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Extends the block editor with settings that are only in the plugin. |
| 75 |
* |
| 76 |
* @param array $settings Existing editor settings. |
| 77 |
* |
| 78 |
* @return array Filtered settings. |
| 79 |
*/ |
| 80 |
function gutenberg_extend_post_editor_settings( $settings ) { |
| 81 |
$image_default_size = get_option( 'image_default_size', 'large' ); |
| 82 |
$image_sizes = wp_list_pluck( $settings['imageSizes'], 'slug' ); |
| 83 |
|
| 84 |
$settings['imageDefaultSize'] = in_array( $image_default_size, $image_sizes, true ) ? $image_default_size : 'large'; |
| 85 |
$settings['__unstableEnableFullSiteEditingBlocks'] = gutenberg_is_fse_theme(); |
| 86 |
|
| 87 |
return $settings; |
| 88 |
} |
| 89 |
add_filter( 'block_editor_settings', 'gutenberg_extend_post_editor_settings' ); |
| 90 |
|
| 91 |
/** |
| 92 |
* Initialize a block-based editor. |
| 93 |
* |
| 94 |
* @param string $editor_name Editor name. |
| 95 |
* @param string $editor_script_handle Editor script handle. |
| 96 |
* @param array $settings { |
| 97 |
* Elements to initialize a block-based editor. |
| 98 |
* |
| 99 |
* @type array $preload_paths Array of paths to preload. |
| 100 |
* @type string $initializer_name Editor initialization function name. |
| 101 |
* @type array $editor_settings Editor settings. |
| 102 |
* } |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $settings ) { |
| 106 |
|
| 107 |
$defaults = array( |
| 108 |
'preload_paths' => array(), |
| 109 |
'initializer_name' => 'initialize', |
| 110 |
'editor_settings' => array(), |
| 111 |
); |
| 112 |
|
| 113 |
$settings = wp_parse_args( $settings, $defaults ); |
| 114 |
|
| 115 |
/** |
| 116 |
* Preload common data by specifying an array of REST API paths that will be preloaded. |
| 117 |
* |
| 118 |
* Filters the array of paths that will be preloaded. |
| 119 |
* |
| 120 |
* @param string[] $preload_paths Array of paths to preload. |
| 121 |
*/ |
| 122 |
$preload_paths = apply_filters( "{$editor_name}_preload_paths", $settings['preload_paths'] ); |
| 123 |
|
| 124 |
$preload_data = array_reduce( |
| 125 |
$preload_paths, |
| 126 |
'rest_preload_api_request', |
| 127 |
array() |
| 128 |
); |
| 129 |
wp_add_inline_script( |
| 130 |
'wp-api-fetch', |
| 131 |
sprintf( |
| 132 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 133 |
wp_json_encode( $preload_data ) |
| 134 |
), |
| 135 |
'after' |
| 136 |
); |
| 137 |
wp_add_inline_script( |
| 138 |
"wp-{$editor_script_handle}", |
| 139 |
sprintf( |
| 140 |
'wp.domReady( function() { |
| 141 |
wp.%s.%s( "%s", %s ); |
| 142 |
} );', |
| 143 |
lcfirst( str_replace( '-', '', ucwords( $editor_script_handle, '-' ) ) ), |
| 144 |
$settings['initializer_name'], |
| 145 |
str_replace( '_', '-', $editor_name ), |
| 146 |
wp_json_encode( $settings['editor_settings'] ) |
| 147 |
) |
| 148 |
); |
| 149 |
|
| 150 |
// Preload server-registered block schemas. |
| 151 |
wp_add_inline_script( |
| 152 |
'wp-blocks', |
| 153 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
| 154 |
); |
| 155 |
|
| 156 |
} |
| 157 |
|