Skip to content
Draft
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
44 changes: 40 additions & 4 deletions src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public function add_update( string $room, $update ): bool {
'value' => $update,
);

return (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false );
return $this->with_suspended_posts_last_changed_update(
fn() => (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false )
);
}

/**
Expand Down Expand Up @@ -162,7 +164,9 @@ public function set_awareness_state( string $room, array $awareness ): bool {
}

// update_post_meta returns false if the value is the same as the existing value.
update_post_meta( $post_id, self::AWARENESS_META_KEY, $awareness );
$this->with_suspended_posts_last_changed_update(
fn() => update_post_meta( $post_id, self::AWARENESS_META_KEY, $awareness )
);
return true;
}

Expand Down Expand Up @@ -305,18 +309,50 @@ public function remove_updates_before_cursor( string $room, int $cursor ): bool
$all_updates = $this->get_all_updates( $room );

// Remove all updates for the room and re-store only those that are newer than the cursor.
if ( ! delete_post_meta( $post_id, self::SYNC_UPDATE_META_KEY ) ) {
if ( ! $this->with_suspended_posts_last_changed_update( fn() => delete_post_meta( $post_id, self::SYNC_UPDATE_META_KEY ) ) ) {
return false;
}

// Re-store envelopes directly to avoid double-wrapping by add_update().
$add_result = true;
foreach ( $all_updates as $envelope ) {
if ( $add_result && $envelope['timestamp'] >= $cursor ) {
$add_result = (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false );
$add_result = $this->with_suspended_posts_last_changed_update(
fn() => (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false )
);
}
}

return $add_result;
}

/**
* Invokes the provided callback while the suspending setting the posts last_changed cache key.
*
* @since 7.0.0
* @see wp_cache_set_posts_last_changed()
*
* @template T
* @param Closure(): T $callback Callback.
* @return T Return value from the callback.
*/
private function with_suspended_posts_last_changed_update( Closure $callback ) {
$priorities = array(
'added_post_meta' => has_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' ),
'updated_post_meta' => has_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' ),
'deleted_post_meta' => has_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' ),
);
foreach ( $priorities as $action => $priority ) {
if ( false !== $priority ) {
remove_action( $action, 'wp_cache_set_posts_last_changed', $priority );
}
}
$return_value = $callback();
foreach ( $priorities as $action => $priority ) {
if ( false !== $priority ) {
add_action( $action, 'wp_cache_set_posts_last_changed', $priority );
}
}
return $return_value;
}
}
Loading