class-wp-customize-section.php 11 KB

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