Make WordPress Core


Ignore:
Timestamp:
02/08/2024 08:55:18 AM (11 months ago)
Author:
gziolo
Message:

Editor: Introduce WP_Block_Bindings_Source class

Abstracts the block bindings source array into a well-defined object.

Fixes #60447.
See #60282.
Follow-up [57373].
Props czapla, santosguillamot, gziolo.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-bindings-registry.php

    r57560 r57562  
    2121     *
    2222     * @since 6.5.0
    23      * @var array
     23     * @var WP_Block_Bindings_Source[]
    2424     */
    2525    private $sources = array();
     
    6767     *                                        the block's original value, null, false to remove an attribute, etc.
    6868     * }
    69      * @return array|false Source when the registration was successful, or `false` on failure.
     69     * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure.
    7070     */
    7171    public function register( string $source_name, array $source_properties ) {
     
    108108        }
    109109
    110         $source = array_merge(
    111             array( 'name' => $source_name ),
     110        /* Validate that the source properties contain the label */
     111        if ( ! isset( $source_properties['label'] ) ) {
     112            _doing_it_wrong(
     113                __METHOD__,
     114                __( 'The $source_properties must contain a "label".' ),
     115                '6.5.0'
     116            );
     117            return false;
     118        }
     119
     120        /* Validate that the source properties contain the get_value_callback */
     121        if ( ! isset( $source_properties['get_value_callback'] ) ) {
     122            _doing_it_wrong(
     123                __METHOD__,
     124                __( 'The $source_properties must contain a "get_value_callback".' ),
     125                '6.5.0'
     126            );
     127            return false;
     128        }
     129
     130        /* Validate that the get_value_callback is a valid callback */
     131        if ( ! is_callable( $source_properties['get_value_callback'] ) ) {
     132            _doing_it_wrong(
     133                __METHOD__,
     134                __( 'The "get_value_callback" parameter must be a valid callback.' ),
     135                '6.5.0'
     136            );
     137            return false;
     138        }
     139
     140        $source = new WP_Block_Bindings_Source(
     141            $source_name,
    112142            $source_properties
    113143        );
     
    124154     *
    125155     * @param string $source_name Block bindings source name including namespace.
    126      * @return array|false The unregistered block bindings source on success and `false` otherwise.
     156     * @return WP_Block_Bindings_Source|false The unregistered block bindings source on success and `false` otherwise.
    127157     */
    128158    public function unregister( string $source_name ) {
     
    148178     * @since 6.5.0
    149179     *
    150      * @return array The array of registered sources.
     180     * @return WP_Block_Bindings_Source[] The array of registered sources.
    151181     */
    152182    public function get_all_registered() {
     
    160190     *
    161191     * @param string $source_name The name of the source.
    162      * @return array|null The registered block bindings source, or `null` if it is not registered.
     192     * @return WP_Block_Bindings_Source|null The registered block bindings source, or `null` if it is not registered.
    163193     */
    164194    public function get_registered( string $source_name ) {
Note: See TracChangeset for help on using the changeset viewer.