| 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 |
* @since 5.2.0 |
| 12 |
* |
| 13 |
* @param array $attributes The block attributes. |
| 14 |
* |
| 15 |
* @return string Returns the block content with received rss items. |
| 16 |
*/ |
| 17 |
function gutenberg_render_block_core_rss( $attributes ) { |
| 18 |
$feed_url = $attributes['feedURL'] ?? null; |
| 19 |
if ( ! is_string( $feed_url ) ) { |
| 20 |
return ''; |
| 21 |
} |
| 22 |
|
| 23 |
if ( in_array( untrailingslashit( $feed_url ), array( site_url(), home_url() ), true ) ) { |
| 24 |
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>'; |
| 25 |
} |
| 26 |
|
| 27 |
$rss = fetch_feed( $feed_url ); |
| 28 |
|
| 29 |
if ( is_wp_error( $rss ) ) { |
| 30 |
return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</div></div>'; |
| 31 |
} |
| 32 |
|
| 33 |
if ( ! $rss->get_item_quantity() ) { |
| 34 |
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>'; |
| 35 |
} |
| 36 |
|
| 37 |
$rss_items = $rss->get_items( 0, $attributes['itemsToShow'] ); |
| 38 |
$list_items = ''; |
| 39 |
|
| 40 |
$open_in_new_tab = ! empty( $attributes['openInNewTab'] ); |
| 41 |
$rel_attr = $attributes['rel'] ?? null; |
| 42 |
$rel = is_string( $rel_attr ) && '' !== $rel_attr ? trim( $rel_attr ) : ''; |
| 43 |
|
| 44 |
$link_attributes = ''; |
| 45 |
|
| 46 |
if ( $open_in_new_tab ) { |
| 47 |
$link_attributes .= ' target="_blank"'; |
| 48 |
} |
| 49 |
|
| 50 |
if ( '' !== $rel ) { |
| 51 |
$link_attributes .= ' rel="' . esc_attr( $rel ) . '"'; |
| 52 |
} |
| 53 |
|
| 54 |
foreach ( $rss_items as $item ) { |
| 55 |
$title = esc_html( trim( strip_tags( html_entity_decode( $item->get_title() ) ) ) ); |
| 56 |
|
| 57 |
if ( empty( $title ) ) { |
| 58 |
$title = __( '(no title)' ); |
| 59 |
} |
| 60 |
$link = $item->get_link(); |
| 61 |
$link = esc_url( $link ); |
| 62 |
|
| 63 |
if ( $link ) { |
| 64 |
$title = "<a href='{$link}'{$link_attributes}>{$title}</a>"; |
| 65 |
} |
| 66 |
$title = "<div class='wp-block-rss__item-title'>{$title}</div>"; |
| 67 |
|
| 68 |
$date_markup = ''; |
| 69 |
if ( ! empty( $attributes['displayDate'] ) ) { |
| 70 |
$timestamp = $item->get_date( 'U' ); |
| 71 |
|
| 72 |
if ( $timestamp ) { |
| 73 |
$gmt_offset = get_option( 'gmt_offset' ); |
| 74 |
$timestamp += (int) ( (float) $gmt_offset * HOUR_IN_SECONDS ); |
| 75 |
|
| 76 |
$date_markup = sprintf( |
| 77 |
'<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ', |
| 78 |
esc_attr( date_i18n( 'c', $timestamp ) ), |
| 79 |
esc_html( date_i18n( get_option( 'date_format' ), $timestamp ) ) |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$author = ''; |
| 85 |
if ( $attributes['displayAuthor'] ) { |
| 86 |
$author = $item->get_author(); |
| 87 |
if ( is_object( $author ) ) { |
| 88 |
$author = $author->get_name(); |
| 89 |
if ( ! empty( $author ) ) { |
| 90 |
$author = '<span class="wp-block-rss__item-author">' . sprintf( |
| 91 |
/* translators: byline. %s: author. */ |
| 92 |
__( 'by %s' ), |
| 93 |
esc_html( strip_tags( $author ) ) |
| 94 |
) . '</span>'; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$excerpt = ''; |
| 100 |
$description = $item->get_description(); |
| 101 |
if ( $attributes['displayExcerpt'] && ! empty( $description ) ) { |
| 102 |
$excerpt = html_entity_decode( $description, ENT_QUOTES, get_option( 'blog_charset' ) ); |
| 103 |
$excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); |
| 104 |
|
| 105 |
// Change existing [...] to […]. |
| 106 |
if ( str_ends_with( $excerpt, '[...]' ) ) { |
| 107 |
$excerpt = substr( $excerpt, 0, -5 ) . '[…]'; |
| 108 |
} |
| 109 |
|
| 110 |
$excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>'; |
| 111 |
} |
| 112 |
|
| 113 |
$list_items .= "<li class='wp-block-rss__item'>{$title}{$date_markup}{$author}{$excerpt}</li>"; |
| 114 |
} |
| 115 |
|
| 116 |
$classnames = array(); |
| 117 |
if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) { |
| 118 |
$classnames[] = 'is-grid'; |
| 119 |
} |
| 120 |
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) { |
| 121 |
$classnames[] = 'columns-' . $attributes['columns']; |
| 122 |
} |
| 123 |
if ( $attributes['displayDate'] ) { |
| 124 |
$classnames[] = 'has-dates'; |
| 125 |
} |
| 126 |
if ( $attributes['displayAuthor'] ) { |
| 127 |
$classnames[] = 'has-authors'; |
| 128 |
} |
| 129 |
if ( $attributes['displayExcerpt'] ) { |
| 130 |
$classnames[] = 'has-excerpts'; |
| 131 |
} |
| 132 |
|
| 133 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) ); |
| 134 |
|
| 135 |
return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Registers the `core/rss` block on server. |
| 140 |
* |
| 141 |
* @since 5.2.0 |
| 142 |
*/ |
| 143 |
function gutenberg_register_block_core_rss() { |
| 144 |
register_block_type_from_metadata( |
| 145 |
__DIR__ . '/rss', |
| 146 |
array( |
| 147 |
'render_callback' => 'gutenberg_render_block_core_rss', |
| 148 |
) |
| 149 |
); |
| 150 |
} |
| 151 |
add_action( 'init', 'gutenberg_register_block_core_rss', 20 ); |
| 152 |
|