3 * WordPress Customize Panel classes
6 * @subpackage Customize
11 * Customize Panel class.
13 * A UI container for sections, managed by the WP_Customize_Manager.
17 * @see WP_Customize_Manager
19 class WP_Customize_Panel {
22 * Incremented with each new class instantiation, then stored in $instance_number.
24 * Used when sorting two instances whose priorities are equal.
33 protected static $instance_count = 0;
36 * Order in which this instance was created in relation to other instances.
42 public $instance_number;
45 * WP_Customize_Manager instance.
49 * @var WP_Customize_Manager
63 * Priority of the panel, defining the display order of panels and sections.
69 public $priority = 160;
72 * Capability required for the panel.
78 public $capability = 'edit_theme_options';
81 * Theme feature support for the panel.
87 public $theme_supports = '';
90 * Title of the panel to show in UI.
99 * Description to show in the UI.
105 public $description = '';
108 * Customizer sections for this panel.
117 * Type of this panel.
123 public $type = 'default';
131 * @see WP_Customize_Section::active()
133 * @var callable Callback is called with one argument, the instance of
134 * {@see WP_Customize_Section}, and returns bool to indicate
135 * whether the section is active (such as it relates to the URL
136 * currently being previewed).
138 public $active_callback = '';
143 * Any supplied $args override class property defaults.
147 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
148 * @param string $id An specific ID for the panel.
149 * @param array $args Panel arguments.
151 public function __construct( $manager, $id, $args = array() ) {
152 $keys = array_keys( get_object_vars( $this ) );
153 foreach ( $keys as $key ) {
154 if ( isset( $args[ $key ] ) ) {
155 $this->$key = $args[ $key ];
159 $this->manager = $manager;
161 if ( empty( $this->active_callback ) ) {
162 $this->active_callback = array( $this, 'active_callback' );
164 self::$instance_count += 1;
165 $this->instance_number = self::$instance_count;
167 $this->sections = array(); // Users cannot customize the $sections array.
171 * Check whether panel is active to current Customizer preview.
176 * @return bool Whether the panel is active to the current preview.
178 final public function active() {
180 $active = call_user_func( $this->active_callback, $this );
183 * Filter response of WP_Customize_Panel::active().
187 * @param bool $active Whether the Customizer panel is active.
188 * @param WP_Customize_Panel $panel {@see WP_Customize_Panel} instance.
190 $active = apply_filters( 'customize_panel_active', $active, $panel );
196 * Default callback used when invoking {@see WP_Customize_Panel::active()}.
198 * Subclasses can override this with their specific logic, or they may
199 * provide 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( 'id', 'description', 'priority', 'type' ) );
219 $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
220 $array['content'] = $this->get_content();
221 $array['active'] = $this->active();
222 $array['instanceNumber'] = $this->instance_number;
227 * Checks required user capabilities and whether the theme has the
228 * feature support required by the panel.
232 * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
234 final public function check_capabilities() {
235 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
239 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
247 * Get the panel's content template for insertion into the Customizer pane.
251 * @return string Content for the panel.
253 final public function get_content() {
255 $this->maybe_render();
256 return trim( ob_get_clean() );
260 * Check capabilities and render the panel.
264 final public function maybe_render() {
265 if ( ! $this->check_capabilities() ) {
270 * Fires before rendering a Customizer panel.
274 * @param WP_Customize_Panel $this WP_Customize_Panel instance.
276 do_action( 'customize_render_panel', $this );
279 * Fires before rendering a specific Customizer panel.
281 * The dynamic portion of the hook name, `$this->id`, refers to
282 * the ID of the specific Customizer panel to be rendered.
286 do_action( "customize_render_panel_{$this->id}" );
292 * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
294 * Panel containers are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}.
299 protected function render() {}
302 * Render the panel UI in a subclass.
304 * Panel contents are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}.
309 protected function render_content() {}
312 * Render the panel's JS templates.
314 * This function is only run for panel types that have been registered with
315 * WP_Customize_Manager::register_panel_type().
319 * @see WP_Customize_Manager::register_panel_type()
321 public function print_template() {
323 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
324 <?php $this->content_template(); ?>
326 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
327 <?php $this->render_template(); ?>
333 * An Underscore (JS) template for rendering this panel's container.
335 * Class variables for this panel class are available in the `data` JS object;
336 * export custom variables by overriding WP_Customize_Panel::json().
338 * @see WP_Customize_Panel::print_template()
343 protected function render_template() {
345 <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
346 <h3 class="accordion-section-title" tabindex="0">
348 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
350 <ul class="accordion-sub-container control-panel-content"></ul>
356 * An Underscore (JS) template for this panel's content (but not its container).
358 * Class variables for this panel class are available in the `data` JS object;
359 * export custom variables by overriding WP_Customize_Panel::json().
361 * @see WP_Customize_Panel::print_template()
366 protected function content_template() {
368 <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
369 <button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button>
370 <div class="accordion-section-title">
371 <span class="preview-notice"><?php
372 /* translators: %s is the site/panel title in the Customizer */
373 echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
375 <button class="customize-help-toggle dashicons dashicons-editor-help" tabindex="0" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
377 <# if ( data.description ) { #>
378 <div class="description customize-panel-description">
379 {{{ data.description }}}
387 /** WP_Customize_Nav_Menus_Panel class */
388 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php' );