Field.php
282 lines
| 1 | <?php // phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure |
| 2 | /** |
| 3 | * WordPress Customizer API abstraction. |
| 4 | * |
| 5 | * @package kirki-framework/field |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 0.1 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Make it easier to create customizer settings & controls with a single call, |
| 19 | * register the control type if needed, run extra actions the the customizer. |
| 20 | * This is a simple abstraction which makes adding simple controls to the Customizer. |
| 21 | * |
| 22 | * This class is not meant to be used as-is, you'll need to extend it from a child class. |
| 23 | * |
| 24 | * @since 0.1 |
| 25 | */ |
| 26 | abstract class Field { |
| 27 | |
| 28 | /** |
| 29 | * The field arguments. |
| 30 | * |
| 31 | * @access protected |
| 32 | * @since 0.1 |
| 33 | * @var array |
| 34 | */ |
| 35 | protected $args; |
| 36 | |
| 37 | /** |
| 38 | * The control class-name. |
| 39 | * |
| 40 | * Use the full classname, with namespace included. |
| 41 | * Example: '\Kirki\Control\Color'. |
| 42 | * |
| 43 | * @access protected |
| 44 | * @since 0.1 |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $control_class; |
| 48 | |
| 49 | /** |
| 50 | * The setting class-name. |
| 51 | * |
| 52 | * @access protected |
| 53 | * @since 0.1 |
| 54 | * @var string|null |
| 55 | */ |
| 56 | protected $settings_class; |
| 57 | |
| 58 | /** |
| 59 | * Whether we should register the control class for JS-templating or not. |
| 60 | * |
| 61 | * @access protected |
| 62 | * @since 0.1 |
| 63 | * @var bool |
| 64 | */ |
| 65 | protected $control_has_js_template = false; |
| 66 | |
| 67 | /** |
| 68 | * Constructor. |
| 69 | * Registers any hooks we need to run. |
| 70 | * |
| 71 | * @access public |
| 72 | * @since 0.1 |
| 73 | * @param array $args The field arguments. |
| 74 | */ |
| 75 | public function __construct( $args ) { |
| 76 | |
| 77 | // Backward compatibility: Ensure $args is an array. |
| 78 | // Old code might pass a string (config_id) as first argument. |
| 79 | if ( ! is_array( $args ) ) { |
| 80 | $args = []; |
| 81 | } |
| 82 | |
| 83 | $control_class = property_exists( $this, 'control_class' ) && ! empty( $this->control_class ) ? $this->control_class : ''; |
| 84 | |
| 85 | // Allow 3rd parties to do their custom "init" work. |
| 86 | do_action( 'kirki_field_custom_init', $this, $args, $control_class ); |
| 87 | |
| 88 | // Allow 3rd parties to early stop the field from being registered. |
| 89 | if ( apply_filters( 'kirki_field_exclude_init', false, $this, $args ) ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Set the arguments in this object. |
| 94 | $this->args = $args; |
| 95 | |
| 96 | if ( ! isset( $this->args['settings'] ) ) { |
| 97 | $this->args['settings'] = md5( wp_json_encode( $this->args ) ); |
| 98 | } |
| 99 | |
| 100 | add_action( |
| 101 | 'wp_loaded', |
| 102 | function() { |
| 103 | do_action( 'kirki_field_init', $this->args, $this ); |
| 104 | } |
| 105 | ); |
| 106 | |
| 107 | add_action( |
| 108 | 'wp', |
| 109 | function() { |
| 110 | do_action( 'kirki_field_wp', $this->args, $this ); |
| 111 | } |
| 112 | ); |
| 113 | |
| 114 | $this->init( $this->args ); |
| 115 | |
| 116 | // Register control-type for JS-templating in the customizer. |
| 117 | add_action( 'customize_register', [ $this, 'register_control_type' ] ); |
| 118 | |
| 119 | // Add customizer setting. |
| 120 | add_action( 'customize_register', [ $this, 'add_setting' ] ); |
| 121 | |
| 122 | // Add customizer control. |
| 123 | add_action( 'customize_register', [ $this, 'add_control' ] ); |
| 124 | |
| 125 | // Add default filters. Can be overridden in child classes. |
| 126 | add_filter( 'kirki_field_add_setting_args', [ $this, 'filter_setting_args' ], 10, 2 ); |
| 127 | add_filter( 'kirki_field_add_control_args', [ $this, 'filter_control_args' ], 10, 2 ); |
| 128 | |
| 129 | // Copy $this->args to a variable to be added to Kirki::$all_fields global. |
| 130 | $field_args = $this->args; |
| 131 | |
| 132 | /** |
| 133 | * Kirki::$fields contains only fields which are not extending the new base Field. |
| 134 | * So we collect all fields and add them to Kirki::$all_fields. |
| 135 | * |
| 136 | * ! This patch is used by Kirki::get_option which calls Values::get_value method. |
| 137 | * Even though this is a patch, this is fine and still a good solution to handle backwards compatibility. |
| 138 | */ |
| 139 | \Kirki\Compatibility\Kirki::$all_fields[ $field_args['settings'] ] = $field_args; |
| 140 | |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Runs in the constructor. Can be used by child-classes to define extra logic. |
| 145 | * |
| 146 | * @access protected |
| 147 | * @since 0.1 |
| 148 | * @param array $args The field arguments. |
| 149 | * @return void |
| 150 | */ |
| 151 | protected function init( $args ) {} |
| 152 | |
| 153 | /** |
| 154 | * Register the control-type. |
| 155 | * |
| 156 | * @access public |
| 157 | * @since 0.1 |
| 158 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 159 | * @return void |
| 160 | */ |
| 161 | public function register_control_type( $wp_customize ) { |
| 162 | |
| 163 | if ( $this->control_class ) { |
| 164 | $wp_customize->register_control_type( $this->control_class ); |
| 165 | } |
| 166 | |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Filter setting args. |
| 171 | * |
| 172 | * @access public |
| 173 | * @since 0.1 |
| 174 | * @param array $args The field arguments. |
| 175 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 176 | * @return array |
| 177 | */ |
| 178 | public function filter_setting_args( $args, $wp_customize ) { |
| 179 | |
| 180 | return $args; |
| 181 | |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Filter control args. |
| 186 | * |
| 187 | * @access public |
| 188 | * @since 0.1 |
| 189 | * @param array $args The field arguments. |
| 190 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 191 | * @return array |
| 192 | */ |
| 193 | public function filter_control_args( $args, $wp_customize ) { |
| 194 | |
| 195 | return $args; |
| 196 | |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Registers the setting. |
| 201 | * |
| 202 | * @access public |
| 203 | * @since 0.1 |
| 204 | * @param WP_Customize_Manager $customizer The customizer instance. |
| 205 | * @return void |
| 206 | */ |
| 207 | public function add_setting( $customizer ) { |
| 208 | |
| 209 | $args = $this->args; |
| 210 | |
| 211 | // This is for postMessage purpose. |
| 212 | // @see wp-content/plugins/kirki/customizer/packages/modules/postmessage/src/Postmessage.php inside 'field_add_setting_args' method. |
| 213 | $args['type'] = isset( $this->type ) ? $this->type : ''; |
| 214 | |
| 215 | /** |
| 216 | * Allow filtering the arguments. |
| 217 | * |
| 218 | * @since 0.1 |
| 219 | * @param array $this->args The arguments. |
| 220 | * @param WP_Customize_Manager $customizer The customizer instance. |
| 221 | * @return array Return the arguments. |
| 222 | */ |
| 223 | $args = apply_filters( 'kirki_field_add_setting_args', $args, $customizer ); |
| 224 | |
| 225 | if ( ! isset( $args['settings'] ) || empty( $args['settings'] ) ) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | $setting_id = $args['settings']; |
| 230 | |
| 231 | $args = [ |
| 232 | 'type' => isset( $args['option_type'] ) ? $args['option_type'] : 'theme_mod', // 'type' here doesn't use the $args['type'] but instead checking the $args['option_type']. |
| 233 | 'capability' => isset( $args['capability'] ) ? $args['capability'] : 'edit_theme_options', |
| 234 | 'theme_supports' => isset( $args['theme_supports'] ) ? $args['theme_supports'] : '', |
| 235 | 'default' => isset( $args['default'] ) ? $args['default'] : '', |
| 236 | 'transport' => isset( $args['transport'] ) ? $args['transport'] : 'refresh', |
| 237 | 'sanitize_callback' => isset( $args['sanitize_callback'] ) ? $args['sanitize_callback'] : '', |
| 238 | 'sanitize_js_callback' => isset( $args['sanitize_js_callback'] ) ? $args['sanitize_js_callback'] : '', |
| 239 | ]; |
| 240 | |
| 241 | $settings_class = $this->settings_class ? $this->settings_class : null; |
| 242 | |
| 243 | if ( $settings_class ) { |
| 244 | $customizer->add_setting( new $settings_class( $customizer, $setting_id, $args ) ); |
| 245 | } else { |
| 246 | $customizer->add_setting( $setting_id, $args ); |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Registers the control. |
| 253 | * |
| 254 | * @access public |
| 255 | * @since 0.1 |
| 256 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 257 | * @return void |
| 258 | */ |
| 259 | public function add_control( $wp_customize ) { |
| 260 | |
| 261 | $control_class = $this->control_class; |
| 262 | |
| 263 | // If no class-name is defined, early exit. |
| 264 | if ( ! $control_class ) { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Allow filtering the arguments. |
| 270 | * |
| 271 | * @since 0.1 |
| 272 | * @param array $this->args The arguments. |
| 273 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 274 | * @return array Return the arguments. |
| 275 | */ |
| 276 | $args = apply_filters( 'kirki_field_add_control_args', $this->args, $wp_customize ); |
| 277 | |
| 278 | $wp_customize->add_control( new $control_class( $wp_customize, $this->args['settings'], $args ) ); |
| 279 | |
| 280 | } |
| 281 | |
| 282 | } |