PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.12
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.12
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.12, at includes/Admin/Settings.php

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