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. * Customizer sections for this panel. 
  79. * @since 4.0.0 
  80. * @access public 
  81. * @var array 
  82. */ 
  83. public $sections; 
  84.  
  85. /** 
  86. * Type of this panel. 
  87. * @since 4.1.0 
  88. * @access public 
  89. * @var string 
  90. */ 
  91. public $type = 'default'; 
  92.  
  93. /** 
  94. * Active callback. 
  95. * @since 4.1.0 
  96. * @access public 
  97. * @see WP_Customize_Section::active() 
  98. * @var callable Callback is called with one argument, the instance of 
  99. * WP_Customize_Section, and returns bool to indicate whether 
  100. * the section is active (such as it relates to the URL currently 
  101. * being previewed). 
  102. */ 
  103. public $active_callback = ''; 
  104.  
  105. /** 
  106. * Constructor. 
  107. * Any supplied $args override class property defaults. 
  108. * @since 4.0.0 
  109. * @param WP_Customize_Manager $manager Customizer bootstrap instance. 
  110. * @param string $id An specific ID for the panel. 
  111. * @param array $args Panel arguments. 
  112. */ 
  113. public function __construct( $manager, $id, $args = array() ) { 
  114. $keys = array_keys( get_object_vars( $this ) ); 
  115. foreach ( $keys as $key ) { 
  116. if ( isset( $args[ $key ] ) ) { 
  117. $this->$key = $args[ $key ]; 
  118.  
  119. $this->manager = $manager; 
  120. $this->id = $id; 
  121. if ( empty( $this->active_callback ) ) { 
  122. $this->active_callback = array( $this, 'active_callback' ); 
  123. self::$instance_count += 1; 
  124. $this->instance_number = self::$instance_count; 
  125.  
  126. $this->sections = array(); // Users cannot customize the $sections array. 
  127.  
  128. /** 
  129. * Check whether panel is active to current Customizer preview. 
  130. * @since 4.1.0 
  131. * @access public 
  132. * @return bool Whether the panel is active to the current preview. 
  133. */ 
  134. final public function active() { 
  135. $panel = $this; 
  136. $active = call_user_func( $this->active_callback, $this ); 
  137.  
  138. /** 
  139. * Filters response of WP_Customize_Panel::active(). 
  140. * @since 4.1.0 
  141. * @param bool $active Whether the Customizer panel is active. 
  142. * @param WP_Customize_Panel $panel WP_Customize_Panel instance. 
  143. */ 
  144. $active = apply_filters( 'customize_panel_active', $active, $panel ); 
  145.  
  146. return $active; 
  147.  
  148. /** 
  149. * Default callback used when invoking WP_Customize_Panel::active(). 
  150. * Subclasses can override this with their specific logic, or they may 
  151. * provide an 'active_callback' argument to the constructor. 
  152. * @since 4.1.0 
  153. * @access public 
  154. * @return bool Always true. 
  155. */ 
  156. public function active_callback() { 
  157. return true; 
  158.  
  159. /** 
  160. * Gather the parameters passed to client JavaScript via JSON. 
  161. * @since 4.1.0 
  162. * @return array The array to be exported to the client as JSON. 
  163. */ 
  164. public function json() { 
  165. $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) ); 
  166. $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) ); 
  167. $array['content'] = $this->get_content(); 
  168. $array['active'] = $this->active(); 
  169. $array['instanceNumber'] = $this->instance_number; 
  170. return $array; 
  171.  
  172. /** 
  173. * Checks required user capabilities and whether the theme has the 
  174. * feature support required by the panel. 
  175. * @since 4.0.0 
  176. * @return bool False if theme doesn't support the panel or the user doesn't have the capability. 
  177. */ 
  178. final public function check_capabilities() { 
  179. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) { 
  180. return false; 
  181.  
  182. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) { 
  183. return false; 
  184.  
  185. return true; 
  186.  
  187. /** 
  188. * Get the panel's content template for insertion into the Customizer pane. 
  189. * @since 4.1.0 
  190. * @return string Content for the panel. 
  191. */ 
  192. final public function get_content() { 
  193. ob_start(); 
  194. $this->maybe_render(); 
  195. return trim( ob_get_clean() ); 
  196.  
  197. /** 
  198. * Check capabilities and render the panel. 
  199. * @since 4.0.0 
  200. */ 
  201. final public function maybe_render() { 
  202. if ( ! $this->check_capabilities() ) { 
  203. return; 
  204.  
  205. /** 
  206. * Fires before rendering a Customizer panel. 
  207. * @since 4.0.0 
  208. * @param WP_Customize_Panel $this WP_Customize_Panel instance. 
  209. */ 
  210. do_action( 'customize_render_panel', $this ); 
  211.  
  212. /** 
  213. * Fires before rendering a specific Customizer panel. 
  214. * The dynamic portion of the hook name, `$this->id`, refers to 
  215. * the ID of the specific Customizer panel to be rendered. 
  216. * @since 4.0.0 
  217. */ 
  218. do_action( "customize_render_panel_{$this->id}" ); 
  219.  
  220. $this->render(); 
  221.  
  222. /** 
  223. * Render the panel container, and then its contents (via `this->render_content()`) in a subclass. 
  224. * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template(). 
  225. * @since 4.0.0 
  226. * @access protected 
  227. */ 
  228. protected function render() {} 
  229.  
  230. /** 
  231. * Render the panel UI in a subclass. 
  232. * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template(). 
  233. * @since 4.1.0 
  234. * @access protected 
  235. */ 
  236. protected function render_content() {} 
  237.  
  238. /** 
  239. * Render the panel's JS templates. 
  240. * This function is only run for panel types that have been registered with 
  241. * WP_Customize_Manager::register_panel_type(). 
  242. * @since 4.3.0 
  243. * @see WP_Customize_Manager::register_panel_type() 
  244. */ 
  245. public function print_template() { 
  246. ?> 
  247. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content"> 
  248. <?php $this->content_template(); ?> 
  249. </script> 
  250. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>"> 
  251. <?php $this->render_template(); ?> 
  252. </script> 
  253. <?php 
  254.  
  255. /** 
  256. * An Underscore (JS) template for rendering this panel's container. 
  257. * Class variables for this panel class are available in the `data` JS object; 
  258. * export custom variables by overriding WP_Customize_Panel::json(). 
  259. * @see WP_Customize_Panel::print_template() 
  260. * @since 4.3.0 
  261. * @access protected 
  262. */ 
  263. protected function render_template() { 
  264. ?> 
  265. <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}"> 
  266. <h3 class="accordion-section-title" tabindex="0"> 
  267. {{ data.title }} 
  268. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span> 
  269. </h3> 
  270. <ul class="accordion-sub-container control-panel-content"></ul> 
  271. </li> 
  272. <?php 
  273.  
  274. /** 
  275. * An Underscore (JS) template for this panel's content (but not its container). 
  276. * Class variables for this panel class are available in the `data` JS object; 
  277. * export custom variables by overriding WP_Customize_Panel::json(). 
  278. * @see WP_Customize_Panel::print_template() 
  279. * @since 4.3.0 
  280. * @access protected 
  281. */ 
  282. protected function content_template() { 
  283. ?> 
  284. <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>"> 
  285. <button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button> 
  286. <div class="accordion-section-title"> 
  287. <span class="preview-notice"><?php 
  288. /** translators: %s: the site/panel title in the Customizer */ 
  289. echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' ); 
  290. ?></span> 
  291. <# if ( data.description ) { #> 
  292. <button class="customize-help-toggle dashicons dashicons-editor-help" tabindex="0" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button> 
  293. <# } #> 
  294. </div> 
  295. <# if ( data.description ) { #> 
  296. <div class="description customize-panel-description"> 
  297. {{{ data.description }}} 
  298. </div> 
  299. <# } #> 
  300. </li> 
  301. <?php