summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/json-endpoints/class.wpcom-json-api-post-endpoint.php')
-rw-r--r--plugins/jetpack/json-endpoints/class.wpcom-json-api-post-endpoint.php844
1 files changed, 451 insertions, 393 deletions
diff --git a/plugins/jetpack/json-endpoints/class.wpcom-json-api-post-endpoint.php b/plugins/jetpack/json-endpoints/class.wpcom-json-api-post-endpoint.php
index 6b1b1dd1..6e7f39e5 100644
--- a/plugins/jetpack/json-endpoints/class.wpcom-json-api-post-endpoint.php
+++ b/plugins/jetpack/json-endpoints/class.wpcom-json-api-post-endpoint.php
@@ -1,83 +1,99 @@
-<?php
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
+/**
+ * Post Endpoint class.
+ */
abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
+ /**
+ * Post object format.
+ *
+ * @var array
+ */
public $post_object_format = array(
// explicitly document and cast all output
- 'ID' => '(int) The post ID.',
- 'site_ID' => '(int) The site ID.',
- 'author' => '(object>author) The author of the post.',
- 'date' => "(ISO 8601 datetime) The post's creation time.",
- 'modified' => "(ISO 8601 datetime) The post's most recent update time.",
- 'title' => '(HTML) <code>context</code> dependent.',
- 'URL' => '(URL) The full permalink URL to the post.',
- 'short_URL' => '(URL) The wp.me short URL.',
- 'content' => '(HTML) <code>context</code> dependent.',
- 'excerpt' => '(HTML) <code>context</code> dependent.',
- 'slug' => '(string) The name (slug) for the post, used in URLs.',
- 'guid' => '(string) The GUID for the post.',
- 'status' => array(
- 'publish' => 'The post is published.',
- 'draft' => 'The post is saved as a draft.',
- 'pending' => 'The post is pending editorial approval.',
- 'private' => 'The post is published privately',
- 'future' => 'The post is scheduled for future publishing.',
- 'trash' => 'The post is in the trash.',
+ 'ID' => '(int) The post ID.',
+ 'site_ID' => '(int) The site ID.',
+ 'author' => '(object>author) The author of the post.',
+ 'date' => "(ISO 8601 datetime) The post's creation time.",
+ 'modified' => "(ISO 8601 datetime) The post's most recent update time.",
+ 'title' => '(HTML) <code>context</code> dependent.',
+ 'URL' => '(URL) The full permalink URL to the post.',
+ 'short_URL' => '(URL) The wp.me short URL.',
+ 'content' => '(HTML) <code>context</code> dependent.',
+ 'excerpt' => '(HTML) <code>context</code> dependent.',
+ 'slug' => '(string) The name (slug) for the post, used in URLs.',
+ 'guid' => '(string) The GUID for the post.',
+ 'status' => array(
+ 'publish' => 'The post is published.',
+ 'draft' => 'The post is saved as a draft.',
+ 'pending' => 'The post is pending editorial approval.',
+ 'private' => 'The post is published privately',
+ 'future' => 'The post is scheduled for future publishing.',
+ 'trash' => 'The post is in the trash.',
'auto-draft' => 'The post is a placeholder for a new post.',
),
- 'sticky' => '(bool) Is the post sticky?',
- 'password' => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.',
- 'parent' => "(object>post_reference|false) A reference to the post's parent, if it has one.",
- 'type' => "(string) The post's post_type. Post types besides post, page and revision need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.",
- 'comments_open' => '(bool) Is the post open for comments?',
- 'pings_open' => '(bool) Is the post open for pingbacks, trackbacks?',
- 'likes_enabled' => "(bool) Is the post open to likes?",
- 'sharing_enabled' => "(bool) Should sharing buttons show on this post?",
- 'comment_count' => '(int) The number of comments for this post.',
- 'like_count' => '(int) The number of likes for this post.',
- 'i_like' => '(bool) Does the current user like this post?',
- 'is_reblogged' => '(bool) Did the current user reblog this post?',
- 'is_following' => '(bool) Is the current user following this blog?',
- 'global_ID' => '(string) A unique WordPress.com-wide representation of a post.',
- 'featured_image' => '(URL) The URL to the featured image for this post if it has one.',
- 'post_thumbnail' => '(object>attachment) The attachment object for the featured image if it has one.',
- 'format' => array(), // see constructor
- 'geo' => '(object>geo|false)',
- 'menu_order' => '(int) (Pages Only) The order pages should appear in.',
- 'publicize_URLs' => '(array:URL) Array of Twitter and Facebook URLs published by this post.',
- 'tags' => '(object:tag) Hash of tags (keyed by tag name) applied to the post.',
- 'categories' => '(object:category) Hash of categories (keyed by category name) applied to the post.',
- 'attachments' => '(object:attachment) Hash of post attachments (keyed by attachment ID).',
- 'metadata' => '(array) Array of post metadata keys and values. All unprotected meta keys are available by default for read requests. Both unprotected and protected meta keys are available for authenticated requests with access. Protected meta keys can be made available with the <code>rest_api_allowed_public_metadata</code> filter.',
- 'meta' => '(object) API result meta data',
+ 'sticky' => '(bool) Is the post sticky?',
+ 'password' => '(string) The plaintext password protecting the post, or, more likely, the empty string if the post is not password protected.',
+ 'parent' => "(object>post_reference|false) A reference to the post's parent, if it has one.",
+ 'type' => "(string) The post's post_type. Post types besides post, page and revision need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.",
+ 'comments_open' => '(bool) Is the post open for comments?',
+ 'pings_open' => '(bool) Is the post open for pingbacks, trackbacks?',
+ 'likes_enabled' => '(bool) Is the post open to likes?',
+ 'sharing_enabled' => '(bool) Should sharing buttons show on this post?',
+ 'comment_count' => '(int) The number of comments for this post.',
+ 'like_count' => '(int) The number of likes for this post.',
+ 'i_like' => '(bool) Does the current user like this post?',
+ 'is_reblogged' => '(bool) Did the current user reblog this post?',
+ 'is_following' => '(bool) Is the current user following this blog?',
+ 'global_ID' => '(string) A unique WordPress.com-wide representation of a post.',
+ 'featured_image' => '(URL) The URL to the featured image for this post if it has one.',
+ 'post_thumbnail' => '(object>attachment) The attachment object for the featured image if it has one.',
+ 'format' => array(), // see constructor
+ 'geo' => '(object>geo|false)',
+ 'menu_order' => '(int) (Pages Only) The order pages should appear in.',
+ 'publicize_URLs' => '(array:URL) Array of Twitter and Facebook URLs published by this post.',
+ 'tags' => '(object:tag) Hash of tags (keyed by tag name) applied to the post.',
+ 'categories' => '(object:category) Hash of categories (keyed by category name) applied to the post.',
+ 'attachments' => '(object:attachment) Hash of post attachments (keyed by attachment ID).',
+ 'metadata' => '(array) Array of post metadata keys and values. All unprotected meta keys are available by default for read requests. Both unprotected and protected meta keys are available for authenticated requests with access. Protected meta keys can be made available with the <code>rest_api_allowed_public_metadata</code> filter.',
+ 'meta' => '(object) API result meta data',
'current_user_can' => '(object) List of permissions. Note, deprecated in favor of `capabilities`',
- 'capabilities' => '(object) List of post-specific permissions for the user; publish_post, edit_post, delete_post',
+ 'capabilities' => '(object) List of post-specific permissions for the user; publish_post, edit_post, delete_post',
);
- // public $response_format =& $this->post_object_format;
-
- function __construct( $args ) {
+ /**
+ * Constructor function.
+ *
+ * @param string|array|object $args — Args.
+ */
+ public function __construct( $args ) {
if ( is_array( $this->post_object_format ) && isset( $this->post_object_format['format'] ) ) {
$this->post_object_format['format'] = get_post_format_strings();
}
- if ( !$this->response_format ) {
+ if ( ! $this->response_format ) {
$this->response_format =& $this->post_object_format;
}
parent::__construct( $args );
}
- function the_password_form() {
+ /**
+ * Filter to replace the password form with a simple message that the post is protected.
+ *
+ * @return string
+ */
+ public function the_password_form() {
return __( 'This post is password protected.', 'jetpack' );
}
/**
* Get a post by a specified field and value
*
- * @param string $field
- * @param string $field_value
- * @param string $context Post use context (e.g. 'display')
- * @return array Post
+ * @param string $field - the field.
+ * @param string $field_value - the field value.
+ * @param string $context Post use context (e.g. 'display').
+ * @return array|bool|WP_Error Post
**/
- function get_post_by( $field, $field_value, $context = 'display' ) {
+ public function get_post_by( $field, $field_value, $context = 'display' ) {
global $blog_id;
/** This filter is documented in class.json-api-endpoints.php */
@@ -96,26 +112,26 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
}
}
- if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'wp-windows8' ) ) {
+ if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && strpos( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), 'wp-windows8' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- we're not using this value and making changes, just checking if it exists.
remove_shortcode( 'gallery', 'gallery_shortcode' );
- add_shortcode( 'gallery', array( &$this, 'win8_gallery_shortcode' ) );
+ add_shortcode( 'gallery', array( $this, 'win8_gallery_shortcode' ) );
}
switch ( $field ) {
- case 'name' :
- $post_id = $this->get_post_id_by_name( $field_value );
- if ( is_wp_error( $post_id ) ) {
- return $post_id;
- }
- break;
- default :
- $post_id = (int) $field_value;
- break;
+ case 'name':
+ $post_id = $this->get_post_id_by_name( $field_value );
+ if ( is_wp_error( $post_id ) ) {
+ return $post_id;
+ }
+ break;
+ default:
+ $post_id = (int) $field_value;
+ break;
}
$post = get_post( $post_id, OBJECT, $context );
- if ( !$post || is_wp_error( $post ) ) {
+ if ( ! $post || is_wp_error( $post ) ) {
return new WP_Error( 'unknown_post', 'Unknown post', 404 );
}
@@ -127,23 +143,23 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
$capabilities = $this->get_current_user_capabilities( $post );
switch ( $context ) {
- case 'edit' :
- if ( ! $capabilities['edit_post'] ) {
- return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
- }
- break;
- case 'display' :
- break;
- default :
- return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
+ case 'edit':
+ if ( ! $capabilities['edit_post'] ) {
+ return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
+ }
+ break;
+ case 'display':
+ break;
+ default:
+ return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
}
$can_view = $this->user_can_view_post( $post->ID );
- if ( !$can_view || is_wp_error( $can_view ) ) {
+ if ( ! $can_view || is_wp_error( $can_view ) ) {
return $can_view;
}
- $GLOBALS['post'] = $post;
+ $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
if ( 'display' === $context ) {
setup_postdata( $post );
@@ -152,321 +168,345 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
$response = array();
$fields = null;
- if ( 'display' === $context && ! empty( $this->api->query['fields'] ) ) {
+ if ( 'display' === $context && ! empty( $this->api->query['fields'] ) ) {
$fields = array_fill_keys( array_map( 'trim', explode( ',', $this->api->query['fields'] ) ), true );
}
foreach ( array_keys( $this->post_object_format ) as $key ) {
- if ( $fields !== null && ! isset( $fields[$key] ) ) {
+ if ( $fields !== null && ! isset( $fields[ $key ] ) ) {
continue;
}
switch ( $key ) {
- case 'ID' :
- // explicitly cast all output
- $response[$key] = (int) $post->ID;
- break;
- case 'site_ID' :
- $response[$key] = (int) $this->api->get_blog_id_for_output();
- break;
- case 'author' :
- $response[$key] = (object) $this->get_author( $post, 'edit' === $context && $capabilities['edit_post'] );
- break;
- case 'date' :
- $response[$key] = (string) $this->format_date( $post->post_date_gmt, $post->post_date );
- break;
- case 'modified' :
- $response[$key] = (string) $this->format_date( $post->post_modified_gmt, $post->post_modified );
- break;
- case 'title' :
- if ( 'display' === $context ) {
- $response[$key] = (string) get_the_title( $post->ID );
- } else {
- $response[$key] = (string) htmlspecialchars_decode( $post->post_title, ENT_QUOTES );
- }
- break;
- case 'URL' :
- if ( 'revision' === $post->post_type ) {
- $response[$key] = (string) esc_url_raw( get_permalink( $post->post_parent ) );
- } else {
- $response[$key] = (string) esc_url_raw( get_permalink( $post->ID ) );
- }
- break;
- case 'short_URL' :
- $response[$key] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) );
- break;
- case 'content' :
- if ( 'display' === $context ) {
- add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
- $response[$key] = (string) $this->get_the_post_content_for_display();
- remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
- } else {
- $response[$key] = (string) $post->post_content;
- }
- break;
- case 'excerpt' :
- if ( 'display' === $context ) {
- add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
- ob_start();
- the_excerpt();
- $response[$key] = (string) ob_get_clean();
- remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
- } else {
- $response[$key] = htmlspecialchars_decode( (string) $post->post_excerpt, ENT_QUOTES );
- }
- break;
- case 'status' :
- $response[$key] = (string) get_post_status( $post->ID );
- break;
- case 'sticky' :
- $response[$key] = (bool) is_sticky( $post->ID );
- break;
- case 'slug' :
- $response[$key] = (string) $post->post_name;
- break;
- case 'guid' :
- $response[$key] = (string) $post->guid;
- break;
- case 'password' :
- $response[$key] = (string) $post->post_password;
- if ( 'edit' === $context ) {
- $response[$key] = htmlspecialchars_decode( (string) $response[$key], ENT_QUOTES );
- }
- break;
- case 'parent' : // (object|false)
- if ( $post->post_parent ) {
- $parent = get_post( $post->post_parent );
+ case 'ID':
+ // explicitly cast all output
+ $response[ $key ] = (int) $post->ID;
+ break;
+ case 'site_ID':
+ $response[ $key ] = (int) $this->api->get_blog_id_for_output();
+ break;
+ case 'author':
+ $response[ $key ] = (object) $this->get_author( $post, 'edit' === $context && $capabilities['edit_post'] );
+ break;
+ case 'date':
+ $response[ $key ] = (string) $this->format_date( $post->post_date_gmt, $post->post_date );
+ break;
+ case 'modified':
+ $response[ $key ] = (string) $this->format_date( $post->post_modified_gmt, $post->post_modified );
+ break;
+ case 'title':
if ( 'display' === $context ) {
- $parent_title = (string) get_the_title( $parent->ID );
+ $response[ $key ] = (string) get_the_title( $post->ID );
} else {
- $parent_title = (string) htmlspecialchars_decode( $post->post_title, ENT_QUOTES );
+ $response[ $key ] = (string) htmlspecialchars_decode( $post->post_title, ENT_QUOTES );
}
- $response[$key] = (object) array(
- 'ID' => (int) $parent->ID,
- 'type' => (string) $parent->post_type,
- 'link' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $parent->ID ),
- 'title' => $parent_title,
- );
- } else {
- $response[$key] = false;
- }
- break;
- case 'type' :
- $response[$key] = (string) $post->post_type;
- break;
- case 'comments_open' :
- $response[$key] = (bool) comments_open( $post->ID );
- break;
- case 'pings_open' :
- $response[$key] = (bool) pings_open( $post->ID );
- break;
- case 'likes_enabled' :
- /** This filter is documented in modules/likes.php */
- $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
- $post_likes_switched = get_post_meta( $post->ID, 'switch_like_status', true );
- $post_likes_enabled = $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
- $response[$key] = (bool) $post_likes_enabled;
- break;
- case 'sharing_enabled' :
- $show = true;
- /** This filter is documented in modules/sharedaddy/sharing-service.php */
- $show = apply_filters( 'sharing_show', $show, $post );
-
- $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
-
- if ( !empty( $switched_status ) )
- $show = false;
- $response[$key] = (bool) $show;
- break;
- case 'comment_count' :
- $response[$key] = (int) $post->comment_count;
- break;
- case 'like_count' :
- $response[$key] = (int) $this->api->post_like_count( $blog_id, $post->ID );
- break;
- case 'i_like' :
- $response[$key] = (bool) $this->api->is_liked( $blog_id, $post->ID );
- break;
- case 'is_reblogged':
- $response[$key] = (bool) $this->api->is_reblogged( $blog_id, $post->ID );
- break;
- case 'is_following':
- $response[$key] = (bool) $this->api->is_following( $blog_id );
- break;
- case 'global_ID':
- $response[$key] = (string) $this->api->add_global_ID( $blog_id, $post->ID );
- break;
- case 'featured_image' :
- if ( $is_jetpack && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
- $response[ $key ] = get_post_meta( $post->ID, '_jetpack_featured_image', true );
- } else {
- $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
- if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
- $response[ $key ] = (string) $image_attributes[0];
+ break;
+ case 'URL':
+ if ( 'revision' === $post->post_type ) {
+ $response[ $key ] = (string) esc_url_raw( get_permalink( $post->post_parent ) );
} else {
- $response[ $key ] = '';
+ $response[ $key ] = (string) esc_url_raw( get_permalink( $post->ID ) );
}
- }
- break;
- case 'post_thumbnail' :
- $response[$key] = null;
-
- $thumb_id = get_post_thumbnail_id( $post->ID );
- if ( ! empty( $thumb_id ) ) {
- $attachment = get_post( $thumb_id );
- if ( ! empty( $attachment ) )
- $featured_image_object = $this->get_attachment( $attachment );
+ break;
+ case 'short_URL':
+ $response[ $key ] = (string) esc_url_raw( wp_get_shortlink( $post->ID ) );
+ break;
+ case 'content':
+ if ( 'display' === $context ) {
+ add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
+ $response[ $key ] = (string) $this->get_the_post_content_for_display();
+ remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
+ } else {
+ $response[ $key ] = (string) $post->post_content;
+ }
+ break;
+ case 'excerpt':
+ if ( 'display' === $context ) {
+ add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
+ ob_start();
+ the_excerpt();
+ $response[ $key ] = (string) ob_get_clean();
+ remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
+ } else {
+ $response[ $key ] = htmlspecialchars_decode( (string) $post->post_excerpt, ENT_QUOTES );
+ }
+ break;
+ case 'status':
+ $response[ $key ] = (string) get_post_status( $post->ID );
+ break;
+ case 'sticky':
+ $response[ $key ] = (bool) is_sticky( $post->ID );
+ break;
+ case 'slug':
+ $response[ $key ] = (string) $post->post_name;
+ break;
+ case 'guid':
+ $response[ $key ] = (string) $post->guid;
+ break;
+ case 'password':
+ $response[ $key ] = (string) $post->post_password;
+ if ( 'edit' === $context ) {
+ $response[ $key ] = htmlspecialchars_decode( (string) $response[ $key ], ENT_QUOTES );
+ }
+ break;
+ case 'parent': /** (object|false) */
+ if ( $post->post_parent ) {
+ $parent = get_post( $post->post_parent );
+ if ( 'display' === $context ) {
+ $parent_title = (string) get_the_title( $parent->ID );
+ } else {
+ $parent_title = (string) htmlspecialchars_decode( $post->post_title, ENT_QUOTES );
+ }
+ $response[ $key ] = (object) array(
+ 'ID' => (int) $parent->ID,
+ 'type' => (string) $parent->post_type,
+ 'link' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $parent->ID ),
+ 'title' => $parent_title,
+ );
+ } else {
+ $response[ $key ] = false;
+ }
+ break;
+ case 'type':
+ $response[ $key ] = (string) $post->post_type;
+ break;
+ case 'comments_open':
+ $response[ $key ] = (bool) comments_open( $post->ID );
+ break;
+ case 'pings_open':
+ $response[ $key ] = (bool) pings_open( $post->ID );
+ break;
+ case 'likes_enabled':
+ /** This filter is documented in modules/likes.php */
+ $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
+ $post_likes_switched = get_post_meta( $post->ID, 'switch_like_status', true );
+ $post_likes_enabled = $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
+ $response[ $key ] = (bool) $post_likes_enabled;
+ break;
+ case 'sharing_enabled':
+ $show = true;
+ /** This filter is documented in modules/sharedaddy/sharing-service.php */
+ $show = apply_filters( 'sharing_show', $show, $post );
+
+ $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
+
+ if ( ! empty( $switched_status ) ) {
+ $show = false;
+ }
+ $response[ $key ] = (bool) $show;
+ break;
+ case 'comment_count':
+ $response[ $key ] = (int) $post->comment_count;
+ break;
+ case 'like_count':
+ $response[ $key ] = (int) $this->api->post_like_count( $blog_id, $post->ID );
+ break;
+ case 'i_like':
+ $response[ $key ] = (bool) $this->api->is_liked( $blog_id, $post->ID );
+ break;
+ case 'is_reblogged':
+ $response[ $key ] = (bool) $this->api->is_reblogged( $blog_id, $post->ID );
+ break;
+ case 'is_following':
+ $response[ $key ] = (bool) $this->api->is_following( $blog_id );
+ break;
+ case 'global_ID':
+ $response[ $key ] = (string) $this->api->add_global_ID( $blog_id, $post->ID );
+ break;
+ case 'featured_image':
+ if ( $is_jetpack && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
+ $response[ $key ] = get_post_meta( $post->ID, '_jetpack_featured_image', true );
+ } else {
+ $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
+ if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
+ $response[ $key ] = (string) $image_attributes[0];
+ } else {
+ $response[ $key ] = '';
+ }
+ }
+ break;
+ case 'post_thumbnail':
+ $response[ $key ] = null;
+
+ $thumb_id = get_post_thumbnail_id( $post->ID );
+ if ( ! empty( $thumb_id ) ) {
+ $attachment = get_post( $thumb_id );
+ if ( ! empty( $attachment ) ) {
+ $featured_image_object = $this->get_attachment( $attachment );
+ }
- if ( ! empty( $featured_image_object ) ) {
- $response[$key] = (object) $featured_image_object;
+ if ( ! empty( $featured_image_object ) ) {
+ $response[ $key ] = (object) $featured_image_object;
+ }
}
- }
- break;
- case 'format' :
- $response[$key] = (string) get_post_format( $post->ID );
- if ( !$response[$key] ) {
- $response[$key] = 'standard';
- }
- break;
- case 'geo' : // (object|false)
- if ( !$geo ) {
- $response[$key] = false;
- } else {
- $geo_data = $geo->get_geo( 'post', $post->ID );
- $response[$key] = false;
- if ( $geo_data ) {
- $geo_data = array_intersect_key( $geo_data, array( 'latitude' => true, 'longitude' => true, 'address' => true, 'public' => true ) );
+ break;
+ case 'format':
+ $response[ $key ] = (string) get_post_format( $post->ID );
+ if ( ! $response[ $key ] ) {
+ $response[ $key ] = 'standard';
+ }
+ break;
+ case 'geo': /** (object|false) */
+ if ( ! $geo ) {
+ $response[ $key ] = false;
+ } else {
+ $geo_data = $geo->get_geo( 'post', $post->ID );
+ $response[ $key ] = false;
if ( $geo_data ) {
- $response[$key] = (object) array(
- 'latitude' => isset( $geo_data['latitude'] ) ? (float) $geo_data['latitude'] : 0,
- 'longitude' => isset( $geo_data['longitude'] ) ? (float) $geo_data['longitude'] : 0,
- 'address' => isset( $geo_data['address'] ) ? (string) $geo_data['address'] : '',
+ $geo_data = array_intersect_key(
+ $geo_data,
+ array(
+ 'latitude' => true,
+ 'longitude' => true,
+ 'address' => true,
+ 'public' => true,
+ )
);
- } else {
- $response[$key] = false;
- }
- // Private
- if ( !isset( $geo_data['public'] ) || !$geo_data['public'] ) {
- if ( 'edit' !== $context || ! $capabilities['edit_post'] ) {
- // user can't access
- $response[$key] = false;
+ if ( $geo_data ) {
+ $response[ $key ] = (object) array(
+ 'latitude' => isset( $geo_data['latitude'] ) ? (float) $geo_data['latitude'] : 0,
+ 'longitude' => isset( $geo_data['longitude'] ) ? (float) $geo_data['longitude'] : 0,
+ 'address' => isset( $geo_data['address'] ) ? (string) $geo_data['address'] : '',
+ );
+ } else {
+ $response[ $key ] = false;
+ }
+ // Private
+ if ( ! isset( $geo_data['public'] ) || ! $geo_data['public'] ) {
+ if ( 'edit' !== $context || ! $capabilities['edit_post'] ) {
+ // user can't access
+ $response[ $key ] = false;
+ }
}
}
}
- }
- break;
- case 'menu_order':
- $response[$key] = (int) $post->menu_order;
- break;
- case 'publicize_URLs' :
- $publicize_URLs = array();
- $publicize = get_post_meta( $post->ID, 'publicize_results', true );
- if ( $publicize ) {
- foreach ( $publicize as $service => $data ) {
- switch ( $service ) {
- case 'twitter' :
- foreach ( $data as $datum ) {
- $publicize_URLs[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
+ break;
+ case 'menu_order':
+ $response[ $key ] = (int) $post->menu_order;
+ break;
+ case 'publicize_URLs':
+ $publicize_urls = array();
+ $publicize = get_post_meta( $post->ID, 'publicize_results', true );
+ if ( $publicize ) {
+ foreach ( $publicize as $service => $data ) {
+ switch ( $service ) {
+ case 'twitter':
+ foreach ( $data as $datum ) {
+ $publicize_urls[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
+ }
+ break;
+ case 'fb':
+ foreach ( $data as $datum ) {
+ $publicize_urls[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
+ }
+ break;
}
- break;
- case 'fb' :
- foreach ( $data as $datum ) {
- $publicize_URLs[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
- }
- break;
}
}
- }
- $response[$key] = (array) $publicize_URLs;
- break;
- case 'tags' :
- $response[$key] = array();
- $terms = wp_get_post_tags( $post->ID );
- foreach ( $terms as $term ) {
- if ( !empty( $term->name ) ) {
- $response[$key][$term->name] = $this->format_taxonomy( $term, 'post_tag', 'display' );
+ $response[ $key ] = (array) $publicize_urls;
+ break;
+ case 'tags':
+ $response[ $key ] = array();
+ $terms = wp_get_post_tags( $post->ID );
+ foreach ( $terms as $term ) {
+ if ( ! empty( $term->name ) ) {
+ $response[ $key ][ $term->name ] = $this->format_taxonomy( $term, 'post_tag', 'display' );
+ }
}
- }
- $response[$key] = (object) $response[$key];
- break;
- case 'categories':
- $response[$key] = array();
- $terms = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'all' ) );
- foreach ( $terms as $term ) {
- if ( !empty( $term->name ) ) {
- $response[$key][$term->name] = $this->format_taxonomy( $term, 'category', 'display' );
+ $response[ $key ] = (object) $response[ $key ];
+ break;
+ case 'categories':
+ $response[ $key ] = array();
+ $terms = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'all' ) );
+ foreach ( $terms as $term ) {
+ if ( ! empty( $term->name ) ) {
+ $response[ $key ][ $term->name ] = $this->format_taxonomy( $term, 'category', 'display' );
+ }
}
- }
- $response[$key] = (object) $response[$key];
- break;
- case 'attachments':
- $response[$key] = array();
- $_attachments = get_posts( array( 'post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'posts_per_page' => 100 ) );
- foreach ( $_attachments as $attachment ) {
- $response[$key][$attachment->ID] = $this->get_attachment( $attachment );
- }
- $response[$key] = (object) $response[$key];
- break;
- case 'metadata' : // (array|false)
- $metadata = array();
- foreach ( (array) has_meta( $post_id ) as $meta ) {
- // Don't expose protected fields.
- $show = false;
- if ( WPCOM_JSON_API_Metadata::is_public( $meta['meta_key'] ) )
- $show = true;
- if ( current_user_can( 'edit_post_meta', $post_id , $meta['meta_key'] ) )
- $show = true;
-
- if ( Jetpack_SEO_Posts::DESCRIPTION_META_KEY === $meta['meta_key'] && ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
- $show = false;
+ $response[ $key ] = (object) $response[ $key ];
+ break;
+ case 'attachments':
+ $response[ $key ] = array();
+ $_attachments = get_posts(
+ array(
+ 'post_parent' => $post->ID,
+ 'post_status' => 'inherit',
+ 'post_type' => 'attachment',
+ 'posts_per_page' => 100,
+ )
+ );
+ foreach ( $_attachments as $attachment ) {
+ $response[ $key ][ $attachment->ID ] = $this->get_attachment( $attachment );
}
+ $response[ $key ] = (object) $response[ $key ];
+ break;
+ case 'metadata': /** (array|false) */
+ $metadata = array();
+ foreach ( (array) has_meta( $post_id ) as $meta ) {
+ // Don't expose protected fields.
+ $show = false;
+ if ( WPCOM_JSON_API_Metadata::is_public( $meta['meta_key'] ) ) {
+ $show = true;
+ }
+ if ( current_user_can( 'edit_post_meta', $post_id, $meta['meta_key'] ) ) {
+ $show = true;
+ }
- if ( !$show )
- continue;
+ if ( Jetpack_SEO_Posts::DESCRIPTION_META_KEY === $meta['meta_key'] && ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
+ $show = false;
+ }
- $metadata[] = array(
- 'id' => $meta['meta_id'],
- 'key' => $meta['meta_key'],
- 'value' => maybe_unserialize( $meta['meta_value'] ),
- );
- }
+ if ( ! $show ) {
+ continue;
+ }
- if ( ! empty( $metadata ) ) {
- $response[$key] = $metadata;
- } else {
- $response[$key] = false;
- }
- break;
- case 'meta' :
- $response[$key] = (object) array(
- 'links' => (object) array(
- 'self' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
- 'help' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'help' ),
- 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
- 'replies' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'replies/' ),
- 'likes' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'likes/' ),
- ),
- );
- break;
- case 'current_user_can' :
- $response[$key] = $capabilities;
- break;
- case 'capabilities' :
- $response[$key] = $capabilities;
- break;
+ $metadata[] = array(
+ 'id' => $meta['meta_id'],
+ 'key' => $meta['meta_key'],
+ 'value' => maybe_unserialize( $meta['meta_value'] ),
+ );
+ }
+
+ if ( ! empty( $metadata ) ) {
+ $response[ $key ] = $metadata;
+ } else {
+ $response[ $key ] = false;
+ }
+ break;
+ case 'meta':
+ $response[ $key ] = (object) array(
+ 'links' => (object) array(
+ 'self' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID ),
+ 'help' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'help' ),
+ 'site' => (string) $this->links->get_site_link( $this->api->get_blog_id_for_output() ),
+ 'replies' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'replies/' ),
+ 'likes' => (string) $this->links->get_post_link( $this->api->get_blog_id_for_output(), $post->ID, 'likes/' ),
+ ),
+ );
+ break;
+ case 'current_user_can':
+ $response[ $key ] = $capabilities;
+ break;
+ case 'capabilities':
+ $response[ $key ] = $capabilities;
+ break;
}
}
- // WPCOM_JSON_API_Post_Endpoint::find_featured_worthy_media( $post );
- // $response['featured_media'] = self::find_featured_media( $response );
-
unset( $GLOBALS['post'] );
return $response;
}
- // No Blog ID parameter. No Post ID parameter. Depends on globals.
- // Expects setup_postdata() to already have been run
- function get_the_post_content_for_display() {
+ /**
+ *
+ * Get the post content for display.
+ *
+ * No Blog ID parameter. No Post ID parameter. Depends on globals.
+ * Expects setup_postdata() to already have been run.
+ *
+ * @return string|false
+ */
+ public function get_the_post_content_for_display() {
global $pages, $page;
$old_pages = $pages;
@@ -474,22 +514,30 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
$content = join( "\n\n", $pages );
$content = preg_replace( '/<!--more(.*?)?-->/', '', $content );
- $pages = array( $content );
- $page = 1;
+ $pages = array( $content ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
+ $page = 1; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
ob_start();
the_content();
$return = ob_get_clean();
- $pages = $old_pages;
- $page = $old_page;
+ $pages = $old_pages; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
+ $page = $old_page; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
return $return;
}
- function get_blog_post( $blog_id, $post_id, $context = 'display' ) {
+ /**
+ * Gets the blog post.
+ *
+ * @param int $blog_id - the blog ID.
+ * @param int $post_id - the post ID.
+ * @param string $context - the context.
+ * @return array|bool|WP_Error Post
+ */
+ public function get_blog_post( $blog_id, $post_id, $context = 'display' ) {
$blog_id = $this->api->get_blog_id( $blog_id );
- if ( !$blog_id || is_wp_error( $blog_id ) ) {
+ if ( ! $blog_id || is_wp_error( $blog_id ) ) {
return $blog_id;
}
switch_to_blog( $blog_id );
@@ -503,7 +551,7 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
* since it's calling WPCOM_JSON_API_Read_Endpoint methods which presently
* rely on wpcom specific functionality.
*
- * @param WP_Post $post
+ * @param WP_Post $post - the WP Post object.
* @return object list of featured media
*/
public static function find_featured_media( &$post ) {
@@ -516,21 +564,26 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
}
-
-
- function win8_gallery_shortcode( $attr ) {
+ /**
+ * Win8 Gallery shortcode.
+ *
+ * @param array $attr - the attribute.
+ */
+ public function win8_gallery_shortcode( $attr ) {
global $post;
static $instance = 0;
$instance++;
- $output = '';
+ // @todo - find out if this is a bug, intentionally unused, or can be removed.
+ $output = ''; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
- if ( !$attr['orderby'] )
+ if ( ! $attr['orderby'] ) {
unset( $attr['orderby'] );
+ }
}
$atts = shortcode_atts(
@@ -604,14 +657,15 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
$link = isset( $attr['link'] ) && 'file' === $attr['link']
? wp_get_attachment_link( $id, $size, false, false )
: wp_get_attachment_link( $id, $size, true, false );
-
- if ( $captiontag && trim($attachment->post_excerpt) ) {
- $output .= "<div class='wp-caption aligncenter'>$link
- <p class='wp-caption-text'>" . wptexturize($attachment->post_excerpt) . "</p>
- </div>";
+ // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
+ if ( $captiontag && trim( $attachment->post_excerpt ) ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable
+ $output .= "<div class='wp-caption aligncenter'>$link
+ <p class='wp-caption-text'>" . wptexturize( $attachment->post_excerpt ) . '</p>
+ </div>';
} else {
$output .= $link . ' ';
}
+ // phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
}
}
}
@@ -619,20 +673,20 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
/**
* Returns attachment object.
*
- * @param $attachment attachment row
+ * @param object - $attachment attachment row.
*
* @return object
*/
- function get_attachment( $attachment ) {
+ public function get_attachment( $attachment ) {
$metadata = wp_get_attachment_metadata( $attachment->ID );
$result = array(
- 'ID' => (int) $attachment->ID,
- 'URL' => (string) wp_get_attachment_url( $attachment->ID ),
- 'guid' => (string) $attachment->guid,
- 'mime_type' => (string) $attachment->post_mime_type,
- 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
- 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
+ 'ID' => (int) $attachment->ID,
+ 'URL' => (string) wp_get_attachment_url( $attachment->ID ),
+ 'guid' => (string) $attachment->guid,
+ 'mime_type' => (string) $attachment->post_mime_type,
+ 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
+ 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
);
if ( isset( $metadata['duration'] ) ) {
@@ -644,14 +698,16 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
/**
* Get post-specific user capabilities
- * @param WP_Post $post post object
- * @return array array of post-level permissions; 'publish_post', 'delete_post', 'edit_post'
+ *
+ * @param WP_Post $post - the WP_Post object.
+ *
+ * @return array - array of post-level permissions; 'publish_post', 'delete_post', 'edit_post'
*/
- function get_current_user_capabilities( $post ) {
+ public function get_current_user_capabilities( $post ) {
return array(
'publish_post' => current_user_can( 'publish_post', $post->ID ),
'delete_post' => current_user_can( 'delete_post', $post->ID ),
- 'edit_post' => current_user_can( 'edit_post', $post->ID )
+ 'edit_post' => current_user_can( 'edit_post', $post->ID ),
);
}
@@ -660,7 +716,7 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
*
* Attempts to match name on post title and page path
*
- * @param string $name
+ * @param string $name - the name of the post.
*
* @return int|object Post ID on success, WP_Error object on failure
**/
@@ -671,11 +727,13 @@ abstract class WPCOM_JSON_API_Post_Endpoint extends WPCOM_JSON_API_Endpoint {
return new WP_Error( 'invalid_post', 'Invalid post', 400 );
}
- $posts = get_posts( array(
- 'name' => $name,
- 'numberposts' => 1,
- 'post_type' => $this->_get_whitelisted_post_types(),
- ) );
+ $posts = get_posts(
+ array(
+ 'name' => $name,
+ 'numberposts' => 1,
+ 'post_type' => $this->_get_whitelisted_post_types(),
+ )
+ );
if ( ! $posts || ! isset( $posts[0]->ID ) || ! $posts[0]->ID ) {
$page = get_page_by_path( $name );