@@ -74,14 +74,24 @@ class WP_Sync_Post_Meta_Storage implements WP_Sync_Storage {
7474 * @return bool True on success, false on failure.
7575 */
7676 public function add_update ( string $ room , $ update ): bool {
77+ global $ wpdb ;
78+
7779 $ post_id = $ this ->get_storage_post_id ( $ room );
7880 if ( null === $ post_id ) {
7981 return false ;
8082 }
8183
82- $ meta_id = add_post_meta ( $ post_id , self ::SYNC_UPDATE_META_KEY , $ update , false );
84+ $ result = $ wpdb ->insert (
85+ $ wpdb ->postmeta ,
86+ array (
87+ 'post_id ' => $ post_id ,
88+ 'meta_key ' => self ::SYNC_UPDATE_META_KEY ,
89+ 'meta_value ' => maybe_serialize ( $ update ),
90+ ),
91+ array ( '%d ' , '%s ' , '%s ' )
92+ );
8393
84- return ( bool ) $ meta_id ;
94+ return false !== $ result ;
8595 }
8696
8797 /**
@@ -93,12 +103,26 @@ public function add_update( string $room, $update ): bool {
93103 * @return array<int, mixed> Awareness state.
94104 */
95105 public function get_awareness_state ( string $ room ): array {
106+ global $ wpdb ;
107+
96108 $ post_id = $ this ->get_storage_post_id ( $ room );
97109 if ( null === $ post_id ) {
98110 return array ();
99111 }
100112
101- $ awareness = get_post_meta ( $ post_id , self ::AWARENESS_META_KEY , true );
113+ $ raw = $ wpdb ->get_var (
114+ $ wpdb ->prepare (
115+ "SELECT meta_value FROM $ wpdb ->postmeta WHERE post_id = %d AND meta_key = %s " ,
116+ $ post_id ,
117+ self ::AWARENESS_META_KEY
118+ )
119+ );
120+
121+ if ( null === $ raw ) {
122+ return array ();
123+ }
124+
125+ $ awareness = maybe_unserialize ( $ raw );
102126
103127 if ( ! is_array ( $ awareness ) ) {
104128 return array ();
@@ -117,13 +141,43 @@ public function get_awareness_state( string $room ): array {
117141 * @return bool True on success, false on failure.
118142 */
119143 public function set_awareness_state ( string $ room , array $ awareness ): bool {
144+ global $ wpdb ;
145+
120146 $ post_id = $ this ->get_storage_post_id ( $ room );
121147 if ( null === $ post_id ) {
122148 return false ;
123149 }
124150
125- // update_post_meta returns false if the value is the same as the existing value.
126- update_post_meta ( $ post_id , wp_slash ( self ::AWARENESS_META_KEY ), wp_slash ( $ awareness ) );
151+ $ serialized = maybe_serialize ( $ awareness );
152+
153+ $ meta_id = $ wpdb ->get_var (
154+ $ wpdb ->prepare (
155+ "SELECT meta_id FROM $ wpdb ->postmeta WHERE post_id = %d AND meta_key = %s " ,
156+ $ post_id ,
157+ self ::AWARENESS_META_KEY
158+ )
159+ );
160+
161+ if ( $ meta_id ) {
162+ $ wpdb ->update (
163+ $ wpdb ->postmeta ,
164+ array ( 'meta_value ' => $ serialized ),
165+ array ( 'meta_id ' => $ meta_id ),
166+ array ( '%s ' ),
167+ array ( '%d ' )
168+ );
169+ } else {
170+ $ wpdb ->insert (
171+ $ wpdb ->postmeta ,
172+ array (
173+ 'post_id ' => $ post_id ,
174+ 'meta_key ' => self ::AWARENESS_META_KEY ,
175+ 'meta_value ' => $ serialized ,
176+ ),
177+ array ( '%d ' , '%s ' , '%s ' )
178+ );
179+ }
180+
127181 return true ;
128182 }
129183
0 commit comments