class-wp-customize-section.php 9.9 KB

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