[ Index ]

PHP Cross Reference of WordPress

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   * @param array $attributes The block attributes.
  12   *
  13   * @return string Returns the block content with received rss items.
  14   */
  15  function render_block_core_rss( $attributes ) {
  16      $rss = fetch_feed( $attributes['feedURL'] );
  17  
  18      if ( is_wp_error( $rss ) ) {
  19          return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</div></div>';
  20      }
  21  
  22      if ( ! $rss->get_item_quantity() ) {
  23          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>';
  24      }
  25  
  26      $rss_items  = $rss->get_items( 0, $attributes['itemsToShow'] );
  27      $list_items = '';
  28      foreach ( $rss_items as $item ) {
  29          $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
  30          if ( empty( $title ) ) {
  31              $title = __( '(no title)' );
  32          }
  33          $link = $item->get_link();
  34          $link = esc_url( $link );
  35          if ( $link ) {
  36              $title = "<a href='{$link}'>{$title}</a>";
  37          }
  38          $title = "<div class='wp-block-rss__item-title'>{$title}</div>";
  39  
  40          $date = '';
  41          if ( $attributes['displayDate'] ) {
  42              $date = $item->get_date( 'U' );
  43  
  44              if ( $date ) {
  45                  $date = sprintf(
  46                      '<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ',
  47                      date_i18n( get_option( 'c' ), $date ),
  48                      date_i18n( get_option( 'date_format' ), $date )
  49                  );
  50              }
  51          }
  52  
  53          $author = '';
  54          if ( $attributes['displayAuthor'] ) {
  55              $author = $item->get_author();
  56              if ( is_object( $author ) ) {
  57                  $author = $author->get_name();
  58                  $author = '<span class="wp-block-rss__item-author">' . sprintf(
  59                      /* translators: %s: the author. */
  60                      __( 'by %s' ),
  61                      esc_html( strip_tags( $author ) )
  62                  ) . '</span>';
  63              }
  64          }
  65  
  66          $excerpt = '';
  67          if ( $attributes['displayExcerpt'] ) {
  68              $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
  69              $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' [&hellip;]' ) );
  70  
  71              // Change existing [...] to [&hellip;].
  72              if ( '[...]' === substr( $excerpt, -5 ) ) {
  73                  $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
  74              }
  75  
  76              $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
  77          }
  78  
  79          $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>";
  80      }
  81  
  82      $class = 'wp-block-rss';
  83      if ( isset( $attributes['align'] ) ) {
  84          $class .= ' align' . $attributes['align'];
  85      }
  86  
  87      if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
  88          $class .= ' is-grid';
  89      }
  90  
  91      if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
  92          $class .= ' columns-' . $attributes['columns'];
  93      }
  94  
  95      if ( isset( $attributes['className'] ) ) {
  96          $class .= ' ' . $attributes['className'];
  97      }
  98  
  99      return sprintf( '<ul class="%s">%s</ul>', esc_attr( $class ), $list_items );
 100  }
 101  
 102  /**
 103   * Registers the `core/rss` block on server.
 104   */
 105  function register_block_core_rss() {
 106      register_block_type_from_metadata(
 107          __DIR__ . '/rss',
 108          array(
 109              'render_callback' => 'render_block_core_rss',
 110          )
 111      );
 112  }
 113  add_action( 'init', 'register_block_core_rss' );


Generated: Sun Sep 20 01:00:03 2020 Cross-referenced by PHPXref 0.7.1