| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Shared\Services\PluginsActivation; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
class SimplyBook extends PluginActivation |
| 8 |
{ |
| 9 |
public static function slug(): string |
| 10 |
{ |
| 11 |
return 'simplybook'; |
| 12 |
} |
| 13 |
|
| 14 |
protected static function simplybookNonce(): string |
| 15 |
{ |
| 16 |
return \wp_create_nonce('simplybook_nonce'); |
| 17 |
} |
| 18 |
|
| 19 |
public static function scriptData(): array |
| 20 |
{ |
| 21 |
// Only their React bundle carries the action, so it can't be read alongside the key. |
| 22 |
return [ |
| 23 |
'recaptchaSiteKey' => static::recaptchaSiteKey(), |
| 24 |
'recaptchaAction' => 'create_company', |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
// SimplyBook assesses the captcha itself, so a token minted with any other site key fails. |
| 29 |
protected static function recaptchaSiteKey(): string |
| 30 |
{ |
| 31 |
$config = WP_PLUGIN_DIR . '/' . static::slug() . '/config/env.php'; |
| 32 |
$env = is_readable($config) ? require $config : []; |
| 33 |
$key = $env['simplybook']['recaptcha']['site_key'] ?? ''; |
| 34 |
|
| 35 |
return is_string($key) ? $key : ''; |
| 36 |
} |
| 37 |
|
| 38 |
public static function createAccount(\WP_REST_Request $request): \WP_REST_Response |
| 39 |
{ |
| 40 |
if (!static::isActive()) { |
| 41 |
return static::pluginNotActiveResponse(); |
| 42 |
} |
| 43 |
|
| 44 |
// Reset onboarding data to have a fresh start |
| 45 |
delete_option('simplybook_onboarding_completed'); |
| 46 |
static::dispatchOnboarding('retry_onboarding'); |
| 47 |
|
| 48 |
$create = static::dispatchOnboarding('create_account', [ |
| 49 |
'email' => \sanitize_email($request->get_param('email')), |
| 50 |
'terms-and-conditions' => (bool) $request->get_param('termsAgreed'), |
| 51 |
'marketing-consent' => (bool) $request->get_param('marketingConsent'), |
| 52 |
'captcha_token' => \sanitize_text_field($request->get_param('captcha_token')), |
| 53 |
]); |
| 54 |
if ($create->is_error()) { |
| 55 |
return $create; |
| 56 |
} |
| 57 |
|
| 58 |
$finish = static::dispatchOnboarding('finish_onboarding'); |
| 59 |
if ($finish->is_error()) { |
| 60 |
return $finish; |
| 61 |
} |
| 62 |
|
| 63 |
return new \WP_REST_Response(['success' => true], 200); |
| 64 |
} |
| 65 |
|
| 66 |
protected static function dispatchOnboarding(string $action, array $body = []): \WP_REST_Response |
| 67 |
{ |
| 68 |
$request = new \WP_REST_Request('POST', '/simplybook/v1/onboarding/' . $action); |
| 69 |
$request->set_header('Content-Type', 'application/json'); |
| 70 |
$request->set_body(\wp_json_encode(array_merge($body, ['nonce' => static::simplybookNonce()]))); |
| 71 |
|
| 72 |
return \rest_do_request($request); |
| 73 |
} |
| 74 |
} |
| 75 |
|