Init.php
412 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Init the Kirki responsive package. |
| 4 | * |
| 5 | * @package kirki-responsive |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\Responsive; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | use Kirki\Field\Responsive; |
| 16 | |
| 17 | /** |
| 18 | * Manage the responsive package. |
| 19 | */ |
| 20 | class Init { |
| 21 | |
| 22 | /** |
| 23 | * Control types with horizontal layout. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private $horizontal_types = [ |
| 28 | 'kirki-checkbox', |
| 29 | 'kirki-toggle', |
| 30 | 'kirki-react-colorful', |
| 31 | ]; |
| 32 | |
| 33 | /** |
| 34 | * The class constructor. |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | |
| 38 | add_action( 'customize_register', [ $this, 'register_control_type' ] ); |
| 39 | add_filter( 'kirki_control_types', [ $this, 'control_type' ] ); |
| 40 | |
| 41 | add_filter( 'kirki_field_exclude_init', array( $this, 'exclude_init' ), 99999, 4 ); |
| 42 | add_action( 'kirki_field_custom_init', [ $this, 'field_init' ], 99999, 3 ); |
| 43 | |
| 44 | /** |
| 45 | * We use 8 as the priority because we want this to run before "kirki_get_value" method |
| 46 | * in wp-content/plugins/kirki-dev/packages/kirki-framework/data-option/src/Option.php file. |
| 47 | */ |
| 48 | add_filter( 'kirki_get_value', [ $this, 'kirki_get_value' ], 8, 4 ); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Register control type. |
| 54 | * |
| 55 | * @param WP_Customize_Manager $wp_customize Instance of WP_Customize_Manager. |
| 56 | */ |
| 57 | public function register_control_type( $wp_customize ) { |
| 58 | |
| 59 | $wp_customize->register_control_type( '\Kirki\Control\Responsive' ); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * The control type. |
| 65 | * |
| 66 | * @param array $control_types The existing control types. |
| 67 | */ |
| 68 | public function control_type( $control_types ) { |
| 69 | |
| 70 | $control_types['kirki-responsive'] = 'Kirki\Control\Responsive'; |
| 71 | |
| 72 | return $control_types; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Parse the "default" argument. |
| 78 | * This method will format the "default" argument to contains the devices that wraps the default value. |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | * |
| 82 | * @param array $args The field arguments. |
| 83 | * @return array $args The modifiled field arguments. |
| 84 | */ |
| 85 | public function parse_default_arg( $args ) { |
| 86 | |
| 87 | $has_responsive_default = true; |
| 88 | |
| 89 | if ( isset( $args['default'] ) ) { |
| 90 | if ( is_array( $args['default'] ) ) { |
| 91 | if ( ! isset( $args['default']['desktop'] ) && ! isset( $args['default']['tablet'] ) && ! isset( $args['default']['mobile'] ) ) { |
| 92 | $has_responsive_default = false; |
| 93 | } |
| 94 | } else { |
| 95 | $has_responsive_default = false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if ( ! $has_responsive_default ) { |
| 100 | if ( isset( $args['default'] ) ) { |
| 101 | $args['default'] = array( |
| 102 | 'desktop' => $args['default'], |
| 103 | ); |
| 104 | } else { |
| 105 | $args['default'] = array( |
| 106 | 'desktop' => '', |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return $args; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Parse the output argument. |
| 117 | * This method will format the "output" argument to modify the "media_query" based on the targeted device. |
| 118 | * This method will be called inside of "default" argument loop when the control is using responsive mode. |
| 119 | * |
| 120 | * @since 1.0.0 |
| 121 | * |
| 122 | * @param array $args The field arguments. |
| 123 | * @param string $device The targeted device. |
| 124 | * |
| 125 | * @return array $args The modified field arguments. |
| 126 | */ |
| 127 | public function parse_output_arg( $args, $device ) { |
| 128 | |
| 129 | if ( isset( $args['output'] ) && ! empty( $args['output'] ) ) { |
| 130 | foreach ( $args['output'] as $index => $output ) { |
| 131 | if ( isset( $output['media_query'] ) ) { |
| 132 | if ( isset( $output['media_query'][ $device ] ) ) { |
| 133 | $args['output'][ $index ]['media_query'] = $output['media_query'][ $device ]; |
| 134 | } else { |
| 135 | // If current device is not set in the "media_query", then the output won't work. |
| 136 | unset( $args['output'][ $index ] ); |
| 137 | } |
| 138 | } else { |
| 139 | // If "media_query" is not provided in the "output" arg, then the output won't work. |
| 140 | unset( $args['output'][ $index ] ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return $args; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Exclude responsive field (field here means group of controls) from default field's init call. |
| 151 | * |
| 152 | * @see wp-content/plugins/kirki-dev/packages/kirki-framework/field/src/Field.php |
| 153 | * @since 1.0.0 |
| 154 | * |
| 155 | * @param bool $condition The existing condition. |
| 156 | * @param Object $field The field object. |
| 157 | * @param array $args The field args. |
| 158 | * |
| 159 | * @return bool |
| 160 | */ |
| 161 | public function exclude_init( $condition, $field, $args ) { |
| 162 | |
| 163 | if ( isset( $args['responsive'] ) && $args['responsive'] ) { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | return $condition; |
| 168 | |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Replace the default field init with custom init. |
| 173 | * |
| 174 | * @see wp-content/plugins/kirki-dev/packages/kirki-framework/field/src/Field.php |
| 175 | * @since 1.0.0 |
| 176 | * |
| 177 | * @param Object $field The field object. |
| 178 | * @param array $args The Kirki field args. |
| 179 | * @param string $control_class The control class name if it exists. |
| 180 | */ |
| 181 | public function field_init( $field, $args, $control_class ) { |
| 182 | |
| 183 | // Stop if this field doesn't have the "responsive" argument. |
| 184 | if ( ! isset( $args['responsive'] ) || ! $args['responsive'] ) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | $this->register_real_controls( $field, $args, $control_class ); |
| 189 | |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Register the real controls. |
| 194 | * |
| 195 | * @since 1.0.0 |
| 196 | * |
| 197 | * @param Object $field The field object. |
| 198 | * @param array $args The Kirki field args. |
| 199 | * @param string $control_class The control class name if it exists. |
| 200 | */ |
| 201 | public function register_real_controls( $field, $args, $control_class = '' ) { |
| 202 | |
| 203 | $defaults = $this->parse_default_arg( $args ); |
| 204 | $defaults = $defaults['default']; |
| 205 | $defaults = array_reverse( $defaults ); |
| 206 | $devices = []; |
| 207 | |
| 208 | $inside_horizontal_layout = property_exists( $field, 'type' ) && in_array( $field->type, $this->horizontal_types, true ) ? true : false; |
| 209 | |
| 210 | foreach ( $defaults as $device => $value ) { |
| 211 | array_push( $devices, $device ); |
| 212 | } |
| 213 | |
| 214 | $devices = array_reverse( $devices ); |
| 215 | |
| 216 | $devices_control_id = 'kirki_responsive__' . $args['settings']; |
| 217 | |
| 218 | $loop_count = 0; |
| 219 | |
| 220 | foreach ( $defaults as $device => $value ) { |
| 221 | $loop_count++; |
| 222 | |
| 223 | if ( ! $inside_horizontal_layout && 1 === $loop_count ) { |
| 224 | $this->add_devices_control( $devices_control_id, $args, $devices, $inside_horizontal_layout ); |
| 225 | } |
| 226 | |
| 227 | $new_control_args = $args; |
| 228 | |
| 229 | $new_control_args['default'] = $value; |
| 230 | $new_control_args['settings'] = $args['settings'] . '[' . $device . ']'; |
| 231 | $new_control_args['device'] = $device; |
| 232 | |
| 233 | if ( ! isset( $new_control_args['wrapper_attrs'] ) ) { |
| 234 | $new_control_args['wrapper_attrs'] = []; |
| 235 | } |
| 236 | |
| 237 | $new_control_args['wrapper_attrs']['data-kirki-parent-responsive-id'] = $devices_control_id; |
| 238 | $new_control_args['wrapper_attrs']['data-kirki-device-preview'] = $device; |
| 239 | |
| 240 | if ( isset( $new_control_args['label'] ) ) { |
| 241 | unset( $new_control_args['label'] ); |
| 242 | } |
| 243 | |
| 244 | if ( isset( $new_control_args['description'] ) ) { |
| 245 | unset( $new_control_args['description'] ); |
| 246 | } |
| 247 | |
| 248 | if ( isset( $new_control_args['responsive'] ) ) { |
| 249 | unset( $new_control_args['responsive'] ); |
| 250 | } |
| 251 | |
| 252 | $wrapper_class = ''; |
| 253 | |
| 254 | /** |
| 255 | * If `$control_class` is empty, then we assume this is a "parent field" and not the "real controls". |
| 256 | * A "parent field" here means: a parent of group of controls such as field-dimensions, field-typography, etc. |
| 257 | */ |
| 258 | if ( ! $control_class ) { |
| 259 | $wrapper_class .= ' customize-control-kirki-hidden-field'; |
| 260 | } |
| 261 | |
| 262 | if ( $inside_horizontal_layout ) { |
| 263 | $wrapper_class .= ' customize-control-kirki-responsive-horizontal'; |
| 264 | } |
| 265 | |
| 266 | if ( ! empty( $wrapper_class ) ) { |
| 267 | if ( isset( $new_control_args['wrapper_attrs']['class'] ) ) { |
| 268 | $new_control_args['wrapper_attrs']['class'] .= $wrapper_class; |
| 269 | } else { |
| 270 | $new_control_args['wrapper_attrs']['class'] = '{default_class}' . $wrapper_class; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | $new_control_args = $this->parse_output_arg( $new_control_args, $device ); |
| 275 | $field_classname = get_class( $field ); |
| 276 | |
| 277 | new $field_classname( $new_control_args ); |
| 278 | |
| 279 | if ( $inside_horizontal_layout && count( $devices ) === $loop_count ) { |
| 280 | $this->add_devices_control( $devices_control_id, $args, $devices, $inside_horizontal_layout ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Add devices control via $wp_customize. |
| 288 | * |
| 289 | * @param string $id The control's ID. |
| 290 | * @param array $args The control arguments. |
| 291 | * @param array $devices The specified devices. |
| 292 | * @param bool $inside_horizontal_layout Whether or not this control is inside a horizontal layout. |
| 293 | */ |
| 294 | public function add_devices_control( $id, $args, $devices, $inside_horizontal_layout = false ) { |
| 295 | |
| 296 | unset( $args['responsive'] ); |
| 297 | |
| 298 | if ( isset( $args['default'] ) ) { |
| 299 | unset( $args['default'] ); |
| 300 | } |
| 301 | |
| 302 | if ( isset( $args['transport'] ) ) { |
| 303 | unset( $args['transport'] ); |
| 304 | } |
| 305 | |
| 306 | if ( isset( $args['output'] ) ) { |
| 307 | unset( $args['output'] ); |
| 308 | } |
| 309 | |
| 310 | if ( isset( $args['wrapper_attrs'] ) ) { |
| 311 | if ( isset( $args['wrapper_attrs']['data-kirki-parent-responsive-id'] ) ) { |
| 312 | unset( $args['wrapper_attrs']['data-kirki-parent-responsive-id'] ); |
| 313 | } |
| 314 | |
| 315 | if ( isset( $args['wrapper_attrs']['data-kirki-device-preview'] ) ) { |
| 316 | unset( $args['wrapper_attrs']['data-kirki-device-preview'] ); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | $args['settings'] = $id; |
| 321 | $args['type'] = 'kirki-responsive'; |
| 322 | |
| 323 | if ( ! $inside_horizontal_layout ) { |
| 324 | if ( ! isset( $args['wrapper_opts'] ) ) { |
| 325 | $args['wrapper_opts'] = []; |
| 326 | } |
| 327 | |
| 328 | $args['wrapper_opts']['gap'] = 'small'; |
| 329 | } |
| 330 | |
| 331 | $args['choices'] = [ |
| 332 | 'devices' => $devices, |
| 333 | ]; |
| 334 | |
| 335 | new Responsive( $args ); |
| 336 | |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Filter the value for responsive fields. |
| 341 | * |
| 342 | * @see wp-content/plugins/kirki-dev/packages/kirki-framework/data-option/src/Option.php |
| 343 | * |
| 344 | * @param mixed $value The field value. |
| 345 | * @param string $field_name The field name. |
| 346 | * @param mixed $default The default value. |
| 347 | * @param string $type The option type (theme_mod or option). |
| 348 | * |
| 349 | * @return mixed The filtered value. |
| 350 | */ |
| 351 | public function kirki_get_value( $value = '', $field_name = '', $default = '', $type = 'theme_mod' ) { |
| 352 | |
| 353 | /** |
| 354 | * The "option" will be handled by "kirki_get_value" method in |
| 355 | * wp-content/plugins/kirki-dev/packages/kirki-framework/data-option/src/Option.php file. |
| 356 | */ |
| 357 | if ( 'option' === $type ) { |
| 358 | return $value; |
| 359 | } |
| 360 | |
| 361 | // If the field name doesn't contain a '[', then it's not a sub-item of another field. |
| 362 | if ( false === strpos( $field_name, '[' ) ) { |
| 363 | return $value; |
| 364 | } |
| 365 | |
| 366 | // If this is not part of a responsive field, then return the value. |
| 367 | if ( ! in_array( $field_name, Responsive::$sub_field_names, true ) ) { |
| 368 | return $value; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * If we got here then this is part of an option array. |
| 373 | * We need to get the 1st level, and then find the item inside that array. |
| 374 | */ |
| 375 | $parts = \explode( '[', $field_name ); |
| 376 | $value = get_theme_mod( $parts[0], [] ); |
| 377 | |
| 378 | foreach ( $parts as $key => $part ) { |
| 379 | /** |
| 380 | * Skip the 1st item, it's already been dealt with |
| 381 | * when we got the value initially right before this loop. |
| 382 | */ |
| 383 | if ( 0 === $key ) { |
| 384 | continue; |
| 385 | } |
| 386 | |
| 387 | $part = str_replace( ']', '', $part ); |
| 388 | |
| 389 | /** |
| 390 | * If the item exists in the value, then change $value to the item. |
| 391 | * This runs recursively for all parts until we get to the end. |
| 392 | */ |
| 393 | if ( is_array( $value ) && isset( $value[ $part ] ) ) { |
| 394 | $value = $value[ $part ]; |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * If we got here, the item was not found in the value. |
| 400 | * We need to change the value accordingly depending on whether |
| 401 | * this is the last item in the loop or not. |
| 402 | */ |
| 403 | $value = ( isset( $parts[ $key + 1 ] ) ) ? [] : ''; |
| 404 | } |
| 405 | |
| 406 | $value = empty( $value ) ? $default : $value; |
| 407 | |
| 408 | return $value; |
| 409 | |
| 410 | } |
| 411 | |
| 412 | } |