WP_Customize_Section

Customize Section class.

Defined (1)

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

/wp-includes/class-wp-customize-section.php  
  1. class WP_Customize_Section { 
  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 3.4.0 
  24. * @access public 
  25. * @var WP_Customize_Manager 
  26. */ 
  27. public $manager; 
  28.  
  29. /** 
  30. * Unique identifier. 
  31. * @since 3.4.0 
  32. * @access public 
  33. * @var string 
  34. */ 
  35. public $id; 
  36.  
  37. /** 
  38. * Priority of the section which informs load order of sections. 
  39. * @since 3.4.0 
  40. * @access public 
  41. * @var integer 
  42. */ 
  43. public $priority = 160; 
  44.  
  45. /** 
  46. * Panel in which to show the section, making it a sub-section. 
  47. * @since 4.0.0 
  48. * @access public 
  49. * @var string 
  50. */ 
  51. public $panel = ''; 
  52.  
  53. /** 
  54. * Capability required for the section. 
  55. * @since 3.4.0 
  56. * @access public 
  57. * @var string 
  58. */ 
  59. public $capability = 'edit_theme_options'; 
  60.  
  61. /** 
  62. * Theme feature support for the section. 
  63. * @since 3.4.0 
  64. * @access public 
  65. * @var string|array 
  66. */ 
  67. public $theme_supports = ''; 
  68.  
  69. /** 
  70. * Title of the section to show in UI. 
  71. * @since 3.4.0 
  72. * @access public 
  73. * @var string 
  74. */ 
  75. public $title = ''; 
  76.  
  77. /** 
  78. * Description to show in the UI. 
  79. * @since 3.4.0 
  80. * @access public 
  81. * @var string 
  82. */ 
  83. public $description = ''; 
  84.  
  85. /** 
  86. * Customizer controls for this section. 
  87. * @since 3.4.0 
  88. * @access public 
  89. * @var array 
  90. */ 
  91. public $controls; 
  92.  
  93. /** 
  94. * Type of this section. 
  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. * Show the description or hide it behind the help icon. 
  115. * @since 4.7.0 
  116. * @access public 
  117. * @var bool Indicates whether the Section's description should be 
  118. * hidden behind a help icon ("?") in the Section header,  
  119. * similar to how help icons are displayed on Panels. 
  120. */ 
  121. public $description_hidden = false; 
  122.  
  123. /** 
  124. * Constructor. 
  125. * Any supplied $args override class property defaults. 
  126. * @since 3.4.0 
  127. * @param WP_Customize_Manager $manager Customizer bootstrap instance. 
  128. * @param string $id An specific ID of the section. 
  129. * @param array $args Section arguments. 
  130. */ 
  131. public function __construct( $manager, $id, $args = array() ) { 
  132. $keys = array_keys( get_object_vars( $this ) ); 
  133. foreach ( $keys as $key ) { 
  134. if ( isset( $args[ $key ] ) ) { 
  135. $this->$key = $args[ $key ]; 
  136.  
  137. $this->manager = $manager; 
  138. $this->id = $id; 
  139. if ( empty( $this->active_callback ) ) { 
  140. $this->active_callback = array( $this, 'active_callback' ); 
  141. self::$instance_count += 1; 
  142. $this->instance_number = self::$instance_count; 
  143.  
  144. $this->controls = array(); // Users cannot customize the $controls array. 
  145.  
  146. /** 
  147. * Check whether section is active to current Customizer preview. 
  148. * @since 4.1.0 
  149. * @access public 
  150. * @return bool Whether the section is active to the current preview. 
  151. */ 
  152. final public function active() { 
  153. $section = $this; 
  154. $active = call_user_func( $this->active_callback, $this ); 
  155.  
  156. /** 
  157. * Filters response of WP_Customize_Section::active(). 
  158. * @since 4.1.0 
  159. * @param bool $active Whether the Customizer section is active. 
  160. * @param WP_Customize_Section $section WP_Customize_Section instance. 
  161. */ 
  162. $active = apply_filters( 'customize_section_active', $active, $section ); 
  163.  
  164. return $active; 
  165.  
  166. /** 
  167. * Default callback used when invoking WP_Customize_Section::active(). 
  168. * Subclasses can override this with their specific logic, or they may provide 
  169. * an 'active_callback' argument to the constructor. 
  170. * @since 4.1.0 
  171. * @access public 
  172. * @return true Always true. 
  173. */ 
  174. public function active_callback() { 
  175. return true; 
  176.  
  177. /** 
  178. * Gather the parameters passed to client JavaScript via JSON. 
  179. * @since 4.1.0 
  180. * @return array The array to be exported to the client as JSON. 
  181. */ 
  182. public function json() { 
  183. $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type', 'description_hidden' ) ); 
  184. $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) ); 
  185. $array['content'] = $this->get_content(); 
  186. $array['active'] = $this->active(); 
  187. $array['instanceNumber'] = $this->instance_number; 
  188.  
  189. if ( $this->panel ) { 
  190. /** translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ 
  191. $array['customizeAction'] = sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) ); 
  192. } else { 
  193. $array['customizeAction'] = __( 'Customizing' ); 
  194.  
  195. return $array; 
  196.  
  197. /** 
  198. * Checks required user capabilities and whether the theme has the 
  199. * feature support required by the section. 
  200. * @since 3.4.0 
  201. * @return bool False if theme doesn't support the section or user doesn't have the capability. 
  202. */ 
  203. final public function check_capabilities() { 
  204. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) { 
  205. return false; 
  206.  
  207. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) { 
  208. return false; 
  209.  
  210. return true; 
  211.  
  212. /** 
  213. * Get the section's content for insertion into the Customizer pane. 
  214. * @since 4.1.0 
  215. * @return string Contents of the section. 
  216. */ 
  217. final public function get_content() { 
  218. ob_start(); 
  219. $this->maybe_render(); 
  220. return trim( ob_get_clean() ); 
  221.  
  222. /** 
  223. * Check capabilities and render the section. 
  224. * @since 3.4.0 
  225. */ 
  226. final public function maybe_render() { 
  227. if ( ! $this->check_capabilities() ) { 
  228. return; 
  229.  
  230. /** 
  231. * Fires before rendering a Customizer section. 
  232. * @since 3.4.0 
  233. * @param WP_Customize_Section $this WP_Customize_Section instance. 
  234. */ 
  235. do_action( 'customize_render_section', $this ); 
  236. /** 
  237. * Fires before rendering a specific Customizer section. 
  238. * The dynamic portion of the hook name, `$this->id`, refers to the ID 
  239. * of the specific Customizer section to be rendered. 
  240. * @since 3.4.0 
  241. */ 
  242. do_action( "customize_render_section_{$this->id}" ); 
  243.  
  244. $this->render(); 
  245.  
  246. /** 
  247. * Render the section UI in a subclass. 
  248. * Sections are now rendered in JS by default, see WP_Customize_Section::print_template(). 
  249. * @since 3.4.0 
  250. */ 
  251. protected function render() {} 
  252.  
  253. /** 
  254. * Render the section's JS template. 
  255. * This function is only run for section types that have been registered with 
  256. * WP_Customize_Manager::register_section_type(). 
  257. * @since 4.3.0 
  258. * @access public 
  259. * @see WP_Customize_Manager::render_template() 
  260. */ 
  261. public function print_template() { 
  262. ?> 
  263. <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>"> 
  264. <?php $this->render_template(); ?> 
  265. </script> 
  266. <?php 
  267.  
  268. /** 
  269. * An Underscore (JS) template for rendering this section. 
  270. * Class variables for this section class are available in the `data` JS object; 
  271. * export custom variables by overriding WP_Customize_Section::json(). 
  272. * @since 4.3.0 
  273. * @access protected 
  274. * @see WP_Customize_Section::print_template() 
  275. */ 
  276. protected function render_template() { 
  277. ?> 
  278. <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}"> 
  279. <h3 class="accordion-section-title" tabindex="0"> 
  280. {{ data.title }} 
  281. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span> 
  282. </h3> 
  283. <ul class="accordion-section-content"> 
  284. <li class="customize-section-description-container section-meta <# if ( data.description_hidden ) { #>customize-info<# } #>"> 
  285. <div class="customize-section-title"> 
  286. <button class="customize-section-back" tabindex="-1"> 
  287. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span> 
  288. </button> 
  289. <h3> 
  290. <span class="customize-action"> 
  291. {{{ data.customizeAction }}} 
  292. </span> 
  293. {{ data.title }} 
  294. </h3> 
  295. <# if ( data.description && data.description_hidden ) { #> 
  296. <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button> 
  297. <div class="description customize-section-description"> 
  298. {{{ data.description }}} 
  299. </div> 
  300. <# } #> 
  301. </div> 
  302.  
  303. <# if ( data.description && ! data.description_hidden ) { #> 
  304. <div class="description customize-section-description"> 
  305. {{{ data.description }}} 
  306. </div> 
  307. <# } #> 
  308. </li> 
  309. </ul> 
  310. </li> 
  311. <?php