@@ -4014,4 +4014,60 @@ public static function data_scalar_default_values() {
40144014 'string default ' => array ( 'string ' , 'string ' , 'string2 ' ),
40154015 );
40164016 }
4017+
4018+ /**
4019+ * Tests that the Meta fields handle an empty object (stdClass) correctly.
4020+ *
4021+ * This verifies the compatibility of filter_response_by_context with object types
4022+ * when meta is cast to an object (e.g., in specific PHP versions or filter modifications).
4023+ *
4024+ * @ticket 54484
4025+ */
4026+ public function test_filter_response_by_context_with_empty_object_meta () {
4027+ wp_set_current_user ( self ::factory ()->user ->create ( array ( 'role ' => 'editor ' ) ) );
4028+
4029+ register_post_meta (
4030+ 'post ' ,
4031+ 'test_object_compat_meta ' ,
4032+ array (
4033+ 'single ' => true ,
4034+ 'type ' => 'string ' ,
4035+ 'show_in_rest ' => true ,
4036+ )
4037+ );
4038+
4039+ add_filter (
4040+ 'rest_prepare_post ' ,
4041+ function ( $ response ) {
4042+ $ data = $ response ->get_data ();
4043+ // Inject an empty object for meta to test type compatibility
4044+ $ data ['meta ' ] = new stdClass ();
4045+ $ response ->set_data ( $ data );
4046+ return $ response ;
4047+ },
4048+ 10
4049+ );
4050+
4051+ $ request = new WP_REST_Request ( 'GET ' , '/wp/v2/posts/ ' . self ::$ post_id );
4052+ $ response = rest_get_server ()->dispatch ( $ request );
4053+ $ data = $ response ->get_data ();
4054+
4055+ $ this ->assertArrayHasKey ( 'meta ' , $ data );
4056+
4057+ // Use a more robust check for both array and object types
4058+ $ meta_data = $ data ['meta ' ];
4059+
4060+ if ( is_object ( $ meta_data ) ) {
4061+ $ this ->assertFalse (
4062+ property_exists ( $ meta_data , 'test_object_compat_meta ' ),
4063+ 'Failed asserting that the meta object does not have the property "test_object_compat_meta". '
4064+ );
4065+ } else {
4066+ $ this ->assertArrayNotHasKey (
4067+ 'test_object_compat_meta ' ,
4068+ $ meta_data ,
4069+ 'Failed asserting that the meta array does not have the key "test_object_compat_meta". '
4070+ );
4071+ }
4072+ }
40174073}
0 commit comments