Make WordPress Core


Ignore:
Timestamp:
10/16/2025 06:54:54 PM (11 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.

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.