PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.2
Jetpack – WP Security, Backup, Speed, & Growth v9.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sal / class.json-api-links.php

class.json-api-links.php in Jetpack – WP Security, Backup, Speed, & Growth 9.2, at sal/class.json-api-links.php

285 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once dirname( __FILE__ ) . '/../class.json-api.php';
4
5 class WPCOM_JSON_API_Links {
6 private $api;
7 private static $instance;
8 private $closest_endpoint_cache_by_version = array();
9 private $matches_by_version = array();
10 private $cache_result = null;
11
12 public static function getInstance() {
13 if ( null === self::$instance ) {
14 self::$instance = new self();
15 }
16
17 return self::$instance;
18 }
19
20 // protect these methods for singleton
21 protected function __construct() {
22 $this->api = WPCOM_JSON_API::init();
23 }
24 private function __clone() { }
25 public function __wakeup() {
26 die( "Please don't __wakeup WPCOM_JSON_API_Links" );
27 }
28
29 /**
30 * Generate a URL to an endpoint
31 *
32 * Used to construct meta links in API responses
33 *
34 * @param mixed ...$args Optional arguments to be appended to URL
35 * @return string Endpoint URL
36 **/
37 function get_link( ...$args ) {
38 $format = array_shift( $args );
39 $base = WPCOM_JSON_API__BASE;
40
41 $path = array_pop( $args );
42
43 if ( $path ) {
44 $path = '/' . ltrim( $path, '/' );
45 // tack the path onto the end of the format string
46 // have to escape %'s in the path as %% because
47 // we're about to pass it through sprintf and we don't
48 // want it to see the % as a placeholder
49 $format .= str_replace( '%', '%%', $path );
50 }
51
52 // Escape any % in args before using sprintf
53 $escaped_args = array();
54 foreach ( $args as $arg_key => $arg_value ) {
55 $escaped_args[ $arg_key ] = str_replace( '%', '%%', $arg_value );
56 }
57
58 $relative_path = vsprintf( $format, $escaped_args );
59
60 if ( ! wp_startswith( $relative_path, '.' ) ) {
61 // Generic version. Match the requested version as best we can
62 $api_version = $this->get_closest_version_of_endpoint( $format, $relative_path );
63 $base = substr( $base, 0, - 1 ) . $api_version;
64 }
65
66 // escape any % in the relative path before running it through sprintf again
67 $relative_path = str_replace( '%', '%%', $relative_path );
68 // http, WPCOM_JSON_API__BASE, ... , path
69 // %s , %s , $format, %s
70 return esc_url_raw( sprintf( "https://%s$relative_path", $base ) );
71 }
72
73 function get_me_link( $path = '' ) {
74 return $this->get_link( '/me', $path );
75 }
76
77 function get_taxonomy_link( $blog_id, $taxonomy_id, $taxonomy_type, $path = '' ) {
78 switch ( $taxonomy_type ) {
79 case 'category':
80 return $this->get_link( '/sites/%d/categories/slug:%s', $blog_id, $taxonomy_id, $path );
81
82 case 'post_tag':
83 return $this->get_link( '/sites/%d/tags/slug:%s', $blog_id, $taxonomy_id, $path );
84
85 default:
86 return $this->get_link( '/sites/%d/taxonomies/%s/terms/slug:%s', $blog_id, $taxonomy_type, $taxonomy_id, $path );
87 }
88 }
89
90 function get_media_link( $blog_id, $media_id, $path = '' ) {
91 return $this->get_link( '/sites/%d/media/%d', $blog_id, $media_id, $path );
92 }
93
94 function get_site_link( $blog_id, $path = '' ) {
95 return $this->get_link( '/sites/%d', $blog_id, $path );
96 }
97
98 function get_post_link( $blog_id, $post_id, $path = '' ) {
99 return $this->get_link( '/sites/%d/posts/%d', $blog_id, $post_id, $path );
100 }
101
102 function get_comment_link( $blog_id, $comment_id, $path = '' ) {
103 return $this->get_link( '/sites/%d/comments/%d', $blog_id, $comment_id, $path );
104 }
105
106 function get_publicize_connection_link( $blog_id, $publicize_connection_id, $path = '' ) {
107 return $this->get_link( '.1/sites/%d/publicize-connections/%d', $blog_id, $publicize_connection_id, $path );
108 }
109
110 function get_publicize_connections_link( $keyring_token_id, $path = '' ) {
111 return $this->get_link( '.1/me/publicize-connections/?keyring_connection_ID=%d', $keyring_token_id, $path );
112 }
113
114 function get_keyring_connection_link( $keyring_token_id, $path = '' ) {
115 return $this->get_link( '.1/me/keyring-connections/%d', $keyring_token_id, $path );
116 }
117
118 function get_external_service_link( $external_service, $path = '' ) {
119 return $this->get_link( '.1/meta/external-services/%s', $external_service, $path );
120 }
121
122 /**
123 * Try to find the closest supported version of an endpoint to the current endpoint
124 *
125 * For example, if we were looking at the path /animals/panda:
126 * - if the current endpoint is v1.3 and there is a v1.3 of /animals/%s available, we return 1.3
127 * - if the current endpoint is v1.3 and there is no v1.3 of /animals/%s known, we fall back to the
128 * maximum available version of /animals/%s, e.g. 1.1
129 *
130 * This method is used in get_link() to construct meta links for API responses.
131 *
132 * @param $template_path string The generic endpoint path, e.g. /sites/%s
133 * @param $path string The current endpoint path, relative to the version, e.g. /sites/12345
134 * @param $request_method string Request method used to access the endpoint path
135 * @return string The current version, or otherwise the maximum version available
136 */
137 function get_closest_version_of_endpoint( $template_path, $path, $request_method = 'GET' ) {
138 $closest_endpoint_cache_by_version = & $this->closest_endpoint_cache_by_version;
139
140 $closest_endpoint_cache = & $closest_endpoint_cache_by_version[ $this->api->version ];
141 if ( !$closest_endpoint_cache ) {
142 $closest_endpoint_cache_by_version[ $this->api->version ] = array();
143 $closest_endpoint_cache = & $closest_endpoint_cache_by_version[ $this->api->version ];
144 }
145
146 if ( ! isset( $closest_endpoint_cache[ $template_path ] ) ) {
147 $closest_endpoint_cache[ $template_path ] = array();
148 } elseif ( isset( $closest_endpoint_cache[ $template_path ][ $request_method ] ) ) {
149 return $closest_endpoint_cache[ $template_path ][ $request_method ];
150 }
151
152 $path = untrailingslashit( $path );
153
154 // /help is a special case - always use the current request version
155 if ( wp_endswith( $path, '/help' ) ) {
156 $closest_endpoint_cache[ $template_path ][ $request_method ] = $this->api->version;
157 return $this->api->version;
158 }
159
160 $matches_by_version = & $this->matches_by_version;
161
162 // try to match out of saved matches
163 if ( ! isset( $matches_by_version[ $this->api->version ] ) ) {
164 $matches_by_version[ $this->api->version ] = array();
165 }
166 foreach ( $matches_by_version[ $this->api->version ] as $match ) {
167 $regex = $match->regex;
168 if ( preg_match( "#^$regex\$#", $path ) ) {
169 $closest_endpoint_cache[ $template_path ][ $request_method ] = $match->version;
170 return $match->version;
171 }
172 }
173
174 $endpoint_path_versions = $this->get_endpoint_path_versions();
175 $last_path_segment = $this->get_last_segment_of_relative_path( $path );
176 $max_version_found = null;
177
178 foreach ( $endpoint_path_versions as $endpoint_last_path_segment => $endpoints ) {
179
180 // Does the last part of the path match the path key? (e.g. 'posts')
181 // If the last part contains a placeholder (e.g. %s), we want to carry on
182 if ( $last_path_segment != $endpoint_last_path_segment && ! strstr( $endpoint_last_path_segment, '%' ) ) {
183 continue;
184 }
185
186 foreach ( $endpoints as $endpoint ) {
187 // Does the request method match?
188 if ( ! in_array( $request_method, $endpoint['request_methods'] ) ) {
189 continue;
190 }
191
192 $endpoint_path = untrailingslashit( $endpoint['path'] );
193 $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path );
194
195 if ( ! preg_match( "#^$endpoint_path_regex\$#", $path ) ) {
196 continue;
197 }
198
199 // Make sure the endpoint exists at the same version
200 if ( version_compare( $this->api->version, $endpoint['min_version'], '>=') &&
201 version_compare( $this->api->version, $endpoint['max_version'], '<=') ) {
202 array_push(
203 $matches_by_version[ $this->api->version ],
204 (object) array( 'version' => $this->api->version, 'regex' => $endpoint_path_regex )
205 );
206 $closest_endpoint_cache[ $template_path ][ $request_method ] = $this->api->version;
207 return $this->api->version;
208 }
209
210 // If the endpoint doesn't exist at the same version, record the max version we found
211 if ( empty( $max_version_found ) || version_compare( $max_version_found['version'], $endpoint['max_version'], '<' ) ) {
212 $max_version_found = array( 'version' => $endpoint['max_version'], 'regex' => $endpoint_path_regex );
213 }
214 }
215 }
216
217 // If the endpoint version is less than the requested endpoint version, return the max version found
218 if ( ! empty( $max_version_found ) ) {
219 array_push(
220 $matches_by_version[ $this->api->version ],
221 (object) $max_version_found
222 );
223 $closest_endpoint_cache[ $template_path ][ $request_method ] = $max_version_found['version'];
224 return $max_version_found['version'];
225 }
226
227 // Otherwise, use the API version of the current request
228 return $this->api->version;
229 }
230
231 /**
232 * Get an array of endpoint paths with their associated versions
233 *
234 * @return array Array of endpoint paths, min_versions and max_versions, keyed by last segment of path
235 **/
236 protected function get_endpoint_path_versions() {
237
238 if ( ! empty ( $this->cache_result ) ) {
239 return $this->cache_result;
240 }
241
242 /*
243 * Create a map of endpoints and their min/max versions keyed by the last segment of the path (e.g. 'posts')
244 * This reduces the search space when finding endpoint matches in get_closest_version_of_endpoint()
245 */
246 $endpoint_path_versions = array();
247
248 foreach ( $this->api->endpoints as $key => $endpoint_objects ) {
249
250 // The key contains a serialized path, min_version and max_version
251 list( $path, $min_version, $max_version ) = unserialize( $key );
252
253 // Grab the last component of the relative path to use as the top-level key
254 $last_path_segment = $this->get_last_segment_of_relative_path( $path );
255
256 $endpoint_path_versions[ $last_path_segment ][] = array(
257 'path' => $path,
258 'min_version' => $min_version,
259 'max_version' => $max_version,
260 'request_methods' => array_keys( $endpoint_objects )
261 );
262 }
263
264 $this->cache_result = $endpoint_path_versions;
265
266 return $endpoint_path_versions;
267 }
268
269 /**
270 * Grab the last segment of a relative path
271 *
272 * @param string $path Path
273 * @return string Last path segment
274 */
275 protected function get_last_segment_of_relative_path( $path) {
276 $path_parts = array_filter( explode( '/', $path ) );
277
278 if ( empty( $path_parts ) ) {
279 return null;
280 }
281
282 return end( $path_parts );
283 }
284 }
285