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