| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Client; |
| 4 |
|
| 5 |
use Code_Snippets\Model\Basic_Cloud_Connection; |
| 6 |
use Code_Snippets\Model\Cloud_Snippet; |
| 7 |
use Code_Snippets\Model\Cloud_Snippets; |
| 8 |
use WP_REST_Request; |
| 9 |
use function Code_Snippets\Utils\unpack_response_body; |
| 10 |
|
| 11 |
/** |
| 12 |
* Client for fetching public data from Code Snippets Cloud. |
| 13 |
* |
| 14 |
* @package Code_Snippets |
| 15 |
*/ |
| 16 |
class Cloud_Public_Client { |
| 17 |
|
| 18 |
/** |
| 19 |
* Request timeout, in seconds, for the cloud search endpoint. Higher than WordPress's 5s |
| 20 |
* default because search can be slow on a cold cache and would otherwise time out. |
| 21 |
*/ |
| 22 |
private const SEARCH_REQUEST_TIMEOUT = 15; |
| 23 |
|
| 24 |
/** |
| 25 |
* Maximum number of cloud search results allowed per page. |
| 26 |
*/ |
| 27 |
public const MAX_RESULTS_PER_PAGE = 100; |
| 28 |
|
| 29 |
/** |
| 30 |
* Connection to Code Snippets Cloud. |
| 31 |
* |
| 32 |
* @var Basic_Cloud_Connection |
| 33 |
*/ |
| 34 |
private Basic_Cloud_Connection $connection; |
| 35 |
|
| 36 |
/** |
| 37 |
* Class constructor. |
| 38 |
* |
| 39 |
* @param Basic_Cloud_Connection $connection Connection to Code Snippets Cloud. |
| 40 |
*/ |
| 41 |
public function __construct( Basic_Cloud_Connection $connection ) { |
| 42 |
$this->connection = $connection; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Verify a REST API request is authorised to access controller functions. |
| 47 |
* |
| 48 |
* @param WP_REST_Request $request The REST API request. |
| 49 |
* |
| 50 |
* @return bool |
| 51 |
*/ |
| 52 |
public function verify_rest_request( WP_REST_Request $request ): bool { |
| 53 |
return $this->connection->verify_rest_request( $request ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Search Code Snippets Cloud. |
| 58 |
* |
| 59 |
* @param string $search_method Search by name of codevault or keyword(s). |
| 60 |
* @param string $search Search query. |
| 61 |
* @param int $page Search result page to retrieve. Defaults to '1'. |
| 62 |
* @param int $per_page Number of search results to retrieve per page. |
| 63 |
* @param array<string,string> $filters Optional filters: category, type, status. |
| 64 |
* |
| 65 |
* @return Cloud_Snippets Result of search query. |
| 66 |
*/ |
| 67 |
public function fetch_search_results( string $search_method, string $search, int $page, int $per_page, array $filters ): ?Cloud_Snippets { |
| 68 |
$params = [ |
| 69 |
's_method' => $search_method, |
| 70 |
's' => $search, |
| 71 |
'page' => max( 0, $page - 1 ), |
| 72 |
'per_page' => max( 1, $per_page ), |
| 73 |
'site_token' => $this->connection->get_local_token(), |
| 74 |
'site_host' => wp_parse_url( get_site_url(), PHP_URL_HOST ), |
| 75 |
]; |
| 76 |
|
| 77 |
foreach ( [ 'category', 'type', 'status' ] as $key ) { |
| 78 |
if ( ! empty( $filters[ $key ] ) ) { |
| 79 |
$params[ $key ] = $filters[ $key ]; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$response = wp_remote_get( |
| 84 |
add_query_arg( $params, sprintf( '%s/public/search', $this->connection->get_api_url() ) ), |
| 85 |
// The search endpoint can be slow on a cold cache; allow more time than WordPress's |
| 86 |
// default 5s request timeout so the request is not cut short and returned as empty. |
| 87 |
[ 'timeout' => self::SEARCH_REQUEST_TIMEOUT ] |
| 88 |
); |
| 89 |
|
| 90 |
return Cloud_Snippets::unpack_api_response( unpack_response_body( $response ), $page ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Retrieve a single cloud snippet from the API. |
| 95 |
* |
| 96 |
* @param int $cloud_id Remote cloud snippet ID. |
| 97 |
* |
| 98 |
* @return Cloud_Snippet Retrieved snippet. |
| 99 |
*/ |
| 100 |
public function get_cloud_snippet( int $cloud_id ): ?Cloud_Snippet { |
| 101 |
$response = wp_remote_get( |
| 102 |
sprintf( '%s/public/getsnippet/%s', $this->connection->get_api_url(), $cloud_id ) |
| 103 |
); |
| 104 |
|
| 105 |
$data = unpack_response_body( $response ); |
| 106 |
|
| 107 |
if ( ! is_array( $data ) || empty( $data['success'] ) || ! is_array( $data['snippet'] ?? null ) ) { |
| 108 |
return null; |
| 109 |
} |
| 110 |
|
| 111 |
return new Cloud_Snippet( $data['snippet'] ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get the current revision of a single cloud snippet. |
| 116 |
* |
| 117 |
* @param string $cloud_id Cloud snippet ID. |
| 118 |
* |
| 119 |
* @return string|null Revision number on success, null otherwise. |
| 120 |
*/ |
| 121 |
public function get_cloud_snippet_revision( string $cloud_id ): ?string { |
| 122 |
$response = wp_remote_get( |
| 123 |
sprintf( '%s/public/getsnippetrevision/%s', $this->connection->get_api_url(), $cloud_id ) |
| 124 |
); |
| 125 |
|
| 126 |
$body = unpack_response_body( $response ); |
| 127 |
return $body['snippet_revision'] ?? null; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Retrieve featured snippets from the cloud API, with transient caching. |
| 132 |
* |
| 133 |
* @param int $page Page number (1-indexed). |
| 134 |
* @param int $per_page Results per page. |
| 135 |
* @param array<string,string> $filters Optional filters: category, type, status. |
| 136 |
* |
| 137 |
* @return Cloud_Snippets|null Featured snippets, or an null on failure. |
| 138 |
*/ |
| 139 |
public function get_featured_snippets( int $page, int $per_page, array $filters ): ?Cloud_Snippets { |
| 140 |
$params = [ |
| 141 |
'page' => max( 0, $page - 1 ), |
| 142 |
'per_page' => min( self::MAX_RESULTS_PER_PAGE, max( 1, $per_page ) ), |
| 143 |
]; |
| 144 |
|
| 145 |
foreach ( [ 'category', 'type', 'status' ] as $key ) { |
| 146 |
if ( ! empty( $filters[ $key ] ) ) { |
| 147 |
$params[ $key ] = $filters[ $key ]; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$response = wp_remote_get( |
| 152 |
add_query_arg( $params, sprintf( '%s/public/featured', $this->connection->get_api_url() ) ), |
| 153 |
[ 'headers' => $this->connection->get_request_headers() ] |
| 154 |
); |
| 155 |
|
| 156 |
return Cloud_Snippets::unpack_api_response( unpack_response_body( $response ), $page ); |
| 157 |
} |
| 158 |
} |
| 159 |
|