PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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