PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.11
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.11
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Admin / Settings.php

Settings.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.11, at includes/Admin/Settings.php

797 lines 40.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Extension Factory
5 *
6 * @package NotificationX\Extensions
7 */
8
9 namespace NotificationX\Admin;
10
11 use NotificationX\Core\Database;
12 use NotificationX\Core\Modules;
13 use NotificationX\Core\REST;
14 use NotificationX\Core\Rules;
15 use NotificationX\Core\Upgrader;
16 use NotificationX\Extensions\GlobalFields;
17 use NotificationX\GetInstance;
18 use NotificationX\NotificationX;
19 use UsabilityDynamics\Settings as UsabilityDynamicsSettings;
20 use NotificationX\Core\Helper;
21
22 /**
23 * Settings Class
24 * @method static Settings get_instance($args = null)
25 */
26 class Settings extends UsabilityDynamicsSettings {
27 /**
28 * Instance of Settings
29 *
30 * @var Settings
31 */
32 use GetInstance;
33
34 protected $wpdb;
35 protected $defaults = [];
36
37 /**
38 * Initially Invoked when initialized.
39 *
40 * @hook init
41 */
42 public function __construct( $args ) {
43 global $wpdb;
44 $this->wpdb = $wpdb;
45 parent::__construct( $args );
46 add_action( 'init', [ $this, 'init' ] );
47 // add_filter('user_has_cap', array($this, 'allow_admin'), 10, 4);
48 }
49
50 /**
51 * Called from Admin::init();
52 * NotificationX
53 *
54 * @return void
55 */
56 public function init() {
57 // add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']);
58 add_action( 'admin_menu', [ $this, 'menu' ], 25 );
59 add_filter( 'nx_branding_url', array( $this, 'nx_branding_url' ), 12 );
60 add_filter( 'nx_rest_miscellaneous', array( $this, 'miscellaneous' ), 10, 2 );
61 }
62
63 /**
64 * This method is responsible for Admin Menu of
65 * NotificationX
66 *
67 * @return void
68 */
69 public function menu() {
70 add_submenu_page( 'nx-admin', 'Settings', 'Settings', 'edit_notificationx_settings', 'nx-settings', [ Admin::get_instance(), 'views' ], 3 );
71 }
72
73 /**
74 * Register scripts and styles.
75 *
76 * @param string $hook
77 * @return void
78 */
79 function get_form_data() {
80 $data = NotificationX::get_instance()->normalize( $this->settings_form() );
81 $settings = apply_filters('nx_settings_page_settings', $this->get( 'settings', [] ));
82
83 $data['current_page'] = 'settings';
84 $data['rest'] = REST::get_instance()->rest_data();
85 $data['savedValues'] = $settings;
86 $data['values'] = $settings;
87 $data['assets'] = [
88 'admin' => NOTIFICATIONX_ADMIN_URL,
89 'public' => NOTIFICATIONX_PUBLIC_URL,
90 ];
91 return $data;
92 }
93
94 /**
95 * Admin Views
96 *
97 * @return void
98 */
99 public function settings_page() {
100 include_once NOTIFICATIONX_INCLUDES . 'Admin/views/settings.views.php';
101 }
102
103
104 public function settings_form( $nx_id = 0 ) {
105 do_action( 'nx_before_settings_fields' );
106 $wp_roles = GlobalFields::get_instance()->normalize_fields( $this->get_roles() );
107 $site_name = get_bloginfo( 'name' );
108 $settings = [
109 'id' => 'notificationx_metabox_wrapper',
110 'title' => __( 'NotificationX', 'notificationx' ),
111 'object_types' => array( 'notificationx' ),
112 'context' => 'normal',
113 'priority' => 'high',
114 'show_header' => false,
115 'tabnumber' => true,
116 'layout' => 'horizontal',
117 'is_pro_active' => NotificationX::get_instance()->is_pro(),
118 'cus_imp_limit' => Settings::get_instance()->get('settings.custom_notification_import_limit', 100),
119 'is_wpml_active'=> Helper::is_wpml_setup(),
120 'config' => [
121 'active' => 'tab-general',
122 'sidebar' => false,
123 'title' => false,
124 ],
125 'submit' => [
126 'show' => true,
127 'label' => __( 'Save Settings', 'notificationx' ),
128 'class' => 'save-settings',
129 ],
130 'tabs' => apply_filters('nx_settings_tab', [
131 'tab-general' => apply_filters('nx_settings_tab_general', [
132 'id' => 'tab-general',
133 'label' => __( 'General', 'notificationx' ),
134 'classes' => 'tab-general',
135 'priority' => 10,
136 'fields' => [
137 'section-modules' => [
138 'label' => __( 'Modules', 'notificationx' ),
139 'name' => 'section-modules',
140 'type' => 'section',
141 'classes' => NotificationX::get_instance()->is_pro() ? 'section-modules pro-activated' : 'section-modules',
142 'fields' => [
143 'modules' => [
144 // 'label' => "Modules",
145 'name' => 'modules',
146 'type' => 'toggle',
147 'multiple' => true,
148 'default' => true,
149 'style' => [
150 'type' => 'card',
151 'column' => 3,
152 ],
153 'options' => array_values( Modules::get_instance()->get_all() ),
154 ],
155 ],
156 ],
157 ],
158 ]
159 ),
160 'advanced-settings-tab' => apply_filters('nx_settings_tab_advanced', [
161 'id' => 'tab-advanced-settings',
162 'label' => __( 'Advanced Settings', 'notificationx' ),
163 'classes' => 'tab-advanced-settings',
164 'priority' => 20,
165 'fields' => [
166 'advanced-general' => [
167 'name' => 'advanced-general',
168 'type' => 'section',
169 'label' => __( 'General', 'notificationx' ),
170 'priority' => 5,
171 'fields' => array(
172 'enable_rest_api' => [
173 'name' => "enable_rest_api",
174 'type' => 'checkbox',
175 'label' => __('Enable REST API', 'notificationx'),
176 'default' => false,
177 'priority' => 10,
178 'description' => __( 'Forcefully enable anonymous REST API for NotificationX.', 'notificationx' ),
179 ],
180 'omit_credentials' => [
181 'name' => "omit_credentials",
182 'type' => 'checkbox',
183 'label' => __('Exclude Credentials', 'notificationx'),
184 'default' => true,
185 'priority' => 15,
186 'description' => __( 'Enabling it will remove cookies, HTTP authentication entries, and TLS client certificates from API calls on the frontend.', 'notificationx' ),
187 'help' => __( 'Note: Recommended if you use any caching plugins.', 'notificationx' ),
188 ],
189 'notification_image_size' => [
190 'name' => "notification_image_size",
191 'type' => 'select',
192 'label' => __('Image Size', 'notificationx'),
193 'default' => '100_100',
194 'options' => GlobalFields::get_instance()->normalize_fields([
195 '100_100' => __('100 x 100 px', 'notificationx'),
196 '200_200' => __('200 x 200 px', 'notificationx'),
197 '300_300' => __('300 x 300 px', 'notificationx'),
198 '400_400' => __('400 x 400 px', 'notificationx'),
199 '500_500' => __('500 x 500 px', 'notificationx'),
200 ]),
201 'priority' => 20,
202 'help' => __('Select the size for your notification image.', 'notificationx'),
203 ],
204 'custom_notification_import_limit' => [
205 'name' => "custom_notification_import_limit",
206 'type' => 'number',
207 'label' => __('Custom Notification Import Limit', 'notificationx'),
208 'default' => 100,
209 'priority' => 25,
210 ],
211 ),
212 ],
213 'powered_by' => [
214 'name' => 'powered_by',
215 'label' => __( 'Powered By', 'notificationx' ),
216 'type' => 'section',
217 'priority' => 15,
218 'fields' => [
219 'disable_powered_by' => [
220 'type' => 'checkbox',
221 'label' => __( 'Disable Powered By', 'notificationx' ),
222 'name' => 'disable_powered_by',
223 'default' => 0,
224 'priority' => 10,
225 'description' => __( 'Click, if you want to disable powered by text from notification', 'notificationx' ),
226 ],
227 ],
228 ],
229 'role_management' => array(
230 'name' => 'role_management',
231 'type' => 'section',
232 'label' => __( 'Role Management', 'notificationx' ),
233 'priority' => 30,
234 'fields' => array(
235 'notification_view_roles' => array(
236 'name' => 'notification_view_roles',
237 'type' => 'select',
238 'label' => __( 'Who Can View Notification?', 'notificationx' ),
239 'priority' => 1,
240 'multiple' => true,
241 'is_pro' => true,
242 'disable' => true,
243 'default' => [ 'administrator' ],
244 'options' => $wp_roles,
245 ),
246 'notification_roles' => array(
247 'name' => 'notification_roles',
248 'type' => 'select',
249 'label' => __( 'Who Can Create Notification?', 'notificationx' ),
250 'priority' => 1,
251 'multiple' => true,
252 'is_pro' => true,
253 'disable' => true,
254 'default' => [ 'administrator' ],
255 'options' => $wp_roles,
256 ),
257 'settings_roles' => array(
258 'name' => 'settings_roles',
259 'type' => 'select',
260 'label' => __( 'Who Can Edit Settings?', 'notificationx' ),
261 'priority' => 2,
262 'multiple' => true,
263 'is_pro' => true,
264 'disable' => true,
265 'default' => [ 'administrator' ],
266 'options' => $wp_roles,
267 ),
268 'analytics_roles' => array(
269 'name' => 'analytics_roles',
270 'type' => 'select',
271 'label' => __( 'Who Can Check Analytics?', 'notificationx' ),
272 'priority' => 3,
273 'multiple' => true,
274 'is_pro' => true,
275 'disable' => true,
276 'default' => [ 'administrator' ],
277 'options' => $wp_roles,
278 ),
279 ),
280 ),
281 ],
282 ]
283 ),
284 'email-analytics-reporting' => apply_filters('nx_settings_tab_email_analytics', [
285 'label' => __( 'Analytics & Reporting', 'notificationx' ),
286 'id' => 'email-analytics-reporting',
287 'classes' => 'tab-advanced-settings',
288 'priority' => 30,
289 'fields' => [
290 'analytics' => array(
291 'name' => 'analytics',
292 'priority' => 10,
293 'type' => 'section',
294 'label' => __( 'Analytics', 'notificationx' ),
295 'fields' => array(
296 'enable_analytics' => array(
297 'name' => 'enable_analytics',
298 'type' => 'checkbox',
299 'label' => __( 'Enable Analytics', 'notificationx' ),
300 'default' => true,
301 'priority' => 0,
302 ),
303 'disable_dashboard_widget' => array(
304 'name' => 'disable_dashboard_widget',
305 'type' => 'checkbox',
306 'label' => __( 'Disable Dashboard Widget', 'notificationx' ),
307 'default' => false,
308 'priority' => 5,
309 'description' => __( 'Click, if you want to disable dashboard widget of analytics only.', 'notificationx' ),
310 'rules' => Rules::is( 'enable_analytics', true ),
311 ),
312 'analytics_from' => array(
313 'name' => 'analytics_from',
314 'type' => 'select',
315 'label' => __( 'Analytics From', 'notificationx' ),
316 'options' => GlobalFields::get_instance()->normalize_fields(array(
317 'everyone' => __( 'Everyone', 'notificationx' ),
318 'guests' => __( 'Guests Only', 'notificationx' ),
319 'registered_users' => __( 'Registered Users Only', 'notificationx' ),
320 )
321 ),
322 'default' => 'everyone',
323 'priority' => 10,
324 'rules' => Rules::is( 'enable_analytics', true ),
325 ),
326 'exclude_bot_analytics' => array(
327 'name' => 'exclude_bot_analytics',
328 'type' => 'checkbox',
329 'label' => __( 'Exclude Bot Analytics', 'notificationx' ),
330 'default' => true,
331 'priority' => 15,
332 'description' => __( 'Select if you want to exclude bot analytics.', 'notificationx' ),
333 'rules' => Rules::is( 'enable_analytics', true ),
334 ),
335 ),
336 ),
337 'entries_mail_receiver' => apply_filters( 'nx_settings_entries_mail_receiver', array(
338 'name' => 'entries_mail_receiver',
339 'priority' => 15,
340 'type' => 'section',
341 'label' => __( 'Entries Mail Receiver', 'notificationx' ),
342 'fields' => array(
343 'enable_entries_mail' => array(
344 'name' => 'enable_entries_mail',
345 'type' => 'checkbox',
346 'label' => __( 'Enable Entries Mail Receiver', 'notificationx' ),
347 'default' => false,
348 'priority' => 0,
349 'description' => __( 'Send an email notification whenever a new entry is received.', 'notificationx' ),
350 ),
351 'entries_mail_email' => array(
352 'name' => 'entries_mail_email',
353 'type' => 'text',
354 'label' => __( 'Entry Recipient Email', 'notificationx' ),
355 'default' => get_option( 'admin_email' ),
356 'priority' => 1,
357 'rules' => Rules::is( 'enable_entries_mail', true ),
358 ),
359 'entries_mail_subject' => array(
360 'name' => 'entries_mail_subject',
361 'type' => 'text',
362 'label' => __( 'Email Subject', 'notificationx' ),
363 'default' => sprintf( __( 'New Entry Received on "%s"', 'notificationx' ), $site_name ),
364 'priority' => 2,
365 'rules' => Rules::is( 'enable_entries_mail', true ),
366 ),
367 'test_entries_mail' => array(
368 'name' => 'test_entries_mail',
369 'label' => __( 'Test Mail', 'notificationx' ),
370 'text' => __( 'Send Test Email', 'notificationx' ),
371 'type' => 'button',
372 'priority' => 3,
373 'rules' => Rules::is( 'enable_entries_mail', true ),
374 'ajax' => [
375 'on' => 'click',
376 'api' => '/notificationx/v1/entries-mail-test',
377 'data' => [
378 'entries_mail_email' => '@entries_mail_email',
379 'entries_mail_subject' => '@entries_mail_subject',
380 ],
381 'swal' => [
382 'text' => __( 'Test email sent successfully.', 'notificationx' ),
383 'icon' => 'success',
384 'autoClose' => 2000,
385 ],
386 ],
387 ),
388 ),
389 ) ),
390 'email_reporting' => array(
391 'name' => 'email_reporting',
392 'priority' => 20,
393 'type' => 'section',
394 'label' => __( 'Reporting', 'notificationx' ),
395 'rules' => Rules::is( 'enable_analytics', true ),
396 'fields' => array(
397 'disable_reporting' => array(
398 'name' => 'disable_reporting',
399 'label' => __( 'Disable Reporting', 'notificationx' ),
400 'type' => 'checkbox',
401 'priority' => 0,
402 'default' => 0,
403 ),
404 'reporting_frequency' => array(
405 'name' => 'reporting_frequency',
406 'type' => 'select',
407 'label' => __( 'Reporting Frequency', 'notificationx' ),
408 'default' => 'nx_weekly',
409 'is_pro' => true,
410 'priority' => 1,
411 'disable' => true,
412 'options' => GlobalFields::get_instance()->normalize_fields(array(
413 'nx_daily' => __( 'Once Daily', 'notificationx' ),
414 'nx_weekly' => __( 'Once Weekly', 'notificationx' ),
415 'nx_monthly' => __( 'Once Monthly', 'notificationx' ),
416 )
417 ),
418 'rules' => Rules::is( 'disable_reporting', false ),
419 'info' => InfoTooltipManager::get_instance()->render('reporting_frequency'),
420 ),
421 'reporting_monthly_help_text' => array(
422 'name' => 'reporting_monthly_help_text',
423 'type' => 'message',
424 'class' => 'nx-warning',
425 'priority' => 1.5,
426 'message' => __( 'It will be triggered on the first day of next month.', 'notificationx' ),
427 'rules' => Rules::is( 'reporting_frequency', 'nx_monthly' ),
428 ),
429 'reporting_day' => array(
430 'name' => 'reporting_day',
431 'type' => 'select',
432 'label' => __( 'Select Reporting Day', 'notificationx' ),
433 'default' => 'monday',
434 'priority' => 2,
435 'options' => GlobalFields::get_instance()->normalize_fields(array(
436 'sunday' => __( 'Sunday', 'notificationx' ),
437 'monday' => __( 'Monday', 'notificationx' ),
438 'tuesday' => __( 'Tuesday', 'notificationx' ),
439 'wednesday' => __( 'Wednesday', 'notificationx' ),
440 'thursday' => __( 'Thursday', 'notificationx' ),
441 'friday' => __( 'Friday', 'notificationx' ),
442 )
443 ),
444 'description' => __( 'Select a day for email report.', 'notificationx' ),
445 'rules' => Rules::logicalRule([
446 Rules::is( 'reporting_frequency', 'nx_weekly' ),
447 Rules::is( 'disable_reporting', false ),
448 ]
449 ),
450 ),
451 'reporting_email' => array(
452 'name' => 'reporting_email',
453 'type' => 'text',
454 'label' => __( 'Reporting Email', 'notificationx' ),
455 'default' => get_option( 'admin_email' ),
456 'priority' => 3,
457 'rules' => Rules::is( 'disable_reporting', false ),
458 ),
459 'reporting_subject' => array(
460 'name' => 'reporting_subject',
461 'type' => 'text',
462 'label' => __( 'Reporting Email Subject', 'notificationx' ),
463 'default' => sprintf( __( 'Weekly Engagement Summary of “%s”', 'notificationx' ), $site_name ),
464 'priority' => 4,
465 'disable' => true,
466 'rules' => Rules::is( 'disable_reporting', false ),
467 ),
468 'test_report' => array(
469 'name' => 'test_report',
470 'label' => __( 'Reporting Test', 'notificationx' ),
471 'text' => __( 'Test Report', 'notificationx' ),
472 'type' => 'button',
473 'priority' => 5,
474 'rules' => Rules::is( 'disable_reporting', false ),
475 'ajax' => [
476 'on' => 'click',
477 'api' => '/notificationx/v1/reporting-test',
478 'data' => [
479 'disable_reporting' => '@disable_reporting',
480 'reporting_subject' => '@reporting_subject',
481 'reporting_email' => '@reporting_email',
482 'reporting_day' => '@reporting_day',
483 'reporting_frequency' => '@reporting_frequency',
484 ],
485 'swal' => [
486 'text' => __( 'Successfully Sent a Test Report in Your Email.', 'notificationx' ),
487 'icon' => 'success',
488 'autoClose' => 2000,
489 ],
490 ],
491 ),
492 ),
493 ),
494 ],
495 ]
496 ),
497 'entries' => apply_filters('nx_settings_tab_entries', [
498 'label' => __( 'Entries', 'notificationx' ),
499 'id' => 'entries',
500 'classes' => 'tab-advanced-settings',
501 'priority' => 35,
502 'fields' => [
503 'feedback_entries' => array(
504 'name' => 'feedback_entries',
505 'priority' => 10,
506 'type' => 'feedback-entries',
507 'label' => __( 'Analytics', 'notificationx' ),
508 ),
509 ],
510 ]
511 ),
512 'cache_settings_tab' => apply_filters('nx_settings_tab_cache', [
513 'id' => 'tab-cache-settings',
514 'label' => __( 'Cache Settings', 'notificationx' ),
515 'priority' => 40,
516 'fields' => [
517 'cache_settings' => array(
518 'name' => 'cache_settings',
519 'type' => 'section',
520 'label' => __( 'Cache Settings', 'notificationx' ),
521 'priority' => 5,
522 'fields' => array(
523 'cache_limit' => array(
524 'name' => 'cache_limit',
525 'type' => 'text',
526 'label' => __( 'Cache Limit', 'notificationx' ),
527 'description' => __( 'Number of Notification Data to be saved in Database.', 'notificationx' ),
528 'default' => '100',
529 'priority' => 1,
530 ),
531 'download_stats_cache_duration' => array(
532 'name' => 'download_stats_cache_duration',
533 'type' => 'text',
534 'label' => __( 'Download Stats Cache Duration', 'notificationx' ),
535 'description' => __( 'Minutes (Schedule Duration to fetch new data).', 'notificationx' ),
536 'default' => 3,
537 'priority' => 2,
538 ),
539 'reviews_cache_duration' => array(
540 'name' => 'reviews_cache_duration',
541 'type' => 'text',
542 'label' => __( 'Reviews Cache Duration', 'notificationx' ),
543 'description' => __( 'Minutes (Schedule Duration to fetch new data).', 'notificationx' ),
544 'default' => '3',
545 'priority' => 3,
546 ),
547 ),
548 ),
549 ],
550 ]
551 ),
552 'tab-miscellaneous-settings' => apply_filters('nx_settings_tab_miscellaneous', [
553 'id' => 'tab-miscellaneous-settings',
554 'label' => __( 'Miscellaneous', 'notificationx' ),
555 'priority' => 50,
556 'fields' => [
557 ],
558 ]
559 ),
560 ]
561 ),
562 ];
563
564 if ( defined( 'NX_DEBUG' ) && NX_DEBUG ) {
565 $settings['tabs']['tab-miscellaneous-settings']['fields'][] = array(
566 'name' => 'danger-zone',
567 'type' => 'section',
568 'label' => __( 'Danger Zone', 'notificationx' ),
569 'priority' => 200,
570 'fields' => array(
571 'delete-settings' => array(
572 'name' => 'delete-settings',
573 'label' => __( 'Delete Settings', 'notificationx' ),
574 'text' => __( 'Delete Settings', 'notificationx' ),
575 'type' => 'button',
576 'priority' => 15,
577 'ajax' => [
578 'on' => 'click',
579 'reload' => true,
580 'api' => '/notificationx/v1/settings',
581 'data' => [
582 'delete_settings' => true,
583 ],
584 'swal' => [
585 'text' => __( 'Successfully deleted Settings.', 'notificationx' ),
586 'icon' => 'deleted',
587 'autoClose' => 2000,
588 ],
589 ],
590 ),
591 'delete-transient' => array(
592 'name' => 'delete-transient',
593 'label' => __( 'Delete Transient', 'notificationx' ),
594 'text' => __( 'Delete Transient', 'notificationx' ),
595 'type' => 'button',
596 'priority' => 25,
597 'ajax' => [
598 'on' => 'click',
599 'reload' => true,
600 'api' => '/notificationx/v1/miscellaneous',
601 'data' => [
602 'delete_transient' => true,
603 ],
604 'swal' => [
605 'text' => __( 'Successfully deleted Transient.', 'notificationx' ),
606 'icon' => 'deleted',
607 'autoClose' => 2000,
608 ],
609 ],
610 ),
611 ),
612 );
613 }
614
615 $settings = apply_filters( 'nx_settings_configs', $settings );
616 return $settings;
617 }
618
619 /**
620 * Get All Roles
621 * dynamically
622 *
623 * @return array
624 */
625 public function get_roles() {
626 $roles = wp_roles()->role_names;
627 unset( $roles['subscriber'] );
628 return $roles;
629 }
630 /**
631 * Get All Roles
632 * dynamically
633 * user_has_cap
634 *
635 * @return array
636 */
637 public function allow_admin( $allcaps, $caps, $args, $user ) {
638 $nx_roles = [
639 'read_notificationx',
640 'edit_notificationx',
641 'edit_notificationx_settings',
642 'read_notificationx_analytics',
643 ];
644 if ( ! empty( $caps[0] ) && array_key_exists( 'administrator', $allcaps ) && ! array_key_exists( $caps[0], $allcaps ) && in_array( $caps[0], $nx_roles ) ) {
645 $role = get_role( 'administrator' );
646 $role->add_cap( $caps[0] );
647 $allcaps[ $caps[0] ] = true;
648 }
649 return $allcaps;
650 }
651
652 public function save_settings( $settings ) {
653 if ( ! current_user_can( 'edit_notificationx_settings' ) ) {
654 return false;
655 }
656 $remove_before_save = [
657 'empty',
658 ];
659
660 $roles = $this->get_selected_roles( $settings );
661 $settings = array_merge( $settings, $roles );
662
663 $settings = apply_filters( 'nx_settings', $settings );
664
665 foreach ( $remove_before_save as $key ) {
666 if ( isset( $settings[ $key ] ) ) {
667 unset( $settings[ $key ] );
668 }
669 }
670 // need this to ensure saved value don't return empty array instead of object.
671 if ( ! empty( $settings['delete_settings'] ) ) {
672 $settings = [ 'empty' => true ];
673 }
674
675 $this->set( 'settings', $settings );
676 delete_transient( 'nx_get_field_names' );
677 do_action( 'nx_settings_saved', $settings );
678 return true;
679 }
680
681 public function get_role_map( $settings = [] ) {
682 if ( empty( $settings ) || count( $settings ) == 1 ) {
683 $settings = $this->get_selected_roles();
684 }
685 return [
686 'read_notificationx' => [
687 'roles' => $settings['notification_view_roles'],
688 'map' => [],
689 ],
690 'edit_notificationx' => [
691 'roles' => $settings['notification_roles'],
692 'map' => [ 'read_notificationx' ],
693 ],
694 'edit_notificationx_settings' => [
695 'roles' => $settings['settings_roles'],
696 'map' => [ 'read_notificationx' ],
697 ],
698 'read_notificationx_analytics' => [
699 'roles' => $settings['analytics_roles'],
700 'map' => [ 'read_notificationx' ],
701 ],
702 ];
703 }
704
705 public function get_selected_roles( $settings = [] ) {
706 $notification_view_roles = isset( $settings['notification_view_roles'] ) ? $settings['notification_view_roles'] : self::get_instance()->get( 'settings.notification_view_roles', [] );
707 $notification_roles = isset( $settings['notification_roles'] ) ? $settings['notification_roles'] : self::get_instance()->get( 'settings.notification_roles', [] );
708 $settings_roles = isset( $settings['settings_roles'] ) ? $settings['settings_roles'] : self::get_instance()->get( 'settings.settings_roles', [] );
709 $analytics_roles = isset( $settings['analytics_roles'] ) ? $settings['analytics_roles'] : self::get_instance()->get( 'settings.analytics_roles', [] );
710
711 if ( ! is_array( $notification_view_roles ) ) {
712 $notification_view_roles = [ $notification_view_roles ];
713 }
714 if ( ! is_array( $notification_roles ) ) {
715 $notification_roles = [ $notification_roles ];
716 }
717 if ( ! is_array( $settings_roles ) ) {
718 $settings_roles = [ $settings_roles ];
719 }
720 if ( ! is_array( $analytics_roles ) ) {
721 $analytics_roles = [ $analytics_roles ];
722 }
723
724 return apply_filters('nx_role_management', [
725 'notification_view_roles' => array_values( array_unique( array_merge( [ 'administrator' ], $notification_view_roles ) ) ),
726 'notification_roles' => array_values( array_unique( array_merge( [ 'administrator' ], $notification_roles ) ) ),
727 'settings_roles' => array_values( array_unique( array_merge( [ 'administrator' ], $settings_roles ) ) ),
728 'analytics_roles' => array_values( array_unique( array_merge( [ 'administrator' ], $analytics_roles ) ) ),
729 ]
730 );
731 }
732
733 /**
734 * Getter for options
735 *
736 * @param bool|\UsabilityDynamics\type $key
737 *
738 * @param bool $default
739 *
740 * @return type
741 */
742 public function Xget( $key = false, $default = false ) {
743 if ( empty( $default ) && strpos( $key, 'settings.' ) === 0 ) {
744 // getting default value from settings form.
745 $_key = str_replace( 'settings.', '', $key );
746 if ( empty( $default ) ) {
747 $defaults = $this->get_defaults();
748 $default = ! empty( $defaults[ $_key ] ) ? $defaults[ $_key ]['default'] : $default;
749 }
750 }
751 return parent::get( $key, $default );
752 }
753
754 /**
755 * Setter for options
756 *
757 * @param string|\UsabilityDynamics\type $key
758 * @param bool|\UsabilityDynamics\type $value
759 * @param bool $bypass_validation
760 *
761 * @internal param bool|\UsabilityDynamics\type $force_save
762 *
763 * @return \UsabilityDynamics\Settings
764 */
765 public function set( $key = '', $value = false, $bypass_validation = false ) {
766 if ( strpos( $key, '.' ) === false ) {
767 parent::flush( false, $key );
768 }
769 return parent::set( $key, $value, $bypass_validation );
770 }
771
772 public function nx_branding_url( $link ) {
773 $affiliate_link = $this->get( 'settings.affiliate_link' );
774 if ( ! empty( $affiliate_link ) ) {
775 $link = $affiliate_link;
776 }
777 return $link;
778 }
779
780 // @todo maybe remove
781 public function get_defaults( $key = null ) {
782 if ( empty( $this->defaults ) ) {
783 $tabs = $this->settings_form();
784 $this->defaults = NotificationX::get_instance()->get_field_names( $tabs['tabs'] );
785 }
786 return $this->defaults;
787 }
788
789 public function miscellaneous( $return, $params ) {
790 if ( !empty( $params['delete_transient'] ) && $params['delete_transient'] ) {
791 Upgrader::get_instance()->clear_transient();
792 $return = true;
793 }
794 return $return;
795 }
796 }
797