| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstrapping 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 |
$settings = array_merge( |
| 47 |
gutenberg_get_common_block_editor_settings(), |
| 48 |
gutenberg_get_legacy_widget_settings() |
| 49 |
); |
| 50 |
|
| 51 |
// This purposefully does not rely on apply_filters( 'block_editor_settings', $settings, null ); |
| 52 |
// Applying that filter would bring over multitude of features from the post editor, some of which |
| 53 |
// may be undesirable. Instead of using that filter, we simply pick just the settings that are needed. |
| 54 |
$settings = gutenberg_experimental_global_styles_settings( $settings ); |
| 55 |
$settings = gutenberg_extend_block_editor_styles( $settings ); |
| 56 |
|
| 57 |
$preload_paths = array( |
| 58 |
array( '/wp/v2/media', 'OPTIONS' ), |
| 59 |
'/wp/v2/sidebars?context=edit&per_page=-1', |
| 60 |
'/wp/v2/widgets?context=edit&per_page=-1', |
| 61 |
); |
| 62 |
$preload_data = array_reduce( |
| 63 |
$preload_paths, |
| 64 |
'rest_preload_api_request', |
| 65 |
array() |
| 66 |
); |
| 67 |
wp_add_inline_script( |
| 68 |
'wp-api-fetch', |
| 69 |
sprintf( |
| 70 |
'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', |
| 71 |
wp_json_encode( $preload_data ) |
| 72 |
), |
| 73 |
'after' |
| 74 |
); |
| 75 |
|
| 76 |
wp_add_inline_script( |
| 77 |
'wp-edit-widgets', |
| 78 |
sprintf( |
| 79 |
'wp.domReady( function() { |
| 80 |
wp.editWidgets.%s( "widgets-editor", %s ); |
| 81 |
} );', |
| 82 |
$initializer_name, |
| 83 |
wp_json_encode( $settings ) |
| 84 |
) |
| 85 |
); |
| 86 |
|
| 87 |
// Preload server-registered block schemas. |
| 88 |
wp_add_inline_script( |
| 89 |
'wp-blocks', |
| 90 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
| 91 |
); |
| 92 |
|
| 93 |
wp_enqueue_script( 'wp-edit-widgets' ); |
| 94 |
wp_enqueue_script( 'admin-widgets' ); |
| 95 |
wp_enqueue_script( 'wp-format-library' ); |
| 96 |
wp_enqueue_style( 'wp-edit-widgets' ); |
| 97 |
wp_enqueue_style( 'wp-format-library' ); |
| 98 |
} |
| 99 |
add_action( 'admin_enqueue_scripts', 'gutenberg_widgets_init' ); |
| 100 |
|
| 101 |
/** |
| 102 |
* Tells the script loader to load the scripts and styles of custom block on widgets editor screen. |
| 103 |
* |
| 104 |
* @param bool $is_block_editor_screen Current decision about loading block assets. |
| 105 |
* @return bool Filtered decision about loading block assets. |
| 106 |
*/ |
| 107 |
function gutenberg_widgets_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { |
| 108 |
if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
return $is_block_editor_screen; |
| 113 |
} |
| 114 |
|
| 115 |
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_widgets_editor_load_block_editor_scripts_and_styles' ); |
| 116 |
|
| 117 |
/** |
| 118 |
* Show responsive embeds correctly on the widgets screen by adding the wp-embed-responsive class. |
| 119 |
* |
| 120 |
* @param string $classes existing admin body classes. |
| 121 |
* |
| 122 |
* @return string admin body classes including the wp-embed-responsive class. |
| 123 |
*/ |
| 124 |
function gutenberg_widgets_editor_add_responsive_embed_body_class( $classes ) { |
| 125 |
return "$classes wp-embed-responsive"; |
| 126 |
} |
| 127 |
|
| 128 |
add_filter( 'admin_body_class', 'gutenberg_widgets_editor_add_responsive_embed_body_class' ); |
| 129 |
|