[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 * @since 5.2.0 12 * 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 ) { 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 22 $rss = fetch_feed( $attributes['feedURL'] ); 23 24 if ( is_wp_error( $rss ) ) { 25 return '<div class="components-placeholder"><div class="notice notice-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . esc_html( $rss->get_error_message() ) . '</div></div>'; 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 ) ) { 37 $title = __( '(no title)' ); 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> ', 53 esc_attr( date_i18n( 'c', $date ) ), 54 esc_attr( date_i18n( get_option( 'date_format' ), $date ) ) 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(); 64 $author = '<span class="wp-block-rss__item-author">' . sprintf( 65 /* translators: byline. %s: author. */ 66 __( 'by %s' ), 67 esc_html( strip_tags( $author ) ) 68 ) . '</span>'; 69 } 70 } 71 72 $excerpt = ''; 73 if ( $attributes['displayExcerpt'] ) { 74 $excerpt = html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); 75 $excerpt = esc_attr( wp_trim_words( $excerpt, $attributes['excerptLength'], ' […]' ) ); 76 77 // Change existing [...] to […]. 78 if ( '[...]' === substr( $excerpt, -5 ) ) { 79 $excerpt = substr( $excerpt, 0, -5 ) . '[…]'; 80 } 81 82 $excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>'; 83 } 84 85 $list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>"; 86 } 87 88 $classnames = array(); 89 if ( isset( $attributes['blockLayout'] ) && 'grid' === $attributes['blockLayout'] ) { 90 $classnames[] = 'is-grid'; 91 } 92 if ( isset( $attributes['columns'] ) && 'grid' === $attributes['blockLayout'] ) { 93 $classnames[] = 'columns-' . $attributes['columns']; 94 } 95 if ( $attributes['displayDate'] ) { 96 $classnames[] = 'has-dates'; 97 } 98 if ( $attributes['displayAuthor'] ) { 99 $classnames[] = 'has-authors'; 100 } 101 if ( $attributes['displayExcerpt'] ) { 102 $classnames[] = 'has-excerpts'; 103 } 104 105 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) ); 106 107 return sprintf( '<ul %s>%s</ul>', $wrapper_attributes, $list_items ); 108 } 109 110 /** 111 * Registers the `core/rss` block on server. 112 * 113 * @since 5.2.0 114 */ 115 function register_block_core_rss() { 116 register_block_type_from_metadata( 117 __DIR__ . '/rss', 118 array( 119 'render_callback' => 'render_block_core_rss', 120 ) 121 ); 122 } 123 add_action( 'init', 'register_block_core_rss' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |