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.
32 protected static $instance_count = 0;
35 * Order in which this instance was created in relation to other instances.
41 public $instance_number;
44 * WP_Customize_Manager instance.
48 * @var WP_Customize_Manager
62 * Priority of the section which informs load order of sections.
68 public $priority = 160;
71 * Panel in which to show the section, making it a sub-section.
80 * Capability required for the section.
86 public $capability = 'edit_theme_options';
89 * Theme feature support for the section.
95 public $theme_supports = '';
98 * Title of the section to show in UI.
107 * Description to show in the UI.
113 public $description = '';
116 * Customizer controls for this section.
125 * Type of this section.
131 public $type = 'default';
139 * @see WP_Customize_Section::active()
141 * @var callable Callback is called with one argument, the instance of
142 * WP_Customize_Section, and returns bool to indicate whether
143 * the section is active (such as it relates to the URL currently
146 public $active_callback = '';
149 * Show the description or hide it behind the help icon.
154 * @var bool Indicates whether the Section's description should be
155 * hidden behind a help icon ("?") in the Section header,
156 * similar to how help icons are displayed on Panels.
158 public $description_hidden = false;
163 * Any supplied $args override class property defaults.
167 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
168 * @param string $id An specific ID of the section.
169 * @param array $args Section arguments.
171 public function __construct( $manager, $id, $args = array() ) {
172 $keys = array_keys( get_object_vars( $this ) );
173 foreach ( $keys as $key ) {
174 if ( isset( $args[ $key ] ) ) {
175 $this->$key = $args[ $key ];
179 $this->manager = $manager;
181 if ( empty( $this->active_callback ) ) {
182 $this->active_callback = array( $this, 'active_callback' );
184 self::$instance_count += 1;
185 $this->instance_number = self::$instance_count;
187 $this->controls = array(); // Users cannot customize the $controls array.
191 * Check whether section is active to current Customizer preview.
196 * @return bool Whether the section is active to the current preview.
198 final public function active() {
200 $active = call_user_func( $this->active_callback, $this );
203 * Filters response of WP_Customize_Section::active().
207 * @param bool $active Whether the Customizer section is active.
208 * @param WP_Customize_Section $section WP_Customize_Section instance.
210 $active = apply_filters( 'customize_section_active', $active, $section );
216 * Default callback used when invoking WP_Customize_Section::active().
218 * Subclasses can override this with their specific logic, or they may provide
219 * an 'active_callback' argument to the constructor.
224 * @return true Always true.
226 public function active_callback() {
231 * Gather the parameters passed to client JavaScript via JSON.
235 * @return array The array to be exported to the client as JSON.
237 public function json() {
238 $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type', 'description_hidden' ) );
239 $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
240 $array['content'] = $this->get_content();
241 $array['active'] = $this->active();
242 $array['instanceNumber'] = $this->instance_number;
244 if ( $this->panel ) {
245 /* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */
246 $array['customizeAction'] = sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) );
248 $array['customizeAction'] = __( 'Customizing' );
255 * Checks required user capabilities and whether the theme has the
256 * feature support required by the section.
260 * @return bool False if theme doesn't support the section or user doesn't have the capability.
262 final public function check_capabilities() {
263 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
267 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
275 * Get the section's content for insertion into the Customizer pane.
279 * @return string Contents of the section.
281 final public function get_content() {
283 $this->maybe_render();
284 return trim( ob_get_clean() );
288 * Check capabilities and render the section.
292 final public function maybe_render() {
293 if ( ! $this->check_capabilities() ) {
298 * Fires before rendering a Customizer section.
302 * @param WP_Customize_Section $this WP_Customize_Section instance.
304 do_action( 'customize_render_section', $this );
306 * Fires before rendering a specific Customizer section.
308 * The dynamic portion of the hook name, `$this->id`, refers to the ID
309 * of the specific Customizer section to be rendered.
313 do_action( "customize_render_section_{$this->id}" );
319 * Render the section UI in a subclass.
321 * Sections are now rendered in JS by default, see WP_Customize_Section::print_template().
325 protected function render() {}
328 * Render the section's JS template.
330 * This function is only run for section types that have been registered with
331 * WP_Customize_Manager::register_section_type().
336 * @see WP_Customize_Manager::render_template()
338 public function print_template() {
340 <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>">
341 <?php $this->render_template(); ?>
347 * An Underscore (JS) template for rendering this section.
349 * Class variables for this section class are available in the `data` JS object;
350 * export custom variables by overriding WP_Customize_Section::json().
355 * @see WP_Customize_Section::print_template()
357 protected function render_template() {
359 <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
360 <h3 class="accordion-section-title" tabindex="0">
362 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span>
364 <ul class="accordion-section-content">
365 <li class="customize-section-description-container section-meta <# if ( data.description_hidden ) { #>customize-info<# } #>">
366 <div class="customize-section-title">
367 <button class="customize-section-back" tabindex="-1">
368 <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
371 <span class="customize-action">
372 {{{ data.customizeAction }}}
376 <# if ( data.description && data.description_hidden ) { #>
377 <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
378 <div class="description customize-section-description">
379 {{{ data.description }}}
384 <# if ( data.description && ! data.description_hidden ) { #>
385 <div class="description customize-section-description">
386 {{{ data.description }}}
396 /** WP_Customize_Themes_Section class */
397 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-themes-section.php' );
399 /** WP_Customize_Sidebar_Section class */
400 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-sidebar-section.php' );
402 /** WP_Customize_Nav_Menu_Section class */
403 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-section.php' );
405 /** WP_Customize_New_Menu_Section class */
406 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-section.php' );