| 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 scriptData(): array |
| 12 |
{ |
| 13 |
return []; |
| 14 |
} |
| 15 |
|
| 16 |
public static function isActive(): bool |
| 17 |
{ |
| 18 |
foreach ((array) \get_option('active_plugins', []) as $plugin) { |
| 19 |
if (dirname($plugin) === static::slug()) { |
| 20 |
return true; |
| 21 |
} |
| 22 |
} |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
protected static function pluginNotActiveResponse(): \WP_REST_Response |
| 27 |
{ |
| 28 |
return new \WP_REST_Response( |
| 29 |
[ |
| 30 |
'code' => static::slug() . '_plugin_not_active', |
| 31 |
'message' => sprintf( |
| 32 |
/* translators: %s: plugin slug */ |
| 33 |
\__('The %s plugin is not active.', 'extendify-local'), |
| 34 |
static::slug() |
| 35 |
), |
| 36 |
], |
| 37 |
424 |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
public static function createAccount(\WP_REST_Request $request): \WP_REST_Response |
| 42 |
{ |
| 43 |
if (!static::isActive()) { |
| 44 |
return static::pluginNotActiveResponse(); |
| 45 |
} |
| 46 |
|
| 47 |
$email = \sanitize_email($request->get_param('email')); |
| 48 |
$headers = (array) ($request->get_param('headers') ?? []); |
| 49 |
$body = (array) ($request->get_param('body') ?? []); |
| 50 |
|
| 51 |
$response = \wp_safe_remote_post(static::API_URL, [ |
| 52 |
'headers' => array_merge(['Content-Type' => 'application/json'], $headers), |
| 53 |
'body' => \wp_json_encode(array_merge(['email' => $email], $body)), |
| 54 |
]); |
| 55 |
|
| 56 |
if (\is_wp_error($response)) { |
| 57 |
return new \WP_REST_Response(['message' => $response->get_error_message()], 500); |
| 58 |
} |
| 59 |
|
| 60 |
$code = \wp_remote_retrieve_response_code($response); |
| 61 |
$data = json_decode(\wp_remote_retrieve_body($response), true) ?? []; |
| 62 |
|
| 63 |
if ($code >= 200 && $code < 300) { |
| 64 |
static::saveKey($data); |
| 65 |
} |
| 66 |
|
| 67 |
return new \WP_REST_Response($data, $code); |
| 68 |
} |
| 69 |
|
| 70 |
protected static function saveKey(array $data) |
| 71 |
{ |
| 72 |
} |
| 73 |
} |
| 74 |
|