WordPress 3.9
[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 = 10;
39
40         /**
41          * Capability required for the section.
42          *
43          * @since 3.4.0
44          * @access public
45          * @var string
46          */
47         public $capability = 'edit_theme_options';
48
49         /**
50          * Theme feature support for the section.
51          *
52          * @since 3.4.0
53          * @access public
54          * @var string|array
55          */
56         public $theme_supports = '';
57
58         /**
59          * Title of the section to show in UI.
60          *
61          * @since 3.4.0
62          * @access public
63          * @var string
64          */
65         public $title = '';
66
67         /**
68          * Description to show in the UI.
69          *
70          * @since 3.4.0
71          * @access public
72          * @var string
73          */
74         public $description = '';
75
76         /**
77          * Customizer controls for this section.
78          *
79          * @since 3.4.0
80          * @access public
81          * @var array
82          */
83         public $controls;
84
85         /**
86          * Constructor.
87          *
88          * Any supplied $args override class property defaults.
89          *
90          * @since 3.4.0
91          *
92          * @param WP_Customize_Manager $manager Customizer bootstrap instance.
93          * @param string               $id      An specific ID of the section.
94          * @param array                $args    Section arguments.
95          */
96         function __construct( $manager, $id, $args = array() ) {
97                 $keys = array_keys( get_class_vars( __CLASS__ ) );
98                 foreach ( $keys as $key ) {
99                         if ( isset( $args[ $key ] ) )
100                                 $this->$key = $args[ $key ];
101                 }
102
103                 $this->manager = $manager;
104                 $this->id = $id;
105
106                 $this->controls = array(); // Users cannot customize the $controls array.
107
108                 return $this;
109         }
110
111         /**
112          * Checks required user capabilities and whether the theme has the
113          * feature support required by the section.
114          *
115          * @since 3.4.0
116          *
117          * @return bool False if theme doesn't support the section or user doesn't have the capability.
118          */
119         public final function check_capabilities() {
120                 if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
121                         return false;
122
123                 if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
124                         return false;
125
126                 return true;
127         }
128
129         /**
130          * Check capabilities and render the section.
131          *
132          * @since 3.4.0
133          */
134         public final function maybe_render() {
135                 if ( ! $this->check_capabilities() )
136                         return;
137
138                 /**
139                  * Fires before rendering a Customizer section.
140                  *
141                  * @since 3.4.0
142                  *
143                  * @param WP_Customize_Section $this WP_Customize_Section instance.
144                  */
145                 do_action( 'customize_render_section', $this );
146                 /**
147                  * Fires before rendering a specific Customizer section.
148                  *
149                  * The dynamic portion of the hook name, $this->id, refers to the ID
150                  * of the specific Customizer section to be rendered.
151                  *
152                  * @since 3.4.0
153                  */
154                 do_action( "customize_render_section_{$this->id}" );
155
156                 $this->render();
157         }
158
159         /**
160          * Render the section, and the controls that have been added to it.
161          *
162          * @since 3.4.0
163          */
164         protected function render() {
165                 ?>
166                 <li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="control-section accordion-section">
167                         <h3 class="accordion-section-title" tabindex="0"><?php echo esc_html( $this->title ); ?></h3>
168                         <ul class="accordion-section-content">
169                                 <?php if ( ! empty( $this->description ) ) : ?>
170                                 <li><p class="description"><?php echo $this->description; ?></p></li>
171                                 <?php endif; ?>
172                                 <?php
173                                 foreach ( $this->controls as $control )
174                                         $control->maybe_render();
175                                 ?>
176                         </ul>
177                 </li>
178                 <?php
179         }
180 }