| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get Global Settings Ability. |
| 4 |
* |
| 5 |
* @package sureforms |
| 6 |
* @since 2.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc\Abilities\Settings; |
| 10 |
|
| 11 |
use SRFM\Inc\Abilities\Abstract_Ability; |
| 12 |
use SRFM\Inc\Helper; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Get_Global_Settings ability class. |
| 20 |
* |
| 21 |
* Retrieves SureForms global settings by category. |
| 22 |
* |
| 23 |
* @since 2.6.0 |
| 24 |
*/ |
| 25 |
class Get_Global_Settings extends Abstract_Ability { |
| 26 |
use Settings_Secret_Keys; |
| 27 |
|
| 28 |
/** |
| 29 |
* Sentinel value used to represent masked secret keys in output. |
| 30 |
* |
| 31 |
* Must match the sentinel in Update_Global_Settings for round-trip safety. |
| 32 |
* |
| 33 |
* @since 2.6.0 |
| 34 |
*/ |
| 35 |
private const SECRET_SENTINEL = '@@SRFM_SECRET_UNCHANGED@@'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor. |
| 39 |
* |
| 40 |
* @since 2.6.0 |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
$this->id = 'sureforms/get-global-settings'; |
| 44 |
$this->label = __( 'Get Global Settings', 'sureforms' ); |
| 45 |
$this->description = __( 'Retrieve SureForms global settings. Optionally filter by category: general, validation-messages, email-summary, security.', 'sureforms' ); |
| 46 |
$this->capability = 'manage_options'; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* {@inheritDoc} |
| 51 |
* |
| 52 |
* @since 2.6.0 |
| 53 |
*/ |
| 54 |
public function get_annotations() { |
| 55 |
return [ |
| 56 |
'readonly' => true, |
| 57 |
'destructive' => false, |
| 58 |
'idempotent' => true, |
| 59 |
'priority' => 1.0, |
| 60 |
'openWorldHint' => false, |
| 61 |
]; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* {@inheritDoc} |
| 66 |
* |
| 67 |
* @since 2.6.0 |
| 68 |
*/ |
| 69 |
public function get_input_schema() { |
| 70 |
return [ |
| 71 |
'type' => 'object', |
| 72 |
'additionalProperties' => false, |
| 73 |
'properties' => [ |
| 74 |
'categories' => [ |
| 75 |
'type' => 'array', |
| 76 |
'description' => __( 'Setting categories to retrieve. Omit for all categories.', 'sureforms' ), |
| 77 |
'items' => [ |
| 78 |
'type' => 'string', |
| 79 |
'enum' => [ 'general', 'validation-messages', 'email-summary', 'security' ], |
| 80 |
], |
| 81 |
], |
| 82 |
], |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* {@inheritDoc} |
| 88 |
* |
| 89 |
* @since 2.6.0 |
| 90 |
*/ |
| 91 |
public function get_output_schema() { |
| 92 |
return [ |
| 93 |
'type' => 'object', |
| 94 |
'properties' => [ |
| 95 |
'general' => [ 'type' => 'object' ], |
| 96 |
'validation-messages' => [ 'type' => 'object' ], |
| 97 |
'email-summary' => [ 'type' => 'object' ], |
| 98 |
'security' => [ 'type' => 'object' ], |
| 99 |
], |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Execute the get-global-settings ability. |
| 105 |
* |
| 106 |
* @param array<string,mixed> $input Validated input data. |
| 107 |
* @since 2.6.0 |
| 108 |
* @return array<string,mixed>|\WP_Error |
| 109 |
*/ |
| 110 |
public function execute( $input ) { |
| 111 |
$categories = ! empty( $input['categories'] ) && is_array( $input['categories'] ) |
| 112 |
? array_map( 'sanitize_text_field', $input['categories'] ) |
| 113 |
: [ 'general', 'validation-messages', 'email-summary', 'security' ]; |
| 114 |
|
| 115 |
$result = []; |
| 116 |
|
| 117 |
foreach ( $categories as $category ) { |
| 118 |
switch ( $category ) { |
| 119 |
case 'general': |
| 120 |
$result['general'] = $this->get_general_settings(); |
| 121 |
break; |
| 122 |
case 'validation-messages': |
| 123 |
$result['validation-messages'] = $this->get_validation_messages(); |
| 124 |
break; |
| 125 |
case 'email-summary': |
| 126 |
$result['email-summary'] = $this->get_email_summary_settings(); |
| 127 |
break; |
| 128 |
case 'security': |
| 129 |
$result['security'] = $this->get_security_settings(); |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $result; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get general settings. |
| 139 |
* |
| 140 |
* @since 2.6.0 |
| 141 |
* @return array<string,mixed> |
| 142 |
*/ |
| 143 |
private function get_general_settings() { |
| 144 |
$settings = get_option( 'srfm_general_settings_options', [] ); |
| 145 |
|
| 146 |
if ( empty( $settings ) || ! is_array( $settings ) ) { |
| 147 |
$settings = [ |
| 148 |
'srfm_ip_log' => false, |
| 149 |
'srfm_form_analytics' => false, |
| 150 |
'srfm_admin_notification' => true, |
| 151 |
'srfm_form_views_tracking' => false, |
| 152 |
'srfm_enable_logs' => true, |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! isset( $settings['srfm_admin_notification'] ) ) { |
| 157 |
$settings['srfm_admin_notification'] = true; |
| 158 |
} |
| 159 |
|
| 160 |
if ( ! isset( $settings['srfm_form_views_tracking'] ) ) { |
| 161 |
$settings['srfm_form_views_tracking'] = false; |
| 162 |
} |
| 163 |
|
| 164 |
if ( ! isset( $settings['srfm_enable_logs'] ) ) { |
| 165 |
$settings['srfm_enable_logs'] = true; |
| 166 |
} |
| 167 |
|
| 168 |
$settings['srfm_bsf_analytics'] = 'yes' === get_option( 'sureforms_usage_optin', false ); |
| 169 |
|
| 170 |
return $settings; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Get validation messages settings. |
| 175 |
* |
| 176 |
* @since 2.6.0 |
| 177 |
* @return array<string,mixed> |
| 178 |
*/ |
| 179 |
private function get_validation_messages() { |
| 180 |
$settings = get_option( 'srfm_default_dynamic_block_option', [] ); |
| 181 |
|
| 182 |
if ( empty( $settings ) || ! is_array( $settings ) ) { |
| 183 |
$settings = Helper::default_dynamic_block_option(); |
| 184 |
} |
| 185 |
|
| 186 |
return $settings; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get email summary settings. |
| 191 |
* |
| 192 |
* @since 2.6.0 |
| 193 |
* @return array<string,mixed> |
| 194 |
*/ |
| 195 |
private function get_email_summary_settings() { |
| 196 |
$settings = get_option( 'srfm_email_summary_settings_options', [] ); |
| 197 |
|
| 198 |
if ( empty( $settings ) || ! is_array( $settings ) ) { |
| 199 |
$settings = [ |
| 200 |
'srfm_email_summary' => false, |
| 201 |
'srfm_email_sent_to' => get_option( 'admin_email' ), |
| 202 |
'srfm_schedule_report' => __( 'Monday', 'sureforms' ), |
| 203 |
]; |
| 204 |
} |
| 205 |
|
| 206 |
return $settings; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get security settings with secret keys masked. |
| 211 |
* |
| 212 |
* @since 2.6.0 |
| 213 |
* @return array<string,mixed> |
| 214 |
*/ |
| 215 |
private function get_security_settings() { |
| 216 |
$settings = get_option( 'srfm_security_settings_options', [] ); |
| 217 |
|
| 218 |
if ( empty( $settings ) || ! is_array( $settings ) ) { |
| 219 |
$settings = [ |
| 220 |
'srfm_v2_checkbox_site_key' => '', |
| 221 |
'srfm_v2_checkbox_secret_key' => '', |
| 222 |
'srfm_v2_invisible_site_key' => '', |
| 223 |
'srfm_v2_invisible_secret_key' => '', |
| 224 |
'srfm_v3_site_key' => '', |
| 225 |
'srfm_v3_secret_key' => '', |
| 226 |
'srfm_cf_appearance_mode' => 'auto', |
| 227 |
'srfm_cf_turnstile_site_key' => '', |
| 228 |
'srfm_cf_turnstile_secret_key' => '', |
| 229 |
'srfm_hcaptcha_site_key' => '', |
| 230 |
'srfm_hcaptcha_secret_key' => '', |
| 231 |
'srfm_honeypot' => false, |
| 232 |
]; |
| 233 |
} |
| 234 |
|
| 235 |
// Mask all secret key fields with the sentinel value. |
| 236 |
foreach ( self::$secret_keys as $key ) { |
| 237 |
if ( ! empty( $settings[ $key ] ) ) { |
| 238 |
$settings[ $key ] = self::SECRET_SENTINEL; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return $settings; |
| 243 |
} |
| 244 |
} |
| 245 |
|