PageRenderTime 45ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/markjaquith/WordPress
PHP | 395 lines | 119 code | 37 blank | 239 comment | 7 complexity | 4acf8a88ef2fe2b140e1935b26f8bb11 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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. * @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 4.0.0
  39. * @var WP_Customize_Manager
  40. */
  41. public $manager;
  42. /**
  43. * Unique identifier.
  44. *
  45. * @since 4.0.0
  46. * @var string
  47. */
  48. public $id;
  49. /**
  50. * Priority of the panel, defining the display order of panels and sections.
  51. *
  52. * @since 4.0.0
  53. * @var int
  54. */
  55. public $priority = 160;
  56. /**
  57. * Capability required for the panel.
  58. *
  59. * @since 4.0.0
  60. * @var string
  61. */
  62. public $capability = 'edit_theme_options';
  63. /**
  64. * Theme features required to support the panel.
  65. *
  66. * @since 4.0.0
  67. * @var string|string[]
  68. */
  69. public $theme_supports = '';
  70. /**
  71. * Title of the panel to show in UI.
  72. *
  73. * @since 4.0.0
  74. * @var string
  75. */
  76. public $title = '';
  77. /**
  78. * Description to show in the UI.
  79. *
  80. * @since 4.0.0
  81. * @var string
  82. */
  83. public $description = '';
  84. /**
  85. * Auto-expand a section in a panel when the panel is expanded when the panel only has the one section.
  86. *
  87. * @since 4.7.4
  88. * @var bool
  89. */
  90. public $auto_expand_sole_section = false;
  91. /**
  92. * Customizer sections for this panel.
  93. *
  94. * @since 4.0.0
  95. * @var array
  96. */
  97. public $sections;
  98. /**
  99. * Type of this panel.
  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. * Constructor.
  120. *
  121. * Any supplied $args override class property defaults.
  122. *
  123. * @since 4.0.0
  124. *
  125. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  126. * @param string $id A specific ID for the panel.
  127. * @param array $args {
  128. * Optional. Array of properties for the new Panel object. Default empty array.
  129. *
  130. * @type int $priority Priority of the panel, defining the display order
  131. * of panels and sections. Default 160.
  132. * @type string $capability Capability required for the panel.
  133. * Default `edit_theme_options`.
  134. * @type string|string[] $theme_supports Theme features required to support the panel.
  135. * @type string $title Title of the panel to show in UI.
  136. * @type string $description Description to show in the UI.
  137. * @type string $type Type of the panel.
  138. * @type callable $active_callback Active callback.
  139. * }
  140. */
  141. public function __construct( $manager, $id, $args = array() ) {
  142. $keys = array_keys( get_object_vars( $this ) );
  143. foreach ( $keys as $key ) {
  144. if ( isset( $args[ $key ] ) ) {
  145. $this->$key = $args[ $key ];
  146. }
  147. }
  148. $this->manager = $manager;
  149. $this->id = $id;
  150. if ( empty( $this->active_callback ) ) {
  151. $this->active_callback = array( $this, 'active_callback' );
  152. }
  153. self::$instance_count += 1;
  154. $this->instance_number = self::$instance_count;
  155. $this->sections = array(); // Users cannot customize the $sections array.
  156. }
  157. /**
  158. * Check whether panel is active to current Customizer preview.
  159. *
  160. * @since 4.1.0
  161. *
  162. * @return bool Whether the panel is active to the current preview.
  163. */
  164. final public function active() {
  165. $panel = $this;
  166. $active = call_user_func( $this->active_callback, $this );
  167. /**
  168. * Filters response of WP_Customize_Panel::active().
  169. *
  170. * @since 4.1.0
  171. *
  172. * @param bool $active Whether the Customizer panel is active.
  173. * @param WP_Customize_Panel $panel WP_Customize_Panel instance.
  174. */
  175. $active = apply_filters( 'customize_panel_active', $active, $panel );
  176. return $active;
  177. }
  178. /**
  179. * Default callback used when invoking WP_Customize_Panel::active().
  180. *
  181. * Subclasses can override this with their specific logic, or they may
  182. * provide an 'active_callback' argument to the constructor.
  183. *
  184. * @since 4.1.0
  185. *
  186. * @return bool Always true.
  187. */
  188. public function active_callback() {
  189. return true;
  190. }
  191. /**
  192. * Gather the parameters passed to client JavaScript via JSON.
  193. *
  194. * @since 4.1.0
  195. *
  196. * @return array The array to be exported to the client as JSON.
  197. */
  198. public function json() {
  199. $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) );
  200. $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
  201. $array['content'] = $this->get_content();
  202. $array['active'] = $this->active();
  203. $array['instanceNumber'] = $this->instance_number;
  204. $array['autoExpandSoleSection'] = $this->auto_expand_sole_section;
  205. return $array;
  206. }
  207. /**
  208. * Checks required user capabilities and whether the theme has the
  209. * feature support required by the panel.
  210. *
  211. * @since 4.0.0
  212. * @since 5.9.0 Method was marked non-final.
  213. *
  214. * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
  215. */
  216. public function check_capabilities() {
  217. if ( $this->capability && ! current_user_can( $this->capability ) ) {
  218. return false;
  219. }
  220. if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
  221. return false;
  222. }
  223. return true;
  224. }
  225. /**
  226. * Get the panel's content template for insertion into the Customizer pane.
  227. *
  228. * @since 4.1.0
  229. *
  230. * @return string Content for the panel.
  231. */
  232. final public function get_content() {
  233. ob_start();
  234. $this->maybe_render();
  235. return trim( ob_get_clean() );
  236. }
  237. /**
  238. * Check capabilities and render the panel.
  239. *
  240. * @since 4.0.0
  241. */
  242. final public function maybe_render() {
  243. if ( ! $this->check_capabilities() ) {
  244. return;
  245. }
  246. /**
  247. * Fires before rendering a Customizer panel.
  248. *
  249. * @since 4.0.0
  250. *
  251. * @param WP_Customize_Panel $panel WP_Customize_Panel instance.
  252. */
  253. do_action( 'customize_render_panel', $this );
  254. /**
  255. * Fires before rendering a specific Customizer panel.
  256. *
  257. * The dynamic portion of the hook name, `$this->id`, refers to
  258. * the ID of the specific Customizer panel to be rendered.
  259. *
  260. * @since 4.0.0
  261. */
  262. do_action( "customize_render_panel_{$this->id}" );
  263. $this->render();
  264. }
  265. /**
  266. * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
  267. *
  268. * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template().
  269. *
  270. * @since 4.0.0
  271. */
  272. protected function render() {}
  273. /**
  274. * Render the panel UI in a subclass.
  275. *
  276. * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template().
  277. *
  278. * @since 4.1.0
  279. */
  280. protected function render_content() {}
  281. /**
  282. * Render the panel's JS templates.
  283. *
  284. * This function is only run for panel types that have been registered with
  285. * WP_Customize_Manager::register_panel_type().
  286. *
  287. * @since 4.3.0
  288. *
  289. * @see WP_Customize_Manager::register_panel_type()
  290. */
  291. public function print_template() {
  292. ?>
  293. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
  294. <?php $this->content_template(); ?>
  295. </script>
  296. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
  297. <?php $this->render_template(); ?>
  298. </script>
  299. <?php
  300. }
  301. /**
  302. * An Underscore (JS) template for rendering this panel's container.
  303. *
  304. * Class variables for this panel class are available in the `data` JS object;
  305. * export custom variables by overriding WP_Customize_Panel::json().
  306. *
  307. * @see WP_Customize_Panel::print_template()
  308. *
  309. * @since 4.3.0
  310. */
  311. protected function render_template() {
  312. ?>
  313. <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ 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 panel' ); ?></span>
  317. </h3>
  318. <ul class="accordion-sub-container control-panel-content"></ul>
  319. </li>
  320. <?php
  321. }
  322. /**
  323. * An Underscore (JS) template for this panel's content (but not its container).
  324. *
  325. * Class variables for this panel class are available in the `data` JS object;
  326. * export custom variables by overriding WP_Customize_Panel::json().
  327. *
  328. * @see WP_Customize_Panel::print_template()
  329. *
  330. * @since 4.3.0
  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">
  338. <?php
  339. /* translators: %s: The site/panel title in the Customizer. */
  340. printf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
  341. ?>
  342. </span>
  343. <# if ( data.description ) { #>
  344. <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
  345. <# } #>
  346. </div>
  347. <# if ( data.description ) { #>
  348. <div class="description customize-panel-description">
  349. {{{ data.description }}}
  350. </div>
  351. <# } #>
  352. <div class="customize-control-notifications-container"></div>
  353. </li>
  354. <?php
  355. }
  356. }
  357. /** WP_Customize_Nav_Menus_Panel class */
  358. require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php';