[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Server-side rendering of the `core/latest-posts` block. 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Renders the `core/latest-posts` block on server. 10 * 11 * @param array $attributes The block attributes. 12 * 13 * @return string Returns the post content with latest posts added. 14 */ 15 function render_block_core_latest_posts( $attributes ) { 16 $args = array( 17 'posts_per_page' => $attributes['postsToShow'], 18 'post_status' => 'publish', 19 'order' => $attributes['order'], 20 'orderby' => $attributes['orderBy'], 21 'suppress_filters' => false, 22 ); 23 24 if ( isset( $attributes['categories'] ) ) { 25 $args['category'] = $attributes['categories']; 26 } 27 28 $recent_posts = get_posts( $args ); 29 30 $list_items_markup = ''; 31 32 $excerpt_length = $attributes['excerptLength']; 33 34 foreach ( $recent_posts as $post ) { 35 $title = get_the_title( $post ); 36 if ( ! $title ) { 37 $title = __( '(no title)' ); 38 } 39 $list_items_markup .= sprintf( 40 '<li><a href="%1$s">%2$s</a>', 41 esc_url( get_permalink( $post ) ), 42 $title 43 ); 44 45 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { 46 $list_items_markup .= sprintf( 47 '<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>', 48 esc_attr( get_the_date( 'c', $post ) ), 49 esc_html( get_the_date( '', $post ) ) 50 ); 51 } 52 53 if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] 54 && isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) { 55 $post_excerpt = $post->post_excerpt; 56 if ( ! ( $post_excerpt ) ) { 57 $post_excerpt = $post->post_content; 58 } 59 $trimmed_excerpt = esc_html( wp_trim_words( $post_excerpt, $excerpt_length, ' … ' ) ); 60 61 $list_items_markup .= sprintf( 62 '<div class="wp-block-latest-posts__post-excerpt">%1$s', 63 $trimmed_excerpt 64 ); 65 66 if ( strpos( $trimmed_excerpt, ' … ' ) !== false ) { 67 $list_items_markup .= sprintf( 68 '<a href="%1$s">%2$s</a></div>', 69 esc_url( get_permalink( $post ) ), 70 __( 'Read more' ) 71 ); 72 } else { 73 $list_items_markup .= sprintf( 74 '</div>' 75 ); 76 } 77 } 78 79 if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] 80 && isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) { 81 $list_items_markup .= sprintf( 82 '<div class="wp-block-latest-posts__post-full-content">%1$s</div>', 83 wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) ) 84 ); 85 } 86 87 $list_items_markup .= "</li>\n"; 88 } 89 90 $class = 'wp-block-latest-posts wp-block-latest-posts__list'; 91 if ( isset( $attributes['align'] ) ) { 92 $class .= ' align' . $attributes['align']; 93 } 94 95 if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) { 96 $class .= ' is-grid'; 97 } 98 99 if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) { 100 $class .= ' columns-' . $attributes['columns']; 101 } 102 103 if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) { 104 $class .= ' has-dates'; 105 } 106 107 if ( isset( $attributes['className'] ) ) { 108 $class .= ' ' . $attributes['className']; 109 } 110 111 return sprintf( 112 '<ul class="%1$s">%2$s</ul>', 113 esc_attr( $class ), 114 $list_items_markup 115 ); 116 } 117 118 /** 119 * Registers the `core/latest-posts` block on server. 120 */ 121 function register_block_core_latest_posts() { 122 register_block_type( 123 'core/latest-posts', 124 array( 125 'attributes' => array( 126 'align' => array( 127 'type' => 'string', 128 'enum' => array( 'left', 'center', 'right', 'wide', 'full' ), 129 ), 130 'className' => array( 131 'type' => 'string', 132 ), 133 'categories' => array( 134 'type' => 'string', 135 ), 136 'postsToShow' => array( 137 'type' => 'number', 138 'default' => 5, 139 ), 140 'displayPostContent' => array( 141 'type' => 'boolean', 142 'default' => false, 143 ), 144 'displayPostContentRadio' => array( 145 'type' => 'string', 146 'default' => 'excerpt', 147 ), 148 'excerptLength' => array( 149 'type' => 'number', 150 'default' => 55, 151 ), 152 'displayPostDate' => array( 153 'type' => 'boolean', 154 'default' => false, 155 ), 156 'postLayout' => array( 157 'type' => 'string', 158 'default' => 'list', 159 ), 160 'columns' => array( 161 'type' => 'number', 162 'default' => 3, 163 ), 164 'order' => array( 165 'type' => 'string', 166 'default' => 'desc', 167 ), 168 'orderBy' => array( 169 'type' => 'string', 170 'default' => 'date', 171 ), 172 ), 173 'render_callback' => 'render_block_core_latest_posts', 174 ) 175 ); 176 } 177 add_action( 'init', 'register_block_core_latest_posts' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Nov 23 20:47:33 2019 | Cross-referenced by PHPXref 0.7 |