[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> rss.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/rss` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/rss` block on server.
  10   *
  11   * @since 5.2.0
  12   *
  13   * @param array $attributes The block attributes.
  14   *
  15   * @return string Returns the block content with received rss items.
  16   */
  17  function render_block_core_rss( $attributes ) {
  18      if ( in_array( untrailingslashit( $attributes['feedURL'] ), array( site_url(), home_url() ), true ) ) {
  19          return '<div class="components-placeholder"><div class="notice notice-error">' . __( 'Adding an RSS feed to this site’s homepage is not supported, as it could lead to a loop that slows down your site. Try using another block, like the <strong>Latest Posts</strong> block, to list posts from the site.' ) . '</div></div>';
  20      }
  21  
  22      $rss = fetch_feed( $attributes['feedURL'] );
  23  
  24      if ( is_wp_error( $rss ) ) {
  25          return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</div></div>';
  26      }
  27  
  28      if ( ! $rss->get_item_quantity() ) {
  29          return '<div class="components-placeholder"><div class="notice notice-error">' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</div></div>';
  30      }
  31  
  32      $rss_items  = $rss->get_items( 0, $attributes['itemsToShow'] );
  33      $list_items = '';
  34  
  35      $open_in_new_tab = ! empty( $attributes['openInNewTab'] );
  36      $rel             = ! empty( $attributes['rel'] ) ? trim( $attributes['rel'] ) : '';
  37  
  38      $link_attributes = '';
  39  
  40      if ( $open_in_new_tab ) {
  41          $link_attributes .= ' target="_blank"';
  42      }
  43  
  44      if ( '' !== $rel ) {
  45          $link_attributes .= ' rel="' . esc_attr( $rel ) . '"';
  46      }
  47  
  48      foreach ( $rss_items as $item ) {
  49          $title = esc_html( trim( strip_tags( html_entity_decode( $item->get_title() ) ) ) );
  50  
  51          if ( empty( $title ) ) {
  52              $title = __( '(no title)' );
  53          }
  54          $link = $item->get_link();
  55          $link = esc_url( $link );
  56  
  57          if ( $link ) {
  58              $title = "<a href='{$link}'{$link_attributes}>{$title}</a>";
  59          }
  60          $title = "<div class='wp-block-rss__item-title'>{$title}</div>";
  61  
  62          $date_markup = '';
  63          if ( ! empty( $attributes['displayDate'] ) ) {
  64              $timestamp = $item->get_date( 'U' );
  65  
  66              if ( $timestamp ) {
  67                  $gmt_offset = get_option( 'gmt_offset' );
  68                  $timestamp += (int) ( (float) $gmt_offset * HOUR_IN_SECONDS );
  69  
  70                  $date_markup = sprintf(
  71                      '<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ',
  72                      esc_attr( date_i18n( 'c', $timestamp ) ),
  73                      esc_html( date_i18n( get_option( 'date_format' ), $timestamp ) )
  74                  );
  75              }
  76          }
  77  
  78          $author = '';
  79          if ( $attributes['displayAuthor'] ) {
  80              $author = $item->get_author();
  81              if ( is_object( $author ) ) {
  82                  $author = $author->get_name();
  83                  if ( ! empty( $author ) ) {
  84                      $author = '<span class="wp-block-rss__item-author">' . sprintf(
  85                          /* translators: byline. %s: author. */
  86                          __( 'by %s' ),
  87                          esc_html( strip_tags( $author ) )
  88                      ) . '</span>';
  89                  }
  90              }
  91          }
  92  
  93          $excerpt     = '';
  94          $description = $item->get_description();
  95          if ( $attributes['displayExcerpt'] && ! empty( $description ) ) {
  96              $excerpt = html_entity_decode( $description, ENT_QUOTES, get_option( 'blog_charset' ) );
  97              $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' [&hellip;]' ) );
  98  
  99              // Change existing [...] to [&hellip;].
 100              if ( '[...]' === substr( $excerpt, -5 ) ) {
 101                  $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
 102              }
 103  
 104              $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
 105          }
 106  
 107          $list_items .= "<li class='wp-block-rss__item'>{$title}{$date_markup}{$author}{$excerpt}</li>";
 108      }
 109  
 110      $classnames = array();
 111      if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
 112          $classnames[] = 'is-grid';
 113      }
 114      if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
 115          $classnames[] = 'columns-' . $attributes['columns'];
 116      }
 117      if ( $attributes['displayDate'] ) {
 118          $classnames[] = 'has-dates';
 119      }
 120      if ( $attributes['displayAuthor'] ) {
 121          $classnames[] = 'has-authors';
 122      }
 123      if ( $attributes['displayExcerpt'] ) {
 124          $classnames[] = 'has-excerpts';
 125      }
 126  
 127      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
 128  
 129      return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items );
 130  }
 131  
 132  /**
 133   * Registers the `core/rss` block on server.
 134   *
 135   * @since 5.2.0
 136   */
 137  function register_block_core_rss() {
 138      register_block_type_from_metadata(
 139          __DIR__ . '/rss',
 140          array(
 141              'render_callback' => 'render_block_core_rss',
 142          )
 143      );
 144  }
 145  add_action( 'init', 'register_block_core_rss' );


Generated : Thu Nov 6 08:20:07 2025 Cross-referenced by PHPXref