-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Font collection registration: handling invalid slugs #58695
Comments
To capture some of the initial discussion in WordPress/wordpress-develop#6027 (comment) From @hellofromtonya
Answer from @creativecoder
|
Additionally from @hellofromtonya
|
I agree that throwing is heavy-handed, and the more idiomatic WordPress thing would be to return an error, but this isn't possible inside a class constructor. But I still think the system should reject any collection with an invalid slug. We're building something brand new, there are no legacy concerns here, and so we should be as strict as possible. The obvious possible approaches to me are:
class Foo {
private function __construct($value) {
if (! $value) {
throw new ValueError();
}
}
public static function instantiate($value) {
try {
return new self($value);
} catch (ValueError $e) {
return $e;
}
}
}
assert(Foo::instantiate(1) instanceof Foo);
assert(Foo::instantiate(0) instanceof ValueError); Is it weird, ugly and unlike how conventions? Yes. So maybe something along the first approach is more suitable. What matters is that, in the end, the only caller we have so far — public function register_font_collection( $slug, $data_or_file ) {
$new_collection = new WP_Font_Collection( $slug, $data_or_file );
+ if ( ! $new_collection->is_valid() ) {
+ $error_message = sprintf( '...' );
+ return new WP_Error( '...' );
+ }
if ( $this->is_collection_registered( $new_collection->slug ) ) {
$error_message = sprintf(
/* translators: %s: Font collection slug. */
__( 'Font collection with slug: "%s" is already registered.' ),
$new_collection->slug
);
_doing_it_wrong(
__METHOD__,
$error_message,
'6.5.0'
);
return new WP_Error( 'font_collection_registration_error', $error_message );
}
$this->collections[ $new_collection->slug ] = $new_collection;
return $new_collection;
} |
One of the complications here is that it's desirable to lazy load font_families when they're provided in a json file, to delay the overhead of reading a (potentially large) file or making an http request until the data is needed. So We could add a slug specific method, e.g. |
Fair enough, I don't want to block this. I trust your judgement :) |
I'm going to close this issue, since WP 6.5 has shipped and the |
Font collections are registered with a slug that is used as a unique identifier for the collection.
For example:
I now reference that collection by its slug in PHP or using the REST API:
WP_Font_Library::get_font_collection( 'my-collection')
wp/v2/font-collections/my-colleciton
Collection slugs are sanitized with
sanitize_title
to create slugs that are url safe values appropriate to use as identifiers.The question came up in this wordpress-develop PR, what is the appropriate level of error handling when a slug is invalid (the sanitized slug value does not match it's given value)?
Some possibilities include
_doing_it_wrong
notice (this is what we're doing at the moment)WP_Error
instance somewhere and returning it at an appropriate time (perhaps when retrieving the slug?)The text was updated successfully, but these errors were encountered: