PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / widgets-page.php

widgets-page.php in Gutenberg 12.1.0, at lib/widgets-page.php

207 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Creates an array of theme styles to load into the block editor.
25 *
26 * @since 5.8.0
27 *
28 * @global array $editor_styles
29 *
30 * @return array
31 */
32 function gutenberg_get_block_editor_theme_styles() {
33 global $editor_styles;
34
35 $styles = array(
36 array(
37 'css' => 'body { font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif }',
38 '__unstableType' => 'core',
39 ),
40 );
41 if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
42 foreach ( $editor_styles as $style ) {
43 if ( preg_match( '~^(https?:)?//~', $style ) ) {
44 $response = wp_remote_get( $style );
45 if ( ! is_wp_error( $response ) ) {
46 $styles[] = array(
47 'css' => wp_remote_retrieve_body( $response ),
48 '__unstableType' => 'theme',
49 );
50 }
51 } else {
52 $file = get_theme_file_path( $style );
53 if ( is_file( $file ) ) {
54 $styles[] = array(
55 'css' => file_get_contents( $file ),
56 'baseURL' => get_theme_file_uri( $style ),
57 '__unstableType' => 'theme',
58 );
59 }
60 }
61 }
62 }
63
64 return $styles;
65 }
66
67 /**
68 * Initialize the Gutenberg widgets page.
69 *
70 * @since 5.2.0
71 *
72 * @param string $hook Page.
73 */
74 function gutenberg_widgets_init( $hook ) {
75 if ( ! in_array( $hook, array( 'appearance_page_gutenberg-widgets' ), true ) ) {
76 return;
77 }
78
79 add_filter( 'admin_body_class', 'gutenberg_widgets_editor_add_admin_body_classes' );
80
81 $widgets_editor_context = new WP_Block_Editor_Context();
82 $settings = array_merge(
83 gutenberg_get_default_block_editor_settings(),
84 gutenberg_get_legacy_widget_settings(),
85 array( 'styles' => gutenberg_get_block_editor_theme_styles() )
86 );
87
88 // This purposefully does not rely on apply_filters( 'block_editor_settings', $settings, null );
89 // Applying that filter would bring over multitude of features from the post editor, some of which
90 // may be undesirable. Instead of using that filter, we simply pick just the settings that are needed.
91 $settings = gutenberg_experimental_global_styles_settings( $settings );
92 $settings = gutenberg_extend_block_editor_styles( $settings );
93
94 gutenberg_initialize_editor(
95 'widgets_editor',
96 'edit-widgets',
97 array(
98 'preload_paths' => array(
99 array( '/wp/v2/media', 'OPTIONS' ),
100 '/wp/v2/sidebars?context=edit&per_page=-1',
101 '/wp/v2/widgets?context=edit&per_page=-1&_embed=about',
102 ),
103 'editor_settings' => $settings,
104 )
105 );
106
107 wp_add_inline_script(
108 'wp-blocks',
109 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( gutenberg_get_block_categories( $widgets_editor_context ) ) ),
110 'after'
111 );
112
113 wp_enqueue_script( 'wp-edit-widgets' );
114 wp_enqueue_script( 'admin-widgets' );
115 wp_enqueue_script( 'wp-format-library' );
116 wp_enqueue_style( 'wp-edit-widgets' );
117 wp_enqueue_style( 'wp-format-library' );
118 do_action( 'enqueue_block_editor_assets' );
119 }
120 add_action( 'admin_enqueue_scripts', 'gutenberg_widgets_init' );
121
122 /**
123 * Tells the script loader to load the scripts and styles of custom block on widgets editor screen.
124 *
125 * @param bool $is_block_editor_screen Current decision about loading block assets.
126 * @return bool Filtered decision about loading block assets.
127 */
128 function gutenberg_widgets_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) {
129 if ( is_callable( 'get_current_screen' ) && get_current_screen() && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) {
130 return true;
131 }
132
133 return $is_block_editor_screen;
134 }
135
136 add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_widgets_editor_load_block_editor_scripts_and_styles' );
137
138 /**
139 * Adds admin classes necessary for the block-based widgets screen.
140 *
141 * - Adds `block-editor-page` editor body class to allow directly styling the admin pages that are based on the block editor.
142 * - Shows responsive embeds correctly on the widgets screen by adding the `wp-embed-responsive` class.
143 *
144 * @param string $classes existing admin body classes.
145 *
146 * @return string admin body classes including the `block-editor-page` and `wp-embed-responsive` classes.
147 */
148 function gutenberg_widgets_editor_add_admin_body_classes( $classes ) {
149 return "$classes block-editor-page wp-embed-responsive";
150 }
151
152 /**
153 * Emulates the Widgets screen `admin_print_styles` when at the block editor
154 * screen.
155 */
156 function gutenberg_block_editor_admin_print_styles() {
157 if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) {
158 /** This action is documented in wp-admin/admin-footer.php */
159 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
160 do_action( 'admin_print_styles-widgets.php' );
161 }
162 }
163 add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
164
165 /**
166 * Emulates the Widgets screen `admin_print_scripts` when at the block editor
167 * screen.
168 */
169 function gutenberg_block_editor_admin_print_scripts() {
170 if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) {
171 /** This action is documented in wp-admin/includes/ajax-actions.php */
172 do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
173 /** This action is documented in wp-admin/includes/ajax-actions.php */
174 do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
175 /** This action is documented in wp-admin/widgets.php */
176 do_action( 'sidebar_admin_setup' );
177 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
178 do_action( 'admin_print_scripts-widgets.php' );
179 }
180 }
181 add_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' );
182
183 /**
184 * Emulates the Widgets screen `admin_print_footer_scripts` when at the block
185 * editor screen.
186 */
187 function gutenberg_block_editor_admin_print_footer_scripts() {
188 if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) {
189 /** This action is documented in wp-admin/admin-footer.php */
190 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
191 do_action( 'admin_print_footer_scripts-widgets.php' );
192 }
193 }
194 add_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' );
195
196 /**
197 * Emulates the Widgets screen `admin_footer` when at the block editor screen.
198 */
199 function gutenberg_block_editor_admin_footer() {
200 if ( is_callable( 'get_current_screen' ) && 'appearance_page_gutenberg-widgets' === get_current_screen()->base ) {
201 /** This action is documented in wp-admin/admin-footer.php */
202 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
203 do_action( 'admin_footer-widgets.php' );
204 }
205 }
206 add_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' );
207