Brak opisu

rss.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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>';
  21. }
  22. $rss_items = $rss->get_items( 0, $attributes['itemsToShow'] );
  23. $list_items = '';
  24. foreach ( $rss_items as $item ) {
  25. $title = esc_html( trim( strip_tags( $item->get_title() ) ) );
  26. if ( empty( $title ) ) {
  27. $title = __( '(no title)' );
  28. }
  29. $link = $item->get_link();
  30. $link = esc_url( $link );
  31. if ( $link ) {
  32. $title = "<a href='{$link}'>{$title}</a>";
  33. }
  34. $title = "<div class='wp-block-rss__item-title'>{$title}</div>";
  35. $date = '';
  36. if ( $attributes['displayDate'] ) {
  37. $date = $item->get_date( 'U' );
  38. if ( $date ) {
  39. $date = sprintf(
  40. '<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ',
  41. date_i18n( get_option( 'c' ), $date ),
  42. date_i18n( get_option( 'date_format' ), $date )
  43. );
  44. }
  45. }
  46. $author = '';
  47. if ( $attributes['displayAuthor'] ) {
  48. $author = $item->get_author();
  49. if ( is_object( $author ) ) {
  50. $author = $author->get_name();
  51. $author = '<span class="wp-block-rss__item-author">' . sprintf(
  52. /* translators: %s: the author. */
  53. __( 'by %s' ),
  54. esc_html( strip_tags( $author ) )
  55. ) . '</span>';
  56. }
  57. }
  58. $excerpt = '';
  59. if ( $attributes['displayExcerpt'] ) {
  60. $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
  61. $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' [&hellip;]' ) );
  62. // Change existing [...] to [&hellip;].
  63. if ( '[...]' === substr( $excerpt, -5 ) ) {
  64. $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
  65. }
  66. $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
  67. }
  68. $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>";
  69. }
  70. $classnames = array();
  71. if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
  72. $classnames[] = 'is-grid';
  73. }
  74. if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
  75. $classnames[] = 'columns-' . $attributes['columns'];
  76. }
  77. $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
  78. return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items );
  79. }
  80. /**
  81. * Registers the `core/rss` block on server.
  82. */
  83. function register_block_core_rss() {
  84. register_block_type_from_metadata(
  85. __DIR__ . '/rss',
  86. array(
  87. 'render_callback' => 'render_block_core_rss',
  88. )
  89. );
  90. }
  91. add_action( 'init', 'register_block_core_rss' );