Margin.php
162 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Override field methods. |
| 4 | * |
| 5 | * @package kirki-margin |
| 6 | * @since 1.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\Field; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | use Kirki\Field; |
| 16 | use Kirki\URL; |
| 17 | |
| 18 | /** |
| 19 | * Field overrides. |
| 20 | * |
| 21 | * @since 1.0 |
| 22 | */ |
| 23 | class Margin extends Field { |
| 24 | |
| 25 | /** |
| 26 | * The field type. |
| 27 | * |
| 28 | * @since 1.0 |
| 29 | * @access public |
| 30 | * @var string |
| 31 | */ |
| 32 | public $type = 'kirki-margin'; |
| 33 | |
| 34 | /** |
| 35 | * The control class-name. |
| 36 | * |
| 37 | * @since 1.0 |
| 38 | * @access protected |
| 39 | * @var string |
| 40 | */ |
| 41 | protected $control_class = '\Kirki\Control\Margin'; |
| 42 | |
| 43 | /** |
| 44 | * Whether we should register the control class for JS-templating or not. |
| 45 | * |
| 46 | * @since 1.0 |
| 47 | * @access protected |
| 48 | * @var bool |
| 49 | */ |
| 50 | protected $control_has_js_template = false; |
| 51 | |
| 52 | /** |
| 53 | * Additional logic for the field. |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | * @access protected |
| 57 | * |
| 58 | * @param array $args The field arguments. |
| 59 | */ |
| 60 | protected function init( $args ) { |
| 61 | |
| 62 | add_action( 'customize_preview_init', [ $this, 'enqueue_customize_preview_scripts' ] ); |
| 63 | add_filter( 'kirki_output_control_classnames', [ $this, 'output_control_classnames' ] ); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Filter arguments before creating the setting. |
| 69 | * |
| 70 | * @param array $args The field arguments. |
| 71 | * @param \WP_Customize_Manager $wp_customize The customizer instance. |
| 72 | * |
| 73 | * @return array $args The maybe-filtered arguments. |
| 74 | */ |
| 75 | public function filter_setting_args( $args, $wp_customize ) { |
| 76 | |
| 77 | if ( $args['settings'] === $this->args['settings'] ) { |
| 78 | $args = parent::filter_setting_args( $args, $wp_customize ); |
| 79 | |
| 80 | // Set the sanitize_callback if none is defined. |
| 81 | if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) { |
| 82 | $args['sanitize_callback'] = [ __CLASS__, 'sanitize' ]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $args; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sanitize the value. |
| 92 | * |
| 93 | * @param mixed $values The value to sanitize. |
| 94 | * @return mixed |
| 95 | */ |
| 96 | public static function sanitize( $values ) { |
| 97 | |
| 98 | foreach ( $values as $position => $value ) { |
| 99 | if ( '' !== $value ) { |
| 100 | if ( is_numeric( $value ) ) { |
| 101 | $value = $value . 'px'; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | $values[ $position ] = sanitize_text_field( $value ); |
| 106 | } |
| 107 | |
| 108 | return $values; |
| 109 | |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Filter arguments before creating the control. |
| 114 | * |
| 115 | * @param array $args The field arguments. |
| 116 | * @param \WP_Customize_Manager $wp_customize The customizer instance. |
| 117 | * |
| 118 | * @return array $args The maybe-filtered arguments. |
| 119 | */ |
| 120 | public function filter_control_args( $args, $wp_customize ) { |
| 121 | |
| 122 | if ( $args['settings'] === $this->args['settings'] ) { |
| 123 | $args = parent::filter_control_args( $args, $wp_customize ); |
| 124 | $args['type'] = 'kirki-margin'; |
| 125 | } |
| 126 | |
| 127 | return $args; |
| 128 | |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Enqueue styles & scripts on 'customize_preview_init' action. |
| 133 | * |
| 134 | * @since 4.0.0 |
| 135 | * @access public |
| 136 | */ |
| 137 | public function enqueue_customize_preview_scripts() { |
| 138 | |
| 139 | |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Add output control class for margin/padding control. |
| 144 | * |
| 145 | * @since 1.0.0 |
| 146 | * @access public |
| 147 | * |
| 148 | * @param array $control_classes The existing control classes. |
| 149 | * @return array |
| 150 | */ |
| 151 | public function output_control_classnames( $control_classes ) { |
| 152 | |
| 153 | $class_name = str_ireplace( 'kirki-', '', $this->type ); |
| 154 | $class_name = ucfirst( $class_name ); |
| 155 | |
| 156 | $control_classes[ $this->type ] = '\Kirki\Field\CSS\\' . $class_name; |
| 157 | |
| 158 | return $control_classes; |
| 159 | |
| 160 | } |
| 161 | |
| 162 | } |