PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
3.24.2 3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 All 106 releases
wp-parsely / src / services / class-cached-service-endpoint.php

class-cached-service-endpoint.php in Parse.ly 3.24.2, at src/services/class-cached-service-endpoint.php

155 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cached Service Endpoint class.
4 *
5 * @package Parsely
6 * @since 3.17.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Services;
12
13 use WP_Error;
14
15 use const Parsely\PARSELY_CACHE_GROUP;
16
17 /**
18 * Cached Service Endpoint class.
19 *
20 * This class is a wrapper around a service endpoint that caches the response
21 * from the API request.
22 *
23 * @since 3.17.0
24 *
25 * @phpstan-import-type WP_HTTP_Request_Args from Base_Service_Endpoint
26 */
27 class Cached_Service_Endpoint extends Base_Service_Endpoint {
28 /**
29 * The service endpoint object.
30 *
31 * @since 3.17.0
32 *
33 * @var Base_Service_Endpoint
34 */
35 private $service_endpoint;
36
37 /**
38 * The cache time-to-live, in milliseconds.
39 *
40 * @since 3.17.0
41 *
42 * @var int
43 */
44 private $cache_ttl;
45
46 /**
47 * Constructor.
48 *
49 * @since 3.17.0
50 *
51 * @param Base_Service_Endpoint $service_endpoint The service endpoint object.
52 * @param int $cache_ttl The cache time-to-live.
53 */
54 public function __construct( Base_Service_Endpoint $service_endpoint, int $cache_ttl ) {
55 $this->service_endpoint = $service_endpoint;
56 $this->cache_ttl = $cache_ttl;
57
58 parent::__construct( $service_endpoint->api_service );
59 }
60
61 /**
62 * Returns the cache key for the API request.
63 *
64 * @since 3.17.0
65 *
66 * @param array<mixed> $args The arguments to pass to the API request.
67 * @return string The cache key for the API request.
68 */
69 private function get_cache_key( array $args ): string {
70 $api_service = $this->service_endpoint->api_service;
71 $identifier = sprintf( '%s-%s-%s', $api_service->get_api_url(), $this->get_endpoint(), (string) wp_json_encode( $args ) );
72
73 return sprintf( 'parsely-api-%s', hash( 'sha256', $identifier ) );
74 }
75
76 /**
77 * Executes the API request, caching the response.
78 *
79 * If the response is already cached, it will be returned from the cache,
80 * otherwise the API request will be made and the response will be cached.
81 *
82 * @since 3.17.0
83 *
84 * @param array<mixed> $args The arguments to pass to the API request.
85 * @return WP_Error|array<mixed> The response from the API.
86 */
87 public function call( array $args = array() ) {
88 $cache_key = $this->get_cache_key( $args );
89 $cache = wp_cache_get( $cache_key, PARSELY_CACHE_GROUP );
90
91 if ( false !== $cache ) {
92 // @phpstan-ignore-next-line
93 return $cache;
94 }
95
96 $response = $this->service_endpoint->call( $args );
97
98 if ( ! is_wp_error( $response ) ) {
99 wp_cache_set( $cache_key, $response, PARSELY_CACHE_GROUP, $this->cache_ttl ); // phpcs:ignore
100 }
101
102 return $response;
103 }
104
105 /**
106 * Returns the endpoint for the API request.
107 *
108 * @since 3.17.0
109 *
110 * @return string The endpoint for the API request.
111 */
112 public function get_endpoint(): string {
113 return $this->service_endpoint->get_endpoint();
114 }
115
116 /**
117 * Returns the uncached endpoint for the API request.
118 *
119 * @since 3.17.0
120 *
121 * @return Base_Service_Endpoint The uncached endpoint for the API request.
122 */
123 public function get_uncached_endpoint(): Base_Service_Endpoint {
124 return $this->service_endpoint;
125 }
126
127 /**
128 * Returns the request options for the remote API request.
129 *
130 * Gets the request options from the uncached service endpoint.
131 *
132 * @since 3.17.0
133 *
134 * @param string $method The HTTP method to use for the request.
135 * @return WP_HTTP_Request_Args The request options for the remote API request.
136 */
137 protected function get_request_options( string $method ): array {
138 return $this->service_endpoint->get_request_options( $method );
139 }
140
141 /**
142 * Returns the common query arguments to send to the remote API.
143 *
144 * Gets the query arguments from the uncached service endpoint.
145 *
146 * @since 3.17.0
147 *
148 * @param array<mixed> $args Additional query arguments to send to the remote API.
149 * @return array<mixed> The query arguments to send to the remote API.
150 */
151 protected function get_query_args( array $args = array() ): array {
152 return $this->service_endpoint->get_query_args( $args );
153 }
154 }
155