PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jetpack/sal/class.json-api-links.php

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