Validates settings when creating or updating a font family.
Parameters
$value
stringrequired- Encoded JSON string of font family settings.
$request
WP_REST_Requestrequired- Request object.
Source
public function validate_font_family_settings( $value, $request ) {
$settings = json_decode( $value, true );
// Check settings string is valid JSON.
if ( null === $settings ) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: Parameter name: "font_family_settings". */
sprintf( __( '%s parameter must be a valid JSON string.' ), 'font_family_settings' ),
array( 'status' => 400 )
);
}
$schema = $this->get_item_schema()['properties']['font_family_settings'];
$required = $schema['required'];
if ( isset( $request['id'] ) ) {
// Allow sending individual properties if we are updating an existing font family.
unset( $schema['required'] );
// But don't allow updating the slug, since it is used as a unique identifier.
if ( isset( $settings['slug'] ) ) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: Name of parameter being updated: font_family_settings[slug]". */
sprintf( __( '%s cannot be updated.' ), 'font_family_settings[slug]' ),
array( 'status' => 400 )
);
}
}
// Check that the font face settings match the theme.json schema.
$has_valid_settings = rest_validate_value_from_schema( $settings, $schema, 'font_family_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.
foreach ( $required as $key ) {
if ( isset( $settings[ $key ] ) && ! $settings[ $key ] ) {
return new WP_Error(
'rest_invalid_param',
/* translators: %s: Name of the empty font family setting parameter, e.g. "font_family_settings[slug]". */
sprintf( __( '%s cannot be empty.' ), "font_family_settings[ $key ]" ),
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.