| 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 ( ! in_array( $hook, array( 'appearance_page_gutenberg-widgets' ), true ) ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
add_filter( 'admin_body_class', 'gutenberg_widgets_editor_add_admin_body_classes' ); |
| 36 |
|
| 37 |
$widgets_editor_context = new WP_Block_Editor_Context(); |
| 38 |
$settings = array_merge( |
| 39 |
gutenberg_get_default_block_editor_settings(), |
| 40 |
gutenberg_get_legacy_widget_settings() |
| 41 |
); |
| 42 |
|
| 43 |
// This purposefully does not rely on apply_filters( 'block_editor_settings', $settings, null ); |
| 44 |
// Applying that filter would bring over multitude of features from the post editor, some of which |
| 45 |
// may be undesirable. Instead of using that filter, we simply pick just the settings that are needed. |
| 46 |
$settings = gutenberg_experimental_global_styles_settings( $settings ); |
| 47 |
$settings = gutenberg_extend_block_editor_styles( $settings ); |
| 48 |
|
| 49 |
gutenberg_initialize_editor( |
| 50 |
'widgets_editor', |
| 51 |
'edit-widgets', |
| 52 |
array( |
| 53 |
'preload_paths' => array( |
| 54 |
array( '/wp/v2/media', 'OPTIONS' ), |
| 55 |
'/wp/v2/sidebars?context=edit&per_page=-1', |
| 56 |
'/wp/v2/widgets?context=edit&per_page=-1&_embed=about', |
| 57 |
), |
| 58 |
'editor_settings' => $settings, |
| 59 |
) |
| 60 |
); |
| 61 |
|
| 62 |
wp_add_inline_script( |
| 63 |
'wp-blocks', |
| 64 |
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( gutenberg_get_block_categories( $widgets_editor_context ) ) ), |
| 65 |
'after' |
| 66 |
); |
| 67 |
|
| 68 |
wp_enqueue_script( 'wp-edit-widgets' ); |
| 69 |
wp_enqueue_script( 'admin-widgets' ); |
| 70 |
wp_enqueue_script( 'wp-format-library' ); |
| 71 |
wp_enqueue_style( 'wp-edit-widgets' ); |
| 72 |
wp_enqueue_style( 'wp-format-library' ); |
| 73 |
do_action( 'enqueue_block_editor_assets' ); |
| 74 |
} |
| 75 |
add_action( 'admin_enqueue_scripts', 'gutenberg_widgets_init' ); |
| 76 |
|
| 77 |
/** |
| 78 |
* Tells the script loader to load the scripts and styles of custom block on widgets editor screen. |
| 79 |
* |
| 80 |
* @param bool $is_block_editor_screen Current decision about loading block assets. |
| 81 |
* @return bool Filtered decision about loading block assets. |
| 82 |
*/ |
| 83 |
function gutenberg_widgets_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { |
| 84 |
if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { |
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
return $is_block_editor_screen; |
| 89 |
} |
| 90 |
|
| 91 |
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_widgets_editor_load_block_editor_scripts_and_styles' ); |
| 92 |
|
| 93 |
/** |
| 94 |
* Adds admin classes necessary for the block-based widgets screen. |
| 95 |
* |
| 96 |
* - Adds `block-editor-page` editor body class to allow directly styling the admin pages that are based on the block editor. |
| 97 |
* - Shows responsive embeds correctly on the widgets screen by adding the `wp-embed-responsive` class. |
| 98 |
* |
| 99 |
* @param string $classes existing admin body classes. |
| 100 |
* |
| 101 |
* @return string admin body classes including the `block-editor-page` and `wp-embed-responsive` classes. |
| 102 |
*/ |
| 103 |
function gutenberg_widgets_editor_add_admin_body_classes( $classes ) { |
| 104 |
return "$classes block-editor-page wp-embed-responsive"; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Emulates the Widgets screen `admin_print_styles` when at the block editor |
| 109 |
* screen. |
| 110 |
*/ |
| 111 |
function gutenberg_block_editor_admin_print_styles() { |
| 112 |
if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { |
| 113 |
/** This action is documented in wp-admin/admin-footer.php */ |
| 114 |
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 115 |
do_action( 'admin_print_styles-widgets.php' ); |
| 116 |
} |
| 117 |
} |
| 118 |
add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); |
| 119 |
|
| 120 |
/** |
| 121 |
* Emulates the Widgets screen `admin_print_scripts` when at the block editor |
| 122 |
* screen. |
| 123 |
*/ |
| 124 |
function gutenberg_block_editor_admin_print_scripts() { |
| 125 |
if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { |
| 126 |
/** This action is documented in wp-admin/includes/ajax-actions.php */ |
| 127 |
do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 128 |
/** This action is documented in wp-admin/includes/ajax-actions.php */ |
| 129 |
do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 130 |
/** This action is documented in wp-admin/widgets.php */ |
| 131 |
do_action( 'sidebar_admin_setup' ); |
| 132 |
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 133 |
do_action( 'admin_print_scripts-widgets.php' ); |
| 134 |
} |
| 135 |
} |
| 136 |
add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' ); |
| 137 |
|
| 138 |
/** |
| 139 |
* Emulates the Widgets screen `admin_print_footer_scripts` when at the block |
| 140 |
* editor screen. |
| 141 |
*/ |
| 142 |
function gutenberg_block_editor_admin_print_footer_scripts() { |
| 143 |
if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { |
| 144 |
/** This action is documented in wp-admin/admin-footer.php */ |
| 145 |
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 146 |
do_action( 'admin_print_footer_scripts-widgets.php' ); |
| 147 |
} |
| 148 |
} |
| 149 |
add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' ); |
| 150 |
|
| 151 |
/** |
| 152 |
* Emulates the Widgets screen `admin_footer` when at the block editor screen. |
| 153 |
*/ |
| 154 |
function gutenberg_block_editor_admin_footer() { |
| 155 |
if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) { |
| 156 |
/** This action is documented in wp-admin/admin-footer.php */ |
| 157 |
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 158 |
do_action( 'admin_footer-widgets.php' ); |
| 159 |
} |
| 160 |
} |
| 161 |
add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); |
| 162 |
|