PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/wordpress/wordpress
PHP | 394 lines | 119 code | 37 blank | 238 comment | 7 complexity | b5f1291956499b5efbb9c90952d82872 MD5 | raw file
Possible License(s): 0BSD
  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 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. *
  213. * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
  214. */
  215. final public function check_capabilities() {
  216. if ( $this->capability && ! current_user_can( $this->capability ) ) {
  217. return false;
  218. }
  219. if ( $this->theme_supports && ! current_theme_supports( ... (array) $this->theme_supports ) ) {
  220. return false;
  221. }
  222. return true;
  223. }
  224. /**
  225. * Get the panel's content template for insertion into the Customizer pane.
  226. *
  227. * @since 4.1.0
  228. *
  229. * @return string Content for the panel.
  230. */
  231. final public function get_content() {
  232. ob_start();
  233. $this->maybe_render();
  234. return trim( ob_get_clean() );
  235. }
  236. /**
  237. * Check capabilities and render the panel.
  238. *
  239. * @since 4.0.0
  240. */
  241. final public function maybe_render() {
  242. if ( ! $this->check_capabilities() ) {
  243. return;
  244. }
  245. /**
  246. * Fires before rendering a Customizer panel.
  247. *
  248. * @since 4.0.0
  249. *
  250. * @param WP_Customize_Panel $this WP_Customize_Panel instance.
  251. */
  252. do_action( 'customize_render_panel', $this );
  253. /**
  254. * Fires before rendering a specific Customizer panel.
  255. *
  256. * The dynamic portion of the hook name, `$this->id`, refers to
  257. * the ID of the specific Customizer panel to be rendered.
  258. *
  259. * @since 4.0.0
  260. */
  261. do_action( "customize_render_panel_{$this->id}" );
  262. $this->render();
  263. }
  264. /**
  265. * Render the panel container, and then its contents (via `this->render_content()`) in a subclass.
  266. *
  267. * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template().
  268. *
  269. * @since 4.0.0
  270. */
  271. protected function render() {}
  272. /**
  273. * Render the panel UI in a subclass.
  274. *
  275. * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template().
  276. *
  277. * @since 4.1.0
  278. */
  279. protected function render_content() {}
  280. /**
  281. * Render the panel's JS templates.
  282. *
  283. * This function is only run for panel types that have been registered with
  284. * WP_Customize_Manager::register_panel_type().
  285. *
  286. * @since 4.3.0
  287. *
  288. * @see WP_Customize_Manager::register_panel_type()
  289. */
  290. public function print_template() {
  291. ?>
  292. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>-content">
  293. <?php $this->content_template(); ?>
  294. </script>
  295. <script type="text/html" id="tmpl-customize-panel-<?php echo esc_attr( $this->type ); ?>">
  296. <?php $this->render_template(); ?>
  297. </script>
  298. <?php
  299. }
  300. /**
  301. * An Underscore (JS) template for rendering this panel's container.
  302. *
  303. * Class variables for this panel class are available in the `data` JS object;
  304. * export custom variables by overriding WP_Customize_Panel::json().
  305. *
  306. * @see WP_Customize_Panel::print_template()
  307. *
  308. * @since 4.3.0
  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. */
  331. protected function content_template() {
  332. ?>
  333. <li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
  334. <button class="customize-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></button>
  335. <div class="accordion-section-title">
  336. <span class="preview-notice">
  337. <?php
  338. /* translators: %s: The site/panel title in the Customizer. */
  339. echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
  340. ?>
  341. </span>
  342. <# if ( data.description ) { #>
  343. <button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false"><span class="screen-reader-text"><?php _e( 'Help' ); ?></span></button>
  344. <# } #>
  345. </div>
  346. <# if ( data.description ) { #>
  347. <div class="description customize-panel-description">
  348. {{{ data.description }}}
  349. </div>
  350. <# } #>
  351. <div class="customize-control-notifications-container"></div>
  352. </li>
  353. <?php
  354. }
  355. }
  356. /** WP_Customize_Nav_Menus_Panel class */
  357. require_once ABSPATH . WPINC . '/customize/class-wp-customize-nav-menus-panel.php';