WP_Customize_Panel

Customize Panel class.

Defined (1)

The class is defined in the following location(s).

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