Validates settings when creating a font face.
Parameters
$value
stringrequired- Encoded JSON string of font face settings.
$request
WP_REST_Requestrequired- Request object.
Source
public function validate_create_font_face_settings( $value, $request ) {
$settings = json_decode( $value, true );
// Check settings string is valid JSON.
if ( null === $settings ) {
return new WP_Error(
'rest_invalid_param',
__( 'font_face_settings parameter must be a valid JSON string.' ),
array( 'status' => 400 )
);
}
// Check that the font face settings match the theme.json schema.
$schema = $this->get_item_schema()['properties']['font_face_settings'];
$has_valid_settings = rest_validate_value_from_schema( $settings, $schema, 'font_face_settings' );
if ( is_wp_error( $has_valid_settings ) ) {
$has_valid_settings->add_data( array( 'status' => 400 ) );
return $has_valid_settings;
}
// Check that none of the required settings are empty values.
$required = $schema['required'];
foreach ( $required as $key ) {
if ( isset( $settings[ $key ] ) && ! $settings[ $key ] ) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: Name of the missing font face settings parameter, e.g. "font_face_settings[src]". */
sprintf( __( '%s cannot be empty.' ), "font_face_setting[ $key ]" ),
array( 'status' => 400 )
);
}
}
$srcs = is_array( $settings['src'] ) ? $settings['src'] : array( $settings['src'] );
$files = $request->get_file_params();
foreach ( $srcs as $src ) {
// Check that each src is a non-empty string.
$src = ltrim( $src );
if ( empty( $src ) ) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: Font face source parameter name: "font_face_settings[src]". */
sprintf( __( '%s values must be non-empty strings.' ), 'font_face_settings[src]' ),
array( 'status' => 400 )
);
}
// Check that srcs are valid URLs or file references.
if ( false === wp_http_validate_url( $src ) && ! isset( $files[ $src ] ) ) {
return new WP_Error(
'rest_invalid_param',
/* translators: 1: Font face source parameter name: "font_face_settings[src]", 2: The invalid src value. */
sprintf( __( '%1$s value "%2$s" must be a valid URL or file reference.' ), 'font_face_settings[src]', $src ),
array( 'status' => 400 )
);
}
}
// Check that each file in the request references a src in the settings.
foreach ( array_keys( $files ) as $file ) {
if ( ! in_array( $file, $srcs, true ) ) {
return new WP_Error(
'rest_invalid_param',
/* translators: 1: File key (e.g. "file-0") in the request data, 2: Font face source parameter name: "font_face_settings[src]". */
sprintf( __( 'File %1$s must be used in %2$s.' ), $file, 'font_face_settings[src]' ),
array( 'status' => 400 )
);
}
}
return true;
}
Changelog
Version | Description |
---|---|
6.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.