rest_validate_value_from_schema example


    protected function prepare_value( $value$schema ) {
        /* * If the value is not valid by the schema, set the value to null. * Null values are specifically non-destructive, so this will not cause * overwriting the current invalid value to null. */
        if ( is_wp_error( rest_validate_value_from_schema( $value$schema ) ) ) {
            return null;
        }

        return rest_sanitize_value_from_schema( $value$schema );
    }

    /** * Updates settings for the settings object. * * @since 4.7.0 * * @param WP_REST_Request $request Full details about the request. * @return array|WP_Error Array on success, or error object on failure. */
if ( ! $block ) {
                                    // This will get rejected in ::get_item().                                     return true;
                                }

                                $schema = array(
                                    'type'                 => 'object',
                                    'properties'           => $block->get_attributes(),
                                    'additionalProperties' => false,
                                );

                                return rest_validate_value_from_schema( $value$schema );
                            },
                            'sanitize_callback' => static function D $value$request ) {
                                $block = WP_Block_Type_Registry::get_instance()->get_registered( $request['name'] );

                                if ( ! $block ) {
                                    // This will get rejected in ::get_item().                                     return true;
                                }

                                $schema = array(
                                    'type'                 => 'object',
                                    
 else {
            add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
        }
    }

    if ( array_key_exists( 'default', $args ) ) {
        $schema = $args;
        if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) {
            $schema = array_merge( $schema$args['show_in_rest']['schema'] );
        }

        $check = rest_validate_value_from_schema( $args['default']$schema );
        if ( is_wp_error( $check ) ) {
            _doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' );

            return false;
        }

        if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) {
            add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 );
        }
    }

    

function rest_validate_request_arg( $value$request$param ) {
    $attributes = $request->get_attributes();
    if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) {
        return true;
    }
    $args = $attributes['args'][ $param ];

    return rest_validate_value_from_schema( $value$args$param );
}

/** * Sanitize a request argument based on details registered to the route. * * @since 4.7.0 * * @param mixed $value * @param WP_REST_Request $request * @param string $param * @return mixed */
            // validated.             if ( ! isset( $this->attributes[ $attribute_name ] ) ) {
                continue;
            }

            $schema = $this->attributes[ $attribute_name ];

            // Validate value by JSON schema. An invalid value should revert to             // its default, if one exists. This occurs by virtue of the missing             // attributes loop immediately following. If there is not a default             // assigned, the attribute value should remain unset.             $is_valid = rest_validate_value_from_schema( $value$schema$attribute_name );
            if ( is_wp_error( $is_valid ) ) {
                unset( $attributes[ $attribute_name ] );
            }
        }

        // Populate values of any missing attributes for which the block type         // defines a default.         $missing_schema_attributes = array_diff_key( $this->attributes, $attributes );
        foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
            if ( isset( $schema['default'] ) ) {
                $attributes[ $attribute_name ] = $schema['default'];
            }

            $value = $new_instance[ $field ];

            /* * Workaround for rest_validate_value_from_schema() due to the fact that * rest_is_boolean( '' ) === false, while rest_is_boolean( '1' ) is true. */
            if ( 'boolean' === $field_schema['type'] && '' === $value ) {
                $value = false;
            }

            if ( true !== rest_validate_value_from_schema( $value$field_schema$field ) ) {
                continue;
            }

            $value = rest_sanitize_value_from_schema( $value$field_schema );

            // @codeCoverageIgnoreStart             if ( is_wp_error( $value ) ) {
                continue; // Handle case when rest_sanitize_value_from_schema() ever returns WP_Error as its phpdoc @return tag indicates.             }

            // @codeCoverageIgnoreEnd
static function D $widget_id ) use ( $wp_registered_widgets ) {
                    return isset( $wp_registered_widgets[ $widget_id ] );
                }
            );

            $sidebar['widgets'] = array_values( $widgets );
        }

        $schema = $this->get_item_schema();
        $data   = array();
        foreach ( $schema['properties'] as $property_id => $property ) {
            if ( isset( $sidebar[ $property_id ] ) && true === rest_validate_value_from_schema( $sidebar[ $property_id ]$property ) ) {
                $data[ $property_id ] = $sidebar[ $property_id ];
            } elseif ( isset( $property['default'] ) ) {
                $data[ $property_id ] = $property['default'];
            }
        }

        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $data    = $this->add_additional_fields_to_object( $data$request );
        $data    = $this->filter_response_by_context( $data$context );

        $response = rest_ensure_response( $data );

        

            if ( is_null( $value ) || ( array() === $value && ! $args['single'] ) ) {
                $args = $this->get_registered_fields()[ $meta_key ];

                if ( $args['single'] ) {
                    $current = get_metadata( $this->get_meta_type()$object_id$meta_key, true );

                    if ( is_wp_error( rest_validate_value_from_schema( $current$args['schema'] ) ) ) {
                        return new WP_Error(
                            'rest_invalid_stored_value',
                            /* translators: %s: Custom field key. */
                            sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' )$name ),
                            array( 'status' => 500 )
                        );
                    }
                }

                $result = $this->delete_meta_value( $object_id$meta_key$name );
                if ( is_wp_error( $result ) ) {
                    
public function check_status( $status$request$param ) {
        if ( $request['id'] ) {
            $post = $this->get_post( $request['id'] );

            if ( ! is_wp_error( $post ) && $post->post_status === $status ) {
                return true;
            }
        }

        $args = $request->get_attributes()['args'][ $param ];

        return rest_validate_value_from_schema( $status$args$param );
    }

    /** * Determines validity and normalizes the given status parameter. * * @since 4.7.0 * * @param string $post_status Post status. * @param WP_Post_Type $post_type Post type. * @return string|WP_Error Post status or WP_Error if lacking the proper permission. */
    
Home | Imprint | This part of the site doesn't use cookies.