Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider multisite for posts abstraction. #1004

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 104 additions & 10 deletions includes/classes/DistributorPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ class DistributorPost {
*/
private $source_site = [];

/**
* The site ID for the current site.
*
* @var int
*/
public $site_id;
protected $switched = array();

/**
* Initialize the DistributorPost object.
*
Expand Down Expand Up @@ -176,6 +184,52 @@ public function __construct( $post ) {
$this->connection_direction = 'pulled';
$this->connection_id = (int) get_post_meta( $post->ID, 'dt_original_source_id', true );
}

$this->site_id = get_current_blog_id();

if ( ! is_multisite() ) {
return;
}

/*
* Populate the source site data for internal connections.
*
* On multisite installations, call each of the methods using a static
* for internal caching to account for site switching.
*
* This ensures the data returned is for the post used to create this
* object. If the method is subsequently called after `switch_to_blog()`
* then the returned data will relate to the initial site rather than
* the switched site.
*/
// $this->get_permalink();
// $this->get_post_thumbnail_id();
// $this->get_post_thumbnail_url();
// $this->get_the_post_thumbnail();
// $this->get_meta();
// $this->get_terms();
}

protected function maybe_switch_blog() {
if ( ! is_multisite() ) {
return;
}

if ( get_current_blog_id() === $this->site_id ) {
$this->switched[] = false;
return;
}

$this->switched[] = true;
switch_to_blog( $this->site_id );
}

protected function maybe_restore_blog() {
if ( empty( $this->switched ) || ! array_pop( $this->switched ) ) {
return;
}

restore_current_blog();
}

/**
Expand Down Expand Up @@ -314,7 +368,10 @@ public function get_the_id() {
* @return string Post permalink.
*/
public function get_permalink() {
return get_permalink( $this->post );
$this->maybe_switch_blog();
$permalink = get_permalink( $this->post );
$this->maybe_restore_blog();
return $permalink;
}

/**
Expand All @@ -323,7 +380,7 @@ public function get_permalink() {
* @return string Post type.
*/
public function get_post_type() {
return get_post_type( $this->post );
return $this->post->post_type;
}

/**
Expand All @@ -332,7 +389,10 @@ public function get_post_type() {
* @return int|false Post thumbnail ID or false if no thumbnail is set.
*/
public function get_post_thumbnail_id() {
return get_post_thumbnail_id( $this->post );
$this->maybe_switch_blog();
$thumbnail_id = get_post_thumbnail_id( $this->post );
$this->maybe_restore_blog();
return $thumbnail_id;
}

/**
Expand All @@ -342,7 +402,10 @@ public function get_post_thumbnail_id() {
* @return string|false The post's thumbnail URL or false if no thumbnail is set.
*/
public function get_post_thumbnail_url( $size = 'post-thumbnail' ) {
return get_the_post_thumbnail_url( $this->post, $size );
$this->maybe_switch_blog();
$thumbnail_url = get_the_post_thumbnail_url( $this->post, $size );
$this->maybe_restore_blog();
return $thumbnail_url;
}

/**
Expand All @@ -353,7 +416,10 @@ public function get_post_thumbnail_url( $size = 'post-thumbnail' ) {
* @return string|false The post's thumbnail HTML or false if no thumbnail is set.
*/
public function get_the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
return get_the_post_thumbnail( $this->post, $size, $attr );
$this->maybe_switch_blog();
$thumbnail = get_the_post_thumbnail( $this->post, $size, $attr );
$this->maybe_restore_blog();
return $thumbnail;
}

/**
Expand Down Expand Up @@ -437,7 +503,11 @@ public function get_author_link( $author_link = '' ) {
|| ! $this->original_post_url
) {
if ( empty( $author_link ) ) {
return get_author_posts_url( $this->post->post_author );
$this->maybe_switch_blog();
$author_posts_url = get_author_posts_url( $this->post->post_author );
$this->maybe_restore_blog();

return $author_posts_url;
}
return $author_link;
}
Expand All @@ -452,7 +522,11 @@ public function get_author_link( $author_link = '' ) {
* @return array Array of meta data.
*/
public function get_meta() {
return Utils\prepare_meta( $this->post->ID );
$this->maybe_switch_blog();
$meta = Utils\prepare_meta( $this->post->ID );
$this->maybe_restore_blog();

return $meta;
}

/**
Expand All @@ -463,7 +537,11 @@ public function get_meta() {
* }
*/
public function get_terms() {
return Utils\prepare_taxonomy_terms( $this->post->ID );
$this->maybe_switch_blog();
$terms = Utils\prepare_taxonomy_terms( $this->post->ID );
$this->maybe_restore_blog();

return $terms;
}

/**
Expand All @@ -472,6 +550,8 @@ public function get_terms() {
* @return array
*/
public function get_media() {
$this->maybe_switch_blog();

$post_id = $this->post->ID;
if ( $this->has_blocks() ) {
$raw_media = $this->parse_media_blocks();
Expand Down Expand Up @@ -500,6 +580,7 @@ public function get_media() {
$media_array[] = $featured_image;
}

$this->maybe_restore_blog();
return $media_array;
}

Expand Down Expand Up @@ -540,11 +621,13 @@ public function parse_media_blocks() {
*
* See https://core.trac.wordpress.org/ticket/57163
*/
$this->maybe_switch_blog();
_prime_post_caches( $media, false, false );
update_object_term_cache( $media, 'attachment' );
update_postmeta_cache( $media );
$media = array_map( 'get_post', $media );
$media = array_filter( $media );
$this->maybe_restore_blog();

return $media;
}
Expand Down Expand Up @@ -615,7 +698,9 @@ private function parse_blocks_for_attachment_id( $block ) {
* }
*/
public function post_data() {
return [
$this->maybe_switch_blog();

$post_data = [
'title' => html_entity_decode( get_the_title( $this->post->ID ), ENT_QUOTES, get_bloginfo( 'charset' ) ),
'slug' => $this->post->post_name,
'post_type' => $this->post->post_type,
Expand All @@ -625,6 +710,10 @@ public function post_data() {
'distributor_terms' => $this->get_terms(),
'distributor_meta' => $this->get_meta(),
];

$this->maybe_restore_blog();

return $post_data;
}

/**
Expand All @@ -648,6 +737,7 @@ public function post_data() {
* }
*/
public function to_insert() {
$this->maybe_switch_blog();
$insert = [];
$post_data = $this->post_data();
$key_mappings = [
Expand All @@ -667,6 +757,7 @@ public function to_insert() {
$insert[ $key ] = $post_data[ $value ];
}

$this->maybe_restore_blog();
return $insert;
}

Expand All @@ -678,6 +769,7 @@ public function to_insert() {
* @return string JSON encoded post data.
*/
public function to_json( $options = 0, $depth = 512 ) {
$this->maybe_switch_blog();
$post_data = $this->post_data();

/*
Expand All @@ -691,6 +783,8 @@ public function to_json( $options = 0, $depth = 512 ) {
$post_data['distributor_raw_content'] = $this->post->post_content;
}

return wp_json_encode( $post_data, $options, $depth );
$result = wp_json_encode( $post_data, $options, $depth );
$this->maybe_restore_blog();
return $result;
}
}