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-customize.php

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

183 lines 5.8 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 editor in the customizer.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Gutenberg's Customize Register.
10 *
11 * Adds a section to the Customizer for editing widgets with Gutenberg.
12 *
13 * @param \WP_Customize_Manager $manager An instance of the class that controls most of the Theme Customization API for WordPress 3.4 and newer.
14 */
15 function gutenberg_widgets_customize_register( $manager ) {
16 global $wp_registered_sidebars;
17
18 if ( ! gutenberg_use_widgets_block_editor() ) {
19 return;
20 }
21
22 require_once __DIR__ . '/class-wp-sidebar-block-editor-control.php';
23
24 foreach ( $manager->sections() as $section ) {
25 if ( $section instanceof WP_Customize_Sidebar_Section ) {
26 $section->description = '';
27 }
28 }
29 foreach ( $manager->controls() as $control ) {
30 if (
31 $control instanceof WP_Widget_Area_Customize_Control ||
32 $control instanceof WP_Widget_Form_Customize_Control
33 ) {
34 $manager->remove_control( $control->id );
35 }
36 }
37
38 foreach ( $wp_registered_sidebars as $sidebar_id => $sidebar ) {
39 $manager->add_setting(
40 "sidebars_widgets[$sidebar_id]",
41 array(
42 'capability' => 'edit_theme_options',
43 'transport' => 'postMessage',
44 )
45 );
46
47 $manager->add_control(
48 new WP_Sidebar_Block_Editor_Control(
49 $manager,
50 "sidebars_widgets[$sidebar_id]",
51 array(
52 'section' => "sidebar-widgets-$sidebar_id",
53 'settings' => "sidebars_widgets[$sidebar_id]",
54 'sidebar_id' => $sidebar_id,
55 'label' => $sidebar['name'],
56 'description' => $sidebar['description'],
57 )
58 )
59 );
60 }
61 }
62
63 /**
64 * Swaps the customizer setting's sanitize_callback and sanitize_js_callback
65 * arguments with our own implementation that adds raw_instance to the sanitized
66 * value. This is only done if the widget has declared that it supports raw
67 * instances via the show_instance_in_rest flag. This lets the block editor use
68 * raw_instance to create blocks.
69 *
70 * When merged to Core, these changes should be made to
71 * WP_Customize_Widgets::sanitize_widget_instance and
72 * WP_Customize_Widgets::sanitize_widget_js_instance.
73 *
74 * @param array $args Array of Customizer setting arguments.
75 * @param string $id Widget setting ID.
76 */
77 function gutenberg_widgets_customize_add_unstable_instance( $args, $id ) {
78 if ( gutenberg_use_widgets_block_editor() && preg_match( '/^widget_(?P<id_base>.+?)(?:\[(?P<widget_number>\d+)\])?$/', $id, $matches ) ) {
79 $id_base = $matches['id_base'];
80
81 $args['sanitize_callback'] = function( $value ) use ( $id_base ) {
82 global $wp_customize;
83
84 if ( isset( $value['raw_instance'] ) ) {
85 $widget_object = gutenberg_get_widget_object( $id_base );
86 if ( ! empty( $widget_object->show_instance_in_rest ) ) {
87 return $value['raw_instance'];
88 }
89 }
90
91 return $wp_customize->widgets->sanitize_widget_instance( $value );
92 };
93
94 $args['sanitize_js_callback'] = function( $value ) use ( $id_base ) {
95 global $wp_customize;
96
97 $sanitized_value = $wp_customize->widgets->sanitize_widget_js_instance( $value );
98
99 $widget_object = gutenberg_get_widget_object( $id_base );
100 if ( ! empty( $widget_object->show_instance_in_rest ) ) {
101 $sanitized_value['raw_instance'] = (object) $value;
102 }
103
104 return $sanitized_value;
105 };
106 }
107
108 return $args;
109 }
110
111 /**
112 * Initialize the Gutenberg customize widgets page.
113 */
114 function gutenberg_customize_widgets_init() {
115 if ( ! gutenberg_use_widgets_block_editor() ) {
116 return;
117 }
118
119 $customizer_context = new WP_Block_Editor_Context();
120 $settings = array_merge(
121 gutenberg_get_default_block_editor_settings(),
122 gutenberg_get_legacy_widget_settings()
123 );
124
125 // This purposefully does not rely on apply_filters( 'block_editor_settings', $settings, null );
126 // Applying that filter would bring over multitude of features from the post editor, some of which
127 // may be undesirable. Instead of using that filter, we simply pick just the settings that are needed.
128 $settings = gutenberg_experimental_global_styles_settings( $settings );
129 $settings = gutenberg_extend_block_editor_styles( $settings );
130
131 gutenberg_initialize_editor(
132 'widgets_customizer',
133 'customize-widgets',
134 array(
135 'editor_settings' => $settings,
136 )
137 );
138
139 wp_add_inline_script(
140 'wp-blocks',
141 sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( gutenberg_get_block_categories( $customizer_context ) ) ),
142 'after'
143 );
144
145 wp_enqueue_script( 'wp-customize-widgets' );
146 wp_enqueue_style( 'wp-customize-widgets' );
147 wp_enqueue_script( 'wp-format-library' );
148 wp_enqueue_style( 'wp-format-library' );
149 do_action( 'enqueue_block_editor_assets' );
150 }
151
152 /**
153 * Tells the script loader to load the scripts and styles of custom block on widgets editor screen.
154 *
155 * @param bool $is_block_editor_screen Current decision about loading block assets.
156 * @return bool Filtered decision about loading block assets.
157 */
158 function gutenberg_widgets_customize_load_block_editor_scripts_and_styles( $is_block_editor_screen ) {
159 if ( ! gutenberg_use_widgets_block_editor() ) {
160 return $is_block_editor_screen;
161 }
162
163 if ( ! is_callable( 'get_current_screen' ) ) {
164 return $is_block_editor_screen;
165 }
166
167 $current_screen = get_current_screen();
168 if ( is_null( $current_screen ) ) {
169 return $is_block_editor_screen;
170 }
171
172 return 'customize' === $current_screen->base ? true : $is_block_editor_screen;
173 }
174
175 // Test for wp_use_widgets_block_editor(), as the existence of this in core
176 // implies that the Customizer already supports the widgets block editor.
177 if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) {
178 add_action( 'customize_register', 'gutenberg_widgets_customize_register' );
179 add_filter( 'widget_customizer_setting_args', 'gutenberg_widgets_customize_add_unstable_instance', 10, 2 );
180 add_action( 'customize_controls_enqueue_scripts', 'gutenberg_customize_widgets_init' );
181 add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_widgets_customize_load_block_editor_scripts_and_styles' );
182 }
183