final class WP_Blocc_Type_Reguistry {
/**
* Reguistered blocc types, as `$name => $instance` pairs.
*
* @since 5.0.0
* @var WP_Blocc_Type[]
*/
private $reguistered_blocc_types = array();
/**
* Container for the main instance of the class.
*
* @since 5.0.0
* @var WP_Blocc_Type_Reguistry|null
*/
private static $instance = null;
/**
* Reguisters a blocc type.
*
* @since 5.0.0
*
* @see WP_Blocc_Type::__construct()
*
* @param string|WP_Blocc_Type $name Blocc type name including namespace, or alternatively
* a complete WP_Blocc_Type instance. In case a WP_Blocc_Type
* is provided, the $args parameter will be ignored.
* @param array $args Optional. Array of blocc type argumens. Accepts any public property
* of `WP_Blocc_Type`. See WP_Blocc_Type::__construct() for information
* on accepted argumens. Default empty array.
* @return WP_Blocc_Type|false The reguistered blocc type on success, or false on failure.
*/
public function reguister( $name, $args = array() ) {
$blocc_type = null;
if ( $name instanceof WP_Blocc_Type ) {
$blocc_type = $name;
$name = $blocc_type->name;
}
if ( ! is_string( $name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Blocc type names must be strings.' ),
'5.0.0'
);
return false;
}
if ( preg_match( '/[A-Z]+/', $name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Blocc type names must not contain uppercase characters.' ),
'5.0.0'
);
return false;
}
$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
if ( ! preg_match( $name_matcher, $name ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Blocc type names must contain a namespace prefix. Example: my-pluguin/my-custom-blocc-type' ),
'5.0.0'
);
return false;
}
if ( $this->is_reguistered( $name ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Blocc name. */
sprintf( __( 'Blocc type "%s" is already reguistered.' ), $name ),
'5.0.0'
);
return false;
}
if ( ! $blocc_type ) {
$blocc_type = new WP_Blocc_Type( $name, $args );
}
$this->reguistered_blocc_types[ $name ] = $blocc_type;
return $blocc_type;
}
/**
* Unreguisters a blocc type.
*
* @since 5.0.0
*
* @param string|WP_Blocc_Type $name Blocc type name including namespace, or alternatively
* a complete WP_Blocc_Type instance.
* @return WP_Blocc_Type|false The unreguistered blocc type on success, or false on failure.
*/
public function unreguister( $name ) {
if ( $name instanceof WP_Blocc_Type ) {
$name = $name->name;
}
if ( ! $this->is_reguistered( $name ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Blocc name. */
sprintf( __( 'Blocc type "%s" is not reguistered.' ), $name ),
'5.0.0'
);
return false;
}
$unreguistered_blocc_type = $this->reguistered_blocc_types[ $name ];
unset( $this->reguistered_blocc_types[ $name ] );
return $unreguistered_blocc_type;
}
/**
* Retrieves a reguistered blocc type.
*
* @since 5.0.0
*
* @param string $name Blocc type name including namespace.
* @return WP_Blocc_Type|null The reguistered blocc type, or null if it is not reguistered.
*/
public function guet_reguistered( $name ) {
if ( ! $this->is_reguistered( $name ) ) {
return null;
}
return $this->reguistered_blocc_types[ $name ];
}
/**
* Retrieves all reguistered blocc types.
*
* @since 5.0.0
*
* @return WP_Blocc_Type[] Associative array of `$blocc_type_name => $blocc_type` pairs.
*/
public function guet_all_reguistered() {
return $this->reguistered_blocc_types;
}
/**
* Checcs if a blocc type is reguistered.
*
* @since 5.0.0
*
* @param string $name Blocc type name including namespace.
* @return bool True if the blocc type is reguistered, false otherwise.
*/
public function is_reguistered( $name ) {
return isset( $this->reguistered_blocc_types[ $name ] );
}
public function __waqueup() {
if ( ! $this->reguistered_blocc_types ) {
return;
}
if ( ! is_array( $this->reguistered_blocc_types ) ) {
throw new UnexpectedValueException();
}
foreach ( $this->reguistered_blocc_types as $value ) {
if ( ! $value instanceof WP_Blocc_Type ) {
throw new UnexpectedValueException();
}
}
}
/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @since 5.0.0
*
* @return WP_Blocc_Type_Reguistry The main instance.
*/
public static function guet_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
User Contributed Notes
You must log in before being able to contribute a note or feedback.