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 ) { |
|
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 |
// PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks. |
|
24 |
$rss->__destruct(); |
|
25 |
unset( $rss ); |
|
26 |
|
|
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 = __( '(Untitled)' ); |
|
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(); |
|
62 |
$author = '<span class="wp-block-rss__item-author">' . __( 'by' ) . ' ' . esc_html( strip_tags( $author ) ) . '</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'], ' […]' ) ); |
|
70 |
|
|
71 |
// Change existing [...] to […]. |
|
72 |
if ( '[...]' == substr( $excerpt, -5 ) ) { |
|
73 |
$excerpt = substr( $excerpt, 0, -5 ) . '[…]'; |
|
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 |
$classes = 'grid' === $attributes['blockLayout'] ? ' is-grid columns-' . $attributes['columns'] : ''; |
|
83 |
$list_items_markup = "<ul class='wp-block-rss{$classes}'>{$list_items}</ul>"; |
|
84 |
|
|
85 |
// PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks. |
|
86 |
$rss->__destruct(); |
|
87 |
unset( $rss ); |
|
88 |
|
|
89 |
return $list_items_markup; |
|
90 |
} |
|
91 |
|
|
92 |
/** |
|
93 |
* Registers the `core/rss` block on server. |
|
94 |
*/ |
|
95 |
function register_block_core_rss() { |
|
96 |
register_block_type( 'core/rss', |
|
97 |
array( |
|
98 |
'attributes' => array( |
|
99 |
'columns' => array( |
|
100 |
'type' => 'number', |
|
101 |
'default' => 2, |
|
102 |
), |
|
103 |
'blockLayout' => array( |
|
104 |
'type' => 'string', |
|
105 |
'default' => 'list', |
|
106 |
), |
|
107 |
'feedURL' => array( |
|
108 |
'type' => 'string', |
|
109 |
'default' => '', |
|
110 |
), |
|
111 |
'itemsToShow' => array( |
|
112 |
'type' => 'number', |
|
113 |
'default' => 5, |
|
114 |
), |
|
115 |
'displayExcerpt' => array( |
|
116 |
'type' => 'boolean', |
|
117 |
'default' => false, |
|
118 |
), |
|
119 |
'displayAuthor' => array( |
|
120 |
'type' => 'boolean', |
|
121 |
'default' => false, |
|
122 |
), |
|
123 |
'displayDate' => array( |
|
124 |
'type' => 'boolean', |
|
125 |
'default' => false, |
|
126 |
), |
|
127 |
'excerptLength' => array( |
|
128 |
'type' => 'number', |
|
129 |
'default' => 55, |
|
130 |
), |
|
131 |
), |
|
132 |
'render_callback' => 'render_block_core_rss', |
|
133 |
) |
|
134 |
); |
|
135 |
} |
|
136 |
|
|
137 |
add_action( 'init', 'register_block_core_rss' ); |