| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sureforms Global Settings. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Global_Settings; |
| 10 |
|
| 11 |
use SRFM\Inc\Global_Settings\Email_Summary; |
| 12 |
use SRFM\Inc\Events_Scheduler; |
| 13 |
use SRFM\Inc\Traits\Get_Instance; |
| 14 |
use SRFM\Inc\Helper; |
| 15 |
use WP_REST_Server; |
| 16 |
use WP_REST_Response; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_Error; |
| 19 |
|
| 20 |
/** |
| 21 |
* Sureforms Global Settings. |
| 22 |
* |
| 23 |
* @since 0.0.1 |
| 24 |
*/ |
| 25 |
class Global_Settings { |
| 26 |
use Get_Instance; |
| 27 |
|
| 28 |
/** |
| 29 |
* Namespace. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $namespace = 'sureforms/v1'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor |
| 37 |
* |
| 38 |
* @since 0.0.1 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add custom API Route submit-form |
| 46 |
* |
| 47 |
* @return void |
| 48 |
* @since 0.0.1 |
| 49 |
*/ |
| 50 |
public function register_custom_endpoint() { |
| 51 |
$sureforms_helper = new Helper(); |
| 52 |
register_rest_route( |
| 53 |
$this->namespace, |
| 54 |
'/srfm-global-settings', |
| 55 |
[ |
| 56 |
'methods' => WP_REST_Server::EDITABLE, |
| 57 |
'callback' => [ $this, 'srfm_save_global_settings' ], |
| 58 |
'permission_callback' => [ $sureforms_helper, 'get_items_permissions_check' ], |
| 59 |
] |
| 60 |
); |
| 61 |
register_rest_route( |
| 62 |
$this->namespace, |
| 63 |
'/srfm-global-settings', |
| 64 |
[ |
| 65 |
'methods' => WP_REST_Server::READABLE, |
| 66 |
'callback' => [ $this, 'srfm_get_general_settings' ], |
| 67 |
'permission_callback' => [ $sureforms_helper, 'get_items_permissions_check' ], |
| 68 |
] |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Save global settings options. |
| 74 |
* |
| 75 |
* @param WP_REST_Request $request Request object. |
| 76 |
* @return WP_REST_Response|WP_Error |
| 77 |
* |
| 78 |
* @since 0.0.1 |
| 79 |
*/ |
| 80 |
public static function srfm_save_global_settings( $request ) { |
| 81 |
|
| 82 |
$nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) ); |
| 83 |
|
| 84 |
if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { |
| 85 |
wp_send_json_error( |
| 86 |
[ |
| 87 |
'data' => __( 'Nonce verification failed.', 'sureforms' ), |
| 88 |
] |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
$setting_options = $request->get_params(); |
| 93 |
|
| 94 |
$tab = $setting_options['srfm_tab']; |
| 95 |
|
| 96 |
unset( $setting_options['srfm_tab'] ); |
| 97 |
|
| 98 |
switch ( $tab ) { |
| 99 |
case 'general-settings': |
| 100 |
$is_option_saved = self::srfm_save_general_settings( $setting_options ); |
| 101 |
break; |
| 102 |
case 'general-settings-dynamic-opt': |
| 103 |
$is_option_saved = self::srfm_save_general_settings_dynamic_opt( $setting_options ); |
| 104 |
break; |
| 105 |
case 'email-settings': |
| 106 |
$is_option_saved = self::srfm_save_email_summary_settings( $setting_options ); |
| 107 |
break; |
| 108 |
case 'security-settings': |
| 109 |
$is_option_saved = self::srfm_save_security_settings( $setting_options ); |
| 110 |
break; |
| 111 |
default: |
| 112 |
$is_option_saved = false; |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! $is_option_saved ) { |
| 117 |
return new WP_Error( 'Error Saving Settings!', 'Global Settings' ); |
| 118 |
} else { |
| 119 |
return new WP_REST_Response( |
| 120 |
[ |
| 121 |
'data' => __( 'Settings Saved Successfully.', 'sureforms' ), |
| 122 |
] |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Save General Settings |
| 129 |
* |
| 130 |
* @param array<mixed> $setting_options Setting options. |
| 131 |
* @return bool |
| 132 |
* @since 0.0.1 |
| 133 |
*/ |
| 134 |
public static function srfm_save_general_settings( $setting_options ) { |
| 135 |
|
| 136 |
$srfm_ip_log = isset( $setting_options['srfm_ip_log'] ) ? $setting_options['srfm_ip_log'] : false; |
| 137 |
$srfm_honeypot = isset( $setting_options['srfm_honeypot'] ) ? $setting_options['srfm_honeypot'] : false; |
| 138 |
$srfm_form_analytics = isset( $setting_options['srfm_form_analytics'] ) ? $setting_options['srfm_form_analytics'] : false; |
| 139 |
|
| 140 |
return update_option( |
| 141 |
'srfm_general_settings_options', |
| 142 |
[ |
| 143 |
'srfm_ip_log' => $srfm_ip_log, |
| 144 |
'srfm_honeypot' => $srfm_honeypot, |
| 145 |
'srfm_form_analytics' => $srfm_form_analytics, |
| 146 |
] |
| 147 |
); |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Save General Settings Dynamic Options |
| 153 |
* |
| 154 |
* @param array<mixed> $setting_options Setting options. |
| 155 |
* @return bool |
| 156 |
* @since 0.0.1 |
| 157 |
*/ |
| 158 |
public static function srfm_save_general_settings_dynamic_opt( $setting_options ) { |
| 159 |
|
| 160 |
$options_names = [ |
| 161 |
'srfm_url_block_required_text' => $setting_options['srfm_url_block_required_text'], |
| 162 |
'srfm_input_block_required_text' => $setting_options['srfm_input_block_required_text'], |
| 163 |
'srfm_input_block_unique_text' => $setting_options['srfm_input_block_unique_text'], |
| 164 |
'srfm_address_block_required_text' => $setting_options['srfm_address_block_required_text'], |
| 165 |
'srfm_phone_block_required_text' => $setting_options['srfm_phone_block_required_text'], |
| 166 |
'srfm_phone_block_unique_text' => $setting_options['srfm_phone_block_unique_text'], |
| 167 |
'srfm_number_block_required_text' => $setting_options['srfm_number_block_required_text'], |
| 168 |
'srfm_textarea_block_required_text' => $setting_options['srfm_textarea_block_required_text'], |
| 169 |
'srfm_multi_choice_block_required_text' => $setting_options['srfm_multi_choice_block_required_text'], |
| 170 |
'srfm_checkbox_block_required_text' => $setting_options['srfm_checkbox_block_required_text'], |
| 171 |
'srfm_gdpr_block_required_text' => $setting_options['srfm_gdpr_block_required_text'], |
| 172 |
'srfm_email_block_required_text' => $setting_options['srfm_email_block_required_text'], |
| 173 |
'srfm_email_block_unique_text' => $setting_options['srfm_email_block_unique_text'], |
| 174 |
'srfm_dropdown_block_required_text' => $setting_options['srfm_dropdown_block_required_text'], |
| 175 |
]; |
| 176 |
|
| 177 |
return update_option( 'get_default_dynamic_block_option', apply_filters( 'srfm_general_dynamic_options_to_save', $options_names, $setting_options ) ); |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Save Email Summary Settings |
| 183 |
* |
| 184 |
* @param array<mixed> $setting_options Setting options. |
| 185 |
* @return bool |
| 186 |
* @since 0.0.1 |
| 187 |
*/ |
| 188 |
public static function srfm_save_email_summary_settings( $setting_options ) { |
| 189 |
|
| 190 |
$srfm_email_summary = isset( $setting_options['srfm_email_summary'] ) ? $setting_options['srfm_email_summary'] : false; |
| 191 |
$srfm_email_sent_to = isset( $setting_options['srfm_email_sent_to'] ) ? $setting_options['srfm_email_sent_to'] : get_option( 'admin_email' ); |
| 192 |
$srfm_schedule_report = isset( $setting_options['srfm_schedule_report'] ) ? $setting_options['srfm_schedule_report'] : 'Monday'; |
| 193 |
|
| 194 |
Events_Scheduler::unschedule_events( 'srfm_weekly_scheduled_events' ); |
| 195 |
|
| 196 |
if ( $srfm_email_summary ) { |
| 197 |
Email_Summary::schedule_weekly_entries_email(); |
| 198 |
} |
| 199 |
|
| 200 |
return update_option( |
| 201 |
'srfm_email_summary_settings_options', |
| 202 |
[ |
| 203 |
'srfm_email_summary' => $srfm_email_summary, |
| 204 |
'srfm_email_sent_to' => $srfm_email_sent_to, |
| 205 |
'srfm_schedule_report' => $srfm_schedule_report, |
| 206 |
] |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Save Security Settings |
| 212 |
* |
| 213 |
* @param array<mixed> $setting_options Setting options. |
| 214 |
* @return bool |
| 215 |
* @since 0.0.1 |
| 216 |
*/ |
| 217 |
public static function srfm_save_security_settings( $setting_options ) { |
| 218 |
|
| 219 |
$srfm_v2_checkbox_site_key = isset( $setting_options['srfm_v2_checkbox_site_key'] ) ? $setting_options['srfm_v2_checkbox_site_key'] : ''; |
| 220 |
$srfm_v2_checkbox_secret_key = isset( $setting_options['srfm_v2_checkbox_secret_key'] ) ? $setting_options['srfm_v2_checkbox_secret_key'] : ''; |
| 221 |
$srfm_v2_invisible_site_key = isset( $setting_options['srfm_v2_invisible_site_key'] ) ? $setting_options['srfm_v2_invisible_site_key'] : ''; |
| 222 |
$srfm_v2_invisible_secret_key = isset( $setting_options['srfm_v2_invisible_secret_key'] ) ? $setting_options['srfm_v2_invisible_secret_key'] : ''; |
| 223 |
$srfm_v3_site_key = isset( $setting_options['srfm_v3_site_key'] ) ? $setting_options['srfm_v3_site_key'] : ''; |
| 224 |
$srfm_v3_secret_key = isset( $setting_options['srfm_v3_secret_key'] ) ? $setting_options['srfm_v3_secret_key'] : ''; |
| 225 |
$srfm_cf_appearance_mode = isset( $setting_options['srfm_cf_appearance_mode'] ) ? $setting_options['srfm_cf_appearance_mode'] : 'auto'; |
| 226 |
$srfm_cf_turnstile_site_key = isset( $setting_options['srfm_cf_turnstile_site_key'] ) ? $setting_options['srfm_cf_turnstile_site_key'] : ''; |
| 227 |
$srfm_cf_turnstile_secret_key = isset( $setting_options['srfm_cf_turnstile_secret_key'] ) ? $setting_options['srfm_cf_turnstile_secret_key'] : ''; |
| 228 |
$srfm_hcaptcha_site_key = ! empty( $setting_options['srfm_hcaptcha_site_key'] ) ? $setting_options['srfm_hcaptcha_site_key'] : ''; |
| 229 |
$srfm_hcaptcha_secret_key = ! empty( $setting_options['srfm_hcaptcha_secret_key'] ) ? $setting_options['srfm_hcaptcha_secret_key'] : ''; |
| 230 |
|
| 231 |
return update_option( |
| 232 |
'srfm_security_settings_options', |
| 233 |
[ |
| 234 |
'srfm_v2_checkbox_site_key' => $srfm_v2_checkbox_site_key, |
| 235 |
'srfm_v2_checkbox_secret_key' => $srfm_v2_checkbox_secret_key, |
| 236 |
'srfm_v2_invisible_site_key' => $srfm_v2_invisible_site_key, |
| 237 |
'srfm_v2_invisible_secret_key' => $srfm_v2_invisible_secret_key, |
| 238 |
'srfm_v3_site_key' => $srfm_v3_site_key, |
| 239 |
'srfm_v3_secret_key' => $srfm_v3_secret_key, |
| 240 |
'srfm_cf_appearance_mode' => $srfm_cf_appearance_mode, |
| 241 |
'srfm_cf_turnstile_site_key' => $srfm_cf_turnstile_site_key, |
| 242 |
'srfm_cf_turnstile_secret_key' => $srfm_cf_turnstile_secret_key, |
| 243 |
'srfm_hcaptcha_site_key' => $srfm_hcaptcha_site_key, |
| 244 |
'srfm_hcaptcha_secret_key' => $srfm_hcaptcha_secret_key, |
| 245 |
] |
| 246 |
); |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Get Settings Form Data |
| 252 |
* |
| 253 |
* @param \WP_REST_Request $request Request object or array containing form data. |
| 254 |
* @return void |
| 255 |
* @since 0.0.1 |
| 256 |
*/ |
| 257 |
public static function srfm_get_general_settings( $request ) { |
| 258 |
|
| 259 |
$nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) ); |
| 260 |
|
| 261 |
if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) { |
| 262 |
wp_send_json_error( |
| 263 |
[ |
| 264 |
'data' => __( 'Nonce verification failed.', 'sureforms' ), |
| 265 |
] |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
$options_to_get = $request->get_param( 'options_to_fetch' ); |
| 270 |
|
| 271 |
$options_to_get = Helper::get_string_value( $options_to_get ); |
| 272 |
|
| 273 |
$options_to_get = explode( ',', $options_to_get ); |
| 274 |
|
| 275 |
$global_setting_options = get_options( $options_to_get ); |
| 276 |
|
| 277 |
if ( empty( $global_setting_options['srfm_general_settings_options'] ) ) { |
| 278 |
$global_setting_options['srfm_general_settings_options'] = [ |
| 279 |
'srfm_ip_log' => false, |
| 280 |
'srfm_honeypot' => false, |
| 281 |
'srfm_form_analytics' => false, |
| 282 |
]; |
| 283 |
} |
| 284 |
if ( empty( $global_setting_options['get_default_dynamic_block_option'] ) ) { |
| 285 |
$global_setting_options['get_default_dynamic_block_option'] = Helper::default_dynamic_block_option(); |
| 286 |
} |
| 287 |
if ( empty( $global_setting_options['srfm_email_summary_settings_options'] ) ) { |
| 288 |
$global_setting_options['srfm_email_summary_settings_options'] = [ |
| 289 |
'srfm_email_summary' => false, |
| 290 |
'srfm_email_sent_to' => get_option( 'admin_email' ), |
| 291 |
'srfm_schedule_report' => 'Monday', |
| 292 |
]; |
| 293 |
} |
| 294 |
if ( empty( $global_setting_options['srfm_security_settings_options'] ) ) { |
| 295 |
$global_setting_options['srfm_security_settings_options'] = [ |
| 296 |
'srfm_v2_checkbox_site_key' => '', |
| 297 |
'srfm_v2_checkbox_secret_key' => '', |
| 298 |
'srfm_v2_invisible_site_key' => '', |
| 299 |
'srfm_v2_invisible_secret_key' => '', |
| 300 |
'srfm_v3_site_key' => '', |
| 301 |
'srfm_v3_secret_key' => '', |
| 302 |
'srfm_cf_appearance_mode' => 'auto', |
| 303 |
'srfm_cf_turnstile_site_key' => '', |
| 304 |
'srfm_cf_turnstile_secret_key' => '', |
| 305 |
'srfm_hcaptcha_site_key' => '', |
| 306 |
'srfm_hcaptcha_secret_key' => '', |
| 307 |
]; |
| 308 |
} |
| 309 |
|
| 310 |
wp_send_json( $global_setting_options ); |
| 311 |
} |
| 312 |
|
| 313 |
} |
| 314 |
|