PluginProbe
Gutenberg / 7.3.0
Gutenberg v7.3.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 / customizer.php

customizer.php in Gutenberg 7.3.0, at lib/customizer.php

125 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Bootstraping the Gutenberg Customizer widget blocks section.
4 *
5 * Widget area edits made in the Customizer are synced to Customizer
6 * changesets as an object, encoded as a JSON string, where the keys
7 * are widget area IDs and the values are serialized block content.
8 * This file takes care of that syncing using the 2-way data binding
9 * supported by `WP_Customize_Control`s. The process is as follows:
10 *
11 * - On load, the client checks if the current changeset has
12 * widget areas that it can parse and use to hydrate the store.
13 * It will load all widget areas for the current theme, but if
14 * the changeset has content for a given area, it will replace
15 * its actual published content with the changeset's.
16 *
17 * - On edit, the client updates the 2-way bound input with a new object that maps
18 * widget area IDs and the values are serialized block content, encoded
19 * as a JSON string.
20 *
21 * - On publish, a PHP action will parse the JSON string in the
22 * changeset and update all the widget areas in it, to store the
23 * new content.
24 *
25 * @package gutenberg
26 */
27
28 /**
29 * The sanitization function for incoming values for the `gutenberg_widget_blocks` setting.
30 * It's a JSON string, so it decodes it and encodes it again to make sure it's valid.
31 *
32 * @param string $value The incoming value.
33 */
34 function gutenberg_customize_sanitize( $value ) {
35 return json_encode( json_decode( $value ) );
36 }
37
38 /**
39 * Gutenberg's Customize Register.
40 *
41 * Adds a section to the Customizer for editing widgets with Gutenberg.
42 *
43 * @param \WP_Customize_Manager $wp_customize An instance of the class that controls most of the Theme Customization API for WordPress 3.4 and newer.
44 * @since 6.1.0
45 */
46 function gutenberg_customize_register( $wp_customize ) {
47 require dirname( __FILE__ ) . '/class-wp-customize-widget-blocks-control.php';
48 $wp_customize->add_setting(
49 'gutenberg_widget_blocks',
50 array(
51 'default' => '{}',
52 'type' => 'gutenberg_widget_blocks',
53 'capability' => 'edit_theme_options',
54 'transport' => 'postMessage',
55 'sanitize_callback' => 'gutenberg_customize_sanitize',
56 )
57 );
58 if ( gutenberg_is_experiment_enabled( 'gutenberg-widget-experiments' ) ) {
59 $wp_customize->add_section(
60 'gutenberg_widget_blocks',
61 array( 'title' => __( 'Widget Blocks (Experimental)', 'gutenberg' ) )
62 );
63 $wp_customize->add_control(
64 new WP_Customize_Widget_Blocks_Control(
65 $wp_customize,
66 'gutenberg_widget_blocks',
67 array(
68 'section' => 'gutenberg_widget_blocks',
69 'settings' => 'gutenberg_widget_blocks',
70 )
71 )
72 );
73 }
74 }
75 add_action( 'customize_register', 'gutenberg_customize_register' );
76
77 /**
78 * Specifies how to save the `gutenberg_widget_blocks` setting. It parses the JSON string and updates the
79 * referenced widget areas with the new content.
80 *
81 * @param string $value The value that is being published.
82 * @param \WP_Customize_Setting $setting The setting instance.
83 */
84 function gutenberg_customize_update( $value, $setting ) {
85 foreach ( json_decode( $value ) as $sidebar_id => $sidebar_content ) {
86 $id_referenced_in_sidebar = Experimental_WP_Widget_Blocks_Manager::get_post_id_referenced_in_sidebar( $sidebar_id );
87
88 $post_id = wp_insert_post(
89 array(
90 'ID' => $id_referenced_in_sidebar,
91 'post_content' => $sidebar_content,
92 'post_type' => 'wp_area',
93 )
94 );
95
96 if ( 0 === $id_referenced_in_sidebar ) {
97 Experimental_WP_Widget_Blocks_Manager::reference_post_id_in_sidebar( $sidebar_id, $post_id );
98 }
99 }
100 }
101 add_action( 'customize_update_gutenberg_widget_blocks', 'gutenberg_customize_update', 10, 2 );
102
103 /**
104 * Filters the Customizer widget settings arguments.
105 * This is needed because the Customizer registers settings for the raw registered widgets, without going through the `sidebars_widgets` filter.
106 * The `WP_Customize_Widgets` class expects sidebars to have an array of widgets registered, not a post ID.
107 * This results in the value passed to `sanitize_js_callback` being `null` and throwing an error.
108 *
109 * TODO: Figure out why core is not running the `sidebars_widgets` filter for the relevant part of the code.
110 * Then, either fix it or change this filter to parse the post IDs and then pass them to the original `sanitize_js_callback`.
111 *
112 * @param array $args Array of Customizer setting arguments.
113 * @param string $id Widget setting ID.
114 * @return array Maybe modified array of Customizer setting arguments.
115 */
116 function filter_widget_customizer_setting_args( $args, $id = null ) {
117 // Posts won't have a settings ID like widgets. We can use that to remove the sanitization callback.
118 if ( ! isset( $id ) ) {
119 unset( $args['sanitize_js_callback'] );
120 }
121
122 return $args;
123 }
124 add_filter( 'widget_customizer_setting_args', 'filter_widget_customizer_setting_args' );
125