wp/wp-includes/blocks/latest-posts.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    15 $block_core_latest_posts_excerpt_length = 0;
    15 $block_core_latest_posts_excerpt_length = 0;
    16 
    16 
    17 /**
    17 /**
    18  * Callback for the excerpt_length filter used by
    18  * Callback for the excerpt_length filter used by
    19  * the Latest Posts block at render time.
    19  * the Latest Posts block at render time.
       
    20  *
       
    21  * @since 5.4.0
    20  *
    22  *
    21  * @return int Returns the global $block_core_latest_posts_excerpt_length variable
    23  * @return int Returns the global $block_core_latest_posts_excerpt_length variable
    22  *             to allow the excerpt_length filter respect the Latest Block setting.
    24  *             to allow the excerpt_length filter respect the Latest Block setting.
    23  */
    25  */
    24 function block_core_latest_posts_get_excerpt_length() {
    26 function block_core_latest_posts_get_excerpt_length() {
    26 	return $block_core_latest_posts_excerpt_length;
    28 	return $block_core_latest_posts_excerpt_length;
    27 }
    29 }
    28 
    30 
    29 /**
    31 /**
    30  * Renders the `core/latest-posts` block on server.
    32  * Renders the `core/latest-posts` block on server.
       
    33  *
       
    34  * @since 5.0.0
    31  *
    35  *
    32  * @param array $attributes The block attributes.
    36  * @param array $attributes The block attributes.
    33  *
    37  *
    34  * @return string Returns the post content with latest posts added.
    38  * @return string Returns the post content with latest posts added.
    35  */
    39  */
    46 	);
    50 	);
    47 
    51 
    48 	$block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
    52 	$block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
    49 	add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
    53 	add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
    50 
    54 
    51 	if ( isset( $attributes['categories'] ) ) {
    55 	if ( ! empty( $attributes['categories'] ) ) {
    52 		$args['category__in'] = array_column( $attributes['categories'], 'id' );
    56 		$args['category__in'] = array_column( $attributes['categories'], 'id' );
    53 	}
    57 	}
    54 	if ( isset( $attributes['selectedAuthor'] ) ) {
    58 	if ( isset( $attributes['selectedAuthor'] ) ) {
    55 		$args['author'] = $attributes['selectedAuthor'];
    59 		$args['author'] = $attributes['selectedAuthor'];
    56 	}
    60 	}
    57 
    61 
    58 	$query        = new WP_Query;
    62 	$query        = new WP_Query();
    59 	$recent_posts = $query->query( $args );
    63 	$recent_posts = $query->query( $args );
    60 
    64 
    61 	if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) {
    65 	if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) {
    62 		update_post_thumbnail_cache( $query );
    66 		update_post_thumbnail_cache( $query );
    63 	}
    67 	}
   141 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   145 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   142 			&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
   146 			&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
   143 
   147 
   144 			$trimmed_excerpt = get_the_excerpt( $post );
   148 			$trimmed_excerpt = get_the_excerpt( $post );
   145 
   149 
       
   150 			/*
       
   151 			 * Adds a "Read more" link with screen reader text.
       
   152 			 * […] is the default excerpt ending from wp_trim_excerpt() in Core.
       
   153 			 */
       
   154 			if ( str_ends_with( $trimmed_excerpt, ' […]' ) ) {
       
   155 				/** This filter is documented in wp-includes/formatting.php */
       
   156 				$excerpt_length = (int) apply_filters( 'excerpt_length', $block_core_latest_posts_excerpt_length );
       
   157 				if ( $excerpt_length <= $block_core_latest_posts_excerpt_length ) {
       
   158 					$trimmed_excerpt  = substr( $trimmed_excerpt, 0, -11 );
       
   159 					$trimmed_excerpt .= sprintf(
       
   160 						/* translators: 1: A URL to a post, 2: Hidden accessibility text: Post title */
       
   161 						__( '… <a class="wp-block-latest-posts__read-more" href="%1$s" rel="noopener noreferrer">Read more<span class="screen-reader-text">: %2$s</span></a>' ),
       
   162 						esc_url( $post_link ),
       
   163 						esc_html( $title )
       
   164 					);
       
   165 				}
       
   166 			}
       
   167 
   146 			if ( post_password_required( $post ) ) {
   168 			if ( post_password_required( $post ) ) {
   147 				$trimmed_excerpt = __( 'This content is password protected.' );
   169 				$trimmed_excerpt = __( 'This content is password protected.' );
   148 			}
   170 			}
   149 
   171 
   150 			$list_items_markup .= sprintf(
   172 			$list_items_markup .= sprintf(
   171 		$list_items_markup .= "</li>\n";
   193 		$list_items_markup .= "</li>\n";
   172 	}
   194 	}
   173 
   195 
   174 	remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
   196 	remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
   175 
   197 
   176 	$class = 'wp-block-latest-posts__list';
   198 	$classes = array( 'wp-block-latest-posts__list' );
   177 
       
   178 	if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
   199 	if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
   179 		$class .= ' is-grid';
   200 		$classes[] = 'is-grid';
   180 	}
   201 	}
   181 
       
   182 	if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
   202 	if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
   183 		$class .= ' columns-' . $attributes['columns'];
   203 		$classes[] = 'columns-' . $attributes['columns'];
   184 	}
   204 	}
   185 
       
   186 	if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
   205 	if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
   187 		$class .= ' has-dates';
   206 		$classes[] = 'has-dates';
   188 	}
   207 	}
   189 
       
   190 	if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   208 	if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   191 		$class .= ' has-author';
   209 		$classes[] = 'has-author';
   192 	}
   210 	}
   193 
   211 	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
   194 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
   212 		$classes[] = 'has-link-color';
       
   213 	}
       
   214 
       
   215 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
   195 
   216 
   196 	return sprintf(
   217 	return sprintf(
   197 		'<ul %1$s>%2$s</ul>',
   218 		'<ul %1$s>%2$s</ul>',
   198 		$wrapper_attributes,
   219 		$wrapper_attributes,
   199 		$list_items_markup
   220 		$list_items_markup
   200 	);
   221 	);
   201 }
   222 }
   202 
   223 
   203 /**
   224 /**
   204  * Registers the `core/latest-posts` block on server.
   225  * Registers the `core/latest-posts` block on server.
       
   226  *
       
   227  * @since 5.0.0
   205  */
   228  */
   206 function register_block_core_latest_posts() {
   229 function register_block_core_latest_posts() {
   207 	register_block_type_from_metadata(
   230 	register_block_type_from_metadata(
   208 		__DIR__ . '/latest-posts',
   231 		__DIR__ . '/latest-posts',
   209 		array(
   232 		array(
   222  * block is dynamic, the usual provisions for block migration are insufficient,
   245  * block is dynamic, the usual provisions for block migration are insufficient,
   223  * as they only act when a block is loaded in the editor.
   246  * as they only act when a block is loaded in the editor.
   224  *
   247  *
   225  * TODO: Remove when and if the bottom client-side deprecation for this block
   248  * TODO: Remove when and if the bottom client-side deprecation for this block
   226  * is removed.
   249  * is removed.
       
   250  *
       
   251  * @since 5.5.0
   227  *
   252  *
   228  * @param array $block A single parsed block object.
   253  * @param array $block A single parsed block object.
   229  *
   254  *
   230  * @return array The migrated block object.
   255  * @return array The migrated block object.
   231  */
   256  */