class-wp-customize-panel.php 10 KB

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