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.
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 panel, defining the display order of panels and sections.
68 public $priority = 160;
71 * Capability required for the panel.
77 public $capability = 'edit_theme_options';
80 * Theme feature support for the panel.
86 public $theme_supports = '';
89 * Title of the panel to show in UI.
98 * Description to show in the UI.
104 public $description = '';
107 * Customizer sections for this panel.
116 * Type of this panel.
122 public $type = 'default';
130 * @see WP_Customize_Section::active()
132 * @var callable Callback is called with one argument, the instance of
133 * {@see WP_Customize_Section}, and returns bool to indicate
134 * whether the section is active (such as it relates to the URL
135 * currently being previewed).
137 public $active_callback = '';
142 * Any supplied $args override class property defaults.
146 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
147 * @param string $id An specific ID for the panel.
148 * @param array $args Panel arguments.
150 public function __construct( $manager, $id, $args = array() ) {
151 $keys = array_keys( get_object_vars( $this ) );
152 foreach ( $keys as $key ) {
153 if ( isset( $args[ $key ] ) ) {
154 $this->$key = $args[ $key ];
158 $this->manager = $manager;
160 if ( empty( $this->active_callback ) ) {
161 $this->active_callback = array( $this, 'active_callback' );
163 self::$instance_count += 1;
164 $this->instance_number = self::$instance_count;
166 $this->sections = array(); // Users cannot customize the $sections array.
170 * Check whether panel is active to current Customizer preview.
175 * @return bool Whether the panel is active to the current preview.
177 final public function active() {
179 $active = call_user_func( $this->active_callback, $this );
182 * Filter response of WP_Customize_Panel::active().
186 * @param bool $active Whether the Customizer panel is active.
187 * @param WP_Customize_Panel $panel {@see WP_Customize_Panel} instance.
189 $active = apply_filters( 'customize_panel_active', $active, $panel );
195 * Default callback used when invoking {@see WP_Customize_Panel::active()}.
197 * Subclasses can override this with their specific logic, or they may
198 * provide an 'active_callback' argument to the constructor.
203 * @return bool Always true.
205 public function active_callback() {
210 * Gather the parameters passed to client JavaScript via JSON.
214 * @return array The array to be exported to the client as JSON.
216 public function json() {
217 $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) );
218 $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
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 panel.
231 * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
233 final public 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 panel's content template for insertion into the Customizer pane.
250 * @return string Content for the panel.
252 final public function get_content() {
254 $this->maybe_render();
255 return trim( ob_get_clean() );
259 * Check capabilities and render the panel.
263 final public function maybe_render() {
264 if ( ! $this->check_capabilities() ) {
269 * Fires before rendering a Customizer panel.
273 * @param WP_Customize_Panel $this WP_Customize_Panel instance.
275 do_action( 'customize_render_panel', $this );
278 * Fires before rendering a specific Customizer panel.
280 * The dynamic portion of the hook name, `$this->id`, refers to
281 * the ID of the specific Customizer panel to be rendered.
285 do_action( "customize_render_panel_{$this->id}" );
291 * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
293 * Panel containers are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}.
298 protected function render() {}
301 * Render the panel UI in a subclass.
303 * Panel contents are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}.
308 protected function render_content() {}
311 * Render the panel's JS templates.
313 * This function is only run for panel types that have been registered with
314 * WP_Customize_Manager::register_panel_type().
318 * @see WP_Customize_Manager::register_panel_type()
320 public function print_template() {
322 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
323 <?php $this->content_template(); ?>
325 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
326 <?php $this->render_template(); ?>
332 * An Underscore (JS) template for rendering this panel's container.
334 * Class variables for this panel class are available in the `data` JS object;
335 * export custom variables by overriding WP_Customize_Panel::json().
337 * @see WP_Customize_Panel::print_template()
342 protected function render_template() {
344 <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
345 <h3 class="accordion-section-title" tabindex="0">
347 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
349 <ul class="accordion-sub-container control-panel-content"></ul>
355 * An Underscore (JS) template for this panel's content (but not its container).
357 * Class variables for this panel class are available in the `data` JS object;
358 * export custom variables by overriding WP_Customize_Panel::json().
360 * @see WP_Customize_Panel::print_template()
365 protected function content_template() {
367 <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
368 <button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button>
369 <div class="accordion-section-title">
370 <span class="preview-notice"><?php
371 /* translators: %s: the site/panel title in the Customizer */
372 echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
374 <# if ( data.description ) { #>
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>
378 <# if ( data.description ) { #>
379 <div class="description customize-panel-description">
380 {{{ data.description }}}
388 /** WP_Customize_Nav_Menus_Panel class */
389 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php' );