PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.2
Elementor Website Builder – more than just a page builder v3.1.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / settings / settings-page.php

settings-page.php in Elementor Website Builder – more than just a page builder 3.1.2, at includes/settings/settings-page.php

403 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Elementor settings page.
10 *
11 * An abstract class that provides the needed properties and methods to handle
12 * WordPress dashboard settings pages in inheriting classes.
13 *
14 * @since 1.0.0
15 * @abstract
16 */
17 abstract class Settings_Page {
18
19 /**
20 * Settings page ID.
21 */
22 const PAGE_ID = '';
23
24 /**
25 * Tabs.
26 *
27 * Holds the settings page tabs, sections and fields.
28 *
29 * @access private
30 *
31 * @var array
32 */
33 private $tabs;
34
35 /**
36 * Create tabs.
37 *
38 * Return the settings page tabs, sections and fields.
39 *
40 * @since 1.5.0
41 * @access protected
42 * @abstract
43 */
44 abstract protected function create_tabs();
45
46 /**
47 * Get settings page title.
48 *
49 * Retrieve the title for the settings page.
50 *
51 * @since 1.5.0
52 * @access protected
53 * @abstract
54 */
55 abstract protected function get_page_title();
56
57 /**
58 * Get settings page URL.
59 *
60 * Retrieve the URL of the settings page.
61 *
62 * @since 1.5.0
63 * @access public
64 * @static
65 *
66 * @return string Settings page URL.
67 */
68 final public static function get_url() {
69 return admin_url( 'admin.php?page=' . static::PAGE_ID );
70 }
71
72 /**
73 * Settings page constructor.
74 *
75 * Initializing Elementor settings page.
76 *
77 * @since 1.5.0
78 * @access public
79 */
80 public function __construct() {
81 if ( ! empty( $_POST['option_page'] ) && static::PAGE_ID === $_POST['option_page'] ) {
82 add_action( 'admin_init', [ $this, 'register_settings_fields' ] );
83 }
84 }
85
86 /**
87 * Get tabs.
88 *
89 * Retrieve the settings page tabs, sections and fields.
90 *
91 * @since 1.5.0
92 * @access public
93 *
94 * @return array Settings page tabs, sections and fields.
95 */
96 final public function get_tabs() {
97 $this->ensure_tabs();
98
99 return $this->tabs;
100 }
101
102 /**
103 * Add tab.
104 *
105 * Register a new tab to a settings page.
106 *
107 * @since 1.5.0
108 * @access public
109 *
110 * @param string $tab_id Tab ID.
111 * @param array $tab_args Optional. Tab arguments. Default is an empty array.
112 */
113 final public function add_tab( $tab_id, array $tab_args = [] ) {
114 $this->ensure_tabs();
115
116 if ( isset( $this->tabs[ $tab_id ] ) ) {
117 // Don't override an existing tab
118 return;
119 }
120
121 if ( ! isset( $tab_args['sections'] ) ) {
122 $tab_args['sections'] = [];
123 }
124
125 $this->tabs[ $tab_id ] = $tab_args;
126 }
127
128 /**
129 * Add section.
130 *
131 * Register a new section to a tab.
132 *
133 * @since 1.5.0
134 * @access public
135 *
136 * @param string $tab_id Tab ID.
137 * @param string $section_id Section ID.
138 * @param array $section_args Optional. Section arguments. Default is an
139 * empty array.
140 */
141 final public function add_section( $tab_id, $section_id, array $section_args = [] ) {
142 $this->ensure_tabs();
143
144 if ( ! isset( $this->tabs[ $tab_id ] ) ) {
145 // If the requested tab doesn't exists, use the first tab
146 $tab_id = key( $this->tabs );
147 }
148
149 if ( isset( $this->tabs[ $tab_id ]['sections'][ $section_id ] ) ) {
150 // Don't override an existing section
151 return;
152 }
153
154 if ( ! isset( $section_args['fields'] ) ) {
155 $section_args['fields'] = [];
156 }
157
158 $this->tabs[ $tab_id ]['sections'][ $section_id ] = $section_args;
159 }
160
161 /**
162 * Add field.
163 *
164 * Register a new field to a section.
165 *
166 * @since 1.5.0
167 * @access public
168 *
169 * @param string $tab_id Tab ID.
170 * @param string $section_id Section ID.
171 * @param string $field_id Field ID.
172 * @param array $field_args Field arguments.
173 */
174 final public function add_field( $tab_id, $section_id, $field_id, array $field_args ) {
175 $this->ensure_tabs();
176
177 if ( ! isset( $this->tabs[ $tab_id ] ) ) {
178 // If the requested tab doesn't exists, use the first tab
179 $tab_id = key( $this->tabs );
180 }
181
182 if ( ! isset( $this->tabs[ $tab_id ]['sections'][ $section_id ] ) ) {
183 // If the requested section doesn't exists, use the first section
184 $section_id = key( $this->tabs[ $tab_id ]['sections'] );
185 }
186
187 if ( isset( $this->tabs[ $tab_id ]['sections'][ $section_id ]['fields'][ $field_id ] ) ) {
188 // Don't override an existing field
189 return;
190 }
191
192 $this->tabs[ $tab_id ]['sections'][ $section_id ]['fields'][ $field_id ] = $field_args;
193 }
194
195 /**
196 * Add fields.
197 *
198 * Register multiple fields to a section.
199 *
200 * @since 1.5.0
201 * @access public
202 *
203 * @param string $tab_id Tab ID.
204 * @param string $section_id Section ID.
205 * @param array $fields {
206 * An array of fields.
207 *
208 * @type string $field_id Field ID.
209 * @type array $field_args Field arguments.
210 * }
211 */
212 final public function add_fields( $tab_id, $section_id, array $fields ) {
213 foreach ( $fields as $field_id => $field_args ) {
214 $this->add_field( $tab_id, $section_id, $field_id, $field_args );
215 }
216 }
217
218 /**
219 * Register settings fields.
220 *
221 * In each tab register his inner sections, and in each section register his
222 * inner fields.
223 *
224 * @since 1.5.0
225 * @access public
226 */
227 final public function register_settings_fields() {
228 $controls_class_name = __NAMESPACE__ . '\Settings_Controls';
229
230 $tabs = $this->get_tabs();
231
232 foreach ( $tabs as $tab_id => $tab ) {
233
234 foreach ( $tab['sections'] as $section_id => $section ) {
235 $full_section_id = 'elementor_' . $section_id . '_section';
236
237 $label = isset( $section['label'] ) ? $section['label'] : '';
238
239 $section_callback = isset( $section['callback'] ) ? $section['callback'] : '__return_empty_string';
240
241 add_settings_section( $full_section_id, $label, $section_callback, static::PAGE_ID );
242
243 foreach ( $section['fields'] as $field_id => $field ) {
244 $full_field_id = ! empty( $field['full_field_id'] ) ? $field['full_field_id'] : 'elementor_' . $field_id;
245
246 $field['field_args']['id'] = $full_field_id;
247
248 $field_classes = [ $full_field_id ];
249
250 if ( ! empty( $field['class'] ) ) {
251 $field_classes[] = $field['field_args']['class'];
252 }
253
254 $field['field_args']['class'] = implode( ' ', $field_classes );
255
256 if ( ! isset( $field['render'] ) ) {
257 $field['render'] = [ $controls_class_name, 'render' ];
258 }
259
260 add_settings_field(
261 $full_field_id,
262 isset( $field['label'] ) ? $field['label'] : '',
263 $field['render'],
264 static::PAGE_ID,
265 $full_section_id,
266 $field['field_args']
267 );
268
269 $setting_args = [];
270
271 if ( ! empty( $field['setting_args'] ) ) {
272 $setting_args = $field['setting_args'];
273 }
274
275 register_setting( static::PAGE_ID, $full_field_id, $setting_args );
276 }
277 }
278 }
279 }
280
281 /**
282 * Display settings page.
283 *
284 * Output the content for the settings page.
285 *
286 * @since 1.5.0
287 * @access public
288 */
289 public function display_settings_page() {
290 $this->register_settings_fields();
291
292 $tabs = $this->get_tabs();
293 ?>
294 <div class="wrap">
295 <h1><?php echo $this->get_page_title(); ?></h1>
296 <div id="elementor-settings-tabs-wrapper" class="nav-tab-wrapper">
297 <?php
298 foreach ( $tabs as $tab_id => $tab ) {
299 if ( empty( $tab['sections'] ) ) {
300 continue;
301 }
302
303 $active_class = '';
304
305 if ( 'general' === $tab_id ) {
306 $active_class = ' nav-tab-active';
307 }
308
309 echo "<a id='elementor-settings-tab-{$tab_id}' class='nav-tab{$active_class}' href='#tab-{$tab_id}'>{$tab['label']}</a>";
310 }
311 ?>
312 </div>
313 <form id="elementor-settings-form" method="post" action="options.php">
314 <?php
315 settings_fields( static::PAGE_ID );
316
317 foreach ( $tabs as $tab_id => $tab ) {
318 if ( empty( $tab['sections'] ) ) {
319 continue;
320 }
321
322 $active_class = '';
323
324 if ( 'general' === $tab_id ) {
325 $active_class = ' elementor-active';
326 }
327
328 echo "<div id='tab-{$tab_id}' class='elementor-settings-form-page{$active_class}'>";
329
330 foreach ( $tab['sections'] as $section_id => $section ) {
331 $full_section_id = 'elementor_' . $section_id . '_section';
332
333 if ( ! empty( $section['label'] ) ) {
334 echo "<h2>{$section['label']}</h2>";
335 }
336
337 if ( ! empty( $section['callback'] ) ) {
338 $section['callback']();
339 }
340
341 echo '<table class="form-table">';
342
343 do_settings_fields( static::PAGE_ID, $full_section_id );
344
345 echo '</table>';
346 }
347
348 echo '</div>';
349 }
350
351 submit_button();
352 ?>
353 </form>
354 </div><!-- /.wrap -->
355 <?php
356 }
357
358 public function get_usage_fields() {
359 return [
360 'allow_tracking' => [
361 'label' => __( 'Usage Data Sharing', 'elementor' ),
362 'field_args' => [
363 'type' => 'checkbox',
364 'value' => 'yes',
365 'default' => '',
366 'sub_desc' => __( 'Become a super contributor by opting in to share non-sensitive plugin data and to receive periodic email updates from us.', 'elementor' ) . sprintf( ' <a href="%1$s" target="_blank">%2$s</a>', 'https://go.elementor.com/usage-data-tracking/', __( 'Learn more.', 'elementor' ) ),
367 ],
368 'setting_args' => [ __NAMESPACE__ . '\Tracker', 'check_for_settings_optin' ],
369 ],
370 ];
371 }
372
373 /**
374 * Ensure tabs.
375 *
376 * Make sure the settings page has tabs before inserting any new sections or
377 * fields.
378 *
379 * @since 1.5.0
380 * @access private
381 */
382 private function ensure_tabs() {
383 if ( null === $this->tabs ) {
384 $this->tabs = $this->create_tabs();
385
386 $page_id = static::PAGE_ID;
387
388 /**
389 * After create settings.
390 *
391 * Fires after the settings are created in Elementor admin page.
392 *
393 * The dynamic portion of the hook name, `$page_id`, refers to the current page ID.
394 *
395 * @since 1.0.0
396 *
397 * @param Settings_Page $this The settings page.
398 */
399 do_action( "elementor/admin/after_create_settings/{$page_id}", $this );
400 }
401 }
402 }
403