class-wp-customize-panel.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 integer
  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 feature support for the panel.
  65. *
  66. * @since 4.0.0
  67. * @var string|array
  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 An specific ID for the panel.
  127. * @param array $args Panel arguments.
  128. */
  129. public function __construct( $manager, $id, $args = array() ) {
  130. $keys = array_keys( get_object_vars( $this ) );
  131. foreach ( $keys as $key ) {
  132. if ( isset( $args[ $key ] ) ) {
  133. $this->$key = $args[ $key ];
  134. }
  135. }
  136. $this->manager = $manager;
  137. $this->id = $id;
  138. if ( empty( $this->active_callback ) ) {
  139. $this->active_callback = array( $this, 'active_callback' );
  140. }
  141. self::$instance_count += 1;
  142. $this->instance_number = self::$instance_count;
  143. $this->sections = array(); // Users cannot customize the $sections array.
  144. }
  145. /**
  146. * Check whether panel is active to current Customizer preview.
  147. *
  148. * @since 4.1.0
  149. *
  150. * @return bool Whether the panel is active to the current preview.
  151. */
  152. final public function active() {
  153. $panel = $this;
  154. $active = call_user_func( $this->active_callback, $this );
  155. /**
  156. * Filters response of WP_Customize_Panel::active().
  157. *
  158. * @since 4.1.0
  159. *
  160. * @param bool $active Whether the Customizer panel is active.
  161. * @param WP_Customize_Panel $panel WP_Customize_Panel instance.
  162. */
  163. $active = apply_filters( 'customize_panel_active', $active, $panel );
  164. return $active;
  165. }
  166. /**
  167. * Default callback used when invoking WP_Customize_Panel::active().
  168. *
  169. * Subclasses can override this with their specific logic, or they may
  170. * provide an 'active_callback' argument to the constructor.
  171. *
  172. * @since 4.1.0
  173. *
  174. * @return bool Always true.
  175. */
  176. public function active_callback() {
  177. return true;
  178. }
  179. /**
  180. * Gather the parameters passed to client JavaScript via JSON.
  181. *
  182. * @since 4.1.0
  183. *
  184. * @return array The array to be exported to the client as JSON.
  185. */
  186. public function json() {
  187. $array = wp_array_slice_assoc( (array) $this, array( 'id', 'description', 'priority', 'type' ) );
  188. $array['title'] = html_entity_decode( $this->title, ENT_QUOTES, get_bloginfo( 'charset' ) );
  189. $array['content'] = $this->get_content();
  190. $array['active'] = $this->active();
  191. $array['instanceNumber'] = $this->instance_number;
  192. $array['autoExpandSoleSection'] = $this->auto_expand_sole_section;
  193. return $array;
  194. }
  195. /**
  196. * Checks required user capabilities and whether the theme has the
  197. * feature support required by the panel.
  198. *
  199. * @since 4.0.0
  200. *
  201. * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
  202. */
  203. final public function check_capabilities() {
  204. if ( $this->capability && ! current_user_can( $this->capability ) ) {
  205. return false;
  206. }
  207. if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
  208. return false;
  209. }
  210. return true;
  211. }
  212. /**
  213. * Get the panel's content template for insertion into the Customizer pane.
  214. *
  215. * @since 4.1.0
  216. *
  217. * @return string Content for the panel.
  218. */
  219. final public function get_content() {
  220. ob_start();
  221. $this->maybe_render();
  222. return trim( ob_get_clean() );
  223. }
  224. /**
  225. * Check capabilities and render the panel.
  226. *
  227. * @since 4.0.0
  228. */
  229. final public function maybe_render() {
  230. if ( ! $this->check_capabilities() ) {
  231. return;
  232. }
  233. /**
  234. * Fires before rendering a Customizer panel.
  235. *
  236. * @since 4.0.0
  237. *
  238. * @param WP_Customize_Panel $this WP_Customize_Panel instance.
  239. */
  240. do_action( 'customize_render_panel', $this );
  241. /**
  242. * Fires before rendering a specific Customizer panel.
  243. *
  244. * The dynamic portion of the hook name, `$this->id`, refers to
  245. * the ID of the specific Customizer panel to be rendered.
  246. *
  247. * @since 4.0.0
  248. */
  249. do_action( "customize_render_panel_{$this->id}" );
  250. $this->render();
  251. }
  252. /**
  253. * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
  254. *
  255. * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template().
  256. *
  257. * @since 4.0.0
  258. */
  259. protected function render() {}
  260. /**
  261. * Render the panel UI in a subclass.
  262. *
  263. * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template().
  264. *
  265. * @since 4.1.0
  266. */
  267. protected function render_content() {}
  268. /**
  269. * Render the panel's JS templates.
  270. *
  271. * This function is only run for panel types that have been registered with
  272. * WP_Customize_Manager::register_panel_type().
  273. *
  274. * @since 4.3.0
  275. *
  276. * @see WP_Customize_Manager::register_panel_type()
  277. */
  278. public function print_template() {
  279. ?>
  280. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
  281. <?php $this->content_template(); ?>
  282. </script>
  283. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
  284. <?php $this->render_template(); ?>
  285. </script>
  286. <?php
  287. }
  288. /**
  289. * An Underscore (JS) template for rendering this panel's container.
  290. *
  291. * Class variables for this panel class are available in the `data` JS object;
  292. * export custom variables by overriding WP_Customize_Panel::json().
  293. *
  294. * @see WP_Customize_Panel::print_template()
  295. *
  296. * @since 4.3.0
  297. */
  298. protected function render_template() {
  299. ?>
  300. <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
  301. <h3 class="accordion-section-title" tabindex="0">
  302. {{ data.title }}
  303. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
  304. </h3>
  305. <ul class="accordion-sub-container control-panel-content"></ul>
  306. </li>
  307. <?php
  308. }
  309. /**
  310. * An Underscore (JS) template for this panel's content (but not its container).
  311. *
  312. * Class variables for this panel class are available in the `data` JS object;
  313. * export custom variables by overriding WP_Customize_Panel::json().
  314. *
  315. * @see WP_Customize_Panel::print_template()
  316. *
  317. * @since 4.3.0
  318. */
  319. protected function content_template() {
  320. ?>
  321. <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
  322. <button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button>
  323. <div class="accordion-section-title">
  324. <span class="preview-notice">
  325. <?php
  326. /* translators: %s: The site/panel title in the Customizer. */
  327. echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
  328. ?>
  329. </span>
  330. <# if ( data.description ) { #>
  331. <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
  332. <# } #>
  333. </div>
  334. <# if ( data.description ) { #>
  335. <div class="description customize-panel-description">
  336. {{{ data.description }}}
  337. </div>
  338. <# } #>
  339. <div class="customize-control-notifications-container"></div>
  340. </li>
  341. <?php
  342. }
  343. }
  344. /** WP_Customize_Nav_Menus_Panel class */
  345. require_once( ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php' );