PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/em645jn/brochure
PHP | 387 lines | 118 code | 38 blank | 231 comment | 9 complexity | cb960ae779c033cf96466c61f7a395a8 MD5 | raw file
  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. * Constructor.
  135. *
  136. * Any supplied $args override class property defaults.
  137. *
  138. * @since 3.4.0
  139. *
  140. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  141. * @param string $id An specific ID of the section.
  142. * @param array $args Section arguments.
  143. */
  144. public function __construct( $manager, $id, $args = array() ) {
  145. $keys = array_keys( get_object_vars( $this ) );
  146. foreach ( $keys as $key ) {
  147. if ( isset( $args[ $key ] ) ) {
  148. $this->$key = $args[ $key ];
  149. }
  150. }
  151. $this->manager = $manager;
  152. $this->id = $id;
  153. if ( empty( $this->active_callback ) ) {
  154. $this->active_callback = array( $this, 'active_callback' );
  155. }
  156. self::$instance_count += 1;
  157. $this->instance_number = self::$instance_count;
  158. $this->controls = array(); // Users cannot customize the $controls array.
  159. }
  160. /**
  161. * Check whether section is active to current Customizer preview.
  162. *
  163. * @since 4.1.0
  164. * @access public
  165. *
  166. * @return bool Whether the section is active to the current preview.
  167. */
  168. final public function active() {
  169. $section = $this;
  170. $active = call_user_func( $this->active_callback, $this );
  171. /**
  172. * Filters response of WP_Customize_Section::active().
  173. *
  174. * @since 4.1.0
  175. *
  176. * @param bool $active Whether the Customizer section is active.
  177. * @param WP_Customize_Section $section WP_Customize_Section instance.
  178. */
  179. $active = apply_filters( 'customize_section_active', $active, $section );
  180. return $active;
  181. }
  182. /**
  183. * Default callback used when invoking WP_Customize_Section::active().
  184. *
  185. * Subclasses can override this with their specific logic, or they may provide
  186. * an 'active_callback' argument to the constructor.
  187. *
  188. * @since 4.1.0
  189. * @access public
  190. *
  191. * @return true Always true.
  192. */
  193. public function active_callback() {
  194. return true;
  195. }
  196. /**
  197. * Gather the parameters passed to client JavaScript via JSON.
  198. *
  199. * @since 4.1.0
  200. *
  201. * @return array The array to be exported to the client as JSON.
  202. */
  203. public function json() {
  204. $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'panel', 'type' ) );
  205. $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
  206. $array['content'] = $this->get_content();
  207. $array['active'] = $this->active();
  208. $array['instanceNumber'] = $this->instance_number;
  209. if ( $this->panel ) {
  210. /* translators: &#9656; is the unicode right-pointing triangle, and %s is the section title in the Customizer */
  211. $array['customizeAction'] = sprintf( __( 'Customizing &#9656; %s' ), esc_html( $this->manager->get_panel( $this->panel )->title ) );
  212. } else {
  213. $array['customizeAction'] = __( 'Customizing' );
  214. }
  215. return $array;
  216. }
  217. /**
  218. * Checks required user capabilities and whether the theme has the
  219. * feature support required by the section.
  220. *
  221. * @since 3.4.0
  222. *
  223. * @return bool False if theme doesn't support the section or user doesn't have the capability.
  224. */
  225. final public function check_capabilities() {
  226. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
  227. return false;
  228. }
  229. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
  230. return false;
  231. }
  232. return true;
  233. }
  234. /**
  235. * Get the section's content for insertion into the Customizer pane.
  236. *
  237. * @since 4.1.0
  238. *
  239. * @return string Contents of the section.
  240. */
  241. final public function get_content() {
  242. ob_start();
  243. $this->maybe_render();
  244. return trim( ob_get_clean() );
  245. }
  246. /**
  247. * Check capabilities and render the section.
  248. *
  249. * @since 3.4.0
  250. */
  251. final public function maybe_render() {
  252. if ( ! $this->check_capabilities() ) {
  253. return;
  254. }
  255. /**
  256. * Fires before rendering a Customizer section.
  257. *
  258. * @since 3.4.0
  259. *
  260. * @param WP_Customize_Section $this WP_Customize_Section instance.
  261. */
  262. do_action( 'customize_render_section', $this );
  263. /**
  264. * Fires before rendering a specific Customizer section.
  265. *
  266. * The dynamic portion of the hook name, `$this->id`, refers to the ID
  267. * of the specific Customizer section to be rendered.
  268. *
  269. * @since 3.4.0
  270. */
  271. do_action( "customize_render_section_{$this->id}" );
  272. $this->render();
  273. }
  274. /**
  275. * Render the section UI in a subclass.
  276. *
  277. * Sections are now rendered in JS by default, see WP_Customize_Section::print_template().
  278. *
  279. * @since 3.4.0
  280. */
  281. protected function render() {}
  282. /**
  283. * Render the section's JS template.
  284. *
  285. * This function is only run for section types that have been registered with
  286. * WP_Customize_Manager::register_section_type().
  287. *
  288. * @since 4.3.0
  289. * @access public
  290. *
  291. * @see WP_Customize_Manager::render_template()
  292. */
  293. public function print_template() {
  294. ?>
  295. <script type="text/html" id="tmpl-customize-section-<?php echo $this->type; ?>">
  296. <?php $this->render_template(); ?>
  297. </script>
  298. <?php
  299. }
  300. /**
  301. * An Underscore (JS) template for rendering this section.
  302. *
  303. * Class variables for this section class are available in the `data` JS object;
  304. * export custom variables by overriding WP_Customize_Section::json().
  305. *
  306. * @since 4.3.0
  307. * @access protected
  308. *
  309. * @see WP_Customize_Section::print_template()
  310. */
  311. protected function render_template() {
  312. ?>
  313. <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }}">
  314. <h3 class="accordion-section-title" tabindex="0">
  315. {{ data.title }}
  316. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this section' ); ?></span>
  317. </h3>
  318. <ul class="accordion-section-content">
  319. <li class="customize-section-description-container">
  320. <div class="customize-section-title">
  321. <button class="customize-section-back" tabindex="-1">
  322. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
  323. </button>
  324. <h3>
  325. <span class="customize-action">
  326. {{{ data.customizeAction }}}
  327. </span>
  328. {{ data.title }}
  329. </h3>
  330. </div>
  331. <# if ( data.description ) { #>
  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. /** WP_Customize_New_Menu_Section class */
  349. require_once( ABSPATH . WPINC . '/customize/class-wp-customize-new-menu-section.php' );