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

Sync: Add links when expanding posts at a later stage #37926

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Sync:Moved the add links part of the expand posts functionality in dedicated sync requests to before_send insetad of before_enqueue
2 changes: 1 addition & 1 deletion projects/packages/sync/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '3.1.3';
const PACKAGE_VERSION = '3.1.4-alpha';

const PACKAGE_SLUG = 'sync';

Expand Down
87 changes: 63 additions & 24 deletions projects/packages/sync/src/modules/class-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ public function init_full_sync_listeners( $callable ) {
* @access public
*/
public function init_before_send() {

add_filter( 'jetpack_sync_before_send_jetpack_sync_save_post', array( $this, 'filter_jetpack_sync_before_send_jetpack_sync_save_post' ) );
add_filter( 'jetpack_sync_before_send_jetpack_published_post', array( $this, 'filter_jetpack_sync_before_send_jetpack_published_post' ) );

// meta.
add_filter( 'jetpack_sync_before_send_added_post_meta', array( $this, 'trim_post_meta' ) );
add_filter( 'jetpack_sync_before_send_updated_post_meta', array( $this, 'trim_post_meta' ) );
Expand Down Expand Up @@ -324,18 +328,6 @@ public function trim_post_meta( $args ) {
return array( $meta_id, $object_id, $meta_key, $meta_value );
}

/**
* Process content before send.
*
* @param array $args Arguments of the `wp_insert_post` hook.
*
* @return array
*/
public function expand_jetpack_sync_save_post( $args ) {
list( $post_id, $post, $update, $previous_state ) = $args;
return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
}

/**
* Filter all blacklisted post types and add filtered post content.
*
Expand All @@ -349,7 +341,7 @@ public function filter_jetpack_sync_before_enqueue_jetpack_sync_save_post( $args
return false;
}

return array( $post_id, $this->filter_post_content_and_add_links( $post ), $update, $previous_state );
return array( $post_id, $this->filter_post_content( $post ), $update, $previous_state );
}

/**
Expand All @@ -360,8 +352,32 @@ public function filter_jetpack_sync_before_enqueue_jetpack_sync_save_post( $args
*/
public function filter_jetpack_sync_before_enqueue_jetpack_published_post( $args ) {
list( $post_id, $flags, $post ) = $args;
if ( in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) {
return false;
}
return array( $post_id, $flags, $this->filter_post_content( $post ) );
}

/**
* Add filtered post content.
*
* @param array $args Hook arguments.
* @return array Hook arguments.
*/
public function filter_jetpack_sync_before_send_jetpack_published_post( $args ) {
list( $post_id, $flags, $filtered_post ) = $args;
return array( $post_id, $flags, $this->add_links( $filtered_post ) );
}

return array( $post_id, $flags, $this->filter_post_content_and_add_links( $post ) );
/**
* Add filtered post content.
*
* @param array $args Hook arguments.
* @return array Hook arguments.
*/
public function filter_jetpack_sync_before_send_jetpack_sync_save_post( $args ) {
list( $post_id, $filtered_post, $update, $previous_state ) = $args;
return array( $post_id, $this->add_links( $filtered_post ), $update, $previous_state );
}

/**
Expand Down Expand Up @@ -451,11 +467,21 @@ public function add_embed() {
}

/**
* Expands wp_insert_post to include filtered content
* Expands wp_insert_post to include filtered content andd add links
*
* @param \WP_Post $post_object Post object.
*/
public function filter_post_content_and_add_links( $post_object ) {
$filtered_post = $this->filter_post_content( $post_object );
return $this->add_links( $filtered_post );
}

/**
* Expands wp_insert_post to include filtered content
*
* @param \WP_Post $post_object Post object.
*/
public function filter_post_content( $post_object ) {
global $post;

// Used to restore the post global.
Expand Down Expand Up @@ -572,6 +598,27 @@ public function filter_post_content_and_add_links( $post_object ) {

$this->add_embed();

$filtered_post = $post;

// Restore global post.
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = $current_post;

return $filtered_post;
}

/**
* Add links to the post object.
*
* @param \WP_Post $post Post object.
* @return \WP_Post Post object with added links.
*/
public function add_links( $post ) {

if ( isset( $post->post_status ) && in_array( $post->post_status, array( 'jetpack_sync_non_registered_post_type', 'jetpack_sync_blocked' ), true ) ) {
return $post;
}

if ( has_post_thumbnail( $post->ID ) ) {
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
Expand All @@ -585,14 +632,7 @@ public function filter_post_content_and_add_links( $post_object ) {
if ( function_exists( 'amp_get_permalink' ) ) {
$post->amp_permalink = amp_get_permalink( $post->ID );
}

$filtered_post = $post;

// Restore global post.
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = $current_post;

return $filtered_post;
return $post;
}

/**
Expand Down Expand Up @@ -742,7 +782,6 @@ public function send_published( $post_ID, $post ) {

// Only Send Pulished Post event if post_type is not blacklisted.
if ( ! in_array( $post->post_type, Settings::get_setting( 'post_types_blacklist' ), true ) ) {

/**
* Action that gets synced when a post type gets published.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: major

Sync:Moved the add links part of the expand posts functionality in dedicated sync requests to before_send instead of before_enqueue
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ protected function assertDataIsSynced() {
$local_posts = array_map(
array(
$posts_sync_module,
'filter_post_content_and_add_links',
'filter_post_content',
),
$local->get_posts()
);
$local_posts = array_map(
array(
$posts_sync_module,
'add_links',
),
$local->get_posts()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public function test_post_content_limit() {
'@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module';

$this->post->post_content = str_repeat( 'X', Automattic\Jetpack\Sync\Modules\Posts::MAX_POST_CONTENT_LENGTH - 1 );
$filtered_post = $post_sync_module->filter_post_content_and_add_links( $this->post );
$filtered_post = $post_sync_module->filter_post_content( $this->post );
$this->assertNotEmpty( $filtered_post->post_content, 'Filtered post content is empty for stings of allowed length.' );

$this->post->post_content = str_repeat( 'X', Automattic\Jetpack\Sync\Modules\Posts::MAX_POST_CONTENT_LENGTH );
$filtered_post = $post_sync_module->filter_post_content_and_add_links( $this->post );
$filtered_post = $post_sync_module->filter_post_content( $this->post );
$this->assertEmpty( $filtered_post->post_content, 'Filtered post content is not truncated (empty) for stings larger than allowed length.' );
}

Expand Down Expand Up @@ -523,13 +523,13 @@ public function test_sync_post_filter_restores_global_post() {

$post_sync_module = Modules::get_module( 'posts' );
'@phan-var \Automattic\Jetpack\Sync\Modules\Posts $post_sync_module';
$post_sync_module->filter_post_content_and_add_links( $this->post );
$post_sync_module->filter_post_content( $this->post );

$this->assertSame( $post_id, $post->ID );

// Test with post global not set.
$post = null;
$post_sync_module->filter_post_content_and_add_links( $this->post );
$post_sync_module->filter_post_content( $this->post );
$this->assertNull( $post );
}

Expand Down Expand Up @@ -666,6 +666,8 @@ public function test_do_not_sync_non_existant_post_types() {
$this->assertSame( '', $synced_post->post_content_filtered );
$this->assertSame( '', $synced_post->post_excerpt_filtered );
$this->assertEquals( 'does_not_exist', $synced_post->post_type );
$this->assertObjectNotHasProperty( 'permalink', $synced_post );
$this->assertObjectNotHasProperty( 'shortlink', $synced_post );
}

/**
Expand Down Expand Up @@ -721,6 +723,8 @@ public function test_sync_post_jetpack_sync_prevent_sending_post_data_filter() {
$this->assertTrue( strtotime( $this->post->post_modified_gmt ) <= strtotime( $post->post_modified_gmt ) );
$this->assertEquals( 'jetpack_sync_blocked', $post->post_status );
$this->assertEquals( 'post', $post->post_type );
$this->assertObjectNotHasProperty( 'permalink', $post );
$this->assertObjectNotHasProperty( 'shortlink', $post );

// Since the filter is not there any more the sync should happen as expected.
$this->post->post_content = 'foo bar';
Expand All @@ -730,6 +734,8 @@ public function test_sync_post_jetpack_sync_prevent_sending_post_data_filter() {
$synced_post = $this->server_replica_storage->get_post( $this->post->ID );
// no we sync the content and it looks like what we expect to be.
$this->assertEquals( $this->post->post_content, $synced_post->post_content );
$this->assertObjectHasProperty( 'permalink', $synced_post );
$this->assertObjectHasProperty( 'shortlink', $synced_post );
}

/**
Expand Down
Loading