class-wp-customize-section.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * @access protected
  28. * @var int
  29. */
  30. protected static $instance_count = 0;
  31. /**
  32. * Order in which this instance was created in relation to other instances.
  33. *
  34. * @since 4.1.0
  35. * @access public
  36. * @var int
  37. */
  38. public $instance_number;
  39. /**
  40. * WP_Customize_Manager instance.
  41. *
  42. * @since 3.4.0
  43. * @access public
  44. * @var WP_Customize_Manager
  45. */
  46. public $manager;
  47. /**
  48. * Unique identifier.
  49. *
  50. * @since 3.4.0
  51. * @access public
  52. * @var string
  53. */
  54. public $id;
  55. /**
  56. * Priority of the section which informs load order of sections.
  57. *
  58. * @since 3.4.0
  59. * @access public
  60. * @var integer
  61. */
  62. public $priority = 160;
  63. /**
  64. * Panel in which to show the section, making it a sub-section.
  65. *
  66. * @since 4.0.0
  67. * @access public
  68. * @var string
  69. */
  70. public $panel = '';
  71. /**
  72. * Capability required for the section.
  73. *
  74. * @since 3.4.0
  75. * @access public
  76. * @var string
  77. */
  78. public $capability = 'edit_theme_options';
  79. /**
  80. * Theme feature support for the section.
  81. *
  82. * @since 3.4.0
  83. * @access public
  84. * @var string|array
  85. */
  86. public $theme_supports = '';
  87. /**
  88. * Title of the section to show in UI.
  89. *
  90. * @since 3.4.0
  91. * @access public
  92. * @var string
  93. */
  94. public $title = '';
  95. /**
  96. * Description to show in the UI.
  97. *
  98. * @since 3.4.0
  99. * @access public
  100. * @var string
  101. */
  102. public $description = '';
  103. /**
  104. * Customizer controls for this section.
  105. *
  106. * @since 3.4.0
  107. * @access public
  108. * @var array
  109. */
  110. public $controls;
  111. /**
  112. * Type of this section.
  113. *
  114. * @since 4.1.0
  115. * @access public
  116. * @var string
  117. */
  118. public $type = 'default';
  119. /**
  120. * Active callback.
  121. *
  122. * @since 4.1.0
  123. * @access public
  124. *
  125. * @see WP_Customize_Section::active()
  126. *
  127. * @var callable Callback is called with one argument, the instance of
  128. * WP_Customize_Section, and returns bool to indicate whether
  129. * the section is active (such as it relates to the URL currently
  130. * being previewed).
  131. */
  132. public $active_callback = '';
  133. /**
  134. * Show the description or hide it behind the help icon.
  135. *
  136. * @since 4.7.0
  137. * @access public
  138. *
  139. * @var bool Indicates whether the Section's description should be
  140. * hidden behind a help icon ("?") in the Section header,
  141. * similar to how help icons are displayed on Panels.
  142. */
  143. public $description_hidden = false;
  144. /**
  145. * Constructor.
  146. *
  147. * Any supplied $args override class property defaults.
  148. *
  149. * @since 3.4.0
  150. *
  151. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  152. * @param string $id An specific ID of the section.
  153. * @param array $args Section arguments.
  154. */
  155. public function __construct( $manager, $id, $args = array() ) {
  156. $keys = array_keys( get_object_vars( $this ) );
  157. foreach ( $keys as $key ) {
  158. if ( isset( $args[ $key ] ) ) {
  159. $this->$key = $args[ $key ];
  160. }
  161. }
  162. $this->manager = $manager;
  163. $this->id = $id;
  164. if ( empty( $this->active_callback ) ) {
  165. $this->active_callback = array( $this, 'active_callback' );
  166. }
  167. self::$instance_count += 1;
  168. $this->instance_number = self::$instance_count;
  169. $this->controls = array(); // Users cannot customize the $controls array.
  170. }
  171. /**
  172. * Check whether section is active to current Customizer preview.
  173. *
  174. * @since 4.1.0
  175. * @access public
  176. *
  177. * @return bool Whether the section is active to the current preview.
  178. */
  179. final public function active() {
  180. $section = $this;
  181. $active = call_user_func( $this->active_callback, $this );
  182. /**
  183. * Filters response of WP_Customize_Section::active().
  184. *
  185. * @since 4.1.0
  186. *
  187. * @param bool $active Whether the Customizer section is active.
  188. * @param WP_Customize_Section $section WP_Customize_Section instance.
  189. */
  190. $active = apply_filters( 'customize_section_active', $active, $section );
  191. return $active;
  192. }
  193. /**
  194. * Default callback used when invoking WP_Customize_Section::active().
  195. *
  196. * Subclasses can override this with their specific logic, or they may provide
  197. * an 'active_callback' argument to the constructor.
  198. *
  199. * @since 4.1.0
  200. * @access public
  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, and %s is the 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 && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
  238. return false;
  239. }
  240. if ( $this->theme_supports && ! call_user_func_array( '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 $this 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. * @access public
  301. *
  302. * @see WP_Customize_Manager::render_template()
  303. */
  304. public function print_template() {
  305. ?>
  306. <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>">
  307. <?php $this->render_template(); ?>
  308. </script>
  309. <?php
  310. }
  311. /**
  312. * An Underscore (JS) template for rendering this section.
  313. *
  314. * Class variables for this section class are available in the `data` JS object;
  315. * export custom variables by overriding WP_Customize_Section::json().
  316. *
  317. * @since 4.3.0
  318. * @access protected
  319. *
  320. * @see WP_Customize_Section::print_template()
  321. */
  322. protected function render_template() {
  323. ?>
  324. <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
  325. <h3 class="accordion-section-title" tabindex="0">
  326. {{ data.title }}
  327. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span>
  328. </h3>
  329. <ul class="accordion-section-content">
  330. <li class="customize-section-description-container section-meta <# if ( data.description_hidden ) { #>customize-info<# } #>">
  331. <div class="customize-section-title">
  332. <button class="customize-section-back" tabindex="-1">
  333. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
  334. </button>
  335. <h3>
  336. <span class="customize-action">
  337. {{{ data.customizeAction }}}
  338. </span>
  339. {{ data.title }}
  340. </h3>
  341. <# if ( data.description && data.description_hidden ) { #>
  342. <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
  343. <div class="description customize-section-description">
  344. {{{ data.description }}}
  345. </div>
  346. <# } #>
  347. </div>
  348. <# if ( data.description && ! data.description_hidden ) { #>
  349. <div class="description customize-section-description">
  350. {{{ data.description }}}
  351. </div>
  352. <# } #>
  353. </li>
  354. </ul>
  355. </li>
  356. <?php
  357. }
  358. }
  359. /** WP_Customize_Themes_Section class */
  360. require_once( ABSPATH . WPINC . '/customize/class-wp-customize-themes-section.php' );
  361. /** WP_Customize_Sidebar_Section class */
  362. require_once( ABSPATH . WPINC . '/customize/class-wp-customize-sidebar-section.php' );
  363. /** WP_Customize_Nav_Menu_Section class */
  364. require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-section.php' );
  365. /** WP_Customize_New_Menu_Section class */
  366. require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-section.php' );