8889841cREST_Route.php000064400000011510150515170200007202 0ustar00uri = trim( $uri, '/' ); $this->args = $args; if ( isset( $this->args['args'] ) ) { $this->args['args'] = $this->parse_param_args( $this->args['args'] ); } // In case there are string arguments, this is only a single endpoint and needs to be turned into a list. if ( ! wp_is_numeric_array( $endpoints ) ) { $endpoints = array( $endpoints ); } $endpoint_defaults = array( 'methods' => WP_REST_Server::READABLE, 'callback' => null, 'args' => array(), ); foreach ( $endpoints as $endpoint ) { $endpoint = wp_parse_args( $endpoint, $endpoint_defaults ); $endpoint['args'] = $this->parse_param_args( $endpoint['args'] ); if ( ! empty( $this->args['args'] ) ) { $endpoint['args'] = array_merge( $this->args['args'], $endpoint['args'] ); } $this->args[] = $endpoint; } } /** * Registers the REST route. * * @since 1.16.0 */ public function register() { register_rest_route( REST_Routes::REST_ROOT, $this->get_uri(), $this->get_args() ); } /** * Gets the route URI. * * @since 1.0.0 * * @return string Unique route URI. */ public function get_uri() { return $this->uri; } /** * Gets the route arguments, including endpoints and schema. * * @since 1.0.0 * * @return array Route arguments. */ public function get_args() { return $this->args; } /** * Parses all supported request arguments and their data. * * @since 1.0.0 * * @param array $args Associative array of $arg => $data pairs. * @return array Parsed arguments. */ protected function parse_param_args( array $args ) { return array_map( array( $this, 'parse_param_arg' ), $args ); } /** * Parses data for a supported request argument. * * @since 1.0.0 * * @param array $data { * Request argument data. * * @type string $type Data type of the argument. Default 'string'. * @type string $description Public description of the argument. Default empty string. * @†ype callable $validate_callback Callback to validate the argument. Default * {@see rest_validate_rest_arg()}. * @type callable $sanitize_callback Callback to sanitize the argument. Default * {@see rest_sanitize_rest_arg()}. * @type bool $required Whether the argument is required. Default false. * @type mixed $default Default value for the argument, if any. Default none. * @type array $enum Allowlist of possible values to validate against. Default none. * @type array $items Only if $type is 'array': Similar specification that applies to each item. * @type array $properties Only if $type is 'object'. Similar specification per property. * } * @return array Parsed data. */ protected function parse_param_arg( array $data ) { return wp_parse_args( $data, array( 'type' => 'string', 'description' => '', 'validate_callback' => 'rest_validate_request_arg', 'sanitize_callback' => 'rest_sanitize_request_arg', 'required' => false, 'default' => null, ) ); } } REST_Routes.php000064400000005271150515170200007374 0ustar00context = $context; } /** * Registers functionality through WordPress hooks. * * @since 1.0.0 */ public function register() { add_action( 'rest_api_init', function() { $this->register_routes(); } ); add_filter( 'do_parse_request', function( $do_parse_request, $wp ) { add_filter( 'query_vars', function( $vars ) use ( $wp ) { // Unsets standard public query vars to escape conflicts between WordPress core // and Google Site Kit APIs which happen when WordPress incorrectly parses request // arguments. $unset_vars = ( $wp->request && stripos( $wp->request, trailingslashit( rest_get_url_prefix() ) . self::REST_ROOT ) !== false ) // Check regular permalinks. || ( empty( $wp->request ) && stripos( $this->context->input()->filter( INPUT_GET, 'rest_route' ), self::REST_ROOT ) !== false ); // Check plain permalinks. if ( $unset_vars ) { // List of variable names to remove from public query variables list. return array_values( array_diff( $vars, array( 'orderby', ) ) ); } return $vars; } ); return $do_parse_request; }, 10, 2 ); } /** * Registers all REST routes. * * @since 1.0.0 * @since 1.16.0 Reworked to use REST_Route::register method to register a route. */ private function register_routes() { $routes = $this->get_routes(); foreach ( $routes as $route ) { $route->register(); } } /** * Gets available REST routes. * * @since 1.0.0 * @since 1.3.0 Moved most routes into individual classes and introduced {@see 'googlesitekit_rest_routes'} filter. * * @return array List of REST_Route instances. */ private function get_routes() { $routes = array(); /** * Filters the list of available REST routes. * * @since 1.3.0 * * @param array $routes List of REST_Route objects. */ return apply_filters( 'googlesitekit_rest_routes', $routes ); } } Data_Request.php000064400000006114150515170200007634 0ustar00method = strtoupper( $method ); $this->type = $type; $this->identifier = $identifier; $this->datapoint = $datapoint; $this->data = $data instanceof self ? $data->data : (array) $data; $this->key = $key; } /** * Gets the accessed property by the given name. * * @param string $name Property name. * * @return mixed */ public function __get( $name ) { return isset( $this->$name ) ? $this->$name : null; } /** * Checks whether or not the given magic property is set. * * @param string $name Property name. * * @return bool */ public function __isset( $name ) { return isset( $this->$name ); } /** * Checks whether the given key exists. * * @param string|int $key Key to check. * * @return bool */ public function offsetExists( $key ) { return array_key_exists( $key, $this->data ); } /** * Gets the value at the given key. * * @param string|int $key Key to return the value for. * * @return mixed */ public function offsetGet( $key ) { if ( $this->offsetExists( $key ) ) { return $this->data[ $key ]; } return null; } /** * Sets the given key to the given value. * * @param string|int $key Key to set the value for. * @param mixed $value New value for the given key. */ public function offsetSet( $key, $value ) { // Data is immutable. } /** * Unsets the given key. * * @param string|int $key Key to unset. */ public function offsetUnset( $key ) { // Data is immutable. } } Exception/Invalid_Datapoint_Exception.php000064400000001733150515170200014622 0ustar00 400, // Bad request. ) ); } }