Changeset 39296
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r39278 r39296 870 870 871 871 /** 872 * Determines if a IPv4address is valid.873 * 874 * Does not handleIPv6 addresses.872 * Determines if an IP address is valid. 873 * 874 * Handles both IPv4 and IPv6 addresses. 875 875 * 876 876 * @since 4.7.0 877 877 * 878 * @param string $ip v4 IP 32-bitaddress.879 * @return string|false The valid IP v4address, otherwise false.880 */ 881 function rest_is_ip_address( $ip v4) {882 $ pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/';883 884 if ( ! preg_match( $ pattern, $ipv4) ) {878 * @param string $ip IP address. 879 * @return string|false The valid IP address, otherwise false. 880 */ 881 function rest_is_ip_address( $ip ) { 882 $ipv4_pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'; 883 884 if ( ! preg_match( $ipv4_pattern, $ip ) && ! Requests_IPv6::check_ipv6( $ip ) ) { 885 885 return false; 886 886 } 887 887 888 return $ip v4;888 return $ip; 889 889 } 890 890 … … 1054 1054 } 1055 1055 break; 1056 case 'ip v4' :1056 case 'ip' : 1057 1057 if ( ! rest_is_ip_address( $value ) ) { 1058 /* translators: %s: IP address */ 1058 /* translators: %s: IP address */ 1059 1059 return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.' ), $value ) ); 1060 1060 } … … 1157 1157 return esc_url_raw( $value ); 1158 1158 1159 case 'ip v4' :1159 case 'ip' : 1160 1160 return sanitize_text_field( $value ); 1161 1161 } -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
r39295 r39296 1116 1116 'description' => __( 'IP address for the object author.' ), 1117 1117 'type' => 'string', 1118 'format' => 'ip v4',1118 'format' => 'ip', 1119 1119 'context' => array( 'edit' ), 1120 1120 'default' => '127.0.0.1', -
trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php
r39222 r39296 64 64 $this->assertEquals( 'a@b.c', rest_sanitize_value_from_schema( 'a@b.c', $schema ) ); 65 65 $this->assertEquals( 'invalid', rest_sanitize_value_from_schema( 'invalid', $schema ) ); 66 } 67 68 public function test_format_ip() { 69 $schema = array( 70 'type' => 'string', 71 'format' => 'ip', 72 ); 73 74 $this->assertEquals( '127.0.0.1', rest_sanitize_value_from_schema( '127.0.0.1', $schema ) ); 75 $this->assertEquals( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) ); 76 $this->assertEquals( '2001:DB8:0:0:8:800:200C:417A', rest_sanitize_value_from_schema( '2001:DB8:0:0:8:800:200C:417A', $schema ) ); 66 77 } 67 78 -
trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php
r39222 r39296 84 84 } 85 85 86 public function test_format_ip v4() {86 public function test_format_ip() { 87 87 $schema = array( 88 88 'type' => 'string', 89 'format' => 'ip v4',89 'format' => 'ip', 90 90 ); 91 92 // IPv4. 91 93 $this->assertTrue( rest_validate_value_from_schema( '127.0.0.1', $schema ) ); 92 94 $this->assertWPError( rest_validate_value_from_schema( '3333.3333.3333.3333', $schema ) ); 93 95 $this->assertWPError( rest_validate_value_from_schema( '1', $schema ) ); 96 97 // IPv6. 98 $this->assertTrue( rest_validate_value_from_schema( '::1', $schema ) ); // Loopback, compressed, non-routable. 99 $this->assertTrue( rest_validate_value_from_schema( '::', $schema ) ); // Unspecified, compressed, non-routable. 100 $this->assertTrue( rest_validate_value_from_schema( '0:0:0:0:0:0:0:1', $schema ) ); // Loopback, full. 101 $this->assertTrue( rest_validate_value_from_schema( '0:0:0:0:0:0:0:0', $schema ) ); // Unspecified, full. 102 $this->assertTrue( rest_validate_value_from_schema( '2001:DB8:0:0:8:800:200C:417A', $schema ) ); // Unicast, full. 103 $this->assertTrue( rest_validate_value_from_schema( 'FF01:0:0:0:0:0:0:101', $schema ) ); // Multicast, full. 104 $this->assertTrue( rest_validate_value_from_schema( '2001:DB8::8:800:200C:417A', $schema ) ); // Unicast, compressed. 105 $this->assertTrue( rest_validate_value_from_schema( 'FF01::101', $schema ) ); // Multicast, compressed. 106 $this->assertTrue( rest_validate_value_from_schema( 'fe80::217:f2ff:fe07:ed62', $schema ) ); 107 $this->assertWPError( rest_validate_value_from_schema( '', $schema ) ); // Empty string. 108 $this->assertWPError( rest_validate_value_from_schema( '2001:DB8:0:0:8:800:200C:417A:221', $schema ) ); // Unicast, full. 109 $this->assertWPError( rest_validate_value_from_schema( 'FF01::101::2', $schema ) ); // Multicast, compressed. 94 110 } 95 111
Note: See TracChangeset
for help on using the changeset viewer.