Plugin Directory

source: wpb-elementor-addons/trunk/admin/class.settings-api.php

Last change on this file was 3401517, checked in by wpbean, 4 months ago

tested with the Plugin Check

  • Property svn:executable set to *
File size: 28.3 KB
Line 
1<?php
2
3/**
4 * The weDevs Settings API wrapper class modified for WPB Elementor addons.
5 *
6 * @package WPB Elementor Addons
7 */
8
9if (! class_exists('WPB_EA_WeDevs_Settings_API')) :
10        /**
11         * The Settings API Class.
12         */
13        class WPB_EA_WeDevs_Settings_API
14        {
15
16                /**
17                 * Settings sections array.
18                 *
19                 * @var array
20                 */
21                protected $settings_sections = array();
22
23                /**
24                 * Settings fields array.
25                 *
26                 * @var array
27                 */
28                protected $settings_fields = array();
29
30                /**
31                 * Class Constructor.
32                 */
33                public function __construct()
34                {
35                        add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
36                }
37
38                /**
39                 * Enqueue scripts and styles.
40                 */
41                public function admin_enqueue_scripts()
42                {
43                        wp_enqueue_style('wp-color-picker');
44                        wp_enqueue_media();
45                        wp_enqueue_script('wp-color-picker');
46                        wp_enqueue_script('jquery');
47                }
48
49                /**
50                 * Set settings sections
51                 *
52                 * @param array $sections setting sections array.
53                 */
54                public function set_sections($sections)
55                {
56                        $this->settings_sections = $sections;
57
58                        return $this;
59                }
60
61                /**
62                 * Add a single section
63                 *
64                 * @param array $section The settings section.
65                 */
66                public function add_section($section)
67                {
68                        $this->settings_sections[] = $section;
69
70                        return $this;
71                }
72
73                /**
74                 * Set settings fields.
75                 *
76                 * @param array $fields settings fields array.
77                 */
78                public function set_fields($fields)
79                {
80                        $this->settings_fields = $fields;
81
82                        return $this;
83                }
84
85                /**
86                 * Add settings field.
87                 *
88                 * @param [type] $section The settings section.
89                 * @param [type] $field The settings field.
90                 * @return array
91                 */
92                public function add_field($section, $field)
93                {
94                        $defaults = array(
95                                'name'  => '',
96                                'label' => '',
97                                'desc'  => '',
98                                'type'  => 'text',
99                        );
100
101                        $arg                                 = wp_parse_args($field, $defaults);
102                        $this->settings_fields[$section][] = $arg;
103
104                        return $this;
105                }
106
107                /**
108                 * Initialize and registers the settings sections and fileds to WordPress
109                 *
110                 * Usually this should be called at `admin_init` hook.
111                 *
112                 * This function gets the initiated settings sections and fields. Then
113                 * registers them to WordPress and ready for use.
114                 */
115                public function admin_init()
116                {
117                        // register settings sections.
118                        foreach ($this->settings_sections as $section) {
119                                if (false === get_option($section['id'])) {
120                                        add_option($section['id']);
121                                }
122
123                                if (isset($section['desc']) && ! empty($section['desc'])) {
124                                        $section['desc'] = '<div class="inside">' . $section['desc'] . '</div>';
125                                        $callback        = function () use ($section) {
126                                                echo wp_kses_post(str_replace('"', '\"', $section['desc']));
127                                        };
128                                } elseif (isset($section['callback'])) {
129                                        $callback = $section['callback'];
130                                } else {
131                                        $callback = null;
132                                }
133
134                                add_settings_section($section['id'], $section['title'], $callback, $section['id']);
135                        }
136
137                        // register settings fields.
138                        foreach ($this->settings_fields as $section => $field) {
139                                foreach ($field as $option) {
140
141                                        $name     = $option['name'];
142                                        $type     = isset($option['type']) ? $option['type'] : 'text';
143                                        $label    = isset($option['label']) ? $option['label'] : '';
144                                        $callback = isset($option['callback']) ? $option['callback'] : array($this, 'callback_' . $type);
145
146                                        $args = array(
147                                                'id'                => $name,
148                                                'class'             => isset($option['class']) ? $option['class'] : $name,
149                                                'label_for'         => "{$section}[{$name}]",
150                                                'desc'              => isset($option['desc']) ? $option['desc'] : '',
151                                                'name'              => $label,
152                                                'section'           => $section,
153                                                'size'              => isset($option['size']) ? $option['size'] : null,
154                                                'options'           => isset($option['options']) ? $option['options'] : '',
155                                                'std'               => isset($option['default']) ? $option['default'] : '',
156                                                'sanitize_callback' => isset($option['sanitize_callback']) ? $option['sanitize_callback'] : '',
157                                                'type'              => $type,
158                                                'placeholder'       => isset($option['placeholder']) ? $option['placeholder'] : '',
159                                                'min'               => isset($option['min']) ? $option['min'] : '',
160                                                'max'               => isset($option['max']) ? $option['max'] : '',
161                                                'step'              => isset($option['step']) ? $option['step'] : '',
162                                                'icon'              => isset($option['icon']) ? $option['icon'] : '',
163                                        );
164
165                                        add_settings_field("{$section}[{$name}]", $label, $callback, $section, $section, $args);
166                                }
167                        }
168
169                        // creates our settings in the options table.
170                        foreach ($this->settings_sections as $section) {
171                                register_setting($section['id'], $section['id'], array($this, 'sanitize_options'));
172                        }
173                }
174
175                /**
176                 * Get field description for display
177                 *
178                 * @param array $args settings field args.
179                 */
180                public function get_field_description($args)
181                {
182                        if (! empty($args['desc'])) {
183                                $desc = sprintf('<p class="description">%s</p>', $args['desc']);
184                        } else {
185                                $desc = '';
186                        }
187
188                        return $desc;
189                }
190
191                /**
192                 * Displays a text field for a settings field
193                 *
194                 * @param array $args settings field args.
195                 */
196                public function callback_text($args)
197                {
198
199                        $value       = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
200                        $size        = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : 'regular';
201                        $type        = isset($args['type']) ? $args['type'] : 'text';
202                        $placeholder = empty($args['placeholder']) ? '' : ' placeholder="' . $args['placeholder'] . '"';
203
204                        $html  = sprintf('<input type="%1$s" class="%2$s-text" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder);
205                        $html .= $this->get_field_description($args);
206
207                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
208                }
209
210                /**
211                 * Displays a url field for a settings field
212                 *
213                 * @param array $args settings field args.
214                 */
215                public function callback_url($args)
216                {
217                        $this->callback_text($args);
218                }
219
220                /**
221                 * Displays a number field for a settings field
222                 *
223                 * @param array $args settings field args.
224                 */
225                public function callback_number($args)
226                {
227                        $value       = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
228                        $size        = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : 'regular';
229                        $type        = isset($args['type']) ? $args['type'] : 'number';
230                        $placeholder = empty($args['placeholder']) ? '' : ' placeholder="' . $args['placeholder'] . '"';
231                        $min         = ('' === $args['min']) ? '' : ' min="' . $args['min'] . '"';
232                        $max         = ('' === $args['max']) ? '' : ' max="' . $args['max'] . '"';
233                        $step        = ('' === $args['step']) ? '' : ' step="' . $args['step'] . '"';
234
235                        $html  = sprintf('<input type="%1$s" class="%2$s-number" id="%3$s[%4$s]" name="%3$s[%4$s]" value="%5$s"%6$s%7$s%8$s%9$s/>', $type, $size, $args['section'], $args['id'], $value, $placeholder, $min, $max, $step);
236                        $html .= $this->get_field_description($args);
237
238                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
239                }
240
241                /**
242                 * Displays a checkbox for a settings field
243                 *
244                 * @param array $args settings field args.
245                 */
246                public function callback_checkbox($args)
247                {
248
249                        $value = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
250
251                        $html  = '<fieldset>';
252                        $html .= sprintf('<label for="wpuf-%1$s[%2$s]">', $args['section'], $args['id']);
253                        $html .= sprintf('<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id']);
254                        $html .= sprintf('<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked($value, 'on', false));
255                        $html .= sprintf('%1$s</label>', $args['desc']);
256                        $html .= '</fieldset>';
257
258                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
259                }
260
261                /**
262                 * Displays a toggle for a settings field
263                 *
264                 * @param array $args settings field args.
265                 */
266                public function callback_toggle($args)
267                {
268
269                        $value = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
270
271                        $html  = '<fieldset>';
272                        $html .= sprintf('<label class="toggle-btn' . esc_attr('on' === $value ? ' active' : '') . '" for="wpuf-%1$s[%2$s]">', $args['section'], $args['id']);
273                        $html .= sprintf('<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id']);
274                        $html .= sprintf('<input type="checkbox" class="checkbox cb-value" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked($value, 'on', false));
275                        $html .= '<span class="round-btn"></span>';
276                        $html .= '</label>';
277                        $html .= '</fieldset>';
278
279                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
280                }
281
282                /**
283                 * Displays a toggle for a settings field
284                 *
285                 * @param array $args settings field args.
286                 */
287                public function callback_premium($args)
288                {
289
290                        $value = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
291
292                        $html  = '<fieldset>';
293                        $html .= sprintf('<label class="toggle-btn' . esc_attr($value === 'on' ? ' active' : '') . '" for="wpuf-%1$s[%2$s]">', $args['section'], $args['id']);
294                        $html .= sprintf('<input type="hidden" name="%1$s[%2$s]" value="off" />', $args['section'], $args['id']);
295                        $html .= sprintf('<input type="checkbox" class="checkbox cb-value" id="wpuf-%1$s[%2$s]" name="%1$s[%2$s]" value="on" %3$s />', $args['section'], $args['id'], checked($value, 'on', false));
296                        $html .= '<span class="round-btn"></span>';
297                        $html .= '</label>';
298                        $html .= '</fieldset>';
299
300                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
301                }
302
303                /**
304                 * Displays a toggle for a settings field
305                 *
306                 * @param array $args settings field args.
307                 */
308                public function callback_section_title($args)
309                {
310
311                        $html  = '<fieldset class="wpb-ea-section-title-area">';
312                        $html .= '<h2 class="wpb-ea-section-title">';
313                        $html .= esc_html($args['name']);
314                        $html .= '</h2>';
315
316                        if (array_key_exists('desc', $args)) {
317                                if ($args['desc']) {
318                                        $html .= sprintf('<p class="about-text">%1$s</p>', $args['desc']);
319                                }
320                        }
321
322                        $html .= '</fieldset>';
323
324                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
325                }
326
327                /**
328                 * Displays a multicheckbox for a settings field
329                 *
330                 * @param array $args settings field args.
331                 */
332                public function callback_multicheck($args)
333                {
334
335                        $value = $this->get_option($args['id'], $args['section'], $args['std']);
336                        $html  = '<fieldset>';
337                        $html .= sprintf('<input type="hidden" name="%1$s[%2$s]" value="" />', $args['section'], $args['id']);
338                        foreach ($args['options'] as $key => $label) {
339                                $checked = isset($value[$key]) ? $value[$key] : '0';
340                                $html   .= sprintf('<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key);
341                                $html   .= sprintf('<input type="checkbox" class="checkbox" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked($checked, $key, false));
342                                $html   .= sprintf('%1$s</label><br>', $label);
343                        }
344
345                        $html .= $this->get_field_description($args);
346                        $html .= '</fieldset>';
347
348                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
349                }
350
351                /**
352                 * Displays a radio button for a settings field
353                 *
354                 * @param array $args settings field args.
355                 */
356                public function callback_radio($args)
357                {
358
359                        $value = $this->get_option($args['id'], $args['section'], $args['std']);
360                        $html  = '<fieldset>';
361
362                        foreach ($args['options'] as $key => $label) {
363                                $html .= sprintf('<label for="wpuf-%1$s[%2$s][%3$s]">', $args['section'], $args['id'], $key);
364                                $html .= sprintf('<input type="radio" class="radio" id="wpuf-%1$s[%2$s][%3$s]" name="%1$s[%2$s]" value="%3$s" %4$s />', $args['section'], $args['id'], $key, checked($value, $key, false));
365                                $html .= sprintf('%1$s</label><br>', $label);
366                        }
367
368                        $html .= $this->get_field_description($args);
369                        $html .= '</fieldset>';
370
371                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
372                }
373
374                /**
375                 * Displays a selectbox for a settings field
376                 *
377                 * @param array $args settings field args.
378                 */
379                public function callback_select($args)
380                {
381
382                        $value = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
383                        $size  = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : 'regular';
384                        $html  = sprintf('<select class="%1$s" name="%2$s[%3$s]" id="%2$s[%3$s]">', $size, $args['section'], $args['id']);
385
386                        foreach ($args['options'] as $key => $label) {
387                                $html .= sprintf('<option value="%s"%s>%s</option>', $key, selected($value, $key, false), $label);
388                        }
389
390                        $html .= sprintf('</select>');
391                        $html .= $this->get_field_description($args);
392
393                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
394                }
395
396                /**
397                 * Displays a textarea for a settings field
398                 *
399                 * @param array $args settings field args.
400                 */
401                public function callback_textarea($args)
402                {
403
404                        $value       = esc_textarea($this->get_option($args['id'], $args['section'], $args['std']));
405                        $size        = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : 'regular';
406                        $placeholder = empty($args['placeholder']) ? '' : ' placeholder="' . $args['placeholder'] . '"';
407
408                        $html  = sprintf('<textarea rows="5" cols="55" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]"%4$s>%5$s</textarea>', $size, $args['section'], $args['id'], $placeholder, $value);
409                        $html .= $this->get_field_description($args);
410
411                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
412                }
413
414                /**
415                 * Displays the html for a settings field.
416                 *
417                 * @param [type] $args Settings field args.
418                 * @return void
419                 */
420                public function callback_html($args)
421                {
422                        echo wp_kses_post($this->get_field_description($args));
423                }
424
425                /**
426                 * Displays a rich text textarea for a settings field
427                 *
428                 * @param array $args settings field args.
429                 */
430                public function callback_wysiwyg($args)
431                {
432
433                        $value = $this->get_option($args['id'], $args['section'], $args['std']);
434                        $size  = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : '500px';
435
436                        echo '<div style="max-width: ' . esc_html($size) . ';">';
437
438                        $editor_settings = array(
439                                'teeny'         => true,
440                                'textarea_name' => $args['section'] . '[' . $args['id'] . ']',
441                                'textarea_rows' => 10,
442                        );
443
444                        if (isset($args['options']) && is_array($args['options'])) {
445                                $editor_settings = array_merge($editor_settings, $args['options']);
446                        }
447
448                        wp_editor($value, $args['section'] . '-' . $args['id'], $editor_settings);
449
450                        echo '</div>';
451
452                        echo esc_html($this->get_field_description($args));
453                }
454
455                /**
456                 * Displays a file upload field for a settings field
457                 *
458                 * @param array $args settings field args.
459                 */
460                public function callback_file($args)
461                {
462
463                        $value = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
464                        $size  = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : 'regular';
465                        $id    = $args['section'] . '[' . $args['id'] . ']';
466                        $label = isset($args['options']['button_label']) ? $args['options']['button_label'] : esc_html__('Choose File', 'wpb-elementor-addons');
467
468                        $html  = sprintf('<input type="text" class="%1$s-text wpsa-url" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value);
469                        $html .= '<input type="button" class="button wpsa-browse" value="' . $label . '" />';
470                        $html .= $this->get_field_description($args);
471
472                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
473                }
474
475                /**
476                 * Displays a password field for a settings field
477                 *
478                 * @param array $args settings field args.
479                 */
480                public function callback_password($args)
481                {
482
483                        $value = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
484                        $size  = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : 'regular';
485
486                        $html  = sprintf('<input type="password" class="%1$s-text" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s"/>', $size, $args['section'], $args['id'], $value);
487                        $html .= $this->get_field_description($args);
488
489                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
490                }
491
492                /**
493                 * Displays a color picker field for a settings field
494                 *
495                 * @param array $args settings field args.
496                 */
497                public function callback_color($args)
498                {
499
500                        $value = esc_attr($this->get_option($args['id'], $args['section'], $args['std']));
501                        $size  = isset($args['size']) && ! is_null($args['size']) ? $args['size'] : 'regular';
502
503                        $html  = sprintf('<input type="text" class="%1$s-text wp-color-picker-field" id="%2$s[%3$s]" name="%2$s[%3$s]" value="%4$s" data-default-color="%5$s" />', $size, $args['section'], $args['id'], $value, $args['std']);
504                        $html .= $this->get_field_description($args);
505
506                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
507                }
508
509                /**
510                 * Displays a select box for creating the pages select box.
511                 *
512                 * @param array $args Settings field args.
513                 */
514                public function callback_pages($args)
515                {
516
517                        $dropdown_args = array(
518                                'selected' => esc_attr($this->get_option($args['id'], $args['section'], $args['std'])),
519                                'name'     => $args['section'] . '[' . $args['id'] . ']',
520                                'id'       => $args['section'] . '[' . $args['id'] . ']',
521                                'echo'     => 0,
522                        );
523                        $html          = wp_dropdown_pages($dropdown_args); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
524                        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
525                }
526
527                /**
528                 * Sanitize callback for Settings API.
529                 *
530                 * @param [type] $options The settings options.
531                 * @return array
532                 */
533                public function sanitize_options($options)
534                {
535
536                        if (! $options) {
537                                return $options;
538                        }
539
540                        foreach ($options as $option_slug => $option_value) {
541                                $sanitize_callback = $this->get_sanitize_callback($option_slug);
542
543                                // If callback is set, call it.
544                                if ($sanitize_callback) {
545                                        $options[$option_slug] = call_user_func($sanitize_callback, $option_value);
546                                        continue;
547                                }
548                        }
549
550                        return $options;
551                }
552
553                /**
554                 * Get sanitization callback for given option slug.
555                 *
556                 * @param string $slug Option slug.
557                 *
558                 * @return mixed String or bool false.
559                 */
560                public function get_sanitize_callback($slug = '')
561                {
562                        if (empty($slug)) {
563                                return false;
564                        }
565
566                        // Iterate over registered fields and see if we can find proper callback.
567                        foreach ($this->settings_fields as $section => $options) {
568                                foreach ($options as $option) {
569                                        if ($option['name'] !== $slug) {
570                                                continue;
571                                        }
572
573                                        // Return the callback name.
574                                        return isset($option['sanitize_callback']) && is_callable($option['sanitize_callback']) ? $option['sanitize_callback'] : false;
575                                }
576                        }
577
578                        return false;
579                }
580
581                /**
582                 * Get the value of a settings field.
583                 *
584                 * @param string $option Settings field name.
585                 * @param string $section The section name this field belongs to.
586                 * @param string $default_value Default text if it's not found.
587                 * @return string
588                 */
589                public function get_option($option, $section, $default_value = '')
590                {
591
592                        $options = get_option($section);
593
594                        if (isset($options[$option])) {
595                                return $options[$option];
596                        }
597
598                        return $default_value;
599                }
600
601                /**
602                 * Show navigations as tab
603                 *
604                 * Shows all the settings section labels as tab
605                 */
606                public function show_navigation()
607                {
608                        $html = '<div class="nav-tab-wrapper">';
609
610                        $count = count($this->settings_sections);
611
612                        // don't show the navigation if only one section exists.
613                        if (1 === $count) {
614                                return;
615                        }
616
617                        foreach ($this->settings_sections as $tab) {
618
619                                if (array_key_exists('icon', $tab)) {
620                                        $html .= sprintf('<a href="#%1$s" class="nav-tab" id="%1$s-tab"><i class="%3$s"></i>%2$s</a>', $tab['id'], $tab['title'], $tab['icon']);
621                                } else {
622                                        $html .= sprintf('<a href="#%1$s" class="nav-tab" id="%1$s-tab">%2$s</a>', $tab['id'], $tab['title']);
623                                }
624                        }
625
626                        $html .= '</div>';
627
628                        echo wp_kses_post($html);
629                }
630
631                /**
632                 * Prints out all settings sections added to a particular settings page.
633                 *
634                 * @param [type] $page The settings page.
635                 * @return void
636                 */
637                public function do_settings_sections($page)
638                {
639                        global $wp_settings_sections, $wp_settings_fields;
640
641                        if (! isset($wp_settings_sections[$page])) {
642                                return;
643                        }
644
645                        foreach ((array) $wp_settings_sections[$page] as $section) {
646
647                                if ($section['title']) {
648                                        echo '<h2>' . esc_html($section['title']) . '</h2>';
649                                }
650
651                                if ($section['callback']) {
652                                        call_user_func($section['callback'], $section);
653                                }
654
655                                if (! isset($wp_settings_fields) || ! isset($wp_settings_fields[$page]) || ! isset($wp_settings_fields[$page][$section['id']])) {
656                                        continue;
657                                }
658                                echo '<div class="form-addons-container">';
659                                $this->do_settings_fields($page, $section['id']);
660                                echo '</div>';
661                        }
662                }
663
664                /**
665                 * Print out the settings fields for a particular settings section.
666                 *
667                 * Part of the Settings API. Use this in a settings page to output
668                 * a specific section. Should normally be called by do_settings_sections()
669                 * rather than directly.
670                 *
671                 * @global $wp_settings_fields Storage array of settings fields and their pages/sections.
672                 *
673                 * @since 2.7.0
674                 *
675                 * @param string $page Slug title of the admin page whose settings fields you want to show.
676                 * @param string $section Slug title of the settings section whose fields you want to show.
677                 */
678                public function do_settings_fields($page, $section)
679                {
680                        global $wp_settings_fields;
681
682                        if (! isset($wp_settings_fields[$page][$section])) {
683                                return;
684                        }
685
686                        foreach ((array) $wp_settings_fields[$page][$section] as $field) {
687
688                                $class = '';
689
690                                if ('section_title' === $field['args']['type']) {
691                                        $class = 'form-addon form-addons-section-title';
692                                } elseif (! empty($field['args']['class'])) {
693                                        $class = 'form-addon ' . esc_attr($field['args']['class']);
694                                }
695
696                                if ('premium' === $field['args']['type']) {
697
698                                        echo '<div class="' . esc_attr($class) . '">';
699                                        echo '<div class="form-addon-inner">';
700                                        echo '<a class="addon-pro-link" target="_blank" href="' . esc_url($field['args']['options']) . '"></a>';
701                                        echo '<div class="addon-name">';
702                                        if (array_key_exists('icon', $field['args'])) {
703                                                if ($field['args']['icon']) {
704                                                        echo '<i class="' . esc_attr($field['args']['icon']) . '"></i>';
705                                                }
706                                        }
707                                        echo '<h3 class="addon-title">' . esc_html($field['title']) . '</h3>';
708                                        if ($field['args']['desc']) {
709                                                echo '<span class="addon-desc">' . esc_html($field['args']['desc']) . '</span>';
710                                        }
711
712                                        echo '</div>';
713
714                                        echo '<div class="addon-switch addon-pro-label">';
715                                        echo esc_html__('Pro', 'wpb-elementor-addons');
716                                        echo '</div>';
717                                        echo '</div>';
718                                        echo '</div>';
719                                } else {
720
721                                        echo '<div class="' . esc_attr($class) . '">';
722                                        echo '<div class="form-addon-inner">';
723                                        echo '<div class="addon-name">';
724                                        if (array_key_exists('icon', $field['args'])) {
725                                                if ($field['args']['icon']) {
726                                                        echo '<i class="' . esc_attr($field['args']['icon']) . '"></i>';
727                                                }
728                                        }
729                                        echo '<h3 class="addon-title">' . esc_html($field['title']) . '</h3>';
730                                        if ($field['args']['desc']) {
731                                                echo '<span class="addon-desc">' . esc_html($field['args']['desc']) . '</span>';
732                                        }
733
734                                        echo '</div>';
735
736                                        echo '<div class="addon-switch">';
737                                        call_user_func($field['callback'], $field['args']);
738                                        echo '</div>';
739                                        echo '</div>';
740                                        echo '</div>';
741                                }
742                        }
743                }
744
745                /**
746                 * Show the section settings forms
747                 *
748                 * This function displays every sections in a different form
749                 */
750                public function show_forms()
751                {
752?>
753                        <div class="metabox-holder">
754                                <?php foreach ($this->settings_sections as $form) { ?>
755                                        <div id="<?php echo esc_attr($form['id']); ?>" class="group" style="display: none;">
756                                                <form method="post" action="options.php">
757                                                        <div class="wpb-ea-settings-submit wpb-ea-settings-submit-top">
758                                                                <div class="wpb-ea-settings-submit-top-inner">
759                                                                        <div class="wpb-ea-settings-title">
760                                                                                <h3>WPB Elementor Addons Settings</h3>
761                                                                                <p>Use this code for 10% off for the <a href="https://wpbean.com/elementor-addons/" target="_blank">premium addons</a> - NewCustomer</p>
762                                                                        </div>
763                                                                        <?php submit_button(); ?>
764                                                                </div>
765                                                        </div>
766                                                        <?php
767                                                        do_action('wsa_form_top_' . $form['id'], $form);
768                                                        settings_fields($form['id']);
769
770                                                        if (array_key_exists('addons', $form)) {
771                                                                $this->do_settings_sections($form['id']);
772                                                        } else {
773                                                                do_settings_sections($form['id']);
774                                                        }
775
776                                                        do_action('wsa_form_bottom_' . $form['id'], $form);
777                                                        if (isset($this->settings_fields[$form['id']])) :
778                                                        ?>
779                                                                <div class="wpb-ea-settings-submit">
780                                                                        <?php submit_button(); ?>
781                                                                </div>
782                                                        <?php endif; ?>
783                                                </form>
784                                        </div>
785                                <?php } ?>
786                        </div>
787                <?php
788                        $this->script();
789                }
790
791                /**
792                 * Tabbable JavaScript codes & Initiate Color Picker
793                 *
794                 * This code uses localstorage for displaying active tabs
795                 */
796                public function script()
797                {
798                ?>
799                        <script>
800                                jQuery(document).ready(function($) {
801                                        // toggle
802                                        $('.cb-value').click(function() {
803                                                var mainParent = $(this).parent('.toggle-btn');
804                                                if ($(mainParent).find('input.cb-value').is(':checked')) {
805                                                        $(mainParent).addClass('active');
806                                                } else {
807                                                        $(mainParent).removeClass('active');
808                                                }
809                                        });
810
811                                        // Settings sticky top
812                                        $(document).ready(function() {
813                                                var stickyTop = $('.wpb-ea-settings-submit-top').offset().top;
814
815                                                $(window).scroll(function() {
816                                                        var windowTop = $(window).scrollTop();
817                                                        if (stickyTop < windowTop && $(".wpb-ea-settings-wrap").height() + $(".wpb-ea-settings-wrap").offset().top - $(".wpb-ea-settings-submit-top").height() > windowTop) {
818                                                                $('.wpb-ea-settings-submit-top').addClass('wpb-ea-settings-submit-top-fixed');
819                                                        } else {
820                                                                $('.wpb-ea-settings-submit-top').removeClass('wpb-ea-settings-submit-top-fixed');
821                                                        }
822                                                });
823                                        });
824
825                                        //Initiate Color Picker
826                                        $('.wp-color-picker-field').wpColorPicker();
827
828                                        // Switches option sections
829                                        $('.group').hide();
830                                        var activetab = '';
831                                        if (typeof(localStorage) != 'undefined') {
832                                                activetab = localStorage.getItem("activetab");
833                                        }
834
835                                        //if url has section id as hash then set it as active or override the current local storage value
836                                        if (window.location.hash) {
837                                                activetab = window.location.hash;
838                                                if (typeof(localStorage) != 'undefined') {
839                                                        localStorage.setItem("activetab", activetab);
840                                                }
841                                        }
842
843                                        if (activetab != '' && $(activetab).length) {
844                                                $(activetab).fadeIn();
845                                        } else {
846                                                $('.group:first').fadeIn();
847                                        }
848                                        $('.group .collapsed').each(function() {
849                                                $(this).find('input:checked').parent().parent().parent().nextAll().each(
850                                                        function() {
851                                                                if ($(this).hasClass('last')) {
852                                                                        $(this).removeClass('hidden');
853                                                                        return false;
854                                                                }
855                                                                $(this).filter('.hidden').removeClass('hidden');
856                                                        });
857                                        });
858
859                                        if (activetab != '' && $(activetab + '-tab').length) {
860                                                $(activetab + '-tab').addClass('nav-tab-active');
861                                        } else {
862                                                $('.nav-tab-wrapper a:first').addClass('nav-tab-active');
863                                        }
864                                        $('.nav-tab-wrapper a').click(function(evt) {
865                                                $('.nav-tab-wrapper a').removeClass('nav-tab-active');
866                                                $(this).addClass('nav-tab-active').blur();
867                                                var clicked_group = $(this).attr('href');
868                                                if (typeof(localStorage) != 'undefined') {
869                                                        localStorage.setItem("activetab", $(this).attr('href'));
870                                                }
871                                                $('.group').hide();
872                                                $(clicked_group).fadeIn();
873                                                evt.preventDefault();
874                                        });
875
876                                        $('.wpsa-browse').on('click', function(event) {
877                                                event.preventDefault();
878
879                                                var self = $(this);
880
881                                                // Create the media frame.
882                                                var file_frame = wp.media.frames.file_frame = wp.media({
883                                                        title: self.data('uploader_title'),
884                                                        button: {
885                                                                text: self.data('uploader_button_text'),
886                                                        },
887                                                        multiple: false
888                                                });
889
890                                                file_frame.on('select', function() {
891                                                        attachment = file_frame.state().get('selection').first().toJSON();
892                                                        self.prev('.wpsa-url').val(attachment.url).change();
893                                                });
894
895                                                // Finally, open the modal
896                                                file_frame.open();
897                                        });
898                                });
899                        </script>
900                        <?php
901                        $this->add_style();
902                }
903
904                /**
905                 * Add Style.
906                 *
907                 * @return void
908                 */
909                public function add_style()
910                {
911                        global $wp_version;
912                        if (version_compare($wp_version, '3.8', '<=')) :
913                        ?>
914                                <style type="text/css">
915                                        /** WordPress 3.8 Fix **/
916                                        .form-table th {
917                                                padding: 20px 10px;
918                                        }
919
920                                        #wpbody-content .metabox-holder {
921                                                padding-top: 5px;
922                                        }
923                                </style>
924<?php
925                        endif;
926                }
927        }
928
929endif;
Note: See TracBrowser for help on using the repository browser.