PluginProbe
Gutenberg / 10.2.1
Gutenberg v10.2.1
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 10.2.1, at lib/widgets-customize.php

124 lines 3.6 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 )
56 )
57 );
58 }
59 }
60
61 /**
62 * Our own implementation of WP_Customize_Widgets::sanitize_widget_instance
63 * which uses __unstable_instance if it exists.
64 *
65 * @param array $value Widget instance to sanitize.
66 * @return array|void Sanitized widget instance.
67 */
68 function gutenberg_widgets_customize_sanitize_widget_instance( $value ) {
69 global $wp_customize;
70
71 if ( isset( $value['__unstable_instance'] ) ) {
72 return $value['__unstable_instance'];
73 }
74
75 return $wp_customize->widgets->sanitize_widget_instance( $value );
76 }
77
78 /**
79 * Our own implementation of WP_Customize_Widgets::sanitize_widget_js_instance
80 * which adds __unstable_instance.
81 *
82 * @param array $value Widget instance to convert to JSON.
83 * @return array JSON-converted widget instance.
84 */
85 function gutenberg_widgets_customize_sanitize_widget_js_instance( $value ) {
86 global $wp_customize;
87
88 $sanitized_value = $wp_customize->widgets->sanitize_widget_js_instance( $value );
89
90 $sanitized_value['__unstable_instance'] = $value;
91
92 return $sanitized_value;
93 }
94
95 /**
96 * TEMPORARY HACK! \o/
97 *
98 * Swaps the customizer setting's sanitize_callback and sanitize_js_callback
99 * arguments with our own implementation that adds __unstable_instance to the
100 * sanitized value.
101 *
102 * This lets the block editor use __unstable_instance to create blocks.
103 *
104 * A proper fix would be to only add the raw instance when the widget is a block
105 * widget and to update the Legacy Widget block to work with encoded instance
106 * values. See https://github.com/WordPress/gutenberg/issues/28902.
107 *
108 * @param array $args Array of Customizer setting arguments.
109 * @param string $id Widget setting ID.
110 */
111 function gutenberg_widgets_customize_add_unstable_instance( $args, $id ) {
112 if ( 0 === strpos( $id, 'widget_' ) ) {
113 $args['sanitize_callback'] = 'gutenberg_widgets_customize_sanitize_widget_instance';
114 $args['sanitize_js_callback'] = 'gutenberg_widgets_customize_sanitize_widget_js_instance';
115 }
116
117 return $args;
118 }
119
120 if ( gutenberg_is_experiment_enabled( 'gutenberg-widgets-in-customizer' ) ) {
121 add_action( 'customize_register', 'gutenberg_widgets_customize_register' );
122 add_filter( 'widget_customizer_setting_args', 'gutenberg_widgets_customize_add_unstable_instance', 10, 2 );
123 }
124