PluginProbe
Gutenberg / 7.4.0
Gutenberg v7.4.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 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / rss.php

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

151 lines 4.1 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 $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">' . __( 'by' ) . ' ' . esc_html( strip_tags( $author ) ) . '</span>';
59 }
60 }
61
62 $excerpt = '';
63 if ( $attributes['displayExcerpt'] ) {
64 $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) );
65 $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' [&hellip;]' ) );
66
67 // Change existing [...] to [&hellip;].
68 if ( '[...]' === substr( $excerpt, -5 ) ) {
69 $excerpt = substr( $excerpt, 0, -5 ) . '[&hellip;]';
70 }
71
72 $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
73 }
74
75 $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>";
76 }
77
78 $class = 'wp-block-rss';
79 if ( isset( $attributes['align'] ) ) {
80 $class .= ' align' . $attributes['align'];
81 }
82
83 if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) {
84 $class .= ' is-grid';
85 }
86
87 if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) {
88 $class .= ' columns-' . $attributes['columns'];
89 }
90
91 if ( isset( $attributes['className'] ) ) {
92 $class .= ' ' . $attributes['className'];
93 }
94
95 return "<ul class='{$class}'>{$list_items}</ul>";
96 }
97
98 /**
99 * Registers the `core/rss` block on server.
100 */
101 function gutenberg_register_block_core_rss() {
102 register_block_type(
103 'core/rss',
104 array(
105 'attributes' => array(
106 'align' => array(
107 'type' => 'string',
108 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ),
109 ),
110 'className' => array(
111 'type' => 'string',
112 ),
113 'columns' => array(
114 'type' => 'number',
115 'default' => 2,
116 ),
117 'blockLayout' => array(
118 'type' => 'string',
119 'default' => 'list',
120 ),
121 'feedURL' => array(
122 'type' => 'string',
123 'default' => '',
124 ),
125 'itemsToShow' => array(
126 'type' => 'number',
127 'default' => 5,
128 ),
129 'displayExcerpt' => array(
130 'type' => 'boolean',
131 'default' => false,
132 ),
133 'displayAuthor' => array(
134 'type' => 'boolean',
135 'default' => false,
136 ),
137 'displayDate' => array(
138 'type' => 'boolean',
139 'default' => false,
140 ),
141 'excerptLength' => array(
142 'type' => 'number',
143 'default' => 55,
144 ),
145 ),
146 'render_callback' => 'gutenberg_render_block_core_rss',
147 )
148 );
149 }
150 add_action( 'init', 'gutenberg_register_block_core_rss', 20 );
151