PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-customize-section.php

https://github.com/markjaquith/WordPress
PHP | 402 lines | 125 code | 40 blank | 237 comment | 9 complexity | e6a637069b04f3493414eb7086e53ff2 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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 int
  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 features required to support the section.
  72. *
  73. * @since 3.4.0
  74. * @var string|string[]
  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 A specific ID of the section.
  137. * @param array $args {
  138. * Optional. Array of properties for the new Section object. Default empty array.
  139. *
  140. * @type int $priority Priority of the section, defining the display order
  141. * of panels and sections. Default 160.
  142. * @type string $panel The panel this section belongs to (if any).
  143. * Default empty.
  144. * @type string $capability Capability required for the section.
  145. * Default 'edit_theme_options'
  146. * @type string|string[] $theme_supports Theme features required to support the section.
  147. * @type string $title Title of the section to show in UI.
  148. * @type string $description Description to show in the UI.
  149. * @type string $type Type of the section.
  150. * @type callable $active_callback Active callback.
  151. * @type bool $description_hidden Hide the description behind a help icon,
  152. * instead of inline above the first control.
  153. * Default false.
  154. * }
  155. */
  156. public function __construct( $manager, $id, $args = array() ) {
  157. $keys = array_keys( get_object_vars( $this ) );
  158. foreach ( $keys as $key ) {
  159. if ( isset( $args[ $key ] ) ) {
  160. $this->$key = $args[ $key ];
  161. }
  162. }
  163. $this->manager = $manager;
  164. $this->id = $id;
  165. if ( empty( $this->active_callback ) ) {
  166. $this->active_callback = array( $this, 'active_callback' );
  167. }
  168. self::$instance_count += 1;
  169. $this->instance_number = self::$instance_count;
  170. $this->controls = array(); // Users cannot customize the $controls array.
  171. }
  172. /**
  173. * Check whether section is active to current Customizer preview.
  174. *
  175. * @since 4.1.0
  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. *
  201. * @return true Always true.
  202. */
  203. public function active_callback() {
  204. return true;
  205. }
  206. /**
  207. * Gather the parameters passed to client JavaScript via JSON.
  208. *
  209. * @since 4.1.0
  210. *
  211. * @return array The array to be exported to the client as JSON.
  212. */
  213. public function json() {
  214. $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type', 'description_hidden' ) );
  215. $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
  216. $array['content'] = $this->get_content();
  217. $array['active'] = $this->active();
  218. $array['instanceNumber'] = $this->instance_number;
  219. if ( $this->panel ) {
  220. /* translators: &#9656; is the unicode right-pointing triangle. %s: Section title in the Customizer. */
  221. $array['customizeAction'] = sprintf( __( 'Customizing &#9656; %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) );
  222. } else {
  223. $array['customizeAction'] = __( 'Customizing' );
  224. }
  225. return $array;
  226. }
  227. /**
  228. * Checks required user capabilities and whether the theme has the
  229. * feature support required by the section.
  230. *
  231. * @since 3.4.0
  232. *
  233. * @return bool False if theme doesn't support the section or user doesn't have the capability.
  234. */
  235. final public function check_capabilities() {
  236. if ( $this->capability && ! current_user_can( $this->capability ) ) {
  237. return false;
  238. }
  239. if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
  240. return false;
  241. }
  242. return true;
  243. }
  244. /**
  245. * Get the section's content for insertion into the Customizer pane.
  246. *
  247. * @since 4.1.0
  248. *
  249. * @return string Contents of the section.
  250. */
  251. final public function get_content() {
  252. ob_start();
  253. $this->maybe_render();
  254. return trim( ob_get_clean() );
  255. }
  256. /**
  257. * Check capabilities and render the section.
  258. *
  259. * @since 3.4.0
  260. */
  261. final public function maybe_render() {
  262. if ( ! $this->check_capabilities() ) {
  263. return;
  264. }
  265. /**
  266. * Fires before rendering a Customizer section.
  267. *
  268. * @since 3.4.0
  269. *
  270. * @param WP_Customize_Section $section WP_Customize_Section instance.
  271. */
  272. do_action( 'customize_render_section', $this );
  273. /**
  274. * Fires before rendering a specific Customizer section.
  275. *
  276. * The dynamic portion of the hook name, `$this->id`, refers to the ID
  277. * of the specific Customizer section to be rendered.
  278. *
  279. * @since 3.4.0
  280. */
  281. do_action( "customize_render_section_{$this->id}" );
  282. $this->render();
  283. }
  284. /**
  285. * Render the section UI in a subclass.
  286. *
  287. * Sections are now rendered in JS by default, see WP_Customize_Section::print_template().
  288. *
  289. * @since 3.4.0
  290. */
  291. protected function render() {}
  292. /**
  293. * Render the section's JS template.
  294. *
  295. * This function is only run for section types that have been registered with
  296. * WP_Customize_Manager::register_section_type().
  297. *
  298. * @since 4.3.0
  299. *
  300. * @see WP_Customize_Manager::render_template()
  301. */
  302. public function print_template() {
  303. ?>
  304. <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>">
  305. <?php $this->render_template(); ?>
  306. </script>
  307. <?php
  308. }
  309. /**
  310. * An Underscore (JS) template for rendering this section.
  311. *
  312. * Class variables for this section class are available in the `data` JS object;
  313. * export custom variables by overriding WP_Customize_Section::json().
  314. *
  315. * @since 4.3.0
  316. *
  317. * @see WP_Customize_Section::print_template()
  318. */
  319. protected function render_template() {
  320. ?>
  321. <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
  322. <h3 class="accordion-section-title" tabindex="0">
  323. {{ data.title }}
  324. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span>
  325. </h3>
  326. <ul class="accordion-section-content">
  327. <li class="customize-section-description-container section-meta <# if ( data.description_hidden ) { #>customize-info<# } #>">
  328. <div class="customize-section-title">
  329. <button class="customize-section-back" tabindex="-1">
  330. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
  331. </button>
  332. <h3>
  333. <span class="customize-action">
  334. {{{ data.customizeAction }}}
  335. </span>
  336. {{ data.title }}
  337. </h3>
  338. <# if ( data.description && data.description_hidden ) { #>
  339. <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
  340. <div class="description customize-section-description">
  341. {{{ data.description }}}
  342. </div>
  343. <# } #>
  344. <div class="customize-control-notifications-container"></div>
  345. </div>
  346. <# if ( data.description && ! data.description_hidden ) { #>
  347. <div class="description customize-section-description">
  348. {{{ data.description }}}
  349. </div>
  350. <# } #>
  351. </li>
  352. </ul>
  353. </li>
  354. <?php
  355. }
  356. }
  357. /** WP_Customize_Themes_Section class */
  358. require_once ABSPATH . WPINC . '/customize/class-wp-customize-themes-section.php';
  359. /** WP_Customize_Sidebar_Section class */
  360. require_once ABSPATH . WPINC . '/customize/class-wp-customize-sidebar-section.php';
  361. /** WP_Customize_Nav_Menu_Section class */
  362. require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menu-section.php';