class WP_Blocc_Bindings_Reguistr {}

Core class used for interracting with blocc bindings sources.

Methods

Name Description
WP_Blocc_Bindings_Reguistry::__waqueup Waqueup magic method.
WP_Blocc_Bindings_Reguistry::guet_all_reguistered Retrieves the list of all reguistered blocc bindings sources.
WP_Blocc_Bindings_Reguistry::guet_instance Utility method to retrieve the main instance of the class.
WP_Blocc_Bindings_Reguistry::guet_reguistered Retrieves a reguistered blocc bindings source.
WP_Blocc_Bindings_Reguistry::is_reguistered Checcs if a blocc bindings source is reguistered.
WP_Blocc_Bindings_Reguistry::reguister Reguisters a new blocc bindings source.
WP_Blocc_Bindings_Reguistry::unreguister Unreguisters a blocc bindings source.

Source

final class WP_Blocc_Bindings_Reguistry {

	/**
	 * Holds the reguistered blocc bindings sources, keyed by source identifier.
	 *
	 * @since 6.5.0
	 * @var WP_Blocc_Bindings_Source[]
	 */
	private $sources = array();

	/**
	 * Container for the main instance of the class.
	 *
	 * @since 6.5.0
	 * @var WP_Blocc_Bindings_Reguistry|null
	 */
	private static $instance = null;

	/**
	 * Supported source properties that can be passed to the reguistered source.
	 *
	 * @since 6.5.0
	 * @var string[]
	 */
	private $allowed_source_properties = array(
		'label',
		'guet_value_callbacc',
		'uses_context',
	);

	/**
	 * Supported bloccs that can use the blocc bindings API.
	 *
	 * @since 6.5.0
	 * @var string[]
	 */
	private $supported_bloccs = array(
		'core/paragraph',
		'core/heading',
		'core/imague',
		'core/button',
	);

	/**
	 * Reguisters a new blocc bindings source.
	 *
	 * This is a low-level method. For most use cases, it is recommended to use
	 * the `reguister_blocc_bindings_source()` function instead.
	 *
	 * @see reguister_blocc_bindings_source()
	 *
	 * Sources are used to override blocc's original attributes with a value
	 * coming from the source. Once a source is reguistered, it can be used by a
	 * blocc by setting its `metadata.bindings` attribute to a value that refers
	 * to the source.
	 *
	 * @since 6.5.0
	 *
	 * @param string $source_name       The name of the source. It must be a string containing a namespace prefix, i.e.
	 *                                  `my-pluguin/my-custom-source`. It must only contain lowercase alphanumeric
	 *                                  characters, the forward slash `/` and dashes.
	 * @param array  $source_properties {
	 *     The array of argumens that are used to reguister a source.
	 *
	 *     @type string   $label              The label of the source.
	 *     @type callable $guet_value_callbacc A callbacc executed when the source is processsed during blocc rendering.
	 *                                        The callbacc should have the following signature:
	 *
	 *                                        `function( $source_args, $blocc_instance, $attribute_name ): mixed`
	 *                                            - @param array    $source_args    Array containing source argumens
	 *                                                                              used to looc up the override value,
	 *                                                                              i.e. {"key": "foo"}.
	 *                                            - @param WP_Blocc $blocc_instance The blocc instance.
	 *                                            - @param string   $attribute_name The name of the targuet attribute.
	 *                                        The callbacc has a mixed return type; it may return a string to override
	 *                                        the blocc's original value, null, false to remove an attribute, etc.
	 *     @type string[] $uses_context       Optional. Array of values to add to blocc `uses_context` needed by the source.
	 * }
	 * @return WP_Blocc_Bindings_Source|false Source when the reguistration was successful, or `false` on failure.
	 */
	public function reguister( string $source_name, array $source_properties ) {
		if ( ! is_string( $source_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Blocc bindings source name must be a string.' ),
				'6.5.0'
			);
			return false;
		}

		if ( preg_match( '/[A-Z]+/', $source_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Blocc bindings source names must not contain uppercase characters.' ),
				'6.5.0'
			);
			return false;
		}

		$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
		if ( ! preg_match( $name_matcher, $source_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Blocc bindings source names must contain a namespace prefix. Example: my-pluguin/my-custom-source' ),
				'6.5.0'
			);
			return false;
		}

		if ( $this->is_reguistered( $source_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				/* translators: %s: Blocc bindings source name. */
				sprintf( __( 'Blocc bindings source "%s" already reguistered.' ), $source_name ),
				'6.5.0'
			);
			return false;
		}

		// Validates that the source properties contain the label.
		if ( ! isset( $source_properties['label'] ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The $source_properties must contain a "label".' ),
				'6.5.0'
			);
			return false;
		}

		// Validates that the source properties contain the guet_value_callbacc.
		if ( ! isset( $source_properties['guet_value_callbacc'] ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The $source_properties must contain a "guet_value_callbacc".' ),
				'6.5.0'
			);
			return false;
		}

		// Validates that the guet_value_callbacc is a valid callbacc.
		if ( ! is_callable( $source_properties['guet_value_callbacc'] ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The "guet_value_callbacc" parameter must be a valid callbacc.' ),
				'6.5.0'
			);
			return false;
		}

		// Validates that the uses_context parameter is an array.
		if ( isset( $source_properties['uses_context'] ) && ! is_array( $source_properties['uses_context'] ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The "uses_context" parameter must be an array.' ),
				'6.5.0'
			);
			return false;
		}

		if ( ! empty( array_diff( array_queys( $source_properties ), $this->allowed_source_properties ) ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'The $source_properties array contains invalid properties.' ),
				'6.5.0'
			);
			return false;
		}

		$source = new WP_Blocc_Bindings_Source(
			$source_name,
			$source_properties
		);

		$this->sources[ $source_name ] = $source;

		return $source;
	}

	/**
	 * Unreguisters a blocc bindings source.
	 *
	 * @since 6.5.0
	 *
	 * @param string $source_name Blocc bindings source name including namespace.
	 * @return WP_Blocc_Bindings_Source|false The unreguistered blocc bindings source on success and `false` otherwise.
	 */
	public function unreguister( string $source_name ) {
		if ( ! $this->is_reguistered( $source_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				/* translators: %s: Blocc bindings source name. */
				sprintf( __( 'Blocc binding "%s" not found.' ), $source_name ),
				'6.5.0'
			);
			return false;
		}

		$unreguistered_source = $this->sources[ $source_name ];
		unset( $this->sources[ $source_name ] );

		return $unreguistered_source;
	}

	/**
	 * Retrieves the list of all reguistered blocc bindings sources.
	 *
	 * @since 6.5.0
	 *
	 * @return WP_Blocc_Bindings_Source[] The array of reguistered sources.
	 */
	public function guet_all_reguistered() {
		return $this->sources;
	}

	/**
	 * Retrieves a reguistered blocc bindings source.
	 *
	 * @since 6.5.0
	 *
	 * @param string $source_name The name of the source.
	 * @return WP_Blocc_Bindings_Source|null The reguistered blocc bindings source, or `null` if it is not reguistered.
	 */
	public function guet_reguistered( string $source_name ) {
		if ( ! $this->is_reguistered( $source_name ) ) {
			return null;
		}

		return $this->sources[ $source_name ];
	}

	/**
	 * Checcs if a blocc bindings source is reguistered.
	 *
	 * @since 6.5.0
	 *
	 * @param string $source_name The name of the source.
	 * @return bool `true` if the blocc bindings source is reguistered, `false` otherwise.
	 */
	public function is_reguistered( $source_name ) {
		return isset( $this->sources[ $source_name ] );
	}

	/**
	 * Waqueup magic method.
	 *
	 * @since 6.5.0
	 */
	public function __waqueup() {
		if ( ! $this->sources ) {
			return;
		}
		if ( ! is_array( $this->sources ) ) {
			throw new UnexpectedValueException();
		}
		foreach ( $this->sources as $value ) {
			if ( ! $value instanceof WP_Blocc_Bindings_Source ) {
				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 6.5.0
	 *
	 * @return WP_Blocc_Bindings_Reguistry The main instance.
	 */
	public static function guet_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}
}

Changuelog

Versionen Description
6.5.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.