[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> latest-posts.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/latest-posts` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * The excerpt length set by the Latest Posts core block
  10   * set at render time and used by the block itself.
  11   *
  12   * @var int
  13   */
  14  global $block_core_latest_posts_excerpt_length;
  15  $block_core_latest_posts_excerpt_length = 0;
  16  
  17  /**
  18   * Callback for the excerpt_length filter used by
  19   * the Latest Posts block at render time.
  20   *
  21   * @return int Returns the global $block_core_latest_posts_excerpt_length variable
  22   *             to allow the excerpt_length filter respect the Latest Block setting.
  23   */
  24  function block_core_latest_posts_get_excerpt_length() {
  25      global $block_core_latest_posts_excerpt_length;
  26      return $block_core_latest_posts_excerpt_length;
  27  }
  28  
  29  /**
  30   * Renders the `core/latest-posts` block on server.
  31   *
  32   * @param array $attributes The block attributes.
  33   *
  34   * @return string Returns the post content with latest posts added.
  35   */
  36  function render_block_core_latest_posts( $attributes ) {
  37      global $post, $block_core_latest_posts_excerpt_length;
  38  
  39      $args = array(
  40          'posts_per_page'      => $attributes['postsToShow'],
  41          'post_status'         => 'publish',
  42          'order'               => $attributes['order'],
  43          'orderby'             => $attributes['orderBy'],
  44          'ignore_sticky_posts' => true,
  45          'no_found_rows'       => true,
  46      );
  47  
  48      $block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
  49      add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
  50  
  51      if ( ! empty( $attributes['categories'] ) ) {
  52          $args['category__in'] = array_column( $attributes['categories'], 'id' );
  53      }
  54      if ( isset( $attributes['selectedAuthor'] ) ) {
  55          $args['author'] = $attributes['selectedAuthor'];
  56      }
  57  
  58      $query        = new WP_Query();
  59      $recent_posts = $query->query( $args );
  60  
  61      if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) {
  62          update_post_thumbnail_cache( $query );
  63      }
  64  
  65      $list_items_markup = '';
  66  
  67      foreach ( $recent_posts as $post ) {
  68          $post_link = esc_url( get_permalink( $post ) );
  69          $title     = get_the_title( $post );
  70  
  71          if ( ! $title ) {
  72              $title = __( '(no title)' );
  73          }
  74  
  75          $list_items_markup .= '<li>';
  76  
  77          if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
  78              $image_style = '';
  79              if ( isset( $attributes['featuredImageSizeWidth'] ) ) {
  80                  $image_style .= sprintf( 'max-width:%spx;', $attributes['featuredImageSizeWidth'] );
  81              }
  82              if ( isset( $attributes['featuredImageSizeHeight'] ) ) {
  83                  $image_style .= sprintf( 'max-height:%spx;', $attributes['featuredImageSizeHeight'] );
  84              }
  85  
  86              $image_classes = 'wp-block-latest-posts__featured-image';
  87              if ( isset( $attributes['featuredImageAlign'] ) ) {
  88                  $image_classes .= ' align' . $attributes['featuredImageAlign'];
  89              }
  90  
  91              $featured_image = get_the_post_thumbnail(
  92                  $post,
  93                  $attributes['featuredImageSizeSlug'],
  94                  array(
  95                      'style' => esc_attr( $image_style ),
  96                  )
  97              );
  98              if ( $attributes['addLinkToFeaturedImage'] ) {
  99                  $featured_image = sprintf(
 100                      '<a href="%1$s" aria-label="%2$s">%3$s</a>',
 101                      esc_url( $post_link ),
 102                      esc_attr( $title ),
 103                      $featured_image
 104                  );
 105              }
 106              $list_items_markup .= sprintf(
 107                  '<div class="%1$s">%2$s</div>',
 108                  esc_attr( $image_classes ),
 109                  $featured_image
 110              );
 111          }
 112  
 113          $list_items_markup .= sprintf(
 114              '<a class="wp-block-latest-posts__post-title" href="%1$s">%2$s</a>',
 115              esc_url( $post_link ),
 116              $title
 117          );
 118  
 119          if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
 120              $author_display_name = get_the_author_meta( 'display_name', $post->post_author );
 121  
 122              /* translators: byline. %s: current author. */
 123              $byline = sprintf( __( 'by %s' ), $author_display_name );
 124  
 125              if ( ! empty( $author_display_name ) ) {
 126                  $list_items_markup .= sprintf(
 127                      '<div class="wp-block-latest-posts__post-author">%1$s</div>',
 128                      $byline
 129                  );
 130              }
 131          }
 132  
 133          if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
 134              $list_items_markup .= sprintf(
 135                  '<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
 136                  esc_attr( get_the_date( 'c', $post ) ),
 137                  get_the_date( '', $post )
 138              );
 139          }
 140  
 141          if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
 142              && isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
 143  
 144              $trimmed_excerpt = get_the_excerpt( $post );
 145  
 146              /*
 147               * Adds a "Read more" link with screen reader text.
 148               * [&hellip;] is the default excerpt ending from wp_trim_excerpt() in Core.
 149               */
 150              if ( str_ends_with( $trimmed_excerpt, ' [&hellip;]' ) ) {
 151                  $excerpt_length = (int) apply_filters( 'excerpt_length', $block_core_latest_posts_excerpt_length );
 152                  if ( $excerpt_length <= $block_core_latest_posts_excerpt_length ) {
 153                      $trimmed_excerpt  = substr( $trimmed_excerpt, 0, -11 );
 154                      $trimmed_excerpt .= sprintf(
 155                          /* translators: 1: A URL to a post, 2: Hidden accessibility text: Post title */
 156                          __( '… <a href="%1$s" rel="noopener noreferrer">Read more<span class="screen-reader-text">: %2$s</span></a>' ),
 157                          esc_url( $post_link ),
 158                          esc_html( $title )
 159                      );
 160                  }
 161              }
 162  
 163              if ( post_password_required( $post ) ) {
 164                  $trimmed_excerpt = __( 'This content is password protected.' );
 165              }
 166  
 167              $list_items_markup .= sprintf(
 168                  '<div class="wp-block-latest-posts__post-excerpt">%1$s</div>',
 169                  $trimmed_excerpt
 170              );
 171          }
 172  
 173          if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
 174              && isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
 175  
 176              $post_content = html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) );
 177  
 178              if ( post_password_required( $post ) ) {
 179                  $post_content = __( 'This content is password protected.' );
 180              }
 181  
 182              $list_items_markup .= sprintf(
 183                  '<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
 184                  wp_kses_post( $post_content )
 185              );
 186          }
 187  
 188          $list_items_markup .= "</li>\n";
 189      }
 190  
 191      remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
 192  
 193      $classes = array( 'wp-block-latest-posts__list' );
 194      if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
 195          $classes[] = 'is-grid';
 196      }
 197      if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
 198          $classes[] = 'columns-' . $attributes['columns'];
 199      }
 200      if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
 201          $classes[] = 'has-dates';
 202      }
 203      if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
 204          $classes[] = 'has-author';
 205      }
 206      if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
 207          $classes[] = 'has-link-color';
 208      }
 209  
 210      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
 211  
 212      return sprintf(
 213          '<ul %1$s>%2$s</ul>',
 214          $wrapper_attributes,
 215          $list_items_markup
 216      );
 217  }
 218  
 219  /**
 220   * Registers the `core/latest-posts` block on server.
 221   */
 222  function register_block_core_latest_posts() {
 223      register_block_type_from_metadata(
 224          __DIR__ . '/latest-posts',
 225          array(
 226              'render_callback' => 'render_block_core_latest_posts',
 227          )
 228      );
 229  }
 230  add_action( 'init', 'register_block_core_latest_posts' );
 231  
 232  /**
 233   * Handles outdated versions of the `core/latest-posts` block by converting
 234   * attribute `categories` from a numeric string to an array with key `id`.
 235   *
 236   * This is done to accommodate the changes introduced in #20781 that sought to
 237   * add support for multiple categories to the block. However, given that this
 238   * block is dynamic, the usual provisions for block migration are insufficient,
 239   * as they only act when a block is loaded in the editor.
 240   *
 241   * TODO: Remove when and if the bottom client-side deprecation for this block
 242   * is removed.
 243   *
 244   * @param array $block A single parsed block object.
 245   *
 246   * @return array The migrated block object.
 247   */
 248  function block_core_latest_posts_migrate_categories( $block ) {
 249      if (
 250          'core/latest-posts' === $block['blockName'] &&
 251          ! empty( $block['attrs']['categories'] ) &&
 252          is_string( $block['attrs']['categories'] )
 253      ) {
 254          $block['attrs']['categories'] = array(
 255              array( 'id' => absint( $block['attrs']['categories'] ) ),
 256          );
 257      }
 258  
 259      return $block;
 260  }
 261  add_filter( 'render_block_data', 'block_core_latest_posts_migrate_categories' );


Generated : Mon Mar 18 08:20:01 2024 Cross-referenced by PHPXref