class-wp-customize-panel.php (10400B)
1 <?php 2 /** 3 * WordPress Customize Panel classes 4 * 5 * @package WordPress 6 * @subpackage Customize 7 * @since 4.0.0 8 */ 9 10 /** 11 * Customize Panel class. 12 * 13 * A UI container for sections, managed by the WP_Customize_Manager. 14 * 15 * @since 4.0.0 16 * 17 * @see WP_Customize_Manager 18 */ 19 class WP_Customize_Panel { 20 21 /** 22 * Incremented with each new class instantiation, then stored in $instance_number. 23 * 24 * Used when sorting two instances whose priorities are equal. 25 * 26 * @since 4.1.0 27 * @var int 28 */ 29 protected static $instance_count = 0; 30 31 /** 32 * Order in which this instance was created in relation to other instances. 33 * 34 * @since 4.1.0 35 * @var int 36 */ 37 public $instance_number; 38 39 /** 40 * WP_Customize_Manager instance. 41 * 42 * @since 4.0.0 43 * @var WP_Customize_Manager 44 */ 45 public $manager; 46 47 /** 48 * Unique identifier. 49 * 50 * @since 4.0.0 51 * @var string 52 */ 53 public $id; 54 55 /** 56 * Priority of the panel, defining the display order of panels and sections. 57 * 58 * @since 4.0.0 59 * @var integer 60 */ 61 public $priority = 160; 62 63 /** 64 * Capability required for the panel. 65 * 66 * @since 4.0.0 67 * @var string 68 */ 69 public $capability = 'edit_theme_options'; 70 71 /** 72 * Theme features required to support the panel. 73 * 74 * @since 4.0.0 75 * @var string|string[] 76 */ 77 public $theme_supports = ''; 78 79 /** 80 * Title of the panel to show in UI. 81 * 82 * @since 4.0.0 83 * @var string 84 */ 85 public $title = ''; 86 87 /** 88 * Description to show in the UI. 89 * 90 * @since 4.0.0 91 * @var string 92 */ 93 public $description = ''; 94 95 /** 96 * Auto-expand a section in a panel when the panel is expanded when the panel only has the one section. 97 * 98 * @since 4.7.4 99 * @var bool 100 */ 101 public $auto_expand_sole_section = false; 102 103 /** 104 * Customizer sections for this panel. 105 * 106 * @since 4.0.0 107 * @var array 108 */ 109 public $sections; 110 111 /** 112 * Type of this panel. 113 * 114 * @since 4.1.0 115 * @var string 116 */ 117 public $type = 'default'; 118 119 /** 120 * Active callback. 121 * 122 * @since 4.1.0 123 * 124 * @see WP_Customize_Section::active() 125 * 126 * @var callable Callback is called with one argument, the instance of 127 * WP_Customize_Section, and returns bool to indicate whether 128 * the section is active (such as it relates to the URL currently 129 * being previewed). 130 */ 131 public $active_callback = ''; 132 133 /** 134 * Constructor. 135 * 136 * Any supplied $args override class property defaults. 137 * 138 * @since 4.0.0 139 * 140 * @param WP_Customize_Manager $manager Customizer bootstrap instance. 141 * @param string $id A specific ID for the panel. 142 * @param array $args { 143 * Optional. Array of properties for the new Panel object. Default empty array. 144 * 145 * @type int $priority Priority of the panel, defining the display order 146 * of panels and sections. Default 160. 147 * @type string $capability Capability required for the panel. 148 * Default `edit_theme_options`. 149 * @type string|string[] $theme_supports Theme features required to support the panel. 150 * @type string $title Title of the panel to show in UI. 151 * @type string $description Description to show in the UI. 152 * @type string $type Type of the panel. 153 * @type callable $active_callback Active callback. 154 * } 155 */ 156 public function __construct( $manager, $id, $args = array() ) { 157 $keys = array_keys( get_object_vars( $this ) ); 158 foreach ( $keys as $key ) { 159 if ( isset( $args[ $key ] ) ) { 160 $this->$key = $args[ $key ]; 161 } 162 } 163 164 $this->manager = $manager; 165 $this->id = $id; 166 if ( empty( $this->active_callback ) ) { 167 $this->active_callback = array( $this, 'active_callback' ); 168 } 169 self::$instance_count += 1; 170 $this->instance_number = self::$instance_count; 171 172 $this->sections = array(); // Users cannot customize the $sections array. 173 } 174 175 /** 176 * Check whether panel is active to current Customizer preview. 177 * 178 * @since 4.1.0 179 * 180 * @return bool Whether the panel is active to the current preview. 181 */ 182 final public function active() { 183 $panel = $this; 184 $active = call_user_func( $this->active_callback, $this ); 185 186 /** 187 * Filters response of WP_Customize_Panel::active(). 188 * 189 * @since 4.1.0 190 * 191 * @param bool $active Whether the Customizer panel is active. 192 * @param WP_Customize_Panel $panel WP_Customize_Panel instance. 193 */ 194 $active = apply_filters( 'customize_panel_active', $active, $panel ); 195 196 return $active; 197 } 198 199 /** 200 * Default callback used when invoking WP_Customize_Panel::active(). 201 * 202 * Subclasses can override this with their specific logic, or they may 203 * provide an 'active_callback' argument to the constructor. 204 * 205 * @since 4.1.0 206 * 207 * @return bool Always true. 208 */ 209 public function active_callback() { 210 return true; 211 } 212 213 /** 214 * Gather the parameters passed to client JavaScript via JSON. 215 * 216 * @since 4.1.0 217 * 218 * @return array The array to be exported to the client as JSON. 219 */ 220 public function json() { 221 $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) ); 222 $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) ); 223 $array['content'] = $this->get_content(); 224 $array['active'] = $this->active(); 225 $array['instanceNumber'] = $this->instance_number; 226 $array['autoExpandSoleSection'] = $this->auto_expand_sole_section; 227 return $array; 228 } 229 230 /** 231 * Checks required user capabilities and whether the theme has the 232 * feature support required by the panel. 233 * 234 * @since 4.0.0 235 * 236 * @return bool False if theme doesn't support the panel or the user doesn't have the capability. 237 */ 238 final public function check_capabilities() { 239 if ( $this->capability && ! current_user_can( $this->capability ) ) { 240 return false; 241 } 242 243 if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) { 244 return false; 245 } 246 247 return true; 248 } 249 250 /** 251 * Get the panel's content template for insertion into the Customizer pane. 252 * 253 * @since 4.1.0 254 * 255 * @return string Content for the panel. 256 */ 257 final public function get_content() { 258 ob_start(); 259 $this->maybe_render(); 260 return trim( ob_get_clean() ); 261 } 262 263 /** 264 * Check capabilities and render the panel. 265 * 266 * @since 4.0.0 267 */ 268 final public function maybe_render() { 269 if ( ! $this->check_capabilities() ) { 270 return; 271 } 272 273 /** 274 * Fires before rendering a Customizer panel. 275 * 276 * @since 4.0.0 277 * 278 * @param WP_Customize_Panel $this WP_Customize_Panel instance. 279 */ 280 do_action( 'customize_render_panel', $this ); 281 282 /** 283 * Fires before rendering a specific Customizer panel. 284 * 285 * The dynamic portion of the hook name, `$this->id`, refers to 286 * the ID of the specific Customizer panel to be rendered. 287 * 288 * @since 4.0.0 289 */ 290 do_action( "customize_render_panel_{$this->id}" ); 291 292 $this->render(); 293 } 294 295 /** 296 * Render the panel container, and then its contents (via `this->render_content()`) in a subclass. 297 * 298 * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template(). 299 * 300 * @since 4.0.0 301 */ 302 protected function render() {} 303 304 /** 305 * Render the panel UI in a subclass. 306 * 307 * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template(). 308 * 309 * @since 4.1.0 310 */ 311 protected function render_content() {} 312 313 /** 314 * Render the panel's JS templates. 315 * 316 * This function is only run for panel types that have been registered with 317 * WP_Customize_Manager::register_panel_type(). 318 * 319 * @since 4.3.0 320 * 321 * @see WP_Customize_Manager::register_panel_type() 322 */ 323 public function print_template() { 324 ?> 325 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content"> 326 <?php $this->content_template(); ?> 327 </script> 328 <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>"> 329 <?php $this->render_template(); ?> 330 </script> 331 <?php 332 } 333 334 /** 335 * An Underscore (JS) template for rendering this panel's container. 336 * 337 * Class variables for this panel class are available in the `data` JS object; 338 * export custom variables by overriding WP_Customize_Panel::json(). 339 * 340 * @see WP_Customize_Panel::print_template() 341 * 342 * @since 4.3.0 343 */ 344 protected function render_template() { 345 ?> 346 <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}"> 347 <h3 class="accordion-section-title" tabindex="0"> 348 {{ data.title }} 349 <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span> 350 </h3> 351 <ul class="accordion-sub-container control-panel-content"></ul> 352 </li> 353 <?php 354 } 355 356 /** 357 * An Underscore (JS) template for this panel's content (but not its container). 358 * 359 * Class variables for this panel class are available in the `data` JS object; 360 * export custom variables by overriding WP_Customize_Panel::json(). 361 * 362 * @see WP_Customize_Panel::print_template() 363 * 364 * @since 4.3.0 365 */ 366 protected function content_template() { 367 ?> 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"> 372 <?php 373 /* translators: %s: The site/panel title in the Customizer. */ 374 printf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' ); 375 ?> 376 </span> 377 <# if ( data.description ) { #> 378 <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button> 379 <# } #> 380 </div> 381 <# if ( data.description ) { #> 382 <div class="description customize-panel-description"> 383 {{{ data.description }}} 384 </div> 385 <# } #> 386 387 <div class="customize-control-notifications-container"></div> 388 </li> 389 <?php 390 } 391 } 392 393 /** WP_Customize_Nav_Menus_Panel class */ 394 require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php';