PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/math4youbyusgroupillinois/WordPress
PHP | 325 lines | 101 code | 30 blank | 194 comment | 9 complexity | ef7d4e79010487a690cc69024893c6ef MD5 | raw file
  1. <?php
  2. /**
  3. * Customize Panel Class.
  4. *
  5. * A UI container for sections, managed by the WP_Customize_Manager.
  6. *
  7. * @package WordPress
  8. * @subpackage Customize
  9. * @since 4.0.0
  10. */
  11. class WP_Customize_Panel {
  12. /**
  13. * Incremented with each new class instantiation, then stored in $instance_number.
  14. *
  15. * Used when sorting two instances whose priorities are equal.
  16. *
  17. * @since 4.1.0
  18. * @access protected
  19. * @var int
  20. */
  21. protected static $instance_count = 0;
  22. /**
  23. * Order in which this instance was created in relation to other instances.
  24. *
  25. * @since 4.1.0
  26. * @access public
  27. * @var int
  28. */
  29. public $instance_number;
  30. /**
  31. * WP_Customize_Manager instance.
  32. *
  33. * @since 4.0.0
  34. * @access public
  35. * @var WP_Customize_Manager
  36. */
  37. public $manager;
  38. /**
  39. * Unique identifier.
  40. *
  41. * @since 4.0.0
  42. * @access public
  43. * @var string
  44. */
  45. public $id;
  46. /**
  47. * Priority of the panel, defining the display order of panels and sections.
  48. *
  49. * @since 4.0.0
  50. * @access public
  51. * @var integer
  52. */
  53. public $priority = 160;
  54. /**
  55. * Capability required for the panel.
  56. *
  57. * @since 4.0.0
  58. * @access public
  59. * @var string
  60. */
  61. public $capability = 'edit_theme_options';
  62. /**
  63. * Theme feature support for the panel.
  64. *
  65. * @since 4.0.0
  66. * @access public
  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. * @access public
  75. * @var string
  76. */
  77. public $title = '';
  78. /**
  79. * Description to show in the UI.
  80. *
  81. * @since 4.0.0
  82. * @access public
  83. * @var string
  84. */
  85. public $description = '';
  86. /**
  87. * Customizer sections for this panel.
  88. *
  89. * @since 4.0.0
  90. * @access public
  91. * @var array
  92. */
  93. public $sections;
  94. /**
  95. * Type of this panel.
  96. *
  97. * @since 4.1.0
  98. * @access public
  99. * @var string
  100. */
  101. public $type = 'default';
  102. /**
  103. * Active callback.
  104. *
  105. * @since 4.1.0
  106. * @access public
  107. *
  108. * @see WP_Customize_Section::active()
  109. *
  110. * @var callable Callback is called with one argument, the instance of
  111. * {@see WP_Customize_Section}, and returns bool to indicate
  112. * whether the section is active (such as it relates to the URL
  113. * currently being previewed).
  114. */
  115. public $active_callback = '';
  116. /**
  117. * Constructor.
  118. *
  119. * Any supplied $args override class property defaults.
  120. *
  121. * @since 4.0.0
  122. *
  123. * @param WP_Customize_Manager $manager Customizer bootstrap instance.
  124. * @param string $id An specific ID for the panel.
  125. * @param array $args Panel arguments.
  126. */
  127. public function __construct( $manager, $id, $args = array() ) {
  128. $keys = array_keys( get_object_vars( $this ) );
  129. foreach ( $keys as $key ) {
  130. if ( isset( $args[ $key ] ) ) {
  131. $this->$key = $args[ $key ];
  132. }
  133. }
  134. $this->manager = $manager;
  135. $this->id = $id;
  136. if ( empty( $this->active_callback ) ) {
  137. $this->active_callback = array( $this, 'active_callback' );
  138. }
  139. self::$instance_count += 1;
  140. $this->instance_number = self::$instance_count;
  141. $this->sections = array(); // Users cannot customize the $sections array.
  142. }
  143. /**
  144. * Check whether panel is active to current Customizer preview.
  145. *
  146. * @since 4.1.0
  147. * @access public
  148. *
  149. * @return bool Whether the panel is active to the current preview.
  150. */
  151. final public function active() {
  152. $panel = $this;
  153. $active = call_user_func( $this->active_callback, $this );
  154. /**
  155. * Filter response of WP_Customize_Panel::active().
  156. *
  157. * @since 4.1.0
  158. *
  159. * @param bool $active Whether the Customizer panel is active.
  160. * @param WP_Customize_Panel $panel {@see WP_Customize_Panel} instance.
  161. */
  162. $active = apply_filters( 'customize_panel_active', $active, $panel );
  163. return $active;
  164. }
  165. /**
  166. * Default callback used when invoking {@see WP_Customize_Panel::active()}.
  167. *
  168. * Subclasses can override this with their specific logic, or they may
  169. * provide an 'active_callback' argument to the constructor.
  170. *
  171. * @since 4.1.0
  172. * @access public
  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( 'title', 'description', 'priority', 'type' ) );
  188. $array['content'] = $this->get_content();
  189. $array['active'] = $this->active();
  190. $array['instanceNumber'] = $this->instance_number;
  191. return $array;
  192. }
  193. /**
  194. * Checks required user capabilities and whether the theme has the
  195. * feature support required by the panel.
  196. *
  197. * @since 4.0.0
  198. *
  199. * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
  200. */
  201. final public function check_capabilities() {
  202. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
  203. return false;
  204. }
  205. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
  206. return false;
  207. }
  208. return true;
  209. }
  210. /**
  211. * Get the panel's content template for insertion into the Customizer pane.
  212. *
  213. * @since 4.1.0
  214. *
  215. * @return string Content for the panel.
  216. */
  217. final public function get_content() {
  218. ob_start();
  219. $this->maybe_render();
  220. $template = trim( ob_get_contents() );
  221. ob_end_clean();
  222. return $template;
  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.
  254. *
  255. * @since 4.0.0
  256. * @access protected
  257. */
  258. protected function render() {
  259. $classes = 'accordion-section control-section control-panel control-panel-' . $this->type;
  260. ?>
  261. <li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
  262. <h3 class="accordion-section-title" tabindex="0">
  263. <?php echo esc_html( $this->title ); ?>
  264. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
  265. </h3>
  266. <ul class="accordion-sub-container control-panel-content">
  267. <?php $this->render_content(); ?>
  268. </ul>
  269. </li>
  270. <?php
  271. }
  272. /**
  273. * Render the sections that have been added to the panel.
  274. *
  275. * @since 4.1.0
  276. * @access protected
  277. */
  278. protected function render_content() {
  279. ?>
  280. <li class="panel-meta accordion-section control-section<?php if ( empty( $this->description ) ) { echo ' cannot-expand'; } ?>">
  281. <div class="accordion-section-title" tabindex="0">
  282. <span class="preview-notice"><?php
  283. /* translators: %s is the site/panel title in the Customizer */
  284. echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">' . esc_html( $this->title ) . '</strong>' );
  285. ?></span>
  286. </div>
  287. <?php if ( ! empty( $this->description ) ) : ?>
  288. <div class="accordion-section-content description">
  289. <?php echo $this->description; ?>
  290. </div>
  291. <?php endif; ?>
  292. </li>
  293. <?php
  294. }
  295. }