| 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 requestHeaders(): array |
| 15 |
{ |
| 16 |
return ['Content-Type' => 'application/json', 'X-WP-Nonce' => \wp_create_nonce('wp_rest')]; |
| 17 |
} |
| 18 |
|
| 19 |
protected static function simplybookNonce(): string |
| 20 |
{ |
| 21 |
return \wp_create_nonce('simplybook_nonce'); |
| 22 |
} |
| 23 |
|
| 24 |
protected static function sslVerify(): bool |
| 25 |
{ |
| 26 |
return !(defined('EXTENDIFY_DEVMODE') && \constant('EXTENDIFY_DEVMODE')); |
| 27 |
} |
| 28 |
|
| 29 |
protected static function authCookies(): array |
| 30 |
{ |
| 31 |
$cookies = []; |
| 32 |
foreach ([\AUTH_COOKIE, \SECURE_AUTH_COOKIE, \LOGGED_IN_COOKIE] as $cookieName) { |
| 33 |
if (isset($_COOKIE[$cookieName])) { |
| 34 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 35 |
$cookieValue = \wp_unslash($_COOKIE[$cookieName]); |
| 36 |
$cookies[] = new \WP_Http_Cookie(['name' => $cookieName, 'value' => $cookieValue]); |
| 37 |
} |
| 38 |
} |
| 39 |
return $cookies; |
| 40 |
} |
| 41 |
|
| 42 |
public static function createAccount(\WP_REST_Request $request): \WP_REST_Response |
| 43 |
{ |
| 44 |
if (!static::isActive()) { |
| 45 |
return static::pluginNotActiveResponse(); |
| 46 |
} |
| 47 |
|
| 48 |
$nonce = static::simplybookNonce(); |
| 49 |
$headers = static::requestHeaders(); |
| 50 |
$sslVerify = static::sslVerify(); |
| 51 |
|
| 52 |
// Reset onboarding data to have a fresh start |
| 53 |
delete_option('simplybook_onboarding_completed'); |
| 54 |
$retryResponse = \wp_remote_post(\rest_url('simplybook/v1/onboarding/retry_onboarding'), [ |
| 55 |
'headers' => $headers, |
| 56 |
'cookies' => static::authCookies(), |
| 57 |
'sslverify' => $sslVerify, |
| 58 |
'body' => \wp_json_encode(['nonce' => $nonce]), |
| 59 |
]); |
| 60 |
|
| 61 |
if (\is_wp_error($retryResponse)) { |
| 62 |
return new \WP_REST_Response(['message' => $retryResponse->get_error_message()], 500); |
| 63 |
} |
| 64 |
|
| 65 |
$createResponse = \wp_remote_post(\rest_url('simplybook/v1/onboarding/create_account'), [ |
| 66 |
'headers' => $headers, |
| 67 |
'cookies' => static::authCookies(), |
| 68 |
'sslverify' => $sslVerify, |
| 69 |
'timeout' => 10, |
| 70 |
'body' => \wp_json_encode([ |
| 71 |
'email' => \sanitize_email($request->get_param('email')), |
| 72 |
'terms-and-conditions' => (bool) $request->get_param('termsAgreed'), |
| 73 |
'marketing-consent' => (bool) $request->get_param('marketingConsent'), |
| 74 |
'captcha_token' => \sanitize_text_field($request->get_param('captcha_token')), |
| 75 |
'nonce' => $nonce, |
| 76 |
]), |
| 77 |
]); |
| 78 |
|
| 79 |
if (\is_wp_error($createResponse)) { |
| 80 |
return new \WP_REST_Response(['message' => $createResponse->get_error_message()], 500); |
| 81 |
} |
| 82 |
|
| 83 |
$createStatus = \wp_remote_retrieve_response_code($createResponse); |
| 84 |
if ($createStatus >= 400) { |
| 85 |
$data = json_decode(\wp_remote_retrieve_body($createResponse), true) ?? []; |
| 86 |
return new \WP_REST_Response($data, $createStatus); |
| 87 |
} |
| 88 |
|
| 89 |
$finishResponse = \wp_remote_post(\rest_url('simplybook/v1/onboarding/finish_onboarding'), [ |
| 90 |
'headers' => $headers, |
| 91 |
'cookies' => static::authCookies(), |
| 92 |
'sslverify' => $sslVerify, |
| 93 |
'body' => \wp_json_encode(['nonce' => $nonce]), |
| 94 |
]); |
| 95 |
|
| 96 |
if (\is_wp_error($finishResponse)) { |
| 97 |
return new \WP_REST_Response(['message' => $finishResponse->get_error_message()], 500); |
| 98 |
} |
| 99 |
|
| 100 |
$finishStatus = \wp_remote_retrieve_response_code($finishResponse); |
| 101 |
if ($finishStatus >= 400) { |
| 102 |
$data = json_decode(\wp_remote_retrieve_body($finishResponse), true) ?? []; |
| 103 |
return new \WP_REST_Response($data, $finishStatus); |
| 104 |
} |
| 105 |
|
| 106 |
return new \WP_REST_Response(['success' => true], 200); |
| 107 |
} |
| 108 |
} |
| 109 |
|