diff options
Diffstat (limited to 'plugins/jetpack/class.json-api-endpoints.php')
-rw-r--r-- | plugins/jetpack/class.json-api-endpoints.php | 325 |
1 files changed, 40 insertions, 285 deletions
diff --git a/plugins/jetpack/class.json-api-endpoints.php b/plugins/jetpack/class.json-api-endpoints.php index 7c655ad0..934def31 100644 --- a/plugins/jetpack/class.json-api-endpoints.php +++ b/plugins/jetpack/class.json-api-endpoints.php @@ -1,12 +1,18 @@ <?php require_once( dirname( __FILE__ ) . '/json-api-config.php' ); +require_once( dirname( __FILE__ ) . '/sal/class.json-api-links.php' ); +require_once( dirname( __FILE__ ) . '/sal/class.json-api-metadata.php' ); +require_once( dirname( __FILE__ ) . '/sal/class.json-api-date.php' ); // Endpoint abstract class WPCOM_JSON_API_Endpoint { // The API Object public $api; + // The link-generating utility class + public $links; + public $pass_wpcom_user_details = false; // One liner. @@ -193,6 +199,7 @@ abstract class WPCOM_JSON_API_Endpoint { } $this->api = WPCOM_JSON_API::init(); // Auto-add to WPCOM_JSON_API + $this->links = WPCOM_JSON_API_Links::getInstance(); /** Example Request/Response ******************************************/ @@ -643,6 +650,18 @@ abstract class WPCOM_JSON_API_Endpoint { ); $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output ); break; + case 'taxonomy': + $docs = array( + 'name' => '(string) The taxonomy slug', + 'label' => '(string) The taxonomy human-readable name', + 'labels' => '(object) Mapping of labels for the taxonomy', + 'description' => '(string) The taxonomy description', + 'hierarchical' => '(bool) Whether the taxonomy is hierarchical', + 'public' => '(bool) Whether the taxonomy is public', + 'capabilities' => '(object) Mapping of current user capabilities for the taxonomy', + ); + $return[$key] = (array) $this->cast_and_filter( $value, $docs, false, $for_output ); + break; default : $method_name = $type['type'] . '_docs'; @@ -1132,9 +1151,9 @@ abstract class WPCOM_JSON_API_Endpoint { $response['meta'] = (object) array( 'links' => (object) array( - 'self' => (string) $this->get_media_link( $this->api->get_blog_id_for_output(), $media_id ), - 'help' => (string) $this->get_media_link( $this->api->get_blog_id_for_output(), $media_id, 'help' ), - 'site' => (string) $this->get_site_link( $this->api->get_blog_id_for_output() ), + 'self' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id ), + 'help' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id, 'help' ), + 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ), ), ); @@ -1244,21 +1263,21 @@ abstract class WPCOM_JSON_API_Endpoint { $response['meta'] = (object) array( 'links' => (object) array( - 'self' => (string) $this->get_media_link( $this->api->get_blog_id_for_output(), $media_id ), - 'help' => (string) $this->get_media_link( $this->api->get_blog_id_for_output(), $media_id, 'help' ), - 'site' => (string) $this->get_site_link( $this->api->get_blog_id_for_output() ), + 'self' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id ), + 'help' => (string) $this->links->get_media_link( $this->api->get_blog_id_for_output(), $media_id, 'help' ), + 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ), ), ); // add VideoPress link to the meta if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) ) ) { if ( function_exists( 'video_get_info_by_blogpostid' ) ) { - $response['meta']->links->videopress = (string) $this->get_link( '/videos/%s', $response['videopress_guid'], '' ); + $response['meta']->links->videopress = (string) $this->links->get_link( '/videos/%s', $response['videopress_guid'], '' ); } } if ( $media_item->post_parent > 0 ) { - $response['meta']->links->parent = (string) $this->get_post_link( $this->api->get_blog_id_for_output(), $media_item->post_parent ); + $response['meta']->links->parent = (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $media_item->post_parent ); } return (object) $response; @@ -1299,14 +1318,15 @@ abstract class WPCOM_JSON_API_Endpoint { $response['description'] = (string) $taxonomy->description; $response['post_count'] = (int) $taxonomy->count; - if ( 'category' === $taxonomy_type ) + if ( is_taxonomy_hierarchical( $taxonomy_type ) ) { $response['parent'] = (int) $taxonomy->parent; + } $response['meta'] = (object) array( 'links' => (object) array( - 'self' => (string) $this->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type ), - 'help' => (string) $this->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type, 'help' ), - 'site' => (string) $this->get_site_link( $this->api->get_blog_id_for_output() ), + 'self' => (string) $this->links->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type ), + 'help' => (string) $this->links->get_taxonomy_link( $this->api->get_blog_id_for_output(), $taxonomy->slug, $taxonomy_type, 'help' ), + 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ), ), ); @@ -1322,47 +1342,7 @@ abstract class WPCOM_JSON_API_Endpoint { * @return string */ function format_date( $date_gmt, $date = null ) { - $timestamp_gmt = strtotime( "$date_gmt+0000" ); - - if ( null === $date ) { - $timestamp = $timestamp_gmt; - $hours = $minutes = $west = 0; - } else { - $date_time = date_create( "$date+0000" ); - if ( $date_time ) { - $timestamp = date_format( $date_time, 'U' ); - } else { - $timestamp = 0; - } - - // "0000-00-00 00:00:00" == -62169984000 - if ( -62169984000 == $timestamp_gmt ) { - // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts - // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts - - // Try to guess the correct offset from the blog's options. - $timezone_string = get_option( 'timezone_string' ); - - if ( $timezone_string && $date_time ) { - $timezone = timezone_open( $timezone_string ); - if ( $timezone ) { - $offset = $timezone->getOffset( $date_time ); - } - } else { - $offset = 3600 * get_option( 'gmt_offset' ); - } - } else { - $offset = $timestamp - $timestamp_gmt; - } - - $west = $offset < 0; - $offset = abs( $offset ); - $hours = (int) floor( $offset / 3600 ); - $offset -= $hours * 3600; - $minutes = (int) floor( $offset / 60 ); - } - - return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); + return WPCOM_JSON_API_Date::format_date( $date_gmt, $date ); } /** @@ -1536,235 +1516,6 @@ abstract class WPCOM_JSON_API_Endpoint { } /** - * Try to find the closest supported version of an endpoint to the current endpoint - * - * For example, if we were looking at the path /animals/panda: - * - if the current endpoint is v1.3 and there is a v1.3 of /animals/%s available, we return 1.3 - * - if the current endpoint is v1.3 and there is no v1.3 of /animals/%s known, we fall back to the - * maximum available version of /animals/%s, e.g. 1.1 - * - * This method is used in get_link() to construct meta links for API responses. - * - * @param $path string The current endpoint path, relative to the version - * @param $method string Request method used to access the endpoint path - * @return string The current version, or otherwise the maximum version available - */ - function get_closest_version_of_endpoint( $path, $request_method = 'GET' ) { - - $path = untrailingslashit( $path ); - - // /help is a special case - always use the current request version - if ( wp_endswith( $path, '/help' ) ) { - return $this->api->version; - } - - static $matches; - if ( empty( $matches ) ) { - $matches = array(); - } else { - // try to match out of saved matches - foreach( $matches as $match ) { - $regex = $match->regex; - if ( preg_match( "#^$regex\$#", $path ) ) { - return $match->version; - } - } - } - - $endpoint_path_versions = $this->get_endpoint_path_versions(); - $last_path_segment = $this->get_last_segment_of_relative_path( $path ); - $max_version_found = null; - - foreach ( $endpoint_path_versions as $endpoint_last_path_segment => $endpoints ) { - - // Does the last part of the path match the path key? (e.g. 'posts') - // If the last part contains a placeholder (e.g. %s), we want to carry on - if ( $last_path_segment != $endpoint_last_path_segment && ! strstr( $endpoint_last_path_segment, '%' ) ) { - continue; - } - - foreach ( $endpoints as $endpoint ) { - // Does the request method match? - if ( ! in_array( $request_method, $endpoint['request_methods'] ) ) { - continue; - } - - $endpoint_path = untrailingslashit( $endpoint['path'] ); - $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path ); - - if ( ! preg_match( "#^$endpoint_path_regex\$#", $path ) ) { - continue; - } - - // Make sure the endpoint exists at the same version - if ( version_compare( $this->api->version, $endpoint['min_version'], '>=') && - version_compare( $this->api->version, $endpoint['max_version'], '<=') ) { - array_push( $matches, (object) array( 'version' => $this->api->version, 'regex' => $endpoint_path_regex ) ); - return $this->api->version; - } - - // If the endpoint doesn't exist at the same version, record the max version we found - if ( empty( $max_version_found ) || version_compare( $max_version_found['version'], $endpoint['max_version'], '<' ) ) { - $max_version_found = array( 'version' => $endpoint['max_version'], 'regex' => $endpoint_path_regex ); - } - } - } - - // If the endpoint version is less than the requested endpoint version, return the max version found - if ( ! empty( $max_version_found ) ) { - array_push( $matches, (object) $max_version_found ); - return $max_version_found['version']; - } - - // Otherwise, use the API version of the current request - return $this->api->version; - } - - /** - * Get an array of endpoint paths with their associated versions - * - * The result is cached for 30 minutes. - * - * @return array Array of endpoint paths, min_versions and max_versions, keyed by last segment of path - **/ - protected function get_endpoint_path_versions() { - - static $cache_result; - - if ( ! empty ( $cache_result ) ) { - return $cache_result; - } - - /* - * Create a map of endpoints and their min/max versions keyed by the last segment of the path (e.g. 'posts') - * This reduces the search space when finding endpoint matches in get_closest_version_of_endpoint() - */ - $endpoint_path_versions = array(); - - foreach ( $this->api->endpoints as $key => $endpoint_objects ) { - - // The key contains a serialized path, min_version and max_version - list( $path, $min_version, $max_version ) = unserialize( $key ); - - // Grab the last component of the relative path to use as the top-level key - $last_path_segment = $this->get_last_segment_of_relative_path( $path ); - - $endpoint_path_versions[ $last_path_segment ][] = array( - 'path' => $path, - 'min_version' => $min_version, - 'max_version' => $max_version, - 'request_methods' => array_keys( $endpoint_objects ) - ); - } - - $cache_result = $endpoint_path_versions; - - return $endpoint_path_versions; - } - - /** - * Grab the last segment of a relative path - * - * @param string $path Path - * @return string Last path segment - */ - protected function get_last_segment_of_relative_path( $path) { - $path_parts = array_filter( explode( '/', $path ) ); - - if ( empty( $path_parts ) ) { - return null; - } - - return end( $path_parts ); - } - - /** - * Generate a URL to an endpoint - * - * Used to construct meta links in API responses - * - * @param mixed $args Optional arguments to be appended to URL - * @return string Endpoint URL - **/ - function get_link() { - $args = func_get_args(); - $format = array_shift( $args ); - $base = WPCOM_JSON_API__BASE; - - $path = array_pop( $args ); - - if ( $path ) { - $path = '/' . ltrim( $path, '/' ); - } - - $args[] = $path; - - // Escape any % in args before using sprintf - $escaped_args = array(); - foreach ( $args as $arg_key => $arg_value ) { - $escaped_args[ $arg_key ] = str_replace( '%', '%%', $arg_value ); - } - - $relative_path = vsprintf( "$format%s", $escaped_args ); - - if ( ! wp_startswith( $relative_path, '.' ) ) { - // Generic version. Match the requested version as best we can - $api_version = $this->get_closest_version_of_endpoint( $relative_path ); - $base = substr( $base, 0, - 1 ) . $api_version; - } - - // escape any % in the relative path before running it through sprintf again - $relative_path = str_replace( '%', '%%', $relative_path ); - // http, WPCOM_JSON_API__BASE, ... , path - // %s , %s , $format, %s - return esc_url_raw( sprintf( "%s://%s$relative_path", $this->api->public_api_scheme, $base ) ); - } - - function get_me_link( $path = '' ) { - return $this->get_link( '/me', $path ); - } - - function get_taxonomy_link( $blog_id, $taxonomy_id, $taxonomy_type, $path = '' ) { - if ( 'category' === $taxonomy_type ) - return $this->get_link( '/sites/%d/categories/slug:%s', $blog_id, $taxonomy_id, $path ); - else - return $this->get_link( '/sites/%d/tags/slug:%s', $blog_id, $taxonomy_id, $path ); - } - - function get_media_link( $blog_id, $media_id, $path = '' ) { - return $this->get_link( '/sites/%d/media/%d', $blog_id, $media_id, $path ); - } - - function get_site_link( $blog_id, $path = '' ) { - return $this->get_link( '/sites/%d', $blog_id, $path ); - } - - function get_post_link( $blog_id, $post_id, $path = '' ) { - return $this->get_link( '/sites/%d/posts/%d', $blog_id, $post_id, $path ); - } - - function get_comment_link( $blog_id, $comment_id, $path = '' ) { - return $this->get_link( '/sites/%d/comments/%d', $blog_id, $comment_id, $path ); - } - - function get_publicize_connection_link( $blog_id, $publicize_connection_id, $path = '' ) { - return $this->get_link( '.1/sites/%d/publicize-connections/%d', $blog_id, $publicize_connection_id, $path ); - } - - function get_publicize_connections_link( $keyring_token_id, $path = '' ) { - return $this->get_link( '.1/me/publicize-connections/?keyring_connection_ID=%d', $keyring_token_id, $path ); - } - - function get_keyring_connection_link( $keyring_token_id, $path = '' ) { - return $this->get_link( '.1/me/keyring-connections/%d', $keyring_token_id, $path ); - } - - function get_external_service_link( $external_service, $path = '' ) { - return $this->get_link( '.1/meta/external-services/%s', $external_service, $path ); - } - - - /** * Check whether a user can view or edit a post type * @param string $post_type post type to check * @param string $context 'display' or 'edit' @@ -1949,7 +1700,7 @@ abstract class WPCOM_JSON_API_Endpoint { } - function handle_media_sideload( $url, $parent_post_id = 0 ) { + function handle_media_sideload( $url, $parent_post_id = 0, $type = 'any' ) { if ( ! function_exists( 'download_url' ) || ! function_exists( 'media_handle_sideload' ) ) return false; @@ -1965,8 +1716,8 @@ abstract class WPCOM_JSON_API_Endpoint { // First check to see if we get a mime-type match by file, otherwise, check to // see if WordPress supports this file as an image. If neither, then it is not supported. - if ( ! $this->is_file_supported_for_sideloading( $tmp ) && ! file_is_displayable_image( $tmp ) ) { - unlink( $tmp ); + if ( ! $this->is_file_supported_for_sideloading( $tmp ) && 'image' === $type && ! file_is_displayable_image( $tmp ) ) { + @unlink( $tmp ); return false; } @@ -2128,6 +1879,10 @@ abstract class WPCOM_JSON_API_Endpoint { return 'GET' == $this->method || ( $this->allow_unauthorized_request && in_array( $origin, $complete_access_origins ) ); } + function get_platform() { + return wpcom_get_sal_platform( $this->api->token_details ); + } + /** * Return endpoint response * |