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

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

https://gitlab.com/geyson/geyson
PHP | 482 lines | 156 code | 40 blank | 286 comment | 7 complexity | 496347d72cb3707d87af65b2b9e1e29e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * WordPress Customize Panel classes
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 4.0.0
  8. */
  9. /**
  10. * Customize Panel class.
  11. *
  12. * A UI container for sections, managed by the WP_Customize_Manager.
  13. *
  14. * @since 4.0.0
  15. *
  16. * @see WP_Customize_Manager
  17. */
  18. class WP_Customize_Panel {
  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. * @static
  29. * @var int
  30. */
  31. protected static $instance_count = 0;
  32. /**
  33. * Order in which this instance was created in relation to other instances.
  34. *
  35. * @since 4.1.0
  36. * @access public
  37. * @var int
  38. */
  39. public $instance_number;
  40. /**
  41. * WP_Customize_Manager instance.
  42. *
  43. * @since 4.0.0
  44. * @access public
  45. * @var WP_Customize_Manager
  46. */
  47. public $manager;
  48. /**
  49. * Unique identifier.
  50. *
  51. * @since 4.0.0
  52. * @access public
  53. * @var string
  54. */
  55. public $id;
  56. /**
  57. * Priority of the panel, defining the display order of panels and sections.
  58. *
  59. * @since 4.0.0
  60. * @access public
  61. * @var integer
  62. */
  63. public $priority = 160;
  64. /**
  65. * Capability required for the panel.
  66. *
  67. * @since 4.0.0
  68. * @access public
  69. * @var string
  70. */
  71. public $capability = 'edit_theme_options';
  72. /**
  73. * Theme feature support for the panel.
  74. *
  75. * @since 4.0.0
  76. * @access public
  77. * @var string|array
  78. */
  79. public $theme_supports = '';
  80. /**
  81. * Title of the panel to show in UI.
  82. *
  83. * @since 4.0.0
  84. * @access public
  85. * @var string
  86. */
  87. public $title = '';
  88. /**
  89. * Description to show in the UI.
  90. *
  91. * @since 4.0.0
  92. * @access public
  93. * @var string
  94. */
  95. public $description = '';
  96. /**
  97. * Customizer sections for this panel.
  98. *
  99. * @since 4.0.0
  100. * @access public
  101. * @var array
  102. */
  103. public $sections;
  104. /**
  105. * Type of this panel.
  106. *
  107. * @since 4.1.0
  108. * @access public
  109. * @var string
  110. */
  111. public $type = 'default';
  112. /**
  113. * Active callback.
  114. *
  115. * @since 4.1.0
  116. * @access public
  117. *
  118. * @see WP_Customize_Section::active()
  119. *
  120. * @var callable Callback is called with one argument, the instance of
  121. * {@see WP_Customize_Section}, and returns bool to indicate
  122. * whether the section is active (such as it relates to the URL
  123. * currently being previewed).
  124. */
  125. public $active_callback = '';
  126. /**
  127. * Constructor.
  128. *
  129. * Any supplied $args override class property defaults.
  130. *
  131. * @since 4.0.0
  132. *
  133. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  134. * @param string $id An specific ID for the panel.
  135. * @param array $args Panel arguments.
  136. */
  137. public function __construct( $manager, $id, $args = array() ) {
  138. $keys = array_keys( get_object_vars( $this ) );
  139. foreach ( $keys as $key ) {
  140. if ( isset( $args[ $key ] ) ) {
  141. $this->$key = $args[ $key ];
  142. }
  143. }
  144. $this->manager = $manager;
  145. $this->id = $id;
  146. if ( empty( $this->active_callback ) ) {
  147. $this->active_callback = array( $this, 'active_callback' );
  148. }
  149. self::$instance_count += 1;
  150. $this->instance_number = self::$instance_count;
  151. $this->sections = array(); // Users cannot customize the $sections array.
  152. }
  153. /**
  154. * Check whether panel is active to current Customizer preview.
  155. *
  156. * @since 4.1.0
  157. * @access public
  158. *
  159. * @return bool Whether the panel is active to the current preview.
  160. */
  161. final public function active() {
  162. $panel = $this;
  163. $active = call_user_func( $this->active_callback, $this );
  164. /**
  165. * Filter response of WP_Customize_Panel::active().
  166. *
  167. * @since 4.1.0
  168. *
  169. * @param bool $active Whether the Customizer panel is active.
  170. * @param WP_Customize_Panel $panel {@see WP_Customize_Panel} instance.
  171. */
  172. $active = apply_filters( 'customize_panel_active', $active, $panel );
  173. return $active;
  174. }
  175. /**
  176. * Default callback used when invoking {@see WP_Customize_Panel::active()}.
  177. *
  178. * Subclasses can override this with their specific logic, or they may
  179. * provide an 'active_callback' argument to the constructor.
  180. *
  181. * @since 4.1.0
  182. * @access public
  183. *
  184. * @return bool Always true.
  185. */
  186. public function active_callback() {
  187. return true;
  188. }
  189. /**
  190. * Gather the parameters passed to client JavaScript via JSON.
  191. *
  192. * @since 4.1.0
  193. *
  194. * @return array The array to be exported to the client as JSON.
  195. */
  196. public function json() {
  197. $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) );
  198. $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
  199. $array['content'] = $this->get_content();
  200. $array['active'] = $this->active();
  201. $array['instanceNumber'] = $this->instance_number;
  202. return $array;
  203. }
  204. /**
  205. * Checks required user capabilities and whether the theme has the
  206. * feature support required by the panel.
  207. *
  208. * @since 4.0.0
  209. *
  210. * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
  211. */
  212. final public function check_capabilities() {
  213. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
  214. return false;
  215. }
  216. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
  217. return false;
  218. }
  219. return true;
  220. }
  221. /**
  222. * Get the panel's content template for insertion into the Customizer pane.
  223. *
  224. * @since 4.1.0
  225. *
  226. * @return string Content for the panel.
  227. */
  228. final public function get_content() {
  229. ob_start();
  230. $this->maybe_render();
  231. return trim( ob_get_clean() );
  232. }
  233. /**
  234. * Check capabilities and render the panel.
  235. *
  236. * @since 4.0.0
  237. */
  238. final public function maybe_render() {
  239. if ( ! $this->check_capabilities() ) {
  240. return;
  241. }
  242. /**
  243. * Fires before rendering a Customizer panel.
  244. *
  245. * @since 4.0.0
  246. *
  247. * @param WP_Customize_Panel $this WP_Customize_Panel instance.
  248. */
  249. do_action( 'customize_render_panel', $this );
  250. /**
  251. * Fires before rendering a specific Customizer panel.
  252. *
  253. * The dynamic portion of the hook name, `$this->id`, refers to
  254. * the ID of the specific Customizer panel to be rendered.
  255. *
  256. * @since 4.0.0
  257. */
  258. do_action( "customize_render_panel_{$this->id}" );
  259. $this->render();
  260. }
  261. /**
  262. * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
  263. *
  264. * Panel containers are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}.
  265. *
  266. * @since 4.0.0
  267. * @access protected
  268. */
  269. protected function render() {}
  270. /**
  271. * Render the panel UI in a subclass.
  272. *
  273. * Panel contents are now rendered in JS by default, see {@see WP_Customize_Panel::print_template()}.
  274. *
  275. * @since 4.1.0
  276. * @access protected
  277. */
  278. protected function render_content() {}
  279. /**
  280. * Render the panel's JS templates.
  281. *
  282. * This function is only run for panel types that have been registered with
  283. * WP_Customize_Manager::register_panel_type().
  284. *
  285. * @since 4.3.0
  286. *
  287. * @see WP_Customize_Manager::register_panel_type()
  288. */
  289. public function print_template() {
  290. ?>
  291. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
  292. <?php $this->content_template(); ?>
  293. </script>
  294. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
  295. <?php $this->render_template(); ?>
  296. </script>
  297. <?php
  298. }
  299. /**
  300. * An Underscore (JS) template for rendering this panel's container.
  301. *
  302. * Class variables for this panel class are available in the `data` JS object;
  303. * export custom variables by overriding WP_Customize_Panel::json().
  304. *
  305. * @see WP_Customize_Panel::print_template()
  306. *
  307. * @since 4.3.0
  308. * @access protected
  309. */
  310. protected function render_template() {
  311. ?>
  312. <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
  313. <h3 class="accordion-section-title" tabindex="0">
  314. {{ data.title }}
  315. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
  316. </h3>
  317. <ul class="accordion-sub-container control-panel-content"></ul>
  318. </li>
  319. <?php
  320. }
  321. /**
  322. * An Underscore (JS) template for this panel's content (but not its container).
  323. *
  324. * Class variables for this panel class are available in the `data` JS object;
  325. * export custom variables by overriding WP_Customize_Panel::json().
  326. *
  327. * @see WP_Customize_Panel::print_template()
  328. *
  329. * @since 4.3.0
  330. * @access protected
  331. */
  332. protected function content_template() {
  333. ?>
  334. <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
  335. <button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button>
  336. <div class="accordion-section-title">
  337. <span class="preview-notice"><?php
  338. /* translators: %s is the site/panel title in the Customizer */
  339. echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
  340. ?></span>
  341. <button class="customize-help-toggle dashicons dashicons-editor-help" tabindex="0" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
  342. </div>
  343. <# if ( data.description ) { #>
  344. <div class="description customize-panel-description">
  345. {{{ data.description }}}
  346. </div>
  347. <# } #>
  348. </li>
  349. <?php
  350. }
  351. }
  352. /**
  353. * Customize Nav Menus Panel Class
  354. *
  355. * Needed to add screen options.
  356. *
  357. * @since 4.3.0
  358. *
  359. * @see WP_Customize_Panel
  360. */
  361. class WP_Customize_Nav_Menus_Panel extends WP_Customize_Panel {
  362. /**
  363. * Control type.
  364. *
  365. * @since 4.3.0
  366. * @access public
  367. * @var string
  368. */
  369. public $type = 'nav_menus';
  370. /**
  371. * Render screen options for Menus.
  372. *
  373. * @since 4.3.0
  374. * @access public
  375. */
  376. public function render_screen_options() {
  377. // Essentially adds the screen options.
  378. add_filter( 'manage_nav-menus_columns', array( $this, 'wp_nav_menu_manage_columns' ) );
  379. // Display screen options.
  380. $screen = WP_Screen::get( 'nav-menus.php' );
  381. $screen->render_screen_options();
  382. }
  383. /**
  384. * Returns the advanced options for the nav menus page.
  385. *
  386. * Link title attribute added as it's a relatively advanced concept for new users.
  387. *
  388. * @since 4.3.0
  389. * @access public
  390. *
  391. * @return array The advanced menu properties.
  392. */
  393. public function wp_nav_menu_manage_columns() {
  394. return array(
  395. '_title' => __( 'Show advanced menu properties' ),
  396. 'cb' => '<input type="checkbox" />',
  397. 'link-target' => __( 'Link Target' ),
  398. 'attr-title' => __( 'Title Attribute' ),
  399. 'css-classes' => __( 'CSS Classes' ),
  400. 'xfn' => __( 'Link Relationship (XFN)' ),
  401. 'description' => __( 'Description' ),
  402. );
  403. }
  404. /**
  405. * An Underscore (JS) template for this panel's content (but not its container).
  406. *
  407. * Class variables for this panel class are available in the `data` JS object;
  408. * export custom variables by overriding WP_Customize_Panel::json().
  409. *
  410. * @since 4.3.0
  411. * @access protected
  412. *
  413. * @see WP_Customize_Panel::print_template()
  414. */
  415. protected function content_template() {
  416. ?>
  417. <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
  418. <button type="button" class="customize-panel-back" tabindex="-1">
  419. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
  420. </button>
  421. <div class="accordion-section-title">
  422. <span class="preview-notice">
  423. <?php
  424. /* Translators: %s is the site/panel title in the Customizer. */
  425. printf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
  426. ?>
  427. </span>
  428. <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false">
  429. <span class="screen-reader-text"><?php _e( 'Help' ); ?></span>
  430. </button>
  431. <button type="button" class="customize-screen-options-toggle" aria-expanded="false">
  432. <span class="screen-reader-text"><?php _e( 'Menu Options' ); ?></span>
  433. </button>
  434. </div>
  435. <# if ( data.description ) { #>
  436. <div class="description customize-panel-description">{{{ data.description }}}</div>
  437. <# } #>
  438. <?php $this->render_screen_options(); ?>
  439. </li>
  440. <?php
  441. }
  442. }