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.
30 protected static $instance_count = 0;
33 * Order in which this instance was created in relation to other instances.
39 public $instance_number;
42 * WP_Customize_Manager instance.
46 * @var WP_Customize_Manager
60 * Priority of the panel, defining the display order of panels and sections.
66 public $priority = 160;
69 * Capability required for the panel.
75 public $capability = 'edit_theme_options';
78 * Theme feature support for the panel.
84 public $theme_supports = '';
87 * Title of the panel to show in UI.
96 * Description to show in the UI.
102 public $description = '';
105 * Customizer sections for this panel.
114 * Type of this panel.
120 public $type = 'default';
128 * @see WP_Customize_Section::active()
130 * @var callable Callback is called with one argument, the instance of
131 * {@see WP_Customize_Section}, and returns bool to indicate
132 * whether the section is active (such as it relates to the URL
133 * currently being previewed).
135 public $active_callback = '';
140 * Any supplied $args override class property defaults.
144 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
145 * @param string $id An specific ID for the panel.
146 * @param array $args Panel arguments.
148 public function __construct( $manager, $id, $args = array() ) {
149 $keys = array_keys( get_object_vars( $this ) );
150 foreach ( $keys as $key ) {
151 if ( isset( $args[ $key ] ) ) {
152 $this->$key = $args[ $key ];
156 $this->manager = $manager;
158 if ( empty( $this->active_callback ) ) {
159 $this->active_callback = array( $this, 'active_callback' );
161 self::$instance_count += 1;
162 $this->instance_number = self::$instance_count;
164 $this->sections = array(); // Users cannot customize the $sections array.
168 * Check whether panel is active to current Customizer preview.
173 * @return bool Whether the panel is active to the current preview.
175 final public function active() {
177 $active = call_user_func( $this->active_callback, $this );
180 * Filter response of WP_Customize_Panel::active().
184 * @param bool $active Whether the Customizer panel is active.
185 * @param WP_Customize_Panel $panel {@see WP_Customize_Panel} instance.
187 $active = apply_filters( 'customize_panel_active', $active, $panel );
193 * Default callback used when invoking {@see WP_Customize_Panel::active()}.
195 * Subclasses can override this with their specific logic, or they may
196 * provide an 'active_callback' argument to the constructor.
201 * @return bool Always true.
203 public function active_callback() {
208 * Gather the parameters passed to client JavaScript via JSON.
212 * @return array The array to be exported to the client as JSON.
214 public function json() {
215 $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'type' ) );
216 $array['content'] = $this->get_content();
217 $array['active'] = $this->active();
218 $array['instanceNumber'] = $this->instance_number;
223 * Checks required user capabilities and whether the theme has the
224 * feature support required by the panel.
228 * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
230 final public function check_capabilities() {
231 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
235 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
243 * Get the panel's content template for insertion into the Customizer pane.
247 * @return string Content for the panel.
249 final public function get_content() {
251 $this->maybe_render();
252 $template = trim( ob_get_contents() );
258 * Check capabilities and render the panel.
262 final public function maybe_render() {
263 if ( ! $this->check_capabilities() ) {
268 * Fires before rendering a Customizer panel.
272 * @param WP_Customize_Panel $this WP_Customize_Panel instance.
274 do_action( 'customize_render_panel', $this );
277 * Fires before rendering a specific Customizer panel.
279 * The dynamic portion of the hook name, `$this->id`, refers to
280 * the ID of the specific Customizer panel to be rendered.
284 do_action( "customize_render_panel_{$this->id}" );
290 * Render the panel container, and then its contents.
295 protected function render() {
296 $classes = 'accordion-section control-section control-panel control-panel-' . $this->type;
298 <li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
299 <h3 class="accordion-section-title" tabindex="0">
300 <?php echo esc_html( $this->title ); ?>
301 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
303 <ul class="accordion-sub-container control-panel-content">
304 <?php $this->render_content(); ?>
311 * Render the sections that have been added to the panel.
316 protected function render_content() {
318 <li class="panel-meta accordion-section control-section<?php if ( empty( $this->description ) ) { echo ' cannot-expand'; } ?>">
319 <div class="accordion-section-title" tabindex="0">
320 <span class="preview-notice"><?php
321 /* translators: %s is the site/panel title in the Customizer */
322 echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">' . esc_html( $this->title ) . '</strong>' );
325 <?php if ( ! empty( $this->description ) ) : ?>
326 <div class="accordion-section-content description">
327 <?php echo $this->description; ?>