wp/wp-includes/rest-api.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    36 		/*
    36 		/*
    37 		 * Non-namespaced routes are not allowed, with the exception of the main
    37 		 * Non-namespaced routes are not allowed, with the exception of the main
    38 		 * and namespace indexes. If you really need to register a
    38 		 * and namespace indexes. If you really need to register a
    39 		 * non-namespaced route, call `WP_REST_Server::register_route` directly.
    39 		 * non-namespaced route, call `WP_REST_Server::register_route` directly.
    40 		 */
    40 		 */
    41 		_doing_it_wrong( 'register_rest_route', __( 'Routes must be namespaced with plugin or theme name and version.' ), '4.4.0' );
    41 		_doing_it_wrong(
       
    42 			__FUNCTION__,
       
    43 			sprintf(
       
    44 				/* translators: 1: string value of the namespace, 2: string value of the route. */
       
    45 				__( 'Routes must be namespaced with plugin or theme name and version. Instead there seems to be an empty namespace \'%1$s\' for route \'%2$s\'.' ),
       
    46 				'<code>' . $route_namespace . '</code>',
       
    47 				'<code>' . $route . '</code>'
       
    48 			),
       
    49 			'4.4.0'
       
    50 		);
    42 		return false;
    51 		return false;
    43 	} elseif ( empty( $route ) ) {
    52 	} elseif ( empty( $route ) ) {
    44 		_doing_it_wrong( 'register_rest_route', __( 'Route must be specified.' ), '4.4.0' );
    53 		_doing_it_wrong(
       
    54 			__FUNCTION__,
       
    55 			sprintf(
       
    56 				/* translators: 1: string value of the namespace, 2: string value of the route. */
       
    57 				__( 'Route must be specified. Instead within the namespace \'%1$s\', there seems to be an empty route \'%2$s\'.' ),
       
    58 				'<code>' . $route_namespace . '</code>',
       
    59 				'<code>' . $route . '</code>'
       
    60 			),
       
    61 			'4.4.0'
       
    62 		);
    45 		return false;
    63 		return false;
    46 	}
    64 	}
    47 
    65 
    48 	$clean_namespace = trim( $route_namespace, '/' );
    66 	$clean_namespace = trim( $route_namespace, '/' );
    49 
    67 
    50 	if ( $clean_namespace !== $route_namespace ) {
    68 	if ( $clean_namespace !== $route_namespace ) {
    51 		_doing_it_wrong( __FUNCTION__, __( 'Namespace must not start or end with a slash.' ), '5.4.2' );
    69 		_doing_it_wrong(
       
    70 			__FUNCTION__,
       
    71 			sprintf(
       
    72 				/* translators: 1: string value of the namespace, 2: string value of the route. */
       
    73 				__( 'Namespace must not start or end with a slash. Instead namespace \'%1$s\' for route \'%2$s\' seems to contain a slash.' ),
       
    74 				'<code>' . $route_namespace . '</code>',
       
    75 				'<code>' . $route . '</code>'
       
    76 			),
       
    77 			'5.4.2'
       
    78 		);
    52 	}
    79 	}
    53 
    80 
    54 	if ( ! did_action( 'rest_api_init' ) ) {
    81 	if ( ! did_action( 'rest_api_init' ) ) {
    55 		_doing_it_wrong(
    82 		_doing_it_wrong(
    56 			'register_rest_route',
    83 			__FUNCTION__,
    57 			sprintf(
    84 			sprintf(
    58 				/* translators: %s: rest_api_init */
    85 				/* translators: 1: rest_api_init, 2: string value of the route, 3: string value of the namespace. */
    59 				__( 'REST API routes must be registered on the %s action.' ),
    86 				__( 'REST API routes must be registered on the %1$s action. Instead route \'%2$s\' with namespace \'%3$s\' was not registered on this action.' ),
    60 				'<code>rest_api_init</code>'
    87 				'<code>rest_api_init</code>',
       
    88 				'<code>' . $route . '</code>',
       
    89 				'<code>' . $route_namespace . '</code>'
    61 			),
    90 			),
    62 			'5.1.0'
    91 			'5.1.0'
    63 		);
    92 		);
    64 	}
    93 	}
    65 
    94 
   399 function rest_api_loaded() {
   428 function rest_api_loaded() {
   400 	if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
   429 	if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
   401 		return;
   430 		return;
   402 	}
   431 	}
   403 
   432 
       
   433 	// Return an error message if query_var is not a string.
       
   434 	if ( ! is_string( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
       
   435 		$rest_type_error = new WP_Error(
       
   436 			'rest_path_invalid_type',
       
   437 			__( 'The REST route parameter must be a string.' ),
       
   438 			array( 'status' => 400 )
       
   439 		);
       
   440 		wp_die( $rest_type_error );
       
   441 	}
       
   442 
   404 	/**
   443 	/**
   405 	 * Whether this is a REST Request.
   444 	 * Whether this is a REST Request.
   406 	 *
   445 	 *
   407 	 * @since 4.4.0
   446 	 * @since 4.4.0
   408 	 * @var bool
   447 	 * @var bool
   787 				$args[ $param ] = $value;
   826 				$args[ $param ] = $value;
   788 			}
   827 			}
   789 		}
   828 		}
   790 
   829 
   791 		foreach ( $endpoints as $endpoint ) {
   830 		foreach ( $endpoints as $endpoint ) {
   792 			// Remove the redundant preg_match() argument.
       
   793 			unset( $args[0] );
       
   794 
       
   795 			$request->set_url_params( $args );
   831 			$request->set_url_params( $args );
   796 			$request->set_attributes( $endpoint );
   832 			$request->set_attributes( $endpoint );
   797 		}
   833 		}
   798 
   834 
   799 		$data = $handler->get_data_for_route( $route, $endpoints, 'help' );
   835 		$data = $handler->get_data_for_route( $route, $endpoints, 'help' );
  1273 }
  1309 }
  1274 
  1310 
  1275 /**
  1311 /**
  1276  * Parses an RFC3339 time into a Unix timestamp.
  1312  * Parses an RFC3339 time into a Unix timestamp.
  1277  *
  1313  *
       
  1314  * Explicitly check for `false` to detect failure, as zero is a valid return
       
  1315  * value on success.
       
  1316  *
  1278  * @since 4.4.0
  1317  * @since 4.4.0
  1279  *
  1318  *
  1280  * @param string $date      RFC3339 timestamp.
  1319  * @param string $date      RFC3339 timestamp.
  1281  * @param bool   $force_utc Optional. Whether to force UTC timezone instead of using
  1320  * @param bool   $force_utc Optional. Whether to force UTC timezone instead of using
  1282  *                          the timestamp's timezone. Default false.
  1321  *                          the timestamp's timezone. Default false.
  1338 	 */
  1377 	 */
  1339 	$has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date );
  1378 	$has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date );
  1340 
  1379 
  1341 	$date = rest_parse_date( $date );
  1380 	$date = rest_parse_date( $date );
  1342 
  1381 
  1343 	if ( empty( $date ) ) {
  1382 	if ( false === $date ) {
  1344 		return null;
  1383 		return null;
  1345 	}
  1384 	}
  1346 
  1385 
  1347 	/*
  1386 	/*
  1348 	 * At this point $date could either be a local date (if we were passed
  1387 	 * At this point $date could either be a local date (if we were passed
  2228 					return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) );
  2267 					return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) );
  2229 				}
  2268 				}
  2230 				break;
  2269 				break;
  2231 
  2270 
  2232 			case 'date-time':
  2271 			case 'date-time':
  2233 				if ( ! rest_parse_date( $value ) ) {
  2272 				if ( false === rest_parse_date( $value ) ) {
  2234 					return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
  2273 					return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
  2235 				}
  2274 				}
  2236 				break;
  2275 				break;
  2237 
  2276 
  2238 			case 'email':
  2277 			case 'email':
  2904 		if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) {
  2943 		if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) {
  2905 			$method = 'GET';
  2944 			$method = 'GET';
  2906 		}
  2945 		}
  2907 	}
  2946 	}
  2908 
  2947 
       
  2948 	// Remove trailing slashes at the end of the REST API path (query part).
  2909 	$path = untrailingslashit( $path );
  2949 	$path = untrailingslashit( $path );
  2910 	if ( empty( $path ) ) {
  2950 	if ( empty( $path ) ) {
  2911 		$path = '/';
  2951 		$path = '/';
  2912 	}
  2952 	}
  2913 
  2953 
  2914 	$path_parts = parse_url( $path );
  2954 	$path_parts = parse_url( $path );
  2915 	if ( false === $path_parts ) {
  2955 	if ( false === $path_parts ) {
  2916 		return $memo;
  2956 		return $memo;
       
  2957 	}
       
  2958 
       
  2959 	if ( isset( $path_parts['path'] ) && '/' !== $path_parts['path'] ) {
       
  2960 		// Remove trailing slashes from the "path" part of the REST API path.
       
  2961 		$path_parts['path'] = untrailingslashit( $path_parts['path'] );
       
  2962 		$path               = str_contains( $path, '?' ) ?
       
  2963 			$path_parts['path'] . '?' . ( $path_parts['query'] ?? '' ) :
       
  2964 			$path_parts['path'];
  2917 	}
  2965 	}
  2918 
  2966 
  2919 	$request = new WP_REST_Request( $method, $path_parts['path'] );
  2967 	$request = new WP_REST_Request( $method, $path_parts['path'] );
  2920 	if ( ! empty( $path_parts['query'] ) ) {
  2968 	if ( ! empty( $path_parts['query'] ) ) {
  2921 		parse_str( $path_parts['query'], $query_params );
  2969 		parse_str( $path_parts['query'], $query_params );