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

Ensure post abstraction methods use correct site ID. #1010

Merged
merged 14 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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: 77 additions & 37 deletions includes/classes/DistributorPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class DistributorPost {
*/
public $connection_id = 0;

/**
* The site ID of this post.
*
* @var int
*/
public $site_id = 0;

/**
* The source site data for internal connections.
*
Expand Down Expand Up @@ -122,7 +129,8 @@ public function __construct( $post ) {
return;
}

$this->post = $post;
$this->post = $post;
$this->site_id = get_current_blog_id();

/*
* The original post ID is listed as excluded post meta and therefore
Expand Down Expand Up @@ -178,6 +186,74 @@ public function __construct( $post ) {
}
}

/**
* Magic method for calling methods on the post object.
*
* This is used to ensure the post object is switched to the correct site before
* running any of the internal methods.
*
* @param string $name Method name.
* @param array $arguments Method arguments.
*/
public function __call( $name, $arguments ) {
$switched = false;
if ( ! method_exists( $this, $name ) ) {
// Emulate default behavior of calling non existent method (a fatal error).
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error, WordPress.Security.EscapeOutput -- class and name are safe.
trigger_error( 'Call to undefined method ' . __CLASS__ . '::' . $name . '()', E_USER_ERROR );
}

if ( get_current_blog_id() !== $this->site_id ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- throwing a warning is the correct behavior.
trigger_error( 'DistributorPost object is not on the correct site.', E_USER_WARNING );
}
switch_to_blog( $this->site_id );
$switched = true;
}
$result = call_user_func_array( array( $this, $name ), $arguments );
if ( $switched ) {
restore_current_blog();
}
return $result;
}

/**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

::__get() and ::__isset() methods are unchanged but have been relocated to group all the magic methods together.

* Magic getter method.
*
* This method is used to get the value of the `source_site` property and
* populate it if needs be. For internal connections the post permalink is
* updated with live data.
*
* @param string $name Property name.
* @return mixed
*/
public function __get( $name ) {
if ( in_array( $name, array( 'source_site', 'original_post_url' ), true ) ) {
$this->populate_source_site();
}

return $this->$name;
}

/**
* Magic isset method.
*
* This method is used to check if the `source_site` property is set and
* populate it if needs be.
*
* @param string $name Property name.
* @return bool
*/
public function __isset( $name ) {
if ( 'source_site' === $name && empty( $this->source_site ) ) {
$this->populate_source_site();
return ! empty( $this->source_site );
}

return isset( $this->$name );
}

/**
* Populate the source site data for internal connections.
*
Expand Down Expand Up @@ -232,42 +308,6 @@ protected function populate_source_site() {
}
}

/**
* Magic getter method.
*
* This method is used to get the value of the `source_site` property and
* populate it if needs be. For internal connections the post permalink is
* updated with live data.
*
* @param string $name Property name.
* @return mixed
*/
public function __get( $name ) {
if ( in_array( $name, array( 'source_site', 'original_post_url' ), true ) ) {
$this->populate_source_site();
}

return $this->$name;
}

/**
* Magic isset method.
*
* This method is used to check if the `source_site` property is set and
* populate it if needs be.
*
* @param string $name Property name.
* @return bool
*/
public function __isset( $name ) {
if ( 'source_site' === $name && empty( $this->source_site ) ) {
$this->populate_source_site();
return ! empty( $this->source_site );
}

return isset( $this->$name );
}

/**
* Determines whether the post has blocks.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/php/DistributorPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public function setup_common() {
]
);

\WP_Mock::userFunction(
'get_current_blog_id',
[
'return' => 1,
]
);

// Return voids.
\WP_Mock::userFunction( '_prime_post_caches' );
\WP_Mock::userFunction( 'update_object_term_cache' );
Expand Down
6 changes: 6 additions & 0 deletions tests/php/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class HooksTest extends TestCase {
public function setUp(): void {
parent::setUp();

\WP_Mock::userFunction(
'get_current_blog_id',
[
'return' => 1,
]
);

// Return voids.
\WP_Mock::userFunction( '_prime_post_caches' );
Expand Down