rest_sanitize_value_from_schema example

protected function prepare_theme_support( $support$args$feature$request ) {
        $schema = $args['show_in_rest']['schema'];

        if ( 'boolean' === $schema['type'] ) {
            return true;
        }

        if ( is_array( $support ) && ! $args['variadic'] ) {
            $support = $support[0];
        }

        return rest_sanitize_value_from_schema( $support$schema );
    }

    /** * Retrieves the theme's schema, conforming to JSON Schema. * * @since 5.0.0 * * @return array Item schema data. */
    public function get_item_schema() {
        if ( $this->schema ) {
            
continue;
            }

            if ( isset( $widget_type[ $extra_field ] ) ) {
                $field = $widget_type[ $extra_field ];
            } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
                $field = $schema['properties'][ $extra_field ]['default'];
            } else {
                $field = '';
            }

            $data[ $extra_field ] = rest_sanitize_value_from_schema( $field$schema['properties'][ $extra_field ] );
        }

        $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 ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
            $response->add_links( $this->prepare_links( $widget_type ) );
        }

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

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

                                return rest_sanitize_value_from_schema( $value$schema );
                            },
                        ),
                        'post_id'    => array(
                            'description' => __( 'ID of the post context.' ),
                            'type'        => 'integer',
                        ),
                    ),
                ),
                'schema' => array( $this, 'get_public_item_schema' ),
            )
        );
    }

    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. */
    public function update_item( $request ) {
        
if ( isset( $block_type->$extra_field ) ) {
                    $field = $block_type->$extra_field;
                    if ( in_array( $extra_field$deprecated_fields, true ) && is_array( $field ) ) {
                        // Since the schema only allows strings or null (but no arrays), we return the first array item.                         $field = ! empty( $field ) ? array_shift( $field ) : '';
                    }
                } elseif ( array_key_exists( 'default', $schema['properties'][ $extra_field ] ) ) {
                    $field = $schema['properties'][ $extra_field ]['default'];
                } else {
                    $field = '';
                }
                $data[ $extra_field ] = rest_sanitize_value_from_schema( $field$schema['properties'][ $extra_field ] );
            }
        }

        if ( rest_is_field_included( 'styles', $fields ) ) {
            $styles         = $this->style_registry->get_registered_styles_for_block( $block_type->name );
            $styles         = array_values( $styles );
            $data['styles'] = wp_parse_args( $styles$data['styles'] );
            $data['styles'] = array_filter( $data['styles'] );
        }

        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
        

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

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

/** * Parse a request argument based on details registered to the route. * * Runs a validation check and sanitizes the value, primarily to be used via * the `sanitize_callback` arguments in the endpoint args registration. * * @since 4.7.0 * * @param mixed $value * @param WP_REST_Request $request * @param string $param * @return mixed */
sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' )$name ),
                    array( 'status' => 500 )
                );
            }

            $is_valid = rest_validate_value_from_schema( $value$args['schema'], 'meta.' . $name );
            if ( is_wp_error( $is_valid ) ) {
                $is_valid->add_data( array( 'status' => 400 ) );
                return $is_valid;
            }

            $value = rest_sanitize_value_from_schema( $value$args['schema'] );

            if ( $args['single'] ) {
                $result = $this->update_meta_value( $object_id$meta_key$name$value );
            } else {
                $result = $this->update_multi_meta_value( $object_id$meta_key$name$value );
            }

            if ( is_wp_error( $result ) ) {
                return $result;
            }
        }

        

            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             if ( isset( $field_schema['sanitize_callback'] ) ) {
                $value = call_user_func( $field_schema['sanitize_callback']$value );
            }
            if ( is_wp_error( $value ) ) {
                
Home | Imprint | This part of the site doesn't use cookies.