| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Model; |
| 4 |
|
| 5 |
use WP_REST_Request; |
| 6 |
|
| 7 |
/** |
| 8 |
* Represents an unauthenticated connection to Code Snippets Cloud. |
| 9 |
*/ |
| 10 |
class Basic_Cloud_Connection { |
| 11 |
|
| 12 |
/** |
| 13 |
* Token used for public API access. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private const PUBLIC_API_TOKEN = 'csc-1a2b3c4d5e6f7g8h9i0j'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Base URL for Code Snippets Cloud. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private string $base_url; |
| 25 |
|
| 26 |
/** |
| 27 |
* Base URL for Code Snippets Cloud API. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private string $api_url; |
| 32 |
|
| 33 |
/** |
| 34 |
* Class constructor. |
| 35 |
* |
| 36 |
* @noinspection PhpUndefinedConstantInspection |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
$this->base_url = defined( 'CS_CLOUD_URL' ) |
| 40 |
? untrailingslashit( CS_CLOUD_URL ) |
| 41 |
: 'https://codesnippets.cloud'; |
| 42 |
|
| 43 |
$this->api_url = defined( 'CS_CLOUD_API_URL' ) |
| 44 |
? untrailingslashit( CS_CLOUD_API_URL ) |
| 45 |
: sprintf( '%s/api/v1', $this->base_url ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Retrieve base URL for Code Snippets Cloud. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public function get_base_url(): string { |
| 54 |
return $this->base_url; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Retrieve base URL for Code Snippets Cloud API. |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public function get_api_url(): string { |
| 63 |
return $this->api_url; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Retrieve the cloud local token. |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function get_local_token(): string { |
| 72 |
return self::PUBLIC_API_TOKEN; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Generate the client ID from the current local token and site URL. |
| 77 |
* |
| 78 |
* @return string Client ID. |
| 79 |
*/ |
| 80 |
public function get_client_id(): string { |
| 81 |
$site_host = wp_parse_url( get_site_url(), PHP_URL_HOST ); |
| 82 |
return sprintf( '%s-%s', $site_host, $this->get_local_token() ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Determine whether this connection is suitable for requests requiring authentication. |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public function is_authenticated(): bool { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Create a list of headers required for a cloud request. |
| 96 |
* |
| 97 |
* @return array<string, string> Header name and value pairs. |
| 98 |
*/ |
| 99 |
public function get_request_headers(): array { |
| 100 |
return [ |
| 101 |
'Local-Token' => $this->get_local_token(), |
| 102 |
]; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Verify a REST API request includes the correct Access-Control header. |
| 107 |
* |
| 108 |
* @param WP_REST_Request $request Incoming REST request. |
| 109 |
* |
| 110 |
* @return bool |
| 111 |
*/ |
| 112 |
public function verify_rest_request( WP_REST_Request $request ): bool { |
| 113 |
return $request->get_header( 'Access-Control' ) === $this->get_local_token(); |
| 114 |
} |
| 115 |
} |
| 116 |
|