3 * Customize Panel Class.
5 * A UI container for sections, managed by the WP_Customize_Manager.
8 * @subpackage Customize
11 class WP_Customize_Panel {
14 * Incremented with each new class instantiation, then stored in $instance_number.
16 * Used when sorting two instances whose priorities are equal.
22 protected static $instance_count = 0;
25 * Order in which this instance was created in relation to other instances.
31 public $instance_number;
34 * WP_Customize_Manager instance.
38 * @var WP_Customize_Manager
52 * Priority of the panel, defining the display order of panels and sections.
58 public $priority = 160;
61 * Capability required for the panel.
67 public $capability = 'edit_theme_options';
70 * Theme feature support for the panel.
76 public $theme_supports = '';
79 * Title of the panel to show in UI.
88 * Description to show in the UI.
94 public $description = '';
97 * Customizer sections for this panel.
106 * Type of this panel.
112 public $type = 'default';
120 * @see WP_Customize_Section::active()
122 * @var callable Callback is called with one argument, the instance of
123 * {@see WP_Customize_Section}, and returns bool to indicate
124 * whether the section is active (such as it relates to the URL
125 * currently being previewed).
127 public $active_callback = '';
132 * Any supplied $args override class property defaults.
136 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
137 * @param string $id An specific ID for the panel.
138 * @param array $args Panel arguments.
140 public function __construct( $manager, $id, $args = array() ) {
141 $keys = array_keys( get_object_vars( $this ) );
142 foreach ( $keys as $key ) {
143 if ( isset( $args[ $key ] ) ) {
144 $this->$key = $args[ $key ];
148 $this->manager = $manager;
150 if ( empty( $this->active_callback ) ) {
151 $this->active_callback = array( $this, 'active_callback' );
153 self::$instance_count += 1;
154 $this->instance_number = self::$instance_count;
156 $this->sections = array(); // Users cannot customize the $sections array.
162 * Check whether panel is active to current Customizer preview.
167 * @return bool Whether the panel is active to the current preview.
169 public final function active() {
171 $active = call_user_func( $this->active_callback, $this );
174 * Filter response of WP_Customize_Panel::active().
178 * @param bool $active Whether the Customizer panel is active.
179 * @param WP_Customize_Panel $panel {@see WP_Customize_Panel} instance.
181 $active = apply_filters( 'customize_panel_active', $active, $panel );
187 * Default callback used when invoking {@see WP_Customize_Panel::active()}.
189 * Subclasses can override this with their specific logic, or they may
190 * provide an 'active_callback' argument to the constructor.
195 * @return bool Always true.
197 public function active_callback() {
202 * Gather the parameters passed to client JavaScript via JSON.
206 * @return array The array to be exported to the client as JSON.
208 public function json() {
209 $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'type' ) );
210 $array['content'] = $this->get_content();
211 $array['active'] = $this->active();
212 $array['instanceNumber'] = $this->instance_number;
217 * Checks required user capabilities and whether the theme has the
218 * feature support required by the panel.
222 * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
224 public final function check_capabilities() {
225 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
229 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
237 * Get the panel's content template for insertion into the Customizer pane.
241 * @return string Content for the panel.
243 public final function get_content() {
245 $this->maybe_render();
246 $template = trim( ob_get_contents() );
252 * Check capabilities and render the panel.
256 public final function maybe_render() {
257 if ( ! $this->check_capabilities() ) {
262 * Fires before rendering a Customizer panel.
266 * @param WP_Customize_Panel $this WP_Customize_Panel instance.
268 do_action( 'customize_render_panel', $this );
271 * Fires before rendering a specific Customizer panel.
273 * The dynamic portion of the hook name, `$this->id`, refers to
274 * the ID of the specific Customizer panel to be rendered.
278 do_action( "customize_render_panel_{$this->id}" );
284 * Render the panel container, and then its contents.
289 protected function render() {
290 $classes = 'accordion-section control-section control-panel control-panel-' . $this->type;
292 <li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
293 <h3 class="accordion-section-title" tabindex="0">
294 <?php echo esc_html( $this->title ); ?>
295 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
297 <ul class="accordion-sub-container control-panel-content">
298 <?php $this->render_content(); ?>
305 * Render the sections that have been added to the panel.
310 protected function render_content() {
312 <li class="panel-meta accordion-section control-section<?php if ( empty( $this->description ) ) { echo ' cannot-expand'; } ?>">
313 <div class="accordion-section-title" tabindex="0">
314 <span class="preview-notice"><?php
315 /* translators: %s is the site/panel title in the Customizer */
316 echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">' . esc_html( $this->title ) . '</strong>' );
319 <?php if ( ! empty( $this->description ) ) : ?>
320 <div class="accordion-section-content description">
321 <?php echo $this->description; ?>