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