[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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          // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks.
  24          $rss->__destruct();
  25          unset( $rss );
  26  
  27          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>';
  28      }
  29  
  30      $rss_items  = $rss->get_items( 0, $attributes['itemsToShow'] );
  31      $list_items = '';
  32      foreach ( $rss_items as $item ) {
  33          $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
  34          if ( empty( $title ) ) {
  35              $title = __( '(no title)' );
  36          }
  37          $link = $item->get_link();
  38          $link = esc_url( $link );
  39          if ( $link ) {
  40              $title = "<a href='{$link}'>{$title}</a>";
  41          }
  42          $title = "<div class='wp-block-rss__item-title'>{$title}</div>";
  43  
  44          $date = '';
  45          if ( $attributes['displayDate'] ) {
  46              $date = $item->get_date( 'U' );
  47  
  48              if ( $date ) {
  49                  $date = sprintf(
  50                      '<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ',
  51                      date_i18n( get_option( 'c' ), $date ),
  52                      date_i18n( get_option( 'date_format' ), $date )
  53                  );
  54              }
  55          }
  56  
  57          $author = '';
  58          if ( $attributes['displayAuthor'] ) {
  59              $author = $item->get_author();
  60              if ( is_object( $author ) ) {
  61                  $author = $author->get_name();
  62                  $author = '<span class="wp-block-rss__item-author">' . __( 'by' ) . ' ' . esc_html( strip_tags( $author ) ) . '</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      $list_items_markup = "<ul class='{$class}'>{$list_items}</ul>";
 100  
 101      // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks.
 102      $rss->__destruct();
 103      unset( $rss );
 104  
 105      return $list_items_markup;
 106  }
 107  
 108  /**
 109   * Registers the `core/rss` block on server.
 110   */
 111  function register_block_core_rss() {
 112      register_block_type(
 113          'core/rss',
 114          array(
 115              'attributes'      => array(
 116                  'align'          => array(
 117                      'type' => 'string',
 118                      'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
 119                  ),
 120                  'className'      => array(
 121                      'type' => 'string',
 122                  ),
 123                  'columns'        => array(
 124                      'type'    => 'number',
 125                      'default' => 2,
 126                  ),
 127                  'blockLayout'    => array(
 128                      'type'    => 'string',
 129                      'default' => 'list',
 130                  ),
 131                  'feedURL'        => array(
 132                      'type'    => 'string',
 133                      'default' => '',
 134                  ),
 135                  'itemsToShow'    => array(
 136                      'type'    => 'number',
 137                      'default' => 5,
 138                  ),
 139                  'displayExcerpt' => array(
 140                      'type'    => 'boolean',
 141                      'default' => false,
 142                  ),
 143                  'displayAuthor'  => array(
 144                      'type'    => 'boolean',
 145                      'default' => false,
 146                  ),
 147                  'displayDate'    => array(
 148                      'type'    => 'boolean',
 149                      'default' => false,
 150                  ),
 151                  'excerptLength'  => array(
 152                      'type'    => 'number',
 153                      'default' => 55,
 154                  ),
 155              ),
 156              'render_callback' => 'render_block_core_rss',
 157          )
 158      );
 159  }
 160  add_action( 'init', 'register_block_core_rss' );


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7