PluginProbe
Gutenberg / 20.0.1
Gutenberg v20.0.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / rss.php

rss.php in Gutenberg 20.0.1, at build/block-library/blocks/rss.php

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