| | 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 | /** |
| | 14 | * WP_Customize_Manager instance. |
| | 15 | * |
| | 16 | * @since 4.0.0 |
| | 17 | * @access public |
| | 18 | * @var WP_Customize_Manager |
| | 19 | */ |
| | 20 | public $manager; |
| | 21 | |
| | 22 | /** |
| | 23 | * Unique identifier. |
| | 24 | * |
| | 25 | * @since 4.0.0 |
| | 26 | * @access public |
| | 27 | * @var string |
| | 28 | */ |
| | 29 | public $id; |
| | 30 | |
| | 31 | /** |
| | 32 | * Priority of the panel, defining the display order of panels and sections. |
| | 33 | * |
| | 34 | * @since 4.0.0 |
| | 35 | * @access public |
| | 36 | * @var integer |
| | 37 | */ |
| | 38 | public $priority = 160; |
| | 39 | |
| | 40 | /** |
| | 41 | * Capability required for the panel. |
| | 42 | * |
| | 43 | * @since 4.0.0 |
| | 44 | * @access public |
| | 45 | * @var string |
| | 46 | */ |
| | 47 | public $capability = 'edit_theme_options'; |
| | 48 | |
| | 49 | /** |
| | 50 | * Theme feature support for the panel. |
| | 51 | * |
| | 52 | * @since 4.0.0 |
| | 53 | * @access public |
| | 54 | * @var string|array |
| | 55 | */ |
| | 56 | public $theme_supports = ''; |
| | 57 | |
| | 58 | /** |
| | 59 | * Title of the panel to show in UI. |
| | 60 | * |
| | 61 | * @since 4.0.0 |
| | 62 | * @access public |
| | 63 | * @var string |
| | 64 | */ |
| | 65 | public $title = ''; |
| | 66 | |
| | 67 | /** |
| | 68 | * Description to show in the UI. |
| | 69 | * |
| | 70 | * @since 4.0.0 |
| | 71 | * @access public |
| | 72 | * @var string |
| | 73 | */ |
| | 74 | public $description = ''; |
| | 75 | |
| | 76 | /** |
| | 77 | * Customizer sections for this panel. |
| | 78 | * |
| | 79 | * @since 4.0.0 |
| | 80 | * @access public |
| | 81 | * @var array |
| | 82 | */ |
| | 83 | public $sections; |
| | 84 | |
| | 85 | /** |
| | 86 | * Constructor. |
| | 87 | * |
| | 88 | * Any supplied $args override class property defaults. |
| | 89 | * |
| | 90 | * @since 4.0.0 |
| | 91 | * |
| | 92 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
| | 93 | * @param string $id An specific ID for the panel. |
| | 94 | * @param array $args Panel arguments. |
| | 95 | */ |
| | 96 | public function __construct( $manager, $id, $args = array() ) { |
| | 97 | $keys = array_keys( get_object_vars( $this ) ); |
| | 98 | foreach ( $keys as $key ) { |
| | 99 | if ( isset( $args[ $key ] ) ) { |
| | 100 | $this->$key = $args[ $key ]; |
| | 101 | } |
| | 102 | } |
| | 103 | |
| | 104 | $this->manager = $manager; |
| | 105 | $this->id = $id; |
| | 106 | |
| | 107 | $this->sections = array(); // Users cannot customize the $sections array. |
| | 108 | |
| | 109 | return $this; |
| | 110 | } |
| | 111 | |
| | 112 | /** |
| | 113 | * Checks required user capabilities and whether the theme has the |
| | 114 | * feature support required by the panel. |
| | 115 | * |
| | 116 | * @since 4.0.0 |
| | 117 | * |
| | 118 | * @return bool False if theme doesn't support the panel or the user doesn't have the capability. |
| | 119 | */ |
| | 120 | public final function check_capabilities() { |
| | 121 | if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) { |
| | 122 | return false; |
| | 123 | } |
| | 124 | |
| | 125 | if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) { |
| | 126 | return false; |
| | 127 | } |
| | 128 | |
| | 129 | return true; |
| | 130 | } |
| | 131 | |
| | 132 | /** |
| | 133 | * Check capabilities and render the panel. |
| | 134 | * |
| | 135 | * @since 4.0.0 |
| | 136 | */ |
| | 137 | public final function maybe_render() { |
| | 138 | if ( ! $this->check_capabilities() ) { |
| | 139 | return; |
| | 140 | } |
| | 141 | |
| | 142 | /** |
| | 143 | * Fires before rendering a Customizer panel. |
| | 144 | * |
| | 145 | * @since 4.0.0 |
| | 146 | * |
| | 147 | * @param WP_Customize_Panel $this WP_Customize_Panel instance. |
| | 148 | */ |
| | 149 | do_action( 'customize_render_panel', $this ); |
| | 150 | |
| | 151 | /** |
| | 152 | * Fires before rendering a specific Customizer panel. |
| | 153 | * |
| | 154 | * The dynamic portion of the hook name, $this->id, refers to the ID |
| | 155 | * of the specific Customizer panel to be rendered. |
| | 156 | * |
| | 157 | * @since 4.0.0 |
| | 158 | */ |
| | 159 | do_action( "customize_render_panel_{$this->id}" ); |
| | 160 | |
| | 161 | $this->render(); |
| | 162 | } |
| | 163 | |
| | 164 | /** |
| | 165 | * Render the panel, and the sections that have been added to it. |
| | 166 | * |
| | 167 | * @since 4.0.0 |
| | 168 | * @access protected |
| | 169 | */ |
| | 170 | protected function render() { |
| | 171 | ?> |
| | 172 | <li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="control-section control-panel accordion-section"> |
| | 173 | <h3 class="accordion-section-title" tabindex="0"> |
| | 174 | <?php echo esc_html( $this->title ); ?> |
| | 175 | <span class="screen-reader-text"><?php _e( 'Press return or enter to open this panel' ); ?></span> |
| | 176 | </h3> |
| | 177 | <span class="control-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></span> |
| | 178 | <ul class="accordion-sub-container control-panel-content"> |
| | 179 | <li class="accordion-section control-section<?php if ( empty( $this->description ) ) echo ' cannot-expand'; ?>"> |
| | 180 | <div class="accordion-section-title" tabindex="0"> |
| | 181 | <span class="preview-notice"><?php |
| | 182 | /* translators: %s is the site/panel title in the Customizer */ |
| | 183 | echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">' . esc_html( $this->title ) . '</strong>' ); |
| | 184 | ?></span> |
| | 185 | </div> |
| | 186 | <?php if ( ! empty( $this->description ) ) : ?> |
| | 187 | <div class="accordion-section-content description"> |
| | 188 | <?php echo $this->description; ?> |
| | 189 | </div> |
| | 190 | <?php endif; ?> |
| | 191 | </li> |
| | 192 | <?php |
| | 193 | foreach ( $this->sections as $section ) { |
| | 194 | $section->maybe_render(); |
| | 195 | } |
| | 196 | ?> |
| | 197 | </ul> |
| | 198 | </li> |
| | 199 | <?php |
| | 200 | } |
| | 201 | } |
| | 202 | No newline at end of file |