author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 5.2.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
9 | 13 |
* @param array $attributes The block attributes. |
14 |
* |
|
15 |
* @return string Returns the block content with received rss items. |
|
16 |
*/ |
|
17 |
function render_block_core_rss( $attributes ) { |
|
19 | 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 |
||
9 | 22 |
$rss = fetch_feed( $attributes['feedURL'] ); |
23 |
||
24 |
if ( is_wp_error( $rss ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
25 |
return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</div></div>'; |
9 | 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 ) ) { |
|
16 | 37 |
$title = __( '(no title)' ); |
9 | 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> ', |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
esc_attr( date_i18n( 'c', $date ) ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
esc_attr( date_i18n( get_option( 'date_format' ), $date ) ) |
9 | 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(); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
64 |
if ( ! empty( $author ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
65 |
$author = '<span class="wp-block-rss__item-author">' . sprintf( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
66 |
/* translators: byline. %s: author. */ |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
67 |
__( 'by %s' ), |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
68 |
esc_html( strip_tags( $author ) ) |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
69 |
) . '</span>'; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
70 |
} |
9 | 71 |
} |
72 |
} |
|
73 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
74 |
$excerpt = ''; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
75 |
$description = $item->get_description(); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
76 |
if ( $attributes['displayExcerpt'] && ! empty( $description ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
77 |
$excerpt = html_entity_decode( $description, ENT_QUOTES, get_option( 'blog_charset' ) ); |
9 | 78 |
$excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); |
79 |
||
80 |
// Change existing [...] to […]. |
|
16 | 81 |
if ( '[...]' === substr( $excerpt, -5 ) ) { |
9 | 82 |
$excerpt = substr( $excerpt, 0, -5 ) . '[…]'; |
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 |
||
18 | 91 |
$classnames = array(); |
16 | 92 |
if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) { |
18 | 93 |
$classnames[] = 'is-grid'; |
16 | 94 |
} |
95 |
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) { |
|
18 | 96 |
$classnames[] = 'columns-' . $attributes['columns']; |
16 | 97 |
} |
19 | 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 |
||
18 | 108 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) ); |
9 | 109 |
|
18 | 110 |
return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items ); |
9 | 111 |
} |
112 |
||
113 |
/** |
|
114 |
* Registers the `core/rss` block on server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
* @since 5.2.0 |
9 | 117 |
*/ |
118 |
function register_block_core_rss() { |
|
16 | 119 |
register_block_type_from_metadata( |
120 |
__DIR__ . '/rss', |
|
9 | 121 |
array( |
122 |
'render_callback' => 'render_block_core_rss', |
|
123 |
) |
|
124 |
); |
|
125 |
} |
|
126 |
add_action( 'init', 'register_block_core_rss' ); |