Complex classes like Kirki_Field often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Kirki_Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Kirki_Field { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The ID of the kirki_config we're using. |
||
| 21 | * |
||
| 22 | * @see Kirki_Config |
||
| 23 | * @access protected |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $kirki_config = 'global'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Thje capability required so that users can edit this field. |
||
| 30 | * |
||
| 31 | * @access protected |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $capability = 'edit_theme_options'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * If we're using options instead of theme_mods |
||
| 38 | * and we want them serialized, this is the option that |
||
| 39 | * will saved in the db. |
||
| 40 | * |
||
| 41 | * @access protected |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $option_name = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Custom input attributes (defined as an array). |
||
| 48 | * |
||
| 49 | * @access protected |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $input_attrs = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Use "theme_mod" or "option". |
||
| 56 | * |
||
| 57 | * @access protected |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $option_type = 'theme_mod'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The name of this setting (id for the db). |
||
| 64 | * |
||
| 65 | * @access protected |
||
| 66 | * @var string|array |
||
| 67 | */ |
||
| 68 | protected $settings = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Set to true if you want to disable all CSS output for this field. |
||
| 72 | * |
||
| 73 | * @access protected |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | protected $disable_output = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The field type. |
||
| 80 | * |
||
| 81 | * @access protected |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $type = 'kirki-generic'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Some fields require options to be set. |
||
| 88 | * We're whitelisting the property here |
||
| 89 | * and suggest you validate this in a child class. |
||
| 90 | * |
||
| 91 | * @access protected |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $choices = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Assign this field to a section. |
||
| 98 | * Fields not assigned to a section will not be displayed in the customizer. |
||
| 99 | * |
||
| 100 | * @access protected |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $section = ''; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The default value for this field. |
||
| 107 | * |
||
| 108 | * @access protected |
||
| 109 | * @var string|array|bool |
||
| 110 | */ |
||
| 111 | protected $default = ''; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Priority determines the position of a control inside a section. |
||
| 115 | * Lower priority numbers move the control to the top. |
||
| 116 | * |
||
| 117 | * @access protected |
||
| 118 | * @var int |
||
| 119 | */ |
||
| 120 | protected $priority = 10; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Unique ID for this field. |
||
| 124 | * This is auto-calculated from the $settings argument. |
||
| 125 | * |
||
| 126 | * @access protected |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | protected $id = ''; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Use if you want to automatically generate CSS from this field's value. |
||
| 133 | * |
||
| 134 | * @see https://kirki.org/docs/arguments/output |
||
| 135 | * @access protected |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | protected $output = array(); |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Use to automatically generate postMessage scripts. |
||
| 142 | * Not necessary to use if you use 'transport' => 'auto' |
||
| 143 | * and have already set an array for the 'output' argument. |
||
| 144 | * |
||
| 145 | * @see https://kirki.org/docs/arguments/js_vars |
||
| 146 | * @access protected |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | protected $js_vars = array(); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * If you want to use a CSS compiler, then use this to set the variable names. |
||
| 153 | * |
||
| 154 | * @see https://kirki.org/docs/arguments/variables |
||
| 155 | * @access protected |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | protected $variables = array(); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Text that will be used in a tooltip to provide extra info for this field. |
||
| 162 | * |
||
| 163 | * @access protected |
||
| 164 | * @var string |
||
| 165 | */ |
||
| 166 | protected $tooltip = ''; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Whitelisting for backwards-compatibility. |
||
| 170 | * |
||
| 171 | * @access protected |
||
| 172 | * @var string |
||
| 173 | */ |
||
| 174 | protected $help = ''; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * A custom callback to determine if the field should be visible or not. |
||
| 178 | * |
||
| 179 | * @access protected |
||
| 180 | * @var string|array |
||
| 181 | */ |
||
| 182 | protected $active_callback = '__return_true'; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * A custom sanitize callback that will be used to properly save the values. |
||
| 186 | * |
||
| 187 | * @access protected |
||
| 188 | * @var string|array |
||
| 189 | */ |
||
| 190 | protected $sanitize_callback = ''; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Use 'refresh', 'postMessage' or 'auto'. |
||
| 194 | * 'auto' will automatically geberate any 'js_vars' from the 'output' argument. |
||
| 195 | * |
||
| 196 | * @access protected |
||
| 197 | * @var string |
||
| 198 | */ |
||
| 199 | protected $transport = 'refresh'; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Define dependencies to show/hide this field based on the values of other fields. |
||
| 203 | * |
||
| 204 | * @access protected |
||
| 205 | * @var array |
||
| 206 | */ |
||
| 207 | protected $required = array(); |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Suggested width for cropped image. |
||
| 211 | * |
||
| 212 | * @access protected |
||
| 213 | * @var int |
||
| 214 | */ |
||
| 215 | protected $width = 150; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Suggested height for cropped image. |
||
| 219 | * |
||
| 220 | * @access protected |
||
| 221 | * @var int |
||
| 222 | */ |
||
| 223 | protected $height = 150; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Whether the width is flexible for cropped image. |
||
| 227 | * |
||
| 228 | * @access protected |
||
| 229 | * @var bool |
||
| 230 | */ |
||
| 231 | protected $flex_width = false; |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Whether the height is flexible for cropped image. |
||
| 235 | * |
||
| 236 | * @access protected |
||
| 237 | * @var bool |
||
| 238 | */ |
||
| 239 | protected $flex_height = false; |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Contain the settings for the repeater rows labels |
||
| 243 | * |
||
| 244 | * @access protected |
||
| 245 | * @var array |
||
| 246 | */ |
||
| 247 | protected $row_label = array(); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Partial Refreshes array. |
||
| 251 | * |
||
| 252 | * @access protected |
||
| 253 | * @var array |
||
| 254 | */ |
||
| 255 | protected $partial_refresh = array(); |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Use only on image, cropped_image, upload controls. |
||
| 259 | * Limit the Media library to a specific mime type |
||
| 260 | * |
||
| 261 | * @access protected |
||
| 262 | * @var array |
||
| 263 | */ |
||
| 264 | protected $mime_type = ''; |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Used by image fields. |
||
| 268 | * |
||
| 269 | * @access protected |
||
| 270 | * @var array |
||
| 271 | * @since 3.0.0 |
||
| 272 | */ |
||
| 273 | protected $button_labels = array(); |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Use only on select controls. |
||
| 277 | * Defines if this is a multi-select or not. |
||
| 278 | * If value is > 1, then the maximum number of selectable options |
||
| 279 | * is the number defined here. |
||
| 280 | * |
||
| 281 | * @access protected |
||
| 282 | * @var integer |
||
| 283 | */ |
||
| 284 | protected $multiple = 1; |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Allows fields to be collapsible. |
||
| 288 | * |
||
| 289 | * @access protected |
||
| 290 | * @since 3.0.0 |
||
| 291 | * @var bool |
||
| 292 | */ |
||
| 293 | protected $collapsible = false; |
||
| 294 | |||
| 295 | /** |
||
| 296 | * The class constructor. |
||
| 297 | * Parses and sanitizes all field arguments. |
||
| 298 | * Then it adds the field to Kirki::$fields. |
||
| 299 | * |
||
| 300 | * @access public |
||
| 301 | * @param string $config_id The ID of the config we want to use. |
||
| 302 | * Defaults to "global". |
||
| 303 | * Configs are handled by the Kirki_Config class. |
||
| 304 | * @param array $args The arguments of the field. |
||
| 305 | */ |
||
| 306 | public function __construct( $config_id = 'global', $args = array() ) { |
||
| 307 | |||
| 308 | if ( isset( $args['setting'] ) && ! empty( $args['setting'] ) && ( ! isset( $args['settings'] ) || empty( $args['settings'] ) ) ) { |
||
| 309 | /* translators: %s represents the field ID where the error occurs. */ |
||
| 310 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Typo found in field %s - setting instead of settings.', 'kirki' ), esc_attr( $args['settings'] ) ), '3.0.10' ); |
||
| 311 | $args['settings'] = $args['setting']; |
||
| 312 | unset( $args['setting'] ); |
||
| 313 | } |
||
| 314 | |||
| 315 | // In case the user only provides 1 argument, |
||
| 316 | // assume that the provided argument is $args and set $config_id = 'global'. |
||
| 317 | if ( is_array( $config_id ) && empty( $args ) ) { |
||
| 318 | /* translators: %1$s represents the field ID where the error occurs. %2$s is the URL in the documentation site. */ |
||
| 319 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Config not defined for field %1$s - See %2$s for details on how to properly add fields.', 'kirki' ), esc_attr( $args['settings'] ), 'https://aristath.github.io/kirki/docs/getting-started/fields.html' ), '3.0.10' ); |
||
| 320 | $args = $config_id; |
||
| 321 | $config_id = 'global'; |
||
| 322 | } |
||
| 323 | |||
| 324 | $args['kirki_config'] = $config_id; |
||
| 325 | |||
| 326 | $this->kirki_config = trim( esc_attr( $config_id ) ); |
||
| 327 | if ( '' === $config_id ) { |
||
| 328 | /* translators: %1$s represents the field ID where the error occurs. %2$s is the URL in the documentation site. */ |
||
| 329 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Config not defined for field %1$s - See %2$s for details on how to properly add fields.', 'kirki' ), esc_attr( $args['settings'] ), 'https://aristath.github.io/kirki/docs/getting-started/fields.html' ), '3.0.10' ); |
||
| 330 | $this->kirki_config = 'global'; |
||
| 331 | } |
||
| 332 | |||
| 333 | // Get defaults from the class. |
||
| 334 | $defaults = get_class_vars( __CLASS__ ); |
||
| 335 | |||
| 336 | // Get the config arguments, and merge them with the defaults. |
||
| 337 | $config_defaults = ( isset( Kirki::$config['global'] ) ) ? Kirki::$config['global'] : array(); |
||
| 338 | if ( 'global' !== $this->kirki_config && isset( Kirki::$config[ $this->kirki_config ] ) ) { |
||
| 339 | $config_defaults = Kirki::$config[ $this->kirki_config ]; |
||
| 340 | } |
||
| 341 | $config_defaults = ( is_array( $config_defaults ) ) ? $config_defaults : array(); |
||
| 342 | foreach ( $config_defaults as $key => $value ) { |
||
| 343 | if ( isset( $defaults[ $key ] ) && ! empty( $value ) && $value != $defaults[ $key ] ) { |
||
| 344 | $defaults[ $key ] = $value; |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | // Merge our args with the defaults. |
||
| 349 | $args = wp_parse_args( $args, $defaults ); |
||
| 350 | |||
| 351 | // Set the class properties using the parsed args. |
||
| 352 | foreach ( $args as $key => $value ) { |
||
| 353 | $this->$key = $value; |
||
| 354 | } |
||
| 355 | |||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Processes the field arguments |
||
| 360 | * |
||
| 361 | * @access protected |
||
| 362 | */ |
||
| 363 | protected function set_field() { |
||
| 364 | |||
| 365 | $properties = get_class_vars( __CLASS__ ); |
||
| 366 | |||
| 367 | // Some things must run before the others. |
||
| 368 | $priorities = array( |
||
| 369 | 'option_name', |
||
| 370 | 'option_type', |
||
| 371 | 'settings', |
||
| 372 | ); |
||
| 373 | |||
| 374 | foreach ( $priorities as $priority ) { |
||
| 375 | if ( method_exists( $this, 'set_' . $priority ) ) { |
||
| 376 | $method_name = 'set_' . $priority; |
||
| 377 | $this->$method_name(); |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | // Sanitize the properties, skipping the ones run from the $priorities. |
||
| 382 | foreach ( $properties as $property => $value ) { |
||
| 383 | if ( in_array( $property, $priorities, true ) ) { |
||
| 384 | continue; |
||
| 385 | } |
||
| 386 | if ( method_exists( $this, 'set_' . $property ) ) { |
||
| 387 | $method_name = 'set_' . $property; |
||
| 388 | $this->$method_name(); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | // Get all arguments with their values. |
||
| 393 | $args = get_object_vars( $this ); |
||
| 394 | foreach ( $args as $key => $default_value ) { |
||
| 395 | $args[ $key ] = $this->$key; |
||
| 396 | } |
||
| 397 | |||
| 398 | // Add the field to the static $fields variable properly indexed. |
||
| 399 | Kirki::$fields[ $this->settings ] = $args; |
||
| 400 | |||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * No need to do anything, these are escaped on the fields themselves. |
||
| 405 | * |
||
| 406 | * @access protected |
||
| 407 | */ |
||
| 408 | protected function set_label() {} |
||
| 409 | |||
| 410 | /** |
||
| 411 | * No need to do anything, these are escaped on the fields themselves. |
||
| 412 | * |
||
| 413 | * @access protected |
||
| 414 | */ |
||
| 415 | protected function set_description() {} |
||
| 416 | |||
| 417 | /** |
||
| 418 | * No need to do anything, these are escaped on the fields themselves. |
||
| 419 | * |
||
| 420 | * @access protected |
||
| 421 | */ |
||
| 422 | protected function set_mode() {} |
||
| 423 | |||
| 424 | /** |
||
| 425 | * No need to do anything, these are escaped on the fields themselves. |
||
| 426 | * Only used in repeaters. |
||
| 427 | * |
||
| 428 | * @access protected |
||
| 429 | */ |
||
| 430 | protected function set_fields() {} |
||
| 431 | |||
| 432 | /** |
||
| 433 | * No need to do anything, these are escaped on the fields themselves. |
||
| 434 | * Only used in repeaters. |
||
| 435 | * |
||
| 436 | * @access protected |
||
| 437 | */ |
||
| 438 | protected function set_row_label() {} |
||
| 439 | |||
| 440 | /** |
||
| 441 | * This allows us to process this on a field-basis |
||
| 442 | * by using sub-classes which can override this method. |
||
| 443 | * |
||
| 444 | * @access protected |
||
| 445 | */ |
||
| 446 | protected function set_default() {} |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Escape $kirki_config. |
||
| 450 | * |
||
| 451 | * @access protected |
||
| 452 | */ |
||
| 453 | protected function set_kirki_config() { |
||
| 454 | |||
| 455 | $this->kirki_config = esc_attr( $this->kirki_config ); |
||
| 456 | |||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Escape $option_name. |
||
| 461 | * |
||
| 462 | * @access protected |
||
| 463 | */ |
||
| 464 | protected function set_option_name() { |
||
| 465 | |||
| 466 | $this->option_name = esc_attr( $this->option_name ); |
||
| 467 | |||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Escape the $section. |
||
| 472 | * |
||
| 473 | * @access protected |
||
| 474 | */ |
||
| 475 | protected function set_section() { |
||
| 476 | |||
| 477 | $this->section = sanitize_key( $this->section ); |
||
| 478 | |||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Escape the $section. |
||
| 483 | * |
||
| 484 | * @access protected |
||
| 485 | */ |
||
| 486 | protected function set_input_attrs() { |
||
| 487 | |||
| 488 | if ( ! is_array( $this->input_attrs ) ) { |
||
| 489 | $this->input_attrs = array(); |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Checks the capability chosen is valid. |
||
| 495 | * If not, then falls back to 'edit_theme_options' |
||
| 496 | * |
||
| 497 | * @access protected |
||
| 498 | */ |
||
| 499 | protected function set_capability() { |
||
| 500 | |||
| 501 | // Early exit if we're using 'edit_theme_options'. |
||
| 502 | if ( 'edit_theme_options' === $this->capability ) { |
||
| 503 | return; |
||
| 504 | } |
||
| 505 | // Escape & trim the capability. |
||
| 506 | $this->capability = trim( esc_attr( $this->capability ) ); |
||
| 507 | |||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Make sure we're using the correct option_type |
||
| 512 | * |
||
| 513 | * @access protected |
||
| 514 | */ |
||
| 515 | protected function set_option_type() { |
||
| 516 | |||
| 517 | // Take care of common typos. |
||
| 518 | if ( 'options' === $this->option_type ) { |
||
| 519 | $this->option_type = 'option'; |
||
| 520 | } |
||
| 521 | // Take care of common typos. |
||
| 522 | if ( 'theme_mods' === $this->option_type ) { |
||
| 523 | /* translators: %1$s represents the field ID where the error occurs. */ |
||
| 524 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( 'Typo found in field %s - "theme_mods" vs "theme_mod"', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
||
| 525 | $this->option_type = 'theme_mod'; |
||
| 526 | } |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Modifications for partial refreshes. |
||
| 531 | * |
||
| 532 | * @access protected |
||
| 533 | */ |
||
| 534 | protected function set_partial_refresh() { |
||
| 535 | |||
| 536 | if ( ! is_array( $this->partial_refresh ) ) { |
||
| 537 | $this->partial_refresh = array(); |
||
| 538 | } |
||
| 539 | foreach ( $this->partial_refresh as $id => $args ) { |
||
| 540 | if ( ! is_array( $args ) || ! isset( $args['selector'] ) || ! isset( $args['render_callback'] ) || ! is_callable( $args['render_callback'] ) ) { |
||
| 541 | /* translators: %1$s represents the field ID where the error occurs. */ |
||
| 542 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"partial_refresh" invalid entry in field %s', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
||
| 543 | unset( $this->partial_refresh[ $id ] ); |
||
| 544 | continue; |
||
| 545 | } |
||
| 546 | } |
||
| 547 | if ( ! empty( $this->partial_refresh ) ) { |
||
| 548 | $this->transport = 'postMessage'; |
||
| 549 | } |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Sets the settings. |
||
| 554 | * If we're using serialized options it makes sure that settings are properly formatted. |
||
| 555 | * We'll also be escaping all setting names here for consistency. |
||
| 556 | * |
||
| 557 | * @access protected |
||
| 558 | */ |
||
| 559 | protected function set_settings() { |
||
| 560 | |||
| 561 | // If settings is not an array, temporarily convert it to an array. |
||
| 562 | // This is just to allow us to process everything the same way and avoid code duplication. |
||
| 563 | // if settings is not an array then it will not be set as an array in the end. |
||
| 564 | if ( ! is_array( $this->settings ) ) { |
||
| 565 | $this->settings = array( |
||
| 566 | 'kirki_placeholder_setting' => $this->settings, |
||
| 567 | ); |
||
| 568 | } |
||
| 569 | $settings = array(); |
||
| 570 | foreach ( $this->settings as $setting_key => $setting_value ) { |
||
| 571 | $settings[ sanitize_key( $setting_key ) ] = esc_attr( $setting_value ); |
||
| 572 | // If we're using serialized options then we need to spice this up. |
||
| 573 | if ( 'option' === $this->option_type && '' !== $this->option_name && ( false === strpos( $setting_key, '[' ) ) ) { |
||
| 574 | $settings[ sanitize_key( $setting_key ) ] = esc_attr( $this->option_name ) . '[' . esc_attr( $setting_value ) . ']'; |
||
| 575 | } |
||
| 576 | } |
||
| 577 | $this->settings = $settings; |
||
| 578 | if ( isset( $this->settings['kirki_placeholder_setting'] ) ) { |
||
| 579 | $this->settings = $this->settings['kirki_placeholder_setting']; |
||
| 580 | } |
||
| 581 | |||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Escapes the tooltip messages. |
||
| 586 | * |
||
| 587 | * @access protected |
||
| 588 | */ |
||
| 589 | protected function set_tooltip() { |
||
| 590 | |||
| 591 | if ( '' !== $this->tooltip ) { |
||
| 592 | $this->tooltip = wp_strip_all_tags( $this->tooltip ); |
||
| 593 | return; |
||
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Sets the active_callback |
||
| 599 | * If we're using the $required argument, |
||
| 600 | * Then this is where the switch is made to our evaluation method. |
||
| 601 | * |
||
| 602 | * @access protected |
||
| 603 | */ |
||
| 604 | protected function set_active_callback() { |
||
| 605 | |||
| 606 | if ( is_array( $this->active_callback ) && ! is_callable( $this->active_callback ) ) { |
||
| 607 | if ( isset( $this->active_callback[0] ) ) { |
||
| 608 | $this->required = $this->active_callback; |
||
| 609 | } |
||
| 610 | } |
||
| 611 | |||
| 612 | if ( ! empty( $this->required ) ) { |
||
| 613 | $this->active_callback = array( 'Kirki_Active_Callback', 'evaluate' ); |
||
| 614 | return; |
||
| 615 | } |
||
| 616 | // No need to proceed any further if we're using the default value. |
||
| 617 | if ( '__return_true' === $this->active_callback ) { |
||
| 618 | return; |
||
| 619 | } |
||
| 620 | // Make sure the function is callable, otherwise fallback to __return_true. |
||
| 621 | if ( ! is_callable( $this->active_callback ) ) { |
||
| 622 | $this->active_callback = '__return_true'; |
||
| 623 | } |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Sets the control type. |
||
| 628 | * |
||
| 629 | * @access protected |
||
| 630 | */ |
||
| 631 | protected function set_type() { |
||
| 632 | |||
| 633 | // Escape the control type (it doesn't hurt to be sure). |
||
| 634 | $this->type = esc_attr( $this->type ); |
||
| 635 | |||
| 636 | } |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Sets the $id. |
||
| 640 | * Setting the ID should happen after the 'settings' sanitization. |
||
| 641 | * This way we can also properly handle cases where the option_type is set to 'option' |
||
| 642 | * and we're using an array instead of individual options. |
||
| 643 | * |
||
| 644 | * @access protected |
||
| 645 | */ |
||
| 646 | protected function set_id() { |
||
| 647 | |||
| 648 | $this->id = sanitize_key( str_replace( '[', '-', str_replace( ']', '', $this->settings ) ) ); |
||
| 649 | |||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Sets the $sanitize_callback |
||
| 654 | * |
||
| 655 | * @access protected |
||
| 656 | */ |
||
| 657 | protected function set_sanitize_callback() {} |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Sets the $choices. |
||
| 661 | * |
||
| 662 | * @access protected |
||
| 663 | */ |
||
| 664 | protected function set_choices() { |
||
| 665 | |||
| 666 | if ( ! is_array( $this->choices ) ) { |
||
| 667 | $this->choices = array(); |
||
| 668 | } |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Escapes the $disable_output. |
||
| 673 | * |
||
| 674 | * @access protected |
||
| 675 | */ |
||
| 676 | protected function set_disable_output() { |
||
| 677 | |||
| 678 | $this->disable_output = (bool) $this->disable_output; |
||
| 679 | |||
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Sets the $sanitize_callback |
||
| 684 | * |
||
| 685 | * @access protected |
||
| 686 | */ |
||
| 687 | protected function set_output() { |
||
| 688 | |||
| 689 | if ( empty( $this->output ) ) { |
||
| 690 | return; |
||
| 691 | } |
||
| 692 | if ( ! empty( $this->output ) && ! is_array( $this->output ) ) { |
||
| 693 | /* translators: %s represents the field ID where the error occurs. */ |
||
| 694 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"output" invalid format in field %s. The "output" argument should be defined as an array of arrays.', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
||
| 695 | $this->output = array( |
||
| 696 | array( |
||
| 697 | 'element' => $this->output, |
||
| 698 | ), |
||
| 699 | ); |
||
| 700 | } |
||
| 701 | // Convert to array of arrays if needed. |
||
| 702 | if ( isset( $this->output['element'] ) ) { |
||
| 703 | /* translators: %s represents the field ID where the error occurs. */ |
||
| 704 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"output" invalid format in field %s. The "output" argument should be defined as an array of arrays.', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
||
| 705 | $this->output = array( $this->output ); |
||
| 706 | } |
||
| 707 | $outputs = array(); |
||
| 708 | foreach ( $this->output as $output ) { |
||
| 709 | if ( ! isset( $output['element'] ) || ( ! isset( $output['property'] ) && ! in_array( $this->type, array( 'kirki-typography', 'kirki-background' ), true ) ) ) { |
||
| 710 | continue; |
||
| 711 | } |
||
| 712 | if ( ! isset( $output['sanitize_callback'] ) && isset( $output['callback'] ) ) { |
||
| 713 | $output['sanitize_callback'] = $output['callback']; |
||
| 714 | } |
||
| 715 | // Convert element arrays to strings. |
||
| 716 | if ( is_array( $output['element'] ) ) { |
||
| 717 | $output['element'] = array_unique( $output['element'] ); |
||
| 718 | sort( $output['element'] ); |
||
| 719 | $output['element'] = implode( ',', $output['element'] ); |
||
| 720 | } |
||
| 721 | $outputs[] = array( |
||
| 722 | 'element' => $output['element'], |
||
| 723 | 'property' => ( isset( $output['property'] ) ) ? $output['property'] : '', |
||
| 724 | 'media_query' => ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global', |
||
| 725 | 'sanitize_callback' => ( isset( $output['sanitize_callback'] ) ) ? $output['sanitize_callback'] : '', |
||
| 726 | 'units' => ( isset( $output['units'] ) ) ? $output['units'] : '', |
||
| 727 | 'prefix' => ( isset( $output['prefix'] ) ) ? $output['prefix'] : '', |
||
| 728 | 'suffix' => ( isset( $output['suffix'] ) ) ? $output['suffix'] : '', |
||
| 729 | 'exclude' => ( isset( $output['exclude'] ) ) ? $output['exclude'] : false, |
||
| 730 | ); |
||
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Sets the $js_vars |
||
| 736 | * |
||
| 737 | * @access protected |
||
| 738 | */ |
||
| 739 | protected function set_js_vars() { |
||
| 740 | |||
| 741 | if ( ! is_array( $this->js_vars ) ) { |
||
| 742 | $this->js_vars = array(); |
||
| 743 | } |
||
| 744 | |||
| 745 | // Check if transport is set to auto. |
||
| 746 | // If not, then skip the auto-calculations and exit early. |
||
| 747 | if ( 'auto' !== $this->transport ) { |
||
| 748 | return; |
||
| 749 | } |
||
| 750 | |||
| 751 | // Set transport to refresh initially. |
||
| 752 | // Serves as a fallback in case we failt to auto-calculate js_vars. |
||
| 753 | $this->transport = 'refresh'; |
||
| 754 | |||
| 755 | $js_vars = array(); |
||
| 756 | |||
| 757 | // Try to auto-generate js_vars. |
||
| 758 | // First we need to check if js_vars are empty, and that output is not empty. |
||
| 759 | if ( empty( $this->js_vars ) && ! empty( $this->output ) ) { |
||
| 760 | |||
| 761 | // Start going through each item in the $output array. |
||
| 762 | foreach ( $this->output as $output ) { |
||
| 763 | $output['function'] = ( isset( $output['function'] ) ) ? $output['function'] : 'style'; |
||
| 764 | |||
| 765 | // If 'element' or 'property' are not defined, skip this. |
||
| 766 | if ( ! isset( $output['element'] ) || ! isset( $output['property'] ) ) { |
||
| 767 | continue; |
||
| 768 | } |
||
| 769 | if ( is_array( $output['element'] ) ) { |
||
| 770 | $output['element'] = implode( ',', $output['element'] ); |
||
| 771 | } |
||
| 772 | |||
| 773 | // If there's a sanitize_callback defined skip this, unless we also have a js_callback defined. |
||
| 774 | if ( isset( $output['sanitize_callback'] ) && ! empty( $output['sanitize_callback'] ) && ! isset( $output['js_callback'] ) ) { |
||
| 775 | continue; |
||
| 776 | } |
||
| 777 | |||
| 778 | // If we got this far, it's safe to add this. |
||
| 779 | $js_vars[] = $output; |
||
| 780 | } |
||
| 781 | |||
| 782 | // Did we manage to get all the items from 'output'? |
||
| 783 | // If not, then we're missing something so don't add this. |
||
| 784 | if ( count( $js_vars ) !== count( $this->output ) ) { |
||
| 785 | return; |
||
| 786 | } |
||
| 787 | $this->js_vars = $js_vars; |
||
| 788 | $this->transport = 'postMessage'; |
||
| 789 | |||
| 790 | } |
||
| 791 | } |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Sets the $variables |
||
| 795 | * |
||
| 796 | * @access protected |
||
| 797 | */ |
||
| 798 | protected function set_variables() { |
||
| 808 | |||
| 809 | /** |
||
| 810 | * This is a fallback method: |
||
| 811 | * $help has now become $tooltip, so this just migrates the data |
||
| 812 | * |
||
| 813 | * @access protected |
||
| 814 | */ |
||
| 815 | protected function set_help() { |
||
| 816 | |||
| 817 | if ( '' !== $this->tooltip ) { |
||
| 818 | return; |
||
| 819 | } |
||
| 820 | if ( '' !== $this->help ) { |
||
| 821 | /* translators: %s represents the field ID where the error occurs. */ |
||
| 822 | _doing_it_wrong( __METHOD__, sprintf( esc_attr__( '"help" argument has been deprecated in favor of "tooltip". Error in field %s.', 'kirki' ), esc_attr( $this->settings ) ), '3.0.10' ); |
||
| 823 | $this->tooltip = wp_strip_all_tags( $this->help ); |
||
| 824 | $this->help = ''; |
||
| 825 | return; |
||
| 826 | } |
||
| 827 | } |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Sets the $transport |
||
| 831 | * |
||
| 832 | * @access protected |
||
| 833 | */ |
||
| 834 | protected function set_transport() { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Sets the $required |
||
| 843 | * |
||
| 844 | * @access protected |
||
| 845 | */ |
||
| 846 | protected function set_required() { |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Sets the $priority |
||
| 855 | * |
||
| 856 | * @access protected |
||
| 857 | */ |
||
| 858 | protected function set_priority() { |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Sets the $collapsible var. |
||
| 866 | * |
||
| 867 | * @access protected |
||
| 868 | */ |
||
| 869 | protected function set_collapsible() { |
||
| 874 | } |
||
| 875 |