PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.0
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 14.7.0, at build/block-library/blocks/rss.php

120 lines 3.9 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 * @param array $attributes The block attributes.
12 *
13 * @return string Returns the block content with received rss items.
14 */
15 function gutenberg_render_block_core_rss( $attributes ) {
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
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> ' . esc_html( $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 ) ) {
35 $title = __( '(no title)' );
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 esc_attr( date_i18n( get_option( 'c' ), $date ) ),
52 esc_attr( 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();
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>';
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'], ' [&hellip;]' ) );
74
75 // Change existing [...] to [&hellip;].
76 if ( '[...]' === substr( $excerpt, -5 ) ) {
77 $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
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
86 $classnames = array();
87 if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
88 $classnames[] = 'is-grid';
89 }
90 if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
91 $classnames[] = 'columns-' . $attributes['columns'];
92 }
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
103 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
104
105 return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items );
106 }
107
108 /**
109 * Registers the `core/rss` block on server.
110 */
111 function gutenberg_register_block_core_rss() {
112 register_block_type_from_metadata(
113 __DIR__ . '/rss',
114 array(
115 'render_callback' => 'gutenberg_render_block_core_rss',
116 )
117 );
118 }
119 add_action( 'init', 'gutenberg_register_block_core_rss', 20 );
120