Dimensions.php
269 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Override field methods |
| 4 | * |
| 5 | * @package kirki-framework/field-dimensions |
| 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 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | use Kirki; |
| 18 | use Kirki\Field; |
| 19 | use Kirki\URL; |
| 20 | |
| 21 | /** |
| 22 | * Field overrides. |
| 23 | * |
| 24 | * @since 1.0 |
| 25 | */ |
| 26 | class Dimensions extends Field { |
| 27 | |
| 28 | /** |
| 29 | * The field type. |
| 30 | * |
| 31 | * @access public |
| 32 | * @since 1.0 |
| 33 | * @var string |
| 34 | */ |
| 35 | public $type = 'kirki-dimensions'; |
| 36 | |
| 37 | /** |
| 38 | * Extra logic for the field. |
| 39 | * |
| 40 | * Adds all sub-fields. |
| 41 | * |
| 42 | * @access public |
| 43 | * @param array $args The arguments of the field. |
| 44 | */ |
| 45 | public function init( $args = array() ) { |
| 46 | |
| 47 | add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 48 | add_action( 'customize_preview_init', array( $this, 'enqueue_customize_preview_scripts' ) ); |
| 49 | add_filter( 'kirki_output_control_classnames', array( $this, 'output_control_classnames' ) ); |
| 50 | |
| 51 | $args['required'] = isset( $args['required'] ) ? (array) $args['required'] : array(); |
| 52 | |
| 53 | $labels = array( |
| 54 | 'left-top' => esc_html__( 'Left Top', 'kirki' ), |
| 55 | 'left-center' => esc_html__( 'Left Center', 'kirki' ), |
| 56 | 'left-bottom' => esc_html__( 'Left Bottom', 'kirki' ), |
| 57 | 'right-top' => esc_html__( 'Right Top', 'kirki' ), |
| 58 | 'right-center' => esc_html__( 'Right Center', 'kirki' ), |
| 59 | 'right-bottom' => esc_html__( 'Right Bottom', 'kirki' ), |
| 60 | 'center-top' => esc_html__( 'Center Top', 'kirki' ), |
| 61 | 'center-center' => esc_html__( 'Center Center', 'kirki' ), |
| 62 | 'center-bottom' => esc_html__( 'Center Bottom', 'kirki' ), |
| 63 | 'font-size' => esc_html__( 'Font Size', 'kirki' ), |
| 64 | 'font-weight' => esc_html__( 'Font Weight', 'kirki' ), |
| 65 | 'line-height' => esc_html__( 'Line Height', 'kirki' ), |
| 66 | 'font-style' => esc_html__( 'Font Style', 'kirki' ), |
| 67 | 'letter-spacing' => esc_html__( 'Letter Spacing', 'kirki' ), |
| 68 | 'word-spacing' => esc_html__( 'Word Spacing', 'kirki' ), |
| 69 | 'top' => esc_html__( 'Top', 'kirki' ), |
| 70 | 'bottom' => esc_html__( 'Bottom', 'kirki' ), |
| 71 | 'left' => esc_html__( 'Left', 'kirki' ), |
| 72 | 'right' => esc_html__( 'Right', 'kirki' ), |
| 73 | 'center' => esc_html__( 'Center', 'kirki' ), |
| 74 | 'size' => esc_html__( 'Size', 'kirki' ), |
| 75 | 'spacing' => esc_html__( 'Spacing', 'kirki' ), |
| 76 | 'width' => esc_html__( 'Width', 'kirki' ), |
| 77 | 'height' => esc_html__( 'Height', 'kirki' ), |
| 78 | 'invalid-value' => esc_html__( 'Invalid Value', 'kirki' ), |
| 79 | ); |
| 80 | |
| 81 | /** |
| 82 | * Add a hidden field, the label & description. |
| 83 | */ |
| 84 | new \Kirki\Field\Generic( |
| 85 | wp_parse_args( |
| 86 | array( |
| 87 | 'type' => 'kirki-generic', |
| 88 | 'default' => '', |
| 89 | 'wrapper_opts' => array( |
| 90 | 'gap' => 'small', |
| 91 | ), |
| 92 | 'sanitize_callback' => isset( $args['sanitize_callback'] ) ? $args['sanitize_callback'] : array( __CLASS__, 'sanitize' ), |
| 93 | 'choices' => array( |
| 94 | 'type' => 'hidden', |
| 95 | 'parent_type' => 'kirki-dimensions', |
| 96 | ), |
| 97 | ), |
| 98 | $args |
| 99 | ) |
| 100 | ); |
| 101 | |
| 102 | $args['choices'] = isset( $args['choices'] ) ? $args['choices'] : array(); |
| 103 | $args['choices']['labels'] = isset( $args['choices']['labels'] ) ? $args['choices']['labels'] : array(); |
| 104 | |
| 105 | if ( isset( $args['transport'] ) && 'auto' === $args['transport'] ) { |
| 106 | $args['transport'] = 'postMessage'; |
| 107 | } |
| 108 | |
| 109 | $total_items = count( $args['default'] ); |
| 110 | $item_count = 0; |
| 111 | |
| 112 | $width = 100; |
| 113 | |
| 114 | $break_indexes = array(); |
| 115 | |
| 116 | // The 'kirki-group-break' only supports 12 group items inside a group. |
| 117 | if ( 2 === $total_items ) { |
| 118 | $width = 50; |
| 119 | } elseif ( 3 === $total_items ) { |
| 120 | $width = 33; |
| 121 | } elseif ( 4 === $total_items ) { |
| 122 | $width = 25; |
| 123 | } elseif ( 5 === $total_items ) { |
| 124 | array_push( $break_indexes, 3 ); |
| 125 | $width = 33; |
| 126 | } elseif ( 6 === $total_items ) { |
| 127 | array_push( $break_indexes, 3 ); |
| 128 | $width = 33; |
| 129 | } elseif ( 7 === $total_items || 8 === $total_items ) { |
| 130 | array_push( $break_indexes, 4 ); |
| 131 | $width = 25; |
| 132 | } elseif ( 9 === $total_items ) { |
| 133 | array_push( $break_indexes, 3, 6 ); |
| 134 | $width = 33; |
| 135 | } elseif ( $total_items > 9 ) { |
| 136 | array_push( $break_indexes, 4, 8 ); |
| 137 | $width = 25; |
| 138 | } |
| 139 | |
| 140 | foreach ( $args['default'] as $choice => $default ) { |
| 141 | $item_count++; |
| 142 | |
| 143 | $label = $choice; |
| 144 | $label = isset( $labels[ $choice ] ) ? $labels[ $choice ] : $label; |
| 145 | $label = isset( $args['choices']['labels'][ $choice ] ) ? $args['choices']['labels'][ $choice ] : $label; |
| 146 | |
| 147 | $wrapper_attrs = array( |
| 148 | 'data-kirki-parent-control-type' => 'kirki-dimensions', |
| 149 | 'data-kirki-parent-control-setting' => $args['settings'], |
| 150 | 'class' => '{default_class} kirki-group-item kirki-w' . $width, |
| 151 | ); |
| 152 | |
| 153 | if ( $item_count === 1 ) { |
| 154 | $wrapper_attrs['class'] .= ' kirki-group-start'; |
| 155 | } |
| 156 | |
| 157 | if ( in_array( $item_count, $break_indexes, true ) ) { |
| 158 | $wrapper_attrs['class'] .= ' kirki-group-break'; |
| 159 | } |
| 160 | |
| 161 | if ( $item_count === $total_items ) { |
| 162 | $wrapper_attrs['class'] .= ' kirki-group-end'; |
| 163 | } |
| 164 | |
| 165 | new \Kirki\Field\Dimension( |
| 166 | wp_parse_args( |
| 167 | array( |
| 168 | 'type' => 'kirki-dimension', |
| 169 | 'settings' => $args['settings'] . '[' . $choice . ']', |
| 170 | 'parent_setting' => $args['settings'], |
| 171 | 'label' => $label, |
| 172 | 'default' => $default, |
| 173 | 'wrapper_attrs' => $wrapper_attrs, |
| 174 | 'choices' => array( |
| 175 | 'label_position' => 'bottom', |
| 176 | ), |
| 177 | 'js_vars' => array(), |
| 178 | 'css_vars' => array(), |
| 179 | 'output' => array(), |
| 180 | ), |
| 181 | $args |
| 182 | ) |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Sanitizes dimension controls. |
| 190 | * |
| 191 | * @static |
| 192 | * @access public |
| 193 | * @since 1.0 |
| 194 | * @param array $value The value. |
| 195 | * @return array |
| 196 | */ |
| 197 | public static function sanitize( $value ) { |
| 198 | |
| 199 | if ( ! is_array( $value ) ) { |
| 200 | return array(); |
| 201 | } |
| 202 | |
| 203 | foreach ( $value as $key => $val ) { |
| 204 | $value[ $key ] = sanitize_text_field( $val ); |
| 205 | } |
| 206 | |
| 207 | return $value; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Override parent method. No need to register any setting. |
| 213 | * |
| 214 | * @access public |
| 215 | * @since 0.1 |
| 216 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 217 | * @return void |
| 218 | */ |
| 219 | public function add_setting( $wp_customize ) {} |
| 220 | |
| 221 | /** |
| 222 | * Override the parent method. No need for a control. |
| 223 | * |
| 224 | * @access public |
| 225 | * @since 0.1 |
| 226 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 227 | * @return void |
| 228 | */ |
| 229 | public function add_control( $wp_customize ) {} |
| 230 | |
| 231 | /** |
| 232 | * Enqueue scripts & styles. |
| 233 | * |
| 234 | * @access public |
| 235 | * @since 1.0 |
| 236 | * @return void |
| 237 | */ |
| 238 | public function enqueue_scripts() { |
| 239 | |
| 240 | |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Enqueue scripts & styles on customize_preview_init. |
| 245 | * |
| 246 | * @access public |
| 247 | * @since 1.0 |
| 248 | * @return void |
| 249 | */ |
| 250 | public function enqueue_customize_preview_scripts() { |
| 251 | |
| 252 | |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Adds a custom output class for typography fields. |
| 257 | * |
| 258 | * @access public |
| 259 | * @since 1.0 |
| 260 | * @param array $classnames The array of classnames. |
| 261 | * @return array |
| 262 | */ |
| 263 | public function output_control_classnames( $classnames ) { |
| 264 | |
| 265 | $classnames['kirki-dimensions'] = '\Kirki\Field\CSS\Dimensions'; |
| 266 | return $classnames; |
| 267 | |
| 268 | } |
| 269 | } |