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