| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Shared\Services\PluginsActivation; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
abstract class PluginActivation |
| 8 |
{ |
| 9 |
abstract public static function slug(): string; |
| 10 |
|
| 11 |
public static function createAccountRoute(): string |
| 12 |
{ |
| 13 |
return '/' . static::slug() . '/create-account'; |
| 14 |
} |
| 15 |
|
| 16 |
public static function scriptData(): array |
| 17 |
{ |
| 18 |
return []; |
| 19 |
} |
| 20 |
|
| 21 |
public static function isActive(): bool |
| 22 |
{ |
| 23 |
foreach ((array) \get_option('active_plugins', []) as $plugin) { |
| 24 |
if (dirname($plugin) === static::slug()) { |
| 25 |
return true; |
| 26 |
} |
| 27 |
} |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
public static function isEligible(): bool |
| 32 |
{ |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
// rest_no_route names the failure better than the code we would mint. |
| 37 |
protected static function withFallbackCode(\WP_REST_Response $response, string $code): \WP_REST_Response |
| 38 |
{ |
| 39 |
return new \WP_REST_Response( |
| 40 |
array_merge(['code' => $code], (array) $response->get_data()), |
| 41 |
$response->get_status() |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
protected static function pluginNotActiveResponse(): \WP_REST_Response |
| 46 |
{ |
| 47 |
return new \WP_REST_Response( |
| 48 |
[ |
| 49 |
'code' => static::slug() . '_plugin_not_active', |
| 50 |
'message' => sprintf( |
| 51 |
/* translators: %s: plugin slug */ |
| 52 |
\__('The %s plugin is not active.', 'extendify-local'), |
| 53 |
static::slug() |
| 54 |
), |
| 55 |
], |
| 56 |
424 |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public static function createAccount(\WP_REST_Request $request): \WP_REST_Response |
| 61 |
{ |
| 62 |
if (!static::isActive()) { |
| 63 |
return static::pluginNotActiveResponse(); |
| 64 |
} |
| 65 |
|
| 66 |
$email = \sanitize_email($request->get_param('email')); |
| 67 |
$headers = (array) ($request->get_param('headers') ?? []); |
| 68 |
$body = (array) ($request->get_param('body') ?? []); |
| 69 |
|
| 70 |
$response = \wp_safe_remote_post(static::API_URL, [ |
| 71 |
// http_request_args only lifts Extendify hosts, and it overrides this value if it ever matches. |
| 72 |
'timeout' => 45, |
| 73 |
'headers' => array_merge(['Content-Type' => 'application/json'], $headers), |
| 74 |
'body' => \wp_json_encode(array_merge(['email' => $email], $body)), |
| 75 |
]); |
| 76 |
|
| 77 |
if (\is_wp_error($response)) { |
| 78 |
return new \WP_REST_Response([ |
| 79 |
'code' => $response->get_error_code(), |
| 80 |
'message' => $response->get_error_message(), |
| 81 |
], 500); |
| 82 |
} |
| 83 |
|
| 84 |
$code = \wp_remote_retrieve_response_code($response); |
| 85 |
$data = json_decode(\wp_remote_retrieve_body($response), true) ?? []; |
| 86 |
|
| 87 |
if ($code >= 200 && $code < 300) { |
| 88 |
static::saveKey($data); |
| 89 |
} |
| 90 |
|
| 91 |
return new \WP_REST_Response($data, $code); |
| 92 |
} |
| 93 |
|
| 94 |
protected static function saveKey(array $data) |
| 95 |
{ |
| 96 |
} |
| 97 |
} |
| 98 |
|