<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
* This class wraps a WP_Post and proxies any undefined attributes
* and methods to the wrapped class. We need to do this because at present
* the WP_Post class is marked as final (in 4.5 this will change, though it's
* not clear if there will be a mechanism to retrieve from the DB into the over-
* ridden class dynamically).
* @package automattic/jetpack
use Automattic\Jetpack\Status;
if ( ! defined( 'ABSPATH' ) ) {
require_once __DIR__ . '/class.json-api-metadata.php';
require_once __DIR__ . '/class.json-api-date.php';
require_once ABSPATH . 'wp-admin/includes/post.php';
require_once ABSPATH . 'wp-includes/post.php';
* Base class for SAL_Post.
abstract class SAL_Post {
* The post request context (for example 'edit' or 'display')
* A Jetpack_Site instance.
* @param Jetpack_Site $site A Jetpack_Site instance.
* @param WP_Post $post A WP_Post instance.
* @param string $context The post request context (for example 'edit' or 'display').
public function __construct( $site, $post, $context ) {
$this->context = $context;
* Setting this WP_Post instance's key value
* @param string $key The post key to set.
* @param string $value The value to set the post key to (for example filter, ID, post_status).
public function __set( $key, $value ) {
$this->post->{ $key } = $value;
* Returning a WPCOM_JSON_API_Links instance if the post key is set to 'links', or the post key value.
* @param string $key The post key value.
* @return WPCOM_JSON_API_Links|string
public function __get( $key ) {
if ( 'links' === $key ) {
require_once __DIR__ . '/class.json-api-links.php';
return WPCOM_JSON_API_Links::getInstance();
return $this->post->{ $key };
* A function to either call a given function, or return an error if it doesn't exist.
* @param string $name A function name to be called.
* @param mixed $arguments Arguments to be passed into the given function.
public function __call( $name, $arguments ) {
if ( is_callable( array( $this->post, $name ) ) ) {
return call_user_func_array( array( $this->post, $name ), $arguments );
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
/* translators: %s is the method name that has been called */
__( 'Call to undefined method %s', 'jetpack' ),
* Checking to see if a given property is set.
* @param string $name Property to check if set.
public function __isset( $name ) {
return isset( $this->post->{ $name } );
* Defining a base get_like_count() function to be extended in the Jetpack_Post class.
* This will define a default value for the like counts on a post, if this hasn't been defined yet.
* @see class.json-api-post-jetpack.php
abstract public function get_like_count();
* Defining a base is_liked() function to be extended in the Jetpack_Post class.
* This will define a default value for whether or not the current user likes this post, if this hasn't been defined yet.
* @see class.json-api-post-jetpack.php
abstract public function is_liked();
* Defining a base is_reblogged() function to be extended in the Jetpack_Post class.
* This will define a default value for whether or not the current user reblogged this post, if this hasn't been defined yet.
* @see class.json-api-post-jetpack.php
abstract public function is_reblogged();
* Defining a base is_following() function to be extended in the Jetpack_Post class.
* This will define a default value for whether or not the current user is following this blog, if this hasn't been defined yet.
* @see class.json-api-post-jetpack.php
abstract public function is_following();
* Defining a base get_global_id() function to be extended in the Jetpack_Post class.
* This will define the unique WordPress.com-wide representation of a post, if this hasn't been defined yet.
* @see class.json-api-post-jetpack.php
abstract public function get_global_id();
* Defining a base get_geo() function to be extended in the Jetpack_Post class.
* This will define a default value for whether or not there is gelocation data for this post, if this hasn't been defined yet.
* @see class.json-api-post-jetpack.php
abstract public function get_geo();
* Returns an int which helps define the menu order for the post.
public function get_menu_order() {
return (int) $this->post->menu_order;
* Returns a string which represents the post's GUID.
public function get_guid() {
return (string) $this->post->guid;
* Returns a string which represents the post type.
public function get_type() {
return (string) $this->post->post_type;
* Returns an object which holds the terms associated with that post object.
public function get_terms() {
$taxonomies = get_object_taxonomies( $this->post, 'objects' );
foreach ( $taxonomies as $taxonomy ) {
if ( ! $taxonomy->public && ! current_user_can( $taxonomy->cap->assign_terms ) ) {
$terms[ $taxonomy->name ] = array();
$taxonomy_terms = wp_get_object_terms( $this->post->ID, $taxonomy->name, array( 'fields' => 'all' ) );
foreach ( $taxonomy_terms as $term ) {
$formatted_term = $this->format_taxonomy( $term, $taxonomy->name, 'display' );
$terms[ $taxonomy->name ][ $term->name ] = $formatted_term;
$terms[ $taxonomy->name ] = (object) $terms[ $taxonomy->name ];
* Returns an object which holds the posts tag details
public function get_tags() {
$terms = wp_get_post_tags( $this->post->ID );
foreach ( $terms as $term ) {
if ( ! empty( $term->name ) ) {
$tags[ $term->name ] = $this->format_taxonomy( $term, 'post_tag', 'display' );
* Returns an object which holds the posts category details
public function get_categories() {
$terms = wp_get_object_terms( $this->post->ID, 'category', array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! empty( $term->name ) ) {
$categories[ $term->name ] = $this->format_taxonomy( $term, 'category', 'display' );
return (object) $categories;
* Returns an array of objects which hold the posts attachment information and numbers representing how many associated posts are found.
public function get_attachments_and_count() {
$_attachments = new WP_Query(
'post_parent' => $this->post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '20',
foreach ( $_attachments->posts as $attachment ) {
$attachments[ $attachment->ID ] = $this->get_media_item_v1_1( $attachment->ID );
return array( (object) $attachments, (int) $_attachments->found_posts );
* Returns an array with a posts metadata information.
public function get_metadata() {
foreach ( (array) has_meta( $this->post->ID ) as $meta ) {
// Don't expose protected fields.
$meta_key = $meta['meta_key'];
$show = ! ( WPCOM_JSON_API_Metadata::is_internal_only( $meta_key ) )
WPCOM_JSON_API_Metadata::is_public( $meta_key )
current_user_can( 'edit_post_meta', $this->post->ID, $meta_key )
if ( Jetpack_SEO_Posts::DESCRIPTION_META_KEY === $meta_key && ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
'id' => $meta['meta_id'],
'key' => $meta['meta_key'],
'value' => $this->safe_maybe_unserialize( $meta['meta_value'] ),
* Returns an object with a posts link meta details.
public function get_meta() {
'links' => (object) array(
'self' => (string) $this->get_post_link(),
'help' => (string) $this->get_post_link( 'help' ),
'site' => (string) $this->get_site_link(),
'replies' => (string) $this->get_post_link( 'replies/' ),
'likes' => (string) $this->get_post_link( 'likes/' ),
$amp_permalink = get_post_meta( $this->post->ID, '_jetpack_amp_permalink', true );
if ( ! empty( $amp_permalink ) ) {
$meta->links->amp = (string) $amp_permalink;
// add autosave link if a more recent autosave exists.
if ( 'edit' === $this->context ) {
$autosave = wp_get_post_autosave( $this->post->ID );
if ( $autosave && $autosave->post_modified > $this->post->post_modified ) {
$meta->links->autosave = (string) $this->get_post_link() . '/autosave';
* Returns an array with the current user's publish, deletion and edit capabilities.
public function get_current_user_capabilities() {
'publish_post' => current_user_can( 'publish_post', $this->post->ID ),
'delete_post' => current_user_can( 'delete_post', $this->post->ID ),
'edit_post' => current_user_can( 'edit_post', $this->post->ID ),
* Returns an array with post revision ids, or false if 'edit' isn't the current post request context.
public function get_revisions() {
if ( 'edit' !== $this->context ) {
'post_type' => 'revision',
'fields' => 'ids', // Fetch only the IDs.
'post_parent' => $this->post->ID,
$revision_query = new WP_Query( $args );
return $revision_query->posts; // This returns an array of revision IDs.
* Returns an object with extra post permalink suggestions.
public function get_other_urls() {
if ( 'publish' !== $this->post->post_status ) {
$other_urls = $this->get_permalink_suggestions( $this->post->post_title );
return (object) $other_urls;
* Calls the WPCOM_JSON_API_Links get_site_link() function to generate a site link endpoint URL.
* @return string Endpoint URL including site information.
protected function get_site_link() {
return $this->links->get_site_link( $this->site->get_id() );
* Calls the WPCOM_JSON_API_Links get_post_link() function to generate a posts endpoint URL.
* @param string $path Optional path to be appended to the URL.
* @return string Endpoint URL including post information.
protected function get_post_link( $path = null ) {
return $this->links->get_post_link( $this->site->get_id(), $this->post->ID, $path );
* Returns an array of user and post specific social media post URLs.
public function get_publicize_urls() {
$publicize_urls = array();
$publicize = get_post_meta( $this->post->ID, 'publicize_results', true );
// get_post_meta(..., true) will return a string if the value was stored as a scalar or serialized, so we may need to unserialize.
if ( is_string( $publicize ) ) {
$maybe_array_publicize = maybe_unserialize( $publicize );
if ( ! is_array( $maybe_array_publicize ) ) {
$maybe_array_publicize = json_decode( $publicize, true );
if ( is_array( $maybe_array_publicize ) ) {
$publicize = $maybe_array_publicize;
if ( is_array( $publicize ) ) {
foreach ( $publicize as $service => $data ) {
// @todo explore removing once Twitter is removed from Publicize.
foreach ( $data as $datum ) {
if ( isset( $datum['user_id'] ) && isset( $datum['post_id'] ) ) {
$publicize_urls[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
foreach ( $data as $datum ) {
if ( isset( $datum['user_id'] ) && isset( $datum['post_id'] ) ) {
$publicize_urls[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
return (array) $publicize_urls;
* Returns a string with the page's custom template metadata.
public function get_page_template() {
return (string) get_post_meta( $this->post->ID, '_wp_page_template', true );
* Returns a string representing the source URL of a post's featured image (or an empty string otherwise).
* Note - this is overridden in jetpack-shadow
public function get_featured_image() {
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $this->post->ID ), 'full' );
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
return (string) $image_attributes[0];
* Returns an object representing a post's featured image thumbnail image.
public function get_post_thumbnail() {