Skip to content

Commit 694fcc6

Browse files
authored
Form Customizer: Refactor to WP and PHPStan standards, add tests. (#84)
* Firts refactor commit * Test settings function * Test customize preview init * Finish tests on form customizer * Remove not needed customizer on block themes * Update tests to pass with old implementation
1 parent 93c1c8b commit 694fcc6

File tree

4 files changed

+554
-63
lines changed

4 files changed

+554
-63
lines changed

bin/baseline.neon

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -360,23 +360,6 @@ parameters:
360360
count: 1
361361
path: ../includes/fields/class-acf-field-wysiwyg.php
362362

363-
-
364-
message: '#^Access to an undefined property acf_form_customizer\:\:\$preview_errors\.$#'
365-
identifier: property.notFound
366-
count: 1
367-
path: ../includes/forms/form-customizer.php
368-
369-
-
370-
message: '#^Access to an undefined property acf_form_customizer\:\:\$preview_fields\.$#'
371-
identifier: property.notFound
372-
count: 2
373-
path: ../includes/forms/form-customizer.php
374-
375-
-
376-
message: '#^Access to an undefined property acf_form_customizer\:\:\$preview_values\.$#'
377-
identifier: property.notFound
378-
count: 2
379-
path: ../includes/forms/form-customizer.php
380363

381364
-
382365
message: '#^Action callback returns type but should not return anything\.$#'

includes/forms/form-customizer.php

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,42 @@
44
exit; // Exit if accessed directly
55
}
66

7-
if ( ! class_exists( 'acf_form_customizer' ) ) :
8-
#[AllowDynamicProperties]
9-
class acf_form_customizer {
7+
if ( ! class_exists( 'acf_form_customizer' ) || ! class_exists( 'ACF_Form_Customizer' ) ) :
8+
/**
9+
* ACF Form Customizer Class
10+
*
11+
* This class handles the integration of Advanced Custom Fields with the WordPress Customizer.
12+
* It manages preview values, fields, and errors for the customizer interface, and handles
13+
* saving ACF data when customizer changes are applied.
14+
*
15+
* @package wordpress/secure-custom-fields
16+
* @since ACF 3.6.0
17+
*/
18+
class ACF_Form_Customizer {
1019

1120

21+
/**
22+
* Values to be used in the preview.
23+
*
24+
* @var array
25+
*/
26+
public $preview_values;
27+
28+
/**
29+
* Fields to be used in the preview.
30+
*
31+
* @var array
32+
*/
33+
public $preview_fields;
34+
35+
36+
/**
37+
* Errors to be used in the preview.
38+
*
39+
* @var array
40+
*/
41+
public $preview_errors;
42+
1243
/**
1344
* This function will setup the class functionality
1445
*
@@ -19,7 +50,7 @@ class acf_form_customizer {
1950
* @param n/a
2051
* @return n/a
2152
*/
22-
function __construct() {
53+
public function __construct() {
2354

2455
// vars
2556
$this->preview_values = array();
@@ -43,11 +74,9 @@ function __construct() {
4374
* @type action (admin_enqueue_scripts)
4475
* @date 26/01/13
4576
* @since ACF 3.6.0
46-
*
47-
* @param N/A
48-
* @return N/A
77+
* @return void
4978
*/
50-
function customize_controls_init() {
79+
public function customize_controls_init() {
5180

5281
// load acf scripts
5382
acf_enqueue_scripts(
@@ -68,15 +97,16 @@ function customize_controls_init() {
6897
* @date 27/05/2015
6998
* @since ACF 5.2.3
7099
*
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
100+
* @param array $instance (array) widget settings.
101+
* @param array $new_instance (array) widget settings.
102+
* @param array $old_instance (array) widget settings.
103+
* @param object $widget (object) widget info.
104+
* @return array $instance Widget settings.
76105
*/
77-
function save_widget( $instance, $new_instance, $old_instance, $widget ) {
78-
106+
public function save_widget( $instance, $new_instance, $old_instance, $widget ) {
79107
// bail early if not valid (customize + acf values + nonce)
108+
109+
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce is verified in acf_verify_nonce.
80110
if ( ! isset( $_POST['wp_customize'] ) || ! isset( $new_instance['acf'] ) || ! acf_verify_nonce( 'widget' ) ) {
81111
return $instance;
82112
}
@@ -122,10 +152,10 @@ function save_widget( $instance, $new_instance, $old_instance, $widget ) {
122152
* @date 22/03/2016
123153
* @since ACF 5.3.2
124154
*
125-
* @param $customizer (object)
126-
* @return $value (mixed)
155+
* @param WP_Customize_Manager $customizer Customizer object.
156+
* @return Mixed boolean | array. The sCustomizer Settings Object.
127157
*/
128-
function settings( $customizer ) {
158+
public function settings( $customizer ) {
129159

130160
// vars
131161
$data = array();
@@ -141,14 +171,13 @@ function settings( $customizer ) {
141171

142172
// vars
143173
$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 {
174+
// Only process widget and nav_menu settings
175+
if ( 'widget' !== substr( $id, 0, 6 ) && 'nav_menu' !== substr( $id, 0, 7 ) ) {
149176
continue;
150177
}
151178

179+
// At this point, we're dealing with either a widget or nav_menu setting
180+
152181
// get value
153182
$value = $setting->post_value();
154183

@@ -181,10 +210,10 @@ function settings( $customizer ) {
181210
* @date 22/03/2016
182211
* @since ACF 5.3.2
183212
*
184-
* @param $customizer (object)
185-
* @return n/a
213+
* @param WP_Customize_Manager $customizer Customizer object.
214+
* @return void
186215
*/
187-
function customize_preview_init( $customizer ) {
216+
public function customize_preview_init( $customizer ) {
188217

189218
// get customizer settings (widgets)
190219
$settings = $this->settings( $customizer );
@@ -216,17 +245,19 @@ function customize_preview_init( $customizer ) {
216245
}
217246

218247
/**
219-
* pre_load_value
248+
* Pre load value function.
220249
*
221-
* Used to inject preview value
250+
* Used to inject preview value.
222251
*
223252
* @date 2/2/18
224253
* @since ACF 5.6.5
225254
*
226-
* @param type $var Description. Default.
227-
* @return type Description.
255+
* @param mixed $value The value to check.
256+
* @param int $post_id The post ID.
257+
* @param array $field The field array.
258+
* @return mixed The preview value if exists, otherwise the original value.
228259
*/
229-
function pre_load_value( $value, $post_id, $field ) {
260+
public function pre_load_value( $value, $post_id, $field ) {
230261

231262
// check
232263
if ( isset( $this->preview_values[ $post_id ][ $field['key'] ] ) ) {
@@ -238,17 +269,19 @@ function pre_load_value( $value, $post_id, $field ) {
238269
}
239270

240271
/**
241-
* pre_load_reference
272+
* Pre load reference function.
242273
*
243-
* Used to inject preview value
274+
* Used to inject reference value.
244275
*
245276
* @date 2/2/18
246277
* @since ACF 5.6.5
247278
*
248-
* @param type $var Description. Default.
249-
* @return type Description.
279+
* @param string $field_key The field key.
280+
* @param string $field_name The field name.
281+
* @param int $post_id The post ID.
282+
* @return string The field key if preview field exists, otherwise the original field key.
250283
*/
251-
function pre_load_reference( $field_key, $field_name, $post_id ) {
284+
public function pre_load_reference( $field_key, $field_name, $post_id ) {
252285

253286
// check
254287
if ( isset( $this->preview_fields[ $post_id ][ $field_name ] ) ) {
@@ -269,10 +302,10 @@ function pre_load_reference( $field_key, $field_name, $post_id ) {
269302
* @date 22/03/2016
270303
* @since ACF 5.3.2
271304
*
272-
* @param $customizer (object)
273-
* @return n/a
305+
* @param WP_Customize_Manager $customizer The WordPress customizer manager object.
306+
* @return void
274307
*/
275-
function customize_save( $customizer ) {
308+
public function customize_save( $customizer ) {
276309

277310
// get customizer settings (widgets)
278311
$settings = $this->settings( $customizer );
@@ -293,7 +326,7 @@ function customize_save( $customizer ) {
293326

294327
// remove [acf] data from saved widget array
295328
$id_data = $setting->id_data();
296-
add_filter( 'pre_update_option_' . $id_data['base'], array( $this, 'pre_update_option' ), 10, 3 );
329+
add_filter( 'pre_update_option_' . $id_data['base'], array( $this, 'pre_update_option' ), 10, 1 );
297330
}
298331
}
299332

@@ -305,10 +338,10 @@ function customize_save( $customizer ) {
305338
* @date 22/03/2016
306339
* @since ACF 5.3.2
307340
*
308-
* @param $post_id (int)
309-
* @return $post_id (int)
341+
* @param mixed $value The new option value.
342+
* @return mixed The filtered option value.
310343
*/
311-
function pre_update_option( $value, $option, $old_value ) {
344+
public function pre_update_option( $value ) {
312345

313346
// bail early if no value
314347
if ( empty( $value ) ) {
@@ -343,7 +376,7 @@ function pre_update_option( $value, $option, $old_value ) {
343376
* @param n/a
344377
* @return n/a
345378
*/
346-
function admin_footer() {
379+
public function admin_footer() {
347380

348381
?>
349382
<script type="text/javascript">
@@ -423,7 +456,7 @@ function admin_footer() {
423456
}
424457
}
425458

426-
new acf_form_customizer();
459+
new ACF_Form_Customizer();
427460
endif;
428461

429462
?>

0 commit comments

Comments
 (0)