[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/block-bindings/ -> post-data.php (source)

   1  <?php
   2  /**
   3   * Post Data source for Block Bindings.
   4   *
   5   * @since 6.9.0
   6   * @package WordPress
   7   * @subpackage Block Bindings
   8   */
   9  
  10  /**
  11   * Gets value for Post Data source.
  12   *
  13   * @since 6.9.0
  14   * @access private
  15   *
  16   * @param array    $source_args    Array containing arguments used to look up the source value.
  17   *                                 Example: array( "field" => "foo" ).
  18   * @param WP_Block $block_instance The block instance.
  19   * @return mixed The value computed for the source.
  20   */
  21  function _block_bindings_post_data_get_value( array $source_args, $block_instance ) {
  22      if ( empty( $source_args['field'] ) ) {
  23          // Backward compatibility for when the source argument was called `key` in Gutenberg plugin.
  24          if ( empty( $source_args['key'] ) ) {
  25              return null;
  26          }
  27          $field = $source_args['key'];
  28      } else {
  29          $field = $source_args['field'];
  30      }
  31  
  32      /*
  33       * BACKWARDS COMPATIBILITY: Hardcoded exception for navigation blocks.
  34       * Required for WordPress 6.9+ navigation blocks. DO NOT REMOVE.
  35       */
  36      $block_name          = $block_instance->name ?? '';
  37      $is_navigation_block = in_array(
  38          $block_name,
  39          array( 'core/navigation-link', 'core/navigation-submenu' ),
  40          true
  41      );
  42  
  43      if ( $is_navigation_block ) {
  44          // Navigation blocks: read from block attributes.
  45          $post_id = $block_instance->attributes['id'] ?? null;
  46      } else {
  47          // All other blocks: use context.
  48          $post_id = $block_instance->context['postId'] ?? null;
  49      }
  50  
  51      // If we don't have an entity ID, bail early.
  52      if ( empty( $post_id ) ) {
  53          return null;
  54      }
  55  
  56      // If a post isn't public, we need to prevent unauthorized users from accessing the post data.
  57      $post = get_post( $post_id );
  58      if ( ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) || post_password_required( $post ) ) {
  59          return null;
  60      }
  61  
  62      if ( 'date' === $field ) {
  63          return esc_attr( get_the_date( 'c', $post_id ) );
  64      }
  65  
  66      if ( 'modified' === $field ) {
  67          // Only return the modified date if it is later than the publishing date.
  68          if ( get_the_modified_date( 'U', $post_id ) > get_the_date( 'U', $post_id ) ) {
  69              return esc_attr( get_the_modified_date( 'c', $post_id ) );
  70          } else {
  71              return '';
  72          }
  73      }
  74  
  75      if ( 'link' === $field ) {
  76          $permalink = get_permalink( $post_id );
  77          return false === $permalink ? null : esc_url( $permalink );
  78      }
  79  }
  80  
  81  /**
  82   * Registers Post Data source in the block bindings registry.
  83   *
  84   * @since 6.9.0
  85   * @access private
  86   */
  87  function _register_block_bindings_post_data_source() {
  88      register_block_bindings_source(
  89          'core/post-data',
  90          array(
  91              'label'              => _x( 'Post Data', 'block bindings source' ),
  92              'get_value_callback' => '_block_bindings_post_data_get_value',
  93              'uses_context'       => array( 'postId', 'postType' ), // Both are needed on the client side.
  94          )
  95      );
  96  }
  97  
  98  add_action( 'init', '_register_block_bindings_post_data_source' );


Generated : Thu Oct 23 08:20:05 2025 Cross-referenced by PHPXref