[ 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 $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'] = $attributes['categories'];
  51      }
  52  
  53      $recent_posts = get_posts( $args );
  54  
  55      $list_items_markup = '';
  56  
  57      foreach ( $recent_posts as $post ) {
  58          $list_items_markup .= '<li>';
  59  
  60          if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
  61              $image_style = '';
  62              if ( isset( $attributes['featuredImageSizeWidth'] ) ) {
  63                  $image_style .= sprintf( 'max-width:%spx;', $attributes['featuredImageSizeWidth'] );
  64              }
  65              if ( isset( $attributes['featuredImageSizeHeight'] ) ) {
  66                  $image_style .= sprintf( 'max-height:%spx;', $attributes['featuredImageSizeHeight'] );
  67              }
  68  
  69              $image_classes = 'wp-block-latest-posts__featured-image';
  70              if ( isset( $attributes['featuredImageAlign'] ) ) {
  71                  $image_classes .= ' align' . $attributes['featuredImageAlign'];
  72              }
  73  
  74              $list_items_markup .= sprintf(
  75                  '<div class="%1$s">%2$s</div>',
  76                  $image_classes,
  77                  get_the_post_thumbnail(
  78                      $post,
  79                      $attributes['featuredImageSizeSlug'],
  80                      array(
  81                          'style' => $image_style,
  82                      )
  83                  )
  84              );
  85          }
  86  
  87          $title = get_the_title( $post );
  88          if ( ! $title ) {
  89              $title = __( '(no title)' );
  90          }
  91          $list_items_markup .= sprintf(
  92              '<a href="%1$s">%2$s</a>',
  93              esc_url( get_permalink( $post ) ),
  94              $title
  95          );
  96  
  97          if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
  98              $list_items_markup .= sprintf(
  99                  '<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
 100                  esc_attr( get_the_date( 'c', $post ) ),
 101                  esc_html( get_the_date( '', $post ) )
 102              );
 103          }
 104  
 105          if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
 106              && isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
 107  
 108              $trimmed_excerpt = get_the_excerpt( $post );
 109  
 110              $list_items_markup .= sprintf(
 111                  '<div class="wp-block-latest-posts__post-excerpt">%1$s',
 112                  $trimmed_excerpt
 113              );
 114  
 115              if ( strpos( $trimmed_excerpt, ' &hellip; ' ) !== false ) {
 116                  $list_items_markup .= sprintf(
 117                      '<a href="%1$s">%2$s</a></div>',
 118                      esc_url( get_permalink( $post ) ),
 119                      __( 'Read more' )
 120                  );
 121              } else {
 122                  $list_items_markup .= sprintf(
 123                      '</div>'
 124                  );
 125              }
 126          }
 127  
 128          if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
 129              && isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
 130              $list_items_markup .= sprintf(
 131                  '<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
 132                  wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) )
 133              );
 134          }
 135  
 136          $list_items_markup .= "</li>\n";
 137      }
 138  
 139      remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
 140  
 141      $class = 'wp-block-latest-posts wp-block-latest-posts__list';
 142      if ( isset( $attributes['align'] ) ) {
 143          $class .= ' align' . $attributes['align'];
 144      }
 145  
 146      if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
 147          $class .= ' is-grid';
 148      }
 149  
 150      if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
 151          $class .= ' columns-' . $attributes['columns'];
 152      }
 153  
 154      if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
 155          $class .= ' has-dates';
 156      }
 157  
 158      if ( isset( $attributes['className'] ) ) {
 159          $class .= ' ' . $attributes['className'];
 160      }
 161  
 162      return sprintf(
 163          '<ul class="%1$s">%2$s</ul>',
 164          esc_attr( $class ),
 165          $list_items_markup
 166      );
 167  }
 168  
 169  /**
 170   * Registers the `core/latest-posts` block on server.
 171   */
 172  function register_block_core_latest_posts() {
 173      register_block_type(
 174          'core/latest-posts',
 175          array(
 176              'attributes'      => array(
 177                  'align'                   => array(
 178                      'type' => 'string',
 179                      'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
 180                  ),
 181                  'className'               => array(
 182                      'type' => 'string',
 183                  ),
 184                  'categories'              => array(
 185                      'type' => 'string',
 186                  ),
 187                  'postsToShow'             => array(
 188                      'type'    => 'number',
 189                      'default' => 5,
 190                  ),
 191                  'displayPostContent'      => array(
 192                      'type'    => 'boolean',
 193                      'default' => false,
 194                  ),
 195                  'displayPostContentRadio' => array(
 196                      'type'    => 'string',
 197                      'default' => 'excerpt',
 198                  ),
 199                  'excerptLength'           => array(
 200                      'type'    => 'number',
 201                      'default' => 55,
 202                  ),
 203                  'displayPostDate'         => array(
 204                      'type'    => 'boolean',
 205                      'default' => false,
 206                  ),
 207                  'postLayout'              => array(
 208                      'type'    => 'string',
 209                      'default' => 'list',
 210                  ),
 211                  'columns'                 => array(
 212                      'type'    => 'number',
 213                      'default' => 3,
 214                  ),
 215                  'order'                   => array(
 216                      'type'    => 'string',
 217                      'default' => 'desc',
 218                  ),
 219                  'orderBy'                 => array(
 220                      'type'    => 'string',
 221                      'default' => 'date',
 222                  ),
 223                  'displayFeaturedImage'    => array(
 224                      'type'    => 'boolean',
 225                      'default' => false,
 226                  ),
 227                  'featuredImageAlign'      => array(
 228                      'type' => 'string',
 229                      'enum' => array( 'left', 'center', 'right' ),
 230                  ),
 231                  'featuredImageSizeSlug'   => array(
 232                      'type'    => 'string',
 233                      'default' => 'thumbnail',
 234                  ),
 235                  'featuredImageSizeWidth'  => array(
 236                      'type'    => 'number',
 237                      'default' => null,
 238                  ),
 239                  'featuredImageSizeHeight' => array(
 240                      'type'    => 'number',
 241                      'default' => null,
 242                  ),
 243              ),
 244              'render_callback' => 'render_block_core_latest_posts',
 245          )
 246      );
 247  }
 248  add_action( 'init', 'register_block_core_latest_posts' );


Generated: Thu Apr 2 01:00:03 2020 Cross-referenced by PHPXref 0.7.1