PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.2
Secure Custom Fields v6.4.2
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / forms / form-customizer.php
secure-custom-fields / includes / forms Last commit date
form-attachment.php 1 year ago form-comment.php 1 year ago form-customizer.php 1 year ago form-front.php 1 year ago form-gutenberg.php 1 year ago form-nav-menu.php 1 year ago form-post.php 1 year ago form-taxonomy.php 1 year ago form-user.php 1 year ago form-widget.php 1 year ago index.php 1 year ago
form-customizer.php
430 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'acf_form_customizer' ) ) :
8 #[AllowDynamicProperties]
9 class acf_form_customizer {
10
11
12 /**
13 * This function will setup the class functionality
14 *
15 * @type function
16 * @date 5/03/2014
17 * @since ACF 5.0.0
18 *
19 * @param n/a
20 * @return n/a
21 */
22 function __construct() {
23
24 // vars
25 $this->preview_values = array();
26 $this->preview_fields = array();
27 $this->preview_errors = array();
28
29 // actions
30 add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) );
31 add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ), 1, 1 );
32 add_action( 'customize_save', array( $this, 'customize_save' ), 1, 1 );
33
34 // save
35 add_filter( 'widget_update_callback', array( $this, 'save_widget' ), 10, 4 );
36 }
37
38
39 /**
40 * This action is run after post query but before any admin script / head actions.
41 * It is a good place to register all actions.
42 *
43 * @type action (admin_enqueue_scripts)
44 * @date 26/01/13
45 * @since ACF 3.6.0
46 *
47 * @param N/A
48 * @return N/A
49 */
50 function customize_controls_init() {
51
52 // load acf scripts
53 acf_enqueue_scripts(
54 array(
55 'context' => 'customize_controls',
56 )
57 );
58
59 // actions
60 add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ), 1 );
61 }
62
63
64 /**
65 * This function will hook into the widget update filter and save ACF data
66 *
67 * @type function
68 * @date 27/05/2015
69 * @since ACF 5.2.3
70 *
71 * @param $instance (array) widget settings
72 * @param $new_instance (array) widget settings
73 * @param $old_instance (array) widget settings
74 * @param $widget (object) widget info
75 * @return $instance
76 */
77 function save_widget( $instance, $new_instance, $old_instance, $widget ) {
78
79 // bail early if not valid (customize + acf values + nonce)
80 if ( ! isset( $_POST['wp_customize'] ) || ! isset( $new_instance['acf'] ) || ! acf_verify_nonce( 'widget' ) ) {
81 return $instance;
82 }
83
84 // vars
85 $data = array(
86 'post_id' => "widget_{$widget->id}",
87 'values' => array(),
88 'fields' => array(),
89 );
90
91 // append values
92 $data['values'] = $new_instance['acf'];
93
94 // append fields (name => key relationship) - used later in 'acf/get_field_reference' for customizer previews
95 foreach ( $data['values'] as $k => $v ) {
96
97 // get field
98 $field = acf_get_field( $k );
99
100 // continue if no field
101 if ( ! $field ) {
102 continue;
103 }
104
105 // update
106 $data['fields'][ $field['name'] ] = $field['key'];
107 }
108
109 // append data to instance
110 $instance['acf'] = $data;
111
112 // return
113 return $instance;
114 }
115
116
117 /**
118 * This function will return an array of cutomizer settings that include ACF data
119 * similar to `$customizer->settings();`
120 *
121 * @type function
122 * @date 22/03/2016
123 * @since ACF 5.3.2
124 *
125 * @param $customizer (object)
126 * @return $value (mixed)
127 */
128 function settings( $customizer ) {
129
130 // vars
131 $data = array();
132 $settings = $customizer->settings();
133
134 // bail early if no settings
135 if ( empty( $settings ) ) {
136 return false;
137 }
138
139 // loop over settings
140 foreach ( $settings as $setting ) {
141
142 // vars
143 $id = $setting->id;
144
145 // verify settings type
146 if ( substr( $id, 0, 6 ) == 'widget' || substr( $id, 0, 7 ) == 'nav_menu' ) {
147 // allow
148 } else {
149 continue;
150 }
151
152 // get value
153 $value = $setting->post_value();
154
155 // bail early if no acf
156 if ( ! is_array( $value ) || ! isset( $value['acf'] ) ) {
157 continue;
158 }
159
160 // set data
161 $setting->acf = $value['acf'];
162
163 // append
164 $data[] = $setting;
165 }
166
167 // bail early if no settings
168 if ( empty( $data ) ) {
169 return false;
170 }
171
172 // return
173 return $data;
174 }
175
176
177 /**
178 * This function is called when customizer preview is initialized
179 *
180 * @type function
181 * @date 22/03/2016
182 * @since ACF 5.3.2
183 *
184 * @param $customizer (object)
185 * @return n/a
186 */
187 function customize_preview_init( $customizer ) {
188
189 // get customizer settings (widgets)
190 $settings = $this->settings( $customizer );
191
192 // bail early if no settings
193 if ( empty( $settings ) ) {
194 return;
195 }
196
197 // append values
198 foreach ( $settings as $setting ) {
199
200 // get acf data
201 $data = $setting->acf;
202
203 // append acf_value to preview_values
204 $this->preview_values[ $data['post_id'] ] = $data['values'];
205 $this->preview_fields[ $data['post_id'] ] = $data['fields'];
206 }
207
208 // bail early if no preview_values
209 if ( empty( $this->preview_values ) ) {
210 return;
211 }
212
213 // add filters
214 add_filter( 'acf/pre_load_value', array( $this, 'pre_load_value' ), 10, 3 );
215 add_filter( 'acf/pre_load_reference', array( $this, 'pre_load_reference' ), 10, 3 );
216 }
217
218 /**
219 * pre_load_value
220 *
221 * Used to inject preview value
222 *
223 * @date 2/2/18
224 * @since ACF 5.6.5
225 *
226 * @param type $var Description. Default.
227 * @return type Description.
228 */
229 function pre_load_value( $value, $post_id, $field ) {
230
231 // check
232 if ( isset( $this->preview_values[ $post_id ][ $field['key'] ] ) ) {
233 return $this->preview_values[ $post_id ][ $field['key'] ];
234 }
235
236 // return
237 return $value;
238 }
239
240 /**
241 * pre_load_reference
242 *
243 * Used to inject preview value
244 *
245 * @date 2/2/18
246 * @since ACF 5.6.5
247 *
248 * @param type $var Description. Default.
249 * @return type Description.
250 */
251 function pre_load_reference( $field_key, $field_name, $post_id ) {
252
253 // check
254 if ( isset( $this->preview_fields[ $post_id ][ $field_name ] ) ) {
255 return $this->preview_fields[ $post_id ][ $field_name ];
256 }
257
258 // return
259 return $field_key;
260 }
261
262
263 /**
264 * This function is called when customizer saves a widget.
265 * Normally, the widget_update_callback filter would be used, but the customizer disables this and runs a custom action
266 * class-customizer-settings.php will save the widget data via the function set_root_value which uses update_option
267 *
268 * @type function
269 * @date 22/03/2016
270 * @since ACF 5.3.2
271 *
272 * @param $customizer (object)
273 * @return n/a
274 */
275 function customize_save( $customizer ) {
276
277 // get customizer settings (widgets)
278 $settings = $this->settings( $customizer );
279
280 // bail early if no settings
281 if ( empty( $settings ) ) {
282 return;
283 }
284
285 // append values
286 foreach ( $settings as $setting ) {
287
288 // get acf data
289 $data = $setting->acf;
290
291 // save acf data
292 acf_save_post( $data['post_id'], $data['values'] );
293
294 // remove [acf] data from saved widget array
295 $id_data = $setting->id_data();
296 add_filter( 'pre_update_option_' . $id_data['base'], array( $this, 'pre_update_option' ), 10, 3 );
297 }
298 }
299
300
301 /**
302 * This function will remove the [acf] data from widget insance
303 *
304 * @type function
305 * @date 22/03/2016
306 * @since ACF 5.3.2
307 *
308 * @param $post_id (int)
309 * @return $post_id (int)
310 */
311 function pre_update_option( $value, $option, $old_value ) {
312
313 // bail early if no value
314 if ( empty( $value ) ) {
315 return $value;
316 }
317
318 // loop over widgets
319 // WP saves all widgets (of the same type) as an array of widgets
320 foreach ( $value as $i => $widget ) {
321
322 // bail early if no acf
323 if ( ! isset( $widget['acf'] ) ) {
324 continue;
325 }
326
327 // remove widget
328 unset( $value[ $i ]['acf'] );
329 }
330
331 // return
332 return $value;
333 }
334
335
336 /**
337 * This function will add some custom HTML to the footer of the edit page
338 *
339 * @type function
340 * @date 11/06/2014
341 * @since ACF 5.0.0
342 *
343 * @param n/a
344 * @return n/a
345 */
346 function admin_footer() {
347
348 ?>
349 <script type="text/javascript">
350 (function($) {
351
352 // customizer saves widget on any input change, so unload is not needed
353 acf.unload.active = 0;
354
355
356 // hack customizer function to remove bug caused by WYSIWYG field using aunique ID
357 // customizer compares returned AJAX HTML with the HTML of the widget form.
358 // the _getInputsSignature() function is used to generate a string based of input name + id.
359 // because ACF generates a unique ID on the WYSIWYG field, this string will not match causing the preview function to bail.
360 // an attempt was made to remove the WYSIWYG unique ID, but this caused multiple issues in the wp-admin and altimately doesn't make sense with the tinymce rule that all editors must have a unique ID.
361 // source: wp-admin/js/customize-widgets.js
362
363 // vars
364 var WidgetControl = wp.customize.Widgets.WidgetControl.prototype;
365
366
367 // backup functions
368 WidgetControl.__getInputsSignature = WidgetControl._getInputsSignature;
369 WidgetControl.__setInputState = WidgetControl._setInputState;
370
371
372 // modify __getInputsSignature
373 WidgetControl._getInputsSignature = function( inputs ) {
374
375 // vars
376 var signature = this.__getInputsSignature( inputs );
377 safe = [];
378
379
380 // split
381 signature = signature.split(';');
382
383
384 // loop
385 for( var i in signature ) {
386
387 // vars
388 var bit = signature[i];
389
390
391 // bail early if acf is found
392 if( bit.indexOf('acf') !== -1 ) continue;
393
394
395 // append
396 safe.push( bit );
397
398 }
399
400
401 // update
402 signature = safe.join(';');
403
404
405 // return
406 return signature;
407
408 };
409
410
411 // modify _setInputState
412 // this function deosn't seem to run on widget title/content, only custom fields
413 // either way, this function is not needed and will break ACF fields
414 WidgetControl._setInputState = function( input, state ) {
415
416 return true;
417
418 };
419
420 })(jQuery);
421 </script>
422 <?php
423 }
424 }
425
426 new acf_form_customizer();
427 endif;
428
429 ?>
430