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. * Constructor. 
  115. * Any supplied $args override class property defaults. 
  116. * @since 3.4.0 
  117. * @param WP_Customize_Manager $manager Customizer bootstrap instance. 
  118. * @param string $id An specific ID of the section. 
  119. * @param array $args Section 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->controls = array(); // Users cannot customize the $controls array. 
  135.  
  136. /** 
  137. * Check whether section is active to current Customizer preview. 
  138. * @since 4.1.0 
  139. * @access public 
  140. * @return bool Whether the section is active to the current preview. 
  141. */ 
  142. final public function active() { 
  143. $section = $this; 
  144. $active = call_user_func( $this->active_callback, $this ); 
  145.  
  146. /** 
  147. * Filters response of WP_Customize_Section::active(). 
  148. * @since 4.1.0 
  149. * @param bool $active Whether the Customizer section is active. 
  150. * @param WP_Customize_Section $section WP_Customize_Section instance. 
  151. */ 
  152. $active = apply_filters( 'customize_section_active', $active, $section ); 
  153.  
  154. return $active; 
  155.  
  156. /** 
  157. * Default callback used when invoking WP_Customize_Section::active(). 
  158. * Subclasses can override this with their specific logic, or they may provide 
  159. * an 'active_callback' argument to the constructor. 
  160. * @since 4.1.0 
  161. * @access public 
  162. * @return true 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', 'panel', '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.  
  179. if ( $this->panel ) { 
  180. /** translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ 
  181. $array['customizeAction'] = sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) ); 
  182. } else { 
  183. $array['customizeAction'] = __( 'Customizing' ); 
  184.  
  185. return $array; 
  186.  
  187. /** 
  188. * Checks required user capabilities and whether the theme has the 
  189. * feature support required by the section. 
  190. * @since 3.4.0 
  191. * @return bool False if theme doesn't support the section or user doesn't have the capability. 
  192. */ 
  193. final public function check_capabilities() { 
  194. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) { 
  195. return false; 
  196.  
  197. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) { 
  198. return false; 
  199.  
  200. return true; 
  201.  
  202. /** 
  203. * Get the section's content for insertion into the Customizer pane. 
  204. * @since 4.1.0 
  205. * @return string Contents of the section. 
  206. */ 
  207. final public function get_content() { 
  208. ob_start(); 
  209. $this->maybe_render(); 
  210. return trim( ob_get_clean() ); 
  211.  
  212. /** 
  213. * Check capabilities and render the section. 
  214. * @since 3.4.0 
  215. */ 
  216. final public function maybe_render() { 
  217. if ( ! $this->check_capabilities() ) { 
  218. return; 
  219.  
  220. /** 
  221. * Fires before rendering a Customizer section. 
  222. * @since 3.4.0 
  223. * @param WP_Customize_Section $this WP_Customize_Section instance. 
  224. */ 
  225. do_action( 'customize_render_section', $this ); 
  226. /** 
  227. * Fires before rendering a specific Customizer section. 
  228. * The dynamic portion of the hook name, `$this->id`, refers to the ID 
  229. * of the specific Customizer section to be rendered. 
  230. * @since 3.4.0 
  231. */ 
  232. do_action( "customize_render_section_{$this->id}" ); 
  233.  
  234. $this->render(); 
  235.  
  236. /** 
  237. * Render the section UI in a subclass. 
  238. * Sections are now rendered in JS by default, see WP_Customize_Section::print_template(). 
  239. * @since 3.4.0 
  240. */ 
  241. protected function render() {} 
  242.  
  243. /** 
  244. * Render the section's JS template. 
  245. * This function is only run for section types that have been registered with 
  246. * WP_Customize_Manager::register_section_type(). 
  247. * @since 4.3.0 
  248. * @access public 
  249. * @see WP_Customize_Manager::render_template() 
  250. */ 
  251. public function print_template() { 
  252. ?> 
  253. <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>"> 
  254. <?php $this->render_template(); ?> 
  255. </script> 
  256. <?php 
  257.  
  258. /** 
  259. * An Underscore (JS) template for rendering this section. 
  260. * Class variables for this section class are available in the `data` JS object; 
  261. * export custom variables by overriding WP_Customize_Section::json(). 
  262. * @since 4.3.0 
  263. * @access protected 
  264. * @see WP_Customize_Section::print_template() 
  265. */ 
  266. protected function render_template() { 
  267. ?> 
  268. <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}"> 
  269. <h3 class="accordion-section-title" tabindex="0"> 
  270. {{ data.title }} 
  271. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span> 
  272. </h3> 
  273. <ul class="accordion-section-content"> 
  274. <li class="customize-section-description-container"> 
  275. <div class="customize-section-title"> 
  276. <button class="customize-section-back" tabindex="-1"> 
  277. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span> 
  278. </button> 
  279. <h3> 
  280. <span class="customize-action"> 
  281. {{{ data.customizeAction }}} 
  282. </span> 
  283. {{ data.title }} 
  284. </h3> 
  285. </div> 
  286. <# if ( data.description ) { #> 
  287. <div class="description customize-section-description"> 
  288. {{{ data.description }}} 
  289. </div> 
  290. <# } #> 
  291. </li> 
  292. </ul> 
  293. </li> 
  294. <?php