RegisterSettingService.php
3 years ago
SettingService.php
3 years ago
SettingsServiceProvider.php
3 years ago
SettingService.php
242 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Settings; |
| 4 | |
| 5 | use SureCart\WordPress\RecaptchaValidationService; |
| 6 | use SureCart\Routing\PermalinksSettingsService; |
| 7 | |
| 8 | /** |
| 9 | * A service for registering settings. |
| 10 | */ |
| 11 | class SettingService { |
| 12 | /** |
| 13 | * Boostrap settings. |
| 14 | * |
| 15 | * @return void |
| 16 | */ |
| 17 | public function bootstrap() { |
| 18 | $this->register( |
| 19 | 'surecart', |
| 20 | 'theme', |
| 21 | [ |
| 22 | 'type' => 'string', |
| 23 | 'show_in_rest' => true, |
| 24 | 'sanitize_callback' => 'sanitize_text_field', |
| 25 | 'default' => 'light', |
| 26 | ] |
| 27 | ); |
| 28 | $this->register( |
| 29 | 'surecart', |
| 30 | 'auto_sync_user_to_customer', |
| 31 | [ |
| 32 | 'type' => 'boolean', |
| 33 | 'show_in_rest' => true, |
| 34 | 'sanitize_callback' => 'boolval', |
| 35 | 'default' => false, |
| 36 | ] |
| 37 | ); |
| 38 | $this->register( |
| 39 | 'surecart', |
| 40 | 'honeypot_enabled', |
| 41 | [ |
| 42 | 'type' => 'boolean', |
| 43 | 'show_in_rest' => true, |
| 44 | 'sanitize_callback' => 'boolval', |
| 45 | 'default' => true, |
| 46 | ] |
| 47 | ); |
| 48 | $this->register( |
| 49 | 'surecart', |
| 50 | 'recaptcha_enabled', |
| 51 | [ |
| 52 | 'type' => 'boolean', |
| 53 | 'show_in_rest' => true, |
| 54 | 'sanitize_callback' => 'boolval', |
| 55 | ] |
| 56 | ); |
| 57 | $this->register( |
| 58 | 'surecart', |
| 59 | 'recaptcha_site_key', |
| 60 | [ |
| 61 | 'type' => 'string', |
| 62 | 'show_in_rest' => true, |
| 63 | 'sanitize_callback' => 'sanitize_text_field', |
| 64 | ] |
| 65 | ); |
| 66 | $this->register( |
| 67 | 'surecart', |
| 68 | 'recaptcha_secret_key', |
| 69 | [ |
| 70 | 'type' => 'string', |
| 71 | 'show_in_rest' => true, |
| 72 | 'sanitize_callback' => 'sanitize_text_field', |
| 73 | ] |
| 74 | ); |
| 75 | $this->register( |
| 76 | 'surecart', |
| 77 | 'recaptcha_min_score', |
| 78 | [ |
| 79 | 'type' => 'number', |
| 80 | 'show_in_rest' => true, |
| 81 | 'default' => 0.5, |
| 82 | 'sanitize_callback' => 'sanitize_text_field', |
| 83 | ] |
| 84 | ); |
| 85 | $this->register( |
| 86 | 'surecart', |
| 87 | 'load_stripe_js', |
| 88 | [ |
| 89 | 'type' => 'boolean', |
| 90 | 'show_in_rest' => true, |
| 91 | 'sanitize_callback' => 'boolval', |
| 92 | ] |
| 93 | ); |
| 94 | $this->register( |
| 95 | 'surecart', |
| 96 | 'tracking_confirmation', |
| 97 | [ |
| 98 | 'type' => 'boolean', |
| 99 | 'show_in_rest' => true, |
| 100 | 'sanitize_callback' => 'boolval', |
| 101 | ] |
| 102 | ); |
| 103 | $this->register( |
| 104 | 'surecart', |
| 105 | 'tracking_confirmation_message', |
| 106 | [ |
| 107 | 'type' => 'string', |
| 108 | 'show_in_rest' => true, |
| 109 | 'default' => esc_html__( 'Your email and cart are saved so we can send email reminders about this order.', 'surecart' ), |
| 110 | 'sanitize_callback' => 'sanitize_text_field', |
| 111 | ] |
| 112 | ); |
| 113 | $this->register( |
| 114 | 'surecart', |
| 115 | 'buy_link_logo_width', |
| 116 | [ |
| 117 | 'type' => 'string', |
| 118 | 'show_in_rest' => true, |
| 119 | 'default' => '180px', |
| 120 | 'sanitize_callback' => 'sanitize_text_field', |
| 121 | ] |
| 122 | ); |
| 123 | $this->register( |
| 124 | 'surecart', |
| 125 | 'cart_menu_alignment', |
| 126 | [ |
| 127 | 'type' => 'string', |
| 128 | 'show_in_rest' => true, |
| 129 | 'sanitize_callback' => 'sanitize_text_field', |
| 130 | 'default' => 'right', |
| 131 | ] |
| 132 | ); |
| 133 | $this->register( |
| 134 | 'surecart', |
| 135 | 'cart_menu_always_shown', |
| 136 | [ |
| 137 | 'type' => 'boolean', |
| 138 | 'show_in_rest' => true, |
| 139 | 'sanitize_callback' => 'boolval', |
| 140 | 'default' => true, |
| 141 | ] |
| 142 | ); |
| 143 | $this->register( |
| 144 | 'surecart', |
| 145 | 'cart_menu_selected_ids', |
| 146 | [ |
| 147 | 'type' => 'array', |
| 148 | 'items' => 'integer', |
| 149 | 'show_in_rest' => [ |
| 150 | 'schema' => [ |
| 151 | 'type' => 'array', |
| 152 | 'items' => [ |
| 153 | 'type' => 'integer', |
| 154 | ], |
| 155 | ], |
| 156 | ], |
| 157 | ] |
| 158 | ); |
| 159 | $this->register( |
| 160 | 'surecart', |
| 161 | 'cart_icon', |
| 162 | [ |
| 163 | 'type' => 'string', |
| 164 | 'show_in_rest' => true, |
| 165 | 'sanitize_callback' => 'sanitize_text_field', |
| 166 | 'default' => 'shopping-bag', // shopping-bag, shopping-cart. |
| 167 | ] |
| 168 | ); |
| 169 | $this->register( |
| 170 | 'surecart', |
| 171 | 'cart_icon_type', |
| 172 | [ |
| 173 | 'type' => 'string', |
| 174 | 'show_in_rest' => true, |
| 175 | 'sanitize_callback' => 'sanitize_text_field', |
| 176 | 'default' => 'floating_icon', // both, floating_icon, menu_icon. |
| 177 | ] |
| 178 | ); |
| 179 | $this->register( |
| 180 | 'surecart', |
| 181 | 'password_validation_enabled', |
| 182 | [ |
| 183 | 'type' => 'boolean', |
| 184 | 'show_in_rest' => true, |
| 185 | 'sanitize_callback' => 'boolval', |
| 186 | 'default' => false, |
| 187 | ] |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Register a setting. |
| 193 | * |
| 194 | * @param string $option_group A settings group name. Should correspond to an allowed option key name. |
| 195 | * Default allowed option key names include 'surecart', 'discussion', 'media', |
| 196 | * 'reading', 'writing', and 'options'. |
| 197 | * @param string $option_name The name of an option to sanitize and save. |
| 198 | * @param array $args { |
| 199 | * Data used to describe the setting when registered. |
| 200 | * |
| 201 | * @type string $type The type of data associated with this setting. |
| 202 | * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. |
| 203 | * @type string $description A description of the data attached to this setting. |
| 204 | * @type callable $sanitize_callback A callback function that sanitizes the option's value. |
| 205 | * @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API. |
| 206 | * When registering complex settings, this argument may optionally be an |
| 207 | * array with a 'schema' key. |
| 208 | * @type mixed $default Default value when calling `get_option()`. |
| 209 | */ |
| 210 | public function register( $option_group, $option_name, $args = [] ) { |
| 211 | $service = new RegisterSettingService( $option_group, $option_name, $args ); |
| 212 | return $service->register(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Recaptcha service. |
| 217 | * |
| 218 | * @return RecaptchaValidationService |
| 219 | */ |
| 220 | public function recaptcha() { |
| 221 | return new RecaptchaValidationService(); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Get the option. |
| 226 | * |
| 227 | * @return mixed |
| 228 | */ |
| 229 | public function get( $name, $default = false ) { |
| 230 | return get_option( "surecart_${name}", $default ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Get the permalinks settings. |
| 235 | * |
| 236 | * @return PermalinksSettingsService |
| 237 | */ |
| 238 | public function permalinks() { |
| 239 | return new PermalinksSettingsService(); |
| 240 | } |
| 241 | } |
| 242 |