| 1 | <?php |
| 2 | /** |
| 3 | * Customize Container Class. |
| 4 | * |
| 5 | * A UI container for Customizer components, managed by the WP_Customize_Manager. |
| 6 | * |
| 7 | * @package WordPress |
| 8 | * @subpackage Customize |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | abstract class WP_Customize_Container { |
| 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 container, which determines display order. |
| 33 | * |
| 34 | * @since 4.0.0 |
| 35 | * @access public |
| 36 | * @var integer |
| 37 | */ |
| 38 | public $priority = 160; |
| 39 | |
| 40 | /** |
| 41 | * Capability required for the container. |
| 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 requied for the container. |
| 51 | * |
| 52 | * @since 4.0.0 |
| 53 | * @access public |
| 54 | * @var string|array |
| 55 | */ |
| 56 | public $theme_supports = ''; |
| 57 | |
| 58 | /** |
| 59 | * Title to show in the 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 | * Constructor. |
| 78 | * |
| 79 | * Any supplied $args override class property defaults. |
| 80 | * |
| 81 | * @since 4.0.0 |
| 82 | * |
| 83 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
| 84 | * @param string $id An specific ID for the container. |
| 85 | * @param array $args Container arguments. |
| 86 | */ |
| 87 | public function __construct( $manager, $id, $args = array() ) { |
| 88 | $keys = array_keys( get_object_vars( $this ) ); |
| 89 | foreach ( $keys as $key ) { |
| 90 | if ( isset( $args[ $key ] ) ) |
| 91 | $this->$key = $args[ $key ]; |
| 92 | } |
| 93 | |
| 94 | $this->manager = $manager; |
| 95 | $this->id = $id; |
| 96 | |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Checks required user capabilities and whether the theme has the |
| 102 | * feature support required by the container. |
| 103 | * |
| 104 | * @since 4.0.0 |
| 105 | * |
| 106 | * @return bool False if theme doesn't support the container or user doesn't have the capability. |
| 107 | */ |
| 108 | public final function check_capabilities() { |
| 109 | if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) |
| 110 | return false; |
| 111 | |
| 112 | if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) |
| 113 | return false; |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check capabilities and render the container. |
| 120 | * |
| 121 | * @since 4.0.0 |
| 122 | */ |
| 123 | public final function maybe_render() { |
| 124 | if ( ! $this->check_capabilities() ) |
| 125 | return; |
| 126 | |
| 127 | /** |
| 128 | * Fires before rendering a Customizer section (or panel). |
| 129 | * |
| 130 | * @since 3.4.0 |
| 131 | * |
| 132 | * @param WP_Customize_Section $this WP_Customize_Section instance. |
| 133 | */ |
| 134 | do_action( 'customize_render_section', $this ); |
| 135 | |
| 136 | /** |
| 137 | * Fires before rendering a specific Customizer section (or panel). |
| 138 | * |
| 139 | * The dynamic portion of the hook name, $this->id, refers to the ID |
| 140 | * of the specific Customizer container to be rendered. |
| 141 | * |
| 142 | * @since 3.4.0 |
| 143 | */ |
| 144 | do_action( "customize_render_section_{$this->id}" ); |
| 145 | |
| 146 | $this->render(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Render the container, and the objects that have been added to it. |
| 151 | * |
| 152 | * @since 4.0.0 |
| 153 | */ |
| 154 | abstract protected function render(); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Customize Panel Class. |
| 160 | * |
| 161 | * A UI container for sections, managed by the WP_Customize_Manager. |
| 162 | * |
| 163 | * @package WordPress |
| 164 | * @subpackage Customize |
| 165 | * @since 4.0.0 |
| 166 | */ |
| 167 | class WP_Customize_Panel extends WP_Customize_Container { |
| 168 | |
| 169 | /** |
| 170 | * Customizer sections for this panel. |
| 171 | * |
| 172 | * @since 4.0.0 |
| 173 | * @access public |
| 174 | * @var array |
| 175 | */ |
| 176 | public $sections; |
| 177 | |
| 178 | /** |
| 179 | * Constructor. |
| 180 | * |
| 181 | * Any supplied $args override class property defaults. |
| 182 | * |
| 183 | * @since 4.0.0 |
| 184 | * @access public |
| 185 | * |
| 186 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
| 187 | * @param string $id An specific ID of the section. |
| 188 | * @param array $args Optional. Section arguments. Default empty array. |
| 189 | */ |
| 190 | public function __construct( $manager, $id, $args = array() ) { |
| 191 | parent::__construct( $manager, $id, $args ); |
| 192 | |
| 193 | $this->sections = array(); // Users cannot customize the $sections array. |
| 194 | |
| 195 | return $this; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Render the panel, and the sections that have been added to it. |
| 200 | * |
| 201 | * @since 4.0.0 |
| 202 | * @access protected |
| 203 | */ |
| 204 | protected function render() { |
| 205 | ?> |
| 206 | <li id="accordion-panel-<?php echo esc_attr( $this->id ); ?>" class="control-section control-panel accordion-section"> |
| 207 | <h3 class="accordion-section-title" tabindex="0"> |
| 208 | <?php echo esc_html( $this->title ); ?> |
| 209 | <span class="screen-reader-text"><?php _e( 'Press return or enter to open panel' ); ?></span> |
| 210 | </h3> |
| 211 | <span class="control-panel-back" tabindex="-1"><span class="screen-reader-text"><?php _e( 'Back' ); ?></span></span> |
| 212 | <ul class="accordion-sub-container control-panel-content"> |
| 213 | <li class="accordion-section control-section<?php if ( empty( $this->description ) ) echo ' cannot-expand'; ?>"> |
| 214 | <div class="accordion-section-title" tabindex="0"> |
| 215 | <span class="preview-notice"><?php |
| 216 | /* translators: %s is the site/panel title in the Customizer */ |
| 217 | echo sprintf( __( 'You are customizing %s' ), '<strong class="panel-title">' . esc_html( $this->title ) . '</strong>' ); |
| 218 | ?></span> |
| 219 | </div> |
| 220 | <?php if ( ! empty( $this->description ) ) : ?> |
| 221 | <div class="accordion-section-content description"> |
| 222 | <?php echo $this->description; ?> |
| 223 | </div> |
| 224 | <?php endif; ?> |
| 225 | </li> |
| 226 | <?php |
| 227 | foreach ( $this->sections as $section ) { |
| 228 | $section->maybe_render(); |
| 229 | } |
| 230 | ?> |
| 231 | </ul> |
| 232 | </li> |
| 233 | <?php |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** |
| 239 | * Customize Section Class. |
| 240 | * |
| 241 | * A UI container for controls, managed by the WP_Customize_Manager. |
| 242 | * |
| 243 | * @package WordPress |
| 244 | * @subpackage Customize |
| 245 | * @since 3.4.0 |
| 246 | */ |
| 247 | class WP_Customize_Section extends WP_Customize_Container { |
| 248 | |
| 249 | /** |
| 250 | * Panel in which to show the section, making it a sub-section. |
| 251 | * |
| 252 | * @since 4.0.0 |
| 253 | * @access public |
| 254 | * @var string |
| 255 | */ |
| 256 | public $panel = ''; |
| 257 | |
| 258 | /** |
| 259 | * Customizer controls for this section. |
| 260 | * |
| 261 | * @since 3.4.0 |
| 262 | * @access public |
| 263 | * @var array |
| 264 | */ |
| 265 | public $controls; |
| 266 | |
| 267 | /** |
| 268 | * Constructor. |
| 269 | * |
| 270 | * Any supplied $args override class property defaults. |
| 271 | * |
| 272 | * @since 3.4.0 |
| 273 | * |
| 274 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
| 275 | * @param string $id An specific ID of the section. |
| 276 | * @param array $args Section arguments. |
| 277 | */ |
| 278 | public function __construct( $manager, $id, $args = array() ) { |
| 279 | parent::__construct( $manager, $id, $args ); |
| 280 | |
| 281 | $this->controls = array(); // Users cannot customize the $controls array. |
| 282 | |
| 283 | return $this; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Render the section, and the controls that have been added to it. |
| 288 | * |
| 289 | * @since 3.4.0 |
| 290 | */ |
| 291 | protected function render() { |
| 292 | $classes = 'control-section accordion-section'; |
| 293 | if ( $this->panel ) { |
| 294 | $classes .= ' control-subsection'; |
| 295 | } |
| 296 | ?> |
| 297 | <li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>"> |
| 298 | <h3 class="accordion-section-title" tabindex="0"> |
| 299 | <?php echo esc_html( $this->title ); ?> |
| 300 | <span class="screen-reader-text"><?php _e( 'Press return or enter to expand' ); ?></span> |
| 301 | </h3> |
| 302 | <ul class="accordion-section-content"> |
| 303 | <?php if ( ! empty( $this->description ) ) : ?> |
| 304 | <li><p class="description customize-section-description"><?php echo $this->description; ?></p></li> |
| 305 | <?php endif; ?> |
| 306 | <?php |
| 307 | foreach ( $this->controls as $control ) |
| 308 | $control->maybe_render(); |
| 309 | ?> |
| 310 | </ul> |
| 311 | </li> |
| 312 | <?php |
| 313 | } |
| 314 | } |