Ticket #38342: 38531.8.diff
File 38531.8.diff, 12.4 KB (added by , 8 years ago) |
---|
-
src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
84 84 $response[ $name ] = $value; 85 85 } 86 86 87 return (object)$response;87 return $response; 88 88 } 89 89 90 90 /** … … 133 133 */ 134 134 if ( is_null( $request[ $name ] ) ) { 135 135 $result = $this->delete_meta_value( $object_id, $name ); 136 } elseif ( $args['single'] ) { 137 $result = $this->update_meta_value( $object_id, $name, $request[ $name ] ); 136 if ( is_wp_error( $result ) ) { 137 return $result; 138 } 139 continue; 140 } 141 142 $is_valid = rest_validate_value_from_schema( $request[ $name ], $args['schema'], 'meta.' . $name ); 143 if ( is_wp_error( $is_valid ) ) { 144 $is_valid->add_data( array( 'status' => 400 ) ); 145 return $is_valid; 146 } 147 148 $value = rest_sanitize_value_from_schema( $request[ $name ], $args['schema'] ); 149 150 if ( $args['single'] ) { 151 $result = $this->update_meta_value( $object_id, $name, $value ); 138 152 } else { 139 $result = $this->update_multi_meta_value( $object_id, $name, $ request[ $name ]);153 $result = $this->update_multi_meta_value( $object_id, $name, $value ); 140 154 } 141 155 142 156 if ( is_wp_error( $result ) ) { … … 319 333 $default_args = array( 320 334 'name' => $name, 321 335 'single' => $args['single'], 336 'type' => ! empty( $args['type'] ) ? $args['type'] : null, 322 337 'schema' => array(), 323 338 'prepare_callback' => array( $this, 'prepare_value' ), 324 339 ); … … 332 347 $rest_args = array_merge( $default_args, $rest_args ); 333 348 $rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] ); 334 349 350 $type = ! empty( $rest_args['type'] ) ? $rest_args['type'] : null; 351 $type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type; 352 353 if ( ! in_array( $type, array( 'string', 'boolean', 'integer', 'number' ) ) ) { 354 continue; 355 } 356 335 357 if ( empty( $rest_args['schema']['type'] ) ) { 336 358 // Skip over meta fields that don't have a defined type. 337 359 if ( empty( $args['type'] ) ) { -
src/wp-includes/rest-api.php
998 998 if ( ! is_array( $value ) ) { 999 999 $value = preg_split( '/[\s,]+/', $value ); 1000 1000 } 1001 if ( ! wp_is_numeric_array( $value ) ) { 1002 return new WP_Error( 'rest_invalid_param', sprintf( /* translators: 1: parameter, 2: type name */ __( '%1$s is not of type %2$s.' ), $param, 'array' ) ); 1003 } 1001 1004 foreach ( $value as $index => $v ) { 1002 1005 $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); 1003 1006 if ( is_wp_error( $is_valid ) ) { … … 1107 1110 foreach ( $value as $index => $v ) { 1108 1111 $value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'] ); 1109 1112 } 1113 // Normalize to numeric array so nothing unexpected 1114 // is in the keys. 1115 $value = array_values( $value ); 1110 1116 return $value; 1111 1117 } 1112 1118 if ( 'integer' === $args['type'] ) { … … 1140 1146 } 1141 1147 } 1142 1148 1149 if ( 'string' === $args['type'] ) { 1150 return strval( $value ); 1151 } 1152 1143 1153 return $value; 1144 1154 } -
tests/phpunit/tests/rest-api/rest-post-meta-fields.php
26 26 register_meta( 'post', 'test_single', array( 27 27 'show_in_rest' => true, 28 28 'single' => true, 29 'type' => 'string', 29 30 )); 30 31 register_meta( 'post', 'test_multi', array( 31 32 'show_in_rest' => true, 32 33 'single' => false, 34 'type' => 'string', 33 35 )); 34 36 register_meta( 'post', 'test_bad_auth', array( 35 37 'show_in_rest' => true, 36 38 'single' => true, 37 39 'auth_callback' => '__return_false', 40 'type' => 'string', 38 41 )); 39 42 register_meta( 'post', 'test_bad_auth_multi', array( 40 43 'show_in_rest' => true, 41 44 'single' => false, 42 45 'auth_callback' => '__return_false', 46 'type' => 'string', 43 47 )); 44 48 register_meta( 'post', 'test_no_rest', array() ); 45 49 register_meta( 'post', 'test_rest_disabled', array( 46 50 'show_in_rest' => false, 51 'type' => 'string', 47 52 )); 48 53 register_meta( 'post', 'test_custom_schema', array( 49 54 'single' => true, … … 56 61 )); 57 62 register_meta( 'post', 'test_invalid_type', array( 58 63 'single' => true, 59 'type' => false, 64 'type' => 'lalala', 65 'show_in_rest' => true, 66 )); 67 register_meta( 'post', 'test_no_type', array( 68 'single' => true, 69 'type' => null, 60 70 'show_in_rest' => true, 61 71 )); 62 72 … … 240 250 $this->assertEquals( 'test_value', $meta['test_single'] ); 241 251 } 242 252 253 254 243 255 /** 244 256 * @depends test_get_value 245 257 */ … … 341 353 $wpdb->show_errors = true; 342 354 } 343 355 356 public function test_set_value_invalid_type() { 357 $values = get_post_meta( self::$post_id, 'test_invalid_type', false ); 358 $this->assertEmpty( $values ); 359 360 $this->grant_write_permission(); 361 362 $data = array( 363 'meta' => array( 364 'test_invalid_type' => 'test_value', 365 ), 366 ); 367 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 368 $request->set_body_params( $data ); 369 370 $response = $this->server->dispatch( $request ); 371 $this->assertEmpty( get_post_meta( self::$post_id, 'test_invalid_type', false ) ); 372 } 373 344 374 public function test_set_value_multiple() { 345 375 // Ensure no data exists currently. 346 376 $values = get_post_meta( self::$post_id, 'test_multi', false ); … … 435 465 $this->assertEmpty( $meta ); 436 466 } 437 467 468 public function test_set_value_invalid_value() { 469 register_meta( 'post', 'my_meta_key', array( 470 'show_in_rest' => true, 471 'single' => true, 472 'type' => 'string', 473 )); 474 475 $this->grant_write_permission(); 476 477 $data = array( 478 'meta' => array( 479 'my_meta_key' => array( 'c', 'n' ), 480 ), 481 ); 482 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 483 $request->set_body_params( $data ); 484 485 $response = $this->server->dispatch( $request ); 486 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 487 } 488 489 public function test_set_value_invalid_value_multiple() { 490 register_meta( 'post', 'my_meta_key', array( 491 'show_in_rest' => true, 492 'single' => false, 493 'type' => 'string', 494 )); 495 496 $this->grant_write_permission(); 497 498 $data = array( 499 'meta' => array( 500 'my_meta_key' => array( array( 'a' ) ), 501 ), 502 ); 503 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 504 $request->set_body_params( $data ); 505 506 $response = $this->server->dispatch( $request ); 507 $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); 508 } 509 510 public function test_set_value_sanitized() { 511 register_meta( 'post', 'my_meta_key', array( 512 'show_in_rest' => true, 513 'single' => true, 514 'type' => 'integer', 515 )); 516 517 $this->grant_write_permission(); 518 519 $data = array( 520 'meta' => array( 521 'my_meta_key' => '1', // Set to a string. 522 ), 523 ); 524 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 525 $request->set_body_params( $data ); 526 527 $response = $this->server->dispatch( $request ); 528 $data = $response->get_data(); 529 $this->assertEquals( 1, $data['meta']['my_meta_key'] ); 530 } 531 532 public function test_set_value_csv() { 533 register_meta( 'post', 'my_meta_key', array( 534 'show_in_rest' => true, 535 'single' => false, 536 'type' => 'integer', 537 )); 538 539 $this->grant_write_permission(); 540 541 $data = array( 542 'meta' => array( 543 'my_meta_key' => '1,2,3', // Set to a string. 544 ), 545 ); 546 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 547 $request->set_body_params( $data ); 548 549 $response = $this->server->dispatch( $request ); 550 $data = $response->get_data(); 551 $this->assertEquals( array( 1, 2, 3 ), $data['meta']['my_meta_key'] ); 552 } 553 438 554 /** 439 555 * @depends test_set_value_multiple 440 556 */ … … 515 631 $this->assertErrorResponse( 'rest_meta_database_error', $response, 500 ); 516 632 } 517 633 634 518 635 public function test_delete_value() { 519 636 add_post_meta( self::$post_id, 'test_single', 'val1' ); 520 637 $current = get_post_meta( self::$post_id, 'test_single', true ); … … 618 735 $this->assertArrayNotHasKey( 'test_no_rest', $meta_schema ); 619 736 $this->assertArrayNotHasKey( 'test_rest_disabled', $meta_schema ); 620 737 $this->assertArrayNotHasKey( 'test_invalid_type', $meta_schema ); 738 $this->assertArrayNotHasKey( 'test_no_type', $meta_schema ); 621 739 } 622 740 623 741 /** -
tests/phpunit/tests/rest-api/rest-schema-sanitization.php
76 76 $this->assertEquals( array( 1 ), rest_sanitize_value_from_schema( array( '1' ), $schema ) ); 77 77 } 78 78 79 public function test_type_array_nested() { 80 $schema = array( 81 'type' => 'array', 82 'items' => array( 83 'type' => 'array', 84 'items' => array( 85 'type' => 'number', 86 ), 87 ), 88 ); 89 $this->assertEquals( array( array( 1 ), array( 2 ) ), rest_sanitize_value_from_schema( array( array( 1 ), array( 2 ) ), $schema ) ); 90 $this->assertEquals( array( array( 1 ), array( 2 ) ), rest_sanitize_value_from_schema( array( array( '1' ), array( '2' ) ), $schema ) ); 91 } 92 79 93 public function test_type_array_as_csv() { 80 94 $schema = array( 81 95 'type' => 'array', … … 110 124 $this->assertEquals( array( 'ribs', 'chicken' ), rest_sanitize_value_from_schema( 'ribs,chicken', $schema ) ); 111 125 $this->assertEquals( array( 'chicken', 'coleslaw' ), rest_sanitize_value_from_schema( 'chicken,coleslaw', $schema ) ); 112 126 } 127 128 public function test_type_array_is_associative() { 129 $schema = array( 130 'type' => 'array', 131 'items' => array( 132 'type' => 'string', 133 ), 134 ); 135 $this->assertEquals( array( '1', '2' ), rest_sanitize_value_from_schema( array( 'first' => '1', 'second' => '2' ), $schema ) ); 136 } 137 138 public function test_type_unknown() { 139 $schema = array( 140 'type' => 'lalala', 141 ); 142 $this->assertEquals( 'Best lyrics', rest_sanitize_value_from_schema( 'Best lyrics', $schema ) ); 143 $this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) ); 144 $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) ); 145 } 146 147 public function test_no_type() { 148 $schema = array( 149 'type' => null, 150 ); 151 $this->assertEquals( 'Nothing', rest_sanitize_value_from_schema( 'Nothing', $schema ) ); 152 $this->assertEquals( 1.10, rest_sanitize_value_from_schema( 1.10, $schema ) ); 153 $this->assertEquals( 1, rest_sanitize_value_from_schema( 1, $schema ) ); 154 } 113 155 } -
tests/phpunit/tests/rest-api/rest-schema-validation.php
104 104 $this->assertWPError( rest_validate_value_from_schema( array( true ), $schema ) ); 105 105 } 106 106 107 public function test_type_array_nested() { 108 $schema = array( 109 'type' => 'array', 110 'items' => array( 111 'type' => 'array', 112 'items' => array( 113 'type' => 'number', 114 ), 115 ), 116 ); 117 $this->assertTrue( rest_validate_value_from_schema( array( array( 1 ), array( 2 ) ), $schema ) ); 118 } 119 107 120 public function test_type_array_as_csv() { 108 121 $schema = array( 109 122 'type' => 'array', … … 139 152 $this->assertTrue( rest_validate_value_from_schema( 'ribs,chicken', $schema ) ); 140 153 $this->assertWPError( rest_validate_value_from_schema( 'chicken,coleslaw', $schema ) ); 141 154 } 155 156 public function test_type_array_is_associative() { 157 $schema = array( 158 'type' => 'array', 159 'items' => array( 160 'type' => 'string', 161 ), 162 ); 163 $this->assertWPError( rest_validate_value_from_schema( array( 'first' => '1', 'second' => '2' ), $schema ) ); 164 } 165 166 public function test_type_unknown() { 167 $schema = array( 168 'type' => 'lalala', 169 ); 170 $this->assertTrue( rest_validate_value_from_schema( 'Best lyrics', $schema ) ); 171 $this->assertTrue( rest_validate_value_from_schema( 1, $schema ) ); 172 $this->assertTrue( rest_validate_value_from_schema( array(), $schema ) ); 173 } 142 174 }