3 * Customize Section Class.
5 * A UI container for controls, managed by the WP_Customize_Manager.
8 * @subpackage Customize
11 class WP_Customize_Section {
14 * Incremented with each new class instantiation, then stored in $instance_number.
16 * Used when sorting two instances whose priorities are equal.
22 protected static $instance_count = 0;
25 * Order in which this instance was created in relation to other instances.
31 public $instance_number;
34 * WP_Customize_Manager instance.
38 * @var WP_Customize_Manager
52 * Priority of the section which informs load order of sections.
58 public $priority = 160;
61 * Panel in which to show the section, making it a sub-section.
70 * Capability required for the section.
76 public $capability = 'edit_theme_options';
79 * Theme feature support for the section.
85 public $theme_supports = '';
88 * Title of the section to show in UI.
97 * Description to show in the UI.
103 public $description = '';
106 * Customizer controls for this section.
115 * Type of this section.
121 public $type = 'default';
129 * @see WP_Customize_Section::active()
131 * @var callable Callback is called with one argument, the instance of
132 * {@see WP_Customize_Section}, and returns bool to indicate
133 * whether the section is active (such as it relates to the URL
134 * currently being previewed).
136 public $active_callback = '';
141 * Any supplied $args override class property defaults.
145 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
146 * @param string $id An specific ID of the section.
147 * @param array $args Section arguments.
149 public function __construct( $manager, $id, $args = array() ) {
150 $keys = array_keys( get_object_vars( $this ) );
151 foreach ( $keys as $key ) {
152 if ( isset( $args[ $key ] ) ) {
153 $this->$key = $args[ $key ];
157 $this->manager = $manager;
159 if ( empty( $this->active_callback ) ) {
160 $this->active_callback = array( $this, 'active_callback' );
162 self::$instance_count += 1;
163 $this->instance_number = self::$instance_count;
165 $this->controls = array(); // Users cannot customize the $controls array.
171 * Check whether section is active to current Customizer preview.
176 * @return bool Whether the section is active to the current preview.
178 public final function active() {
180 $active = call_user_func( $this->active_callback, $this );
183 * Filter response of {@see WP_Customize_Section::active()}.
187 * @param bool $active Whether the Customizer section is active.
188 * @param WP_Customize_Section $section {@see WP_Customize_Section} instance.
190 $active = apply_filters( 'customize_section_active', $active, $section );
196 * Default callback used when invoking {@see WP_Customize_Section::active()}.
198 * Subclasses can override this with their specific logic, or they may provide
199 * an 'active_callback' argument to the constructor.
204 * @return bool Always true.
206 public function active_callback() {
211 * Gather the parameters passed to client JavaScript via JSON.
215 * @return array The array to be exported to the client as JSON.
217 public function json() {
218 $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'panel', 'type' ) );
219 $array['content'] = $this->get_content();
220 $array['active'] = $this->active();
221 $array['instanceNumber'] = $this->instance_number;
226 * Checks required user capabilities and whether the theme has the
227 * feature support required by the section.
231 * @return bool False if theme doesn't support the section or user doesn't have the capability.
233 public final function check_capabilities() {
234 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
238 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
246 * Get the section's content template for insertion into the Customizer pane.
250 * @return string Contents of the section.
252 public final function get_content() {
254 $this->maybe_render();
255 $template = trim( ob_get_contents() );
261 * Check capabilities and render the section.
265 public final function maybe_render() {
266 if ( ! $this->check_capabilities() ) {
271 * Fires before rendering a Customizer section.
275 * @param WP_Customize_Section $this WP_Customize_Section instance.
277 do_action( 'customize_render_section', $this );
279 * Fires before rendering a specific Customizer section.
281 * The dynamic portion of the hook name, `$this->id`, refers to the ID
282 * of the specific Customizer section to be rendered.
286 do_action( "customize_render_section_{$this->id}" );
292 * Render the section, and the controls that have been added to it.
296 protected function render() {
297 $classes = 'accordion-section control-section control-section-' . $this->type;
299 <li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
300 <h3 class="accordion-section-title" tabindex="0">
301 <?php echo esc_html( $this->title ); ?>
302 <span class="screen-reader-text"><?php _e( 'Press return or enter to expand' ); ?></span>
304 <ul class="accordion-section-content">
305 <?php if ( ! empty( $this->description ) ) : ?>
306 <li class="customize-section-description-container">
307 <p class="description customize-section-description"><?php echo $this->description; ?></p>
317 * Customizer section representing widget area (sidebar).
320 * @subpackage Customize
324 * @see WP_Customize_Section
326 class WP_Customize_Sidebar_Section extends WP_Customize_Section {
329 * Type of this section.
335 public $type = 'sidebar';
347 * Gather the parameters passed to client JavaScript via JSON.
351 * @return array The array to be exported to the client as JSON.
353 public function json() {
354 $json = parent::json();
355 $json['sidebarId'] = $this->sidebar_id;
360 * Whether the current sidebar is rendered on the page.
365 * @return bool Whether sidebar is rendered.
367 public function active_callback() {
368 return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );