PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/geyson/geyson
PHP | 577 lines | 195 code | 52 blank | 330 comment | 14 complexity | 951a5e89a07f3999d4d88396c19effa6 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  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. * {@see WP_Customize_Section}, and returns bool to indicate
  129. * whether the section is active (such as it relates to the URL
  130. * currently 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. * Filter response of {@see 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 {@see 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 {@see 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 {@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' ); ?></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. /**
  343. * Customize Themes Section class.
  344. *
  345. * A UI container for theme controls, which behaves like a backwards Panel.
  346. *
  347. * @since 4.2.0
  348. *
  349. * @see WP_Customize_Section
  350. */
  351. class WP_Customize_Themes_Section extends WP_Customize_Section {
  352. /**
  353. * Customize section type.
  354. *
  355. * @since 4.2.0
  356. * @access public
  357. * @var string
  358. */
  359. public $type = 'themes';
  360. /**
  361. * Render the themes section, which behaves like a panel.
  362. *
  363. * @since 4.2.0
  364. * @access protected
  365. */
  366. protected function render() {
  367. $classes = 'accordion-section control-section control-section-' . $this->type;
  368. ?>
  369. <li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
  370. <h3 class="accordion-section-title">
  371. <?php
  372. if ( $this->manager->is_theme_active() ) {
  373. echo '<span class="customize-action">' . __( 'Active theme' ) . '</span> ' . $this->title;
  374. } else {
  375. echo '<span class="customize-action">' . __( 'Previewing theme' ) . '</span> ' . $this->title;
  376. }
  377. ?>
  378. <button type="button" class="button change-theme" tabindex="0"><?php _ex( 'Change', 'theme' ); ?></button>
  379. </h3>
  380. <div class="customize-themes-panel control-panel-content themes-php">
  381. <h3 class="accordion-section-title customize-section-title">
  382. <span class="customize-action"><?php _e( 'Customizing' ); ?></span>
  383. <?php _e( 'Themes' ); ?>
  384. <span class="title-count theme-count"><?php echo count( $this->controls ) + 1 /* Active theme */; ?></span>
  385. </h3>
  386. <h3 class="accordion-section-title customize-section-title">
  387. <?php
  388. if ( $this->manager->is_theme_active() ) {
  389. echo '<span class="customize-action">' . __( 'Active theme' ) . '</span> ' . $this->title;
  390. } else {
  391. echo '<span class="customize-action">' . __( 'Previewing theme' ) . '</span> ' . $this->title;
  392. }
  393. ?>
  394. <button type="button" class="button customize-theme"><?php _e( 'Customize' ); ?></button>
  395. </h3>
  396. <div class="theme-overlay" tabindex="0" role="dialog" aria-label="<?php esc_attr_e( 'Theme Details' ); ?>"></div>
  397. <div id="customize-container"></div>
  398. <?php if ( count( $this->controls ) > 4 ) : ?>
  399. <p><label for="themes-filter">
  400. <span class="screen-reader-text"><?php _e( 'Search installed themes...' ); ?></span>
  401. <input type="text" id="themes-filter" placeholder="<?php esc_attr_e( 'Search installed themes...' ); ?>" />
  402. </label></p>
  403. <?php endif; ?>
  404. <div class="theme-browser rendered">
  405. <ul class="themes accordion-section-content">
  406. </ul>
  407. </div>
  408. </div>
  409. </li>
  410. <?php }
  411. }
  412. /**
  413. * Customizer section representing widget area (sidebar).
  414. *
  415. * @since 4.1.0
  416. *
  417. * @see WP_Customize_Section
  418. */
  419. class WP_Customize_Sidebar_Section extends WP_Customize_Section {
  420. /**
  421. * Type of this section.
  422. *
  423. * @since 4.1.0
  424. * @access public
  425. * @var string
  426. */
  427. public $type = 'sidebar';
  428. /**
  429. * Unique identifier.
  430. *
  431. * @since 4.1.0
  432. * @access public
  433. * @var string
  434. */
  435. public $sidebar_id;
  436. /**
  437. * Gather the parameters passed to client JavaScript via JSON.
  438. *
  439. * @since 4.1.0
  440. *
  441. * @return array The array to be exported to the client as JSON.
  442. */
  443. public function json() {
  444. $json = parent::json();
  445. $json['sidebarId'] = $this->sidebar_id;
  446. return $json;
  447. }
  448. /**
  449. * Whether the current sidebar is rendered on the page.
  450. *
  451. * @since 4.1.0
  452. * @access public
  453. *
  454. * @return bool Whether sidebar is rendered.
  455. */
  456. public function active_callback() {
  457. return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
  458. }
  459. }
  460. /**
  461. * Customize Menu Section Class
  462. *
  463. * Custom section only needed in JS.
  464. *
  465. * @since 4.3.0
  466. *
  467. * @see WP_Customize_Section
  468. */
  469. class WP_Customize_Nav_Menu_Section extends WP_Customize_Section {
  470. /**
  471. * Control type.
  472. *
  473. * @since 4.3.0
  474. * @access public
  475. * @var string
  476. */
  477. public $type = 'nav_menu';
  478. /**
  479. * Get section parameters for JS.
  480. *
  481. * @since 4.3.0
  482. * @access public
  483. * @return array Exported parameters.
  484. */
  485. public function json() {
  486. $exported = parent::json();
  487. $exported['menu_id'] = intval( preg_replace( '/^nav_menu\[(\d+)\]/', '$1', $this->id ) );
  488. return $exported;
  489. }
  490. }
  491. /**
  492. * Customize Menu Section Class
  493. *
  494. * Implements the new-menu-ui toggle button instead of a regular section.
  495. *
  496. * @since 4.3.0
  497. *
  498. * @see WP_Customize_Section
  499. */
  500. class WP_Customize_New_Menu_Section extends WP_Customize_Section {
  501. /**
  502. * Control type.
  503. *
  504. * @since 4.3.0
  505. * @access public
  506. * @var string
  507. */
  508. public $type = 'new_menu';
  509. /**
  510. * Render the section, and the controls that have been added to it.
  511. *
  512. * @since 4.3.0
  513. * @access protected
  514. */
  515. protected function render() {
  516. ?>
  517. <li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="accordion-section-new-menu">
  518. <button type="button" class="button-secondary add-new-menu-item add-menu-toggle" aria-expanded="false">
  519. <?php echo esc_html( $this->title ); ?>
  520. </button>
  521. <ul class="new-menu-section-content"></ul>
  522. </li>
  523. <?php
  524. }
  525. }