Skip to content

Commit

Permalink
Merge pull request #6542 from Automattic/update/jetpack_published_pos…
Browse files Browse the repository at this point in the history
…t_logic

Sync: Don't clear $this->just_published completely
  • Loading branch information
lezama authored Mar 3, 2017
2 parents e53ab5c + 7ee951f commit 6a83fb5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 90 deletions.
24 changes: 4 additions & 20 deletions modules/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ function subscription_post_page_metabox() {

global $post;
$disable_subscribe_value = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true );
// Nonce it
wp_nonce_field( 'disable_subscribe', 'disable_subscribe_nonce' );
// only show checkbox if post hasn't been published and is a 'post' post type.
if ( get_post_status( $post->ID ) !== 'publish' && get_post_type( $post->ID ) == 'post' ) : ?>
if ( get_post_status( $post->ID ) !== 'publish' && get_post_type( $post->ID ) == 'post' ) :
// Nonce it
wp_nonce_field( 'disable_subscribe', 'disable_subscribe_nonce' );
?>
<div class="misc-pub-section">
<label for="_jetpack_dont_email_post_to_subs"><?php _e( 'Jetpack Subscriptions:', 'jetpack' ); ?></label><br>
<input type="checkbox" name="_jetpack_dont_email_post_to_subs" id="jetpack-per-post-subscribe" value="1" <?php checked( $disable_subscribe_value, 1, true ); ?> />
Expand Down Expand Up @@ -157,23 +158,6 @@ function maybe_send_subscription_email( $new_status, $old_status, $post ) {
$set_checkbox = isset( $_POST['_jetpack_dont_email_post_to_subs'] ) ? 1 : 0;
update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', $set_checkbox );
}

// Only do things on publish
if ( 'publish' !== $new_status ) {
return;
}

/**
* If we're updating the post, let's make sure the flag to not send to subscribers
* is set to minimize the chances of sending posts multiple times.
*/
if ( 'publish' == $old_status ) {
update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', 1 );
}

if ( ! $this->should_email_post_to_subscribers( $post ) ) {
update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', 1 );
}
}

public function should_email_post_to_subscribers( $post ) {
Expand Down
111 changes: 61 additions & 50 deletions sync/class.jetpack-sync-module-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Jetpack_Sync_Module_Posts extends Jetpack_Sync_Module {

private $just_published = array();
private $action_handler;

public function name() {
return 'posts';
Expand All @@ -22,8 +23,14 @@ public function set_defaults() {
}

public function init_listeners( $callable ) {
add_action( 'wp_insert_post', $callable, 10, 3 );
add_action( 'wp_insert_post', array( $this, 'send_published'), 11, 3 );
$this->action_handler = $callable;

// Core < 4.7 doesn't deal with nested wp_insert_post calls very well
global $wp_version;
$priority = version_compare( $wp_version, '4.7-alpha', '<' ) ? 0 : 11;

add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ), $priority, 3 );

add_action( 'deleted_post', $callable, 10 );
add_action( 'jetpack_publicize_post', $callable );
add_action( 'jetpack_published_post', $callable, 10, 2 );
Expand Down Expand Up @@ -66,7 +73,7 @@ private function get_where_sql( $config ) {

// config is a list of post IDs to sync
if ( is_array( $config ) ) {
$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
$where_sql .= ' AND ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
}

return $where_sql;
Expand All @@ -78,8 +85,11 @@ function get_full_sync_actions() {

/**
* Process content before send
*
* @param array $args wp_insert_post arguments
*
* @return array
*/

function expand_wp_insert_post( $args ) {
return array( $args[0], $this->filter_post_content_and_add_links( $args[1] ), $args[2] );
}
Expand Down Expand Up @@ -110,6 +120,7 @@ function is_whitelisted_post_meta( $meta_key ) {

function is_post_type_allowed( $post_id ) {
$post = get_post( $post_id );

return ! in_array( $post->post_type, Jetpack_Sync_Settings::get_setting( 'post_types_blacklist' ) );
}

Expand Down Expand Up @@ -138,13 +149,13 @@ function filter_post_content_and_add_links( $post_object ) {

// return non existant post
$post_type = get_post_type_object( $post->post_type );
if ( empty( $post_type) || ! is_object( $post_type ) ) {
if ( empty( $post_type ) || ! is_object( $post_type ) ) {
$non_existant_post = new stdClass();
$non_existant_post->ID = $post->ID;
$non_existant_post->post_modified = $post->post_modified;
$non_existant_post->post_modified_gmt = $post->post_modified_gmt;
$non_existant_post->post_status = 'jetpack_sync_non_registered_post_type';

return $non_existant_post;
}
/**
Expand Down Expand Up @@ -179,9 +190,8 @@ function filter_post_content_and_add_links( $post_object ) {
}

/** This filter is already documented in core. wp-includes/post-template.php */
if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
if ( Jetpack_Sync_Settings::get_setting( 'render_filtered_content' ) && $post_type->public ) {
global $shortcode_tags;
$shortcodes_and_callbacks_to_remove = array();
/**
* Filter prevents some shortcodes from expanding.
*
Expand All @@ -192,22 +202,25 @@ function filter_post_content_and_add_links( $post_object ) {
*
* @param array of shortcode tags to remove.
*/
$shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array( 'gallery', 'slideshow' ) );
$shortcodes_to_remove = apply_filters( 'jetpack_sync_do_not_expand_shortcodes', array(
'gallery',
'slideshow'
) );
$removed_shortcode_callbacks = array();
foreach ( $shortcodes_to_remove as $shortcode ) {
if ( isset ( $shortcode_tags[ $shortcode ] ) ) {
$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
if ( isset ( $shortcode_tags[ $shortcode ] ) ) {
$removed_shortcode_callbacks[ $shortcode ] = $shortcode_tags[ $shortcode ];
}
}

array_map( 'remove_shortcode' , array_keys( $removed_shortcode_callbacks ) );
array_map( 'remove_shortcode', array_keys( $removed_shortcode_callbacks ) );

$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );
$post->post_content_filtered = apply_filters( 'the_content', $post->post_content );
$post->post_excerpt_filtered = apply_filters( 'the_excerpt', $post->post_excerpt );

foreach ( $removed_shortcode_callbacks as $shortcode => $callback ) {
add_shortcode( $shortcode, $callback );
}
add_shortcode( $shortcode, $callback );
}
}

$this->add_embed();
Expand All @@ -219,8 +232,8 @@ function filter_post_content_and_add_links( $post_object ) {
}
}

$post->permalink = get_permalink( $post->ID );
$post->shortlink = wp_get_shortlink( $post->ID );
$post->permalink = get_permalink( $post->ID );
$post->shortlink = wp_get_shortlink( $post->ID );

return $post;
}
Expand All @@ -231,44 +244,42 @@ public function save_published( $new_status, $old_status, $post ) {
}
}

public function send_published( $post_ID, $post, $update ) {
public function wp_insert_post( $post_ID, $post, $update ) {
call_user_func( $this->action_handler, $post_ID, $post, $update );
$this->send_published( $post_ID, $post );
}

public function send_published( $post_ID, $post ) {
if ( ! in_array( $post_ID, $this->just_published ) ) {
return;
}

// Post revisions cause race conditions where this send_published add the action before the actual post gets synced
if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
return;
}

if ( ! empty( $this->just_published ) && in_array( $post_ID, $this->just_published ) ) {
$published = array_reverse( array_unique( $this->just_published ) );
// Pre 4.7 WP does not have run though send_published for every save_published call
// So lets clear out any just_published that we recorded
foreach ( $published as $just_published_post_ID ) {
if ( $post_ID !== $just_published_post_ID ) {
$post = get_post( $just_published_post_ID );
}
/**
* Filter that is used to add to the post flags ( meta data ) when a post gets published
*
* @since 4.4.0
*
* @param mixed array post flags that are added to the post
* @param mixed $post WP_POST object
*/
$flags = apply_filters( 'jetpack_published_post_flags', array(), $post );

/**
* Filter that is used to add to the post flags ( meta data ) when a post gets published
*
* @since 4.4.0
*
* @param mixed array post flags that are added to the post
* @param mixed $post WP_POST object
*/
$flags = apply_filters( 'jetpack_published_post_flags', array(), $post );

/**
* Action that gets synced when a post type gets published.
*
* @since 4.4.0
*
* @param int post_id
* @param mixed array post flags that are added to the post
*/
do_action( 'jetpack_published_post', $just_published_post_ID, $flags );
}
$this->just_published = array();
}
/**
* Action that gets synced when a post type gets published.
*
* @since 4.4.0
*
* @param int $post_ID
* @param mixed array $flags post flags that are added to the post
*/
do_action( 'jetpack_published_post', $post_ID, $flags );

$this->just_published = array_diff( $this->just_published, array( $post_ID ) );
}

public function expand_post_ids( $args ) {
Expand Down
1 change: 0 additions & 1 deletion tests/php/modules/publicize/test_class.publicize.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function test_fires_jetpack_publicize_post_on_save_as_published() {
$this->post->post_status = 'publish';

wp_insert_post( $this->post->to_array() );

$this->assertPublicized( true, $this->post );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,4 @@ function test_publishing_post_first_time_does_not_set_do_not_send_subscription_f
$this->assertEmpty( get_post_meta( $post_id, '_jetpack_dont_email_post_to_subs', true ) );
}

function test_updating_post_sets_do_not_send_subscription_flag() {
$post_id = $this->factory->post->create();

// Publish and then immediately update the post, which should set the do not send flag
wp_publish_post( $post_id );
wp_update_post( array(
'ID' => $post_id,
'post_content' => 'The updated post content',
) );

$this->assertEquals( '1', get_post_meta( $post_id, '_jetpack_dont_email_post_to_subs', true ) );
}
}
20 changes: 13 additions & 7 deletions tests/php/sync/test_class.jetpack-sync-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ public function test_sync_jetpack_published_post_should_set_set_send_subscriptio
$this->sender->do_sync();

$events = $this->server_event_storage->get_all_events( 'jetpack_published_post' );
$this->assertEquals( count( $events ), 1 );
$this->assertEquals( 1, count( $events ) );

$post_flags = $events[0]->args[1];
$this->assertTrue( $post_flags['send_subscription'] );
Expand All @@ -886,22 +886,28 @@ public function test_sync_jetpack_publish_post_works_with_interjecting_plugins()
$this->server_event_storage->reset();
$this->test_already = false;
add_action( 'wp_insert_post', array( $this, 'add_a_hello_post_type' ), 9 );
$post_id = $this->factory->post->create( array( 'post_type' => 'post' ) );
$this->factory->post->create( array( 'post_type' => 'post' ) );
remove_action( 'wp_insert_post', array( $this, 'add_a_hello_post_type' ), 9 );

$this->sender->do_sync();
$events = $this->server_event_storage->get_all_events( 'jetpack_published_post' );

$this->assertEquals( 2, count( $events ) );
$events = $this->server_event_storage->get_all_events();

$events = array_slice( $events, -4);

$this->assertEquals( $events[0]->args[0], $events[1]->args[0] );
$this->assertEquals( $events[0]->action, 'wp_insert_post' );
$this->assertEquals( $events[1]->action, 'jetpack_published_post' );

// The first event is the hello post type...
$this->assertEquals( $events[1]->args[0], $post_id );
$this->assertEquals( $events[2]->args[0], $events[3]->args[0] );
$this->assertEquals( $events[2]->action, 'wp_insert_post' );
$this->assertEquals( $events[3]->action, 'jetpack_published_post' );
}

function add_a_hello_post_type() {
if ( ! $this->test_already ) {
$this->test_already = true;
$post_id = $this->factory->post->create( array( 'post_type' => 'hello' ) );
$this->factory->post->create( array( 'post_type' => 'hello' ) );
return;
}
}
Expand Down

0 comments on commit 6a83fb5

Please sign in to comment.