rss.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/rss` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/rss` block on server.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Returns the block content with received rss items.
  13. */
  14. function render_block_core_rss( $attributes ) {
  15. $rss = fetch_feed( $attributes['feedURL'] );
  16. if ( is_wp_error( $rss ) ) {
  17. return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</div></div>';
  18. }
  19. if ( ! $rss->get_item_quantity() ) {
  20. // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks.
  21. $rss->__destruct();
  22. unset( $rss );
  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. $rss_items = $rss->get_items( 0, $attributes['itemsToShow'] );
  26. $list_items = '';
  27. foreach ( $rss_items as $item ) {
  28. $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
  29. if ( empty( $title ) ) {
  30. $title = __( '(no title)' );
  31. }
  32. $link = $item->get_link();
  33. $link = esc_url( $link );
  34. if ( $link ) {
  35. $title = "<a href='{$link}'>{$title}</a>";
  36. }
  37. $title = "<div class='wp-block-rss__item-title'>{$title}</div>";
  38. $date = '';
  39. if ( $attributes['displayDate'] ) {
  40. $date = $item->get_date( 'U' );
  41. if ( $date ) {
  42. $date = sprintf(
  43. '<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ',
  44. date_i18n( get_option( 'c' ), $date ),
  45. date_i18n( get_option( 'date_format' ), $date )
  46. );
  47. }
  48. }
  49. $author = '';
  50. if ( $attributes['displayAuthor'] ) {
  51. $author = $item->get_author();
  52. if ( is_object( $author ) ) {
  53. $author = $author->get_name();
  54. $author = '<span class="wp-block-rss__item-author">' . __( 'by' ) . ' ' . esc_html( strip_tags( $author ) ) . '</span>';
  55. }
  56. }
  57. $excerpt = '';
  58. if ( $attributes['displayExcerpt'] ) {
  59. $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
  60. $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' [&hellip;]' ) );
  61. // Change existing [...] to [&hellip;].
  62. if ( '[...]' === substr( $excerpt, -5 ) ) {
  63. $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
  64. }
  65. $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
  66. }
  67. $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>";
  68. }
  69. $class = 'wp-block-rss';
  70. if ( isset( $attributes['align'] ) ) {
  71. $class .= ' align' . $attributes['align'];
  72. }
  73. if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
  74. $class .= ' is-grid';
  75. }
  76. if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
  77. $class .= ' columns-' . $attributes['columns'];
  78. }
  79. if ( isset( $attributes['className'] ) ) {
  80. $class .= ' ' . $attributes['className'];
  81. }
  82. $list_items_markup = "<ul class='{$class}'>{$list_items}</ul>";
  83. // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks.
  84. $rss->__destruct();
  85. unset( $rss );
  86. return $list_items_markup;
  87. }
  88. /**
  89. * Registers the `core/rss` block on server.
  90. */
  91. function register_block_core_rss() {
  92. register_block_type(
  93. 'core/rss',
  94. array(
  95. 'attributes' => array(
  96. 'align' => array(
  97. 'type' => 'string',
  98. 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
  99. ),
  100. 'className' => array(
  101. 'type' => 'string',
  102. ),
  103. 'columns' => array(
  104. 'type' => 'number',
  105. 'default' => 2,
  106. ),
  107. 'blockLayout' => array(
  108. 'type' => 'string',
  109. 'default' => 'list',
  110. ),
  111. 'feedURL' => array(
  112. 'type' => 'string',
  113. 'default' => '',
  114. ),
  115. 'itemsToShow' => array(
  116. 'type' => 'number',
  117. 'default' => 5,
  118. ),
  119. 'displayExcerpt' => array(
  120. 'type' => 'boolean',
  121. 'default' => false,
  122. ),
  123. 'displayAuthor' => array(
  124. 'type' => 'boolean',
  125. 'default' => false,
  126. ),
  127. 'displayDate' => array(
  128. 'type' => 'boolean',
  129. 'default' => false,
  130. ),
  131. 'excerptLength' => array(
  132. 'type' => 'number',
  133. 'default' => 55,
  134. ),
  135. ),
  136. 'render_callback' => 'render_block_core_rss',
  137. )
  138. );
  139. }
  140. add_action( 'init', 'register_block_core_rss' );