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