summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/class.json-api-endpoints.php')
-rw-r--r--plugins/jetpack/class.json-api-endpoints.php116
1 files changed, 81 insertions, 35 deletions
diff --git a/plugins/jetpack/class.json-api-endpoints.php b/plugins/jetpack/class.json-api-endpoints.php
index 1943d272..e1fe3a60 100644
--- a/plugins/jetpack/class.json-api-endpoints.php
+++ b/plugins/jetpack/class.json-api-endpoints.php
@@ -8,7 +8,6 @@ abstract class WPCOM_JSON_API_Endpoint {
public $api;
public $pass_wpcom_user_details = false;
- public $can_use_user_details_instead_of_blog_membership = false;
// One liner.
public $description;
@@ -69,6 +68,9 @@ abstract class WPCOM_JSON_API_Endpoint {
// Is this endpoint still allowed if the site in question is flagged?
public $allowed_if_flagged = false;
+ // Is this endpoint allowed if the site is red flagged?
+ public $allowed_if_red_flagged = false;
+
/**
* @var string Version of the API
*/
@@ -113,6 +115,7 @@ abstract class WPCOM_JSON_API_Endpoint {
$defaults = array(
'in_testing' => false,
'allowed_if_flagged' => false,
+ 'allowed_if_red_flagged' => false,
'description' => '',
'group' => '',
'method' => 'GET',
@@ -133,7 +136,6 @@ abstract class WPCOM_JSON_API_Endpoint {
'example_response' => '',
'required_scope' => '',
'pass_wpcom_user_details' => false,
- 'can_use_user_details_instead_of_blog_membership' => false,
'custom_fields_filtering' => false,
'allow_cross_origin_request' => false,
'allow_unauthorized_request' => false,
@@ -145,6 +147,7 @@ abstract class WPCOM_JSON_API_Endpoint {
$this->in_testing = $args['in_testing'];
$this->allowed_if_flagged = $args['allowed_if_flagged'];
+ $this->allowed_if_red_flagged = $args['allowed_if_red_flagged'];
$this->description = $args['description'];
$this->group = $args['group'];
@@ -162,7 +165,6 @@ abstract class WPCOM_JSON_API_Endpoint {
$this->pass_wpcom_user_details = $args['pass_wpcom_user_details'];
$this->custom_fields_filtering = (bool) $args['custom_fields_filtering'];
- $this->can_use_user_details_instead_of_blog_membership = $args['can_use_user_details_instead_of_blog_membership'];
$this->allow_cross_origin_request = (bool) $args['allow_cross_origin_request'];
$this->allow_unauthorized_request = (bool) $args['allow_unauthorized_request'];
@@ -330,7 +332,8 @@ abstract class WPCOM_JSON_API_Endpoint {
/**
* Casts $value according to $type.
* Handles fallbacks for certain values of $type when $value is not that $type
- * Currently, only handles fallback between string <-> array (two way), from string -> false (one way), and from object -> false (one way)
+ * Currently, only handles fallback between string <-> array (two way), from string -> false (one way), and from object -> false (one way),
+ * and string -> object (one way)
*
* Handles "child types" - array:URL, object:category
* array:URL means an array of URLs
@@ -351,7 +354,7 @@ abstract class WPCOM_JSON_API_Endpoint {
$return[$key] = (string) esc_url_raw( $value );
break;
case 'string' :
- // Fallback string -> array, or string -> object
+ // Fallback string -> array, or for string -> object
if ( is_array( $value ) || is_object( $value ) ) {
if ( !empty( $types[0] ) ) {
$next_type = array_shift( $types );
@@ -376,11 +379,11 @@ abstract class WPCOM_JSON_API_Endpoint {
break;
case 'media' :
if ( is_array( $value ) ) {
- if ( isset( $value['name'] ) ) {
+ if ( isset( $value['name'] ) && is_array( $value['name'] ) ) {
// It's a $_FILES array
// Reformat into array of $_FILES items
-
$files = array();
+
foreach ( $value['name'] as $k => $v ) {
$files[$k] = array();
foreach ( array_keys( $value ) as $file_key ) {
@@ -1009,6 +1012,7 @@ abstract class WPCOM_JSON_API_Endpoint {
$first_name = '';
$last_name = '';
$URL = $author->comment_author_url;
+ $avatar_URL = $this->api->get_avatar_url( $author );
$profile_URL = 'http://en.gravatar.com/' . md5( strtolower( trim( $email ) ) );
$nice = '';
$site_id = -1;
@@ -1019,7 +1023,11 @@ abstract class WPCOM_JSON_API_Endpoint {
$$field = str_replace( '&amp;', '&', $$field );
}
} else {
- if ( isset( $author->post_author ) ) {
+ if ( isset( $author->user_id ) && $author->user_id ) {
+ $author = $author->user_id;
+ } elseif ( isset( $author->user_email ) ) {
+ $author = $author->ID;
+ } elseif ( isset( $author->post_author ) ) {
// then $author is a Post Object.
if ( 0 == $author->post_author )
return null;
@@ -1047,10 +1055,6 @@ abstract class WPCOM_JSON_API_Endpoint {
} else {
$author = $author->post_author;
}
- } elseif ( isset( $author->user_id ) && $author->user_id ) {
- $author = $author->user_id;
- } elseif ( isset( $author->user_email ) ) {
- $author = $author->ID;
}
if ( ! isset( $ID ) ) {
@@ -1077,9 +1081,9 @@ abstract class WPCOM_JSON_API_Endpoint {
$profile_URL = 'http://en.gravatar.com/' . md5( strtolower( trim( $email ) ) );
$site_id = -1;
}
- }
- $avatar_URL = $this->api->get_avatar_url( $email );
+ $avatar_URL = $this->api->get_avatar_url( $email );
+ }
$email = $show_email ? (string) $email : false;
@@ -1153,6 +1157,7 @@ abstract class WPCOM_JSON_API_Endpoint {
'guid' => $media_item->guid,
'date' => (string) $this->format_date( $media_item->post_date_gmt, $media_item->post_date ),
'post_ID' => $media_item->post_parent,
+ 'author_ID' => (int) $media_item->post_author,
'file' => $file,
'mime_type' => $media_item->post_mime_type,
'extension' => $ext,
@@ -1421,15 +1426,18 @@ abstract class WPCOM_JSON_API_Endpoint {
if ( defined( 'REST_API_THEME_FUNCTIONS_LOADED' ) )
return;
+ // VIP context loading is handled elsewhere, so bail to prevent
+ // duplicate loading. See `switch_to_blog_and_validate_user()`
+ if ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) {
+ return;
+ }
+
define( 'REST_API_THEME_FUNCTIONS_LOADED', true );
// the theme info we care about is found either within functions.php or one of the jetpack files.
$function_files = array( '/functions.php', '/inc/jetpack.compat.php', '/inc/jetpack.php', '/includes/jetpack.compat.php' );
$copy_dirs = array( get_template_directory() );
- if ( wpcom_is_vip() ) {
- $copy_dirs[] = WP_CONTENT_DIR . '/themes/vip/plugins/';
- }
// Is this a child theme? Load the child theme's functions file.
if ( get_stylesheet_directory() !== get_template_directory() && wpcom_is_child_theme() ) {
@@ -1550,6 +1558,19 @@ abstract class WPCOM_JSON_API_Endpoint {
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;
@@ -1571,26 +1592,28 @@ abstract class WPCOM_JSON_API_Endpoint {
$endpoint_path = untrailingslashit( $endpoint['path'] );
$endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path );
- if ( ! preg_match( "#^$endpoint_path_regex\$#", $path, $matches ) ) {
+ 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, $endpoint['max_version'], '<' ) ) {
- $max_version_found = $endpoint['max_version'];
+ 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 ) ) {
- return $max_version_found;
+ array_push( $matches, (object) $max_version_found );
+ return $max_version_found['version'];
}
// Otherwise, use the API version of the current request
@@ -1606,8 +1629,7 @@ abstract class WPCOM_JSON_API_Endpoint {
**/
protected function get_endpoint_path_versions() {
- // Do we already have the result of this method in the cache?
- $cache_result = get_transient( 'endpoint_path_versions' );
+ static $cache_result;
if ( ! empty ( $cache_result ) ) {
return $cache_result;
@@ -1635,11 +1657,7 @@ abstract class WPCOM_JSON_API_Endpoint {
);
}
- set_transient(
- 'endpoint_path_versions',
- $endpoint_path_versions,
- (HOUR_IN_SECONDS / 2)
- );
+ $cache_result = $endpoint_path_versions;
return $endpoint_path_versions;
}
@@ -1881,21 +1899,49 @@ abstract class WPCOM_JSON_API_Endpoint {
$attrs = $media_attrs[$index];
$insert = array();
- if ( ! empty( $attrs['title'] ) ) {
+ // Attributes: Title, Caption, Description
+
+ if ( isset( $attrs['title'] ) ) {
$insert['post_title'] = $attrs['title'];
}
- if ( ! empty( $attrs['caption'] ) )
+ if ( isset( $attrs['caption'] ) ) {
$insert['post_excerpt'] = $attrs['caption'];
+ }
- if ( ! empty( $attrs['description'] ) )
+ if ( isset( $attrs['description'] ) ) {
$insert['post_content'] = $attrs['description'];
+ }
- if ( empty( $insert ) )
- continue;
+ if ( ! empty( $insert ) ) {
+ $insert['ID'] = $media_id;
+ wp_update_post( (object) $insert );
+ }
+
+ // Attributes: Alt
- $insert['ID'] = $media_id;
- wp_update_post( (object) $insert );
+ if ( isset( $attrs['alt'] ) ) {
+ $alt = wp_strip_all_tags( $attrs['alt'], true );
+ update_post_meta( $media_id, '_wp_attachment_image_alt', $alt );
+ }
+
+ // Attributes: Artist, Album
+
+ $id3_meta = array();
+
+ foreach ( array( 'artist', 'album' ) as $key ) {
+ if ( isset( $attrs[ $key ] ) ) {
+ $id3_meta[ $key ] = wp_strip_all_tags( $attrs[ $key ], true );
+ }
+ }
+
+ if ( ! empty( $id3_meta ) ) {
+ // Before updating metadata, ensure that the item is audio
+ $item = $this->get_media_item_v1_1( $media_id );
+ if ( 0 === strpos( $item->mime_type, 'audio/' ) ) {
+ wp_update_attachment_metadata( $media_id, $id3_meta );
+ }
+ }
}
}