[ Index ]

PHP Cross Reference of WordPress

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


Generated: Sat Nov 28 01:00:03 2020 Cross-referenced by PHPXref 0.7.1