PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.3.7
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.3.7
7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / hustle-settings-admin-ajax.php
wordpress-popup / inc Last commit date
display-conditions 5 years ago front 5 years ago helpers 5 years ago metas 5 years ago palettes 5 years ago provider 5 years ago providers 5 years ago templates 5 years ago update 5 years ago class-hustle-admin-page-abstract.php 5 years ago class-hustle-condition-factory.php 6 years ago class-hustle-dashboard-admin.php 5 years ago class-hustle-data.php 5 years ago class-hustle-db.php 6 years ago class-hustle-module-admin.php 5 years ago class-hustle-module-collection.php 5 years ago class-hustle-module-decorator.php 5 years ago class-hustle-module-page-abstract.php 5 years ago class-hustle-notifications.php 5 years ago class-hustle-settings-admin.php 5 years ago class-hustle-upsell-page.php 5 years ago class-hustle-wp-dashboard-page.php 5 years ago hustle-collection.php 6 years ago hustle-deletion.php 5 years ago hustle-embedded-admin.php 6 years ago hustle-entries-admin.php 5 years ago hustle-entry-model.php 5 years ago hustle-general-data-protection.php 6 years ago hustle-init.php 5 years ago hustle-mail.php 5 years ago hustle-meta.php 5 years ago hustle-migration.php 5 years ago hustle-model.php 5 years ago hustle-module-model.php 5 years ago hustle-module-widget-legacy.php 5 years ago hustle-module-widget.php 5 years ago hustle-modules-common-admin-ajax.php 5 years ago hustle-popup-admin.php 6 years ago hustle-providers-admin.php 5 years ago hustle-providers.php 6 years ago hustle-settings-admin-ajax.php 5 years ago hustle-settings-page.php 5 years ago hustle-slidein-admin.php 6 years ago hustle-sshare-admin.php 5 years ago hustle-sshare-model.php 5 years ago hustle-tracking-model.php 5 years ago opt-in-geo.php 5 years ago opt-in-utils.php 5 years ago opt-in-wpmudev-api.php 6 years ago
hustle-settings-admin-ajax.php
789 lines
1 <?php
2
3 class Hustle_Settings_Admin_Ajax {
4 private $_hustle;
5
6 private $_admin;
7
8 public function __construct() {
9
10 add_action( 'wp_ajax_hustle_remove_ips', array( $this, 'remove_ips_from_tables' ) );
11 add_action( 'wp_ajax_hustle_reset_settings', array( $this, 'reset_settings' ) );
12
13 // Return the recaptcha script for preview
14 add_action( 'wp_ajax_hustle_load_recaptcha_preview', array( $this, 'load_recaptcha_preview' ) );
15
16 // Color Palette tab actions.
17 add_action( 'wp_ajax_hustle_handle_palette_actions', array( $this, 'handle_palette_actions' ) );
18
19 // Handle saving settings.
20 add_action( 'wp_ajax_hustle_save_settings', array( $this, 'ajax_settings_save' ) );
21 }
22
23 /**
24 * Filter IPs
25 *
26 * @since 4.0
27 * @param string $ip_string
28 * @return array valid IPs
29 */
30 private function filter_ips( $ip_string ) {
31
32 // Create an array with their values.
33 $ip_array = preg_split( '/[\s,]+/', $ip_string, null, PREG_SPLIT_NO_EMPTY );
34
35 // Remove from the array the IPs that are not valid IPs.
36 foreach ( $ip_array as $key => $ip ) {
37 if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
38 unset( $ip_array[ $key ] );
39 continue;
40 }
41 }
42
43 return $ip_array;
44 }
45
46 /**
47 * Reset the plugin
48 *
49 * @since 4.0.3
50 */
51 public function reset_settings() {
52 Opt_In_Utils::validate_ajax_call( 'hustle_reset_settings' );
53 Opt_In_Utils::is_user_allowed_ajax( 'hustle_edit_settings' );
54
55 /**
56 * Fires before Settings reset
57 *
58 * @since 4.0.3
59 */
60 do_action( 'hustle_before_reset_settings' );
61
62 // Delete starts here.
63 Hustle_Deletion::hustle_delete_custom_options();
64 Hustle_Deletion::hustle_delete_addon_options();
65 Hustle_Deletion::hustle_clear_module_views();
66 Hustle_Deletion::hustle_clear_module_submissions();
67 Hustle_Deletion::hustle_clear_modules();
68
69 /**
70 * Fires after Settings reset
71 *
72 * @since 4.0.3
73 */
74 do_action( 'hustle_after_reset_settings' );
75
76 }
77
78 /**
79 * Remove the requested IPs from views and conversions on batches.
80 *
81 * @since 3.0.6
82 */
83 public function remove_ips_from_tables() {
84 Opt_In_Utils::validate_ajax_call( 'hustle_remove_ips' );
85 Opt_In_Utils::is_user_allowed_ajax( 'hustle_edit_settings' );
86
87 /**
88 * From Tracking
89 */
90 $range = filter_input( INPUT_POST, 'range', FILTER_SANITIZE_STRING );
91 $tracking = Hustle_Tracking_Model::get_instance();
92 $hustle_entries_admin = new Hustle_Entry_Model();
93
94 if ( 'all' === $range ) {
95 $tracking->set_null_on_all_ips();
96 $hustle_entries_admin->delete_all_ips();
97 $message = esc_html__( 'All IP addresses have been successfully deleted from the database.', 'hustle' );
98
99 } else {
100 $values = filter_input( INPUT_POST, 'ips', FILTER_SANITIZE_STRING );
101 if ( ! empty( $values ) ) {
102 $values = preg_replace( '/ /', '', $values );
103 $r = preg_split( '/[\r\n]/', $values );
104 $ios = array();
105 foreach ( $r as $one ) {
106 $is_valid = ( filter_var( $one, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) || filter_var( $one, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) );
107
108 if ( $is_valid ) {
109 $ips[] = $one;
110 continue;
111 }
112 $a = explode( '-', $one );
113 if ( 2 !== count( $a ) ) {
114 continue;
115 }
116 $is_valid = filter_var( $a[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
117 if ( ! $is_valid ) {
118 continue;
119 }
120 $is_valid = filter_var( $a[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
121 if ( ! $is_valid ) {
122 continue;
123 }
124 $ips[] = array_map( 'ip2long', $a );
125 }
126 $tracking->set_null_on_selected_ips( $ips );
127 $hustle_entries_admin->delete_selected_ips( $ips );
128 $message = esc_html__( 'All selected IP addresses have been successfully deleted from the database.', 'hustle' );
129
130 } else {
131 $message = esc_html__( 'No IPs were deleted. You must provide at least one IP.', 'hustle' );
132 }
133 }
134
135 wp_send_json_success( array( 'message' => $message ) );
136 }
137
138 /**
139 * Saves the global privacy settings.
140 *
141 * @since 4.0
142 */
143 public function save_privacy_settings() {
144
145 $filter_args = array(
146 'ip_tracking' => FILTER_SANITIZE_STRING,
147 // Account erasure request
148 'retain_sub_on_erasure' => FILTER_SANITIZE_STRING,
149 // Submissions retention
150 'retain_submission_forever' => FILTER_SANITIZE_STRING,
151 'submissions_retention_number' => FILTER_SANITIZE_NUMBER_INT,
152 'submissions_retention_number_unit' => FILTER_SANITIZE_STRING,
153 // IPs retention
154 'retain_ip_forever' => FILTER_SANITIZE_STRING,
155 'ip_retention_number' => FILTER_SANITIZE_NUMBER_INT,
156 'ip_retention_number_unit' => FILTER_SANITIZE_STRING,
157 // Tracking retention
158 'retain_tracking_forever' => FILTER_SANITIZE_STRING,
159 'tracking_retention_number' => FILTER_SANITIZE_NUMBER_INT,
160 'tracking_retention_number_unit' => FILTER_SANITIZE_STRING,
161 );
162 $data = filter_input_array( INPUT_POST, $filter_args, false );
163
164 $stored_settings = Hustle_Settings_Admin::get_privacy_settings();
165
166 $new_settings = array_merge( $stored_settings, $data );
167
168 Hustle_Settings_Admin::update_hustle_settings( $new_settings, 'privacy' );
169 wp_send_json_success();
170 }
171
172 /**
173 * Saves the global privacy settings.
174 *
175 * @since 4.0.2
176 */
177 public function save_data_settings() {
178
179 $reset_settings_uninstall = filter_input( INPUT_POST, 'reset_settings_uninstall', FILTER_SANITIZE_STRING );
180
181 $value = array(
182 'reset_settings_uninstall' => '1' === $reset_settings_uninstall ? '1' : '0',
183 );
184
185 Hustle_Settings_Admin::update_hustle_settings( $value, 'data' );
186 wp_send_json_success();
187 }
188
189 /**
190 * Save the data under the Top Metric tab.
191 *
192 * @since 4.0.0
193 */
194 private function save_top_metrics_settings() {
195 $data = filter_input( INPUT_POST, 'metrics', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
196 $metrics = ! empty( $data ) ? array_filter( $data ) : array();
197
198 // Only 3 metrics can be selected. No more.
199 if ( 3 < count( $metrics ) ) {
200 wp_send_json_error(
201 array(
202 'notification' => array(
203 'status' => 'error',
204 'message' => esc_html__( "You can't select more than 3 metrics.", 'hustle' ),
205 ),
206 )
207 );
208 }
209
210 $allowed_metric_keys = array(
211 'average_conversion_rate',
212 'today_conversions',
213 'last_week_conversions',
214 'last_month_conversions',
215 'total_conversions',
216 'most_conversions',
217 'inactive_modules_count',
218 'total_modules_count',
219 );
220
221 $data_to_store = array();
222 foreach ( $metrics as $name ) {
223 if ( in_array( $name, $allowed_metric_keys, true ) ) {
224 $data_to_store[] = $name;
225 }
226 }
227
228 Hustle_Settings_Admin::update_hustle_settings( $data_to_store, 'top_metrics' );
229 wp_send_json_success();
230 }
231
232 /**
233 * Save the reCaptcha settings.
234 *
235 * @since 4.0
236 */
237 private function save_recaptcha_settings() {
238
239 $settings_to_save = array(
240 // V2 Checkbox
241 'v2_checkbox_site_key' => '',
242 'v2_checkbox_secret_key' => '',
243 // V2 Invisible
244 'v2_invisible_site_key' => '',
245 'v2_invisible_secret_key' => '',
246 // V3 Recaptcha
247 'v3_recaptcha_site_key' => '',
248 'v3_recaptcha_secret_key' => '',
249 'language' => 'automatic',
250 );
251
252 foreach ( $settings_to_save as $key => $value ) {
253 $incoming_setting = filter_input( INPUT_POST, $key, FILTER_SANITIZE_STRING );
254
255 if ( $incoming_setting ) {
256 $settings_to_save[ $key ] = trim( $incoming_setting );
257 }
258 }
259
260 // Keep these keys stored in case the user rolls back to before 4.0.3.
261 $settings_to_save['sitekey'] = $settings_to_save['v2_checkbox_site_key'];
262 $settings_to_save['secret'] = $settings_to_save['v2_checkbox_secret_key'];
263
264 Hustle_Settings_Admin::update_hustle_settings( $settings_to_save, 'recaptcha' );
265
266 wp_send_json_success(
267 array(
268 'notification' => array(
269 'status' => 'success',
270 'message' => esc_html__( 'reCAPTCHA configured successfully. You can now add reCAPTCHA field to your opt-in forms where you want the reCAPTCHA to appear.', 'hustle' ),
271 ),
272 'callback' => 'actionSaveRecaptcha',
273 )
274 );
275 }
276
277 /**
278 * Save the Accessibility settings.
279 *
280 * @since 4.0.0
281 */
282 private function save_accessibility_settings() {
283
284 $accessibility_color = filter_input( INPUT_POST, 'hustle-accessibility-color', FILTER_VALIDATE_BOOLEAN );
285
286 if ( is_null( $accessibility_color ) ) {
287 wp_send_json_error();
288 }
289 $value = array(
290 'accessibility_color' => $accessibility_color,
291 );
292
293 Hustle_Settings_Admin::update_hustle_settings( $value, 'accessibility' );
294
295 wp_send_json_success( array( 'url' => true ) );
296 }
297
298 /**
299 * Save the Unsubscribe settings.
300 *
301 * @since 4.0.0
302 */
303 private function save_unsubscribe_settings() {
304
305 $data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
306 $email_body = wp_json_encode( $data['email_message'] );
307 $sanitized_data = Opt_In_Utils::validate_and_sanitize_fields( $data );
308
309 // Save the messages to be displayed in the unsubscription process.
310 $messages_data = array(
311 'enabled' => isset( $sanitized_data['messages_enabled'] ) ? $sanitized_data['messages_enabled'] : '0',
312 'get_lists_button_text' => $sanitized_data['get_lists_button_text'],
313 'submit_button_text' => $sanitized_data['submit_button_text'],
314 'invalid_email' => $sanitized_data['invalid_email'],
315 'email_not_found' => $sanitized_data['email_not_found'],
316 'invalid_data' => $sanitized_data['invalid_data'],
317 'email_submitted' => $sanitized_data['email_submitted'],
318 'successful_unsubscription' => $sanitized_data['successful_unsubscription'],
319 'email_not_processed' => $sanitized_data['email_not_processed'],
320 );
321
322 // Save the unsubscription email settings.
323 $email_data = array(
324 'enabled' => isset( $sanitized_data['email_enabled'] ) ? $sanitized_data['email_enabled'] : '0',
325 'email_subject' => $sanitized_data['email_subject'],
326 'email_body' => $email_body,
327 );
328
329 $value = array(
330 'messages' => $messages_data,
331 'email' => $email_data,
332 );
333 Hustle_Settings_Admin::update_hustle_settings( $value, 'unsubscribe' );
334
335 wp_send_json_success();
336
337 }
338
339 /**
340 * Return the recaptcha script to be added in the page.
341 * This script changes when the recaptcha's language changes,
342 * so it must be updated on language change when previewing.
343 *
344 * @since 4.0.3
345 */
346 public function load_recaptcha_preview() {
347
348 $source = Hustle_Module_Front::add_recaptcha_script( '', true, true );
349 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
350 $html = '<script src="' . $source . '" async defer></script>';
351
352 wp_send_json_success( $html );
353 }
354
355 /**
356 * Save Hustle settings
357 *
358 * @since 4.0
359 *
360 * @todo Handle error messages
361 */
362 public function ajax_settings_save() {
363 Opt_In_Utils::validate_ajax_call( 'hustle_settings_save' );
364 Opt_In_Utils::is_user_allowed_ajax( 'hustle_edit_settings' );
365
366 $tab = filter_input( INPUT_POST, 'target', FILTER_SANITIZE_STRING );
367
368 switch ( $tab ) {
369 case 'permissions':
370 $this->save_permissions_settings();
371 break;
372
373 case 'general':
374 $this->save_general_settings();
375 break;
376
377 case 'top_metrics':
378 $this->save_top_metrics_settings();
379 break;
380
381 case 'analytics':
382 $this->save_dashboard_analytics_settings();
383 break;
384
385 case 'recaptcha':
386 $this->save_recaptcha_settings();
387 break;
388
389 case 'accessibility':
390 $this->save_accessibility_settings();
391 break;
392
393 case 'unsubscribe':
394 $this->save_unsubscribe_settings();
395 break;
396
397 case 'privacy':
398 $this->save_privacy_settings();
399 break;
400
401 case 'data':
402 $this->save_data_settings();
403 break;
404
405 default:
406 break;
407 }
408
409 // The action is not listed. No one should land here if following the regular plugin's paths.
410 wp_send_json_error(
411 array(
412 'notification' => array(
413 'status' => 'error',
414 'message' => esc_html__( "The action you're trying to perform was not found.", 'hustle' ),
415 ),
416 )
417 );
418 }
419
420
421 /**
422 * Handles saving the "Permissions" settings.
423 *
424 * @since 4.1.0
425 */
426 private function save_permissions_settings() {
427
428 // Handle per module roles. We'll go with per permission next.
429 $current_modules_ids = filter_input( INPUT_POST, 'modules_ids', FILTER_SANITIZE_STRING );
430 $modules_ids = empty( $current_modules_ids ) ? array() : explode( ',', $current_modules_ids );
431
432 if ( ! empty( $modules_ids ) ) {
433 $modules_roles = filter_input( INPUT_POST, 'modules', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY );
434
435 foreach ( $modules_ids as $module_id ) {
436
437 $module = new Hustle_Module_Model( $module_id );
438 if ( ! is_wp_error( $module ) ) {
439
440 $selected_roles = isset( $modules_roles[ $module_id ] ) ? $modules_roles[ $module_id ] : array();
441 $module->update_edit_roles( $selected_roles );
442 }
443 }
444 }
445
446 // Handling per permissions roles here.
447 $filter = array(
448 'filter' => FILTER_SANITIZE_STRING,
449 'flags' => FILTER_REQUIRE_ARRAY,
450 );
451 $filter_options = array(
452 'create' => $filter,
453 'edit_integrations' => $filter,
454 'access_emails' => $filter,
455 'edit_settings' => $filter,
456 );
457 $incoming = filter_input_array( INPUT_POST, $filter_options );
458
459 // If the role can create modules, it can also edit them.
460 $incoming['edit'] = $incoming['create'];
461
462 // Capability related to each incoming input.
463 $hustle_capabilities = array(
464 'create' => 'hustle_create',
465 'edit' => 'hustle_edit_module',
466 'edit_integrations' => 'hustle_edit_integrations',
467 'access_emails' => 'hustle_access_emails',
468 'edit_settings' => 'hustle_edit_settings',
469 );
470
471 $existing_roles = Opt_In_Utils::get_user_roles();
472
473 // Loop through the submitted capabilities.
474 foreach ( $incoming as $capability => $selected_roles ) {
475
476 if ( ! is_array( $selected_roles ) ) {
477 // The filter failed. No roles were selected.
478 $incoming[ $capability ] = array();
479 $selected_roles = array();
480
481 } else {
482
483 // Loop through the selected roles of this capability. Unset any invalid role.
484 foreach ( $selected_roles as $key => $role_slug ) {
485
486 if ( ! isset( $existing_roles[ $role_slug ] ) ) {
487 unset( $incoming[ $capability ][ $key ] );
488 }
489 }
490 }
491
492 // Update roles capabilities.
493 foreach ( $existing_roles as $role_slug => $role_name ) {
494 if ( Opt_In_Utils::is_admin_role( $role_slug ) ) {
495 continue;
496 }
497
498 $role = get_role( $role_slug );
499
500 $cap = $hustle_capabilities[ $capability ];
501 if ( in_array( $role_slug, $selected_roles, true ) ) {
502 // Add capability.
503 $role->add_cap( $cap );
504
505 } else {
506
507 // Check if this role can edit at least one module before removing the cap.
508 if ( 'edit' === $capability ) {
509
510 if ( ! Hustle_Module_Model::can_role_edit_one_module( $role_slug ) ) {
511 // Remove capability.
512 $role->remove_cap( $cap );
513 } else {
514 $role->add_cap( $cap );
515 }
516 } else {
517 // Remove capability.
518 $role->remove_cap( $cap );
519 }
520 }
521 }
522 }
523
524 // Store per permission roles.
525 Hustle_Settings_Admin::update_hustle_settings( $incoming, 'permissions' );
526
527 wp_send_json_success();
528
529 }
530
531 /**
532 * Handles saving the "General" settings.
533 *
534 * @since 4.1.0
535 */
536 private function save_general_settings() {
537
538 // Retrieve the stored data.
539 $stored_values = Hustle_Settings_Admin::get_general_settings();
540
541 // Sanitize the incoming data.
542 foreach ( $stored_values as $key => $value ) {
543 if ( 'sender_email_address' !== $key ) {
544 $new_value = filter_input( INPUT_POST, $key, FILTER_SANITIZE_STRING );
545 } else {
546 $new_value = filter_input( INPUT_POST, $key, FILTER_VALIDATE_EMAIL );
547 }
548
549 // Update it if valid.
550 if ( false !== $new_value && ! is_null( $new_value ) ) {
551 $stored_values[ $key ] = $new_value;
552 }
553 }
554
555 Hustle_Settings_Admin::update_hustle_settings( $stored_values, 'general' );
556 wp_send_json_success();
557 }
558
559 /**
560 * Handles saving the "Dashboard Analytics" settings.
561 *
562 * @since 4.1.0
563 */
564 private function save_dashboard_analytics_settings() {
565
566 $reload = false;
567 $value = Hustle_Settings_Admin::get_hustle_settings( 'analytics' );
568
569 // Handle enable/disable action.
570 $enable_toggled = filter_input( INPUT_POST, 'enabled', FILTER_SANITIZE_STRING );
571 if ( false !== $enable_toggled && ! is_null( $enable_toggled ) ) {
572 $value['enabled'] = $enable_toggled;
573 $reload = true;
574
575 } else {
576
577 // Handle storing the actual settings.
578 $filter_args = array(
579 'modules' => array(
580 'filter' => FILTER_SANITIZE_STRING,
581 'flags' => FILTER_REQUIRE_ARRAY,
582 ),
583 'role' => array(
584 'filter' => FILTER_SANITIZE_STRING,
585 'flags' => FILTER_REQUIRE_ARRAY,
586 ),
587 'title' => FILTER_SANITIZE_STRING,
588 );
589 $filtered_data = filter_input_array( INPUT_POST, $filter_args );
590
591 // Use defaults if the filter fails or the value isn't set.
592 $modules = ! empty( $filtered_data['modules'] ) ? array_filter( $filtered_data['modules'] ) : array();
593 $selected_roles = ! empty( $filtered_data['role'] ) ? $filtered_data['role'] : array();
594 $title = is_string( $filtered_data['title'] ) ? $filtered_data['title'] : '';
595
596 $value = array(
597 'enabled' => '1',
598 'title' => $title,
599 'modules' => $modules,
600 'role' => Opt_In_Utils::get_admin_roles(),
601 );
602
603 // Store the roles if they exist.
604 $roles = Opt_In_Utils::get_user_roles();
605 foreach ( $selected_roles as $role_slug ) {
606 if ( isset( $roles[ $role_slug ] ) ) {
607 $value['role'][ $role_slug ] = $roles[ $role_slug ];
608 }
609 }
610
611 // Update roles capability.
612 foreach ( $roles as $role_key => $role_name ) {
613 $role = get_role( $role_key );
614 if ( Opt_In_Utils::is_admin_role( $role_key ) || ! $role ) {
615 continue;
616 }
617 $cap = 'hustle_analytics';
618 if ( in_array( $role_key, $selected_roles, true ) ) {
619 // add capability.
620 $role->add_cap( $cap );
621 } else {
622 // remove capability.
623 $role->remove_cap( $cap );
624 }
625 }
626 }
627
628 // TODO: delete transient on uninstall.
629 // TODO: get these dynamically.
630 // Delete the transients set for retrieving this data in the WP Dashboard.
631 // These are the same values available in Hustle_Wp_Dashboard_Page::get_analytic_ranges().
632 delete_transient( 'hustle_wp_widget_daily_stats_7' );
633 delete_transient( 'hustle_wp_widget_daily_stats_30' );
634 delete_transient( 'hustle_wp_widget_daily_stats_90' );
635
636 Hustle_Settings_Admin::update_hustle_settings( $value, 'analytics' );
637
638 if ( ! $reload ) {
639 wp_send_json_success();
640 } else {
641 wp_send_json_success( array( 'url' => true ) );
642 }
643 }
644
645 /**
646 * Handle the palette's actions.
647 *
648 * @since 4.0.3
649 */
650 public function handle_palette_actions() {
651
652 Opt_In_Utils::validate_ajax_call( 'hustle_palette_action' );
653 Opt_In_Utils::is_user_allowed_ajax( 'hustle_edit_settings' );
654
655 $palette_id = filter_input( INPUT_POST, 'id', FILTER_SANITIZE_STRING );
656 $action = filter_input( INPUT_POST, 'hustleAction', FILTER_SANITIZE_STRING );
657
658 $args = array(
659 'page' => Hustle_Data::SETTINGS_PAGE,
660 'section' => 'palettes',
661 );
662
663 switch ( $action ) {
664
665 case 'delete':
666 $name = Hustle_Settings_Admin::delete_custom_palette( $palette_id );
667 $args['show-notice'] = 'success';
668 $args['notice'] = 'palette_deleted';
669 $args['deleted-name'] = rawurlencode( $name );
670 break;
671
672 case 'go-to-step':
673 $step = filter_input( INPUT_POST, 'step', FILTER_SANITIZE_STRING );
674
675 if ( '2' === $step ) {
676 $this->action_edit_palette_go_second_step();
677 } else {
678 $id = $this->action_edit_palette_save();
679 $args['show-notice'] = 'success';
680 $args['notice'] = 'palette_saved';
681 $args['saved-id'] = rawurlencode( $id );
682 }
683 break;
684
685 default:
686 break;
687 }
688
689 $url = add_query_arg( $args, 'admin.php' );
690 $response = array( 'url' => $url );
691
692 wp_send_json_success( $response );
693 }
694
695 /**
696 * Palettes -> Edit palette. Handle the action from when going to second step.
697 *
698 * @since 4.0.3
699 */
700 private function action_edit_palette_go_second_step() {
701
702 $palette_slug = filter_input( INPUT_POST, 'slug', FILTER_SANITIZE_STRING );
703
704 if ( $palette_slug ) { // Editing an existing palette.
705
706 $palette_name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
707 $palette_array = Hustle_Palettes_Helper::get_palette_array( $palette_slug );
708 $palette_array['slug'] = $palette_slug;
709 $palette_array['name'] = $palette_name;
710
711 $callback = 'actionOpenEditPalette';
712
713 } else { // Creating a new palette.
714
715 $callback = 'actionGoToSecondStep';
716 $base_source = filter_input( INPUT_POST, 'base_source', FILTER_SANITIZE_STRING );
717
718 if ( 'palette' === $base_source ) {
719 // Use an existing palette as the base.
720 $palette = filter_input( INPUT_POST, 'base_palette', FILTER_SANITIZE_STRING );
721 $palette_array = Hustle_Palettes_Helper::get_palette_array( $palette );
722
723 } else {
724 // Use a module's palette as the base.
725
726 $fallback_palette_name = filter_input( INPUT_POST, 'fallback_palette', FILTER_SANITIZE_STRING );
727 $fallback_palette = Hustle_Palettes_Helper::get_palette_array( $fallback_palette_name );
728
729 $module_id = filter_input( INPUT_POST, 'module_id', FILTER_SANITIZE_STRING );
730
731 $module = new Hustle_Module_Model( $module_id );
732
733 if ( is_wp_error( $module ) ) {
734 $palette_array = $fallback_palette;
735
736 } else {
737 $design = $module->get_design()->to_array();
738
739 // remove option color keys from info modules.
740 if ( 'informational' === $module->module_mode ) {
741 $info = Hustle_Palettes_Helper::get_palette_array( 'info-module' );
742 $design = array_diff_key( $design, $info );
743 }
744
745 $module_palette = array_intersect_key( $design, $fallback_palette );
746 $palette_array = array_merge( $fallback_palette, $module_palette );
747 }
748 }
749 }
750
751 wp_send_json_success(
752 array(
753 'callback' => $callback,
754 'palette_data' => $palette_array,
755 )
756 );
757 }
758
759 /**
760 * Handle action for when saving the palette.
761 *
762 * @since 4.0.3
763 */
764 private function action_edit_palette_save() {
765
766 $palette_slug = filter_input( INPUT_POST, 'slug', FILTER_SANITIZE_STRING );
767 $palette_name = filter_input( INPUT_POST, 'palette_name', FILTER_SANITIZE_STRING );
768
769 // Remove non-palette data.
770 $palette_colors = array_intersect_key( $_POST, Hustle_Palettes_Helper::get_palette_array( 'gray_slate' ) ); // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
771
772 $palette_data = array( 'palette' => $palette_colors );
773
774 if ( $palette_slug ) {
775 // Updating an existing palette.
776 $palette_data['slug'] = $palette_slug;
777
778 } else {
779 // Creating a new one.
780 $palette_data['name'] = $palette_name ? $palette_name : wp_rand();
781 }
782
783 $id = Hustle_Settings_Admin::save_custom_palette( $palette_data );
784
785 return $id;
786 }
787
788 }
789