Base.php
347 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Customizer Controls Base. |
| 4 | * |
| 5 | * Extend this in other controls. |
| 6 | * |
| 7 | * @package kirki-framework/control-base |
| 8 | * @copyright Copyright (c) 2023, Themeum |
| 9 | * @license https://opensource.org/licenses/MIT |
| 10 | * @since 1.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Kirki\Control; |
| 14 | |
| 15 | use Kirki\URL; |
| 16 | |
| 17 | /** |
| 18 | * A base for controls. |
| 19 | * |
| 20 | * @since 1.0 |
| 21 | */ |
| 22 | class Base extends \WP_Customize_Control { |
| 23 | |
| 24 | /** |
| 25 | * Used to automatically generate all CSS output. |
| 26 | * |
| 27 | * Whitelisting property for use in Kirki modules. |
| 28 | * |
| 29 | * @access public |
| 30 | * @since 1.0 |
| 31 | * @var array |
| 32 | */ |
| 33 | public $output = []; |
| 34 | |
| 35 | /** |
| 36 | * Data type |
| 37 | * |
| 38 | * @access public |
| 39 | * @since 1.0 |
| 40 | * @var string |
| 41 | */ |
| 42 | public $option_type = 'theme_mod'; |
| 43 | |
| 44 | /** |
| 45 | * Option name (if using options). |
| 46 | * |
| 47 | * Whitelisting property for use in Kirki modules. |
| 48 | * |
| 49 | * @access public |
| 50 | * @since 1.0 |
| 51 | * @var string |
| 52 | */ |
| 53 | public $option_name = false; |
| 54 | |
| 55 | /** |
| 56 | * The kirki_config we're using for this control |
| 57 | * |
| 58 | * Whitelisting property for use in Kirki modules. |
| 59 | * |
| 60 | * @access public |
| 61 | * @since 1.0 |
| 62 | * @var string |
| 63 | */ |
| 64 | public $kirki_config = 'global'; |
| 65 | |
| 66 | /** |
| 67 | * Whitelisting the "preset" argument for use in Kirki modules. |
| 68 | * |
| 69 | * @access public |
| 70 | * @since 1.0 |
| 71 | * @var array |
| 72 | */ |
| 73 | public $preset = []; |
| 74 | |
| 75 | /** |
| 76 | * Whitelisting the "css_vars" argument for use in Kirki modules. |
| 77 | * |
| 78 | * @access public |
| 79 | * @since 1.0 |
| 80 | * @var string |
| 81 | */ |
| 82 | public $css_vars = ''; |
| 83 | |
| 84 | /** |
| 85 | * The version. Used in scripts & styles for cache-busting. |
| 86 | * |
| 87 | * @static |
| 88 | * @access public |
| 89 | * @since 1.0 |
| 90 | * @var string |
| 91 | */ |
| 92 | public static $control_ver = '1.0.4'; |
| 93 | |
| 94 | /** |
| 95 | * Parent setting. |
| 96 | * |
| 97 | * Used for composite controls to denote the setting that should be saved. |
| 98 | * |
| 99 | * @access public |
| 100 | * @since 1.1 |
| 101 | * @var string |
| 102 | */ |
| 103 | public $parent_setting; |
| 104 | |
| 105 | /** |
| 106 | * Wrapper attributes. |
| 107 | * |
| 108 | * The value of this property will be rendered to the wrapper element. |
| 109 | * Can be 'class', 'id', 'data-*', and other attributes. |
| 110 | * |
| 111 | * @access public |
| 112 | * @since 1.1 |
| 113 | * @var array |
| 114 | */ |
| 115 | public $wrapper_attrs = []; |
| 116 | |
| 117 | /** |
| 118 | * Backwards compatibility support for `$wrapper_attrs`. |
| 119 | * |
| 120 | * Kirki v3 already has this `$wrapper_atts` property. |
| 121 | * It was not published in the documentation, and more for internal use. |
| 122 | * |
| 123 | * The `WP_Customize_Control` is using `input_attrs` not `input_atts` (see, attrs vs atts). |
| 124 | * So Kirki uses `$wrapper_attrs` for consistency and keep the old `$wrapper_atts` backwards compatibility. |
| 125 | * |
| 126 | * This property could be removed in the future. |
| 127 | * Please use `$wrapper_attrs` instead. |
| 128 | * |
| 129 | * @since 1.1 |
| 130 | * @deprecated 1.0.1 This variable could be removed in the future. Please use `$wrapper_attrs` instead. |
| 131 | * @var array |
| 132 | */ |
| 133 | public $wrapper_atts = []; |
| 134 | |
| 135 | /** |
| 136 | * Wrapper options. |
| 137 | * |
| 138 | * This won't be rendered automatically to the wrapper element. |
| 139 | * The purpose is to allow us to have custom options so we can manage it when needed. |
| 140 | * |
| 141 | * @access public |
| 142 | * @since 1.1 |
| 143 | * @var array |
| 144 | */ |
| 145 | public $wrapper_opts = []; |
| 146 | |
| 147 | /** |
| 148 | * Extra script dependencies. |
| 149 | * |
| 150 | * @access public |
| 151 | * @since 1.0 |
| 152 | * @return array |
| 153 | */ |
| 154 | public function kirki_script_dependencies() { |
| 155 | return []; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Enqueue control related scripts/styles. |
| 160 | * |
| 161 | * @access public |
| 162 | * @since 1.0 |
| 163 | * @return void |
| 164 | */ |
| 165 | public function enqueue() { |
| 166 | |
| 167 | // Enqueue the styles. |
| 168 | |
| 169 | // Enqueue the scripts. |
| 170 | |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Renders the control wrapper and calls $this->render_content() for the internals. |
| 175 | * |
| 176 | * @since 1.0 |
| 177 | */ |
| 178 | protected function render() { |
| 179 | |
| 180 | $id = 'customize-control-' . str_replace( [ '[', ']' ], [ '-', '' ], $this->id ); |
| 181 | $class = 'customize-control customize-control-kirki customize-control-' . $this->type; |
| 182 | $gap = isset( $this->wrapper_opts['gap'] ) ? $this->wrapper_opts['gap'] : 'default'; |
| 183 | $tag = isset( $this->wrapper_opts['tag'] ) ? $this->wrapper_opts['tag'] : 'li'; |
| 184 | |
| 185 | switch ( $gap ) { |
| 186 | case 'small': |
| 187 | $class .= ' customize-control-has-small-gap'; |
| 188 | break; |
| 189 | |
| 190 | case 'none': |
| 191 | $class .= ' customize-control-is-gapless'; |
| 192 | break; |
| 193 | |
| 194 | default: |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | if ( empty( $this->wrapper_attrs ) && ! empty( $this->wrapper_atts ) ) { |
| 199 | $this->wrapper_attrs = $this->wrapper_atts; |
| 200 | } |
| 201 | |
| 202 | if ( isset( $this->wrapper_attrs['id'] ) ) { |
| 203 | $id = $this->wrapper_attrs['id']; |
| 204 | } |
| 205 | |
| 206 | if ( ! isset( $this->wrapper_attrs['data-kirki-setting'] ) ) { |
| 207 | $this->wrapper_attrs['data-kirki-setting'] = $this->id; |
| 208 | } |
| 209 | |
| 210 | if ( ! isset( $this->wrapper_attrs['data-kirki-setting-link'] ) ) { |
| 211 | if ( isset( $this->settings['default'] ) ) { |
| 212 | $this->wrapper_attrs['data-kirki-setting-link'] = $this->settings['default']->id; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | $data_attrs = ''; |
| 217 | |
| 218 | foreach ( $this->wrapper_attrs as $attr_key => $attr_value ) { |
| 219 | if ( 0 === strpos( $attr_key, 'data-' ) ) { |
| 220 | $data_attrs .= ' ' . esc_attr( $attr_key ) . '="' . esc_attr( $attr_value ) . '"'; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if ( isset( $this->wrapper_attrs['class'] ) ) { |
| 225 | $class = str_ireplace( '{default_class}', $class, $this->wrapper_attrs['class'] ); |
| 226 | } |
| 227 | |
| 228 | // ! Consider to esc $data_attrs. |
| 229 | // ? What function we can use to escape string like data-xx="yy"? |
| 230 | // $data_attrs is pre-escaped during construction in the loop above. |
| 231 | printf( '<' . esc_attr( $tag ) . ' id="%s" class="%s" %s>', esc_attr( $id ), esc_attr( $class ), $data_attrs ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 232 | $this->render_content(); |
| 233 | echo '</' . esc_attr( $tag ) . '>'; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Refresh the parameters passed to the JavaScript via JSON. |
| 239 | * |
| 240 | * @access public |
| 241 | * @since 1.0 |
| 242 | * @see WP_Customize_Control::to_json() |
| 243 | * @return void |
| 244 | */ |
| 245 | public function to_json() { |
| 246 | |
| 247 | // Get the basics from the parent class. |
| 248 | parent::to_json(); |
| 249 | |
| 250 | // Default value. |
| 251 | $this->json['default'] = $this->setting->default; |
| 252 | |
| 253 | if ( isset( $this->default ) ) { |
| 254 | $this->json['default'] = $this->default; |
| 255 | } |
| 256 | |
| 257 | // Output. |
| 258 | $this->json['output'] = $this->output; |
| 259 | |
| 260 | // Value. |
| 261 | $this->json['value'] = $this->value(); |
| 262 | |
| 263 | // Choices. |
| 264 | $this->json['choices'] = $this->choices; |
| 265 | |
| 266 | // The link. |
| 267 | $this->json['link'] = $this->get_link(); |
| 268 | |
| 269 | // The ID. |
| 270 | $this->json['id'] = $this->id; |
| 271 | |
| 272 | // Translation strings. |
| 273 | $this->json['l10n'] = $this->l10n(); |
| 274 | |
| 275 | // The ajaxurl in case we need it. |
| 276 | $this->json['ajaxurl'] = admin_url( 'admin-ajax.php' ); |
| 277 | |
| 278 | // Input attributes. |
| 279 | $this->json['inputAttrs'] = ''; |
| 280 | |
| 281 | if ( is_array( $this->input_attrs ) ) { |
| 282 | foreach ( $this->input_attrs as $attr => $value ) { |
| 283 | $this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" '; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // The kirki-config. |
| 288 | $this->json['kirkiConfig'] = $this->kirki_config; |
| 289 | |
| 290 | // The option-type. |
| 291 | $this->json['kirkiOptionType'] = $this->option_type; |
| 292 | |
| 293 | // The option-name. |
| 294 | $this->json['kirkiOptionName'] = $this->option_name; |
| 295 | |
| 296 | // The preset. |
| 297 | $this->json['preset'] = $this->preset; |
| 298 | |
| 299 | // The CSS-Variables. |
| 300 | $this->json['css-var'] = $this->css_vars; |
| 301 | |
| 302 | // Parent setting. |
| 303 | $this->json['parent_setting'] = $this->parent_setting; |
| 304 | |
| 305 | // Wrapper Attributes. |
| 306 | $this->json['wrapper_attrs'] = $this->wrapper_attrs; |
| 307 | $this->json['wrapper_atts'] = $this->wrapper_attrs; // For backward compatibility - Could be removed in the future. |
| 308 | |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Render the control's content. |
| 313 | * |
| 314 | * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`. |
| 315 | * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template(). |
| 316 | * |
| 317 | * @access protected |
| 318 | * @since 1.0 |
| 319 | * @return void |
| 320 | */ |
| 321 | protected function render_content() {} |
| 322 | |
| 323 | /** |
| 324 | * An Underscore (JS) template for this control's content (but not its container). |
| 325 | * |
| 326 | * Class variables for this control class are available in the `data` JS object; |
| 327 | * export custom variables by overriding {@see WP_Customize_Control::to_json()}. |
| 328 | * |
| 329 | * @access protected |
| 330 | * @since 1.0 |
| 331 | * @see WP_Customize_Control::print_template() |
| 332 | * @return void |
| 333 | */ |
| 334 | protected function content_template() {} |
| 335 | |
| 336 | /** |
| 337 | * Returns an array of translation strings. |
| 338 | * |
| 339 | * @access protected |
| 340 | * @since 3.0.0 |
| 341 | * @return array |
| 342 | */ |
| 343 | protected function l10n() { |
| 344 | return []; |
| 345 | } |
| 346 | } |
| 347 |