SettingService.php
| 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 | add_action( 'init', [ $this, 'registerSettings' ] ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Register settings. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | public function registerSettings() { |
| 27 | $this->register( |
| 28 | 'surecart', |
| 29 | 'auto_sync_user_to_customer', |
| 30 | [ |
| 31 | 'type' => 'boolean', |
| 32 | 'show_in_rest' => true, |
| 33 | 'sanitize_callback' => 'boolval', |
| 34 | 'default' => false, |
| 35 | ] |
| 36 | ); |
| 37 | $this->register( |
| 38 | 'surecart', |
| 39 | 'honeypot_enabled', |
| 40 | [ |
| 41 | 'type' => 'boolean', |
| 42 | 'show_in_rest' => true, |
| 43 | 'sanitize_callback' => 'boolval', |
| 44 | 'default' => true, |
| 45 | ] |
| 46 | ); |
| 47 | $this->register( |
| 48 | 'surecart', |
| 49 | 'recaptcha_enabled', |
| 50 | [ |
| 51 | 'type' => 'boolean', |
| 52 | 'show_in_rest' => true, |
| 53 | 'sanitize_callback' => 'boolval', |
| 54 | ] |
| 55 | ); |
| 56 | $this->register( |
| 57 | 'surecart', |
| 58 | 'recaptcha_site_key', |
| 59 | [ |
| 60 | 'type' => 'string', |
| 61 | 'show_in_rest' => true, |
| 62 | 'sanitize_callback' => 'sanitize_text_field', |
| 63 | ] |
| 64 | ); |
| 65 | $this->register( |
| 66 | 'surecart', |
| 67 | 'recaptcha_secret_key', |
| 68 | [ |
| 69 | 'type' => 'string', |
| 70 | 'show_in_rest' => true, |
| 71 | 'sanitize_callback' => 'sanitize_text_field', |
| 72 | ] |
| 73 | ); |
| 74 | $this->register( |
| 75 | 'surecart', |
| 76 | 'recaptcha_min_score', |
| 77 | [ |
| 78 | 'type' => 'number', |
| 79 | 'show_in_rest' => true, |
| 80 | 'default' => 0.5, |
| 81 | 'sanitize_callback' => 'sanitize_text_field', |
| 82 | ] |
| 83 | ); |
| 84 | $this->register( |
| 85 | 'surecart', |
| 86 | 'load_stripe_js', |
| 87 | [ |
| 88 | 'type' => 'boolean', |
| 89 | 'show_in_rest' => true, |
| 90 | 'sanitize_callback' => 'boolval', |
| 91 | ] |
| 92 | ); |
| 93 | $this->register( |
| 94 | 'surecart', |
| 95 | 'google_map_api_key_enabled', |
| 96 | [ |
| 97 | 'type' => 'boolean', |
| 98 | 'show_in_rest' => true, |
| 99 | 'sanitize_callback' => 'boolval', |
| 100 | ] |
| 101 | ); |
| 102 | $this->register( |
| 103 | 'surecart', |
| 104 | 'google_map_api_key', |
| 105 | [ |
| 106 | 'type' => 'string', |
| 107 | 'show_in_rest' => true, |
| 108 | 'sanitize_callback' => 'sanitize_text_field', |
| 109 | ] |
| 110 | ); |
| 111 | $this->register( |
| 112 | 'surecart', |
| 113 | 'tracking_confirmation', |
| 114 | [ |
| 115 | 'type' => 'boolean', |
| 116 | 'show_in_rest' => true, |
| 117 | 'sanitize_callback' => 'boolval', |
| 118 | ] |
| 119 | ); |
| 120 | $this->register( |
| 121 | 'surecart', |
| 122 | 'tracking_confirmation_message', |
| 123 | [ |
| 124 | 'type' => 'string', |
| 125 | 'show_in_rest' => true, |
| 126 | 'default' => esc_html__( 'Your email and cart are saved so we can send email reminders about this order.', 'surecart' ), |
| 127 | 'sanitize_callback' => 'sanitize_text_field', |
| 128 | ] |
| 129 | ); |
| 130 | $this->register( |
| 131 | 'surecart', |
| 132 | 'buy_link_logo_width', |
| 133 | [ |
| 134 | 'type' => 'string', |
| 135 | 'show_in_rest' => true, |
| 136 | 'default' => '180px', |
| 137 | 'sanitize_callback' => 'sanitize_text_field', |
| 138 | ] |
| 139 | ); |
| 140 | $this->register( |
| 141 | 'surecart', |
| 142 | 'cart_menu_alignment', |
| 143 | [ |
| 144 | 'type' => 'string', |
| 145 | 'show_in_rest' => true, |
| 146 | 'sanitize_callback' => 'sanitize_text_field', |
| 147 | 'default' => 'right', |
| 148 | ] |
| 149 | ); |
| 150 | $this->register( |
| 151 | 'surecart', |
| 152 | 'cart_menu_always_shown', |
| 153 | [ |
| 154 | 'type' => 'boolean', |
| 155 | 'show_in_rest' => true, |
| 156 | 'sanitize_callback' => 'boolval', |
| 157 | 'default' => true, |
| 158 | ] |
| 159 | ); |
| 160 | $this->register( |
| 161 | 'surecart', |
| 162 | 'cart_menu_selected_ids', |
| 163 | [ |
| 164 | 'type' => 'array', |
| 165 | 'items' => 'integer', |
| 166 | 'show_in_rest' => [ |
| 167 | 'schema' => [ |
| 168 | 'type' => 'array', |
| 169 | 'items' => [ |
| 170 | 'type' => 'integer', |
| 171 | ], |
| 172 | ], |
| 173 | ], |
| 174 | ] |
| 175 | ); |
| 176 | $this->register( |
| 177 | 'surecart', |
| 178 | 'cart_icon', |
| 179 | [ |
| 180 | 'type' => 'string', |
| 181 | 'show_in_rest' => true, |
| 182 | 'sanitize_callback' => 'sanitize_text_field', |
| 183 | 'default' => 'shopping-bag', // shopping-bag, shopping-cart. |
| 184 | ] |
| 185 | ); |
| 186 | $this->register( |
| 187 | 'surecart', |
| 188 | 'cart_icon_type', |
| 189 | [ |
| 190 | 'type' => 'string', |
| 191 | 'show_in_rest' => true, |
| 192 | 'sanitize_callback' => 'sanitize_text_field', |
| 193 | 'default' => 'floating_icon', // both, floating_icon, menu_icon. |
| 194 | ] |
| 195 | ); |
| 196 | $this->register( |
| 197 | 'surecart', |
| 198 | 'admin_toolbar_disabled', |
| 199 | [ |
| 200 | 'type' => 'boolean', |
| 201 | 'show_in_rest' => true, |
| 202 | 'sanitize_callback' => 'boolval', |
| 203 | 'default' => false, |
| 204 | ] |
| 205 | ); |
| 206 | $this->register( |
| 207 | 'surecart', |
| 208 | 'password_validation_enabled', |
| 209 | [ |
| 210 | 'type' => 'boolean', |
| 211 | 'show_in_rest' => true, |
| 212 | 'sanitize_callback' => 'boolval', |
| 213 | 'default' => false, |
| 214 | ] |
| 215 | ); |
| 216 | $this->register( |
| 217 | 'surecart', |
| 218 | 'shop_admin_menu', |
| 219 | [ |
| 220 | 'type' => 'boolean', |
| 221 | 'show_in_rest' => true, |
| 222 | 'sanitize_callback' => 'boolval', |
| 223 | 'default' => true, |
| 224 | ] |
| 225 | ); |
| 226 | $this->register( |
| 227 | 'surecart', |
| 228 | 'cart_admin_menu', |
| 229 | [ |
| 230 | 'type' => 'boolean', |
| 231 | 'show_in_rest' => true, |
| 232 | 'sanitize_callback' => 'boolval', |
| 233 | 'default' => true, |
| 234 | ] |
| 235 | ); |
| 236 | $this->register( |
| 237 | 'surecart', |
| 238 | 'checkout_admin_menu', |
| 239 | [ |
| 240 | 'type' => 'boolean', |
| 241 | 'show_in_rest' => true, |
| 242 | 'sanitize_callback' => 'boolval', |
| 243 | 'default' => true, |
| 244 | ] |
| 245 | ); |
| 246 | $this->register( |
| 247 | 'surecart', |
| 248 | 'dashboard_admin_menu', |
| 249 | [ |
| 250 | 'type' => 'boolean', |
| 251 | 'show_in_rest' => true, |
| 252 | 'sanitize_callback' => 'boolval', |
| 253 | 'default' => true, |
| 254 | ] |
| 255 | ); |
| 256 | $this->register( |
| 257 | 'surecart', |
| 258 | 'unrestricted_test_mode', |
| 259 | [ |
| 260 | 'type' => 'boolean', |
| 261 | 'show_in_rest' => true, |
| 262 | 'sanitize_callback' => 'boolval', |
| 263 | ] |
| 264 | ); |
| 265 | $this->register( |
| 266 | 'surecart', |
| 267 | 'hide_help_widget', |
| 268 | [ |
| 269 | 'type' => 'boolean', |
| 270 | 'show_in_rest' => true, |
| 271 | 'sanitize_callback' => 'boolval', |
| 272 | 'default' => false, |
| 273 | ] |
| 274 | ); |
| 275 | $this->register( |
| 276 | 'surecart', |
| 277 | 'currency_switcher_alignment', |
| 278 | [ |
| 279 | 'type' => 'string', |
| 280 | 'show_in_rest' => true, |
| 281 | 'sanitize_callback' => 'sanitize_text_field', |
| 282 | 'default' => 'right', |
| 283 | ] |
| 284 | ); |
| 285 | $this->register( |
| 286 | 'surecart', |
| 287 | 'currency_switcher_selected_ids', |
| 288 | [ |
| 289 | 'type' => 'array', |
| 290 | 'items' => 'integer', |
| 291 | 'show_in_rest' => [ |
| 292 | 'schema' => [ |
| 293 | 'type' => 'array', |
| 294 | 'items' => [ |
| 295 | 'type' => 'integer', |
| 296 | ], |
| 297 | ], |
| 298 | ], |
| 299 | ] |
| 300 | ); |
| 301 | $this->register( |
| 302 | 'surecart', |
| 303 | 'currency_geolocation_enabled', |
| 304 | [ |
| 305 | 'type' => 'boolean', |
| 306 | 'show_in_rest' => true, |
| 307 | 'sanitize_callback' => 'boolval', |
| 308 | 'default' => true, |
| 309 | ] |
| 310 | ); |
| 311 | $this->register( |
| 312 | 'surecart', |
| 313 | 'currency_locale', |
| 314 | [ |
| 315 | 'type' => 'string', |
| 316 | 'show_in_rest' => true, |
| 317 | 'sanitize_callback' => 'sanitize_text_field', |
| 318 | ] |
| 319 | ); |
| 320 | $this->register( |
| 321 | 'surecart', |
| 322 | 'hide_verified_buyer_badge', |
| 323 | [ |
| 324 | 'type' => 'boolean', |
| 325 | 'show_in_rest' => true, |
| 326 | 'sanitize_callback' => 'boolval', |
| 327 | 'default' => false, |
| 328 | ] |
| 329 | ); |
| 330 | $this->register( |
| 331 | 'surecart', |
| 332 | 'learn_admin_menu', |
| 333 | [ |
| 334 | 'type' => 'boolean', |
| 335 | 'show_in_rest' => true, |
| 336 | 'sanitize_callback' => 'boolval', |
| 337 | 'default' => true, |
| 338 | ] |
| 339 | ); |
| 340 | $this->register( |
| 341 | 'surecart', |
| 342 | 'learn_completed_steps', |
| 343 | [ |
| 344 | 'type' => 'array', |
| 345 | 'show_in_rest' => [ |
| 346 | 'schema' => [ |
| 347 | 'type' => 'array', |
| 348 | 'items' => [ |
| 349 | 'type' => 'string', |
| 350 | ], |
| 351 | ], |
| 352 | ], |
| 353 | 'default' => [], |
| 354 | 'autoload' => false, |
| 355 | 'sanitize_callback' => function ( $value ) { |
| 356 | if ( ! is_array( $value ) ) { |
| 357 | return []; |
| 358 | } |
| 359 | return array_values( |
| 360 | array_unique( |
| 361 | array_filter( |
| 362 | array_map( 'sanitize_key', $value ) |
| 363 | ) |
| 364 | ) |
| 365 | ); |
| 366 | }, |
| 367 | ] |
| 368 | ); |
| 369 | $this->register( |
| 370 | 'surecart', |
| 371 | 'learn_total_steps', |
| 372 | [ |
| 373 | 'type' => 'integer', |
| 374 | 'show_in_rest' => true, |
| 375 | 'sanitize_callback' => 'absint', |
| 376 | 'default' => 24, |
| 377 | 'autoload' => false, |
| 378 | ] |
| 379 | ); |
| 380 | $this->register( |
| 381 | 'surecart', |
| 382 | 'mcp_abilities_enabled', |
| 383 | [ |
| 384 | 'type' => 'boolean', |
| 385 | 'show_in_rest' => true, |
| 386 | 'sanitize_callback' => 'boolval', |
| 387 | 'default' => true, |
| 388 | 'autoload' => false, |
| 389 | ] |
| 390 | ); |
| 391 | $this->register( |
| 392 | 'surecart', |
| 393 | 'mcp_edit_abilities_enabled', |
| 394 | [ |
| 395 | 'type' => 'boolean', |
| 396 | 'show_in_rest' => true, |
| 397 | 'sanitize_callback' => 'boolval', |
| 398 | 'default' => true, |
| 399 | 'autoload' => false, |
| 400 | ] |
| 401 | ); |
| 402 | $this->register( |
| 403 | 'surecart', |
| 404 | 'mcp_delete_abilities_enabled', |
| 405 | [ |
| 406 | 'type' => 'boolean', |
| 407 | 'show_in_rest' => true, |
| 408 | 'sanitize_callback' => 'boolval', |
| 409 | 'default' => true, |
| 410 | 'autoload' => false, |
| 411 | ] |
| 412 | ); |
| 413 | } |
| 414 | /** |
| 415 | * Register a setting. |
| 416 | * |
| 417 | * @param string $option_group A settings group name. Should correspond to an allowed option key name. |
| 418 | * Default allowed option key names include 'surecart', 'discussion', 'media', |
| 419 | * 'reading', 'writing', and 'options'. |
| 420 | * @param string $option_name The name of an option to sanitize and save. |
| 421 | * @param array $args { |
| 422 | * Data used to describe the setting when registered. |
| 423 | * |
| 424 | * @type string $type The type of data associated with this setting. |
| 425 | * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. |
| 426 | * @type string $description A description of the data attached to this setting. |
| 427 | * @type callable $sanitize_callback A callback function that sanitizes the option's value. |
| 428 | * @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API. |
| 429 | * When registering complex settings, this argument may optionally be an |
| 430 | * array with a 'schema' key. |
| 431 | * @type mixed $default Default value when calling `get_option()`. |
| 432 | */ |
| 433 | public function register( $option_group, $option_name, $args = [] ) { |
| 434 | $service = new RegisterSettingService( $option_group, $option_name, $args ); |
| 435 | return $service->register(); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Recaptcha service. |
| 440 | * |
| 441 | * @return RecaptchaValidationService |
| 442 | */ |
| 443 | public function recaptcha() { |
| 444 | return new RecaptchaValidationService(); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Get the option. |
| 449 | * |
| 450 | * @return mixed |
| 451 | */ |
| 452 | public function get( $name, $default = false ) { |
| 453 | return get_option( 'surecart_' . $name, $default ); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Get the number of remaining learn steps. |
| 458 | * |
| 459 | * @return int |
| 460 | */ |
| 461 | public function getLearnRemainingSteps() { |
| 462 | $total = (int) $this->get( 'learn_total_steps', 24 ); |
| 463 | $completed = $this->get( 'learn_completed_steps', [] ); |
| 464 | return max( 0, $total - count( (array) $completed ) ); |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Get the permalinks settings. |
| 469 | * |
| 470 | * @return PermalinksSettingsService |
| 471 | */ |
| 472 | public function permalinks() { |
| 473 | return new PermalinksSettingsService(); |
| 474 | } |
| 475 | } |
| 476 |