Skip to content

Commit 19274ba

Browse files
committed
Prevent calling wp_cache_set_posts_last_changed() when touching post meta in WP_Sync_Post_Meta_Storage
1 parent 84e5ace commit 19274ba

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public function add_update( string $room, $update ): bool {
8585
'value' => $update,
8686
);
8787

88-
return (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false );
88+
return $this->with_suspended_posts_last_changed_update(
89+
fn() => (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false )
90+
);
8991
}
9092

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

164166
// update_post_meta returns false if the value is the same as the existing value.
165-
update_post_meta( $post_id, self::AWARENESS_META_KEY, $awareness );
167+
$this->with_suspended_posts_last_changed_update(
168+
fn() => update_post_meta( $post_id, self::AWARENESS_META_KEY, $awareness )
169+
);
166170
return true;
167171
}
168172

@@ -305,18 +309,49 @@ public function remove_updates_before_cursor( string $room, int $cursor ): bool
305309
$all_updates = $this->get_all_updates( $room );
306310

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

312316
// Re-store envelopes directly to avoid double-wrapping by add_update().
313317
$add_result = true;
314318
foreach ( $all_updates as $envelope ) {
315319
if ( $add_result && $envelope['timestamp'] >= $cursor ) {
316-
$add_result = (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false );
320+
$add_result = $this->with_suspended_posts_last_changed_update(
321+
fn() => (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false )
322+
);
317323
}
318324
}
319325

320326
return $add_result;
321327
}
328+
329+
/**
330+
* Invokes the provided callback while the suspending setting the posts last_changed cache key.
331+
*
332+
* @since 7.0.0
333+
* @see wp_cache_set_posts_last_changed()
334+
*
335+
* @param Closure $callback Callback
336+
* @return mixed Return value from the callback.
337+
*/
338+
private function with_suspended_posts_last_changed_update( Closure $callback ) {
339+
$priorities = array(
340+
'added_post_meta' => has_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' ),
341+
'updated_post_meta' => has_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' ),
342+
'deleted_post_meta' => has_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' ),
343+
);
344+
foreach ( $priorities as $action => $priority ) {
345+
if ( false !== $priority ) {
346+
remove_action( $action, 'wp_cache_set_posts_last_changed', $priority );
347+
}
348+
}
349+
$return_value = $callback();
350+
foreach ( $priorities as $action => $priority ) {
351+
if ( false !== $priority ) {
352+
add_action( $action, 'wp_cache_set_posts_last_changed', $priority );
353+
}
354+
}
355+
return $return_value;
356+
}
322357
}

0 commit comments

Comments
 (0)