| 1 |
<?php |
| 2 |
/** |
| 3 |
* SettingsController. |
| 4 |
* php version 5.6 |
| 5 |
* |
| 6 |
* @category SettingsController |
| 7 |
* @package SureTrigger |
| 8 |
* @author BSF <username@example.com> |
| 9 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 |
* @link https://www.brainstormforce.com/ |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace SureTriggers\Controllers; |
| 15 |
|
| 16 |
use SureTriggers\Traits\SingletonLoader; |
| 17 |
|
| 18 |
if ( ! class_exists( 'SettingsController' ) ) : |
| 19 |
/** |
| 20 |
* SettingsController |
| 21 |
* |
| 22 |
* @category SettingsController |
| 23 |
* @package SureTrigger |
| 24 |
* @author BSF <username@example.com> |
| 25 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 26 |
* @link https://www.brainstormforce.com/ |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
class SettingsController { |
| 30 |
|
| 31 |
use SingletonLoader; |
| 32 |
|
| 33 |
/** |
| 34 |
* SettingsController constructor. |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
add_action( 'wp_ajax_suretriggers_save_settings', [ $this, 'suretriggers_save_settings_callback' ] ); |
| 38 |
add_action( 'wp_ajax_nopriv_suretriggers_save_settings', [ $this, 'suretriggers_settings_unauthorized_access' ] ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Save settings. |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function suretriggers_settings_unauthorized_access() { |
| 47 |
wp_send_json_error( [ 'message' => 'Unauthorized access' ] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Save settings. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function suretriggers_save_settings_callback() { |
| 56 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 57 |
wp_send_json_error( [ 'message' => 'Permission denied' ] ); |
| 58 |
} |
| 59 |
if ( ! isset( $_POST['suretriggers_settings_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['suretriggers_settings_nonce'] ) ), 'suretriggers_settings_nonce_action' ) ) { |
| 60 |
wp_send_json_error( [ 'message' => 'Security check failed' ] ); |
| 61 |
} |
| 62 |
$user_id = get_current_user_id(); |
| 63 |
$transient_name = 'suretriggers_settings_save_' . $user_id; |
| 64 |
if ( get_transient( $transient_name ) ) { |
| 65 |
wp_send_json_error( [ 'message' => 'Please wait before submitting again' ] ); |
| 66 |
} |
| 67 |
set_transient( $transient_name, true, 3 ); |
| 68 |
|
| 69 |
// Always reset to empty arrays first. |
| 70 |
$user_ids = []; |
| 71 |
$user_roles = []; |
| 72 |
|
| 73 |
// Process user IDs if present. |
| 74 |
if ( isset( $_POST['st_selected_users'] ) && is_array( $_POST['st_selected_users'] ) ) { |
| 75 |
$user_ids = array_map( 'absint', array_map( 'sanitize_text_field', wp_unslash( $_POST['st_selected_users'] ) ) ); |
| 76 |
} |
| 77 |
|
| 78 |
// Process user roles if present. |
| 79 |
if ( isset( $_POST['st_selected_user_roles'] ) && is_array( $_POST['st_selected_user_roles'] ) ) { |
| 80 |
$user_roles = array_map( 'sanitize_key', array_map( 'sanitize_text_field', wp_unslash( $_POST['st_selected_user_roles'] ) ) ); |
| 81 |
} |
| 82 |
|
| 83 |
// Update options with the processed arrays (empty or with values). |
| 84 |
update_option( 'suretriggers_enabled_users', $user_ids ); |
| 85 |
update_option( 'suretriggers_enabled_user_roles', $user_roles ); |
| 86 |
|
| 87 |
wp_send_json_success( [ 'message' => 'Settings saved successfully' ] ); |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
SettingsController::get_instance(); |
| 92 |
|
| 93 |
endif; |
| 94 |
|