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
Config.php
180 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Processes configurations. |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @category Compatibility |
| 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 | * The Config object |
| 16 | */ |
| 17 | final class Config { |
| 18 | |
| 19 | /** |
| 20 | * Each instance is stored separately in this array. |
| 21 | * |
| 22 | * @static |
| 23 | * @access private |
| 24 | * @var array |
| 25 | */ |
| 26 | private static $instances = []; |
| 27 | |
| 28 | /** |
| 29 | * The finalized configuration array. |
| 30 | * |
| 31 | * @access protected |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $config_final = []; |
| 35 | |
| 36 | /** |
| 37 | * The configuration ID. |
| 38 | * |
| 39 | * @access public |
| 40 | * @var string |
| 41 | */ |
| 42 | public $id = 'global'; |
| 43 | |
| 44 | /** |
| 45 | * Capability (fields will inherit this). |
| 46 | * |
| 47 | * @access protected |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $capability = 'edit_theme_options'; |
| 51 | |
| 52 | /** |
| 53 | * The data-type we'll be using. |
| 54 | * |
| 55 | * @access protected |
| 56 | * @var string |
| 57 | */ |
| 58 | protected $option_type = 'theme_mod'; |
| 59 | |
| 60 | /** |
| 61 | * If we're using serialized options, then this is the global option name. |
| 62 | * |
| 63 | * @access protected |
| 64 | * @var string |
| 65 | */ |
| 66 | protected $option_name = ''; |
| 67 | |
| 68 | /** |
| 69 | * The compiler. |
| 70 | * |
| 71 | * @access protected |
| 72 | * @var array |
| 73 | */ |
| 74 | protected $compiler = []; |
| 75 | |
| 76 | /** |
| 77 | * Set to true if you want to completely disable any Kirki-generated CSS. |
| 78 | * |
| 79 | * @access protected |
| 80 | * @var bool |
| 81 | */ |
| 82 | protected $disable_output = false; |
| 83 | |
| 84 | /** |
| 85 | * The class constructor. |
| 86 | * Use the get_instance() static method to get the instance you need. |
| 87 | * |
| 88 | * @access private |
| 89 | * @param string $config_id @see Kirki\Compatibility\Config::get_instance(). |
| 90 | * @param array $args @see Kirki\Compatibility\Config::get_instance(). |
| 91 | */ |
| 92 | private function __construct( $config_id = 'global', $args = [] ) { |
| 93 | |
| 94 | // Get defaults from the class. |
| 95 | $defaults = get_class_vars( __CLASS__ ); |
| 96 | // Skip what we don't need in this context. |
| 97 | unset( $defaults['config_final'] ); |
| 98 | unset( $defaults['instances'] ); |
| 99 | // Apply any kirki_config global filters. |
| 100 | $defaults = apply_filters( 'kirki_config', $defaults ); |
| 101 | // Merge our args with the defaults. |
| 102 | $args = wp_parse_args( $args, $defaults ); |
| 103 | |
| 104 | // Modify default values with the defined ones. |
| 105 | foreach ( $args as $key => $value ) { |
| 106 | // Is this property whitelisted? |
| 107 | if ( property_exists( $this, $key ) ) { |
| 108 | $args[ $key ] = $value; |
| 109 | } |
| 110 | } |
| 111 | $this->id = $config_id; |
| 112 | |
| 113 | $this->config_final = wp_parse_args( |
| 114 | [ |
| 115 | 'id' => $config_id, |
| 116 | ], |
| 117 | $args |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Use this method to get an instance of your config. |
| 123 | * Each config has its own instance of this object. |
| 124 | * |
| 125 | * @static |
| 126 | * @access public |
| 127 | * @param string $id Config ID. |
| 128 | * @param array $args { |
| 129 | * Optional. Arguments to override config defaults. |
| 130 | * |
| 131 | * @type string $capability @see https://codex.wordpress.org/Roles_and_Capabilities |
| 132 | * @type string $option_type theme_mod or option. |
| 133 | * @type string $option_name If we want to used serialized options, |
| 134 | * this is where we'll be adding the option name. |
| 135 | * All fields using this config will be items in that array. |
| 136 | * @type array $compiler Not yet fully implemented |
| 137 | * @type bool $disable_output If set to true, no CSS will be generated |
| 138 | * from fields using this configuration. |
| 139 | * } |
| 140 | * |
| 141 | * @return Kirki\Compatibility\Config |
| 142 | */ |
| 143 | public static function get_instance( $id = 'global', $args = [] ) { |
| 144 | if ( empty( $id ) ) { |
| 145 | $id = 'global'; |
| 146 | } |
| 147 | $id_md5 = md5( $id ); |
| 148 | if ( ! isset( self::$instances[ $id_md5 ] ) ) { |
| 149 | self::$instances[ $id_md5 ] = new self( $id, $args ); |
| 150 | } |
| 151 | return self::$instances[ $id_md5 ]; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the IDs of all current configs. |
| 156 | * |
| 157 | * @static |
| 158 | * @access public |
| 159 | * @since 3.0.22 |
| 160 | * @return array |
| 161 | */ |
| 162 | public static function get_config_ids() { |
| 163 | $configs = []; |
| 164 | foreach ( self::$instances as $instance ) { |
| 165 | $configs[] = $instance->id; |
| 166 | } |
| 167 | return array_unique( $configs ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the $config_final property |
| 172 | * |
| 173 | * @access public |
| 174 | * @return array |
| 175 | */ |
| 176 | public function get_config() { |
| 177 | return $this->config_final; |
| 178 | } |
| 179 | } |
| 180 |