author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
5 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Customize Widgets classes |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Customize |
|
7 |
* @since 3.9.0 |
|
8 |
*/ |
|
9 |
||
10 |
/** |
|
11 |
* Customize Widgets class. |
|
12 |
* |
|
13 |
* Implements widget management in the Customizer. |
|
14 |
* |
|
15 |
* @since 3.9.0 |
|
16 |
* |
|
17 |
* @see WP_Customize_Manager |
|
18 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
19 |
#[AllowDynamicProperties] |
5 | 20 |
final class WP_Customize_Widgets { |
21 |
||
22 |
/** |
|
23 |
* WP_Customize_Manager instance. |
|
24 |
* |
|
25 |
* @since 3.9.0 |
|
26 |
* @var WP_Customize_Manager |
|
27 |
*/ |
|
28 |
public $manager; |
|
29 |
||
30 |
/** |
|
31 |
* All id_bases for widgets defined in core. |
|
32 |
* |
|
33 |
* @since 3.9.0 |
|
34 |
* @var array |
|
35 |
*/ |
|
36 |
protected $core_widget_id_bases = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
'archives', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
'calendar', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
'categories', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
'custom_html', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
'links', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
'media_audio', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
'media_image', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
'media_video', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
'meta', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
'nav_menu', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
'pages', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
'recent-comments', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
'recent-posts', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
'rss', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
'search', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
'tag_cloud', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
'text', |
5 | 54 |
); |
55 |
||
56 |
/** |
|
57 |
* @since 3.9.0 |
|
58 |
* @var array |
|
59 |
*/ |
|
60 |
protected $rendered_sidebars = array(); |
|
61 |
||
62 |
/** |
|
63 |
* @since 3.9.0 |
|
64 |
* @var array |
|
65 |
*/ |
|
66 |
protected $rendered_widgets = array(); |
|
67 |
||
68 |
/** |
|
69 |
* @since 3.9.0 |
|
70 |
* @var array |
|
71 |
*/ |
|
72 |
protected $old_sidebars_widgets = array(); |
|
73 |
||
74 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* Mapping of widget ID base to whether it supports selective refresh. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* @var array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
protected $selective_refreshable_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
/** |
5 | 83 |
* Mapping of setting type to setting ID pattern. |
84 |
* |
|
85 |
* @since 4.2.0 |
|
86 |
* @var array |
|
87 |
*/ |
|
88 |
protected $setting_id_patterns = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
'widget_instance' => '/^widget_(?P<id_base>.+?)(?:\[(?P<widget_number>\d+)\])?$/', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
'sidebar_widgets' => '/^sidebars_widgets\[(?P<sidebar_id>.+?)\]$/', |
5 | 91 |
); |
92 |
||
93 |
/** |
|
94 |
* Initial loader. |
|
95 |
* |
|
96 |
* @since 3.9.0 |
|
97 |
* |
|
16 | 98 |
* @param WP_Customize_Manager $manager Customizer bootstrap instance. |
5 | 99 |
*/ |
100 |
public function __construct( $manager ) { |
|
101 |
$this->manager = $manager; |
|
102 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
// See https://github.com/xwp/wp-customize-snapshots/blob/962586659688a5b1fd9ae93618b7ce2d4e7a421c/php/class-customize-snapshot-manager.php#L420-L449 |
9 | 104 |
add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 ); |
105 |
add_action( 'widgets_init', array( $this, 'register_settings' ), 95 ); |
|
106 |
add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
// Skip remaining hooks when the user can't manage widgets anyway. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
|
9 | 113 |
add_action( 'wp_loaded', array( $this, 'override_sidebars_widgets_for_theme_switch' ) ); |
114 |
add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) ); |
|
115 |
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
116 |
add_action( 'customize_controls_print_styles', array( $this, 'print_styles' ) ); |
|
117 |
add_action( 'customize_controls_print_scripts', array( $this, 'print_scripts' ) ); |
|
5 | 118 |
add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_footer_scripts' ) ); |
119 |
add_action( 'customize_controls_print_footer_scripts', array( $this, 'output_widget_control_templates' ) ); |
|
9 | 120 |
add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) ); |
121 |
add_filter( 'customize_refresh_nonces', array( $this, 'refresh_nonces' ) ); |
|
18 | 122 |
add_filter( 'should_load_block_editor_scripts_and_styles', array( $this, 'should_load_block_editor_scripts_and_styles' ) ); |
5 | 123 |
|
9 | 124 |
add_action( 'dynamic_sidebar', array( $this, 'tally_rendered_widgets' ) ); |
125 |
add_filter( 'is_active_sidebar', array( $this, 'tally_sidebars_via_is_active_sidebar_calls' ), 10, 2 ); |
|
126 |
add_filter( 'dynamic_sidebar_has_widgets', array( $this, 'tally_sidebars_via_dynamic_sidebar_calls' ), 10, 2 ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
// Selective Refresh. |
9 | 129 |
add_filter( 'customize_dynamic_partial_args', array( $this, 'customize_dynamic_partial_args' ), 10, 2 ); |
130 |
add_action( 'customize_preview_init', array( $this, 'selective_refresh_init' ) ); |
|
5 | 131 |
} |
132 |
||
133 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
* List whether each registered widget can be use selective refresh. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* If the theme does not support the customize-selective-refresh-widgets feature, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* then this will always return an empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* @global WP_Widget_Factory $wp_widget_factory |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
* @return array Mapping of id_base to support. If theme doesn't support |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
* selective refresh, an empty array is returned. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
public function get_selective_refreshable_widgets() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
global $wp_widget_factory; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
return array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
if ( ! isset( $this->selective_refreshable_widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
$this->selective_refreshable_widgets = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
foreach ( $wp_widget_factory->widgets as $wp_widget ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
$this->selective_refreshable_widgets[ $wp_widget->id_base ] = ! empty( $wp_widget->widget_options['customize_selective_refresh'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
return $this->selective_refreshable_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* Determines if a widget supports selective refresh. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @param string $id_base Widget ID Base. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* @return bool Whether the widget can be selective refreshed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
public function is_widget_selective_refreshable( $id_base ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
$selective_refreshable_widgets = $this->get_selective_refreshable_widgets(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
return ! empty( $selective_refreshable_widgets[ $id_base ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
* Retrieves the widget setting type given a setting ID. |
5 | 175 |
* |
176 |
* @since 4.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* @param string $setting_id Setting ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
* @return string|void Setting type. |
5 | 180 |
*/ |
181 |
protected function get_setting_type( $setting_id ) { |
|
182 |
static $cache = array(); |
|
183 |
if ( isset( $cache[ $setting_id ] ) ) { |
|
184 |
return $cache[ $setting_id ]; |
|
185 |
} |
|
186 |
foreach ( $this->setting_id_patterns as $type => $pattern ) { |
|
187 |
if ( preg_match( $pattern, $setting_id ) ) { |
|
188 |
$cache[ $setting_id ] = $type; |
|
189 |
return $type; |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* Inspects the incoming customized data for any widget settings, and dynamically adds |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
* them up-front so widgets will be initialized properly. |
5 | 197 |
* |
198 |
* @since 4.2.0 |
|
199 |
*/ |
|
200 |
public function register_settings() { |
|
9 | 201 |
$widget_setting_ids = array(); |
5 | 202 |
$incoming_setting_ids = array_keys( $this->manager->unsanitized_post_values() ); |
203 |
foreach ( $incoming_setting_ids as $setting_id ) { |
|
204 |
if ( ! is_null( $this->get_setting_type( $setting_id ) ) ) { |
|
205 |
$widget_setting_ids[] = $setting_id; |
|
206 |
} |
|
207 |
} |
|
208 |
if ( $this->manager->doing_ajax( 'update-widget' ) && isset( $_REQUEST['widget-id'] ) ) { |
|
209 |
$widget_setting_ids[] = $this->get_setting_id( wp_unslash( $_REQUEST['widget-id'] ) ); |
|
210 |
} |
|
211 |
||
212 |
$settings = $this->manager->add_dynamic_settings( array_unique( $widget_setting_ids ) ); |
|
213 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
if ( $this->manager->settings_previewed() ) { |
5 | 215 |
foreach ( $settings as $setting ) { |
216 |
$setting->preview(); |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
* Determines the arguments for a dynamically-created setting. |
5 | 223 |
* |
224 |
* @since 4.2.0 |
|
225 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
* @param false|array $args The arguments to the WP_Customize_Setting constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
* @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. |
16 | 228 |
* @return array|false Setting arguments, false otherwise. |
5 | 229 |
*/ |
230 |
public function filter_customize_dynamic_setting_args( $args, $setting_id ) { |
|
231 |
if ( $this->get_setting_type( $setting_id ) ) { |
|
232 |
$args = $this->get_setting_args( $setting_id ); |
|
233 |
} |
|
234 |
return $args; |
|
235 |
} |
|
236 |
||
237 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
* Retrieves an unslashed post value or return a default. |
5 | 239 |
* |
240 |
* @since 3.9.0 |
|
241 |
* |
|
19 | 242 |
* @param string $name Post value. |
243 |
* @param mixed $default_value Default post value. |
|
5 | 244 |
* @return mixed Unslashed post value or default value. |
245 |
*/ |
|
19 | 246 |
protected function get_post_value( $name, $default_value = null ) { |
5 | 247 |
if ( ! isset( $_POST[ $name ] ) ) { |
19 | 248 |
return $default_value; |
5 | 249 |
} |
250 |
||
251 |
return wp_unslash( $_POST[ $name ] ); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Override sidebars_widgets for theme switch. |
|
256 |
* |
|
257 |
* When switching a theme via the Customizer, supply any previously-configured |
|
258 |
* sidebars_widgets from the target theme as the initial sidebars_widgets |
|
259 |
* setting. Also store the old theme's existing settings so that they can |
|
260 |
* be passed along for storing in the sidebars_widgets theme_mod when the |
|
261 |
* theme gets switched. |
|
262 |
* |
|
263 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
* @global array $sidebars_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
* @global array $_wp_sidebars_widgets |
5 | 267 |
*/ |
268 |
public function override_sidebars_widgets_for_theme_switch() { |
|
269 |
global $sidebars_widgets; |
|
270 |
||
271 |
if ( $this->manager->doing_ajax() || $this->manager->is_theme_active() ) { |
|
272 |
return; |
|
273 |
} |
|
274 |
||
275 |
$this->old_sidebars_widgets = wp_get_sidebars_widgets(); |
|
276 |
add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
$this->manager->set_post_value( 'old_sidebars_widgets_data', $this->old_sidebars_widgets ); // Override any value cached in changeset. |
5 | 278 |
|
16 | 279 |
// retrieve_widgets() looks at the global $sidebars_widgets. |
5 | 280 |
$sidebars_widgets = $this->old_sidebars_widgets; |
281 |
$sidebars_widgets = retrieve_widgets( 'customize' ); |
|
282 |
add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 ); |
|
16 | 283 |
// Reset global cache var used by wp_get_sidebars_widgets(). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
unset( $GLOBALS['_wp_sidebars_widgets'] ); |
5 | 285 |
} |
286 |
||
287 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
* Filters old_sidebars_widgets_data Customizer setting. |
5 | 289 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
* When switching themes, filter the Customizer setting old_sidebars_widgets_data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
* to supply initial $sidebars_widgets before they were overridden by retrieve_widgets(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
* The value for old_sidebars_widgets_data gets set in the old theme's sidebars_widgets |
5 | 293 |
* theme_mod. |
294 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
* @since 3.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
* |
5 | 297 |
* @see WP_Customize_Widgets::handle_theme_switch() |
298 |
* |
|
299 |
* @param array $old_sidebars_widgets |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
* @return array |
5 | 301 |
*/ |
302 |
public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) { |
|
303 |
return $this->old_sidebars_widgets; |
|
304 |
} |
|
305 |
||
306 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
* Filters sidebars_widgets option for theme switch. |
5 | 308 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* When switching themes, the retrieve_widgets() function is run when the Customizer initializes, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* and then the new sidebars_widgets here get supplied as the default value for the sidebars_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
* option. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
* @since 3.9.0 |
5 | 314 |
* |
315 |
* @see WP_Customize_Widgets::handle_theme_switch() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
* @global array $sidebars_widgets |
5 | 317 |
* |
318 |
* @param array $sidebars_widgets |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
* @return array |
5 | 320 |
*/ |
321 |
public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) { |
|
9 | 322 |
$sidebars_widgets = $GLOBALS['sidebars_widgets']; |
5 | 323 |
$sidebars_widgets['array_version'] = 3; |
324 |
return $sidebars_widgets; |
|
325 |
} |
|
326 |
||
327 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
* Ensures all widgets get loaded into the Customizer. |
5 | 329 |
* |
330 |
* Note: these actions are also fired in wp_ajax_update_widget(). |
|
331 |
* |
|
332 |
* @since 3.9.0 |
|
333 |
*/ |
|
334 |
public function customize_controls_init() { |
|
335 |
/** This action is documented in wp-admin/includes/ajax-actions.php */ |
|
16 | 336 |
do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
5 | 337 |
|
338 |
/** This action is documented in wp-admin/includes/ajax-actions.php */ |
|
16 | 339 |
do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
5 | 340 |
|
341 |
/** This action is documented in wp-admin/widgets.php */ |
|
342 |
do_action( 'sidebar_admin_setup' ); |
|
343 |
} |
|
344 |
||
345 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* Ensures widgets are available for all types of previews. |
5 | 347 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
* When in preview, hook to {@see 'customize_register'} for settings after WordPress is loaded |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
* so that all filters have been initialized (e.g. Widget Visibility). |
5 | 350 |
* |
351 |
* @since 3.9.0 |
|
352 |
*/ |
|
353 |
public function schedule_customize_register() { |
|
354 |
if ( is_admin() ) { |
|
355 |
$this->customize_register(); |
|
356 |
} else { |
|
357 |
add_action( 'wp', array( $this, 'customize_register' ) ); |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
* Registers Customizer settings and controls for all sidebars and widgets. |
5 | 363 |
* |
364 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
* @global array $wp_registered_widget_controls |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* @global array $wp_registered_sidebars |
5 | 369 |
*/ |
370 |
public function customize_register() { |
|
371 |
global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_sidebars; |
|
372 |
||
18 | 373 |
$use_widgets_block_editor = wp_use_widgets_block_editor(); |
374 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
|
5 | 377 |
$sidebars_widgets = array_merge( |
378 |
array( 'wp_inactive_widgets' => array() ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
array_fill_keys( array_keys( $wp_registered_sidebars ), array() ), |
5 | 380 |
wp_get_sidebars_widgets() |
381 |
); |
|
382 |
||
383 |
$new_setting_ids = array(); |
|
384 |
||
385 |
/* |
|
386 |
* Register a setting for all widgets, including those which are active, |
|
387 |
* inactive, and orphaned since a widget may get suppressed from a sidebar |
|
388 |
* via a plugin (like Widget Visibility). |
|
389 |
*/ |
|
390 |
foreach ( array_keys( $wp_registered_widgets ) as $widget_id ) { |
|
391 |
$setting_id = $this->get_setting_id( $widget_id ); |
|
392 |
$setting_args = $this->get_setting_args( $setting_id ); |
|
393 |
if ( ! $this->manager->get_setting( $setting_id ) ) { |
|
394 |
$this->manager->add_setting( $setting_id, $setting_args ); |
|
395 |
} |
|
396 |
$new_setting_ids[] = $setting_id; |
|
397 |
} |
|
398 |
||
399 |
/* |
|
400 |
* Add a setting which will be supplied for the theme's sidebars_widgets |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
* theme_mod when the theme is switched. |
5 | 402 |
*/ |
403 |
if ( ! $this->manager->is_theme_active() ) { |
|
9 | 404 |
$setting_id = 'old_sidebars_widgets_data'; |
405 |
$setting_args = $this->get_setting_args( |
|
406 |
$setting_id, |
|
407 |
array( |
|
408 |
'type' => 'global_variable', |
|
409 |
'dirty' => true, |
|
410 |
) |
|
411 |
); |
|
5 | 412 |
$this->manager->add_setting( $setting_id, $setting_args ); |
413 |
} |
|
414 |
||
9 | 415 |
$this->manager->add_panel( |
416 |
'widgets', |
|
417 |
array( |
|
418 |
'type' => 'widgets', |
|
419 |
'title' => __( 'Widgets' ), |
|
420 |
'description' => __( 'Widgets are independent sections of content that can be placed into widgetized areas provided by your theme (commonly called sidebars).' ), |
|
421 |
'priority' => 110, |
|
422 |
'active_callback' => array( $this, 'is_panel_active' ), |
|
423 |
'auto_expand_sole_section' => true, |
|
19 | 424 |
'theme_supports' => 'widgets', |
9 | 425 |
) |
426 |
); |
|
5 | 427 |
|
428 |
foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) { |
|
429 |
if ( empty( $sidebar_widget_ids ) ) { |
|
430 |
$sidebar_widget_ids = array(); |
|
431 |
} |
|
432 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
$is_registered_sidebar = is_registered_sidebar( $sidebar_id ); |
5 | 434 |
$is_inactive_widgets = ( 'wp_inactive_widgets' === $sidebar_id ); |
435 |
$is_active_sidebar = ( $is_registered_sidebar && ! $is_inactive_widgets ); |
|
436 |
||
437 |
// Add setting for managing the sidebar's widgets. |
|
438 |
if ( $is_registered_sidebar || $is_inactive_widgets ) { |
|
439 |
$setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id ); |
|
440 |
$setting_args = $this->get_setting_args( $setting_id ); |
|
441 |
if ( ! $this->manager->get_setting( $setting_id ) ) { |
|
442 |
if ( ! $this->manager->is_theme_active() ) { |
|
443 |
$setting_args['dirty'] = true; |
|
444 |
} |
|
445 |
$this->manager->add_setting( $setting_id, $setting_args ); |
|
446 |
} |
|
447 |
$new_setting_ids[] = $setting_id; |
|
448 |
||
449 |
// Add section to contain controls. |
|
450 |
$section_id = sprintf( 'sidebar-widgets-%s', $sidebar_id ); |
|
451 |
if ( $is_active_sidebar ) { |
|
452 |
||
453 |
$section_args = array( |
|
18 | 454 |
'title' => $wp_registered_sidebars[ $sidebar_id ]['name'], |
455 |
'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ), true ), |
|
456 |
'panel' => 'widgets', |
|
457 |
'sidebar_id' => $sidebar_id, |
|
5 | 458 |
); |
459 |
||
18 | 460 |
if ( $use_widgets_block_editor ) { |
461 |
$section_args['description'] = ''; |
|
462 |
} else { |
|
463 |
$section_args['description'] = $wp_registered_sidebars[ $sidebar_id ]['description']; |
|
464 |
} |
|
465 |
||
5 | 466 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
* Filters Customizer widget section arguments for a given sidebar. |
5 | 468 |
* |
469 |
* @since 3.9.0 |
|
470 |
* |
|
471 |
* @param array $section_args Array of Customizer widget section arguments. |
|
472 |
* @param string $section_id Customizer section ID. |
|
473 |
* @param int|string $sidebar_id Sidebar ID. |
|
474 |
*/ |
|
475 |
$section_args = apply_filters( 'customizer_widgets_section_args', $section_args, $section_id, $sidebar_id ); |
|
476 |
||
477 |
$section = new WP_Customize_Sidebar_Section( $this->manager, $section_id, $section_args ); |
|
478 |
$this->manager->add_section( $section ); |
|
479 |
||
18 | 480 |
if ( $use_widgets_block_editor ) { |
481 |
$control = new WP_Sidebar_Block_Editor_Control( |
|
482 |
$this->manager, |
|
483 |
$setting_id, |
|
484 |
array( |
|
485 |
'section' => $section_id, |
|
486 |
'sidebar_id' => $sidebar_id, |
|
487 |
'label' => $section_args['title'], |
|
488 |
'description' => $section_args['description'], |
|
489 |
) |
|
490 |
); |
|
491 |
} else { |
|
492 |
$control = new WP_Widget_Area_Customize_Control( |
|
493 |
$this->manager, |
|
494 |
$setting_id, |
|
495 |
array( |
|
496 |
'section' => $section_id, |
|
497 |
'sidebar_id' => $sidebar_id, |
|
498 |
'priority' => count( $sidebar_widget_ids ), // place 'Add Widget' and 'Reorder' buttons at end. |
|
499 |
) |
|
500 |
); |
|
501 |
} |
|
5 | 502 |
|
503 |
$this->manager->add_control( $control ); |
|
18 | 504 |
|
505 |
$new_setting_ids[] = $setting_id; |
|
5 | 506 |
} |
507 |
} |
|
508 |
||
18 | 509 |
if ( ! $use_widgets_block_editor ) { |
510 |
// Add a control for each active widget (located in a sidebar). |
|
511 |
foreach ( $sidebar_widget_ids as $i => $widget_id ) { |
|
5 | 512 |
|
18 | 513 |
// Skip widgets that may have gone away due to a plugin being deactivated. |
514 |
if ( ! $is_active_sidebar || ! isset( $wp_registered_widgets[ $widget_id ] ) ) { |
|
515 |
continue; |
|
516 |
} |
|
5 | 517 |
|
18 | 518 |
$registered_widget = $wp_registered_widgets[ $widget_id ]; |
519 |
$setting_id = $this->get_setting_id( $widget_id ); |
|
520 |
$id_base = $wp_registered_widget_controls[ $widget_id ]['id_base']; |
|
5 | 521 |
|
18 | 522 |
$control = new WP_Widget_Form_Customize_Control( |
523 |
$this->manager, |
|
524 |
$setting_id, |
|
525 |
array( |
|
526 |
'label' => $registered_widget['name'], |
|
527 |
'section' => $section_id, |
|
528 |
'sidebar_id' => $sidebar_id, |
|
529 |
'widget_id' => $widget_id, |
|
530 |
'widget_id_base' => $id_base, |
|
531 |
'priority' => $i, |
|
532 |
'width' => $wp_registered_widget_controls[ $widget_id ]['width'], |
|
533 |
'height' => $wp_registered_widget_controls[ $widget_id ]['height'], |
|
534 |
'is_wide' => $this->is_wide_widget( $widget_id ), |
|
535 |
) |
|
536 |
); |
|
537 |
$this->manager->add_control( $control ); |
|
538 |
} |
|
5 | 539 |
} |
540 |
} |
|
541 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
if ( $this->manager->settings_previewed() ) { |
5 | 543 |
foreach ( $new_setting_ids as $new_setting_id ) { |
544 |
$this->manager->get_setting( $new_setting_id )->preview(); |
|
545 |
} |
|
546 |
} |
|
547 |
} |
|
548 |
||
549 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
550 |
* Determines whether the widgets panel is active, based on whether there are sidebars registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
552 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
553 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
* @see WP_Customize_Panel::$active_callback |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
* @global array $wp_registered_sidebars |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
* @return bool Active. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
public function is_panel_active() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
global $wp_registered_sidebars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
return ! empty( $wp_registered_sidebars ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
* Converts a widget_id into its corresponding Customizer setting ID (option name). |
5 | 566 |
* |
567 |
* @since 3.9.0 |
|
568 |
* |
|
569 |
* @param string $widget_id Widget ID. |
|
570 |
* @return string Maybe-parsed widget ID. |
|
571 |
*/ |
|
572 |
public function get_setting_id( $widget_id ) { |
|
573 |
$parsed_widget_id = $this->parse_widget_id( $widget_id ); |
|
574 |
$setting_id = sprintf( 'widget_%s', $parsed_widget_id['id_base'] ); |
|
575 |
||
576 |
if ( ! is_null( $parsed_widget_id['number'] ) ) { |
|
577 |
$setting_id .= sprintf( '[%d]', $parsed_widget_id['number'] ); |
|
578 |
} |
|
579 |
return $setting_id; |
|
580 |
} |
|
581 |
||
582 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
* Determines whether the widget is considered "wide". |
5 | 584 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
* Core widgets which may have controls wider than 250, but can still be shown |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
* in the narrow Customizer panel. The RSS and Text widgets in Core, for example, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
* have widths of 400 and yet they still render fine in the Customizer panel. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
589 |
* This method will return all Core widgets as being not wide, but this can be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
590 |
* overridden with the {@see 'is_wide_widget_in_customizer'} filter. |
5 | 591 |
* |
592 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
* |
16 | 594 |
* @global array $wp_registered_widget_controls |
5 | 595 |
* |
596 |
* @param string $widget_id Widget ID. |
|
597 |
* @return bool Whether or not the widget is a "wide" widget. |
|
598 |
*/ |
|
599 |
public function is_wide_widget( $widget_id ) { |
|
600 |
global $wp_registered_widget_controls; |
|
601 |
||
602 |
$parsed_widget_id = $this->parse_widget_id( $widget_id ); |
|
9 | 603 |
$width = $wp_registered_widget_controls[ $widget_id ]['width']; |
16 | 604 |
$is_core = in_array( $parsed_widget_id['id_base'], $this->core_widget_id_bases, true ); |
5 | 605 |
$is_wide = ( $width > 250 && ! $is_core ); |
606 |
||
607 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
608 |
* Filters whether the given widget is considered "wide". |
5 | 609 |
* |
610 |
* @since 3.9.0 |
|
611 |
* |
|
612 |
* @param bool $is_wide Whether the widget is wide, Default false. |
|
613 |
* @param string $widget_id Widget ID. |
|
614 |
*/ |
|
615 |
return apply_filters( 'is_wide_widget_in_customizer', $is_wide, $widget_id ); |
|
616 |
} |
|
617 |
||
618 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
* Converts a widget ID into its id_base and number components. |
5 | 620 |
* |
621 |
* @since 3.9.0 |
|
622 |
* |
|
623 |
* @param string $widget_id Widget ID. |
|
624 |
* @return array Array containing a widget's id_base and number components. |
|
625 |
*/ |
|
626 |
public function parse_widget_id( $widget_id ) { |
|
627 |
$parsed = array( |
|
9 | 628 |
'number' => null, |
5 | 629 |
'id_base' => null, |
630 |
); |
|
631 |
||
632 |
if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $matches ) ) { |
|
633 |
$parsed['id_base'] = $matches[1]; |
|
18 | 634 |
$parsed['number'] = (int) $matches[2]; |
5 | 635 |
} else { |
16 | 636 |
// Likely an old single widget. |
5 | 637 |
$parsed['id_base'] = $widget_id; |
638 |
} |
|
639 |
return $parsed; |
|
640 |
} |
|
641 |
||
642 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
643 |
* Converts a widget setting ID (option path) to its id_base and number components. |
5 | 644 |
* |
645 |
* @since 3.9.0 |
|
646 |
* |
|
647 |
* @param string $setting_id Widget setting ID. |
|
16 | 648 |
* @return array|WP_Error Array containing a widget's id_base and number components, |
5 | 649 |
* or a WP_Error object. |
650 |
*/ |
|
651 |
public function parse_widget_setting_id( $setting_id ) { |
|
652 |
if ( ! preg_match( '/^(widget_(.+?))(?:\[(\d+)\])?$/', $setting_id, $matches ) ) { |
|
653 |
return new WP_Error( 'widget_setting_invalid_id' ); |
|
654 |
} |
|
655 |
||
656 |
$id_base = $matches[2]; |
|
18 | 657 |
$number = isset( $matches[3] ) ? (int) $matches[3] : null; |
5 | 658 |
|
659 |
return compact( 'id_base', 'number' ); |
|
660 |
} |
|
661 |
||
662 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
* Calls admin_print_styles-widgets.php and admin_print_styles hooks to |
5 | 664 |
* allow custom styles from plugins. |
665 |
* |
|
666 |
* @since 3.9.0 |
|
667 |
*/ |
|
668 |
public function print_styles() { |
|
669 |
/** This action is documented in wp-admin/admin-header.php */ |
|
16 | 670 |
do_action( 'admin_print_styles-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
5 | 671 |
|
672 |
/** This action is documented in wp-admin/admin-header.php */ |
|
673 |
do_action( 'admin_print_styles' ); |
|
674 |
} |
|
675 |
||
676 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
677 |
* Calls admin_print_scripts-widgets.php and admin_print_scripts hooks to |
5 | 678 |
* allow custom scripts from plugins. |
679 |
* |
|
680 |
* @since 3.9.0 |
|
681 |
*/ |
|
682 |
public function print_scripts() { |
|
683 |
/** This action is documented in wp-admin/admin-header.php */ |
|
16 | 684 |
do_action( 'admin_print_scripts-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
5 | 685 |
|
686 |
/** This action is documented in wp-admin/admin-header.php */ |
|
687 |
do_action( 'admin_print_scripts' ); |
|
688 |
} |
|
689 |
||
690 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
* Enqueues scripts and styles for Customizer panel and export data to JavaScript. |
5 | 692 |
* |
693 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
694 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
* @global WP_Scripts $wp_scripts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
* @global array $wp_registered_sidebars |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
* @global array $wp_registered_widgets |
5 | 698 |
*/ |
699 |
public function enqueue_scripts() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
global $wp_scripts, $wp_registered_sidebars, $wp_registered_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
|
5 | 702 |
wp_enqueue_style( 'customize-widgets' ); |
703 |
wp_enqueue_script( 'customize-widgets' ); |
|
704 |
||
705 |
/** This action is documented in wp-admin/admin-header.php */ |
|
706 |
do_action( 'admin_enqueue_scripts', 'widgets.php' ); |
|
707 |
||
708 |
/* |
|
709 |
* Export available widgets with control_tpl removed from model |
|
710 |
* since plugins need templates to be in the DOM. |
|
711 |
*/ |
|
712 |
$available_widgets = array(); |
|
713 |
||
714 |
foreach ( $this->get_available_widgets() as $available_widget ) { |
|
715 |
unset( $available_widget['control_tpl'] ); |
|
716 |
$available_widgets[] = $available_widget; |
|
717 |
} |
|
718 |
||
719 |
$widget_reorder_nav_tpl = sprintf( |
|
720 |
'<div class="widget-reorder-nav"><span class="move-widget" tabindex="0">%1$s</span><span class="move-widget-down" tabindex="0">%2$s</span><span class="move-widget-up" tabindex="0">%3$s</span></div>', |
|
721 |
__( 'Move to another area…' ), |
|
722 |
__( 'Move down' ), |
|
723 |
__( 'Move up' ) |
|
724 |
); |
|
725 |
||
726 |
$move_widget_area_tpl = str_replace( |
|
727 |
array( '{description}', '{btn}' ), |
|
728 |
array( |
|
729 |
__( 'Select an area to move this widget into:' ), |
|
730 |
_x( 'Move', 'Move widget' ), |
|
731 |
), |
|
732 |
'<div class="move-widget-area"> |
|
733 |
<p class="description">{description}</p> |
|
734 |
<ul class="widget-area-select"> |
|
735 |
<% _.each( sidebars, function ( sidebar ){ %> |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
736 |
<li class="" data-id="<%- sidebar.id %>" tabindex="0"> |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
737 |
<div><strong><%- sidebar.name %></strong></div> |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
738 |
<div><%- sidebar.description %></div> |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
739 |
</li> |
5 | 740 |
<% }); %> |
741 |
</ul> |
|
742 |
<div class="move-widget-actions"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
743 |
<button class="move-widget-btn button" type="button">{btn}</button> |
5 | 744 |
</div> |
745 |
</div>' |
|
746 |
); |
|
747 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
748 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
749 |
* Gather all strings in PHP that may be needed by JS on the client. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
750 |
* Once JS i18n is implemented (in #20491), this can be removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
751 |
*/ |
9 | 752 |
$some_non_rendered_areas_messages = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
753 |
$some_non_rendered_areas_messages[1] = html_entity_decode( |
19 | 754 |
__( 'Your theme has 1 other widget area, but this particular page does not display it.' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
755 |
ENT_QUOTES, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
756 |
get_bloginfo( 'charset' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
); |
9 | 758 |
$registered_sidebar_count = count( $wp_registered_sidebars ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
759 |
for ( $non_rendered_count = 2; $non_rendered_count < $registered_sidebar_count; $non_rendered_count++ ) { |
9 | 760 |
$some_non_rendered_areas_messages[ $non_rendered_count ] = html_entity_decode( |
761 |
sprintf( |
|
16 | 762 |
/* translators: %s: The number of other widget areas registered but not rendered. */ |
9 | 763 |
_n( |
19 | 764 |
'Your theme has %s other widget area, but this particular page does not display it.', |
765 |
'Your theme has %s other widget areas, but this particular page does not display them.', |
|
9 | 766 |
$non_rendered_count |
767 |
), |
|
768 |
number_format_i18n( $non_rendered_count ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
), |
9 | 770 |
ENT_QUOTES, |
771 |
get_bloginfo( 'charset' ) |
|
772 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
773 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
774 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
775 |
if ( 1 === $registered_sidebar_count ) { |
9 | 776 |
$no_areas_shown_message = html_entity_decode( |
777 |
sprintf( |
|
19 | 778 |
__( 'Your theme has 1 widget area, but this particular page does not display it.' ) |
9 | 779 |
), |
780 |
ENT_QUOTES, |
|
781 |
get_bloginfo( 'charset' ) |
|
782 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
} else { |
9 | 784 |
$no_areas_shown_message = html_entity_decode( |
785 |
sprintf( |
|
16 | 786 |
/* translators: %s: The total number of widget areas registered. */ |
9 | 787 |
_n( |
19 | 788 |
'Your theme has %s widget area, but this particular page does not display it.', |
789 |
'Your theme has %s widget areas, but this particular page does not display them.', |
|
9 | 790 |
$registered_sidebar_count |
791 |
), |
|
792 |
number_format_i18n( $registered_sidebar_count ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
), |
9 | 794 |
ENT_QUOTES, |
795 |
get_bloginfo( 'charset' ) |
|
796 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
797 |
} |
5 | 798 |
|
799 |
$settings = array( |
|
9 | 800 |
'registeredSidebars' => array_values( $wp_registered_sidebars ), |
801 |
'registeredWidgets' => $wp_registered_widgets, |
|
16 | 802 |
'availableWidgets' => $available_widgets, // @todo Merge this with registered_widgets. |
9 | 803 |
'l10n' => array( |
5 | 804 |
'saveBtnLabel' => __( 'Apply' ), |
805 |
'saveBtnTooltip' => __( 'Save and preview changes before publishing them.' ), |
|
806 |
'removeBtnLabel' => __( 'Remove' ), |
|
9 | 807 |
'removeBtnTooltip' => __( 'Keep widget settings and move it to the inactive widgets' ), |
5 | 808 |
'error' => __( 'An error has occurred. Please reload the page and try again.' ), |
809 |
'widgetMovedUp' => __( 'Widget moved up' ), |
|
810 |
'widgetMovedDown' => __( 'Widget moved down' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
811 |
'navigatePreview' => __( 'You can navigate to other pages on your site while using the Customizer to view and edit the widgets displayed on those pages.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
812 |
'someAreasShown' => $some_non_rendered_areas_messages, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
'noAreasShown' => $no_areas_shown_message, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
814 |
'reorderModeOn' => __( 'Reorder mode enabled' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
'reorderModeOff' => __( 'Reorder mode closed' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
'reorderLabelOn' => esc_attr__( 'Reorder widgets' ), |
16 | 817 |
/* translators: %d: The number of widgets found. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
818 |
'widgetsFound' => __( 'Number of widgets found: %d' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
819 |
'noWidgetsFound' => __( 'No widgets found.' ), |
5 | 820 |
), |
9 | 821 |
'tpl' => array( |
5 | 822 |
'widgetReorderNav' => $widget_reorder_nav_tpl, |
823 |
'moveWidgetArea' => $move_widget_area_tpl, |
|
824 |
), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(), |
5 | 826 |
); |
827 |
||
828 |
foreach ( $settings['registeredWidgets'] as &$registered_widget ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
829 |
unset( $registered_widget['callback'] ); // May not be JSON-serializable. |
5 | 830 |
} |
831 |
||
832 |
$wp_scripts->add_data( |
|
833 |
'customize-widgets', |
|
834 |
'data', |
|
835 |
sprintf( 'var _wpCustomizeWidgetsSettings = %s;', wp_json_encode( $settings ) ) |
|
836 |
); |
|
18 | 837 |
|
838 |
/* |
|
839 |
* TODO: Update 'wp-customize-widgets' to not rely so much on things in |
|
840 |
* 'customize-widgets'. This will let us skip most of the above and not |
|
841 |
* enqueue 'customize-widgets' which saves bytes. |
|
842 |
*/ |
|
843 |
||
844 |
if ( wp_use_widgets_block_editor() ) { |
|
19 | 845 |
$block_editor_context = new WP_Block_Editor_Context( |
846 |
array( |
|
847 |
'name' => 'core/customize-widgets', |
|
848 |
) |
|
849 |
); |
|
18 | 850 |
|
851 |
$editor_settings = get_block_editor_settings( |
|
852 |
get_legacy_widget_block_editor_settings(), |
|
853 |
$block_editor_context |
|
854 |
); |
|
855 |
||
856 |
wp_add_inline_script( |
|
857 |
'wp-customize-widgets', |
|
858 |
sprintf( |
|
859 |
'wp.domReady( function() { |
|
860 |
wp.customizeWidgets.initialize( "widgets-customizer", %s ); |
|
861 |
} );', |
|
862 |
wp_json_encode( $editor_settings ) |
|
863 |
) |
|
864 |
); |
|
865 |
||
866 |
// Preload server-registered block schemas. |
|
867 |
wp_add_inline_script( |
|
868 |
'wp-blocks', |
|
869 |
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');' |
|
870 |
); |
|
871 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
872 |
// Preload server-registered block bindings sources. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
873 |
$registered_sources = get_all_registered_block_bindings_sources(); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
874 |
if ( ! empty( $registered_sources ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
875 |
$filtered_sources = array(); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
876 |
foreach ( $registered_sources as $source ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
877 |
$filtered_sources[] = array( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
878 |
'name' => $source->name, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
879 |
'label' => $source->label, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
880 |
'usesContext' => $source->uses_context, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
881 |
); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
882 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
883 |
$script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
884 |
wp_add_inline_script( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
885 |
'wp-blocks', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
886 |
$script |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
887 |
); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
888 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
889 |
|
18 | 890 |
wp_add_inline_script( |
891 |
'wp-blocks', |
|
892 |
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ), |
|
893 |
'after' |
|
894 |
); |
|
895 |
||
896 |
wp_enqueue_script( 'wp-customize-widgets' ); |
|
897 |
wp_enqueue_style( 'wp-customize-widgets' ); |
|
898 |
||
899 |
/** This action is documented in edit-form-blocks.php */ |
|
900 |
do_action( 'enqueue_block_editor_assets' ); |
|
901 |
} |
|
5 | 902 |
} |
903 |
||
904 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
905 |
* Renders the widget form control templates into the DOM. |
5 | 906 |
* |
907 |
* @since 3.9.0 |
|
908 |
*/ |
|
909 |
public function output_widget_control_templates() { |
|
910 |
?> |
|
911 |
<div id="widgets-left"><!-- compatibility with JS which looks for widget templates here --> |
|
912 |
<div id="available-widgets"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
913 |
<div class="customize-section-title"> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
<button class="customize-section-back" tabindex="-1"> |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
915 |
<span class="screen-reader-text"> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
916 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
917 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
918 |
_e( 'Back' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
919 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
920 |
</span> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
921 |
</button> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
922 |
<h3> |
9 | 923 |
<span class="customize-action"> |
924 |
<?php |
|
16 | 925 |
/* translators: ▸ is the unicode right-pointing triangle. %s: Section title in the Customizer. */ |
926 |
printf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'widgets' )->title ) ); |
|
9 | 927 |
?> |
928 |
</span> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
<?php _e( 'Add a Widget' ); ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
</h3> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
</div> |
5 | 932 |
<div id="available-widgets-filter"> |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
933 |
<label for="widgets-search"> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
934 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
935 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
936 |
_e( 'Search Widgets' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
937 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
938 |
</label> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
939 |
<input type="text" id="widgets-search" aria-describedby="widgets-search-desc" /> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
940 |
<div class="search-icon" aria-hidden="true"></div> |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
941 |
<button type="button" class="clear-results"><span class="screen-reader-text"> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
942 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
943 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
944 |
_e( 'Clear Results' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
945 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
946 |
</span></button> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
947 |
<p class="screen-reader-text" id="widgets-search-desc"> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
948 |
<?php |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
949 |
/* translators: Hidden accessibility text. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
950 |
_e( 'The search results will be updated as you type.' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
951 |
?> |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
952 |
</p> |
5 | 953 |
</div> |
954 |
<div id="available-widgets-list"> |
|
9 | 955 |
<?php foreach ( $this->get_available_widgets() as $available_widget ) : ?> |
956 |
<div id="widget-tpl-<?php echo esc_attr( $available_widget['id'] ); ?>" data-widget-id="<?php echo esc_attr( $available_widget['id'] ); ?>" class="widget-tpl <?php echo esc_attr( $available_widget['id'] ); ?>" tabindex="0"> |
|
5 | 957 |
<?php echo $available_widget['control_tpl']; ?> |
958 |
</div> |
|
959 |
<?php endforeach; ?> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
<p class="no-widgets-found-message"><?php _e( 'No widgets found.' ); ?></p> |
5 | 961 |
</div><!-- #available-widgets-list --> |
962 |
</div><!-- #available-widgets --> |
|
963 |
</div><!-- #widgets-left --> |
|
964 |
<?php |
|
965 |
} |
|
966 |
||
967 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
* Calls admin_print_footer_scripts and admin_print_scripts hooks to |
5 | 969 |
* allow custom scripts from plugins. |
970 |
* |
|
971 |
* @since 3.9.0 |
|
972 |
*/ |
|
973 |
public function print_footer_scripts() { |
|
974 |
/** This action is documented in wp-admin/admin-footer.php */ |
|
16 | 975 |
do_action( 'admin_print_footer_scripts-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
/** This action is documented in wp-admin/admin-footer.php */ |
5 | 978 |
do_action( 'admin_print_footer_scripts' ); |
979 |
||
980 |
/** This action is documented in wp-admin/admin-footer.php */ |
|
16 | 981 |
do_action( 'admin_footer-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
5 | 982 |
} |
983 |
||
984 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
985 |
* Retrieves common arguments to supply when constructing a Customizer setting. |
5 | 986 |
* |
987 |
* @since 3.9.0 |
|
988 |
* |
|
989 |
* @param string $id Widget setting ID. |
|
990 |
* @param array $overrides Array of setting overrides. |
|
991 |
* @return array Possibly modified setting arguments. |
|
992 |
*/ |
|
993 |
public function get_setting_args( $id, $overrides = array() ) { |
|
994 |
$args = array( |
|
995 |
'type' => 'option', |
|
996 |
'capability' => 'edit_theme_options', |
|
997 |
'default' => array(), |
|
998 |
); |
|
999 |
||
1000 |
if ( preg_match( $this->setting_id_patterns['sidebar_widgets'], $id, $matches ) ) { |
|
9 | 1001 |
$args['sanitize_callback'] = array( $this, 'sanitize_sidebar_widgets' ); |
5 | 1002 |
$args['sanitize_js_callback'] = array( $this, 'sanitize_sidebar_widgets_js_instance' ); |
9 | 1003 |
$args['transport'] = current_theme_supports( 'customize-selective-refresh-widgets' ) ? 'postMessage' : 'refresh'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
} elseif ( preg_match( $this->setting_id_patterns['widget_instance'], $id, $matches ) ) { |
18 | 1005 |
$id_base = $matches['id_base']; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1006 |
$args['sanitize_callback'] = function ( $value ) use ( $id_base ) { |
18 | 1007 |
return $this->sanitize_widget_instance( $value, $id_base ); |
1008 |
}; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1009 |
$args['sanitize_js_callback'] = function ( $value ) use ( $id_base ) { |
18 | 1010 |
return $this->sanitize_widget_js_instance( $value, $id_base ); |
1011 |
}; |
|
9 | 1012 |
$args['transport'] = $this->is_widget_selective_refreshable( $matches['id_base'] ) ? 'postMessage' : 'refresh'; |
5 | 1013 |
} |
1014 |
||
1015 |
$args = array_merge( $args, $overrides ); |
|
1016 |
||
1017 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1018 |
* Filters the common arguments supplied when constructing a Customizer setting. |
5 | 1019 |
* |
1020 |
* @since 3.9.0 |
|
1021 |
* |
|
1022 |
* @see WP_Customize_Setting |
|
1023 |
* |
|
1024 |
* @param array $args Array of Customizer setting arguments. |
|
1025 |
* @param string $id Widget setting ID. |
|
1026 |
*/ |
|
1027 |
return apply_filters( 'widget_customizer_setting_args', $args, $id ); |
|
1028 |
} |
|
1029 |
||
1030 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1031 |
* Ensures sidebar widget arrays only ever contain widget IDS. |
5 | 1032 |
* |
1033 |
* Used as the 'sanitize_callback' for each $sidebars_widgets setting. |
|
1034 |
* |
|
1035 |
* @since 3.9.0 |
|
1036 |
* |
|
9 | 1037 |
* @param string[] $widget_ids Array of widget IDs. |
1038 |
* @return string[] Array of sanitized widget IDs. |
|
5 | 1039 |
*/ |
1040 |
public function sanitize_sidebar_widgets( $widget_ids ) { |
|
9 | 1041 |
$widget_ids = array_map( 'strval', (array) $widget_ids ); |
5 | 1042 |
$sanitized_widget_ids = array(); |
1043 |
foreach ( $widget_ids as $widget_id ) { |
|
1044 |
$sanitized_widget_ids[] = preg_replace( '/[^a-z0-9_\-]/', '', $widget_id ); |
|
1045 |
} |
|
1046 |
return $sanitized_widget_ids; |
|
1047 |
} |
|
1048 |
||
1049 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1050 |
* Builds up an index of all available widgets for use in Backbone models. |
5 | 1051 |
* |
1052 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1054 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1055 |
* @global array $wp_registered_widget_controls |
5 | 1056 |
* |
1057 |
* @see wp_list_widgets() |
|
1058 |
* |
|
1059 |
* @return array List of available widgets. |
|
1060 |
*/ |
|
1061 |
public function get_available_widgets() { |
|
1062 |
static $available_widgets = array(); |
|
1063 |
if ( ! empty( $available_widgets ) ) { |
|
1064 |
return $available_widgets; |
|
1065 |
} |
|
1066 |
||
1067 |
global $wp_registered_widgets, $wp_registered_widget_controls; |
|
16 | 1068 |
require_once ABSPATH . 'wp-admin/includes/widgets.php'; // For next_widget_id_number(). |
5 | 1069 |
|
1070 |
$sort = $wp_registered_widgets; |
|
1071 |
usort( $sort, array( $this, '_sort_name_callback' ) ); |
|
1072 |
$done = array(); |
|
1073 |
||
1074 |
foreach ( $sort as $widget ) { |
|
16 | 1075 |
if ( in_array( $widget['callback'], $done, true ) ) { // We already showed this multi-widget. |
5 | 1076 |
continue; |
1077 |
} |
|
1078 |
||
1079 |
$sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false ); |
|
1080 |
$done[] = $widget['callback']; |
|
1081 |
||
1082 |
if ( ! isset( $widget['params'][0] ) ) { |
|
1083 |
$widget['params'][0] = array(); |
|
1084 |
} |
|
1085 |
||
1086 |
$available_widget = $widget; |
|
16 | 1087 |
unset( $available_widget['callback'] ); // Not serializable to JSON. |
5 | 1088 |
|
1089 |
$args = array( |
|
1090 |
'widget_id' => $widget['id'], |
|
1091 |
'widget_name' => $widget['name'], |
|
1092 |
'_display' => 'template', |
|
1093 |
); |
|
1094 |
||
1095 |
$is_disabled = false; |
|
9 | 1096 |
$is_multi_widget = ( isset( $wp_registered_widget_controls[ $widget['id'] ]['id_base'] ) && isset( $widget['params'][0]['number'] ) ); |
5 | 1097 |
if ( $is_multi_widget ) { |
9 | 1098 |
$id_base = $wp_registered_widget_controls[ $widget['id'] ]['id_base']; |
5 | 1099 |
$args['_temp_id'] = "$id_base-__i__"; |
1100 |
$args['_multi_num'] = next_widget_id_number( $id_base ); |
|
1101 |
$args['_add'] = 'multi'; |
|
1102 |
} else { |
|
1103 |
$args['_add'] = 'single'; |
|
1104 |
||
1105 |
if ( $sidebar && 'wp_inactive_widgets' !== $sidebar ) { |
|
1106 |
$is_disabled = true; |
|
1107 |
} |
|
1108 |
$id_base = $widget['id']; |
|
1109 |
} |
|
1110 |
||
9 | 1111 |
$list_widget_controls_args = wp_list_widget_controls_dynamic_sidebar( |
1112 |
array( |
|
1113 |
0 => $args, |
|
1114 |
1 => $widget['params'][0], |
|
1115 |
) |
|
1116 |
); |
|
1117 |
$control_tpl = $this->get_widget_control( $list_widget_controls_args ); |
|
5 | 1118 |
|
1119 |
// The properties here are mapped to the Backbone Widget model. |
|
9 | 1120 |
$available_widget = array_merge( |
1121 |
$available_widget, |
|
1122 |
array( |
|
1123 |
'temp_id' => isset( $args['_temp_id'] ) ? $args['_temp_id'] : null, |
|
1124 |
'is_multi' => $is_multi_widget, |
|
1125 |
'control_tpl' => $control_tpl, |
|
16 | 1126 |
'multi_number' => ( 'multi' === $args['_add'] ) ? $args['_multi_num'] : false, |
9 | 1127 |
'is_disabled' => $is_disabled, |
1128 |
'id_base' => $id_base, |
|
1129 |
'transport' => $this->is_widget_selective_refreshable( $id_base ) ? 'postMessage' : 'refresh', |
|
1130 |
'width' => $wp_registered_widget_controls[ $widget['id'] ]['width'], |
|
1131 |
'height' => $wp_registered_widget_controls[ $widget['id'] ]['height'], |
|
1132 |
'is_wide' => $this->is_wide_widget( $widget['id'] ), |
|
1133 |
) |
|
1134 |
); |
|
5 | 1135 |
|
1136 |
$available_widgets[] = $available_widget; |
|
1137 |
} |
|
1138 |
||
1139 |
return $available_widgets; |
|
1140 |
} |
|
1141 |
||
1142 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
* Naturally orders available widgets by name. |
5 | 1144 |
* |
1145 |
* @since 3.9.0 |
|
1146 |
* |
|
1147 |
* @param array $widget_a The first widget to compare. |
|
1148 |
* @param array $widget_b The second widget to compare. |
|
1149 |
* @return int Reorder position for the current widget comparison. |
|
1150 |
*/ |
|
1151 |
protected function _sort_name_callback( $widget_a, $widget_b ) { |
|
1152 |
return strnatcasecmp( $widget_a['name'], $widget_b['name'] ); |
|
1153 |
} |
|
1154 |
||
1155 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
* Retrieves the widget control markup. |
5 | 1157 |
* |
1158 |
* @since 3.9.0 |
|
1159 |
* |
|
1160 |
* @param array $args Widget control arguments. |
|
1161 |
* @return string Widget control form HTML markup. |
|
1162 |
*/ |
|
1163 |
public function get_widget_control( $args ) { |
|
9 | 1164 |
$args[0]['before_form'] = '<div class="form">'; |
1165 |
$args[0]['after_form'] = '</div><!-- .form -->'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
$args[0]['before_widget_content'] = '<div class="widget-content">'; |
9 | 1167 |
$args[0]['after_widget_content'] = '</div><!-- .widget-content -->'; |
5 | 1168 |
ob_start(); |
16 | 1169 |
wp_widget_control( ...$args ); |
5 | 1170 |
$control_tpl = ob_get_clean(); |
1171 |
return $control_tpl; |
|
1172 |
} |
|
1173 |
||
1174 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
* Retrieves the widget control markup parts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
* @param array $args Widget control arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
* @return array { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
* @type string $control Markup for widget control wrapping form. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
* @type string $content The contents of the widget form itself. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
public function get_widget_control_parts( $args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1186 |
$args[0]['before_widget_content'] = '<div class="widget-content">'; |
9 | 1187 |
$args[0]['after_widget_content'] = '</div><!-- .widget-content -->'; |
1188 |
$control_markup = $this->get_widget_control( $args ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
$content_start_pos = strpos( $control_markup, $args[0]['before_widget_content'] ); |
9 | 1191 |
$content_end_pos = strrpos( $control_markup, $args[0]['after_widget_content'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
|
9 | 1193 |
$control = substr( $control_markup, 0, $content_start_pos + strlen( $args[0]['before_widget_content'] ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
$control .= substr( $control_markup, $content_end_pos ); |
9 | 1195 |
$content = trim( |
1196 |
substr( |
|
1197 |
$control_markup, |
|
1198 |
$content_start_pos + strlen( $args[0]['before_widget_content'] ), |
|
1199 |
$content_end_pos - $content_start_pos - strlen( $args[0]['before_widget_content'] ) |
|
1200 |
) |
|
1201 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
return compact( 'control', 'content' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
* Adds hooks for the Customizer preview. |
5 | 1208 |
* |
1209 |
* @since 3.9.0 |
|
1210 |
*/ |
|
1211 |
public function customize_preview_init() { |
|
1212 |
add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue' ) ); |
|
9 | 1213 |
add_action( 'wp_print_styles', array( $this, 'print_preview_css' ), 1 ); |
1214 |
add_action( 'wp_footer', array( $this, 'export_preview_data' ), 20 ); |
|
5 | 1215 |
} |
1216 |
||
1217 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
* Refreshes the nonce for widget updates. |
5 | 1219 |
* |
1220 |
* @since 4.2.0 |
|
1221 |
* |
|
16 | 1222 |
* @param array $nonces Array of nonces. |
1223 |
* @return array Array of nonces. |
|
5 | 1224 |
*/ |
1225 |
public function refresh_nonces( $nonces ) { |
|
1226 |
$nonces['update-widget'] = wp_create_nonce( 'update-widget' ); |
|
1227 |
return $nonces; |
|
1228 |
} |
|
1229 |
||
1230 |
/** |
|
18 | 1231 |
* Tells the script loader to load the scripts and styles of custom blocks |
1232 |
* if the widgets block editor is enabled. |
|
1233 |
* |
|
1234 |
* @since 5.8.0 |
|
1235 |
* |
|
1236 |
* @param bool $is_block_editor_screen Current decision about loading block assets. |
|
1237 |
* @return bool Filtered decision about loading block assets. |
|
1238 |
*/ |
|
1239 |
public function should_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { |
|
1240 |
if ( wp_use_widgets_block_editor() ) { |
|
1241 |
return true; |
|
1242 |
} |
|
1243 |
||
1244 |
return $is_block_editor_screen; |
|
1245 |
} |
|
1246 |
||
1247 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
* When previewing, ensures the proper previewing widgets are used. |
5 | 1249 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1250 |
* Because wp_get_sidebars_widgets() gets called early at {@see 'init' } (via |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
* wp_convert_widget_settings()) and can set global variable `$_wp_sidebars_widgets` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1252 |
* to the value of `get_option( 'sidebars_widgets' )` before the Customizer preview |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1253 |
* filter is added, it has to be reset after the filter has been added. |
5 | 1254 |
* |
1255 |
* @since 3.9.0 |
|
1256 |
* |
|
1257 |
* @param array $sidebars_widgets List of widgets for the current sidebar. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
* @return array |
5 | 1259 |
*/ |
1260 |
public function preview_sidebars_widgets( $sidebars_widgets ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1261 |
$sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
5 | 1262 |
|
1263 |
unset( $sidebars_widgets['array_version'] ); |
|
1264 |
return $sidebars_widgets; |
|
1265 |
} |
|
1266 |
||
1267 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
* Enqueues scripts for the Customizer preview. |
5 | 1269 |
* |
1270 |
* @since 3.9.0 |
|
1271 |
*/ |
|
1272 |
public function customize_preview_enqueue() { |
|
1273 |
wp_enqueue_script( 'customize-preview-widgets' ); |
|
1274 |
} |
|
1275 |
||
1276 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
* Inserts default style for highlighted widget at early point so theme |
5 | 1278 |
* stylesheet can override. |
1279 |
* |
|
1280 |
* @since 3.9.0 |
|
1281 |
*/ |
|
1282 |
public function print_preview_css() { |
|
1283 |
?> |
|
1284 |
<style> |
|
1285 |
.widget-customizer-highlighted-widget { |
|
1286 |
outline: none; |
|
9 | 1287 |
-webkit-box-shadow: 0 0 2px rgba(30, 140, 190, 0.8); |
1288 |
box-shadow: 0 0 2px rgba(30, 140, 190, 0.8); |
|
5 | 1289 |
position: relative; |
1290 |
z-index: 1; |
|
1291 |
} |
|
1292 |
</style> |
|
1293 |
<?php |
|
1294 |
} |
|
1295 |
||
1296 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
* Communicates the sidebars that appeared on the page at the very end of the page, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
* and at the very end of the wp_footer, |
5 | 1299 |
* |
1300 |
* @since 3.9.0 |
|
9 | 1301 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
* @global array $wp_registered_sidebars |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
* @global array $wp_registered_widgets |
5 | 1304 |
*/ |
1305 |
public function export_preview_data() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1306 |
global $wp_registered_sidebars, $wp_registered_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1307 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1308 |
$switched_locale = switch_to_user_locale( get_current_user_id() ); |
16 | 1309 |
|
1310 |
$l10n = array( |
|
9 | 1311 |
'widgetTooltip' => __( 'Shift-click to edit this widget.' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1312 |
); |
16 | 1313 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1314 |
if ( $switched_locale ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1315 |
restore_previous_locale(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1316 |
} |
5 | 1317 |
|
16 | 1318 |
$rendered_sidebars = array_filter( $this->rendered_sidebars ); |
1319 |
$rendered_widgets = array_filter( $this->rendered_widgets ); |
|
1320 |
||
5 | 1321 |
// Prepare Customizer settings to pass to JavaScript. |
1322 |
$settings = array( |
|
16 | 1323 |
'renderedSidebars' => array_fill_keys( array_keys( $rendered_sidebars ), true ), |
1324 |
'renderedWidgets' => array_fill_keys( array_keys( $rendered_widgets ), true ), |
|
9 | 1325 |
'registeredSidebars' => array_values( $wp_registered_sidebars ), |
1326 |
'registeredWidgets' => $wp_registered_widgets, |
|
1327 |
'l10n' => $l10n, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1328 |
'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(), |
5 | 1329 |
); |
16 | 1330 |
|
5 | 1331 |
foreach ( $settings['registeredWidgets'] as &$registered_widget ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1332 |
unset( $registered_widget['callback'] ); // May not be JSON-serializable. |
5 | 1333 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1334 |
wp_print_inline_script_tag( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1335 |
sprintf( 'var _wpWidgetCustomizerPreviewSettings = %s;', wp_json_encode( $settings ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1336 |
); |
5 | 1337 |
} |
1338 |
||
1339 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
* Tracks the widgets that were rendered. |
5 | 1341 |
* |
1342 |
* @since 3.9.0 |
|
1343 |
* |
|
1344 |
* @param array $widget Rendered widget to tally. |
|
1345 |
*/ |
|
1346 |
public function tally_rendered_widgets( $widget ) { |
|
1347 |
$this->rendered_widgets[ $widget['id'] ] = true; |
|
1348 |
} |
|
1349 |
||
1350 |
/** |
|
1351 |
* Determine if a widget is rendered on the page. |
|
1352 |
* |
|
1353 |
* @since 4.0.0 |
|
1354 |
* |
|
1355 |
* @param string $widget_id Widget ID to check. |
|
1356 |
* @return bool Whether the widget is rendered. |
|
1357 |
*/ |
|
1358 |
public function is_widget_rendered( $widget_id ) { |
|
16 | 1359 |
return ! empty( $this->rendered_widgets[ $widget_id ] ); |
5 | 1360 |
} |
1361 |
||
1362 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
* Determines if a sidebar is rendered on the page. |
5 | 1364 |
* |
1365 |
* @since 4.0.0 |
|
1366 |
* |
|
1367 |
* @param string $sidebar_id Sidebar ID to check. |
|
1368 |
* @return bool Whether the sidebar is rendered. |
|
1369 |
*/ |
|
1370 |
public function is_sidebar_rendered( $sidebar_id ) { |
|
16 | 1371 |
return ! empty( $this->rendered_sidebars[ $sidebar_id ] ); |
5 | 1372 |
} |
1373 |
||
1374 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
* Tallies the sidebars rendered via is_active_sidebar(). |
5 | 1376 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
* Keep track of the times that is_active_sidebar() is called in the template, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
* and assume that this means that the sidebar would be rendered on the template |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
* if there were widgets populating it. |
5 | 1380 |
* |
1381 |
* @since 3.9.0 |
|
1382 |
* |
|
1383 |
* @param bool $is_active Whether the sidebar is active. |
|
1384 |
* @param string $sidebar_id Sidebar ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
* @return bool Whether the sidebar is active. |
5 | 1386 |
*/ |
1387 |
public function tally_sidebars_via_is_active_sidebar_calls( $is_active, $sidebar_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
if ( is_registered_sidebar( $sidebar_id ) ) { |
16 | 1389 |
$this->rendered_sidebars[ $sidebar_id ] = true; |
5 | 1390 |
} |
16 | 1391 |
|
5 | 1392 |
/* |
1393 |
* We may need to force this to true, and also force-true the value |
|
1394 |
* for 'dynamic_sidebar_has_widgets' if we want to ensure that there |
|
1395 |
* is an area to drop widgets into, if the sidebar is empty. |
|
1396 |
*/ |
|
1397 |
return $is_active; |
|
1398 |
} |
|
1399 |
||
1400 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
* Tallies the sidebars rendered via dynamic_sidebar(). |
5 | 1402 |
* |
1403 |
* Keep track of the times that dynamic_sidebar() is called in the template, |
|
1404 |
* and assume this means the sidebar would be rendered on the template if |
|
1405 |
* there were widgets populating it. |
|
1406 |
* |
|
1407 |
* @since 3.9.0 |
|
1408 |
* |
|
1409 |
* @param bool $has_widgets Whether the current sidebar has widgets. |
|
1410 |
* @param string $sidebar_id Sidebar ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* @return bool Whether the current sidebar has widgets. |
5 | 1412 |
*/ |
1413 |
public function tally_sidebars_via_dynamic_sidebar_calls( $has_widgets, $sidebar_id ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
if ( is_registered_sidebar( $sidebar_id ) ) { |
16 | 1415 |
$this->rendered_sidebars[ $sidebar_id ] = true; |
5 | 1416 |
} |
1417 |
||
1418 |
/* |
|
1419 |
* We may need to force this to true, and also force-true the value |
|
1420 |
* for 'is_active_sidebar' if we want to ensure there is an area to |
|
1421 |
* drop widgets into, if the sidebar is empty. |
|
1422 |
*/ |
|
1423 |
return $has_widgets; |
|
1424 |
} |
|
1425 |
||
1426 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
* Retrieves MAC for a serialized widget instance string. |
5 | 1428 |
* |
1429 |
* Allows values posted back from JS to be rejected if any tampering of the |
|
1430 |
* data has occurred. |
|
1431 |
* |
|
1432 |
* @since 3.9.0 |
|
1433 |
* |
|
1434 |
* @param string $serialized_instance Widget instance. |
|
1435 |
* @return string MAC for serialized widget instance. |
|
1436 |
*/ |
|
1437 |
protected function get_instance_hash_key( $serialized_instance ) { |
|
1438 |
return wp_hash( $serialized_instance ); |
|
1439 |
} |
|
1440 |
||
1441 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1442 |
* Sanitizes a widget instance. |
5 | 1443 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1444 |
* Unserialize the JS-instance for storing in the options. It's important that this filter |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1445 |
* only get applied to an instance *once*. |
5 | 1446 |
* |
1447 |
* @since 3.9.0 |
|
18 | 1448 |
* @since 5.8.0 Added the `$id_base` parameter. |
5 | 1449 |
* |
18 | 1450 |
* @global WP_Widget_Factory $wp_widget_factory |
1451 |
* |
|
1452 |
* @param array $value Widget instance to sanitize. |
|
1453 |
* @param string $id_base Optional. Base of the ID of the widget being sanitized. Default null. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
* @return array|void Sanitized widget instance. |
5 | 1455 |
*/ |
18 | 1456 |
public function sanitize_widget_instance( $value, $id_base = null ) { |
1457 |
global $wp_widget_factory; |
|
1458 |
||
16 | 1459 |
if ( array() === $value ) { |
5 | 1460 |
return $value; |
1461 |
} |
|
1462 |
||
18 | 1463 |
if ( isset( $value['raw_instance'] ) && $id_base && wp_use_widgets_block_editor() ) { |
1464 |
$widget_object = $wp_widget_factory->get_widget_object( $id_base ); |
|
1465 |
if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) { |
|
1466 |
if ( 'block' === $id_base && ! current_user_can( 'unfiltered_html' ) ) { |
|
1467 |
/* |
|
19 | 1468 |
* The content of the 'block' widget is not filtered on the fly while editing. |
1469 |
* Filter the content here to prevent vulnerabilities. |
|
18 | 1470 |
*/ |
1471 |
$value['raw_instance']['content'] = wp_kses_post( $value['raw_instance']['content'] ); |
|
1472 |
} |
|
1473 |
||
1474 |
return $value['raw_instance']; |
|
1475 |
} |
|
1476 |
} |
|
1477 |
||
1478 |
if ( |
|
1479 |
empty( $value['is_widget_customizer_js_value'] ) || |
|
1480 |
empty( $value['instance_hash_key'] ) || |
|
1481 |
empty( $value['encoded_serialized_instance'] ) |
|
1482 |
) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1483 |
return; |
5 | 1484 |
} |
1485 |
||
1486 |
$decoded = base64_decode( $value['encoded_serialized_instance'], true ); |
|
1487 |
if ( false === $decoded ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
return; |
5 | 1489 |
} |
1490 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1491 |
if ( ! hash_equals( $this->get_instance_hash_key( $decoded ), $value['instance_hash_key'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
return; |
5 | 1493 |
} |
1494 |
||
1495 |
$instance = unserialize( $decoded ); |
|
1496 |
if ( false === $instance ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1497 |
return; |
5 | 1498 |
} |
1499 |
||
1500 |
return $instance; |
|
1501 |
} |
|
1502 |
||
1503 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1504 |
* Converts a widget instance into JSON-representable format. |
5 | 1505 |
* |
1506 |
* @since 3.9.0 |
|
18 | 1507 |
* @since 5.8.0 Added the `$id_base` parameter. |
5 | 1508 |
* |
18 | 1509 |
* @global WP_Widget_Factory $wp_widget_factory |
1510 |
* |
|
1511 |
* @param array $value Widget instance to convert to JSON. |
|
1512 |
* @param string $id_base Optional. Base of the ID of the widget being sanitized. Default null. |
|
5 | 1513 |
* @return array JSON-converted widget instance. |
1514 |
*/ |
|
18 | 1515 |
public function sanitize_widget_js_instance( $value, $id_base = null ) { |
1516 |
global $wp_widget_factory; |
|
1517 |
||
5 | 1518 |
if ( empty( $value['is_widget_customizer_js_value'] ) ) { |
1519 |
$serialized = serialize( $value ); |
|
1520 |
||
18 | 1521 |
$js_value = array( |
5 | 1522 |
'encoded_serialized_instance' => base64_encode( $serialized ), |
1523 |
'title' => empty( $value['title'] ) ? '' : $value['title'], |
|
1524 |
'is_widget_customizer_js_value' => true, |
|
1525 |
'instance_hash_key' => $this->get_instance_hash_key( $serialized ), |
|
1526 |
); |
|
18 | 1527 |
|
1528 |
if ( $id_base && wp_use_widgets_block_editor() ) { |
|
1529 |
$widget_object = $wp_widget_factory->get_widget_object( $id_base ); |
|
1530 |
if ( ! empty( $widget_object->widget_options['show_instance_in_rest'] ) ) { |
|
1531 |
$js_value['raw_instance'] = (object) $value; |
|
1532 |
} |
|
1533 |
} |
|
1534 |
||
1535 |
return $js_value; |
|
5 | 1536 |
} |
18 | 1537 |
|
5 | 1538 |
return $value; |
1539 |
} |
|
1540 |
||
1541 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1542 |
* Strips out widget IDs for widgets which are no longer registered. |
5 | 1543 |
* |
1544 |
* One example where this might happen is when a plugin orphans a widget |
|
1545 |
* in a sidebar upon deactivation. |
|
1546 |
* |
|
1547 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1548 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1549 |
* @global array $wp_registered_widgets |
5 | 1550 |
* |
1551 |
* @param array $widget_ids List of widget IDs. |
|
1552 |
* @return array Parsed list of widget IDs. |
|
1553 |
*/ |
|
1554 |
public function sanitize_sidebar_widgets_js_instance( $widget_ids ) { |
|
1555 |
global $wp_registered_widgets; |
|
1556 |
$widget_ids = array_values( array_intersect( $widget_ids, array_keys( $wp_registered_widgets ) ) ); |
|
1557 |
return $widget_ids; |
|
1558 |
} |
|
1559 |
||
1560 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
* Finds and invokes the widget update and control callbacks. |
5 | 1562 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1563 |
* Requires that `$_POST` be populated with the instance data. |
5 | 1564 |
* |
1565 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1566 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1567 |
* @global array $wp_registered_widget_updates |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1568 |
* @global array $wp_registered_widget_controls |
5 | 1569 |
* |
16 | 1570 |
* @param string $widget_id Widget ID. |
1571 |
* @return array|WP_Error Array containing the updated widget information. |
|
5 | 1572 |
* A WP_Error object, otherwise. |
1573 |
*/ |
|
1574 |
public function call_widget_update( $widget_id ) { |
|
1575 |
global $wp_registered_widget_updates, $wp_registered_widget_controls; |
|
1576 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1577 |
$setting_id = $this->get_setting_id( $widget_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1578 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1579 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
* Make sure that other setting changes have previewed since this widget |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1581 |
* may depend on them (e.g. Menus being present for Navigation Menu widget). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1582 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1583 |
if ( ! did_action( 'customize_preview_init' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1584 |
foreach ( $this->manager->settings() as $setting ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1585 |
if ( $setting->id !== $setting_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1586 |
$setting->preview(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1587 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1588 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1589 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1590 |
|
5 | 1591 |
$this->start_capturing_option_updates(); |
1592 |
$parsed_id = $this->parse_widget_id( $widget_id ); |
|
1593 |
$option_name = 'widget_' . $parsed_id['id_base']; |
|
1594 |
||
1595 |
/* |
|
1596 |
* If a previously-sanitized instance is provided, populate the input vars |
|
1597 |
* with its values so that the widget update callback will read this instance |
|
1598 |
*/ |
|
1599 |
$added_input_vars = array(); |
|
1600 |
if ( ! empty( $_POST['sanitized_widget_setting'] ) ) { |
|
1601 |
$sanitized_widget_setting = json_decode( $this->get_post_value( 'sanitized_widget_setting' ), true ); |
|
1602 |
if ( false === $sanitized_widget_setting ) { |
|
1603 |
$this->stop_capturing_option_updates(); |
|
1604 |
return new WP_Error( 'widget_setting_malformed' ); |
|
1605 |
} |
|
1606 |
||
18 | 1607 |
$instance = $this->sanitize_widget_instance( $sanitized_widget_setting, $parsed_id['id_base'] ); |
5 | 1608 |
if ( is_null( $instance ) ) { |
1609 |
$this->stop_capturing_option_updates(); |
|
1610 |
return new WP_Error( 'widget_setting_unsanitized' ); |
|
1611 |
} |
|
1612 |
||
1613 |
if ( ! is_null( $parsed_id['number'] ) ) { |
|
9 | 1614 |
$value = array(); |
1615 |
$value[ $parsed_id['number'] ] = $instance; |
|
1616 |
$key = 'widget-' . $parsed_id['id_base']; |
|
16 | 1617 |
$_REQUEST[ $key ] = wp_slash( $value ); |
1618 |
$_POST[ $key ] = $_REQUEST[ $key ]; |
|
9 | 1619 |
$added_input_vars[] = $key; |
5 | 1620 |
} else { |
1621 |
foreach ( $instance as $key => $value ) { |
|
16 | 1622 |
$_REQUEST[ $key ] = wp_slash( $value ); |
1623 |
$_POST[ $key ] = $_REQUEST[ $key ]; |
|
5 | 1624 |
$added_input_vars[] = $key; |
1625 |
} |
|
1626 |
} |
|
1627 |
} |
|
1628 |
||
1629 |
// Invoke the widget update callback. |
|
1630 |
foreach ( (array) $wp_registered_widget_updates as $name => $control ) { |
|
1631 |
if ( $name === $parsed_id['id_base'] && is_callable( $control['callback'] ) ) { |
|
1632 |
ob_start(); |
|
1633 |
call_user_func_array( $control['callback'], $control['params'] ); |
|
1634 |
ob_end_clean(); |
|
1635 |
break; |
|
1636 |
} |
|
1637 |
} |
|
1638 |
||
16 | 1639 |
// Clean up any input vars that were manually added. |
5 | 1640 |
foreach ( $added_input_vars as $key ) { |
1641 |
unset( $_POST[ $key ] ); |
|
1642 |
unset( $_REQUEST[ $key ] ); |
|
1643 |
} |
|
1644 |
||
1645 |
// Make sure the expected option was updated. |
|
1646 |
if ( 0 !== $this->count_captured_options() ) { |
|
1647 |
if ( $this->count_captured_options() > 1 ) { |
|
1648 |
$this->stop_capturing_option_updates(); |
|
1649 |
return new WP_Error( 'widget_setting_too_many_options' ); |
|
1650 |
} |
|
1651 |
||
1652 |
$updated_option_name = key( $this->get_captured_options() ); |
|
1653 |
if ( $updated_option_name !== $option_name ) { |
|
1654 |
$this->stop_capturing_option_updates(); |
|
1655 |
return new WP_Error( 'widget_setting_unexpected_option' ); |
|
1656 |
} |
|
1657 |
} |
|
1658 |
||
1659 |
// Obtain the widget instance. |
|
1660 |
$option = $this->get_captured_option( $option_name ); |
|
1661 |
if ( null !== $parsed_id['number'] ) { |
|
1662 |
$instance = $option[ $parsed_id['number'] ]; |
|
1663 |
} else { |
|
1664 |
$instance = $option; |
|
1665 |
} |
|
1666 |
||
1667 |
/* |
|
1668 |
* Override the incoming $_POST['customized'] for a newly-created widget's |
|
1669 |
* setting with the new $instance so that the preview filter currently |
|
1670 |
* in place from WP_Customize_Setting::preview() will use this value |
|
1671 |
* instead of the default widget instance value (an empty array). |
|
1672 |
*/ |
|
18 | 1673 |
$this->manager->set_post_value( $setting_id, $this->sanitize_widget_js_instance( $instance, $parsed_id['id_base'] ) ); |
5 | 1674 |
|
1675 |
// Obtain the widget control with the updated instance in place. |
|
1676 |
ob_start(); |
|
1677 |
$form = $wp_registered_widget_controls[ $widget_id ]; |
|
1678 |
if ( $form ) { |
|
1679 |
call_user_func_array( $form['callback'], $form['params'] ); |
|
1680 |
} |
|
1681 |
$form = ob_get_clean(); |
|
1682 |
||
1683 |
$this->stop_capturing_option_updates(); |
|
1684 |
||
1685 |
return compact( 'instance', 'form' ); |
|
1686 |
} |
|
1687 |
||
1688 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
* Updates widget settings asynchronously. |
5 | 1690 |
* |
1691 |
* Allows the Customizer to update a widget using its form, but return the new |
|
1692 |
* instance info via Ajax instead of saving it to the options table. |
|
1693 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1694 |
* Most code here copied from wp_ajax_save_widget(). |
5 | 1695 |
* |
1696 |
* @since 3.9.0 |
|
1697 |
* |
|
1698 |
* @see wp_ajax_save_widget() |
|
1699 |
*/ |
|
1700 |
public function wp_ajax_update_widget() { |
|
1701 |
||
1702 |
if ( ! is_user_logged_in() ) { |
|
1703 |
wp_die( 0 ); |
|
1704 |
} |
|
1705 |
||
1706 |
check_ajax_referer( 'update-widget', 'nonce' ); |
|
1707 |
||
1708 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
1709 |
wp_die( -1 ); |
|
1710 |
} |
|
1711 |
||
1712 |
if ( empty( $_POST['widget-id'] ) ) { |
|
1713 |
wp_send_json_error( 'missing_widget-id' ); |
|
1714 |
} |
|
1715 |
||
1716 |
/** This action is documented in wp-admin/includes/ajax-actions.php */ |
|
16 | 1717 |
do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
5 | 1718 |
|
1719 |
/** This action is documented in wp-admin/includes/ajax-actions.php */ |
|
16 | 1720 |
do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
5 | 1721 |
|
1722 |
/** This action is documented in wp-admin/widgets.php */ |
|
1723 |
do_action( 'sidebar_admin_setup' ); |
|
1724 |
||
1725 |
$widget_id = $this->get_post_value( 'widget-id' ); |
|
1726 |
$parsed_id = $this->parse_widget_id( $widget_id ); |
|
9 | 1727 |
$id_base = $parsed_id['id_base']; |
5 | 1728 |
|
1729 |
$is_updating_widget_template = ( |
|
1730 |
isset( $_POST[ 'widget-' . $id_base ] ) |
|
1731 |
&& |
|
1732 |
is_array( $_POST[ 'widget-' . $id_base ] ) |
|
1733 |
&& |
|
1734 |
preg_match( '/__i__|%i%/', key( $_POST[ 'widget-' . $id_base ] ) ) |
|
1735 |
); |
|
1736 |
if ( $is_updating_widget_template ) { |
|
1737 |
wp_send_json_error( 'template_widget_not_updatable' ); |
|
1738 |
} |
|
1739 |
||
1740 |
$updated_widget = $this->call_widget_update( $widget_id ); // => {instance,form} |
|
1741 |
if ( is_wp_error( $updated_widget ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1742 |
wp_send_json_error( $updated_widget->get_error_code() ); |
5 | 1743 |
} |
1744 |
||
9 | 1745 |
$form = $updated_widget['form']; |
18 | 1746 |
$instance = $this->sanitize_widget_js_instance( $updated_widget['instance'], $id_base ); |
5 | 1747 |
|
1748 |
wp_send_json_success( compact( 'form', 'instance' ) ); |
|
1749 |
} |
|
1750 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1751 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1752 |
* Selective Refresh Methods |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1753 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1754 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1755 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1756 |
* Filters arguments for dynamic widget partials. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1757 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1758 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1759 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1760 |
* @param array|false $partial_args Partial arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1761 |
* @param string $partial_id Partial ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1762 |
* @return array (Maybe) modified partial arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1763 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1764 |
public function customize_dynamic_partial_args( $partial_args, $partial_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1766 |
return $partial_args; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1767 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1768 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1769 |
if ( preg_match( '/^widget\[(?P<widget_id>.+)\]$/', $partial_id, $matches ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1770 |
if ( false === $partial_args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1771 |
$partial_args = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1772 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1773 |
$partial_args = array_merge( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1774 |
$partial_args, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1775 |
array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1776 |
'type' => 'widget', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1777 |
'render_callback' => array( $this, 'render_widget_partial' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1778 |
'container_inclusive' => true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1779 |
'settings' => array( $this->get_setting_id( $matches['widget_id'] ) ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1780 |
'capability' => 'edit_theme_options', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1781 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1782 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1783 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1784 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1785 |
return $partial_args; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1786 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1787 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1788 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1789 |
* Adds hooks for selective refresh. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1790 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1791 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1792 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1793 |
public function selective_refresh_init() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1794 |
if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1795 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1796 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1797 |
add_filter( 'dynamic_sidebar_params', array( $this, 'filter_dynamic_sidebar_params' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1798 |
add_filter( 'wp_kses_allowed_html', array( $this, 'filter_wp_kses_allowed_data_attributes' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1799 |
add_action( 'dynamic_sidebar_before', array( $this, 'start_dynamic_sidebar' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1800 |
add_action( 'dynamic_sidebar_after', array( $this, 'end_dynamic_sidebar' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1801 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1802 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1803 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1804 |
* Inject selective refresh data attributes into widget container elements. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1805 |
* |
9 | 1806 |
* @since 4.5.0 |
1807 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1808 |
* @param array $params { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1809 |
* Dynamic sidebar params. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1810 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1811 |
* @type array $args Sidebar args. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1812 |
* @type array $widget_args Widget args. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1813 |
* } |
9 | 1814 |
* @see WP_Customize_Nav_Menus::filter_wp_nav_menu_args() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1815 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1816 |
* @return array Params. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1817 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1818 |
public function filter_dynamic_sidebar_params( $params ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1819 |
$sidebar_args = array_merge( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1820 |
array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1821 |
'before_widget' => '', |
9 | 1822 |
'after_widget' => '', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1823 |
), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1824 |
$params[0] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1825 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1826 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1827 |
// Skip widgets not in a registered sidebar or ones which lack a proper wrapper element to attach the data-* attributes to. |
9 | 1828 |
$matches = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1829 |
$is_valid = ( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1830 |
isset( $sidebar_args['id'] ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1831 |
&& |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1832 |
is_registered_sidebar( $sidebar_args['id'] ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1833 |
&& |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1834 |
( isset( $this->current_dynamic_sidebar_id_stack[0] ) && $this->current_dynamic_sidebar_id_stack[0] === $sidebar_args['id'] ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1835 |
&& |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1836 |
preg_match( '#^<(?P<tag_name>\w+)#', $sidebar_args['before_widget'], $matches ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1837 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1838 |
if ( ! $is_valid ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1839 |
return $params; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1840 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1841 |
$this->before_widget_tags_seen[ $matches['tag_name'] ] = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1842 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1843 |
$context = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1844 |
'sidebar_id' => $sidebar_args['id'], |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1845 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1846 |
if ( isset( $this->context_sidebar_instance_number ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1847 |
$context['sidebar_instance_number'] = $this->context_sidebar_instance_number; |
9 | 1848 |
} elseif ( isset( $sidebar_args['id'] ) && isset( $this->sidebar_instance_count[ $sidebar_args['id'] ] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1849 |
$context['sidebar_instance_number'] = $this->sidebar_instance_count[ $sidebar_args['id'] ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1850 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1851 |
|
9 | 1852 |
$attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'widget[' . $sidebar_args['widget_id'] . ']' ) ); |
1853 |
$attributes .= ' data-customize-partial-type="widget"'; |
|
1854 |
$attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $context ) ) ); |
|
1855 |
$attributes .= sprintf( ' data-customize-widget-id="%s"', esc_attr( $sidebar_args['widget_id'] ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1856 |
$sidebar_args['before_widget'] = preg_replace( '#^(<\w+)#', '$1 ' . $attributes, $sidebar_args['before_widget'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1857 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1858 |
$params[0] = $sidebar_args; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1859 |
return $params; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1860 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1861 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1862 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1863 |
* List of the tag names seen for before_widget strings. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1864 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1865 |
* This is used in the {@see 'filter_wp_kses_allowed_html'} filter to ensure that the |
16 | 1866 |
* data-* attributes can be allowed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1867 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1868 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1869 |
* @var array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1870 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1871 |
protected $before_widget_tags_seen = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1872 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1873 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1874 |
* Ensures the HTML data-* attributes for selective refresh are allowed by kses. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1875 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1876 |
* This is needed in case the `$before_widget` is run through wp_kses() when printed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1877 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1878 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1879 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1880 |
* @param array $allowed_html Allowed HTML. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1881 |
* @return array (Maybe) modified allowed HTML. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1882 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1883 |
public function filter_wp_kses_allowed_data_attributes( $allowed_html ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1884 |
foreach ( array_keys( $this->before_widget_tags_seen ) as $tag_name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1885 |
if ( ! isset( $allowed_html[ $tag_name ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1886 |
$allowed_html[ $tag_name ] = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1887 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1888 |
$allowed_html[ $tag_name ] = array_merge( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1889 |
$allowed_html[ $tag_name ], |
9 | 1890 |
array_fill_keys( |
1891 |
array( |
|
1892 |
'data-customize-partial-id', |
|
1893 |
'data-customize-partial-type', |
|
1894 |
'data-customize-partial-placement-context', |
|
1895 |
'data-customize-partial-widget-id', |
|
1896 |
'data-customize-partial-options', |
|
1897 |
), |
|
1898 |
true |
|
1899 |
) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1900 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1901 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1902 |
return $allowed_html; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1903 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1904 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1905 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1906 |
* Keep track of the number of times that dynamic_sidebar() was called for a given sidebar index. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1907 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1908 |
* This helps facilitate the uncommon scenario where a single sidebar is rendered multiple times on a template. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1909 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1910 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1911 |
* @var array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1912 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1913 |
protected $sidebar_instance_count = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1914 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1915 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1916 |
* The current request's sidebar_instance_number context. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1917 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1918 |
* @since 4.5.0 |
16 | 1919 |
* @var int|null |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1920 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1921 |
protected $context_sidebar_instance_number; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1922 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1923 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1924 |
* Current sidebar ID being rendered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1925 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1926 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1927 |
* @var array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1928 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1929 |
protected $current_dynamic_sidebar_id_stack = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1930 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1931 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1932 |
* Begins keeping track of the current sidebar being rendered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1933 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1934 |
* Insert marker before widgets are rendered in a dynamic sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1935 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1937 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1938 |
* @param int|string $index Index, name, or ID of the dynamic sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1939 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
public function start_dynamic_sidebar( $index ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1941 |
array_unshift( $this->current_dynamic_sidebar_id_stack, $index ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1942 |
if ( ! isset( $this->sidebar_instance_count[ $index ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1943 |
$this->sidebar_instance_count[ $index ] = 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1944 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1945 |
$this->sidebar_instance_count[ $index ] += 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1946 |
if ( ! $this->manager->selective_refresh->is_render_partials_request() ) { |
18 | 1947 |
printf( "\n<!--dynamic_sidebar_before:%s:%d-->\n", esc_html( $index ), (int) $this->sidebar_instance_count[ $index ] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1948 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1949 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1950 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1951 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1952 |
* Finishes keeping track of the current sidebar being rendered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1953 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1954 |
* Inserts a marker after widgets are rendered in a dynamic sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1955 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1956 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1957 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1958 |
* @param int|string $index Index, name, or ID of the dynamic sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1959 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1960 |
public function end_dynamic_sidebar( $index ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1961 |
array_shift( $this->current_dynamic_sidebar_id_stack ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1962 |
if ( ! $this->manager->selective_refresh->is_render_partials_request() ) { |
18 | 1963 |
printf( "\n<!--dynamic_sidebar_after:%s:%d-->\n", esc_html( $index ), (int) $this->sidebar_instance_count[ $index ] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1964 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1965 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1966 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1967 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1968 |
* Current sidebar being rendered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1969 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1970 |
* @since 4.5.0 |
16 | 1971 |
* @var string|null |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1972 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1973 |
protected $rendering_widget_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1974 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1975 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1976 |
* Current widget being rendered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1977 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1978 |
* @since 4.5.0 |
16 | 1979 |
* @var string|null |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1980 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1981 |
protected $rendering_sidebar_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1982 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1983 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1984 |
* Filters sidebars_widgets to ensure the currently-rendered widget is the only widget in the current sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1985 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1986 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1987 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1988 |
* @param array $sidebars_widgets Sidebars widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1989 |
* @return array Filtered sidebars widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1990 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1991 |
public function filter_sidebars_widgets_for_rendering_widget( $sidebars_widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1992 |
$sidebars_widgets[ $this->rendering_sidebar_id ] = array( $this->rendering_widget_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1993 |
return $sidebars_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1994 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1995 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1996 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1997 |
* Renders a specific widget using the supplied sidebar arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1998 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1999 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2000 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2001 |
* @see dynamic_sidebar() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2002 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2003 |
* @param WP_Customize_Partial $partial Partial. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2004 |
* @param array $context { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2005 |
* Sidebar args supplied as container context. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2006 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2007 |
* @type string $sidebar_id ID for sidebar for widget to render into. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2008 |
* @type int $sidebar_instance_number Disambiguating instance number. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2009 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2010 |
* @return string|false |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2011 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2012 |
public function render_widget_partial( $partial, $context ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2013 |
$id_data = $partial->id_data(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2014 |
$widget_id = array_shift( $id_data['keys'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2015 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2016 |
if ( ! is_array( $context ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2017 |
|| empty( $context['sidebar_id'] ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2018 |
|| ! is_registered_sidebar( $context['sidebar_id'] ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2019 |
) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2020 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2021 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2022 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2023 |
$this->rendering_sidebar_id = $context['sidebar_id']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2024 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2025 |
if ( isset( $context['sidebar_instance_number'] ) ) { |
18 | 2026 |
$this->context_sidebar_instance_number = (int) $context['sidebar_instance_number']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2027 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2028 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2029 |
// Filter sidebars_widgets so that only the queried widget is in the sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2030 |
$this->rendering_widget_id = $widget_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2031 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2032 |
$filter_callback = array( $this, 'filter_sidebars_widgets_for_rendering_widget' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2033 |
add_filter( 'sidebars_widgets', $filter_callback, 1000 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2034 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2035 |
// Render the widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2036 |
ob_start(); |
16 | 2037 |
$this->rendering_sidebar_id = $context['sidebar_id']; |
2038 |
dynamic_sidebar( $this->rendering_sidebar_id ); |
|
2039 |
$container = ob_get_clean(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2040 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2041 |
// Reset variables for next partial render. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2042 |
remove_filter( 'sidebars_widgets', $filter_callback, 1000 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2043 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2044 |
$this->context_sidebar_instance_number = null; |
9 | 2045 |
$this->rendering_sidebar_id = null; |
2046 |
$this->rendering_widget_id = null; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2047 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2048 |
return $container; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2049 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2050 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2051 |
// |
16 | 2052 |
// Option Update Capturing. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2053 |
// |
5 | 2054 |
|
2055 |
/** |
|
2056 |
* List of captured widget option updates. |
|
2057 |
* |
|
2058 |
* @since 3.9.0 |
|
2059 |
* @var array $_captured_options Values updated while option capture is happening. |
|
2060 |
*/ |
|
2061 |
protected $_captured_options = array(); |
|
2062 |
||
2063 |
/** |
|
2064 |
* Whether option capture is currently happening. |
|
2065 |
* |
|
2066 |
* @since 3.9.0 |
|
2067 |
* @var bool $_is_current Whether option capture is currently happening or not. |
|
2068 |
*/ |
|
2069 |
protected $_is_capturing_option_updates = false; |
|
2070 |
||
2071 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2072 |
* Determines whether the captured option update should be ignored. |
5 | 2073 |
* |
2074 |
* @since 3.9.0 |
|
2075 |
* |
|
2076 |
* @param string $option_name Option name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2077 |
* @return bool Whether the option capture is ignored. |
5 | 2078 |
*/ |
2079 |
protected function is_option_capture_ignored( $option_name ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
2080 |
return ( str_starts_with( $option_name, '_transient_' ) ); |
5 | 2081 |
} |
2082 |
||
2083 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2084 |
* Retrieves captured widget option updates. |
5 | 2085 |
* |
2086 |
* @since 3.9.0 |
|
2087 |
* |
|
2088 |
* @return array Array of captured options. |
|
2089 |
*/ |
|
2090 |
protected function get_captured_options() { |
|
2091 |
return $this->_captured_options; |
|
2092 |
} |
|
2093 |
||
2094 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2095 |
* Retrieves the option that was captured from being saved. |
5 | 2096 |
* |
2097 |
* @since 4.2.0 |
|
2098 |
* |
|
19 | 2099 |
* @param string $option_name Option name. |
2100 |
* @param mixed $default_value Optional. Default value to return if the option does not exist. Default false. |
|
5 | 2101 |
* @return mixed Value set for the option. |
2102 |
*/ |
|
19 | 2103 |
protected function get_captured_option( $option_name, $default_value = false ) { |
5 | 2104 |
if ( array_key_exists( $option_name, $this->_captured_options ) ) { |
2105 |
$value = $this->_captured_options[ $option_name ]; |
|
2106 |
} else { |
|
19 | 2107 |
$value = $default_value; |
5 | 2108 |
} |
2109 |
return $value; |
|
2110 |
} |
|
2111 |
||
2112 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2113 |
* Retrieves the number of captured widget option updates. |
5 | 2114 |
* |
2115 |
* @since 3.9.0 |
|
2116 |
* |
|
2117 |
* @return int Number of updated options. |
|
2118 |
*/ |
|
2119 |
protected function count_captured_options() { |
|
2120 |
return count( $this->_captured_options ); |
|
2121 |
} |
|
2122 |
||
2123 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2124 |
* Begins keeping track of changes to widget options, caching new values. |
5 | 2125 |
* |
2126 |
* @since 3.9.0 |
|
2127 |
*/ |
|
2128 |
protected function start_capturing_option_updates() { |
|
2129 |
if ( $this->_is_capturing_option_updates ) { |
|
2130 |
return; |
|
2131 |
} |
|
2132 |
||
2133 |
$this->_is_capturing_option_updates = true; |
|
2134 |
||
2135 |
add_filter( 'pre_update_option', array( $this, 'capture_filter_pre_update_option' ), 10, 3 ); |
|
2136 |
} |
|
2137 |
||
2138 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2139 |
* Pre-filters captured option values before updating. |
5 | 2140 |
* |
2141 |
* @since 3.9.0 |
|
2142 |
* |
|
2143 |
* @param mixed $new_value The new option value. |
|
2144 |
* @param string $option_name Name of the option. |
|
2145 |
* @param mixed $old_value The old option value. |
|
2146 |
* @return mixed Filtered option value. |
|
2147 |
*/ |
|
2148 |
public function capture_filter_pre_update_option( $new_value, $option_name, $old_value ) { |
|
2149 |
if ( $this->is_option_capture_ignored( $option_name ) ) { |
|
16 | 2150 |
return $new_value; |
5 | 2151 |
} |
2152 |
||
2153 |
if ( ! isset( $this->_captured_options[ $option_name ] ) ) { |
|
2154 |
add_filter( "pre_option_{$option_name}", array( $this, 'capture_filter_pre_get_option' ) ); |
|
2155 |
} |
|
2156 |
||
2157 |
$this->_captured_options[ $option_name ] = $new_value; |
|
2158 |
||
2159 |
return $old_value; |
|
2160 |
} |
|
2161 |
||
2162 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2163 |
* Pre-filters captured option values before retrieving. |
5 | 2164 |
* |
2165 |
* @since 3.9.0 |
|
2166 |
* |
|
2167 |
* @param mixed $value Value to return instead of the option value. |
|
2168 |
* @return mixed Filtered option value. |
|
2169 |
*/ |
|
2170 |
public function capture_filter_pre_get_option( $value ) { |
|
2171 |
$option_name = preg_replace( '/^pre_option_/', '', current_filter() ); |
|
2172 |
||
2173 |
if ( isset( $this->_captured_options[ $option_name ] ) ) { |
|
2174 |
$value = $this->_captured_options[ $option_name ]; |
|
2175 |
||
2176 |
/** This filter is documented in wp-includes/option.php */ |
|
9 | 2177 |
$value = apply_filters( 'option_' . $option_name, $value, $option_name ); |
5 | 2178 |
} |
2179 |
||
2180 |
return $value; |
|
2181 |
} |
|
2182 |
||
2183 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2184 |
* Undoes any changes to the options since options capture began. |
5 | 2185 |
* |
2186 |
* @since 3.9.0 |
|
2187 |
*/ |
|
2188 |
protected function stop_capturing_option_updates() { |
|
2189 |
if ( ! $this->_is_capturing_option_updates ) { |
|
2190 |
return; |
|
2191 |
} |
|
2192 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2193 |
remove_filter( 'pre_update_option', array( $this, 'capture_filter_pre_update_option' ), 10 ); |
5 | 2194 |
|
2195 |
foreach ( array_keys( $this->_captured_options ) as $option_name ) { |
|
2196 |
remove_filter( "pre_option_{$option_name}", array( $this, 'capture_filter_pre_get_option' ) ); |
|
2197 |
} |
|
2198 |
||
9 | 2199 |
$this->_captured_options = array(); |
5 | 2200 |
$this->_is_capturing_option_updates = false; |
2201 |
} |
|
2202 |
||
2203 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2204 |
* {@internal Missing Summary} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2205 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2206 |
* See the {@see 'customize_dynamic_setting_args'} filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2207 |
* |
5 | 2208 |
* @since 3.9.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2209 |
* @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. |
5 | 2210 |
*/ |
2211 |
public function setup_widget_addition_previews() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2212 |
_deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); |
5 | 2213 |
} |
2214 |
||
2215 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2216 |
* {@internal Missing Summary} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2217 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2218 |
* See the {@see 'customize_dynamic_setting_args'} filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2219 |
* |
5 | 2220 |
* @since 3.9.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2221 |
* @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. |
5 | 2222 |
*/ |
2223 |
public function prepreview_added_sidebars_widgets() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2224 |
_deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); |
5 | 2225 |
} |
2226 |
||
2227 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2228 |
* {@internal Missing Summary} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2229 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2230 |
* See the {@see 'customize_dynamic_setting_args'} filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2231 |
* |
5 | 2232 |
* @since 3.9.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2233 |
* @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. |
5 | 2234 |
*/ |
2235 |
public function prepreview_added_widget_instance() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2236 |
_deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); |
5 | 2237 |
} |
2238 |
||
2239 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2240 |
* {@internal Missing Summary} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2241 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2242 |
* See the {@see 'customize_dynamic_setting_args'} filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2243 |
* |
5 | 2244 |
* @since 3.9.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2245 |
* @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. |
5 | 2246 |
*/ |
2247 |
public function remove_prepreview_filters() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2248 |
_deprecated_function( __METHOD__, '4.2.0', 'customize_dynamic_setting_args' ); |
5 | 2249 |
} |
2250 |
} |