| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraping the Gutenberg widgets page. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* The main entry point for the Gutenberg widgets page. |
| 10 |
* |
| 11 |
* @since 5.2.0 |
| 12 |
*/ |
| 13 |
function the_gutenberg_widgets() { |
| 14 |
?> |
| 15 |
<div |
| 16 |
id="widgets-editor" |
| 17 |
class="blocks-widgets-container" |
| 18 |
> |
| 19 |
</div> |
| 20 |
<?php |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Initialize the Gutenberg widgets page. |
| 25 |
* |
| 26 |
* @since 5.2.0 |
| 27 |
* |
| 28 |
* @param string $hook Page. |
| 29 |
*/ |
| 30 |
function gutenberg_widgets_init( $hook ) { |
| 31 |
if ( 'widgets.php' === $hook ) { |
| 32 |
wp_enqueue_style( 'wp-block-library' ); |
| 33 |
wp_enqueue_style( 'wp-block-library-theme' ); |
| 34 |
wp_add_inline_style( |
| 35 |
'wp-block-library-theme', |
| 36 |
'.block-widget-form .widget-control-save { display: none; }' |
| 37 |
); |
| 38 |
return; |
| 39 |
} |
| 40 |
if ( ! in_array( $hook, array( 'appearance_page_gutenberg-widgets' ), true ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$initializer_name = 'initialize'; |
| 45 |
|
| 46 |
// Media settings. |
| 47 |
$max_upload_size = wp_max_upload_size(); |
| 48 |
if ( ! $max_upload_size ) { |
| 49 |
$max_upload_size = 0; |
| 50 |
} |
| 51 |
|
| 52 |
/** This filter is documented in wp-admin/includes/media.php */ |
| 53 |
$image_size_names = apply_filters( |
| 54 |
'image_size_names_choose', |
| 55 |
array( |
| 56 |
'thumbnail' => __( 'Thumbnail', 'gutenberg' ), |
| 57 |
'medium' => __( 'Medium', 'gutenberg' ), |
| 58 |
'large' => __( 'Large', 'gutenberg' ), |
| 59 |
'full' => __( 'Full Size', 'gutenberg' ), |
| 60 |
) |
| 61 |
); |
| 62 |
|
| 63 |
$available_image_sizes = array(); |
| 64 |
foreach ( $image_size_names as $image_size_slug => $image_size_name ) { |
| 65 |
$available_image_sizes[] = array( |
| 66 |
'slug' => $image_size_slug, |
| 67 |
'name' => $image_size_name, |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
$settings = array_merge( |
| 72 |
array( |
| 73 |
'imageSizes' => $available_image_sizes, |
| 74 |
'isRTL' => is_rtl(), |
| 75 |
'maxUploadFileSize' => $max_upload_size, |
| 76 |
), |
| 77 |
gutenberg_get_legacy_widget_settings() |
| 78 |
); |
| 79 |
|
| 80 |
// This purposefully does not rely on apply_filters( 'block_editor_settings', $settings, null ); |
| 81 |
// Applying that filter would bring over multitude of features from the post editor, some of which |
| 82 |
// may be undesirable. Instead of using that filter, we simply pick just the settings that are needed. |
| 83 |
$settings = gutenberg_experimental_global_styles_settings( $settings ); |
| 84 |
$settings = gutenberg_extend_block_editor_styles( $settings ); |
| 85 |
|
| 86 |
$preload_paths = array( |
| 87 |
array( '/wp/v2/media', 'OPTIONS' ), |
| 88 |
'/wp/v2/sidebars?context=edit&per_page=-1', |
| 89 |
'/wp/v2/widgets?context=edit&per_page=-1', |
| 90 |
); |
| 91 |
$preload_data = array_reduce( |
| 92 |
$preload_paths, |
| 93 |
'rest_preload_api_request', |
| 94 |
array() |
| 95 |
); |
| 96 |
wp_add_inline_script( |
| 97 |
'wp-api-fetch', |
| 98 |
sprintf( |
| 99 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 100 |
wp_json_encode( $preload_data ) |
| 101 |
), |
| 102 |
'after' |
| 103 |
); |
| 104 |
|
| 105 |
wp_add_inline_script( |
| 106 |
'wp-edit-widgets', |
| 107 |
sprintf( |
| 108 |
'wp.domReady( function() { |
| 109 |
wp.editWidgets.%s( "widgets-editor", %s ); |
| 110 |
} );', |
| 111 |
$initializer_name, |
| 112 |
wp_json_encode( gutenberg_experiments_editor_settings( $settings ) ) |
| 113 |
) |
| 114 |
); |
| 115 |
|
| 116 |
// Preload server-registered block schemas. |
| 117 |
wp_add_inline_script( |
| 118 |
'wp-blocks', |
| 119 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
| 120 |
); |
| 121 |
|
| 122 |
wp_enqueue_script( 'wp-edit-widgets' ); |
| 123 |
wp_enqueue_script( 'admin-widgets' ); |
| 124 |
wp_enqueue_script( 'wp-format-library' ); |
| 125 |
wp_enqueue_style( 'wp-edit-widgets' ); |
| 126 |
wp_enqueue_style( 'wp-format-library' ); |
| 127 |
} |
| 128 |
add_action( 'admin_enqueue_scripts', 'gutenberg_widgets_init' ); |
| 129 |
|
| 130 |
/** |
| 131 |
* Tells the script loader to load the scripts and styles of custom block on widgets editor screen. |
| 132 |
* |
| 133 |
* @param bool $is_block_editor_screen Current decision about loading block assets. |
| 134 |
* @return bool Filtered decision about loading block assets. |
| 135 |
*/ |
| 136 |
function gutenberg_widgets_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { |
| 137 |
if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
return $is_block_editor_screen; |
| 142 |
} |
| 143 |
|
| 144 |
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_widgets_editor_load_block_editor_scripts_and_styles' ); |
| 145 |
|
| 146 |
/** |
| 147 |
* Show responsive embeds correctly on the widgets screen by adding the wp-embed-responsive class. |
| 148 |
* |
| 149 |
* @param string $classes existing admin body classes. |
| 150 |
* |
| 151 |
* @return string admin body classes including the wp-embed-responsive class. |
| 152 |
*/ |
| 153 |
function gutenberg_widgets_editor_add_responsive_embed_body_class( $classes ) { |
| 154 |
return "$classes wp-embed-responsive"; |
| 155 |
} |
| 156 |
|
| 157 |
add_filter( 'admin_body_class', 'gutenberg_widgets_editor_add_responsive_embed_body_class' ); |
| 158 |
|