| 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'], ' […]' ) ); |
| 66 |
|
| 67 |
// Change existing [...] to […]. |
| 68 |
if ( '[...]' === substr( $excerpt, -5 ) ) { |
| 69 |
$excerpt = substr( $excerpt, 0, -5 ) . '[…]'; |
| 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 |
|