| 1 |
<?php |
| 2 |
namespace Manage\Classes; |
| 3 |
|
| 4 |
use ElementorOne\Connect\Classes\GrantTypes; |
| 5 |
use Manage\Modules\Connect\Module as Connect; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Manage_Client extends Platform_Client { |
| 12 |
private static ?Manage_Client $instance = null; |
| 13 |
|
| 14 |
public static function get_instance(): Manage_Client { |
| 15 |
if ( ! self::$instance ) { |
| 16 |
self::$instance = new self(); |
| 17 |
} |
| 18 |
|
| 19 |
return self::$instance; |
| 20 |
} |
| 21 |
|
| 22 |
protected static function get_api_path(): string { |
| 23 |
return 'manage/api/v1/'; |
| 24 |
} |
| 25 |
|
| 26 |
protected static function get_grant_type(): string { |
| 27 |
return GrantTypes::CLIENT_CREDENTIALS; |
| 28 |
} |
| 29 |
|
| 30 |
public static function get_site_info(): array { |
| 31 |
return [ |
| 32 |
'app_version' => MANAGE_VERSION, |
| 33 |
'site_lang' => get_bloginfo( 'language' ), |
| 34 |
'site_url' => trailingslashit( home_url() ), |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
public static function get_client_id(): ?string { |
| 39 |
return Connect::get_connect()->data()->get_client_id(); |
| 40 |
} |
| 41 |
|
| 42 |
public function make_request( $method, $endpoint, $body = [], array $headers = [], $send_json = false ) { |
| 43 |
$body = array_replace_recursive( $body, static::get_site_info() ); |
| 44 |
|
| 45 |
if ( static::is_connected() ) { |
| 46 |
$headers = array_replace_recursive( |
| 47 |
$headers, |
| 48 |
$this->generate_authentication_headers( $endpoint ) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
return parent::make_request( $method, $endpoint, $body, $headers, $send_json ); |
| 53 |
} |
| 54 |
|
| 55 |
public static function is_connected(): bool { |
| 56 |
return Connect::is_connected(); |
| 57 |
} |
| 58 |
|
| 59 |
protected function generate_authentication_headers( $endpoint ): array { |
| 60 |
$headers = [ |
| 61 |
'endpoint' => $endpoint, |
| 62 |
]; |
| 63 |
|
| 64 |
return $this->add_bearer_token( $headers ); |
| 65 |
} |
| 66 |
|
| 67 |
protected function handle_empty_body( string $raw_body, int $response_code, $decoded_body, array $response ) { |
| 68 |
if ( empty( $decoded_body ) ) { |
| 69 |
return new \WP_Error( \WP_Http::UNPROCESSABLE_ENTITY, 'Wrong Server Response' ); |
| 70 |
} |
| 71 |
|
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
protected function handle_non_ok_response( $decoded_body, int $response_code, array $response ): ?\WP_Error { |
| 76 |
if ( ! $this->refreshed && ! empty( $decoded_body->message ) && false !== strpos( $decoded_body->message, 'site url mismatch' ) ) { |
| 77 |
Connect::get_connect()->data()->set_home_url( 'https://wrongurl' ); |
| 78 |
|
| 79 |
return new \WP_Error( \WP_Http::UNAUTHORIZED, 'site url mismatch' ); |
| 80 |
} |
| 81 |
|
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
public static function register_website() { |
| 86 |
$home_url = trailingslashit( home_url() ); |
| 87 |
|
| 88 |
return self::get_instance()->make_request( 'POST', 'sites', [ |
| 89 |
'rest_url' => get_rest_url( null, 'manage/v1' ), |
| 90 |
'home_url' => $home_url, |
| 91 |
] ); |
| 92 |
} |
| 93 |
|
| 94 |
public static function sync_website() { |
| 95 |
return self::get_instance()->make_request( 'POST', 'sites/sync' ); |
| 96 |
} |
| 97 |
} |
| 98 |
|