Changeset 43740
- Timestamp:
- 10/17/2018 08:09:33 PM (6 years ago)
- Location:
- branches/5.0
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/5.0/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
r43510 r43740 296 296 } 297 297 298 $meta_key = wp_slash( $meta_key );299 $meta_value = wp_slash( $value );300 301 298 // Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false. 302 299 $old_value = get_metadata( $meta_type, $object_id, $meta_key ); 300 $subtype = get_object_subtype( $meta_type, $object_id ); 303 301 304 302 if ( 1 === count( $old_value ) ) { 305 if ( $old_value[0] === $meta_value) {303 if ( (string) sanitize_meta( $meta_key, $value, $meta_type, $subtype ) === $old_value[0] ) { 306 304 return true; 307 305 } 308 306 } 309 307 310 if ( ! update_metadata( $meta_type, $object_id, $meta_key, $meta_value) ) {308 if ( ! update_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) { 311 309 return new WP_Error( 312 310 'rest_meta_database_error', -
branches/5.0/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
r43510 r43740 21 21 ) ); 22 22 23 self::$wp_meta_keys_saved = $GLOBALS['wp_meta_keys'];23 self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array(); 24 24 self::$post_id = $factory->post->create(); 25 25 self::$cpt_post_id = $factory->post->create( array( 'post_type' => 'cpt' ) ); … … 130 130 'auth_callback' => '__return_false', 131 131 ) ); 132 133 register_meta( 134 'post', 135 'test_boolean_update', 136 array( 137 'single' => true, 138 'type' => 'boolean', 139 'sanitize_callback' => 'absint', 140 'show_in_rest' => true, 141 ) 142 ); 143 144 register_meta( 145 'post', 146 'test_textured_text_update', 147 array( 148 'single' => true, 149 'type' => 'string', 150 'sanitize_callback' => 'sanitize_text_field', 151 'show_in_rest' => true, 152 ) 153 ); 154 155 register_meta( 156 'post', 157 'test_json_encoded', 158 array( 159 'single' => true, 160 'type' => 'string', 161 'show_in_rest' => true, 162 ) 163 ); 164 165 register_meta( 166 'post', 167 'test\'slashed\'key', 168 array( 169 'single' => true, 170 'type' => 'string', 171 'show_in_rest' => true, 172 ) 173 ); 132 174 133 175 /** @var WP_REST_Server $wp_rest_server */ … … 1163 1205 1164 1206 /** 1207 * @ticket 42069 1208 * @dataProvider data_update_value_return_success_with_same_value 1209 */ 1210 public function test_update_value_return_success_with_same_value( $meta_key, $meta_value ) { 1211 add_post_meta( self::$post_id, $meta_key, $meta_value ); 1212 1213 $this->grant_write_permission(); 1214 1215 $data = array( 1216 'meta' => array( 1217 $meta_key => $meta_value, 1218 ), 1219 ); 1220 1221 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1222 $request->set_body_params( $data ); 1223 1224 $response = rest_get_server()->dispatch( $request ); 1225 1226 $this->assertEquals( 200, $response->get_status() ); 1227 } 1228 1229 public function data_update_value_return_success_with_same_value() { 1230 return array( 1231 array( 'test_boolean_update', false ), 1232 array( 'test_boolean_update', true ), 1233 array( 'test_textured_text_update', 'She said, "What about the > 10,000 penguins in the kitchen?"' ), 1234 array( 'test_textured_text_update', "He's about to do something rash..." ), 1235 array( 'test_json_encoded', json_encode( array( 'foo' => 'bar' ) ) ), 1236 array( 'test\'slashed\'key', 'Hello' ), 1237 ); 1238 } 1239 1240 /** 1241 * @ticket 42069 1242 */ 1243 public function test_slashed_meta_key() { 1244 1245 add_post_meta( self::$post_id, 'test\'slashed\'key', 'Hello' ); 1246 1247 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 1248 1249 $response = rest_get_server()->dispatch( $request ); 1250 $data = $response->get_data(); 1251 1252 $this->assertArrayHasKey( 'test\'slashed\'key', $data['meta'] ); 1253 $this->assertEquals( 'Hello', $data['meta']['test\'slashed\'key'] ); 1254 } 1255 1256 /** 1165 1257 * Internal function used to disable an insert query which 1166 1258 * will trigger a wpdb error for testing purposes.
Note: See TracChangeset
for help on using the changeset viewer.