3 * WordPress Customize Section classes
6 * @subpackage Customize
11 * Customize Section class.
13 * A UI container for controls, managed by the WP_Customize_Manager class.
17 * @see WP_Customize_Manager
19 class WP_Customize_Section {
22 * Incremented with each new class instantiation, then stored in $instance_number.
24 * Used when sorting two instances whose priorities are equal.
30 protected static $instance_count = 0;
33 * Order in which this instance was created in relation to other instances.
39 public $instance_number;
42 * WP_Customize_Manager instance.
46 * @var WP_Customize_Manager
60 * Priority of the section which informs load order of sections.
66 public $priority = 160;
69 * Panel in which to show the section, making it a sub-section.
78 * Capability required for the section.
84 public $capability = 'edit_theme_options';
87 * Theme feature support for the section.
93 public $theme_supports = '';
96 * Title of the section to show in UI.
105 * Description to show in the UI.
111 public $description = '';
114 * Customizer controls for this section.
123 * Type of this section.
129 public $type = 'default';
137 * @see WP_Customize_Section::active()
139 * @var callable Callback is called with one argument, the instance of
140 * {@see WP_Customize_Section}, and returns bool to indicate
141 * whether the section is active (such as it relates to the URL
142 * currently being previewed).
144 public $active_callback = '';
149 * Any supplied $args override class property defaults.
153 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
154 * @param string $id An specific ID of the section.
155 * @param array $args Section arguments.
157 public function __construct( $manager, $id, $args = array() ) {
158 $keys = array_keys( get_object_vars( $this ) );
159 foreach ( $keys as $key ) {
160 if ( isset( $args[ $key ] ) ) {
161 $this->$key = $args[ $key ];
165 $this->manager = $manager;
167 if ( empty( $this->active_callback ) ) {
168 $this->active_callback = array( $this, 'active_callback' );
170 self::$instance_count += 1;
171 $this->instance_number = self::$instance_count;
173 $this->controls = array(); // Users cannot customize the $controls array.
177 * Check whether section is active to current Customizer preview.
182 * @return bool Whether the section is active to the current preview.
184 final public function active() {
186 $active = call_user_func( $this->active_callback, $this );
189 * Filter response of {@see WP_Customize_Section::active()}.
193 * @param bool $active Whether the Customizer section is active.
194 * @param WP_Customize_Section $section {@see WP_Customize_Section} instance.
196 $active = apply_filters( 'customize_section_active', $active, $section );
202 * Default callback used when invoking {@see WP_Customize_Section::active()}.
204 * Subclasses can override this with their specific logic, or they may provide
205 * an 'active_callback' argument to the constructor.
210 * @return bool Always true.
212 public function active_callback() {
217 * Gather the parameters passed to client JavaScript via JSON.
221 * @return array The array to be exported to the client as JSON.
223 public function json() {
224 $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'panel', 'type' ) );
225 $array['content'] = $this->get_content();
226 $array['active'] = $this->active();
227 $array['instanceNumber'] = $this->instance_number;
232 * Checks required user capabilities and whether the theme has the
233 * feature support required by the section.
237 * @return bool False if theme doesn't support the section or user doesn't have the capability.
239 final public function check_capabilities() {
240 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
244 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
252 * Get the section's content template for insertion into the Customizer pane.
256 * @return string Contents of the section.
258 final public function get_content() {
260 $this->maybe_render();
261 $template = trim( ob_get_contents() );
267 * Check capabilities and render the section.
271 final public function maybe_render() {
272 if ( ! $this->check_capabilities() ) {
277 * Fires before rendering a Customizer section.
281 * @param WP_Customize_Section $this WP_Customize_Section instance.
283 do_action( 'customize_render_section', $this );
285 * Fires before rendering a specific Customizer section.
287 * The dynamic portion of the hook name, `$this->id`, refers to the ID
288 * of the specific Customizer section to be rendered.
292 do_action( "customize_render_section_{$this->id}" );
298 * Render the section, and the controls that have been added to it.
302 protected function render() {
303 $classes = 'accordion-section control-section control-section-' . $this->type;
305 <li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
306 <h3 class="accordion-section-title" tabindex="0">
307 <?php echo esc_html( $this->title ); ?>
308 <span class="screen-reader-text"><?php _e( 'Press return or enter to expand' ); ?></span>
310 <ul class="accordion-section-content">
311 <?php if ( ! empty( $this->description ) ) : ?>
312 <li class="customize-section-description-container">
313 <p class="description customize-section-description"><?php echo $this->description; ?></p>
323 * Customize Themes Section class.
325 * A UI container for theme controls, which behaves like a backwards Panel.
329 * @see WP_Customize_Section
331 class WP_Customize_Themes_Section extends WP_Customize_Section {
334 * Customize section type.
340 public $type = 'themes';
343 * Render the themes section, which behaves like a panel.
348 protected function render() {
349 $classes = 'accordion-section control-section control-section-' . $this->type;
351 <li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
352 <h3 class="accordion-section-title">
354 if ( $this->manager->is_theme_active() ) {
355 /* translators: %s: theme name */
356 printf( __( '<span>Active theme</span> %s' ), $this->title );
358 /* translators: %s: theme name */
359 printf( __( '<span>Previewing theme</span> %s' ), $this->title );
363 <button type="button" class="button change-theme"><?php _ex( 'Change', 'theme' ); ?></button>
365 <div class="customize-themes-panel control-panel-content themes-php">
367 <?php _e( 'Themes' ); ?>
368 <span class="title-count theme-count"><?php echo count( $this->controls ) + 1 /* Active theme */; ?></span>
371 <h3 class="accordion-section-title customize-section-title">
373 if ( $this->manager->is_theme_active() ) {
374 /* translators: %s: theme name */
375 printf( __( '<span>Active theme</span> %s' ), $this->title );
377 /* translators: %s: theme name */
378 printf( __( '<span>Previewing theme</span> %s' ), $this->title );
381 <button type="button" class="button customize-theme"><?php _e( 'Customize' ); ?></button>
384 <div class="theme-overlay" tabindex="0" role="dialog" aria-label="<?php esc_attr_e( 'Theme Details' ); ?>"></div>
386 <div id="customize-container"></div>
387 <?php if ( count( $this->controls ) > 4 ) : ?>
388 <p><label for="themes-filter">
389 <span class="screen-reader-text"><?php _e( 'Search installed themes...' ); ?></span>
390 <input type="text" id="themes-filter" placeholder="<?php esc_attr_e( 'Search installed themes...' ); ?>" />
393 <div class="theme-browser rendered">
394 <ul class="themes accordion-section-content">
403 * Customizer section representing widget area (sidebar).
407 * @see WP_Customize_Section
409 class WP_Customize_Sidebar_Section extends WP_Customize_Section {
412 * Type of this section.
418 public $type = 'sidebar';
430 * Gather the parameters passed to client JavaScript via JSON.
434 * @return array The array to be exported to the client as JSON.
436 public function json() {
437 $json = parent::json();
438 $json['sidebarId'] = $this->sidebar_id;
443 * Whether the current sidebar is rendered on the page.
448 * @return bool Whether sidebar is rendered.
450 public function active_callback() {
451 return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );