wp/wp-includes/rest-api.php
author ymh <ymh.work@gmail.com>
Tue, 27 Sep 2022 16:37:53 +0200
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
permissions -rw-r--r--
upgrade wordpress to 6.0.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * REST API functions.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage REST_API
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * Version number for our API.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * @var string
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
define( 'REST_API_VERSION', '2.0' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * Registers a REST API route.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 *
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    20
 * Note: Do not use before the {@see 'rest_api_init'} hook.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    21
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * @since 4.4.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    23
 * @since 5.1.0 Added a `_doing_it_wrong()` notice when not called on or after the `rest_api_init` hook.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    24
 * @since 5.5.0 Added a `_doing_it_wrong()` notice when the required `permission_callback` argument is not set.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 * @param string $namespace The first URL segment after core prefix. Should be unique to your package/plugin.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 * @param string $route     The base URL for route you are adding.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 * @param array  $args      Optional. Either an array of options for the endpoint, or an array of arrays for
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 *                          multiple methods. Default empty array.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
 * @param bool   $override  Optional. If the route already exists, should we override it? True overrides,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 *                          false merges (with newer overriding if duplicate keys exist). Default false.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
 * @return bool True on success, false on error.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
function register_rest_route( $namespace, $route, $args = array(), $override = false ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	if ( empty( $namespace ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		/*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		 * Non-namespaced routes are not allowed, with the exception of the main
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
		 * and namespace indexes. If you really need to register a
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		 * non-namespaced route, call `WP_REST_Server::register_route` directly.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
		 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		_doing_it_wrong( 'register_rest_route', __( 'Routes must be namespaced with plugin or theme name and version.' ), '4.4.0' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		return false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    43
	} elseif ( empty( $route ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		_doing_it_wrong( 'register_rest_route', __( 'Route must be specified.' ), '4.4.0' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		return false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    48
	$clean_namespace = trim( $namespace, '/' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    49
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    50
	if ( $clean_namespace !== $namespace ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    51
		_doing_it_wrong( __FUNCTION__, __( 'Namespace must not start or end with a slash.' ), '5.4.2' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    52
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    53
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    54
	if ( ! did_action( 'rest_api_init' ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    55
		_doing_it_wrong(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    56
			'register_rest_route',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    57
			sprintf(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    58
				/* translators: %s: rest_api_init */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    59
				__( 'REST API routes must be registered on the %s action.' ),
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    60
				'<code>rest_api_init</code>'
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    61
			),
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    62
			'5.1.0'
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    63
		);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    64
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    65
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	if ( isset( $args['args'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		$common_args = $args['args'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		unset( $args['args'] );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		$common_args = array();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	if ( isset( $args['callback'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		// Upgrade a single set to multiple.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		$args = array( $args );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	$defaults = array(
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    79
		'methods'  => 'GET',
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    80
		'callback' => null,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    81
		'args'     => array(),
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	);
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    83
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	foreach ( $args as $key => &$arg_group ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		if ( ! is_numeric( $key ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
			// Route option, skip here.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			continue;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
    90
		$arg_group         = array_merge( $defaults, $arg_group );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		$arg_group['args'] = array_merge( $common_args, $arg_group['args'] );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    92
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    93
		if ( ! isset( $arg_group['permission_callback'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    94
			_doing_it_wrong(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    95
				__FUNCTION__,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    96
				sprintf(
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
    97
					/* translators: 1: The REST API route being registered, 2: The argument name, 3: The suggested function name. */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    98
					__( 'The REST API route definition for %1$s is missing the required %2$s argument. For REST API routes that are intended to be public, use %3$s as the permission callback.' ),
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    99
					'<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   100
					'<code>permission_callback</code>',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   101
					'<code>__return_true</code>'
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   102
				),
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   103
				'5.5.0'
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   104
			);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   105
		}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   108
	$full_route = '/' . $clean_namespace . '/' . trim( $route, '/' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   109
	rest_get_server()->register_route( $clean_namespace, $full_route, $args, $override );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	return true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
 * Registers a new field on an existing WordPress object type.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
 * @global array $wp_rest_additional_fields Holds registered fields, organized
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
 *                                          by object type.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   121
 * @param string|array $object_type Object(s) the field is being registered to,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   122
 *                                  "post"|"term"|"comment" etc.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   123
 * @param string       $attribute   The attribute name.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   124
 * @param array        $args {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
 *     Optional. An array of arguments used to handle the registered field.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   127
 *     @type callable|null $get_callback    Optional. The callback function used to retrieve the field value. Default is
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   128
 *                                          'null', the field will not be returned in the response. The function will
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   129
 *                                          be passed the prepared object data.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   130
 *     @type callable|null $update_callback Optional. The callback function used to set and update the field value. Default
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   131
 *                                          is 'null', the value cannot be set or updated. The function will be passed
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   132
 *                                          the model object, like WP_Post.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   133
 *     @type array|null $schema             Optional. The schema for this field.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   134
 *                                          Default is 'null', no schema entry will be returned.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 * }
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
function register_rest_field( $object_type, $attribute, $args = array() ) {
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   138
	global $wp_rest_additional_fields;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   139
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
	$defaults = array(
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
		'get_callback'    => null,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
		'update_callback' => null,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		'schema'          => null,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
	);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
	$args = wp_parse_args( $args, $defaults );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	$object_types = (array) $object_type;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
	foreach ( $object_types as $object_type ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
		$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
/**
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   156
 * Registers rewrite rules for the REST API.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
 * @see rest_api_register_rewrites()
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
 * @global WP $wp Current WordPress environment instance.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
function rest_api_init() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
	rest_api_register_rewrites();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	global $wp;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
	$wp->add_query_var( 'rest_route' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
 * Adds REST rewrite rules.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
 * @see add_rewrite_rule()
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   176
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
function rest_api_register_rewrites() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	global $wp_rewrite;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   181
	add_rewrite_rule( '^' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   182
	add_rewrite_rule( '^' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   183
	add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/?$', 'index.php?rest_route=/', 'top' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   184
	add_rewrite_rule( '^' . $wp_rewrite->index . '/' . rest_get_url_prefix() . '/(.*)?', 'index.php?rest_route=/$matches[1]', 'top' );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
 * Registers the default REST API filters.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
 * Attached to the {@see 'rest_api_init'} action
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
 * to make testing and disabling these filters easier.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
function rest_api_default_filters() {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   196
	if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   197
		// Deprecated reporting.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   198
		add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   199
		add_filter( 'deprecated_function_trigger_error', '__return_false' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   200
		add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   201
		add_filter( 'deprecated_argument_trigger_error', '__return_false' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   202
		add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   203
		add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   204
	}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	// Default serving.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
	add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
	add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
	add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 );
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   212
	add_filter( 'rest_index', 'rest_add_application_passwords_to_index' );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 * Registers default REST API routes.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
function create_initial_rest_routes() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
	foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   222
		$controller = $post_type->get_rest_controller();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   224
		if ( ! $controller ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
			continue;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
		$controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
		if ( post_type_supports( $post_type->name, 'revisions' ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
			$revisions_controller = new WP_REST_Revisions_Controller( $post_type->name );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
			$revisions_controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   234
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   235
		if ( 'attachment' !== $post_type->name ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   236
			$autosaves_controller = new WP_REST_Autosaves_Controller( $post_type->name );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   237
			$autosaves_controller->register_routes();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   238
		}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	// Post types.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
	$controller = new WP_REST_Post_Types_Controller;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
	$controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
	// Post statuses.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
	$controller = new WP_REST_Post_Statuses_Controller;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
	$controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
	// Taxonomies.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
	$controller = new WP_REST_Taxonomies_Controller;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
	$controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
	// Terms.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
	foreach ( get_taxonomies( array( 'show_in_rest' => true ), 'object' ) as $taxonomy ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   255
		$controller = $taxonomy->get_rest_controller();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   257
		if ( ! $controller ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
			continue;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
		$controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
	// Users.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
	$controller = new WP_REST_Users_Controller;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
	$controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   268
	// Application Passwords
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   269
	$controller = new WP_REST_Application_Passwords_Controller();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   270
	$controller->register_routes();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   271
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
	// Comments.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
	$controller = new WP_REST_Comments_Controller;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	$controller->register_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   276
	$search_handlers = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   277
		new WP_REST_Post_Search_Handler(),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   278
		new WP_REST_Term_Search_Handler(),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   279
		new WP_REST_Post_Format_Search_Handler(),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   280
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   281
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   282
	/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   283
	 * Filters the search handlers to use in the REST search controller.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   284
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   285
	 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   286
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   287
	 * @param array $search_handlers List of search handlers to use in the controller. Each search
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   288
	 *                               handler instance must extend the `WP_REST_Search_Handler` class.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   289
	 *                               Default is only a handler for posts.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   290
	 */
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   291
	$search_handlers = apply_filters( 'wp_rest_search_handlers', $search_handlers );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   292
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   293
	$controller = new WP_REST_Search_Controller( $search_handlers );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   294
	$controller->register_routes();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   295
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   296
	// Block Renderer.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   297
	$controller = new WP_REST_Block_Renderer_Controller();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   298
	$controller->register_routes();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   299
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   300
	// Block Types.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   301
	$controller = new WP_REST_Block_Types_Controller();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   302
	$controller->register_routes();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   303
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   304
	// Global Styles.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   305
	$controller = new WP_REST_Global_Styles_Controller;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   306
	$controller->register_routes();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   307
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
	// Settings.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
	$controller = new WP_REST_Settings_Controller;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
	$controller->register_routes();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   311
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   312
	// Themes.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   313
	$controller = new WP_REST_Themes_Controller;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   314
	$controller->register_routes();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   315
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   316
	// Plugins.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   317
	$controller = new WP_REST_Plugins_Controller();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   318
	$controller->register_routes();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   319
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   320
	// Sidebars.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   321
	$controller = new WP_REST_Sidebars_Controller();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   322
	$controller->register_routes();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   323
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   324
	// Widget Types.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   325
	$controller = new WP_REST_Widget_Types_Controller();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   326
	$controller->register_routes();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   327
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   328
	// Widgets.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   329
	$controller = new WP_REST_Widgets_Controller();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   330
	$controller->register_routes();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   331
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   332
	// Block Directory.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   333
	$controller = new WP_REST_Block_Directory_Controller();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   334
	$controller->register_routes();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   335
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   336
	// Pattern Directory.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   337
	$controller = new WP_REST_Pattern_Directory_Controller();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   338
	$controller->register_routes();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   339
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   340
	// Block Patterns.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   341
	$controller = new WP_REST_Block_Patterns_Controller();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   342
	$controller->register_routes();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   343
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   344
	// Block Pattern Categories.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   345
	$controller = new WP_REST_Block_Pattern_Categories_Controller();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   346
	$controller->register_routes();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   347
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   348
	// Site Health.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   349
	$site_health = WP_Site_Health::get_instance();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   350
	$controller  = new WP_REST_Site_Health_Controller( $site_health );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   351
	$controller->register_routes();
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   352
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   353
	// URL Details.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   354
	$controller = new WP_REST_URL_Details_Controller();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   355
	$controller->register_routes();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   356
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   357
	// Menu Locations.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   358
	$controller = new WP_REST_Menu_Locations_Controller();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   359
	$controller->register_routes();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   360
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   361
	// Site Editor Export.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   362
	$controller = new WP_REST_Edit_Site_Export_Controller();
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   363
	$controller->register_routes();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
 * Loads the REST API.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   371
 * @global WP $wp Current WordPress environment instance.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
function rest_api_loaded() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
	if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
	 * Whether this is a REST Request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
	 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
	 * @var bool
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
	define( 'REST_REQUEST', true );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
	// Initialize the server.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	$server = rest_get_server();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
	// Fire off the request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	$route = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	if ( empty( $route ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		$route = '/';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
	$server->serve_request( $route );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
	// We're done.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
	die();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
 * Retrieves the URL prefix for any API resource.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
 * @return string Prefix.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
function rest_get_url_prefix() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
	 * Filters the REST URL prefix.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
	 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
	 * @param string $prefix URL prefix. Default 'wp-json'.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
	return apply_filters( 'rest_url_prefix', 'wp-json' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
 * Retrieves the URL to a REST endpoint on a site.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
 * Note: The returned URL is NOT escaped.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
 * @todo Check if this is even necessary
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   426
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   428
 * @param int|null $blog_id Optional. Blog ID. Default of null returns URL for current blog.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   429
 * @param string   $path    Optional. REST route. Default '/'.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   430
 * @param string   $scheme  Optional. Sanitization scheme. Default 'rest'.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
 * @return string Full URL to the endpoint.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
	if ( empty( $path ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
		$path = '/';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   438
	$path = '/' . ltrim( $path, '/' );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   439
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
	if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
		global $wp_rewrite;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
		if ( $wp_rewrite->using_index_permalinks() ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
			$url = get_home_url( $blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
		} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
			$url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   449
		$url .= $path;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
	} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
		$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   452
		// nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
		// To work around this, we manually add index.php to the URL, avoiding the redirect.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
		if ( 'index.php' !== substr( $url, 9 ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
			$url .= 'index.php';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
		$url = add_query_arg( 'rest_route', $path, $url );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   461
	if ( is_ssl() && isset( $_SERVER['SERVER_NAME'] ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
		// If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   463
		if ( parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) === $_SERVER['SERVER_NAME'] ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
			$url = set_url_scheme( $url, 'https' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	if ( is_admin() && force_ssl_admin() ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   469
		/*
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   470
		 * In this situation the home URL may be http:, and `is_ssl()` may be false,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   471
		 * but the admin is served over https: (one way or another), so REST API usage
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   472
		 * will be blocked by browsers unless it is also served over HTTPS.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   473
		 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
		$url = set_url_scheme( $url, 'https' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
	 * Filters the REST URL.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	 * Use this filter to adjust the url returned by the get_rest_url() function.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
	 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
	 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   484
	 * @param string   $url     REST URL.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   485
	 * @param string   $path    REST route.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   486
	 * @param int|null $blog_id Blog ID.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   487
	 * @param string   $scheme  Sanitization scheme.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
	return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
 * Retrieves the URL to a REST endpoint.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
 * Note: The returned URL is NOT escaped.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
 * @param string $path   Optional. REST route. Default empty.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   500
 * @param string $scheme Optional. Sanitization scheme. Default 'rest'.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
 * @return string Full URL to the endpoint.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   503
function rest_url( $path = '', $scheme = 'rest' ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	return get_rest_url( null, $path, $scheme );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
 * Do a REST request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
 * Used primarily to route internal requests through WP_REST_Server.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
 * @param WP_REST_Request|string $request Request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
 * @return WP_REST_Response REST response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
function rest_do_request( $request ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
	$request = rest_ensure_request( $request );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
	return rest_get_server()->dispatch( $request );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
 * Retrieves the current REST server instance.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
 * Instantiates a new instance if none exists already.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
 * @since 4.5.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
 * @global WP_REST_Server $wp_rest_server REST server instance.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
 * @return WP_REST_Server REST server instance.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
function rest_get_server() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
	/* @var WP_REST_Server $wp_rest_server */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
	global $wp_rest_server;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
	if ( empty( $wp_rest_server ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
		/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
		 * Filters the REST Server Class.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
		 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   541
		 * This filter allows you to adjust the server class used by the REST API, using a
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
		 * different class to handle requests.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
		 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		 * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
		 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
		$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   549
		$wp_rest_server       = new $wp_rest_server_class;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		/**
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   552
		 * Fires when preparing to serve a REST API request.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
		 * Endpoint objects should be created and register their hooks on this action rather
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
		 * than another action to ensure they're only loaded when needed.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
		 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
		 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
		 * @param WP_REST_Server $wp_rest_server Server object.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
		 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
		do_action( 'rest_api_init', $wp_rest_server );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
	return $wp_rest_server;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
 * Ensures request arguments are a request object (for consistency).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
 * @since 4.4.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   571
 * @since 5.3.0 Accept string argument for the request path.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   573
 * @param array|string|WP_REST_Request $request Request to check.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
 * @return WP_REST_Request REST request instance.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
function rest_ensure_request( $request ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
	if ( $request instanceof WP_REST_Request ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
		return $request;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   581
	if ( is_string( $request ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   582
		return new WP_REST_Request( 'GET', $request );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   583
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   584
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
	return new WP_REST_Request( 'GET', '', $request );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
 * Ensures a REST response is a response object (for consistency).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   591
 * This implements WP_REST_Response, allowing usage of `set_status`/`header`/etc
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
 * without needing to double-check the object. Will also allow WP_Error to indicate error
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
 * responses, so users should immediately check for this value.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   597
 * @param WP_REST_Response|WP_Error|WP_HTTP_Response|mixed $response Response to check.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   598
 * @return WP_REST_Response|WP_Error If response generated an error, WP_Error, if response
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   599
 *                                   is already an instance, WP_REST_Response, otherwise
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   600
 *                                   returns a new WP_REST_Response instance.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
function rest_ensure_response( $response ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
	if ( is_wp_error( $response ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
		return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   607
	if ( $response instanceof WP_REST_Response ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   608
		return $response;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   609
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   610
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   611
	// While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   612
	// all the required methods used in WP_REST_Server::dispatch().
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
	if ( $response instanceof WP_HTTP_Response ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   614
		return new WP_REST_Response(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   615
			$response->get_data(),
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   616
			$response->get_status(),
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   617
			$response->get_headers()
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   618
		);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
	return new WP_REST_Response( $response );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
 * Handles _deprecated_function() errors.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
 * @param string $function    The function that was called.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
 * @param string $replacement The function that should have been called.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
 * @param string $version     Version.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
function rest_handle_deprecated_function( $function, $replacement, $version ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
	if ( ! WP_DEBUG || headers_sent() ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	if ( ! empty( $replacement ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   638
		/* translators: 1: Function name, 2: WordPress version number, 3: New function name. */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
		$string = sprintf( __( '%1$s (since %2$s; use %3$s instead)' ), $function, $version, $replacement );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
	} else {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   641
		/* translators: 1: Function name, 2: WordPress version number. */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
		$string = sprintf( __( '%1$s (since %2$s; no alternative available)' ), $function, $version );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
	header( sprintf( 'X-WP-DeprecatedFunction: %s', $string ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
 * Handles _deprecated_argument() errors.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
 * @param string $function    The function that was called.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
 * @param string $message     A message regarding the change.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
 * @param string $version     Version.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
function rest_handle_deprecated_argument( $function, $message, $version ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
	if ( ! WP_DEBUG || headers_sent() ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
	}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   661
	if ( $message ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   662
		/* translators: 1: Function name, 2: WordPress version number, 3: Error message. */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
		$string = sprintf( __( '%1$s (since %2$s; %3$s)' ), $function, $version, $message );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
	} else {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   665
		/* translators: 1: Function name, 2: WordPress version number. */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
		$string = sprintf( __( '%1$s (since %2$s; no alternative available)' ), $function, $version );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
	header( sprintf( 'X-WP-DeprecatedParam: %s', $string ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   673
 * Handles _doing_it_wrong errors.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   674
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   675
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   676
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   677
 * @param string      $function The function that was called.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   678
 * @param string      $message  A message explaining what has been done incorrectly.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   679
 * @param string|null $version  The version of WordPress where the message was added.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   680
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   681
function rest_handle_doing_it_wrong( $function, $message, $version ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   682
	if ( ! WP_DEBUG || headers_sent() ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   683
		return;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   684
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   685
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   686
	if ( $version ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   687
		/* translators: Developer debugging message. 1: PHP function name, 2: WordPress version number, 3: Explanatory message. */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   688
		$string = __( '%1$s (since %2$s; %3$s)' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   689
		$string = sprintf( $string, $function, $version, $message );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   690
	} else {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   691
		/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message. */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   692
		$string = __( '%1$s (%2$s)' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   693
		$string = sprintf( $string, $function, $message );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   694
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   695
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   696
	header( sprintf( 'X-WP-DoingItWrong: %s', $string ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   697
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   698
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   699
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
 * Sends Cross-Origin Resource Sharing headers with API requests.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
 * @param mixed $value Response data.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
 * @return mixed Response data.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
function rest_send_cors_headers( $value ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
	$origin = get_http_origin();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
	if ( $origin ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   711
		// Requests from file:// and data: URLs send "Origin: null".
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
		if ( 'null' !== $origin ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
			$origin = esc_url_raw( $origin );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
		header( 'Access-Control-Allow-Origin: ' . $origin );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
		header( 'Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, PATCH, DELETE' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
		header( 'Access-Control-Allow-Credentials: true' );
13
d255fe9cd479 Upgrade wordpress again
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   718
		header( 'Vary: Origin', false );
d255fe9cd479 Upgrade wordpress again
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   719
	} elseif ( ! headers_sent() && 'GET' === $_SERVER['REQUEST_METHOD'] && ! is_user_logged_in() ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   720
		header( 'Vary: Origin', false );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
	return $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
 * Handles OPTIONS requests for the server.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
 * This is handled outside of the server code, as it doesn't obey normal route
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
 * mapping.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
 * @param mixed           $response Current response, either response or `null` to indicate pass-through.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
 * @param WP_REST_Server  $handler  ResponseHandler instance (usually WP_REST_Server).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
 * @param WP_REST_Request $request  The request that was used to make current response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
 * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
function rest_handle_options_request( $response, $handler, $request ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
	if ( ! empty( $response ) || $request->get_method() !== 'OPTIONS' ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
		return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
	$response = new WP_REST_Response();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   745
	$data     = array();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
	foreach ( $handler->get_routes() as $route => $endpoints ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   748
		$match = preg_match( '@^' . $route . '$@i', $request->get_route(), $matches );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
		if ( ! $match ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
			continue;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   754
		$args = array();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   755
		foreach ( $matches as $param => $value ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   756
			if ( ! is_int( $param ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   757
				$args[ $param ] = $value;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   758
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   759
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   760
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   761
		foreach ( $endpoints as $endpoint ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   762
			// Remove the redundant preg_match() argument.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   763
			unset( $args[0] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   764
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   765
			$request->set_url_params( $args );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   766
			$request->set_attributes( $endpoint );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   767
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   768
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
		$data = $handler->get_data_for_route( $route, $endpoints, 'help' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
		$response->set_matched_route( $route );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
		break;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
	$response->set_data( $data );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
	return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
 * Sends the "Allow" header to state all methods that can be sent to the current route.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
 * @param WP_REST_Response $response Current response being served.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
 * @param WP_REST_Server   $server   ResponseHandler instance (usually WP_REST_Server).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
 * @param WP_REST_Request  $request  The request that was used to make current response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
 * @return WP_REST_Response Response to be served, with "Allow" header if route has allowed methods.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
function rest_send_allow_header( $response, $server, $request ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
	$matched_route = $response->get_matched_route();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
	if ( ! $matched_route ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
		return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
	$routes = $server->get_routes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
	$allowed_methods = array();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
	// Get the allowed methods across the routes.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
	foreach ( $routes[ $matched_route ] as $_handler ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
		foreach ( $_handler['methods'] as $handler_method => $value ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
			if ( ! empty( $_handler['permission_callback'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
				$permission = call_user_func( $_handler['permission_callback'], $request );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
				$allowed_methods[ $handler_method ] = true === $permission;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
			} else {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
				$allowed_methods[ $handler_method ] = true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
			}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
	// Strip out all the methods that are not allowed (false values).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
	$allowed_methods = array_filter( $allowed_methods );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
	if ( $allowed_methods ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
		$response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
	return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   825
 * Recursively computes the intersection of arrays using keys for comparison.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   826
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   827
 * @since 5.3.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   828
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   829
 * @param array $array1 The array with master keys to check.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   830
 * @param array $array2 An array to compare keys against.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   831
 * @return array An associative array containing all the entries of array1 which have keys
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   832
 *               that are present in all arguments.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   833
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   834
function _rest_array_intersect_key_recursive( $array1, $array2 ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   835
	$array1 = array_intersect_key( $array1, $array2 );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   836
	foreach ( $array1 as $key => $value ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   837
		if ( is_array( $value ) && is_array( $array2[ $key ] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   838
			$array1[ $key ] = _rest_array_intersect_key_recursive( $value, $array2[ $key ] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   839
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   840
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   841
	return $array1;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   842
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   843
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   844
/**
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   845
 * Filters the REST API response to include only a white-listed set of response object fields.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
 * @since 4.8.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
 * @param WP_REST_Response $response Current response being served.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
 * @param WP_REST_Server   $server   ResponseHandler instance (usually WP_REST_Server).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
 * @param WP_REST_Request  $request  The request that was used to make current response.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
 * @return WP_REST_Response Response to be served, trimmed down to contain a subset of fields.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
function rest_filter_response_fields( $response, $server, $request ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
	if ( ! isset( $request['_fields'] ) || $response->is_error() ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
		return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
	$data = $response->get_data();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   861
	$fields = wp_parse_list( $request['_fields'] );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
	if ( 0 === count( $fields ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
		return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
	// Trim off outside whitespace from the comma delimited list.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
	$fields = array_map( 'trim', $fields );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   870
	// Create nested array of accepted field hierarchy.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   871
	$fields_as_keyed = array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   872
	foreach ( $fields as $field ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   873
		$parts = explode( '.', $field );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   874
		$ref   = &$fields_as_keyed;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   875
		while ( count( $parts ) > 1 ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   876
			$next = array_shift( $parts );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   877
			if ( isset( $ref[ $next ] ) && true === $ref[ $next ] ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   878
				// Skip any sub-properties if their parent prop is already marked for inclusion.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   879
				break 2;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   880
			}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   881
			$ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   882
			$ref          = &$ref[ $next ];
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   883
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   884
		$last         = array_shift( $parts );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   885
		$ref[ $last ] = true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   886
	}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
	if ( wp_is_numeric_array( $data ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
		$new_data = array();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
		foreach ( $data as $item ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   891
			$new_data[] = _rest_array_intersect_key_recursive( $item, $fields_as_keyed );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
	} else {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   894
		$new_data = _rest_array_intersect_key_recursive( $data, $fields_as_keyed );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
	$response->set_data( $new_data );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
	return $response;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   903
 * Given an array of fields to include in a response, some of which may be
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   904
 * `nested.fields`, determine whether the provided field should be included
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   905
 * in the response body.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   906
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   907
 * If a parent field is passed in, the presence of any nested field within
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   908
 * that parent will cause the method to return `true`. For example "title"
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   909
 * will return true if any of `title`, `title.raw` or `title.rendered` is
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   910
 * provided.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   911
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   912
 * @since 5.3.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   913
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   914
 * @param string $field  A field to test for inclusion in the response body.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   915
 * @param array  $fields An array of string fields supported by the endpoint.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   916
 * @return bool Whether to include the field or not.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   917
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   918
function rest_is_field_included( $field, $fields ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   919
	if ( in_array( $field, $fields, true ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   920
		return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   921
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   922
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   923
	foreach ( $fields as $accepted_field ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   924
		// Check to see if $field is the parent of any item in $fields.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   925
		// A field "parent" should be accepted if "parent.child" is accepted.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   926
		if ( strpos( $accepted_field, "$field." ) === 0 ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   927
			return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   928
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   929
		// Conversely, if "parent" is accepted, all "parent.child" fields
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   930
		// should also be accepted.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   931
		if ( strpos( $field, "$accepted_field." ) === 0 ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   932
			return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   933
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   934
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   935
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   936
	return false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   937
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   938
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   939
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
 * Adds the REST API URL to the WP RSD endpoint.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
 * @see get_rest_url()
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
function rest_output_rsd() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
	$api_root = get_rest_url();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
	if ( empty( $api_root ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
	?>
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
	<api name="WP-API" blogID="1" preferred="false" apiLink="<?php echo esc_url( $api_root ); ?>" />
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
	<?php
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
 * Outputs the REST API link tag into page header.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
 * @see get_rest_url()
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
function rest_output_link_wp_head() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
	$api_root = get_rest_url();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
	if ( empty( $api_root ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   971
	printf( '<link rel="https://api.w.org/" href="%s" />', esc_url( $api_root ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   972
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   973
	$resource = rest_get_queried_resource_route();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   974
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   975
	if ( $resource ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   976
		printf( '<link rel="alternate" type="application/json" href="%s" />', esc_url( rest_url( $resource ) ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   977
	}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
 * Sends a Link header for the REST API.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
function rest_output_link_header() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
	if ( headers_sent() ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
	$api_root = get_rest_url();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
	if ( empty( $api_root ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   996
	header( sprintf( 'Link: <%s>; rel="https://api.w.org/"', esc_url_raw( $api_root ) ), false );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   997
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   998
	$resource = rest_get_queried_resource_route();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   999
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1000
	if ( $resource ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1001
		header( sprintf( 'Link: <%s>; rel="alternate"; type="application/json"', esc_url_raw( rest_url( $resource ) ) ), false );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1002
	}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
 * Checks for errors when using cookie-based authentication.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
 * WordPress' built-in cookie authentication is always active
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
 * for logged in users. However, the API has to check nonces
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
 * for each request to ensure users are not vulnerable to CSRF.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
 * @global mixed          $wp_rest_auth_cookie
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
 * @param WP_Error|mixed $result Error from another authentication handler,
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1017
 *                               null if we should handle it, or another value if not.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
function rest_cookie_check_errors( $result ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
	if ( ! empty( $result ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
		return $result;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
	global $wp_rest_auth_cookie;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
	/*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
	 * Is cookie authentication being used? (If we get an auth
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
	 * error, but we're still logged in, another authentication
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	 * must have been used).
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
	if ( true !== $wp_rest_auth_cookie && is_user_logged_in() ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
		return $result;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
	// Determine if there is a nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
	$nonce = null;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
	if ( isset( $_REQUEST['_wpnonce'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
		$nonce = $_REQUEST['_wpnonce'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
	} elseif ( isset( $_SERVER['HTTP_X_WP_NONCE'] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
		$nonce = $_SERVER['HTTP_X_WP_NONCE'];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
	if ( null === $nonce ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
		// No nonce at all, so act as if it's an unauthenticated request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
		wp_set_current_user( 0 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
		return true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
	// Check the nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
	$result = wp_verify_nonce( $nonce, 'wp_rest' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
	if ( ! $result ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1055
		return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie check failed' ), array( 'status' => 403 ) );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
	// Send a refreshed nonce in header.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
	rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
	return true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
 * Collects cookie authentication status.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
 * Collects errors from wp_validate_auth_cookie for use by rest_cookie_check_errors.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
 * @see current_action()
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
 * @global mixed $wp_rest_auth_cookie
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
function rest_cookie_collect_status() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
	global $wp_rest_auth_cookie;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
	$status_type = current_action();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
	if ( 'auth_cookie_valid' !== $status_type ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
		$wp_rest_auth_cookie = substr( $status_type, 12 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
		return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
	$wp_rest_auth_cookie = true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
/**
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1088
 * Collects the status of authenticating with an application password.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1089
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1090
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1091
 * @since 5.7.0 Added the `$app_password` parameter.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1092
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1093
 * @global WP_User|WP_Error|null $wp_rest_application_password_status
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1094
 * @global string|null $wp_rest_application_password_uuid
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1095
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1096
 * @param WP_Error $user_or_error The authenticated user or error instance.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1097
 * @param array    $app_password  The Application Password used to authenticate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1098
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1099
function rest_application_password_collect_status( $user_or_error, $app_password = array() ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1100
	global $wp_rest_application_password_status, $wp_rest_application_password_uuid;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1101
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1102
	$wp_rest_application_password_status = $user_or_error;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1103
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1104
	if ( empty( $app_password['uuid'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1105
		$wp_rest_application_password_uuid = null;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1106
	} else {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1107
		$wp_rest_application_password_uuid = $app_password['uuid'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1108
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1109
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1110
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1111
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1112
 * Gets the Application Password used for authenticating the request.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1113
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1114
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1115
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1116
 * @global string|null $wp_rest_application_password_uuid
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1117
 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  1118
 * @return string|null The Application Password UUID, or null if Application Passwords was not used.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1119
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1120
function rest_get_authenticated_app_password() {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1121
	global $wp_rest_application_password_uuid;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1122
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1123
	return $wp_rest_application_password_uuid;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1124
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1125
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1126
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1127
 * Checks for errors when using application password-based authentication.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1128
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1129
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1130
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1131
 * @global WP_User|WP_Error|null $wp_rest_application_password_status
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1132
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1133
 * @param WP_Error|null|true $result Error from another authentication handler,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1134
 *                                   null if we should handle it, or another value if not.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1135
 * @return WP_Error|null|true WP_Error if the application password is invalid, the $result, otherwise true.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1136
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1137
function rest_application_password_check_errors( $result ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1138
	global $wp_rest_application_password_status;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1139
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1140
	if ( ! empty( $result ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1141
		return $result;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1142
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1143
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1144
	if ( is_wp_error( $wp_rest_application_password_status ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1145
		$data = $wp_rest_application_password_status->get_error_data();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1146
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1147
		if ( ! isset( $data['status'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1148
			$data['status'] = 401;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1149
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1150
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1151
		$wp_rest_application_password_status->add_data( $data );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1152
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1153
		return $wp_rest_application_password_status;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1154
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1155
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1156
	if ( $wp_rest_application_password_status instanceof WP_User ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1157
		return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1158
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1159
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1160
	return $result;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1161
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1162
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1163
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1164
 * Adds Application Passwords info to the REST API index.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1165
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1166
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1167
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1168
 * @param WP_REST_Response $response The index response object.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1169
 * @return WP_REST_Response
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1170
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1171
function rest_add_application_passwords_to_index( $response ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1172
	if ( ! wp_is_application_passwords_available() ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1173
		return $response;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1174
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1175
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1176
	$response->data['authentication']['application-passwords'] = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1177
		'endpoints' => array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1178
			'authorization' => admin_url( 'authorize-application.php' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1179
		),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1180
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1181
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1182
	return $response;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1183
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1184
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1185
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1186
 * Retrieves the avatar urls in various sizes.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1187
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1188
 * @since 4.7.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1189
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1190
 * @see get_avatar_url()
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1191
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1192
 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1193
 *                           user email, WP_User object, WP_Post object, or WP_Comment object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1194
 * @return array Avatar URLs keyed by size. Each value can be a URL string or boolean false.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1195
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1196
function rest_get_avatar_urls( $id_or_email ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1197
	$avatar_sizes = rest_get_avatar_sizes();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1198
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1199
	$urls = array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1200
	foreach ( $avatar_sizes as $size ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1201
		$urls[ $size ] = get_avatar_url( $id_or_email, array( 'size' => $size ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1202
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1203
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1204
	return $urls;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1205
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1206
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1207
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1208
 * Retrieves the pixel sizes for avatars.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1209
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1210
 * @since 4.7.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1211
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1212
 * @return int[] List of pixel sizes for avatars. Default `[ 24, 48, 96 ]`.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1213
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1214
function rest_get_avatar_sizes() {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1215
	/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1216
	 * Filters the REST avatar sizes.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1217
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1218
	 * Use this filter to adjust the array of sizes returned by the
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1219
	 * `rest_get_avatar_sizes` function.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1220
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1221
	 * @since 4.4.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1222
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1223
	 * @param int[] $sizes An array of int values that are the pixel sizes for avatars.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1224
	 *                     Default `[ 24, 48, 96 ]`.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1225
	 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1226
	return apply_filters( 'rest_avatar_sizes', array( 24, 48, 96 ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1227
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1228
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1229
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
 * Parses an RFC3339 time into a Unix timestamp.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
 * @param string $date      RFC3339 timestamp.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
 * @param bool   $force_utc Optional. Whether to force UTC timezone instead of using
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
 *                          the timestamp's timezone. Default false.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
 * @return int Unix timestamp.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
function rest_parse_date( $date, $force_utc = false ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
	if ( $force_utc ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
		$date = preg_replace( '/[+-]\d+:?\d+$/', '+00:00', $date );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
	$regex = '#^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}(?::\d{2})?)?$#';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
	if ( ! preg_match( $regex, $date, $matches ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
		return false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
	return strtotime( $date );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1254
 * Parses a 3 or 6 digit hex color (with #).
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1255
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1256
 * @since 5.4.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1257
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1258
 * @param string $color 3 or 6 digit hex color (with #).
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1259
 * @return string|false
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1260
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1261
function rest_parse_hex_color( $color ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1262
	$regex = '|^#([A-Fa-f0-9]{3}){1,2}$|';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1263
	if ( ! preg_match( $regex, $color, $matches ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1264
		return false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1265
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1266
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1267
	return $color;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1268
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1269
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1270
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
 * @since 4.4.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
 * @see rest_parse_date()
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
 * @param string $date   RFC3339 timestamp.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
 * @param bool   $is_utc Whether the provided date should be interpreted as UTC. Default false.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
 * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
 *                    null on failure.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
function rest_get_date_with_gmt( $date, $is_utc = false ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1283
	/*
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1284
	 * Whether or not the original date actually has a timezone string
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1285
	 * changes the way we need to do timezone conversion.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1286
	 * Store this info before parsing the date, and use it later.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1287
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
	$has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
	$date = rest_parse_date( $date );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
	if ( empty( $date ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
		return null;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1296
	/*
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1297
	 * At this point $date could either be a local date (if we were passed
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1298
	 * a *local* date without a timezone offset) or a UTC date (otherwise).
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1299
	 * Timezone conversion needs to be handled differently between these two cases.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1300
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
	if ( ! $is_utc && ! $has_timezone ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1302
		$local = gmdate( 'Y-m-d H:i:s', $date );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  1303
		$utc   = get_gmt_from_date( $local );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
	} else {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1305
		$utc   = gmdate( 'Y-m-d H:i:s', $date );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
		$local = get_date_from_gmt( $utc );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
	return array( $local, $utc );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
 * Returns a contextual HTTP error code for authorization failure.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
 *
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1317
 * @return int 401 if the user is not logged in, 403 if the user is logged in.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
function rest_authorization_required_code() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
	return is_user_logged_in() ? 403 : 401;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
 * Validate a request argument based on details registered to the route.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1328
 * @param mixed           $value
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1329
 * @param WP_REST_Request $request
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1330
 * @param string          $param
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1331
 * @return true|WP_Error
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
function rest_validate_request_arg( $value, $request, $param ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
	$attributes = $request->get_attributes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
	if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
		return true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
	$args = $attributes['args'][ $param ];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
	return rest_validate_value_from_schema( $value, $args, $param );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
 * Sanitize a request argument based on details registered to the route.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1348
 * @param mixed           $value
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1349
 * @param WP_REST_Request $request
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1350
 * @param string          $param
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1351
 * @return mixed
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
function rest_sanitize_request_arg( $value, $request, $param ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
	$attributes = $request->get_attributes();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
	if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
		return $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
	$args = $attributes['args'][ $param ];
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1360
	return rest_sanitize_value_from_schema( $value, $args, $param );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
 * Parse a request argument based on details registered to the route.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
 * Runs a validation check and sanitizes the value, primarily to be used via
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
 * the `sanitize_callback` arguments in the endpoint args registration.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1371
 * @param mixed           $value
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1372
 * @param WP_REST_Request $request
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1373
 * @param string          $param
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
 * @return mixed
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
function rest_parse_request_arg( $value, $request, $param ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
	$is_valid = rest_validate_request_arg( $value, $request, $param );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
	if ( is_wp_error( $is_valid ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
		return $is_valid;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
	$value = rest_sanitize_request_arg( $value, $request, $param );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
	return $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
 * Determines if an IP address is valid.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
 * Handles both IPv4 and IPv6 addresses.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1395
 * @param string $ip IP address.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
 * @return string|false The valid IP address, otherwise false.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
function rest_is_ip_address( $ip ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
	$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]?)$/';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
	if ( ! preg_match( $ipv4_pattern, $ip ) && ! Requests_IPv6::check_ipv6( $ip ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
		return false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
	return $ip;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
 * Changes a boolean-like value into the proper boolean value.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
 * @param bool|string|int $value The value being evaluated.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1414
 * @return bool Returns the proper associated boolean value.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
function rest_sanitize_boolean( $value ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
	// String values are translated to `true`; make sure 'false' is false.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  1418
	if ( is_string( $value ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
		$value = strtolower( $value );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
		if ( in_array( $value, array( 'false', '0' ), true ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
			$value = false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
	// Everything else will map nicely to boolean.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  1426
	return (bool) $value;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
 * Determines if a given value is boolean-like.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
 * @since 4.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
 * @param bool|string $maybe_bool The value being evaluated.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1435
 * @return bool True if a boolean, otherwise false.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
function rest_is_boolean( $maybe_bool ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
	if ( is_bool( $maybe_bool ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
		return true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
	if ( is_string( $maybe_bool ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
		$maybe_bool = strtolower( $maybe_bool );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
		$valid_boolean_values = array(
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
			'false',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
			'true',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
			'0',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
			'1',
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
		);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
		return in_array( $maybe_bool, $valid_boolean_values, true );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
	if ( is_int( $maybe_bool ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
		return in_array( $maybe_bool, array( 0, 1 ), true );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
	return false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1463
 * Determines if a given value is integer-like.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1464
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1465
 * @since 5.5.0
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1467
 * @param mixed $maybe_integer The value being evaluated.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1468
 * @return bool True if an integer, otherwise false.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1469
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1470
function rest_is_integer( $maybe_integer ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1471
	return is_numeric( $maybe_integer ) && round( (float) $maybe_integer ) === (float) $maybe_integer;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1472
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1473
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1474
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1475
 * Determines if a given value is array-like.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1477
 * @since 5.5.0
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1479
 * @param mixed $maybe_array The value being evaluated.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1480
 * @return bool
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1482
function rest_is_array( $maybe_array ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1483
	if ( is_scalar( $maybe_array ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1484
		$maybe_array = wp_parse_list( $maybe_array );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1485
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1486
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1487
	return wp_is_numeric_array( $maybe_array );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1488
}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1490
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1491
 * Converts an array-like value to an array.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1492
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1493
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1494
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1495
 * @param mixed $maybe_array The value being evaluated.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1496
 * @return array Returns the array extracted from the value.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1497
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1498
function rest_sanitize_array( $maybe_array ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1499
	if ( is_scalar( $maybe_array ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1500
		return wp_parse_list( $maybe_array );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1501
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1502
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1503
	if ( ! is_array( $maybe_array ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1504
		return array();
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1507
	// Normalize to numeric array so nothing unexpected is in the keys.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1508
	return array_values( $maybe_array );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1509
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1510
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1511
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1512
 * Determines if a given value is object-like.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1513
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1514
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1515
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1516
 * @param mixed $maybe_object The value being evaluated.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1517
 * @return bool True if object like, otherwise false.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1518
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1519
function rest_is_object( $maybe_object ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1520
	if ( '' === $maybe_object ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1521
		return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1522
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1523
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1524
	if ( $maybe_object instanceof stdClass ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1525
		return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1526
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1527
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1528
	if ( $maybe_object instanceof JsonSerializable ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1529
		$maybe_object = $maybe_object->jsonSerialize();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1530
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1531
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1532
	return is_array( $maybe_object );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1533
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1534
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1535
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1536
 * Converts an object-like value to an object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1537
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1538
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1539
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1540
 * @param mixed $maybe_object The value being evaluated.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1541
 * @return array Returns the object extracted from the value.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1542
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1543
function rest_sanitize_object( $maybe_object ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1544
	if ( '' === $maybe_object ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1545
		return array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1546
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1547
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1548
	if ( $maybe_object instanceof stdClass ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1549
		return (array) $maybe_object;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1550
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1551
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1552
	if ( $maybe_object instanceof JsonSerializable ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1553
		$maybe_object = $maybe_object->jsonSerialize();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1554
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1555
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1556
	if ( ! is_array( $maybe_object ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1557
		return array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1558
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1559
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1560
	return $maybe_object;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1564
 * Gets the best type for a value.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1565
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1566
 * @since 5.5.0
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1568
 * @param mixed $value The value to check.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1569
 * @param array $types The list of possible types.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1570
 * @return string The best matching type, an empty string if no types match.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1571
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1572
function rest_get_best_type_for_value( $value, $types ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1573
	static $checks = array(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1574
		'array'   => 'rest_is_array',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1575
		'object'  => 'rest_is_object',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1576
		'integer' => 'rest_is_integer',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1577
		'number'  => 'is_numeric',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1578
		'boolean' => 'rest_is_boolean',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1579
		'string'  => 'is_string',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1580
		'null'    => 'is_null',
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1581
	);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1582
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1583
	// Both arrays and objects allow empty strings to be converted to their types.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1584
	// But the best answer for this type is a string.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1585
	if ( '' === $value && in_array( 'string', $types, true ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1586
		return 'string';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1587
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1588
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1589
	foreach ( $types as $type ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1590
		if ( isset( $checks[ $type ] ) && $checks[ $type ]( $value ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1591
			return $type;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1592
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1593
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1594
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1595
	return '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1596
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1597
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1598
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1599
 * Handles getting the best type for a multi-type schema.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1601
 * This is a wrapper for {@see rest_get_best_type_for_value()} that handles
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1602
 * backward compatibility for schemas that use invalid types.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1603
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1604
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1605
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1606
 * @param mixed  $value The value to check.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1607
 * @param array  $args  The schema array to use.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1608
 * @param string $param The parameter name, used in error messages.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1609
 * @return string
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1611
function rest_handle_multi_type_schema( $value, $args, $param = '' ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1612
	$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1613
	$invalid_types = array_diff( $args['type'], $allowed_types );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1614
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1615
	if ( $invalid_types ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1616
		_doing_it_wrong(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1617
			__FUNCTION__,
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1618
			/* translators: 1: Parameter, 2: List of allowed types. */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1619
			wp_sprintf( __( 'The "type" schema keyword for %1$s can only contain the built-in types: %2$l.' ), $param, $allowed_types ),
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1620
			'5.5.0'
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1621
		);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1622
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1623
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1624
	$best_type = rest_get_best_type_for_value( $value, $args['type'] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1625
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1626
	if ( ! $best_type ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1627
		if ( ! $invalid_types ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1628
			return '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1629
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1630
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1631
		// Backward compatibility for previous behavior which allowed the value if there was an invalid type used.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1632
		$best_type = reset( $invalid_types );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1633
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1634
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1635
	return $best_type;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1636
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1637
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1638
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1639
 * Checks if an array is made up of unique items.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1640
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1641
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1642
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1643
 * @param array $array The array to check.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1644
 * @return bool True if the array contains unique items, false otherwise.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1645
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1646
function rest_validate_array_contains_unique_items( $array ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1647
	$seen = array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1648
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1649
	foreach ( $array as $item ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1650
		$stabilized = rest_stabilize_value( $item );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1651
		$key        = serialize( $stabilized );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1652
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1653
		if ( ! isset( $seen[ $key ] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1654
			$seen[ $key ] = true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1655
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1656
			continue;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1657
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1658
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1659
		return false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1660
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1661
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1662
	return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1663
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1664
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1665
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1666
 * Stabilizes a value following JSON Schema semantics.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1667
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1668
 * For lists, order is preserved. For objects, properties are reordered alphabetically.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1669
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1670
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1671
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1672
 * @param mixed $value The value to stabilize. Must already be sanitized. Objects should have been converted to arrays.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1673
 * @return mixed The stabilized value.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1674
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1675
function rest_stabilize_value( $value ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1676
	if ( is_scalar( $value ) || is_null( $value ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1677
		return $value;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1678
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1679
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1680
	if ( is_object( $value ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1681
		_doing_it_wrong( __FUNCTION__, __( 'Cannot stabilize objects. Convert the object to an array first.' ), '5.5.0' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1682
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1683
		return $value;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1684
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1685
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1686
	ksort( $value );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1687
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1688
	foreach ( $value as $k => $v ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1689
		$value[ $k ] = rest_stabilize_value( $v );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1690
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1691
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  1692
	return $value;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
/**
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1696
 * Validates if the JSON Schema pattern matches a value.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1697
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1698
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1699
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1700
 * @param string $pattern The pattern to match against.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1701
 * @param string $value   The value to check.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1702
 * @return bool           True if the pattern matches the given value, false otherwise.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1703
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1704
function rest_validate_json_schema_pattern( $pattern, $value ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1705
	$escaped_pattern = str_replace( '#', '\\#', $pattern );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1706
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1707
	return 1 === preg_match( '#' . $escaped_pattern . '#u', $value );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1708
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1709
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1710
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1711
 * Finds the schema for a property using the patternProperties keyword.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1712
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1713
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1714
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1715
 * @param string $property The property name to check.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1716
 * @param array  $args     The schema array to use.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1717
 * @return array|null      The schema of matching pattern property, or null if no patterns match.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1718
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1719
function rest_find_matching_pattern_property_schema( $property, $args ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1720
	if ( isset( $args['patternProperties'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1721
		foreach ( $args['patternProperties'] as $pattern => $child_schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1722
			if ( rest_validate_json_schema_pattern( $pattern, $property ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1723
				return $child_schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1724
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1725
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1726
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1727
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1728
	return null;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1729
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1730
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1731
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1732
 * Formats a combining operation error into a WP_Error object.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1733
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1734
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1735
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1736
 * @param string $param The parameter name.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1737
 * @param array $error  The error details.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1738
 * @return WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1739
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1740
function rest_format_combining_operation_error( $param, $error ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1741
	$position = $error['index'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1742
	$reason   = $error['error_object']->get_error_message();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1743
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1744
	if ( isset( $error['schema']['title'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1745
		$title = $error['schema']['title'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1746
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1747
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1748
			'rest_no_matching_schema',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1749
			/* translators: 1: Parameter, 2: Schema title, 3: Reason. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1750
			sprintf( __( '%1$s is not a valid %2$s. Reason: %3$s' ), $param, $title, $reason ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1751
			array( 'position' => $position )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1752
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1753
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1754
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1755
	return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1756
		'rest_no_matching_schema',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1757
		/* translators: 1: Parameter, 2: Reason. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1758
		sprintf( __( '%1$s does not match the expected format. Reason: %2$s' ), $param, $reason ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1759
		array( 'position' => $position )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1760
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1761
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1762
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1763
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1764
 * Gets the error of combining operation.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1765
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1766
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1767
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1768
 * @param array  $value  The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1769
 * @param string $param  The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1770
 * @param array  $errors The errors array, to search for possible error.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1771
 * @return WP_Error      The combining operation error.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1772
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1773
function rest_get_combining_operation_error( $value, $param, $errors ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1774
	// If there is only one error, simply return it.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1775
	if ( 1 === count( $errors ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1776
		return rest_format_combining_operation_error( $param, $errors[0] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1777
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1778
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1779
	// Filter out all errors related to type validation.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1780
	$filtered_errors = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1781
	foreach ( $errors as $error ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1782
		$error_code = $error['error_object']->get_error_code();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1783
		$error_data = $error['error_object']->get_error_data();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1784
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1785
		if ( 'rest_invalid_type' !== $error_code || ( isset( $error_data['param'] ) && $param !== $error_data['param'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1786
			$filtered_errors[] = $error;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1787
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1788
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1789
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1790
	// If there is only one error left, simply return it.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1791
	if ( 1 === count( $filtered_errors ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1792
		return rest_format_combining_operation_error( $param, $filtered_errors[0] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1793
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1794
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1795
	// If there are only errors related to object validation, try choosing the most appropriate one.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1796
	if ( count( $filtered_errors ) > 1 && 'object' === $filtered_errors[0]['schema']['type'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1797
		$result = null;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1798
		$number = 0;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1799
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1800
		foreach ( $filtered_errors as $error ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1801
			if ( isset( $error['schema']['properties'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1802
				$n = count( array_intersect_key( $error['schema']['properties'], $value ) );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1803
				if ( $n > $number ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1804
					$result = $error;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1805
					$number = $n;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1806
				}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1807
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1808
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1809
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1810
		if ( null !== $result ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1811
			return rest_format_combining_operation_error( $param, $result );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1812
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1813
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1814
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1815
	// If each schema has a title, include those titles in the error message.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1816
	$schema_titles = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1817
	foreach ( $errors as $error ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1818
		if ( isset( $error['schema']['title'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1819
			$schema_titles[] = $error['schema']['title'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1820
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1821
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1822
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1823
	if ( count( $schema_titles ) === count( $errors ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1824
		/* translators: 1: Parameter, 2: Schema titles. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1825
		return new WP_Error( 'rest_no_matching_schema', wp_sprintf( __( '%1$s is not a valid %2$l.' ), $param, $schema_titles ) );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1826
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1827
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1828
	/* translators: %s: Parameter. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1829
	return new WP_Error( 'rest_no_matching_schema', sprintf( __( '%s does not match any of the expected formats.' ), $param ) );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1830
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1831
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1832
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1833
 * Finds the matching schema among the "anyOf" schemas.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1834
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1835
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1836
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1837
 * @param mixed  $value   The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1838
 * @param array  $args    The schema array to use.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1839
 * @param string $param   The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1840
 * @return array|WP_Error The matching schema or WP_Error instance if all schemas do not match.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1841
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1842
function rest_find_any_matching_schema( $value, $args, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1843
	$errors = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1844
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1845
	foreach ( $args['anyOf'] as $index => $schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1846
		if ( ! isset( $schema['type'] ) && isset( $args['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1847
			$schema['type'] = $args['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1848
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1849
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1850
		$is_valid = rest_validate_value_from_schema( $value, $schema, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1851
		if ( ! is_wp_error( $is_valid ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1852
			return $schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1853
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1854
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1855
		$errors[] = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1856
			'error_object' => $is_valid,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1857
			'schema'       => $schema,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1858
			'index'        => $index,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1859
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1860
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1861
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1862
	return rest_get_combining_operation_error( $value, $param, $errors );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1863
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1864
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1865
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1866
 * Finds the matching schema among the "oneOf" schemas.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1867
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1868
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1869
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1870
 * @param mixed  $value                  The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1871
 * @param array  $args                   The schema array to use.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1872
 * @param string $param                  The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1873
 * @param bool   $stop_after_first_match Optional. Whether the process should stop after the first successful match.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1874
 * @return array|WP_Error                The matching schema or WP_Error instance if the number of matching schemas is not equal to one.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1875
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1876
function rest_find_one_matching_schema( $value, $args, $param, $stop_after_first_match = false ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1877
	$matching_schemas = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1878
	$errors           = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1879
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1880
	foreach ( $args['oneOf'] as $index => $schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1881
		if ( ! isset( $schema['type'] ) && isset( $args['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1882
			$schema['type'] = $args['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1883
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1884
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1885
		$is_valid = rest_validate_value_from_schema( $value, $schema, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1886
		if ( ! is_wp_error( $is_valid ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1887
			if ( $stop_after_first_match ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1888
				return $schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1889
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1890
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1891
			$matching_schemas[] = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1892
				'schema_object' => $schema,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1893
				'index'         => $index,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1894
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1895
		} else {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1896
			$errors[] = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1897
				'error_object' => $is_valid,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1898
				'schema'       => $schema,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1899
				'index'        => $index,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1900
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1901
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1902
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1903
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1904
	if ( ! $matching_schemas ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1905
		return rest_get_combining_operation_error( $value, $param, $errors );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1906
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1907
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1908
	if ( count( $matching_schemas ) > 1 ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1909
		$schema_positions = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1910
		$schema_titles    = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1911
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1912
		foreach ( $matching_schemas as $schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1913
			$schema_positions[] = $schema['index'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1914
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1915
			if ( isset( $schema['schema_object']['title'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1916
				$schema_titles[] = $schema['schema_object']['title'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1917
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1918
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1919
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1920
		// If each schema has a title, include those titles in the error message.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1921
		if ( count( $schema_titles ) === count( $matching_schemas ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1922
			return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1923
				'rest_one_of_multiple_matches',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1924
				/* translators: 1: Parameter, 2: Schema titles. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1925
				wp_sprintf( __( '%1$s matches %2$l, but should match only one.' ), $param, $schema_titles ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1926
				array( 'positions' => $schema_positions )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1927
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1928
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1929
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1930
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1931
			'rest_one_of_multiple_matches',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1932
			/* translators: %s: Parameter. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1933
			sprintf( __( '%s matches more than one of the expected formats.' ), $param ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1934
			array( 'positions' => $schema_positions )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1935
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1936
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1937
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1938
	return $matching_schemas[0]['schema_object'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1939
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1940
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1941
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1942
 * Checks the equality of two values, following JSON Schema semantics.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1943
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1944
 * Property order is ignored for objects.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1945
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1946
 * Values must have been previously sanitized/coerced to their native types.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1947
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1948
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1949
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1950
 * @param mixed $value1 The first value to check.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1951
 * @param mixed $value2 The second value to check.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1952
 * @return bool True if the values are equal or false otherwise.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1953
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1954
function rest_are_values_equal( $value1, $value2 ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1955
	if ( is_array( $value1 ) && is_array( $value2 ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1956
		if ( count( $value1 ) !== count( $value2 ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1957
			return false;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1958
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1959
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1960
		foreach ( $value1 as $index => $value ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1961
			if ( ! array_key_exists( $index, $value2 ) || ! rest_are_values_equal( $value, $value2[ $index ] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1962
				return false;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1963
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1964
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1965
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1966
		return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1967
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1968
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1969
	if ( is_int( $value1 ) && is_float( $value2 )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1970
		|| is_float( $value1 ) && is_int( $value2 )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1971
	) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1972
		return (float) $value1 === (float) $value2;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1973
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1974
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1975
	return $value1 === $value2;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1976
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1977
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1978
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1979
 * Validates that the given value is a member of the JSON Schema "enum".
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1980
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1981
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1982
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1983
 * @param mixed  $value  The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1984
 * @param array  $args   The schema array to use.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1985
 * @param string $param  The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1986
 * @return true|WP_Error True if the "enum" contains the value or a WP_Error instance otherwise.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1987
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1988
function rest_validate_enum( $value, $args, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1989
	$sanitized_value = rest_sanitize_value_from_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1990
	if ( is_wp_error( $sanitized_value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1991
		return $sanitized_value;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1992
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1993
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1994
	foreach ( $args['enum'] as $enum_value ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1995
		if ( rest_are_values_equal( $sanitized_value, $enum_value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1996
			return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1997
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1998
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  1999
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2000
	$encoded_enum_values = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2001
	foreach ( $args['enum'] as $enum_value ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2002
		$encoded_enum_values[] = is_scalar( $enum_value ) ? $enum_value : wp_json_encode( $enum_value );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2003
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2004
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2005
	if ( count( $encoded_enum_values ) === 1 ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2006
		/* translators: 1: Parameter, 2: Valid values. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2007
		return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not %2$s.' ), $param, $encoded_enum_values[0] ) );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2008
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2009
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2010
	/* translators: 1: Parameter, 2: List of valid values. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2011
	return new WP_Error( 'rest_not_in_enum', wp_sprintf( __( '%1$s is not one of %2$l.' ), $param, $encoded_enum_values ) );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2012
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2013
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2014
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2015
 * Get all valid JSON schema properties.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2016
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2017
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2018
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2019
 * @return string[] All valid JSON schema properties.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2020
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2021
function rest_get_allowed_schema_keywords() {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2022
	return array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2023
		'title',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2024
		'description',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2025
		'default',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2026
		'type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2027
		'format',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2028
		'enum',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2029
		'items',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2030
		'properties',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2031
		'additionalProperties',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2032
		'patternProperties',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2033
		'minProperties',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2034
		'maxProperties',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2035
		'minimum',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2036
		'maximum',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2037
		'exclusiveMinimum',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2038
		'exclusiveMaximum',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2039
		'multipleOf',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2040
		'minLength',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2041
		'maxLength',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2042
		'pattern',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2043
		'minItems',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2044
		'maxItems',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2045
		'uniqueItems',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2046
		'anyOf',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2047
		'oneOf',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2048
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2049
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2050
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2051
/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2052
 * Validate a value based on a schema.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2053
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
 * @since 4.7.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2055
 * @since 4.9.0 Support the "object" type.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2056
 * @since 5.2.0 Support validating "additionalProperties" against a schema.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2057
 * @since 5.3.0 Support multiple types.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2058
 * @since 5.4.0 Convert an empty string to an empty object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2059
 * @since 5.5.0 Add the "uuid" and "hex-color" formats.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2060
 *              Support the "minLength", "maxLength" and "pattern" keywords for strings.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2061
 *              Support the "minItems", "maxItems" and "uniqueItems" keywords for arrays.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2062
 *              Validate required properties.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2063
 * @since 5.6.0 Support the "minProperties" and "maxProperties" keywords for objects.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2064
 *              Support the "multipleOf" keyword for numbers and integers.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2065
 *              Support the "patternProperties" keyword for objects.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2066
 *              Support the "anyOf" and "oneOf" keywords.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
 * @param mixed  $value The value to validate.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
 * @param array  $args  Schema array to use for validation.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
 * @param string $param The parameter name, used in error messages.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
 * @return true|WP_Error
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
function rest_validate_value_from_schema( $value, $args, $param = '' ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2074
	if ( isset( $args['anyOf'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2075
		$matching_schema = rest_find_any_matching_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2076
		if ( is_wp_error( $matching_schema ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2077
			return $matching_schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2078
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2079
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2080
		if ( ! isset( $args['type'] ) && isset( $matching_schema['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2081
			$args['type'] = $matching_schema['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2082
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2083
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2084
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2085
	if ( isset( $args['oneOf'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2086
		$matching_schema = rest_find_one_matching_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2087
		if ( is_wp_error( $matching_schema ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2088
			return $matching_schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2089
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2090
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2091
		if ( ! isset( $args['type'] ) && isset( $matching_schema['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2092
			$args['type'] = $matching_schema['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2093
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2094
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2095
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2096
	$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2097
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2098
	if ( ! isset( $args['type'] ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2099
		/* translators: %s: Parameter. */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2100
		_doing_it_wrong( __FUNCTION__, sprintf( __( 'The "type" schema keyword for %s is required.' ), $param ), '5.5.0' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2101
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2102
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2103
	if ( is_array( $args['type'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2104
		$best_type = rest_handle_multi_type_schema( $value, $args, $param );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2105
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2106
		if ( ! $best_type ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2107
			return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2108
				'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2109
				/* translators: 1: Parameter, 2: List of types. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2110
				sprintf( __( '%1$s is not of type %2$s.' ), $param, implode( ',', $args['type'] ) ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2111
				array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2112
			);
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2113
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2114
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2115
		$args['type'] = $best_type;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2116
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2117
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2118
	if ( ! in_array( $args['type'], $allowed_types, true ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2119
		_doing_it_wrong(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2120
			__FUNCTION__,
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2121
			/* translators: 1: Parameter, 2: The list of allowed types. */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2122
			wp_sprintf( __( 'The "type" schema keyword for %1$s can only be one of the built-in types: %2$l.' ), $param, $allowed_types ),
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2123
			'5.5.0'
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2124
		);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2125
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2126
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2127
	switch ( $args['type'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2128
		case 'null':
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2129
			$is_valid = rest_validate_null_value_from_schema( $value, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2130
			break;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2131
		case 'boolean':
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2132
			$is_valid = rest_validate_boolean_value_from_schema( $value, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2133
			break;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2134
		case 'object':
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2135
			$is_valid = rest_validate_object_value_from_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2136
			break;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2137
		case 'array':
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2138
			$is_valid = rest_validate_array_value_from_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2139
			break;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2140
		case 'number':
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2141
			$is_valid = rest_validate_number_value_from_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2142
			break;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2143
		case 'string':
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2144
			$is_valid = rest_validate_string_value_from_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2145
			break;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2146
		case 'integer':
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2147
			$is_valid = rest_validate_integer_value_from_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2148
			break;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2149
		default:
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2150
			$is_valid = true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2151
			break;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2152
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2153
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2154
	if ( is_wp_error( $is_valid ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2155
		return $is_valid;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2156
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2157
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2158
	if ( ! empty( $args['enum'] ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2159
		$enum_contains_value = rest_validate_enum( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2160
		if ( is_wp_error( $enum_contains_value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2161
			return $enum_contains_value;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2162
		}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2163
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2164
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2165
	// The "format" keyword should only be applied to strings. However, for backward compatibility,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2166
	// we allow the "format" keyword if the type keyword was not specified, or was set to an invalid value.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2167
	if ( isset( $args['format'] )
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2168
		&& ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) )
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2169
	) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2170
		switch ( $args['format'] ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2171
			case 'hex-color':
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2172
				if ( ! rest_parse_hex_color( $value ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2173
					return new WP_Error( 'rest_invalid_hex_color', __( 'Invalid hex color.' ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2174
				}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2175
				break;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2176
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2177
			case 'date-time':
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2178
				if ( ! rest_parse_date( $value ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2179
					return new WP_Error( 'rest_invalid_date', __( 'Invalid date.' ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2180
				}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2181
				break;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2182
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2183
			case 'email':
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2184
				if ( ! is_email( $value ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2185
					return new WP_Error( 'rest_invalid_email', __( 'Invalid email address.' ) );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2186
				}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2187
				break;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2188
			case 'ip':
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2189
				if ( ! rest_is_ip_address( $value ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2190
					/* translators: %s: IP address. */
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2191
					return new WP_Error( 'rest_invalid_ip', sprintf( __( '%s is not a valid IP address.' ), $param ) );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2192
				}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2193
				break;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2194
			case 'uuid':
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2195
				if ( ! wp_is_uuid( $value ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2196
					/* translators: %s: The name of a JSON field expecting a valid UUID. */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2197
					return new WP_Error( 'rest_invalid_uuid', sprintf( __( '%s is not a valid UUID.' ), $param ) );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2198
				}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2199
				break;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2200
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2201
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2202
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2203
	return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2204
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2205
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2206
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2207
 * Validates a null value based on a schema.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2208
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2209
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2210
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2211
 * @param mixed  $value The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2212
 * @param string $param The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2213
 * @return true|WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2214
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2215
function rest_validate_null_value_from_schema( $value, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2216
	if ( null !== $value ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2217
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2218
			'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2219
			/* translators: 1: Parameter, 2: Type name. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2220
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'null' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2221
			array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2222
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2223
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2224
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2225
	return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2226
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2227
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2228
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2229
 * Validates a boolean value based on a schema.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2230
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2231
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2232
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2233
 * @param mixed  $value The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2234
 * @param string $param The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2235
 * @return true|WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2236
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2237
function rest_validate_boolean_value_from_schema( $value, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2238
	if ( ! rest_is_boolean( $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2239
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2240
			'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2241
			/* translators: 1: Parameter, 2: Type name. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2242
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'boolean' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2243
			array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2244
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2245
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2246
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2247
	return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2248
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2249
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2250
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2251
 * Validates an object value based on a schema.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2252
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2253
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2254
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2255
 * @param mixed  $value The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2256
 * @param array  $args  Schema array to use for validation.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2257
 * @param string $param The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2258
 * @return true|WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2259
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2260
function rest_validate_object_value_from_schema( $value, $args, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2261
	if ( ! rest_is_object( $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2262
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2263
			'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2264
			/* translators: 1: Parameter, 2: Type name. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2265
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'object' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2266
			array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2267
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2268
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2269
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2270
	$value = rest_sanitize_object( $value );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2271
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2272
	if ( isset( $args['required'] ) && is_array( $args['required'] ) ) { // schema version 4
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2273
		foreach ( $args['required'] as $name ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2274
			if ( ! array_key_exists( $name, $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2275
				return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2276
					'rest_property_required',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2277
					/* translators: 1: Property of an object, 2: Parameter. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2278
					sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2279
				);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2280
			}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2281
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2282
	} elseif ( isset( $args['properties'] ) ) { // schema version 3
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2283
		foreach ( $args['properties'] as $name => $property ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2284
			if ( isset( $property['required'] ) && true === $property['required'] && ! array_key_exists( $name, $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2285
				return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2286
					'rest_property_required',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2287
					/* translators: 1: Property of an object, 2: Parameter. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2288
					sprintf( __( '%1$s is a required property of %2$s.' ), $name, $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2289
				);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2290
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2291
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2292
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2293
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2294
	foreach ( $value as $property => $v ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2295
		if ( isset( $args['properties'][ $property ] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2296
			$is_valid = rest_validate_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2297
			if ( is_wp_error( $is_valid ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2298
				return $is_valid;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2299
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2300
			continue;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2301
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2302
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2303
		$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2304
		if ( null !== $pattern_property_schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2305
			$is_valid = rest_validate_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2306
			if ( is_wp_error( $is_valid ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2307
				return $is_valid;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2308
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2309
			continue;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2310
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2311
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2312
		if ( isset( $args['additionalProperties'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2313
			if ( false === $args['additionalProperties'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2314
				return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2315
					'rest_additional_properties_forbidden',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2316
					/* translators: %s: Property of an object. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2317
					sprintf( __( '%1$s is not a valid property of Object.' ), $property )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2318
				);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2319
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2320
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2321
			if ( is_array( $args['additionalProperties'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2322
				$is_valid = rest_validate_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2323
				if ( is_wp_error( $is_valid ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2324
					return $is_valid;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2325
				}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2326
			}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2327
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2328
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2329
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2330
	if ( isset( $args['minProperties'] ) && count( $value ) < $args['minProperties'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2331
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2332
			'rest_too_few_properties',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2333
			sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2334
				/* translators: 1: Parameter, 2: Number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2335
				_n(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2336
					'%1$s must contain at least %2$s property.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2337
					'%1$s must contain at least %2$s properties.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2338
					$args['minProperties']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2339
				),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2340
				$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2341
				number_format_i18n( $args['minProperties'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2342
			)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2343
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2344
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2345
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2346
	if ( isset( $args['maxProperties'] ) && count( $value ) > $args['maxProperties'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2347
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2348
			'rest_too_many_properties',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2349
			sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2350
				/* translators: 1: Parameter, 2: Number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2351
				_n(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2352
					'%1$s must contain at most %2$s property.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2353
					'%1$s must contain at most %2$s properties.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2354
					$args['maxProperties']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2355
				),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2356
				$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2357
				number_format_i18n( $args['maxProperties'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2358
			)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2359
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2360
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2361
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2362
	return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2363
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2364
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2365
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2366
 * Validates an array value based on a schema.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2367
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2368
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2369
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2370
 * @param mixed  $value The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2371
 * @param array  $args  Schema array to use for validation.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2372
 * @param string $param The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2373
 * @return true|WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2374
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2375
function rest_validate_array_value_from_schema( $value, $args, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2376
	if ( ! rest_is_array( $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2377
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2378
			'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2379
			/* translators: 1: Parameter, 2: Type name. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2380
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'array' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2381
			array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2382
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2383
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2384
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2385
	$value = rest_sanitize_array( $value );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2386
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2387
	if ( isset( $args['items'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2388
		foreach ( $value as $index => $v ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2389
			$is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2390
			if ( is_wp_error( $is_valid ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2391
				return $is_valid;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2392
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2393
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2394
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2395
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2396
	if ( isset( $args['minItems'] ) && count( $value ) < $args['minItems'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2397
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2398
			'rest_too_few_items',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2399
			sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2400
				/* translators: 1: Parameter, 2: Number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2401
				_n(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2402
					'%1$s must contain at least %2$s item.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2403
					'%1$s must contain at least %2$s items.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2404
					$args['minItems']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2405
				),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2406
				$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2407
				number_format_i18n( $args['minItems'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2408
			)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2409
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2410
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2411
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2412
	if ( isset( $args['maxItems'] ) && count( $value ) > $args['maxItems'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2413
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2414
			'rest_too_many_items',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2415
			sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2416
				/* translators: 1: Parameter, 2: Number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2417
				_n(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2418
					'%1$s must contain at most %2$s item.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2419
					'%1$s must contain at most %2$s items.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2420
					$args['maxItems']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2421
				),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2422
				$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2423
				number_format_i18n( $args['maxItems'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2424
			)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2425
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2426
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2427
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2428
	if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2429
		/* translators: %s: Parameter. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2430
		return new WP_Error( 'rest_duplicate_items', sprintf( __( '%s has duplicate items.' ), $param ) );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2431
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2432
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2433
	return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2434
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2435
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2436
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2437
 * Validates a number value based on a schema.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2438
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2439
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2440
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2441
 * @param mixed  $value The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2442
 * @param array  $args  Schema array to use for validation.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2443
 * @param string $param The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2444
 * @return true|WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2445
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2446
function rest_validate_number_value_from_schema( $value, $args, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2447
	if ( ! is_numeric( $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2448
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2449
			'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2450
			/* translators: 1: Parameter, 2: Type name. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2451
			sprintf( __( '%1$s is not of type %2$s.' ), $param, $args['type'] ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2452
			array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2453
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2454
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2455
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2456
	if ( isset( $args['multipleOf'] ) && fmod( $value, $args['multipleOf'] ) !== 0.0 ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2457
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2458
			'rest_invalid_multiple',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2459
			/* translators: 1: Parameter, 2: Multiplier. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2460
			sprintf( __( '%1$s must be a multiple of %2$s.' ), $param, $args['multipleOf'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2461
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2462
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2463
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2464
	if ( isset( $args['minimum'] ) && ! isset( $args['maximum'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2465
		if ( ! empty( $args['exclusiveMinimum'] ) && $value <= $args['minimum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2466
			return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2467
				'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2468
				/* translators: 1: Parameter, 2: Minimum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2469
				sprintf( __( '%1$s must be greater than %2$d' ), $param, $args['minimum'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2470
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2471
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2472
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2473
		if ( empty( $args['exclusiveMinimum'] ) && $value < $args['minimum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2474
			return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2475
				'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2476
				/* translators: 1: Parameter, 2: Minimum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2477
				sprintf( __( '%1$s must be greater than or equal to %2$d' ), $param, $args['minimum'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2478
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2479
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2480
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2481
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2482
	if ( isset( $args['maximum'] ) && ! isset( $args['minimum'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2483
		if ( ! empty( $args['exclusiveMaximum'] ) && $value >= $args['maximum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2484
			return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2485
				'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2486
				/* translators: 1: Parameter, 2: Maximum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2487
				sprintf( __( '%1$s must be less than %2$d' ), $param, $args['maximum'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2488
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2489
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2490
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2491
		if ( empty( $args['exclusiveMaximum'] ) && $value > $args['maximum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2492
			return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2493
				'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2494
				/* translators: 1: Parameter, 2: Maximum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2495
				sprintf( __( '%1$s must be less than or equal to %2$d' ), $param, $args['maximum'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2496
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2497
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2498
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2499
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2500
	if ( isset( $args['minimum'], $args['maximum'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2501
		if ( ! empty( $args['exclusiveMinimum'] ) && ! empty( $args['exclusiveMaximum'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2502
			if ( $value >= $args['maximum'] || $value <= $args['minimum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2503
				return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2504
					'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2505
					sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2506
						/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2507
						__( '%1$s must be between %2$d (exclusive) and %3$d (exclusive)' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2508
						$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2509
						$args['minimum'],
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2510
						$args['maximum']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2511
					)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2512
				);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2513
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2514
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2515
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2516
		if ( ! empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2517
			if ( $value > $args['maximum'] || $value <= $args['minimum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2518
				return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2519
					'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2520
					sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2521
						/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2522
						__( '%1$s must be between %2$d (exclusive) and %3$d (inclusive)' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2523
						$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2524
						$args['minimum'],
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2525
						$args['maximum']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2526
					)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2527
				);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2528
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2529
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2530
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2531
		if ( ! empty( $args['exclusiveMaximum'] ) && empty( $args['exclusiveMinimum'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2532
			if ( $value >= $args['maximum'] || $value < $args['minimum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2533
				return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2534
					'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2535
					sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2536
						/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2537
						__( '%1$s must be between %2$d (inclusive) and %3$d (exclusive)' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2538
						$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2539
						$args['minimum'],
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2540
						$args['maximum']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2541
					)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2542
				);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2543
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2544
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2545
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2546
		if ( empty( $args['exclusiveMinimum'] ) && empty( $args['exclusiveMaximum'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2547
			if ( $value > $args['maximum'] || $value < $args['minimum'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2548
				return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2549
					'rest_out_of_bounds',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2550
					sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2551
						/* translators: 1: Parameter, 2: Minimum number, 3: Maximum number. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2552
						__( '%1$s must be between %2$d (inclusive) and %3$d (inclusive)' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2553
						$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2554
						$args['minimum'],
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2555
						$args['maximum']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2556
					)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2557
				);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2558
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2559
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2560
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2561
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2562
	return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2563
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2564
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2565
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2566
 * Validates a string value based on a schema.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2567
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2568
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2569
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2570
 * @param mixed  $value The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2571
 * @param array  $args  Schema array to use for validation.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2572
 * @param string $param The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2573
 * @return true|WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2574
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2575
function rest_validate_string_value_from_schema( $value, $args, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2576
	if ( ! is_string( $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2577
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2578
			'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2579
			/* translators: 1: Parameter, 2: Type name. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2580
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'string' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2581
			array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2582
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2583
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2584
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2585
	if ( isset( $args['minLength'] ) && mb_strlen( $value ) < $args['minLength'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2586
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2587
			'rest_too_short',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2588
			sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2589
				/* translators: 1: Parameter, 2: Number of characters. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2590
				_n(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2591
					'%1$s must be at least %2$s character long.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2592
					'%1$s must be at least %2$s characters long.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2593
					$args['minLength']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2594
				),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2595
				$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2596
				number_format_i18n( $args['minLength'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2597
			)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2598
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2599
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2600
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2601
	if ( isset( $args['maxLength'] ) && mb_strlen( $value ) > $args['maxLength'] ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2602
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2603
			'rest_too_long',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2604
			sprintf(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2605
				/* translators: 1: Parameter, 2: Number of characters. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2606
				_n(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2607
					'%1$s must be at most %2$s character long.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2608
					'%1$s must be at most %2$s characters long.',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2609
					$args['maxLength']
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2610
				),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2611
				$param,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2612
				number_format_i18n( $args['maxLength'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2613
			)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2614
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2615
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2616
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2617
	if ( isset( $args['pattern'] ) && ! rest_validate_json_schema_pattern( $args['pattern'], $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2618
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2619
			'rest_invalid_pattern',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2620
			/* translators: 1: Parameter, 2: Pattern. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2621
			sprintf( __( '%1$s does not match pattern %2$s.' ), $param, $args['pattern'] )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2622
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2623
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2624
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2625
	return true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2626
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2627
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2628
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2629
 * Validates an integer value based on a schema.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2630
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2631
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2632
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2633
 * @param mixed  $value The value to validate.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2634
 * @param array  $args  Schema array to use for validation.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2635
 * @param string $param The parameter name, used in error messages.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2636
 * @return true|WP_Error
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2637
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2638
function rest_validate_integer_value_from_schema( $value, $args, $param ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2639
	$is_valid_number = rest_validate_number_value_from_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2640
	if ( is_wp_error( $is_valid_number ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2641
		return $is_valid_number;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2642
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2643
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2644
	if ( ! rest_is_integer( $value ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2645
		return new WP_Error(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2646
			'rest_invalid_type',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2647
			/* translators: 1: Parameter, 2: Type name. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2648
			sprintf( __( '%1$s is not of type %2$s.' ), $param, 'integer' ),
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2649
			array( 'param' => $param )
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2650
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2651
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2652
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2653
	return true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2654
}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2655
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2656
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2657
 * Sanitize a value based on a schema.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2658
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2659
 * @since 4.7.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2660
 * @since 5.5.0 Added the `$param` parameter.
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2661
 * @since 5.6.0 Support the "anyOf" and "oneOf" keywords.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2662
 * @since 5.9.0 Added `text-field` and `textarea-field` formats.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2663
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2664
 * @param mixed  $value The value to sanitize.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2665
 * @param array  $args  Schema array to use for sanitization.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2666
 * @param string $param The parameter name, used in error messages.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2667
 * @return mixed|WP_Error The sanitized value or a WP_Error instance if the value cannot be safely sanitized.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2668
 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2669
function rest_sanitize_value_from_schema( $value, $args, $param = '' ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2670
	if ( isset( $args['anyOf'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2671
		$matching_schema = rest_find_any_matching_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2672
		if ( is_wp_error( $matching_schema ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2673
			return $matching_schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2674
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2675
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2676
		if ( ! isset( $args['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2677
			$args['type'] = $matching_schema['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2678
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2679
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2680
		$value = rest_sanitize_value_from_schema( $value, $matching_schema, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2681
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2682
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2683
	if ( isset( $args['oneOf'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2684
		$matching_schema = rest_find_one_matching_schema( $value, $args, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2685
		if ( is_wp_error( $matching_schema ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2686
			return $matching_schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2687
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2688
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2689
		if ( ! isset( $args['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2690
			$args['type'] = $matching_schema['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2691
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2692
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2693
		$value = rest_sanitize_value_from_schema( $value, $matching_schema, $param );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2694
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2695
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2696
	$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2697
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2698
	if ( ! isset( $args['type'] ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2699
		/* translators: %s: Parameter. */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2700
		_doing_it_wrong( __FUNCTION__, sprintf( __( 'The "type" schema keyword for %s is required.' ), $param ), '5.5.0' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2701
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2702
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2703
	if ( is_array( $args['type'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2704
		$best_type = rest_handle_multi_type_schema( $value, $args, $param );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2705
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2706
		if ( ! $best_type ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2707
			return null;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2708
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2709
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2710
		$args['type'] = $best_type;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2711
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2712
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2713
	if ( ! in_array( $args['type'], $allowed_types, true ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2714
		_doing_it_wrong(
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2715
			__FUNCTION__,
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2716
			/* translators: 1: Parameter, 2: The list of allowed types. */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2717
			wp_sprintf( __( 'The "type" schema keyword for %1$s can only be one of the built-in types: %2$l.' ), $param, $allowed_types ),
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2718
			'5.5.0'
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2719
		);
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2720
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2721
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2722
	if ( 'array' === $args['type'] ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2723
		$value = rest_sanitize_array( $value );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2724
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2725
		if ( ! empty( $args['items'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2726
			foreach ( $value as $index => $v ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2727
				$value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2728
			}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2729
		}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2730
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2731
		if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2732
			/* translators: %s: Parameter. */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2733
			return new WP_Error( 'rest_duplicate_items', sprintf( __( '%s has duplicate items.' ), $param ) );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2734
		}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2735
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2736
		return $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2737
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2738
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2739
	if ( 'object' === $args['type'] ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2740
		$value = rest_sanitize_object( $value );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2741
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2742
		foreach ( $value as $property => $v ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2743
			if ( isset( $args['properties'][ $property ] ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2744
				$value[ $property ] = rest_sanitize_value_from_schema( $v, $args['properties'][ $property ], $param . '[' . $property . ']' );
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2745
				continue;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2746
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2747
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2748
			$pattern_property_schema = rest_find_matching_pattern_property_schema( $property, $args );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2749
			if ( null !== $pattern_property_schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2750
				$value[ $property ] = rest_sanitize_value_from_schema( $v, $pattern_property_schema, $param . '[' . $property . ']' );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2751
				continue;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2752
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2753
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2754
			if ( isset( $args['additionalProperties'] ) ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2755
				if ( false === $args['additionalProperties'] ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2756
					unset( $value[ $property ] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2757
				} elseif ( is_array( $args['additionalProperties'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2758
					$value[ $property ] = rest_sanitize_value_from_schema( $v, $args['additionalProperties'], $param . '[' . $property . ']' );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2759
				}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2760
			}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2761
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2762
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2763
		return $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2764
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2765
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2766
	if ( 'null' === $args['type'] ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2767
		return null;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2768
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2769
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2770
	if ( 'integer' === $args['type'] ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2771
		return (int) $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2772
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2773
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2774
	if ( 'number' === $args['type'] ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2775
		return (float) $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2776
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2777
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2778
	if ( 'boolean' === $args['type'] ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2779
		return rest_sanitize_boolean( $value );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2780
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2781
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2782
	// This behavior matches rest_validate_value_from_schema().
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2783
	if ( isset( $args['format'] )
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2784
		&& ( ! isset( $args['type'] ) || 'string' === $args['type'] || ! in_array( $args['type'], $allowed_types, true ) )
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2785
	) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2786
		switch ( $args['format'] ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2787
			case 'hex-color':
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2788
				return (string) sanitize_hex_color( $value );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2789
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2790
			case 'date-time':
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2791
				return sanitize_text_field( $value );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2792
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2793
			case 'email':
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2794
				// sanitize_email() validates, which would be unexpected.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2795
				return sanitize_text_field( $value );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2796
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2797
			case 'uri':
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2798
				return esc_url_raw( $value );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2799
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2800
			case 'ip':
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2801
				return sanitize_text_field( $value );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2802
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2803
			case 'uuid':
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2804
				return sanitize_text_field( $value );
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2805
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2806
			case 'text-field':
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2807
				return sanitize_text_field( $value );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2808
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2809
			case 'textarea-field':
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2810
				return sanitize_textarea_field( $value );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2811
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2812
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2813
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2814
	if ( 'string' === $args['type'] ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2815
		return (string) $value;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2816
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2817
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2818
	return $value;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2819
}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2820
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2821
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2822
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2823
 * Expected to be called in the context of `array_reduce`.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2824
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2825
 * @since 5.0.0
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2826
 *
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2827
 * @param array  $memo Reduce accumulator.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2828
 * @param string $path REST API path to preload.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2829
 * @return array Modified reduce accumulator.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2830
 */
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2831
function rest_preload_api_request( $memo, $path ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2832
	// array_reduce() doesn't support passing an array in PHP 5.2,
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2833
	// so we need to make sure we start with one.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2834
	if ( ! is_array( $memo ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2835
		$memo = array();
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2836
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2837
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2838
	if ( empty( $path ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2839
		return $memo;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2840
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2841
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2842
	$method = 'GET';
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2843
	if ( is_array( $path ) && 2 === count( $path ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2844
		$method = end( $path );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2845
		$path   = reset( $path );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2846
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2847
		if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2848
			$method = 'GET';
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2849
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2850
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2851
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2852
	$path = untrailingslashit( $path );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2853
	if ( empty( $path ) ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2854
		$path = '/';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2855
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2856
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2857
	$path_parts = parse_url( $path );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2858
	if ( false === $path_parts ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2859
		return $memo;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2860
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2861
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2862
	$request = new WP_REST_Request( $method, $path_parts['path'] );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2863
	if ( ! empty( $path_parts['query'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2864
		parse_str( $path_parts['query'], $query_params );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2865
		$request->set_query_params( $query_params );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2866
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2867
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2868
	$response = rest_do_request( $request );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2869
	if ( 200 === $response->status ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2870
		$server = rest_get_server();
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2871
		/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  2872
		$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $server, $request );
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2873
		$embed  = $request->has_param( '_embed' ) ? rest_parse_embed_param( $request['_embed'] ) : false;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2874
		$data   = (array) $server->response_to_data( $response, $embed );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2875
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2876
		if ( 'OPTIONS' === $method ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2877
			$memo[ $method ][ $path ] = array(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2878
				'body'    => $data,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2879
				'headers' => $response->headers,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2880
			);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2881
		} else {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2882
			$memo[ $path ] = array(
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2883
				'body'    => $data,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2884
				'headers' => $response->headers,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2885
			);
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2886
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2887
	}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2888
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2889
	return $memo;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
  2890
}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2891
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2892
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2893
 * Parses the "_embed" parameter into the list of resources to embed.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2894
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2895
 * @since 5.4.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2896
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2897
 * @param string|array $embed Raw "_embed" parameter value.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2898
 * @return true|string[] Either true to embed all embeds, or a list of relations to embed.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2899
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2900
function rest_parse_embed_param( $embed ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2901
	if ( ! $embed || 'true' === $embed || '1' === $embed ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2902
		return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2903
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2904
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2905
	$rels = wp_parse_list( $embed );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2906
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2907
	if ( ! $rels ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2908
		return true;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2909
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2910
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2911
	return $rels;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2912
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2913
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2914
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2915
 * Filters the response to remove any fields not available in the given context.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2916
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2917
 * @since 5.5.0
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2918
 * @since 5.6.0 Support the "patternProperties" keyword for objects.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2919
 *              Support the "anyOf" and "oneOf" keywords.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2920
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2921
 * @param array|object $data    The response data to modify.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2922
 * @param array        $schema  The schema for the endpoint used to filter the response.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2923
 * @param string       $context The requested context.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2924
 * @return array|object The filtered response data.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2925
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2926
function rest_filter_response_by_context( $data, $schema, $context ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2927
	if ( isset( $schema['anyOf'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2928
		$matching_schema = rest_find_any_matching_schema( $data, $schema, '' );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2929
		if ( ! is_wp_error( $matching_schema ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2930
			if ( ! isset( $schema['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2931
				$schema['type'] = $matching_schema['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2932
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2933
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2934
			$data = rest_filter_response_by_context( $data, $matching_schema, $context );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2935
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2936
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2937
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2938
	if ( isset( $schema['oneOf'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2939
		$matching_schema = rest_find_one_matching_schema( $data, $schema, '', true );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2940
		if ( ! is_wp_error( $matching_schema ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2941
			if ( ! isset( $schema['type'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2942
				$schema['type'] = $matching_schema['type'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2943
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2944
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2945
			$data = rest_filter_response_by_context( $data, $matching_schema, $context );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2946
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2947
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2948
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2949
	if ( ! is_array( $data ) && ! is_object( $data ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2950
		return $data;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2951
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2952
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2953
	if ( isset( $schema['type'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2954
		$type = $schema['type'];
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2955
	} elseif ( isset( $schema['properties'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2956
		$type = 'object'; // Back compat if a developer accidentally omitted the type.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2957
	} else {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2958
		return $data;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2959
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2960
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2961
	$is_array_type  = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2962
	$is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2963
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2964
	if ( $is_array_type && $is_object_type ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2965
		if ( rest_is_array( $data ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2966
			$is_object_type = false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2967
		} else {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2968
			$is_array_type = false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2969
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2970
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2971
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2972
	$has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2973
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2974
	foreach ( $data as $key => $value ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2975
		$check = array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2976
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2977
		if ( $is_array_type ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2978
			$check = isset( $schema['items'] ) ? $schema['items'] : array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2979
		} elseif ( $is_object_type ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2980
			if ( isset( $schema['properties'][ $key ] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2981
				$check = $schema['properties'][ $key ];
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2982
			} else {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2983
				$pattern_property_schema = rest_find_matching_pattern_property_schema( $key, $schema );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2984
				if ( null !== $pattern_property_schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2985
					$check = $pattern_property_schema;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2986
				} elseif ( $has_additional_properties ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2987
					$check = $schema['additionalProperties'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  2988
				}
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2989
			}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2990
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2991
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2992
		if ( ! isset( $check['context'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2993
			continue;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2994
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2995
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2996
		if ( ! in_array( $context, $check['context'], true ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2997
			if ( $is_array_type ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2998
				// All array items share schema, so there's no need to check each one.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  2999
				$data = array();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3000
				break;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3001
			}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3002
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3003
			if ( is_object( $data ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3004
				unset( $data->$key );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3005
			} else {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3006
				unset( $data[ $key ] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3007
			}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3008
		} elseif ( is_array( $value ) || is_object( $value ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3009
			$new_value = rest_filter_response_by_context( $value, $check, $context );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3010
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3011
			if ( is_object( $data ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3012
				$data->$key = $new_value;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3013
			} else {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3014
				$data[ $key ] = $new_value;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3015
			}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3016
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3017
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3018
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3019
	return $data;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3020
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3021
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3022
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3023
 * Sets the "additionalProperties" to false by default for all object definitions in the schema.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3024
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3025
 * @since 5.5.0
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3026
 * @since 5.6.0 Support the "patternProperties" keyword.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3027
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3028
 * @param array $schema The schema to modify.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3029
 * @return array The modified schema.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3030
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3031
function rest_default_additional_properties_to_false( $schema ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3032
	$type = (array) $schema['type'];
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3033
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3034
	if ( in_array( 'object', $type, true ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3035
		if ( isset( $schema['properties'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3036
			foreach ( $schema['properties'] as $key => $child_schema ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3037
				$schema['properties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3038
			}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3039
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3040
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3041
		if ( isset( $schema['patternProperties'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3042
			foreach ( $schema['patternProperties'] as $key => $child_schema ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3043
				$schema['patternProperties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3044
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3045
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3046
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3047
		if ( ! isset( $schema['additionalProperties'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3048
			$schema['additionalProperties'] = false;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3049
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3050
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3051
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3052
	if ( in_array( 'array', $type, true ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3053
		if ( isset( $schema['items'] ) ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3054
			$schema['items'] = rest_default_additional_properties_to_false( $schema['items'] );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3055
		}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3056
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3057
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3058
	return $schema;
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3059
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3060
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3061
/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3062
 * Gets the REST API route for a post.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3063
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3064
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3065
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3066
 * @param int|WP_Post $post Post ID or post object.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3067
 * @return string The route path with a leading slash for the given post,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3068
 *                or an empty string if there is not a route.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3069
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3070
function rest_get_route_for_post( $post ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3071
	$post = get_post( $post );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3072
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3073
	if ( ! $post instanceof WP_Post ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3074
		return '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3075
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3076
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3077
	$post_type_route = rest_get_route_for_post_type_items( $post->post_type );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3078
	if ( ! $post_type_route ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3079
		return '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3080
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3081
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3082
	$route = sprintf( '%s/%d', $post_type_route, $post->ID );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3083
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3084
	/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3085
	 * Filters the REST API route for a post.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3086
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3087
	 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3088
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3089
	 * @param string  $route The route path.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3090
	 * @param WP_Post $post  The post object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3091
	 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3092
	return apply_filters( 'rest_route_for_post', $route, $post );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3093
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3094
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3095
/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3096
 * Gets the REST API route for a post type.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3097
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3098
 * @since 5.9.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3099
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3100
 * @param string $post_type The name of a registered post type.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3101
 * @return string The route path with a leading slash for the given post type,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3102
 *                or an empty string if there is not a route.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3103
 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3104
function rest_get_route_for_post_type_items( $post_type ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3105
	$post_type = get_post_type_object( $post_type );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3106
	if ( ! $post_type ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3107
		return '';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3108
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3109
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3110
	if ( ! $post_type->show_in_rest ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3111
		return '';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3112
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3113
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3114
	$namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3115
	$rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3116
	$route     = sprintf( '/%s/%s', $namespace, $rest_base );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3117
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3118
	/**
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3119
	 * Filters the REST API route for a post type.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3120
	 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3121
	 * @since 5.9.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3122
	 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3123
	 * @param string       $route      The route path.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3124
	 * @param WP_Post_Type $post_type  The post type object.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3125
	 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3126
	return apply_filters( 'rest_route_for_post_type_items', $route, $post_type );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3127
}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3128
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3129
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3130
 * Gets the REST API route for a term.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3131
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3132
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3133
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3134
 * @param int|WP_Term $term Term ID or term object.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3135
 * @return string The route path with a leading slash for the given term,
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3136
 *                or an empty string if there is not a route.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3137
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3138
function rest_get_route_for_term( $term ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3139
	$term = get_term( $term );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3140
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3141
	if ( ! $term instanceof WP_Term ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3142
		return '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3143
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3144
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3145
	$taxonomy_route = rest_get_route_for_taxonomy_items( $term->taxonomy );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3146
	if ( ! $taxonomy_route ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3147
		return '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3148
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3149
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3150
	$route = sprintf( '%s/%d', $taxonomy_route, $term->term_id );
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3151
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3152
	/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3153
	 * Filters the REST API route for a term.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3154
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3155
	 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3156
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3157
	 * @param string  $route The route path.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3158
	 * @param WP_Term $term  The term object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3159
	 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3160
	return apply_filters( 'rest_route_for_term', $route, $term );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3161
}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3162
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3163
/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3164
 * Gets the REST API route for a taxonomy.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3165
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3166
 * @since 5.9.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3167
 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3168
 * @param string $taxonomy Name of taxonomy.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3169
 * @return string The route path with a leading slash for the given taxonomy.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3170
 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3171
function rest_get_route_for_taxonomy_items( $taxonomy ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3172
	$taxonomy = get_taxonomy( $taxonomy );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3173
	if ( ! $taxonomy ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3174
		return '';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3175
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3176
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3177
	if ( ! $taxonomy->show_in_rest ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3178
		return '';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3179
	}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3180
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3181
	$namespace = ! empty( $taxonomy->rest_namespace ) ? $taxonomy->rest_namespace : 'wp/v2';
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3182
	$rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3183
	$route     = sprintf( '/%s/%s', $namespace, $rest_base );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3184
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3185
	/**
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3186
	 * Filters the REST API route for a taxonomy.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3187
	 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3188
	 * @since 5.9.0
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3189
	 *
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3190
	 * @param string      $route    The route path.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3191
	 * @param WP_Taxonomy $taxonomy The taxonomy object.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3192
	 */
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3193
	return apply_filters( 'rest_route_for_taxonomy_items', $route, $taxonomy );
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3194
}
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3195
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3196
/**
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3197
 * Gets the REST route for the currently queried object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3198
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3199
 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3200
 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3201
 * @return string The REST route of the resource, or an empty string if no resource identified.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3202
 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3203
function rest_get_queried_resource_route() {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3204
	if ( is_singular() ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3205
		$route = rest_get_route_for_post( get_queried_object() );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3206
	} elseif ( is_category() || is_tag() || is_tax() ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3207
		$route = rest_get_route_for_term( get_queried_object() );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3208
	} elseif ( is_author() ) {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3209
		$route = '/wp/v2/users/' . get_queried_object_id();
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3210
	} else {
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3211
		$route = '';
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3212
	}
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3213
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3214
	/**
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3215
	 * Filters the REST route for the currently queried object.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3216
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3217
	 * @since 5.5.0
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3218
	 *
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3219
	 * @param string $link The route with a leading slash, or an empty string.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3220
	 */
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3221
	return apply_filters( 'rest_queried_resource_route', $route );
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
  3222
}
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3223
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3224
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3225
 * Retrieves an array of endpoint arguments from the item schema and endpoint method.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3226
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3227
 * @since 5.6.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3228
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3229
 * @param array  $schema The full JSON schema for the endpoint.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3230
 * @param string $method Optional. HTTP method of the endpoint. The arguments for `CREATABLE` endpoints are
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3231
 *                       checked for required values and may fall-back to a given default, this is not done
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3232
 *                       on `EDITABLE` endpoints. Default WP_REST_Server::CREATABLE.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3233
 * @return array The endpoint arguments.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3234
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3235
function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::CREATABLE ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3236
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3237
	$schema_properties       = ! empty( $schema['properties'] ) ? $schema['properties'] : array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3238
	$endpoint_args           = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3239
	$valid_schema_properties = rest_get_allowed_schema_keywords();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3240
	$valid_schema_properties = array_diff( $valid_schema_properties, array( 'default', 'required' ) );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3241
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3242
	foreach ( $schema_properties as $field_id => $params ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3243
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3244
		// Arguments specified as `readonly` are not allowed to be set.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3245
		if ( ! empty( $params['readonly'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3246
			continue;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3247
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3248
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3249
		$endpoint_args[ $field_id ] = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3250
			'validate_callback' => 'rest_validate_request_arg',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3251
			'sanitize_callback' => 'rest_sanitize_request_arg',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3252
		);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3253
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3254
		if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3255
			$endpoint_args[ $field_id ]['default'] = $params['default'];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3256
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3257
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3258
		if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3259
			$endpoint_args[ $field_id ]['required'] = true;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3260
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3261
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3262
		foreach ( $valid_schema_properties as $schema_prop ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3263
			if ( isset( $params[ $schema_prop ] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3264
				$endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3265
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3266
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3267
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3268
		// Merge in any options provided by the schema property.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3269
		if ( isset( $params['arg_options'] ) ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3270
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3271
			// Only use required / default from arg_options on CREATABLE endpoints.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3272
			if ( WP_REST_Server::CREATABLE !== $method ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3273
				$params['arg_options'] = array_diff_key(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3274
					$params['arg_options'],
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3275
					array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3276
						'required' => '',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3277
						'default'  => '',
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3278
					)
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3279
				);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3280
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3281
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3282
			$endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3283
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3284
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3285
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3286
	return $endpoint_args;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3287
}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3288
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3289
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3290
/**
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3291
 * Converts an error to a response object.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3292
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3293
 * This iterates over all error codes and messages to change it into a flat
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3294
 * array. This enables simpler client behaviour, as it is represented as a
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3295
 * list in JSON rather than an object/map.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3296
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3297
 * @since 5.7.0
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3298
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3299
 * @param WP_Error $error WP_Error instance.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3300
 *
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3301
 * @return WP_REST_Response List of associative arrays with code and message keys.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3302
 */
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3303
function rest_convert_error_to_response( $error ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3304
	$status = array_reduce(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3305
		$error->get_all_error_data(),
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
  3306
		static function ( $status, $error_data ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3307
			return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3308
		},
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3309
		500
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3310
	);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3311
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3312
	$errors = array();
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3313
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3314
	foreach ( (array) $error->errors as $code => $messages ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3315
		$all_data  = $error->get_all_error_data( $code );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3316
		$last_data = array_pop( $all_data );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3317
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3318
		foreach ( (array) $messages as $message ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3319
			$formatted = array(
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3320
				'code'    => $code,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3321
				'message' => $message,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3322
				'data'    => $last_data,
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3323
			);
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3324
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3325
			if ( $all_data ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3326
				$formatted['additional_data'] = $all_data;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3327
			}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3328
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3329
			$errors[] = $formatted;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3330
		}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3331
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3332
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3333
	$data = $errors[0];
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3334
	if ( count( $errors ) > 1 ) {
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3335
		// Remove the primary error.
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3336
		array_shift( $errors );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3337
		$data['additional_errors'] = $errors;
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3338
	}
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3339
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3340
	return new WP_REST_Response( $data, $status );
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
  3341
}