class-wp-customize-section.php 10.0 KB

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