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 */
15function 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		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
26	$rss_items  = $rss->get_items( 0, $attributes['itemsToShow'] );
27	$list_items = '';
28	foreach ( $rss_items as $item ) {
29		$title = esc_html( trim( strip_tags( $item->get_title() ) ) );
30		if ( empty( $title ) ) {
31			$title = __( '(no title)' );
32		}
33		$link = $item->get_link();
34		$link = esc_url( $link );
35		if ( $link ) {
36			$title = "<a href='{$link}'>{$title}</a>";
37		}
38		$title = "<div class='wp-block-rss__item-title'>{$title}</div>";
39
40		$date = '';
41		if ( $attributes['displayDate'] ) {
42			$date = $item->get_date( 'U' );
43
44			if ( $date ) {
45				$date = sprintf(
46					'<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ',
47					date_i18n( get_option( 'c' ), $date ),
48					date_i18n( get_option( 'date_format' ), $date )
49				);
50			}
51		}
52
53		$author = '';
54		if ( $attributes['displayAuthor'] ) {
55			$author = $item->get_author();
56			if ( is_object( $author ) ) {
57				$author = $author->get_name();
58				$author = '<span class="wp-block-rss__item-author">' . sprintf(
59					/* translators: %s: the author. */
60					__( 'by %s' ),
61					esc_html( strip_tags( $author ) )
62				) . '</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	$classnames = array();
83	if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
84		$classnames[] = 'is-grid';
85	}
86
87	if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
88		$classnames[] = 'columns-' . $attributes['columns'];
89	}
90	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
91
92	return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items );
93}
94
95/**
96 * Registers the `core/rss` block on server.
97 */
98function register_block_core_rss() {
99	register_block_type_from_metadata(
100		__DIR__ . '/rss',
101		array(
102			'render_callback' => 'render_block_core_rss',
103		)
104	);
105}
106add_action( 'init', 'register_block_core_rss' );
107