[ 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   * Renders the `core/latest-posts` block on server.
  10   *
  11   * @param array $attributes The block attributes.
  12   *
  13   * @return string Returns the post content with latest posts added.
  14   */
  15  function render_block_core_latest_posts( $attributes ) {
  16      $args = array(
  17          'posts_per_page'   => $attributes['postsToShow'],
  18          'post_status'      => 'publish',
  19          'order'            => $attributes['order'],
  20          'orderby'          => $attributes['orderBy'],
  21          'suppress_filters' => false,
  22      );
  23  
  24      if ( isset( $attributes['categories'] ) ) {
  25          $args['category'] = $attributes['categories'];
  26      }
  27  
  28      $recent_posts = get_posts( $args );
  29  
  30      $list_items_markup = '';
  31  
  32      foreach ( $recent_posts as $post ) {
  33          $list_items_markup .= '<li>';
  34  
  35          if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
  36              $image_style = '';
  37              if ( isset( $attributes['featuredImageSizeWidth'] ) ) {
  38                  $image_style .= sprintf( 'max-width:%spx;', $attributes['featuredImageSizeWidth'] );
  39              }
  40              if ( isset( $attributes['featuredImageSizeHeight'] ) ) {
  41                  $image_style .= sprintf( 'max-height:%spx;', $attributes['featuredImageSizeHeight'] );
  42              }
  43  
  44              $image_classes = 'wp-block-latest-posts__featured-image';
  45              if ( isset( $attributes['featuredImageAlign'] ) ) {
  46                  $image_classes .= ' align' . $attributes['featuredImageAlign'];
  47              }
  48  
  49              $list_items_markup .= sprintf(
  50                  '<div class="%1$s">%2$s</div>',
  51                  $image_classes,
  52                  get_the_post_thumbnail(
  53                      $post,
  54                      $attributes['featuredImageSizeSlug'],
  55                      array(
  56                          'style' => $image_style,
  57                      )
  58                  )
  59              );
  60          }
  61  
  62          $title = get_the_title( $post );
  63          if ( ! $title ) {
  64              $title = __( '(no title)' );
  65          }
  66          $list_items_markup .= sprintf(
  67              '<a href="%1$s">%2$s</a>',
  68              esc_url( get_permalink( $post ) ),
  69              $title
  70          );
  71  
  72          if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
  73              $list_items_markup .= sprintf(
  74                  '<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
  75                  esc_attr( get_the_date( 'c', $post ) ),
  76                  esc_html( get_the_date( '', $post ) )
  77              );
  78          }
  79  
  80          if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
  81              && isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
  82  
  83              $trimmed_excerpt = get_the_excerpt( $post );
  84  
  85              $list_items_markup .= sprintf(
  86                  '<div class="wp-block-latest-posts__post-excerpt">%1$s',
  87                  $trimmed_excerpt
  88              );
  89  
  90              if ( strpos( $trimmed_excerpt, ' &hellip; ' ) !== false ) {
  91                  $list_items_markup .= sprintf(
  92                      '<a href="%1$s">%2$s</a></div>',
  93                      esc_url( get_permalink( $post ) ),
  94                      __( 'Read more' )
  95                  );
  96              } else {
  97                  $list_items_markup .= sprintf(
  98                      '</div>'
  99                  );
 100              }
 101          }
 102  
 103          if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
 104              && isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
 105              $list_items_markup .= sprintf(
 106                  '<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
 107                  wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) )
 108              );
 109          }
 110  
 111          $list_items_markup .= "</li>\n";
 112      }
 113  
 114      $class = 'wp-block-latest-posts wp-block-latest-posts__list';
 115      if ( isset( $attributes['align'] ) ) {
 116          $class .= ' align' . $attributes['align'];
 117      }
 118  
 119      if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
 120          $class .= ' is-grid';
 121      }
 122  
 123      if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
 124          $class .= ' columns-' . $attributes['columns'];
 125      }
 126  
 127      if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
 128          $class .= ' has-dates';
 129      }
 130  
 131      if ( isset( $attributes['className'] ) ) {
 132          $class .= ' ' . $attributes['className'];
 133      }
 134  
 135      return sprintf(
 136          '<ul class="%1$s">%2$s</ul>',
 137          esc_attr( $class ),
 138          $list_items_markup
 139      );
 140  }
 141  
 142  /**
 143   * Registers the `core/latest-posts` block on server.
 144   */
 145  function register_block_core_latest_posts() {
 146      register_block_type(
 147          'core/latest-posts',
 148          array(
 149              'attributes'      => array(
 150                  'align'                   => array(
 151                      'type' => 'string',
 152                      'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
 153                  ),
 154                  'className'               => array(
 155                      'type' => 'string',
 156                  ),
 157                  'categories'              => array(
 158                      'type' => 'string',
 159                  ),
 160                  'postsToShow'             => array(
 161                      'type'    => 'number',
 162                      'default' => 5,
 163                  ),
 164                  'displayPostContent'      => array(
 165                      'type'    => 'boolean',
 166                      'default' => false,
 167                  ),
 168                  'displayPostContentRadio' => array(
 169                      'type'    => 'string',
 170                      'default' => 'excerpt',
 171                  ),
 172                  'excerptLength'           => array(
 173                      'type'    => 'number',
 174                      'default' => 55,
 175                  ),
 176                  'displayPostDate'         => array(
 177                      'type'    => 'boolean',
 178                      'default' => false,
 179                  ),
 180                  'postLayout'              => array(
 181                      'type'    => 'string',
 182                      'default' => 'list',
 183                  ),
 184                  'columns'                 => array(
 185                      'type'    => 'number',
 186                      'default' => 3,
 187                  ),
 188                  'order'                   => array(
 189                      'type'    => 'string',
 190                      'default' => 'desc',
 191                  ),
 192                  'orderBy'                 => array(
 193                      'type'    => 'string',
 194                      'default' => 'date',
 195                  ),
 196                  'displayFeaturedImage'    => array(
 197                      'type'    => 'boolean',
 198                      'default' => false,
 199                  ),
 200                  'featuredImageAlign'      => array(
 201                      'type' => 'string',
 202                      'enum' => array( 'left', 'center', 'right' ),
 203                  ),
 204                  'featuredImageSizeSlug'   => array(
 205                      'type'    => 'string',
 206                      'default' => 'thumbnail',
 207                  ),
 208                  'featuredImageSizeWidth'  => array(
 209                      'type'    => 'number',
 210                      'default' => null,
 211                  ),
 212                  'featuredImageSizeHeight' => array(
 213                      'type'    => 'number',
 214                      'default' => null,
 215                  ),
 216              ),
 217              'render_callback' => 'render_block_core_latest_posts',
 218          )
 219      );
 220  }
 221  add_action( 'init', 'register_block_core_latest_posts' );


Generated: Thu Feb 20 01:00:03 2020 Cross-referenced by PHPXref 0.7.1