Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form Customizer: Refactor to WP and PHPStan standards, add tests. #84

Merged
merged 10 commits into from
Mar 25, 2025
17 changes: 0 additions & 17 deletions bin/baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -360,23 +360,6 @@ parameters:
count: 1
path: ../includes/fields/class-acf-field-wysiwyg.php

-
message: '#^Access to an undefined property acf_form_customizer\:\:\$preview_errors\.$#'
identifier: property.notFound
count: 1
path: ../includes/forms/form-customizer.php

-
message: '#^Access to an undefined property acf_form_customizer\:\:\$preview_fields\.$#'
identifier: property.notFound
count: 2
path: ../includes/forms/form-customizer.php

-
message: '#^Access to an undefined property acf_form_customizer\:\:\$preview_values\.$#'
identifier: property.notFound
count: 2
path: ../includes/forms/form-customizer.php

-
message: '#^Action callback returns type but should not return anything\.$#'
Expand Down
123 changes: 78 additions & 45 deletions includes/forms/form-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,42 @@
exit; // Exit if accessed directly
}

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


/**
* Values to be used in the preview.
*
* @var array
*/
public $preview_values;

/**
* Fields to be used in the preview.
*
* @var array
*/
public $preview_fields;


/**
* Errors to be used in the preview.
*
* @var array
*/
public $preview_errors;

/**
* This function will setup the class functionality
*
Expand All @@ -19,7 +50,7 @@ class acf_form_customizer {
* @param n/a
* @return n/a
*/
function __construct() {
public function __construct() {

// vars
$this->preview_values = array();
Expand All @@ -43,11 +74,9 @@ function __construct() {
* @type action (admin_enqueue_scripts)
* @date 26/01/13
* @since ACF 3.6.0
*
* @param N/A
* @return N/A
* @return void
*/
function customize_controls_init() {
public function customize_controls_init() {

// load acf scripts
acf_enqueue_scripts(
Expand All @@ -68,15 +97,16 @@ function customize_controls_init() {
* @date 27/05/2015
* @since ACF 5.2.3
*
* @param $instance (array) widget settings
* @param $new_instance (array) widget settings
* @param $old_instance (array) widget settings
* @param $widget (object) widget info
* @return $instance
* @param array $instance (array) widget settings.
* @param array $new_instance (array) widget settings.
* @param array $old_instance (array) widget settings.
* @param object $widget (object) widget info.
* @return array $instance Widget settings.
*/
function save_widget( $instance, $new_instance, $old_instance, $widget ) {

public function save_widget( $instance, $new_instance, $old_instance, $widget ) {
// bail early if not valid (customize + acf values + nonce)

// phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce is verified in acf_verify_nonce.
if ( ! isset( $_POST['wp_customize'] ) || ! isset( $new_instance['acf'] ) || ! acf_verify_nonce( 'widget' ) ) {
return $instance;
}
Expand Down Expand Up @@ -122,10 +152,10 @@ function save_widget( $instance, $new_instance, $old_instance, $widget ) {
* @date 22/03/2016
* @since ACF 5.3.2
*
* @param $customizer (object)
* @return $value (mixed)
* @param WP_Customize_Manager $customizer Customizer object.
* @return Mixed boolean | array. The sCustomizer Settings Object.
*/
function settings( $customizer ) {
public function settings( $customizer ) {

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

// vars
$id = $setting->id;

// verify settings type
if ( substr( $id, 0, 6 ) == 'widget' || substr( $id, 0, 7 ) == 'nav_menu' ) {
// allow
} else {
// Only process widget and nav_menu settings
if ( 'widget' !== substr( $id, 0, 6 ) && 'nav_menu' !== substr( $id, 0, 7 ) ) {
continue;
}

// At this point, we're dealing with either a widget or nav_menu setting

// get value
$value = $setting->post_value();

Expand Down Expand Up @@ -181,10 +210,10 @@ function settings( $customizer ) {
* @date 22/03/2016
* @since ACF 5.3.2
*
* @param $customizer (object)
* @return n/a
* @param WP_Customize_Manager $customizer Customizer object.
* @return void
*/
function customize_preview_init( $customizer ) {
public function customize_preview_init( $customizer ) {

// get customizer settings (widgets)
$settings = $this->settings( $customizer );
Expand Down Expand Up @@ -216,17 +245,19 @@ function customize_preview_init( $customizer ) {
}

/**
* pre_load_value
* Pre load value function.
*
* Used to inject preview value
* Used to inject preview value.
*
* @date 2/2/18
* @since ACF 5.6.5
*
* @param type $var Description. Default.
* @return type Description.
* @param mixed $value The value to check.
* @param int $post_id The post ID.
* @param array $field The field array.
* @return mixed The preview value if exists, otherwise the original value.
*/
function pre_load_value( $value, $post_id, $field ) {
public function pre_load_value( $value, $post_id, $field ) {

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

/**
* pre_load_reference
* Pre load reference function.
*
* Used to inject preview value
* Used to inject reference value.
*
* @date 2/2/18
* @since ACF 5.6.5
*
* @param type $var Description. Default.
* @return type Description.
* @param string $field_key The field key.
* @param string $field_name The field name.
* @param int $post_id The post ID.
* @return string The field key if preview field exists, otherwise the original field key.
*/
function pre_load_reference( $field_key, $field_name, $post_id ) {
public function pre_load_reference( $field_key, $field_name, $post_id ) {

// check
if ( isset( $this->preview_fields[ $post_id ][ $field_name ] ) ) {
Expand All @@ -269,10 +302,10 @@ function pre_load_reference( $field_key, $field_name, $post_id ) {
* @date 22/03/2016
* @since ACF 5.3.2
*
* @param $customizer (object)
* @return n/a
* @param WP_Customize_Manager $customizer The WordPress customizer manager object.
* @return void
*/
function customize_save( $customizer ) {
public function customize_save( $customizer ) {

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

// remove [acf] data from saved widget array
$id_data = $setting->id_data();
add_filter( 'pre_update_option_' . $id_data['base'], array( $this, 'pre_update_option' ), 10, 3 );
add_filter( 'pre_update_option_' . $id_data['base'], array( $this, 'pre_update_option' ), 10, 1 );
}
}

Expand All @@ -305,10 +338,10 @@ function customize_save( $customizer ) {
* @date 22/03/2016
* @since ACF 5.3.2
*
* @param $post_id (int)
* @return $post_id (int)
* @param mixed $value The new option value.
* @return mixed The filtered option value.
*/
function pre_update_option( $value, $option, $old_value ) {
public function pre_update_option( $value ) {

// bail early if no value
if ( empty( $value ) ) {
Expand Down Expand Up @@ -343,7 +376,7 @@ function pre_update_option( $value, $option, $old_value ) {
* @param n/a
* @return n/a
*/
function admin_footer() {
public function admin_footer() {

?>
<script type="text/javascript">
Expand Down Expand Up @@ -423,7 +456,7 @@ function admin_footer() {
}
}

new acf_form_customizer();
new ACF_Form_Customizer();
endif;

?>
Loading