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
178 changes: 110 additions & 68 deletions lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package gutenberg
*/

if ( ! class_exists( 'WP_Sync_Post_Meta_Storage' ) ) {
if (!class_exists('WP_Sync_Post_Meta_Storage')) {

Check failure on line 8 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space before closing parenthesis is prohibited

Check failure on line 8 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 8 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 8 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after "!"; 0 found

Check failure on line 8 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space before "!"; 0 found

Check failure on line 8 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space after opening parenthesis is prohibited

/**
* Core class that provides an interface for storing and retrieving sync
Expand All @@ -17,7 +17,8 @@
*
* @access private
*/
class WP_Sync_Post_Meta_Storage implements WP_Sync_Storage {
class WP_Sync_Post_Meta_Storage implements WP_Sync_Storage
{

Check failure on line 21 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Opening brace should be on the same line as the declaration for class WP_Sync_Post_Meta_Storage
/**
* Post type for sync storage.
*
Expand Down Expand Up @@ -75,19 +76,20 @@
* @param mixed $update Sync update.
* @return bool True on success, false on failure.
*/
public function add_update( string $room, $update ): bool {
$post_id = $this->get_storage_post_id( $room );
if ( null === $post_id ) {
public function add_update(string $room, $update): bool

Check failure on line 79 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 79 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found
{

Check failure on line 80 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Opening brace should be on the same line as the declaration
$post_id = $this->get_storage_post_id($room);
if (null === $post_id) {
return false;
}

// Create an envelope and stamp each update to enable cursor-based filtering.
$envelope = array(
'timestamp' => $this->get_time_marker(),
'value' => $update,
'value' => $update,

Check warning on line 89 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 5 space(s) between "'value'" and double arrow, but found 1.
);

return (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false );
return (bool) add_post_meta($post_id, self::SYNC_UPDATE_META_KEY, $envelope, false);
}

/**
Expand All @@ -98,29 +100,30 @@
* @param string $room Room identifier.
* @return array<int, array{ timestamp: int, value: mixed }> Sync updates.
*/
private function get_all_updates( string $room ): array {
$this->room_cursors[ $room ] = $this->get_time_marker() - 100; // Small buffer to ensure consistency.
private function get_all_updates(string $room): array
{
$this->room_cursors[$room] = $this->get_time_marker() - 100; // Small buffer to ensure consistency.

$post_id = $this->get_storage_post_id( $room );
if ( null === $post_id ) {
$post_id = $this->get_storage_post_id($room);
if (null === $post_id) {
return array();
}

$updates = get_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, false );
$updates = get_post_meta($post_id, self::SYNC_UPDATE_META_KEY, false);

if ( ! is_array( $updates ) ) {
if (!is_array($updates)) {
$updates = array();
}

// Filter out any updates that don't have the expected structure.
$updates = array_filter(
$updates,
static function ( $update ): bool {
return is_array( $update ) && isset( $update['timestamp'], $update['value'] ) && is_int( $update['timestamp'] );
static function ($update): bool {
return is_array($update) && isset($update['timestamp'], $update['value']) && is_int($update['timestamp']);
}
);

$this->room_update_counts[ $room ] = count( $updates );
$this->room_update_counts[$room] = count($updates);

return $updates;
}
Expand All @@ -133,19 +136,20 @@
* @param string $room Room identifier.
* @return array<int, mixed> Awareness state.
*/
public function get_awareness_state( string $room ): array {
$post_id = $this->get_storage_post_id( $room );
if ( null === $post_id ) {
public function get_awareness_state(string $room): array
{
$post_id = $this->get_storage_post_id($room);
if (null === $post_id) {
return array();
}

$awareness = get_post_meta( $post_id, self::AWARENESS_META_KEY, true );
$awareness = get_post_meta($post_id, self::AWARENESS_META_KEY, true);

if ( ! is_array( $awareness ) ) {
if (!is_array($awareness)) {
return array();
}

return array_values( $awareness );
return array_values($awareness);
}

/**
Expand All @@ -157,14 +161,15 @@
* @param array<int, mixed> $awareness Serializable awareness state.
* @return bool True on success, false on failure.
*/
public function set_awareness_state( string $room, array $awareness ): bool {
$post_id = $this->get_storage_post_id( $room );
if ( null === $post_id ) {
public function set_awareness_state(string $room, array $awareness): bool
{
$post_id = $this->get_storage_post_id($room);
if (null === $post_id) {
return false;
}

// 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 );
update_post_meta($post_id, self::AWARENESS_META_KEY, $awareness);
return true;
}

Expand All @@ -180,8 +185,9 @@
* @param string $room Room identifier.
* @return int Current cursor for the room.
*/
public function get_cursor( string $room ): int {
return $this->room_cursors[ $room ] ?? 0;
public function get_cursor(string $room): int
{
return $this->room_cursors[$room] ?? 0;
}

/**
Expand All @@ -195,42 +201,43 @@
* @param string $room Room identifier.
* @return int|null Post ID.
*/
private function get_storage_post_id( string $room ): ?int {
$room_hash = md5( $room );
private function get_storage_post_id(string $room): ?int
{
$room_hash = md5($room);

if ( isset( self::$storage_post_ids[ $room_hash ] ) ) {
return self::$storage_post_ids[ $room_hash ];
if (isset(self::$storage_post_ids[$room_hash])) {
return self::$storage_post_ids[$room_hash];
}

// Try to find an existing post for this room.
$posts = get_posts(
array(
'post_type' => self::POST_TYPE,
'post_type' => self::POST_TYPE,

Check warning on line 215 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 6 space(s) between "'post_type'" and double arrow, but found 1.
'posts_per_page' => 1,
'post_status' => 'publish',
'name' => $room_hash,
'fields' => 'ids',
'post_status' => 'publish',

Check warning on line 217 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 4 space(s) between "'post_status'" and double arrow, but found 1.
'name' => $room_hash,

Check warning on line 218 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 11 space(s) between "'name'" and double arrow, but found 1.
'fields' => 'ids',

Check warning on line 219 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 9 space(s) between "'fields'" and double arrow, but found 1.
)
);

$post_id = array_first( $posts );
if ( is_int( $post_id ) ) {
self::$storage_post_ids[ $room_hash ] = $post_id;
$post_id = array_first($posts);
if (is_int($post_id)) {
self::$storage_post_ids[$room_hash] = $post_id;
return $post_id;
}

// Create new post for this room.
$post_id = wp_insert_post(
array(
'post_type' => self::POST_TYPE,
'post_type' => self::POST_TYPE,

Check warning on line 232 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 3 space(s) between "'post_type'" and double arrow, but found 1.
'post_status' => 'publish',
'post_title' => 'Sync Storage',
'post_name' => $room_hash,
'post_title' => 'Sync Storage',

Check warning on line 234 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 2 space(s) between "'post_title'" and double arrow, but found 1.
'post_name' => $room_hash,

Check warning on line 235 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 3 space(s) between "'post_name'" and double arrow, but found 1.
)
);

if ( is_int( $post_id ) ) {
self::$storage_post_ids[ $room_hash ] = $post_id;
if (is_int($post_id)) {
self::$storage_post_ids[$room_hash] = $post_id;
return $post_id;
}

Expand All @@ -244,8 +251,9 @@
*
* @return int Current time in milliseconds.
*/
private function get_time_marker(): int {
return (int) floor( microtime( true ) * 1000 );
private function get_time_marker(): int
{
return (int) floor(microtime(true) * 1000);
}

/**
Expand All @@ -256,8 +264,9 @@
* @param string $room Room identifier.
* @return int Number of updates stored for the room.
*/
public function get_update_count( string $room ): int {
return $this->room_update_counts[ $room ] ?? 0;
public function get_update_count(string $room): int
{
return $this->room_update_counts[$room] ?? 0;
}

/**
Expand All @@ -270,23 +279,24 @@
* @param int $cursor Return updates after this cursor.
* @return array<int, mixed> Sync updates.
*/
public function get_updates_after_cursor( string $room, int $cursor ): array {
$all_updates = $this->get_all_updates( $room );
$updates = array();
public function get_updates_after_cursor(string $room, int $cursor): array
{
$all_updates = $this->get_all_updates($room);
$updates = array();

Check warning on line 285 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

foreach ( $all_updates as $update ) {
if ( $update['timestamp'] > $cursor ) {
foreach ($all_updates as $update) {
if ($update['timestamp'] > $cursor) {
$updates[] = $update;
}
}

// Sort by timestamp to ensure order.
usort(
$updates,
fn ( $a, $b ) => $a['timestamp'] <=> $b['timestamp']
fn($a, $b) => $a['timestamp'] <=> $b['timestamp']
);

return wp_list_pluck( $updates, 'value' );
return wp_list_pluck($updates, 'value');
}

/**
Expand All @@ -298,28 +308,60 @@
* @param int $cursor Remove updates with markers < this cursor.
* @return bool True on success, false on failure.
*/
public function remove_updates_before_cursor( string $room, int $cursor ): bool {
$post_id = $this->get_storage_post_id( $room );
if ( null === $post_id ) {
public function remove_updates_before_cursor(string $room, int $cursor): bool
{
global $wpdb;

$post_id = $this->get_storage_post_id($room);
if (null === $post_id) {
return false;
}

$all_updates = $this->get_all_updates( $room );
// Fetch all metas for the sync update meta key for this post, including meta_id and value.
$meta_key = self::SYNC_UPDATE_META_KEY;
$metas = $wpdb->get_results(

Check warning on line 322 in lib/compat/wordpress-7.0/class-wp-sync-post-meta-storage.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
$wpdb->prepare(
"
SELECT meta_id, meta_value
FROM $wpdb->postmeta
WHERE post_id = %d
AND meta_key = %s
",
$post_id,
$meta_key
),
ARRAY_A
);

// 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 ) ) {
return false;
if (!is_array($metas)) {
return true;
}

$delete_ids = array();
foreach ($metas as $meta) {
$envelope = maybe_unserialize($meta['meta_value']);
if (
is_array($envelope) &&
isset($envelope['timestamp']) &&
is_int($envelope['timestamp']) &&
$envelope['timestamp'] < $cursor
) {
$delete_ids[] = (int) $meta['meta_id'];
}
}

// 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 );
if (empty($delete_ids)) {
return true;
}

foreach ($delete_ids as $meta_id) {
$deleted = delete_metadata_by_mid('post', $meta_id);
if (! $deleted) {
return false;
}
}

return $add_result;
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still not failing if delete_post_meta fails

}
}
}
Loading