get-global-settings.php
5 months ago
settings-secret-keys.php
5 months ago
update-global-settings.php
1 month ago
get-global-settings.php
235 lines
| 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 | ]; |
| 152 | } |
| 153 | |
| 154 | if ( ! isset( $settings['srfm_admin_notification'] ) ) { |
| 155 | $settings['srfm_admin_notification'] = true; |
| 156 | } |
| 157 | |
| 158 | $settings['srfm_bsf_analytics'] = 'yes' === get_option( 'sureforms_usage_optin', false ); |
| 159 | |
| 160 | return $settings; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get validation messages settings. |
| 165 | * |
| 166 | * @since 2.6.0 |
| 167 | * @return array<string,mixed> |
| 168 | */ |
| 169 | private function get_validation_messages() { |
| 170 | $settings = get_option( 'srfm_default_dynamic_block_option', [] ); |
| 171 | |
| 172 | if ( empty( $settings ) || ! is_array( $settings ) ) { |
| 173 | $settings = Helper::default_dynamic_block_option(); |
| 174 | } |
| 175 | |
| 176 | return $settings; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get email summary settings. |
| 181 | * |
| 182 | * @since 2.6.0 |
| 183 | * @return array<string,mixed> |
| 184 | */ |
| 185 | private function get_email_summary_settings() { |
| 186 | $settings = get_option( 'srfm_email_summary_settings_options', [] ); |
| 187 | |
| 188 | if ( empty( $settings ) || ! is_array( $settings ) ) { |
| 189 | $settings = [ |
| 190 | 'srfm_email_summary' => false, |
| 191 | 'srfm_email_sent_to' => get_option( 'admin_email' ), |
| 192 | 'srfm_schedule_report' => __( 'Monday', 'sureforms' ), |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | return $settings; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get security settings with secret keys masked. |
| 201 | * |
| 202 | * @since 2.6.0 |
| 203 | * @return array<string,mixed> |
| 204 | */ |
| 205 | private function get_security_settings() { |
| 206 | $settings = get_option( 'srfm_security_settings_options', [] ); |
| 207 | |
| 208 | if ( empty( $settings ) || ! is_array( $settings ) ) { |
| 209 | $settings = [ |
| 210 | 'srfm_v2_checkbox_site_key' => '', |
| 211 | 'srfm_v2_checkbox_secret_key' => '', |
| 212 | 'srfm_v2_invisible_site_key' => '', |
| 213 | 'srfm_v2_invisible_secret_key' => '', |
| 214 | 'srfm_v3_site_key' => '', |
| 215 | 'srfm_v3_secret_key' => '', |
| 216 | 'srfm_cf_appearance_mode' => 'auto', |
| 217 | 'srfm_cf_turnstile_site_key' => '', |
| 218 | 'srfm_cf_turnstile_secret_key' => '', |
| 219 | 'srfm_hcaptcha_site_key' => '', |
| 220 | 'srfm_hcaptcha_secret_key' => '', |
| 221 | 'srfm_honeypot' => false, |
| 222 | ]; |
| 223 | } |
| 224 | |
| 225 | // Mask all secret key fields with the sentinel value. |
| 226 | foreach ( self::$secret_keys as $key ) { |
| 227 | if ( ! empty( $settings[ $key ] ) ) { |
| 228 | $settings[ $key ] = self::SECRET_SENTINEL; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return $settings; |
| 233 | } |
| 234 | } |
| 235 |