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

https://github.com/mhoofman/wordpress-heroku · PHP · 327 lines · 102 code · 31 blank · 194 comment · 9 complexity · 29669a79d39df6d72587a340d0ba7585 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. return $this;
  143. }
  144. /**
  145. * Check whether panel is active to current Customizer preview.
  146. *
  147. * @since 4.1.0
  148. * @access public
  149. *
  150. * @return bool Whether the panel is active to the current preview.
  151. */
  152. public final function active() {
  153. $panel = $this;
  154. $active = call_user_func( $this->active_callback, $this );
  155. /**
  156. * Filter 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 {@see 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 {@see 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. * @access public
  174. *
  175. * @return bool Always true.
  176. */
  177. public function active_callback() {
  178. return true;
  179. }
  180. /**
  181. * Gather the parameters passed to client JavaScript via JSON.
  182. *
  183. * @since 4.1.0
  184. *
  185. * @return array The array to be exported to the client as JSON.
  186. */
  187. public function json() {
  188. $array = wp_array_slice_assoc( (array) $this, array( 'title', 'description', 'priority', 'type' ) );
  189. $array['content'] = $this->get_content();
  190. $array['active'] = $this->active();
  191. $array['instanceNumber'] = $this->instance_number;
  192. return $array;
  193. }
  194. /**
  195. * Checks required user capabilities and whether the theme has the
  196. * feature support required by the panel.
  197. *
  198. * @since 4.0.0
  199. *
  200. * @return bool False if theme doesn't support the panel or the user doesn't have the capability.
  201. */
  202. public final function check_capabilities() {
  203. if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
  204. return false;
  205. }
  206. if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
  207. return false;
  208. }
  209. return true;
  210. }
  211. /**
  212. * Get the panel's content template for insertion into the Customizer pane.
  213. *
  214. * @since 4.1.0
  215. *
  216. * @return string Content for the panel.
  217. */
  218. public final function get_content() {
  219. ob_start();
  220. $this->maybe_render();
  221. $template = trim( ob_get_contents() );
  222. ob_end_clean();
  223. return $template;
  224. }
  225. /**
  226. * Check capabilities and render the panel.
  227. *
  228. * @since 4.0.0
  229. */
  230. public final function maybe_render() {
  231. if ( ! $this->check_capabilities() ) {
  232. return;
  233. }
  234. /**
  235. * Fires before rendering a Customizer panel.
  236. *
  237. * @since 4.0.0
  238. *
  239. * @param WP_Customize_Panel $this WP_Customize_Panel instance.
  240. */
  241. do_action( 'customize_render_panel', $this );
  242. /**
  243. * Fires before rendering a specific Customizer panel.
  244. *
  245. * The dynamic portion of the hook name, `$this->id`, refers to
  246. * the ID of the specific Customizer panel to be rendered.
  247. *
  248. * @since 4.0.0
  249. */
  250. do_action( "customize_render_panel_{$this->id}" );
  251. $this->render();
  252. }
  253. /**
  254. * Render the panel container, and then its contents.
  255. *
  256. * @since 4.0.0
  257. * @access protected
  258. */
  259. protected function render() {
  260. $classes = 'accordion-section control-section control-panel control-panel-' . $this->type;
  261. ?>
  262. <li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
  263. <h3 class="accordion-section-title" tabindex="0">
  264. <?php echo esc_html( $this->title ); ?>
  265. <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span>
  266. </h3>
  267. <ul class="accordion-sub-container control-panel-content">
  268. <?php $this->render_content(); ?>
  269. </ul>
  270. </li>
  271. <?php
  272. }
  273. /**
  274. * Render the sections that have been added to the panel.
  275. *
  276. * @since 4.1.0
  277. * @access protected
  278. */
  279. protected function render_content() {
  280. ?>
  281. <li class="panel-meta accordion-section control-section<?php if ( empty( $this->description ) ) { echo ' cannot-expand'; } ?>">
  282. <div class="accordion-section-title" tabindex="0">
  283. <span class="preview-notice"><?php
  284. /* translators: %s is the site/panel title in the Customizer */
  285. echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">' . esc_html( $this->title ) . '</strong>' );
  286. ?></span>
  287. </div>
  288. <?php if ( ! empty( $this->description ) ) : ?>
  289. <div class="accordion-section-content description">
  290. <?php echo $this->description; ?>
  291. </div>
  292. <?php endif; ?>
  293. </li>
  294. <?php
  295. }
  296. }