Init.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Init the Kirki tabs package. |
| 4 | * |
| 5 | * @package kirki-tabs |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\Tabs; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | use Kirki\Field\Tabs; |
| 16 | |
| 17 | /** |
| 18 | * Manage the tabs package. |
| 19 | */ |
| 20 | class Init { |
| 21 | |
| 22 | /** |
| 23 | * The class constructor. |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | |
| 27 | add_filter( 'kirki_control_types', [ $this, 'control_type' ] ); |
| 28 | add_filter( 'kirki_field_add_control_args', array( $this, 'filter_control_args' ), 10, 2 ); |
| 29 | add_action( 'kirki_section_init', [ $this, 'add_tab' ], 10, 2 ); |
| 30 | |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * The control type. |
| 35 | * |
| 36 | * @param array $control_types The existing control types. |
| 37 | */ |
| 38 | public function control_type( $control_types ) { |
| 39 | |
| 40 | $control_types['kirki-tab'] = 'Kirki\Control\Tabs'; |
| 41 | |
| 42 | return $control_types; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Filter customizer's control arguments. |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | * |
| 51 | * @param array $args The field arguments. |
| 52 | * @param WP_Customize_Manager $wp_customize The customizer instance. |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function filter_control_args( $args, $wp_customize ) { |
| 57 | |
| 58 | if ( isset( $args['tab'] ) ) { |
| 59 | $tabs = Tabs::$tabs; |
| 60 | $section = $args['section']; |
| 61 | |
| 62 | if ( isset( $tabs[ $section ] ) ) { |
| 63 | $tab_wrapper_attrs = array( |
| 64 | 'data-kirki-parent-tab-id' => $section, |
| 65 | 'data-kirki-parent-tab-item' => isset( $args['tab'] ) ? $args['tab'] : '', |
| 66 | ); |
| 67 | |
| 68 | if ( isset( $args['wrapper_attrs'] ) ) { |
| 69 | $args['wrapper_attrs'] = array_merge( $args['wrapper_attrs'], $tab_wrapper_attrs ); |
| 70 | } else { |
| 71 | $args['wrapper_attrs'] = $tab_wrapper_attrs; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $args; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Add the tab by creating custom control using Kirki API. |
| 82 | * |
| 83 | * @since 1.0.0 |
| 84 | * |
| 85 | * @param string $section_id The The section id. |
| 86 | * @param array $args The section args. |
| 87 | */ |
| 88 | public function add_tab( $section_id, $args ) { |
| 89 | |
| 90 | if ( ! isset( $args['tabs'] ) ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | new \Kirki\Field\Tabs( |
| 95 | [ |
| 96 | 'settings' => 'kirki_tabs_' . $section_id, |
| 97 | 'section' => $section_id, |
| 98 | 'priority' => 0, |
| 99 | 'choices' => [ |
| 100 | 'tabs' => $args['tabs'], |
| 101 | ], |
| 102 | ] |
| 103 | ); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | } |