Multicheck.php
101 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Override field methods |
| 4 | * |
| 5 | * @package kirki-framework/control-multicheck |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\Field; |
| 12 | |
| 13 | use Kirki\Field; |
| 14 | |
| 15 | /** |
| 16 | * Field overrides. |
| 17 | * |
| 18 | * @since 1.0 |
| 19 | */ |
| 20 | class Multicheck extends Field { |
| 21 | |
| 22 | /** |
| 23 | * The field type. |
| 24 | * |
| 25 | * @access public |
| 26 | * @since 1.0 |
| 27 | * @var string |
| 28 | */ |
| 29 | public $type = 'kirki-multicheck'; |
| 30 | |
| 31 | /** |
| 32 | * The control class-name. |
| 33 | * |
| 34 | * @access protected |
| 35 | * @since 0.1 |
| 36 | * @var string |
| 37 | */ |
| 38 | protected $control_class = '\Kirki\Control\Multicheck'; |
| 39 | |
| 40 | /** |
| 41 | * Whether we should register the control class for JS-templating or not. |
| 42 | * |
| 43 | * @access protected |
| 44 | * @since 0.1 |
| 45 | * @var bool |
| 46 | */ |
| 47 | protected $control_has_js_template = true; |
| 48 | |
| 49 | /** |
| 50 | * Filter arguments before creating the setting. |
| 51 | * |
| 52 | * @access public |
| 53 | * @since 0.1 |
| 54 | * @param array $args The field arguments. |
| 55 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 56 | * @return array |
| 57 | */ |
| 58 | public function filter_setting_args( $args, $wp_customize ) { |
| 59 | if ( $args['settings'] === $this->args['settings'] ) { |
| 60 | $args = parent::filter_setting_args( $args, $wp_customize ); |
| 61 | |
| 62 | // Set the sanitize-callback if none is defined. |
| 63 | if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) { |
| 64 | $args['sanitize_callback'] = [ __CLASS__, 'sanitize' ]; |
| 65 | } |
| 66 | } |
| 67 | return $args; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Filter arguments before creating the control. |
| 72 | * |
| 73 | * @access public |
| 74 | * @since 0.1 |
| 75 | * @param array $args The field arguments. |
| 76 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 77 | * @return array |
| 78 | */ |
| 79 | public function filter_control_args( $args, $wp_customize ) { |
| 80 | if ( $args['settings'] === $this->args['settings'] ) { |
| 81 | $args = parent::filter_control_args( $args, $wp_customize ); |
| 82 | $args['type'] = 'kirki-multicheck'; |
| 83 | } |
| 84 | return $args; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * The sanitize method that will be used as a falback |
| 89 | * |
| 90 | * @static |
| 91 | * @access public |
| 92 | * @since 1.0 |
| 93 | * @param string|array $value The control's value. |
| 94 | * @return array |
| 95 | */ |
| 96 | public static function sanitize( $value ) { |
| 97 | $value = ( ! is_array( $value ) ) ? explode( ',', $value ) : $value; |
| 98 | return ( ! empty( $value ) ) ? array_map( 'sanitize_text_field', $value ) : []; |
| 99 | } |
| 100 | } |
| 101 |