| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Register_Site |
| 4 |
{ |
| 5 |
public static function execute($data = []) |
| 6 |
{ |
| 7 |
$logtivity_options = new Logtivity_Options; |
| 8 |
|
| 9 |
if ($logtivity_options->getApiKey()) { |
| 10 |
return new WP_Error( |
| 11 |
'logtivity_register_site_error', |
| 12 |
__('You cannot register a site that already has an API Key.', 'logtivity') |
| 13 |
); |
| 14 |
} |
| 15 |
|
| 16 |
$response = json_decode( |
| 17 |
(new Logtivity_Api)->setApiKey($data['team_api_key']) |
| 18 |
->post('/sites', [ |
| 19 |
'name' => $data['name'] ?? home_url(), |
| 20 |
'url' => $data['url'] ?? home_url(), |
| 21 |
]) |
| 22 |
); |
| 23 |
|
| 24 |
if ($response && property_exists($response, 'message')) { |
| 25 |
return wp_send_json(['response' => $response->message]); |
| 26 |
} |
| 27 |
|
| 28 |
$logtivity_options->update( |
| 29 |
array_merge( |
| 30 |
$data, |
| 31 |
[ |
| 32 |
'logtivity_site_api_key' => $response->api_key |
| 33 |
] |
| 34 |
) |
| 35 |
); |
| 36 |
} |
| 37 |
} |