rest_get_endpoint_args_for_schema

函数
rest_get_endpoint_args_for_schema ( $schema, $method = WP_REST_Server::CREATABLE )
参数
  • (array) $schema The full JSON schema for the endpoint.
    Required:
  • (string) $method Optional. HTTP method of the endpoint. The arguments for `CREATABLE` endpoints are checked for required values and may fall-back to a given default, this is not done on `EDITABLE` endpoints. Default WP_REST_Server::CREATABLE.
    Required:
    Default: WP_REST_Server::CREATABLE
返回值
  • (array) The endpoint arguments.
定义位置
相关方法
rest_get_route_for_termrest_get_best_type_for_valueget_admin_users_for_domainrest_get_avatar_urlsrest_get_route_for_post
引入
5.6.0
弃用
-

rest_get_endpoint_args_for_schema: 这个函数用于检索特定模式的端点参数。它接受两个参数,第一个是需要端点参数的模式,第二个是一个布尔值,表示是否在响应中包括模式描述。它返回一个指定模式的端点参数数组。

从项目模式和端点方法中检索一个端点参数阵列。

function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::CREATABLE ) {

	$schema_properties       = ! empty( $schema['properties'] ) ? $schema['properties'] : array();
	$endpoint_args           = array();
	$valid_schema_properties = rest_get_allowed_schema_keywords();
	$valid_schema_properties = array_diff( $valid_schema_properties, array( 'default', 'required' ) );

	foreach ( $schema_properties as $field_id => $params ) {

		// Arguments specified as `readonly` are not allowed to be set.
		if ( ! empty( $params['readonly'] ) ) {
			continue;
		}

		$endpoint_args[ $field_id ] = array(
			'validate_callback' => 'rest_validate_request_arg',
			'sanitize_callback' => 'rest_sanitize_request_arg',
		);

		if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) {
			$endpoint_args[ $field_id ]['default'] = $params['default'];
		}

		if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) {
			$endpoint_args[ $field_id ]['required'] = true;
		}

		foreach ( $valid_schema_properties as $schema_prop ) {
			if ( isset( $params[ $schema_prop ] ) ) {
				$endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
			}
		}

		// Merge in any options provided by the schema property.
		if ( isset( $params['arg_options'] ) ) {

			// Only use required / default from arg_options on CREATABLE endpoints.
			if ( WP_REST_Server::CREATABLE !== $method ) {
				$params['arg_options'] = array_diff_key(
					$params['arg_options'],
					array(
						'required' => '',
						'default'  => '',
					)
				);
			}

			$endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] );
		}
	}

	return $endpoint_args;
}

常见问题

FAQs
查看更多 >