9
|
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 |
*/ |
|
15 |
function render_block_core_rss( $attributes ) { |
19
|
16 |
if ( in_array( untrailingslashit( $attributes['feedURL'] ), array( site_url(), home_url() ), true ) ) { |
|
17 |
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>'; |
|
18 |
} |
|
19 |
|
9
|
20 |
$rss = fetch_feed( $attributes['feedURL'] ); |
|
21 |
|
|
22 |
if ( is_wp_error( $rss ) ) { |
|
23 |
return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</div></div>'; |
|
24 |
} |
|
25 |
|
|
26 |
if ( ! $rss->get_item_quantity() ) { |
|
27 |
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>'; |
|
28 |
} |
|
29 |
|
|
30 |
$rss_items = $rss->get_items( 0, $attributes['itemsToShow'] ); |
|
31 |
$list_items = ''; |
|
32 |
foreach ( $rss_items as $item ) { |
|
33 |
$title = esc_html( trim( strip_tags( $item->get_title() ) ) ); |
|
34 |
if ( empty( $title ) ) { |
16
|
35 |
$title = __( '(no title)' ); |
9
|
36 |
} |
|
37 |
$link = $item->get_link(); |
|
38 |
$link = esc_url( $link ); |
|
39 |
if ( $link ) { |
|
40 |
$title = "<a href='{$link}'>{$title}</a>"; |
|
41 |
} |
|
42 |
$title = "<div class='wp-block-rss__item-title'>{$title}</div>"; |
|
43 |
|
|
44 |
$date = ''; |
|
45 |
if ( $attributes['displayDate'] ) { |
|
46 |
$date = $item->get_date( 'U' ); |
|
47 |
|
|
48 |
if ( $date ) { |
|
49 |
$date = sprintf( |
|
50 |
'<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ', |
|
51 |
date_i18n( get_option( 'c' ), $date ), |
|
52 |
date_i18n( get_option( 'date_format' ), $date ) |
|
53 |
); |
|
54 |
} |
|
55 |
} |
|
56 |
|
|
57 |
$author = ''; |
|
58 |
if ( $attributes['displayAuthor'] ) { |
|
59 |
$author = $item->get_author(); |
|
60 |
if ( is_object( $author ) ) { |
|
61 |
$author = $author->get_name(); |
16
|
62 |
$author = '<span class="wp-block-rss__item-author">' . sprintf( |
|
63 |
/* translators: %s: the author. */ |
|
64 |
__( 'by %s' ), |
|
65 |
esc_html( strip_tags( $author ) ) |
|
66 |
) . '</span>'; |
9
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
$excerpt = ''; |
|
71 |
if ( $attributes['displayExcerpt'] ) { |
|
72 |
$excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); |
|
73 |
$excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); |
|
74 |
|
|
75 |
// Change existing [...] to […]. |
16
|
76 |
if ( '[...]' === substr( $excerpt, -5 ) ) { |
9
|
77 |
$excerpt = substr( $excerpt, 0, -5 ) . '[…]'; |
|
78 |
} |
|
79 |
|
|
80 |
$excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>'; |
|
81 |
} |
|
82 |
|
|
83 |
$list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>"; |
|
84 |
} |
|
85 |
|
18
|
86 |
$classnames = array(); |
16
|
87 |
if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) { |
18
|
88 |
$classnames[] = 'is-grid'; |
16
|
89 |
} |
|
90 |
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) { |
18
|
91 |
$classnames[] = 'columns-' . $attributes['columns']; |
16
|
92 |
} |
19
|
93 |
if ( $attributes['displayDate'] ) { |
|
94 |
$classnames[] = 'has-dates'; |
|
95 |
} |
|
96 |
if ( $attributes['displayAuthor'] ) { |
|
97 |
$classnames[] = 'has-authors'; |
|
98 |
} |
|
99 |
if ( $attributes['displayExcerpt'] ) { |
|
100 |
$classnames[] = 'has-excerpts'; |
|
101 |
} |
|
102 |
|
18
|
103 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) ); |
9
|
104 |
|
18
|
105 |
return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items ); |
9
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* Registers the `core/rss` block on server. |
|
110 |
*/ |
|
111 |
function register_block_core_rss() { |
16
|
112 |
register_block_type_from_metadata( |
|
113 |
__DIR__ . '/rss', |
9
|
114 |
array( |
|
115 |
'render_callback' => 'render_block_core_rss', |
|
116 |
) |
|
117 |
); |
|
118 |
} |
|
119 |
add_action( 'init', 'register_block_core_rss' ); |