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