Make WordPress Core

Changeset 60946


Ignore:
Timestamp:
10/16/2025 06:54:54 PM (5 months ago)
Author:
Bernhard Reiter
Message:

Block Bindings: Add core/term-data source; extend core/post-data.

Add a new core/term-data source to make information about a taxonomy term available for use in Block Bindings.
Additionally, expose a post object's permalink through the core/post-data source.

Props bernhard-reiter, get_dave, jeryj, cbravobernal.
Fixes #64107.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-bindings/post-data.php

    r60778 r60946  
    2424    }
    2525
    26     if ( empty( $block_instance->context['postId'] ) ) {
     26    /*
     27     * BACKWARDS COMPATIBILITY: Hardcoded exception for navigation blocks.
     28     * Required for WordPress 6.9+ navigation blocks. DO NOT REMOVE.
     29     */
     30    $block_name          = $block_instance->name ?? '';
     31    $is_navigation_block = in_array(
     32        $block_name,
     33        array( 'core/navigation-link', 'core/navigation-submenu' ),
     34        true
     35    );
     36
     37    if ( $is_navigation_block ) {
     38        // Navigation blocks: read from block attributes.
     39        $post_id = $block_instance->attributes['id'] ?? null;
     40    } else {
     41        // All other blocks: use context.
     42        $post_id = $block_instance->context['postId'] ?? null;
     43    }
     44
     45    // If we don't have an entity ID, bail early.
     46    if ( empty( $post_id ) ) {
    2747        return null;
    2848    }
    29     $post_id = $block_instance->context['postId'];
    3049
    3150    // If a post isn't public, we need to prevent unauthorized users from accessing the post data.
     
    4665            return '';
    4766        }
     67    }
     68
     69    if ( 'link' === $source_args['key'] ) {
     70        $permalink = get_permalink( $post_id );
     71        return false === $permalink ? null : esc_url( $permalink );
    4872    }
    4973}
  • trunk/src/wp-settings.php

    r60939 r60946  
    370370require ABSPATH . WPINC . '/block-bindings/post-data.php';
    371371require ABSPATH . WPINC . '/block-bindings/post-meta.php';
     372require ABSPATH . WPINC . '/block-bindings/term-data.php';
    372373require ABSPATH . WPINC . '/blocks.php';
    373374require ABSPATH . WPINC . '/blocks/index.php';
  • trunk/tests/phpunit/includes/functions.php

    r60539 r60946  
    351351    remove_action( 'init', '_register_block_bindings_post_data_source' );
    352352    remove_action( 'init', '_register_block_bindings_post_meta_source' );
     353    remove_action( 'init', '_register_block_bindings_term_data_source' );
    353354}
    354355tests_add_filter( 'init', '_unhook_block_registration', 1000 );
  • trunk/tests/phpunit/tests/block-bindings/register.php

    r60539 r60946  
    7676            'core/post-meta'         => get_block_bindings_source( 'core/post-meta' ),
    7777            'core/pattern-overrides' => get_block_bindings_source( 'core/pattern-overrides' ),
     78            'core/term-data'         => get_block_bindings_source( 'core/term-data' ),
    7879        );
    7980
Note: See TracChangeset for help on using the changeset viewer.