[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> post-date.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/post-date` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/post-date` block on the server.
  10   *
  11   * @param array    $attributes Block attributes.
  12   * @param string   $content    Block default content.
  13   * @param WP_Block $block      Block instance.
  14   * @return string Returns the filtered post date for the current post wrapped inside "time" tags.
  15   */
  16  function render_block_core_post_date( $attributes, $content, $block ) {
  17      if ( ! isset( $block->context['postId'] ) ) {
  18          return '';
  19      }
  20  
  21      $post_ID          = $block->context['postId'];
  22      $formatted_date   = get_the_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
  23      $unformatted_date = esc_attr( get_the_date( 'c', $post_ID ) );
  24      $classes          = array();
  25  
  26      if ( isset( $attributes['textAlign'] ) ) {
  27          $classes[] = 'has-text-align-' . $attributes['textAlign'];
  28      }
  29      if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
  30          $classes[] = 'has-link-color';
  31      }
  32  
  33      /*
  34       * If the "Display last modified date" setting is enabled,
  35       * only display the modified date if it is later than the publishing date.
  36       */
  37      if ( isset( $attributes['displayType'] ) && 'modified' === $attributes['displayType'] ) {
  38          if ( get_the_modified_date( 'Ymdhi', $post_ID ) > get_the_date( 'Ymdhi', $post_ID ) ) {
  39              $formatted_date   = get_the_modified_date( empty( $attributes['format'] ) ? '' : $attributes['format'], $post_ID );
  40              $unformatted_date = esc_attr( get_the_modified_date( 'c', $post_ID ) );
  41              $classes[]        = 'wp-block-post-date__modified-date';
  42          } else {
  43              return '';
  44          }
  45      }
  46  
  47      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
  48  
  49      if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
  50          $formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date );
  51      }
  52  
  53      return sprintf(
  54          '<div %1$s><time datetime="%2$s">%3$s</time></div>',
  55          $wrapper_attributes,
  56          $unformatted_date,
  57          $formatted_date
  58      );
  59  }
  60  
  61  /**
  62   * Registers the `core/post-date` block on the server.
  63   */
  64  function register_block_core_post_date() {
  65      register_block_type_from_metadata(
  66          __DIR__ . '/post-date',
  67          array(
  68              'render_callback' => 'render_block_core_post_date',
  69          )
  70      );
  71  }
  72  add_action( 'init', 'register_block_core_post_date' );


Generated : Wed Apr 24 08:20:01 2024 Cross-referenced by PHPXref