PluginProbe
Selection Lite / 1.12
Selection Lite v1.12
trunk 1.0 1.1 1.10 1.11 1.12 1.14 1.15 1.16 1.17 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
selection-lite / src / Merkulove / Unity / Settings.php

Settings.php in Selection Lite 1.12, at src/Merkulove/Unity/Settings.php

607 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Selection Lite
4 * Carefully selected Elementor addons bundle, for building the most awesome websites
5 *
6 * @encoding UTF-8
7 * @version 1.12
8 * @copyright (C) 2018-2024 Merkulove ( https://merkulov.design/ ). All rights reserved.
9 * @license GPLv3
10 * @contributors merkulove, vladcherviakov, phoenixmkua, podolianochka, viktorialev01
11 * @support help@merkulov.design
12 **/
13
14 namespace Merkulove\SelectionLite\Unity;
15
16 use Merkulove\SelectionLite\Caster;
17
18 /** Exit if accessed directly. */
19 if ( ! defined( 'ABSPATH' ) ) {
20 header( 'Status: 403 Forbidden' );
21 header( 'HTTP/1.1 403 Forbidden' );
22 exit;
23 }
24
25 /**
26 * SINGLETON: Class used to implement plugin settings.
27 *
28 * @since 1.0
29 *
30 **/
31 final class Settings {
32
33 /**
34 * Plugin settings.
35 *
36 * @since 1.0
37 * @access public
38 * @var array
39 **/
40 public $options = [];
41
42 /**
43 * The one true Settings.
44 *
45 * @since 1.0
46 * @access private
47 * @var Settings
48 **/
49 private static $instance;
50
51 /**
52 * Sets up a new Settings instance.
53 *
54 * @since 1.0
55 * @access private
56 *
57 * @return void
58 **/
59 private function __construct() {
60
61 /** Get plugin settings. */
62 $this->get_options();
63
64 /** Add plugin settings page. */
65 $this->add_settings_page();
66
67 }
68
69 /**
70 * Render Tabs Headers.
71 *
72 * @since 1.0
73 * @access private
74 *
75 * @return void
76 **/
77 private function render_tabs() {
78
79 /** Selected tab key. */
80 $current = $this->get_current_tab();
81
82 /** Tabs array. */
83 $tabs = Plugin::get_tabs();
84
85 /** Render Tabs. */
86 ?>
87 <aside class="mdc-drawer">
88 <div class="mdc-drawer__content">
89 <nav class="mdc-list">
90
91 <?php $this->render_logo(); ?>
92 <hr class="mdc-plugin-menu">
93 <hr class="mdc-list-divider">
94
95 <h6 class="mdc-list-group__subheader"><?php echo esc_html__( 'Plugin settings', 'selection-lite' ) ?></h6>
96 <?php
97
98 foreach ( $tabs as $tab => $value ) {
99
100 /** Skip disabled tabs. */
101 if ( ! Tab::is_tab_enabled( $tab ) ) { continue; }
102
103 /** Prepare CSS classes. */
104 $classes = [];
105 $classes[] = 'mdc-list-item';
106 $classes[] = "mdp-menu-tab-{$tab}";
107
108 /** Mark Active Tab. */
109 if ( $tab === $current ) {
110 $classes[] = 'mdc-list-item--activated';
111 }
112
113 /** Prepare link. */
114 $link = '?page=mdp_selection_lite_settings&tab=' . $tab;
115
116 ?>
117 <a class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" href="<?php echo esc_attr( $link ); ?>">
118 <i class='material-icons mdc-list-item__graphic' aria-hidden='true'><?php echo esc_html( $value['icon'] ); ?></i>
119 <span class='mdc-list-item__text'><?php echo esc_html( $value['label'] ); ?></span>
120 </a>
121 <?php
122
123 }
124
125 /** Helpful links. */
126 $this->support_link();
127
128 /** Create Go Pro Tab. */
129 $this->display_pro();
130
131 ?>
132 </nav>
133 </div>
134 </aside>
135 <?php
136 }
137
138 /**
139 * Displays useful links for an activated and non-activated plugin.
140 *
141 * @since 1.0
142 * @access private
143 *
144 * @return void
145 **/
146 private function support_link() {
147
148 ?>
149 <hr class="mdc-list-divider">
150 <h6 class="mdc-list-group__subheader"><?php echo esc_html__( 'Helpful links', 'selection-lite' ) ?></h6>
151
152 <a class="mdc-list-item" href="https://docs.merkulov.design/tag/selection" target="_blank">
153 <i class="material-icons mdc-list-item__graphic" aria-hidden="true">collections_bookmark</i>
154 <span class="mdc-list-item__text"><?php echo esc_html__( 'Documentation', 'selection-lite' ) ?></span>
155 </a>
156
157 <a class="mdc-list-item" href="https://wordpress.org/support/plugin/selection-lite/" target="_blank">
158 <i class="material-icons mdc-list-item__graphic" aria-hidden="true">mail</i>
159 <span class="mdc-list-item__text"><?php echo esc_html__( 'Get help', 'selection-lite' ) ?></span>
160 </a>
161
162 <a class="mdc-list-item" href="https://wordpress.org/support/plugin/selection-lite/reviews/#new-post" target="_blank">
163 <i class="material-icons mdc-list-item__graphic" aria-hidden="true">thumb_up</i>
164 <span class="mdc-list-item__text"><?php echo esc_html__( 'Rate this plugin', 'selection-lite' ) ?></span>
165 </a>
166
167 <a class="mdc-list-item" href="https://1.envato.market/cc-merkulove" target="_blank">
168 <i class="material-icons mdc-list-item__graphic" aria-hidden="true">store</i>
169 <span class="mdc-list-item__text"><?php echo esc_html__( 'More plugins', 'selection-lite' ) ?></span>
170 </a>
171 <?php
172
173 }
174
175 /**
176 * Display Go Pro button.
177 *
178 * @since 1.0
179 * @access public
180 **/
181 private function display_pro() {
182
183 $go_pro_tab = admin_url( 'admin.php?page=mdp_selection_lite_settings&tab=pro' );
184
185 ?>
186 <hr class="mdc-list-divider">
187 <h6 class="mdc-list-group__subheader"><?php esc_html_e( 'Upgrade License', 'selection-lite' ); ?></h6>
188
189 <a class="mdc-list-item mdc-activation-status activated" href="<?php echo esc_url( $go_pro_tab ); ?>">
190 <i class='material-icons mdc-list-item__graphic' aria-hidden='true'>label_important</i>
191 <span class="mdc-list-item__text"><?php esc_html_e( 'Upgrade to Pro', 'selection-lite' ); ?></span>
192 </a>
193 <?php
194
195 }
196
197 /**
198 * Add plugin settings page.
199 *
200 * @since 1.0
201 * @access public
202 *
203 * @return void
204 **/
205 public function add_settings_page() {
206
207 add_action( 'admin_menu', [ $this, 'add_admin_menu' ], 1000 );
208 add_action( 'admin_init', [ $this, 'settings_init' ] );
209
210 }
211
212 /**
213 * Generate Settings Page.
214 *
215 * @since 1.0
216 * @access public
217 *
218 * @return void
219 **/
220 public function settings_init() {
221
222 /** Add settings foreach tab. */
223 foreach ( Plugin::get_tabs() as $tab_slug => $tab ) {
224
225 /** Skip tabs without handlers. */
226 if ( empty( $tab['class'] ) ) { continue; }
227
228 /** Call add_settings from appropriate class for each tab. */
229 call_user_func( [ $tab['class'], 'get_instance' ] )->add_settings( $tab_slug );
230
231 }
232
233 }
234
235 /**
236 * Add admin menu for plugin settings.
237 *
238 * @since 1.0
239 * @access public
240 *
241 * @return void
242 **/
243 public function add_admin_menu() {
244
245 /** Submenu for Elementor plugins. */
246 if ( 'elementor' === Plugin::get_type() ) {
247
248 $this->add_submenu_elementor();
249
250 /** Root level menu for WordPress plugins. */
251 } else {
252
253 $this->add_menu_wordpress();
254
255 }
256
257 }
258
259 /**
260 * Add admin menu for Elementor plugins.
261 *
262 * @since 1.0
263 * @access private
264 *
265 * @return void
266 **/
267 private function add_submenu_elementor() {
268
269 /** Check if Elementor installed and activated. */
270 $parent = 'options-general.php';
271 if ( did_action( 'elementor/loaded' ) ) {
272 $parent = 'elementor';
273 //$parent = 'edit-comments.php';
274
275 }
276
277 add_submenu_page(
278 $parent,
279 esc_html__( 'Selection Settings', 'selection-lite' ),
280 esc_html__( 'Selection for Lite', 'selection-lite' ),
281 'manage_options',
282 'mdp_selection_lite_settings',
283 [ $this, 'options_page' ]
284 );
285
286 }
287
288 /**
289 * Add admin menu for WordPress plugins.
290 *
291 * @since 1.0
292 * @access private
293 *
294 * @return void
295 **/
296 private function add_menu_wordpress() {
297
298 add_menu_page(
299 esc_html__( 'Selection Settings', 'selection-lite' ),
300 esc_html__( 'Selection Lite', 'selection-lite' ),
301 'manage_options',
302 'mdp_selection_lite_settings',
303 [ $this, 'options_page' ],
304 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHg9IjBweCIgeT0iMHB4IgoJIHZpZXdCb3g9IjAgMCA0OCA0OCIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNDggNDg7IiB4bWw6c3BhY2U9InByZXNlcnZlIj4KPHBhdGggZmlsbD0iI0EwQTVBQiIgZD0iTTI4LDBoLThDOSwwLDAsOSwwLDIwdjhjMCwxMSw5LDIwLDIwLDIwaDhjMTEsMCwyMC05LDIwLTIwdi04QzQ4LDksMzksMCwyOCwweiBNMTEsMTkuM0MxMSwxMy4xLDE3LjIsOCwyNC44LDgKCWMzLjEsMCw2LjIsMC45LDguNiwyLjZjMC4yLDAuMSwwLjQsMC40LDAuNCwwLjdzLTAuMSwwLjYtMC4zLDAuOEwzMiwxMy42Yy0wLjQsMC4zLTAuOSwwLjMtMS4zLDAuMWMtMS43LTEtMy44LTEuNi02LTEuNgoJYy01LjMsMC05LjYsMy4zLTkuNiw3LjNzNC4zLDcuMyw5LjYsNy4zYzAuMywwLDAuNiwwLjEsMC44LDAuM3MwLjMsMC40LDAuMywwLjd2MmMwLDAuNS0wLjQsMS0xLDFDMTcuMiwzMC43LDExLDI1LjYsMTEsMTkuM3oKCSBNMjMuMiw0MGMtMy4xLDAtNi4yLTAuOS04LjYtMi41Yy0wLjMtMC4yLTAuNC0wLjQtMC40LTAuN3MwLjEtMC42LDAuMy0wLjhsMS41LTEuNWMwLjMtMC4zLDAuOS0wLjQsMS4zLTAuMWMxLjcsMS4xLDMuOCwxLjYsNiwxLjYKCWM1LjMsMCw5LjYtMy4zLDkuNi03LjNzLTQuMy03LjMtOS42LTcuM2MtMC4zLDAtMC42LTAuMS0wLjgtMC4zYy0wLjItMC4yLTAuMy0wLjQtMC4zLTAuN3YtMmMwLTAuNiwwLjUtMSwxLTEKCWM3LjYtMC4xLDEzLjgsNSwxMy44LDExLjNDMzcsMzQuOSwzMC44LDQwLDIzLjIsNDB6Ii8+Cjwvc3ZnPgo=',
305 $this->get_admin_menu_position()
306 );
307
308 }
309
310 /**
311 * Calculate admin menu position based on plugin slug value.
312 *
313 * @since 1.0
314 * @access public
315 *
316 * @return string
317 **/
318 private function get_admin_menu_position() {
319
320 $hash = md5( Plugin::get_slug() );
321
322 $int = (int) filter_var( $hash, FILTER_SANITIZE_NUMBER_INT );
323 $int = (int) ( $int / 1000000000000 );
324
325 return '58.' . $int;
326
327 }
328
329 /**
330 * Plugin Settings Page.
331 *
332 * @since 1.0
333 * @access public
334 *
335 * @return void
336 **/
337 public function options_page() {
338
339 /** User rights check. */
340 if ( ! current_user_can( 'manage_options' ) ) { return; } ?>
341
342 <!--suppress HtmlUnknownTarget -->
343 <form action='options.php' method='post'>
344 <div class="wrap">
345 <?php
346
347 /** Render "Settings saved!" message. */
348 $this->render_nags();
349
350 /** Update custom CSS file */
351 Caster::get_instance()->update_custom_css_file();
352
353 /** Render Tabs Headers. */
354 ?><section class="mdp-aside"><?php $this->render_tabs(); ?></section><?php
355
356 /** Render Tabs Body. */
357 ?><section class="mdp-tab-content mdp-tab-name-<?php echo esc_attr( $this->get_current_tab() ) ?>"><?php
358
359 /** Call settings from appropriate class for current tab. */
360 foreach ( Plugin::get_tabs() as $tab_slug => $tab ) {
361
362 /** Work only on current tab. */
363 if ( ! $this->is_tab( $tab_slug ) ) { continue; }
364
365 /** Skip tabs without handlers. */
366 if ( empty( $tab['class'] ) ) { continue; }
367
368 call_user_func( [ $tab['class'], 'get_instance' ] )->do_settings( $tab_slug );
369
370 }
371
372 ?>
373 </section>
374 </div>
375 </form>
376
377 <?php
378 }
379
380 /**
381 * Return current selected tab or first tab.
382 *
383 * @since 1.0
384 * @access private
385 *
386 * @return string
387 **/
388 private function get_current_tab() {
389
390 $tab = key( Plugin::get_tabs() ); // First tab is default tab
391
392 if ( isset ( $_GET['tab'] ) ) {
393
394 if ( isset( $_POST['selection_lite_settings_updated_nonce'] ) ) {
395 if ( ! wp_verify_nonce( $_POST['selection_lite_settings_updated_nonce'], 'selection-lite-settings-updated' ) ) {
396 wp_die( 'Nonce verification failed.' );
397 }
398 }
399
400 $tab = sanitize_key( $_GET['tab'] );
401
402 }
403
404 return $tab;
405 }
406
407 /**
408 * Check if passed tab is current tab and tab is enabled.
409 *
410 * @param string $tab - Tab slug to check.
411 *
412 * @since 1.0
413 * @access private
414 *
415 * @return bool
416 **/
417 private function is_tab( $tab ) {
418
419 $current_tab = $this->get_current_tab();
420
421 return ( $tab === $current_tab ) && Tab::is_tab_enabled( $current_tab );
422
423 }
424
425 /**
426 * Render nags on after settings save.
427 *
428 * @since 1.0
429 * @access private
430 *
431 * @return void
432 **/
433 private function render_nags() {
434
435 /** Exit if this is not settings update. */
436 if ( ! isset( $_GET['settings-updated'] ) ) {
437
438 if ( isset( $_POST['selection_lite_settings_updated_nonce'] ) ) {
439 if ( ! wp_verify_nonce( $_POST['selection_lite_settings_updated_nonce'], 'selection-lite-settings-updated' ) ) {
440 wp_die( 'Nonce verification failed.' );
441 }
442 }
443
444 return;
445 }
446
447 /** Render "Settings Saved" message. */
448 $this->render_nag_saved();
449
450 }
451
452 /**
453 * Render "Settings Saved" message.
454 *
455 * @since 1.0
456 * @access private
457 *
458 * @return void
459 **/
460 private function render_nag_saved() {
461
462 /** Exit if settings saving was not successful. */
463 if ( 'true' !== $_GET['settings-updated'] ) {
464
465 if ( isset( $_POST['selection_lite_settings_updated_nonce'] ) ) {
466 if ( ! wp_verify_nonce( $_POST['selection_lite_settings_updated_nonce'], 'selection-lite-settings-updated' ) ) {
467 wp_die( 'Nonce verification failed.' );
468 }
469 }
470
471 return;
472
473 }
474
475 /** Render "Settings Saved" message. */
476 UI::get_instance()->render_snackbar( esc_html__( 'Settings saved!', 'selection-lite' ) );
477
478 }
479
480 /**
481 * Render logo and Save changes button in plugin settings.
482 *
483 * @since 1.0
484 * @access private
485 *
486 * @return void
487 **/
488 private function render_logo() {
489
490 ?>
491 <div class="mdc-drawer__header mdc-plugin-fixed">
492 <a class="mdc-list-item mdp-plugin-title" href="#">
493 <i class="mdc-list-item__graphic" aria-hidden="true">
494 <img src="<?php echo esc_attr( Plugin::get_url() . 'images/logo-color.svg' ); ?>" alt="<?php esc_html_e( 'SelectionLite', 'selection-lite' ) ?>">
495 </i>
496 <span class="mdc-list-item__text">
497 <?php if ( 'wordpress' === Plugin::get_type() ) : ?>
498 <?php esc_html_e( 'Selection Lite', 'selection-lite' ) ?>
499 <?php else: ?>
500 <?php esc_html_e( 'Selection', 'selection-lite' ) ?>
501 <?php endif; ?>
502 </span>
503 <span class="mdc-list-item__text">
504 <sup>
505 <?php esc_html_e( 'v.', 'selection-lite' ); ?>
506 <?php echo esc_attr( Plugin::get_version() ); ?>
507 </sup>
508 </span>
509 </a>
510 <button type="submit" name="submit" id="submit" class="mdc-button mdc-button--dense mdc-button--raised">
511 <span class="mdc-button__label"><?php esc_html_e( 'Save changes', 'selection-lite' ) ?></span>
512 </button>
513 </div>
514 <?php
515
516 }
517
518 /**
519 * Return settings array with default values.
520 *
521 * @since 1.0
522 * @access public
523 *
524 * @return array
525 **/
526 private function get_default_settings() {
527
528 /** Get all plugin tabs with settings fields. */
529 $tabs = Plugin::get_tabs();
530
531 $default = [];
532
533 /** Collect settings from each tab. */
534 foreach ( $tabs as $tab_slug => $tab ) {
535
536 /** If current tab haven't fields. */
537 if ( empty( $tab['fields'] ) ) { continue; }
538
539 /** Collect default values from each field. */
540 foreach ( $tab['fields'] as $field_slug => $field ) {
541
542 $default[$field_slug] = $field['default'];
543
544 }
545
546 }
547
548 return $default;
549
550 }
551
552 /**
553 * Get plugin settings with default values.
554 *
555 * @since 1.0
556 * @access public
557 *
558 * @return void
559 **/
560 public function get_options() {
561
562 /** Default values. */
563 $defaults = $this->get_default_settings();
564
565 $results = [];
566
567 /** Get all plugin tabs with settings fields. */
568 $tabs = Plugin::get_tabs();
569
570 /** Collect settings from each tab. */
571 foreach ( $tabs as $tab_slug => $tab ) {
572
573 $opt_name = "mdp_selection_lite_{$tab_slug}_settings";
574 $options = get_option( $opt_name );
575 $results = wp_parse_args( $options, $defaults );
576 $defaults = $results;
577
578 }
579
580 $this->options = $results;
581
582 }
583
584 /**
585 * Main Settings Instance.
586 * Insures that only one instance of Settings exists in memory at any one time.
587 *
588 * @static
589 * @since 1.0
590 * @access public
591 *
592 * @return Settings
593 **/
594 public static function get_instance() {
595
596 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) {
597
598 self::$instance = new self;
599
600 }
601
602 return self::$instance;
603
604 }
605
606 }
607