18 if ( is_wp_error( $rss ) ) { |
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>'; |
19 return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</div></div>'; |
20 } |
20 } |
21 |
21 |
22 if ( ! $rss->get_item_quantity() ) { |
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>'; |
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>'; |
28 } |
24 } |
29 |
25 |
30 $rss_items = $rss->get_items( 0, $attributes['itemsToShow'] ); |
26 $rss_items = $rss->get_items( 0, $attributes['itemsToShow'] ); |
31 $list_items = ''; |
27 $list_items = ''; |
32 foreach ( $rss_items as $item ) { |
28 foreach ( $rss_items as $item ) { |
33 $title = esc_html( trim( strip_tags( $item->get_title() ) ) ); |
29 $title = esc_html( trim( strip_tags( $item->get_title() ) ) ); |
34 if ( empty( $title ) ) { |
30 if ( empty( $title ) ) { |
35 $title = __( '(Untitled)' ); |
31 $title = __( '(no title)' ); |
36 } |
32 } |
37 $link = $item->get_link(); |
33 $link = $item->get_link(); |
38 $link = esc_url( $link ); |
34 $link = esc_url( $link ); |
39 if ( $link ) { |
35 if ( $link ) { |
40 $title = "<a href='{$link}'>{$title}</a>"; |
36 $title = "<a href='{$link}'>{$title}</a>"; |
57 $author = ''; |
53 $author = ''; |
58 if ( $attributes['displayAuthor'] ) { |
54 if ( $attributes['displayAuthor'] ) { |
59 $author = $item->get_author(); |
55 $author = $item->get_author(); |
60 if ( is_object( $author ) ) { |
56 if ( is_object( $author ) ) { |
61 $author = $author->get_name(); |
57 $author = $author->get_name(); |
62 $author = '<span class="wp-block-rss__item-author">' . __( 'by' ) . ' ' . esc_html( strip_tags( $author ) ) . '</span>'; |
58 $author = '<span class="wp-block-rss__item-author">' . sprintf( |
|
59 /* translators: %s: the author. */ |
|
60 __( 'by %s' ), |
|
61 esc_html( strip_tags( $author ) ) |
|
62 ) . '</span>'; |
63 } |
63 } |
64 } |
64 } |
65 |
65 |
66 $excerpt = ''; |
66 $excerpt = ''; |
67 if ( $attributes['displayExcerpt'] ) { |
67 if ( $attributes['displayExcerpt'] ) { |
68 $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); |
68 $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); |
69 $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); |
69 $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); |
70 |
70 |
71 // Change existing [...] to […]. |
71 // Change existing [...] to […]. |
72 if ( '[...]' == substr( $excerpt, -5 ) ) { |
72 if ( '[...]' === substr( $excerpt, -5 ) ) { |
73 $excerpt = substr( $excerpt, 0, -5 ) . '[…]'; |
73 $excerpt = substr( $excerpt, 0, -5 ) . '[…]'; |
74 } |
74 } |
75 |
75 |
76 $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>'; |
76 $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>'; |
77 } |
77 } |
78 |
78 |
79 $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>"; |
79 $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>"; |
80 } |
80 } |
81 |
81 |
82 $classes = 'grid' === $attributes['blockLayout'] ? ' is-grid columns-' . $attributes['columns'] : ''; |
82 $class = 'wp-block-rss'; |
83 $list_items_markup = "<ul class='wp-block-rss{$classes}'>{$list_items}</ul>"; |
83 if ( isset( $attributes['align'] ) ) { |
|
84 $class .= ' align' . $attributes['align']; |
|
85 } |
84 |
86 |
85 // PHP 5.2 compatibility. See: http://simplepie.org/wiki/faq/i_m_getting_memory_leaks. |
87 if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) { |
86 $rss->__destruct(); |
88 $class .= ' is-grid'; |
87 unset( $rss ); |
89 } |
88 |
90 |
89 return $list_items_markup; |
91 if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) { |
|
92 $class .= ' columns-' . $attributes['columns']; |
|
93 } |
|
94 |
|
95 if ( isset( $attributes['className'] ) ) { |
|
96 $class .= ' ' . $attributes['className']; |
|
97 } |
|
98 |
|
99 return sprintf( '<ul class="%s">%s</ul>', esc_attr( $class ), $list_items ); |
90 } |
100 } |
91 |
101 |
92 /** |
102 /** |
93 * Registers the `core/rss` block on server. |
103 * Registers the `core/rss` block on server. |
94 */ |
104 */ |
95 function register_block_core_rss() { |
105 function register_block_core_rss() { |
96 register_block_type( 'core/rss', |
106 register_block_type_from_metadata( |
|
107 __DIR__ . '/rss', |
97 array( |
108 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', |
109 'render_callback' => 'render_block_core_rss', |
133 ) |
110 ) |
134 ); |
111 ); |
135 } |
112 } |
136 |
|
137 add_action( 'init', 'register_block_core_rss' ); |
113 add_action( 'init', 'register_block_core_rss' ); |