Section.php
182 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Creates a new Section. |
| 4 | * |
| 5 | * @package kirki-framework/module-sections |
| 6 | * @copyright Copyright (c) 2023, Themeum |
| 7 | * @license https://opensource.org/licenses/MIT |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Section. |
| 19 | */ |
| 20 | class Section { |
| 21 | |
| 22 | /** |
| 23 | * The section ID. |
| 24 | * |
| 25 | * @access protected |
| 26 | * @since 1.0.0 |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $id; |
| 30 | |
| 31 | /** |
| 32 | * The section arguments. |
| 33 | * |
| 34 | * @access protected |
| 35 | * @since 1.0.0 |
| 36 | * @var array |
| 37 | */ |
| 38 | protected $args; |
| 39 | |
| 40 | /** |
| 41 | * An array of our section types. |
| 42 | * |
| 43 | * @access private |
| 44 | * @var array |
| 45 | */ |
| 46 | private $section_types = [ |
| 47 | 'kirki-expanded' => '\Kirki\Section_Types\Expanded', |
| 48 | 'kirki-nested' => '\Kirki\Section_Types\Nested', |
| 49 | 'kirki-link' => '\Kirki\Section_Types\Link', |
| 50 | 'kirki-outer' => '\Kirki\Section_Types\Outer', |
| 51 | ]; |
| 52 | |
| 53 | /** |
| 54 | * Constructor. |
| 55 | * |
| 56 | * @access public |
| 57 | * @since 1.0.0 |
| 58 | * @param string $id The section ID. |
| 59 | * @param array $args The section args. |
| 60 | */ |
| 61 | public function __construct( $id, $args = [] ) { |
| 62 | $this->id = $id; |
| 63 | $this->args = $args; |
| 64 | |
| 65 | $this->section_types = apply_filters( 'kirki_section_types', $this->section_types ); |
| 66 | |
| 67 | do_action( 'kirki_section_init', $id, $args ); |
| 68 | |
| 69 | add_action( 'customize_register', [ $this, 'register_section_types' ] ); |
| 70 | |
| 71 | if ( $this->args ) { |
| 72 | add_action( 'customize_register', [ $this, 'add_section' ] ); |
| 73 | } |
| 74 | add_action( 'customize_controls_enqueue_scripts', [ $this, 'enqueue_scrips' ] ); |
| 75 | add_action( 'customize_controls_print_footer_scripts', [ $this, 'outer_sections_css' ] ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Register section types. |
| 80 | * |
| 81 | * @access public |
| 82 | * @since 1.0.0 |
| 83 | * @param object $wp_customize The customizer object. |
| 84 | * @return void |
| 85 | */ |
| 86 | public function register_section_types( $wp_customize ) { |
| 87 | foreach ( $this->section_types as $section_type ) { |
| 88 | $wp_customize->register_section_type( $section_type ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Add the section using the Customizer API. |
| 94 | * |
| 95 | * @access public |
| 96 | * @since 1.0.0 |
| 97 | * @param object $wp_customize The customizer object. |
| 98 | */ |
| 99 | public function add_section( $wp_customize ) { |
| 100 | |
| 101 | // Figure out the type of this section. |
| 102 | $this->args['type'] = isset( $this->args['type'] ) ? $this->args['type'] : 'default'; |
| 103 | if ( isset( $this->args['section'] ) && ! empty( $this->args['section'] ) ) { |
| 104 | $this->args['type'] = 'kirki-nested'; |
| 105 | |
| 106 | // We need to check if the parent section is nested inside a panel. |
| 107 | $parent_section = $wp_customize->get_section( $this->args['section'] ); |
| 108 | if ( $parent_section && isset( $parent_section->panel ) ) { |
| 109 | $this->args['panel'] = $parent_section->panel; |
| 110 | } |
| 111 | } |
| 112 | $this->args['type'] = false === strpos( $this->args['type'], 'kirki-' ) ? 'kirki-' . $this->args['type'] : $this->args['type']; |
| 113 | |
| 114 | // Get the class we'll be using to create this section. |
| 115 | $section_classname = '\WP_Customize_Section'; |
| 116 | if ( isset( $this->section_types[ $this->args['type'] ] ) ) { |
| 117 | $section_classname = $this->section_types[ $this->args['type'] ]; |
| 118 | } |
| 119 | |
| 120 | if ( isset( $this->args['type'] ) && 'kirki-outer' === $this->args['type'] ) { |
| 121 | $this->args['type'] = 'outer'; |
| 122 | $section_classname = 'WP_Customize_Section'; // ? Bagus: we should be using `\` (backslash) right? Lookk at above. |
| 123 | } |
| 124 | |
| 125 | // Add the section. |
| 126 | $wp_customize->add_section( |
| 127 | new $section_classname( |
| 128 | $wp_customize, |
| 129 | $this->id, |
| 130 | apply_filters( 'kirki_section_args', $this->args, $this->id ) |
| 131 | ) |
| 132 | ); |
| 133 | |
| 134 | // Run an action after the section has been added. |
| 135 | do_action( 'kirki_section_added', $this->id, $this->args ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Removes the section. |
| 140 | * |
| 141 | * @access public |
| 142 | * @since 1.0.0 |
| 143 | * @return void |
| 144 | */ |
| 145 | public function remove() { |
| 146 | add_action( 'customize_register', [ $this, 'remove_section' ], 9999 ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Add the section using the Customizer API. |
| 151 | * |
| 152 | * @access public |
| 153 | * @since 1.0.0 |
| 154 | * @param object $wp_customize The customizer object. |
| 155 | */ |
| 156 | public function remove_section( $wp_customize ) { |
| 157 | $wp_customize->remove_section( $this->id ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Enqueues any necessary scripts and styles. |
| 162 | * |
| 163 | * @access public |
| 164 | * @since 1.0.0 |
| 165 | */ |
| 166 | public function enqueue_scrips() { |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Generate CSS for the outer sections. |
| 171 | * These are by default hidden, we need to expose them. |
| 172 | * |
| 173 | * @access public |
| 174 | * @since 1.0.0 |
| 175 | * @return void |
| 176 | */ |
| 177 | public function outer_sections_css() { |
| 178 | if ( isset( $this->args['type'] ) && ( 'outer' === $this->args['type'] || 'kirki-outer' === $this->args['type'] ) ) { |
| 179 | echo '<style>#customize-theme-controls li#accordion-section-' . esc_html( $this->id ) . ',li#sub-accordion-section-' . esc_html( $this->id ) . '{display:list-item!important;}</style>'; |
| 180 | } |
| 181 | } |
| 182 | } |