rss.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. if ( in_array( untrailingslashit( $attributes['feedURL'] ), array( site_url(), home_url() ), true ) ) {
  16. 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>';
  17. }
  18. $rss = fetch_feed( $attributes['feedURL'] );
  19. if ( is_wp_error( $rss ) ) {
  20. return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</div></div>';
  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. $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. esc_attr( date_i18n( get_option( 'c' ), $date ) ),
  45. esc_attr( 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">' . sprintf(
  55. /* translators: %s: the author. */
  56. __( 'by %s' ),
  57. esc_html( strip_tags( $author ) )
  58. ) . '</span>';
  59. }
  60. }
  61. $excerpt = '';
  62. if ( $attributes['displayExcerpt'] ) {
  63. $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
  64. $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' [&hellip;]' ) );
  65. // Change existing [...] to [&hellip;].
  66. if ( '[...]' === substr( $excerpt, -5 ) ) {
  67. $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
  68. }
  69. $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
  70. }
  71. $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>";
  72. }
  73. $classnames = array();
  74. if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
  75. $classnames[] = 'is-grid';
  76. }
  77. if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
  78. $classnames[] = 'columns-' . $attributes['columns'];
  79. }
  80. if ( $attributes['displayDate'] ) {
  81. $classnames[] = 'has-dates';
  82. }
  83. if ( $attributes['displayAuthor'] ) {
  84. $classnames[] = 'has-authors';
  85. }
  86. if ( $attributes['displayExcerpt'] ) {
  87. $classnames[] = 'has-excerpts';
  88. }
  89. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
  90. return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items );
  91. }
  92. /**
  93. * Registers the `core/rss` block on server.
  94. */
  95. function register_block_core_rss() {
  96. register_block_type_from_metadata(
  97. __DIR__ . '/rss',
  98. array(
  99. 'render_callback' => 'render_block_core_rss',
  100. )
  101. );
  102. }
  103. add_action( 'init', 'register_block_core_rss' );