deprecated
1 month ago
Aliases.php
2 months ago
Config.php
5 months ago
Control.php
5 months ago
Deprecated.php
5 months ago
Field.php
2 months ago
Framework.php
5 months ago
Init.php
2 months ago
Kirki.php
2 months ago
Modules.php
2 months ago
Pro_Namespace_Compatibility.php
5 months ago
Sanitize_Values.php
5 months ago
Scripts.php
2 months ago
Settings.php
5 months ago
Values.php
5 months ago
Control.php
130 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Controls handler |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @category Core |
| 7 | * @author Themeum |
| 8 | * @copyright Copyright (c) 2023, Themeum |
| 9 | * @license https://opensource.org/licenses/MIT |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Compatibility; |
| 13 | |
| 14 | /** |
| 15 | * Our main Kirki\Control object |
| 16 | */ |
| 17 | class Control { |
| 18 | |
| 19 | /** |
| 20 | * The $wp_customize WordPress global. |
| 21 | * |
| 22 | * @access protected |
| 23 | * @var WP_Customize_Manager |
| 24 | */ |
| 25 | protected $wp_customize; |
| 26 | |
| 27 | /** |
| 28 | * An array of all available control types. |
| 29 | * |
| 30 | * @access protected |
| 31 | * @var array |
| 32 | */ |
| 33 | protected static $control_types = []; |
| 34 | |
| 35 | /** |
| 36 | * The class constructor. |
| 37 | * Creates the actual controls in the customizer. |
| 38 | * |
| 39 | * @access public |
| 40 | * @param array $args The field definition as sanitized in Kirki\Field. |
| 41 | */ |
| 42 | public function __construct( $args ) { |
| 43 | |
| 44 | // Set the $wp_customize property. |
| 45 | global $wp_customize; |
| 46 | if ( ! $wp_customize ) { |
| 47 | return; |
| 48 | } |
| 49 | $this->wp_customize = $wp_customize; |
| 50 | |
| 51 | // Set the control types. |
| 52 | $this->set_control_types(); |
| 53 | |
| 54 | // Add the control. |
| 55 | $this->add_control( $args ); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the class name of the class needed to create tis control. |
| 61 | * |
| 62 | * @access private |
| 63 | * @param array $args The field definition as sanitized in Kirki\Field. |
| 64 | * |
| 65 | * @return string the name of the class that will be used to create this control. |
| 66 | */ |
| 67 | final function get_control_class_name( $args ) { |
| 68 | |
| 69 | // Set a default class name. |
| 70 | $class_name = 'WP_Customize_Control'; |
| 71 | |
| 72 | // Get the classname from the array of control classnames. |
| 73 | if ( array_key_exists( $args['type'], self::$control_types ) ) { |
| 74 | $class_name = self::$control_types[ $args['type'] ]; |
| 75 | } |
| 76 | return $class_name; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Adds the control. |
| 81 | * |
| 82 | * @access protected |
| 83 | * @param array $args The field definition as sanitized in Kirki\Field. |
| 84 | */ |
| 85 | final protected function add_control( $args ) { |
| 86 | |
| 87 | // Get the name of the class we're going to use. |
| 88 | $class_name = $this->get_control_class_name( $args ); |
| 89 | |
| 90 | /** |
| 91 | * Allow filtering the arguments. |
| 92 | * |
| 93 | * @since 0.1 |
| 94 | * @param array $args The arguments. |
| 95 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 96 | * @return array Return the arguments. |
| 97 | */ |
| 98 | $args = apply_filters( 'kirki_field_add_control_args', $args, $this->wp_customize ); |
| 99 | |
| 100 | // Add the control. |
| 101 | $this->wp_customize->add_control( new $class_name( $this->wp_customize, $args['settings'], $args ) ); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sets the $control_types property. |
| 107 | * Makes sure the kirki_control_types filter is applied |
| 108 | * and that the defined classes actually exist. |
| 109 | * If a defined class does not exist, it is removed. |
| 110 | * |
| 111 | * @access private |
| 112 | */ |
| 113 | final function set_control_types() { |
| 114 | |
| 115 | // Early exit if this has already run. |
| 116 | if ( ! empty( self::$control_types ) ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | self::$control_types = apply_filters( 'kirki_control_types', [] ); |
| 121 | |
| 122 | // Make sure the defined classes actually exist. |
| 123 | foreach ( self::$control_types as $key => $classname ) { |
| 124 | if ( ! class_exists( $classname ) ) { |
| 125 | unset( self::$control_types[ $key ] ); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 |