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 * {@see WP_Customize_Section}, and returns bool to indicate
143 * whether the section is active (such as it relates to the URL
144 * currently being previewed).
146 public $active_callback = '';
151 * Any supplied $args override class property defaults.
155 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
156 * @param string $id An specific ID of the section.
157 * @param array $args Section arguments.
159 public function __construct( $manager, $id, $args = array() ) {
160 $keys = array_keys( get_object_vars( $this ) );
161 foreach ( $keys as $key ) {
162 if ( isset( $args[ $key ] ) ) {
163 $this->$key = $args[ $key ];
167 $this->manager = $manager;
169 if ( empty( $this->active_callback ) ) {
170 $this->active_callback = array( $this, 'active_callback' );
172 self::$instance_count += 1;
173 $this->instance_number = self::$instance_count;
175 $this->controls = array(); // Users cannot customize the $controls array.
179 * Check whether section is active to current Customizer preview.
184 * @return bool Whether the section is active to the current preview.
186 final public function active() {
188 $active = call_user_func( $this->active_callback, $this );
191 * Filter response of {@see WP_Customize_Section::active()}.
195 * @param bool $active Whether the Customizer section is active.
196 * @param WP_Customize_Section $section {@see WP_Customize_Section} instance.
198 $active = apply_filters( 'customize_section_active', $active, $section );
204 * Default callback used when invoking {@see WP_Customize_Section::active()}.
206 * Subclasses can override this with their specific logic, or they may provide
207 * an 'active_callback' argument to the constructor.
212 * @return true Always true.
214 public function active_callback() {
219 * Gather the parameters passed to client JavaScript via JSON.
223 * @return array The array to be exported to the client as JSON.
225 public function json() {
226 $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type' ) );
227 $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
228 $array['content'] = $this->get_content();
229 $array['active'] = $this->active();
230 $array['instanceNumber'] = $this->instance_number;
232 if ( $this->panel ) {
233 /* translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */
234 $array['customizeAction'] = sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) );
236 $array['customizeAction'] = __( 'Customizing' );
243 * Checks required user capabilities and whether the theme has the
244 * feature support required by the section.
248 * @return bool False if theme doesn't support the section or user doesn't have the capability.
250 final public function check_capabilities() {
251 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
255 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
263 * Get the section's content for insertion into the Customizer pane.
267 * @return string Contents of the section.
269 final public function get_content() {
271 $this->maybe_render();
272 return trim( ob_get_clean() );
276 * Check capabilities and render the section.
280 final public function maybe_render() {
281 if ( ! $this->check_capabilities() ) {
286 * Fires before rendering a Customizer section.
290 * @param WP_Customize_Section $this WP_Customize_Section instance.
292 do_action( 'customize_render_section', $this );
294 * Fires before rendering a specific Customizer section.
296 * The dynamic portion of the hook name, `$this->id`, refers to the ID
297 * of the specific Customizer section to be rendered.
301 do_action( "customize_render_section_{$this->id}" );
307 * Render the section UI in a subclass.
309 * Sections are now rendered in JS by default, see {@see WP_Customize_Section::print_template()}.
313 protected function render() {}
316 * Render the section's JS template.
318 * This function is only run for section types that have been registered with
319 * WP_Customize_Manager::register_section_type().
324 * @see WP_Customize_Manager::render_template()
326 public function print_template() {
328 <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>">
329 <?php $this->render_template(); ?>
335 * An Underscore (JS) template for rendering this section.
337 * Class variables for this section class are available in the `data` JS object;
338 * export custom variables by overriding WP_Customize_Section::json().
343 * @see WP_Customize_Section::print_template()
345 protected function render_template() {
347 <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
348 <h3 class="accordion-section-title" tabindex="0">
350 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span>
352 <ul class="accordion-section-content">
353 <li class="customize-section-description-container">
354 <div class="customize-section-title">
355 <button class="customize-section-back" tabindex="-1">
356 <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
359 <span class="customize-action">
360 {{{ data.customizeAction }}}
365 <# if ( data.description ) { #>
366 <div class="description customize-section-description">
367 {{{ data.description }}}
377 /** WP_Customize_Themes_Section class */
378 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-themes-section.php' );
380 /** WP_Customize_Sidebar_Section class */
381 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-sidebar-section.php' );
383 /** WP_Customize_Nav_Menu_Section class */
384 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-section.php' );
386 /** WP_Customize_New_Menu_Section class */
387 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-section.php' );