Panel.php
138 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Creates a new panel. |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @subpackage Custom Sections Module |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Panel. |
| 20 | */ |
| 21 | class Panel { |
| 22 | |
| 23 | /** |
| 24 | * The panel ID. |
| 25 | * |
| 26 | * @access protected |
| 27 | * @since 1.0 |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $id; |
| 31 | |
| 32 | /** |
| 33 | * The panel arguments. |
| 34 | * |
| 35 | * @access protected |
| 36 | * @since 1.0 |
| 37 | * @var array |
| 38 | */ |
| 39 | protected $args; |
| 40 | |
| 41 | /** |
| 42 | * An array of our panel types. |
| 43 | * |
| 44 | * @access private |
| 45 | * @var array |
| 46 | */ |
| 47 | private $panel_types = [ |
| 48 | 'default' => 'WP_Customize_Panel', |
| 49 | 'kirki-nested' => '\Kirki\Panel_Types\Nested', |
| 50 | ]; |
| 51 | |
| 52 | /** |
| 53 | * Constructor. |
| 54 | * |
| 55 | * @access public |
| 56 | * @since 1.0 |
| 57 | * @param string $id The panel ID. |
| 58 | * @param array $args The panel args. |
| 59 | */ |
| 60 | public function __construct( $id, $args = [] ) { |
| 61 | $this->id = $id; |
| 62 | $this->args = $args; |
| 63 | |
| 64 | $this->panel_types = apply_filters( 'kirki_panel_types', $this->panel_types ); |
| 65 | |
| 66 | if ( $this->args ) { |
| 67 | add_action( 'customize_register', [ $this, 'add_panel' ] ); |
| 68 | } |
| 69 | add_action( 'customize_controls_enqueue_scripts', [ $this, 'enqueue_scrips' ] ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Add the panel using the Customizer API. |
| 74 | * |
| 75 | * @access public |
| 76 | * @since 1.0 |
| 77 | * @param object $wp_customize The customizer object. |
| 78 | */ |
| 79 | public function add_panel( $wp_customize ) { |
| 80 | |
| 81 | // Figure out the type of this panel. |
| 82 | $this->args['type'] = isset( $this->args['type'] ) ? $this->args['type'] : 'default'; |
| 83 | if ( isset( $this->args['panel'] ) && ! empty( $this->args['panel'] ) ) { |
| 84 | $this->args['type'] = 'kirki-nested'; |
| 85 | } |
| 86 | $this->args['type'] = false === strpos( $this->args['type'], 'kirki-' ) ? 'kirki-' . $this->args['type'] : $this->args['type']; |
| 87 | $this->args['type'] = 'kirki-default' === $this->args['type'] ? 'default' : $this->args['type']; |
| 88 | |
| 89 | // Get the class we'll be using to create this panel. |
| 90 | $panel_classname = $this->panel_types[ $this->args['type'] ]; |
| 91 | |
| 92 | // Fallback to the default panel type if the custom class doesn't exist. |
| 93 | $panel_classname = class_exists( $panel_classname ) ? $panel_classname : 'WP_Customize_Panel'; |
| 94 | |
| 95 | // Add the panel. |
| 96 | $wp_customize->add_panel( |
| 97 | new $panel_classname( |
| 98 | $wp_customize, |
| 99 | $this->id, |
| 100 | apply_filters( 'kirki_panel_args', $this->args, $this->id ) |
| 101 | ) |
| 102 | ); |
| 103 | |
| 104 | // Run an action after the panel has been added. |
| 105 | do_action( 'kirki_panel_added', $this->id, $this->args ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Removes the panel. |
| 110 | * |
| 111 | * @access public |
| 112 | * @since 1.0 |
| 113 | * @return void |
| 114 | */ |
| 115 | public function remove() { |
| 116 | add_action( 'customize_register', [ $this, 'remove_panel' ], 9999 ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add the panel using the Customizer API. |
| 121 | * |
| 122 | * @access public |
| 123 | * @since 1.0 |
| 124 | * @param object $wp_customize The customizer object. |
| 125 | */ |
| 126 | public function remove_panel( $wp_customize ) { |
| 127 | $wp_customize->remove_panel( $this->id ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Enqueues any necessary scripts and styles. |
| 132 | * |
| 133 | * @access public |
| 134 | * @since 1.0 |
| 135 | */ |
| 136 | public function enqueue_scrips() { |
| 137 | } |
| 138 | } |