wp/wp-includes/blocks/latest-posts.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    35  */
    35  */
    36 function render_block_core_latest_posts( $attributes ) {
    36 function render_block_core_latest_posts( $attributes ) {
    37 	global $post, $block_core_latest_posts_excerpt_length;
    37 	global $post, $block_core_latest_posts_excerpt_length;
    38 
    38 
    39 	$args = array(
    39 	$args = array(
    40 		'posts_per_page'   => $attributes['postsToShow'],
    40 		'posts_per_page'      => $attributes['postsToShow'],
    41 		'post_status'      => 'publish',
    41 		'post_status'         => 'publish',
    42 		'order'            => $attributes['order'],
    42 		'order'               => $attributes['order'],
    43 		'orderby'          => $attributes['orderBy'],
    43 		'orderby'             => $attributes['orderBy'],
    44 		'suppress_filters' => false,
    44 		'ignore_sticky_posts' => true,
       
    45 		'no_found_rows'       => true,
    45 	);
    46 	);
    46 
    47 
    47 	$block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
    48 	$block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
    48 	add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
    49 	add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
    49 
    50 
    52 	}
    53 	}
    53 	if ( isset( $attributes['selectedAuthor'] ) ) {
    54 	if ( isset( $attributes['selectedAuthor'] ) ) {
    54 		$args['author'] = $attributes['selectedAuthor'];
    55 		$args['author'] = $attributes['selectedAuthor'];
    55 	}
    56 	}
    56 
    57 
    57 	$recent_posts = get_posts( $args );
    58 	$query        = new WP_Query;
       
    59 	$recent_posts = $query->query( $args );
       
    60 
       
    61 	if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) {
       
    62 		update_post_thumbnail_cache( $query );
       
    63 	}
    58 
    64 
    59 	$list_items_markup = '';
    65 	$list_items_markup = '';
    60 
    66 
    61 	foreach ( $recent_posts as $post ) {
    67 	foreach ( $recent_posts as $post ) {
    62 		$post_link = esc_url( get_permalink( $post ) );
    68 		$post_link = esc_url( get_permalink( $post ) );
       
    69 		$title     = get_the_title( $post );
       
    70 
       
    71 		if ( ! $title ) {
       
    72 			$title = __( '(no title)' );
       
    73 		}
    63 
    74 
    64 		$list_items_markup .= '<li>';
    75 		$list_items_markup .= '<li>';
    65 
    76 
    66 		if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
    77 		if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
    67 			$image_style = '';
    78 			$image_style = '';
    79 
    90 
    80 			$featured_image = get_the_post_thumbnail(
    91 			$featured_image = get_the_post_thumbnail(
    81 				$post,
    92 				$post,
    82 				$attributes['featuredImageSizeSlug'],
    93 				$attributes['featuredImageSizeSlug'],
    83 				array(
    94 				array(
    84 					'style' => $image_style,
    95 					'style' => esc_attr( $image_style ),
    85 				)
    96 				)
    86 			);
    97 			);
    87 			if ( $attributes['addLinkToFeaturedImage'] ) {
    98 			if ( $attributes['addLinkToFeaturedImage'] ) {
    88 				$featured_image = sprintf(
    99 				$featured_image = sprintf(
    89 					'<a href="%1$s">%2$s</a>',
   100 					'<a href="%1$s" aria-label="%2$s">%3$s</a>',
    90 					$post_link,
   101 					esc_url( $post_link ),
       
   102 					esc_attr( $title ),
    91 					$featured_image
   103 					$featured_image
    92 				);
   104 				);
    93 			}
   105 			}
    94 			$list_items_markup .= sprintf(
   106 			$list_items_markup .= sprintf(
    95 				'<div class="%1$s">%2$s</div>',
   107 				'<div class="%1$s">%2$s</div>',
    96 				$image_classes,
   108 				esc_attr( $image_classes ),
    97 				$featured_image
   109 				$featured_image
    98 			);
   110 			);
    99 		}
   111 		}
   100 
   112 
   101 		$title = get_the_title( $post );
       
   102 		if ( ! $title ) {
       
   103 			$title = __( '(no title)' );
       
   104 		}
       
   105 		$list_items_markup .= sprintf(
   113 		$list_items_markup .= sprintf(
   106 			'<a href="%1$s">%2$s</a>',
   114 			'<a class="wp-block-latest-posts__post-title" href="%1$s">%2$s</a>',
   107 			$post_link,
   115 			esc_url( $post_link ),
   108 			$title
   116 			$title
   109 		);
   117 		);
   110 
   118 
   111 		if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   119 		if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   112 			$author_display_name = get_the_author_meta( 'display_name', $post->post_author );
   120 			$author_display_name = get_the_author_meta( 'display_name', $post->post_author );
   115 			$byline = sprintf( __( 'by %s' ), $author_display_name );
   123 			$byline = sprintf( __( 'by %s' ), $author_display_name );
   116 
   124 
   117 			if ( ! empty( $author_display_name ) ) {
   125 			if ( ! empty( $author_display_name ) ) {
   118 				$list_items_markup .= sprintf(
   126 				$list_items_markup .= sprintf(
   119 					'<div class="wp-block-latest-posts__post-author">%1$s</div>',
   127 					'<div class="wp-block-latest-posts__post-author">%1$s</div>',
   120 					esc_html( $byline )
   128 					$byline
   121 				);
   129 				);
   122 			}
   130 			}
   123 		}
   131 		}
   124 
   132 
   125 		if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
   133 		if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
   126 			$list_items_markup .= sprintf(
   134 			$list_items_markup .= sprintf(
   127 				'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
   135 				'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
   128 				esc_attr( get_the_date( 'c', $post ) ),
   136 				esc_attr( get_the_date( 'c', $post ) ),
   129 				esc_html( get_the_date( '', $post ) )
   137 				get_the_date( '', $post )
   130 			);
   138 			);
   131 		}
   139 		}
   132 
   140 
   133 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   141 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   134 			&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
   142 			&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
   146 		}
   154 		}
   147 
   155 
   148 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   156 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   149 			&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
   157 			&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
   150 
   158 
   151 			$post_content = wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) );
   159 			$post_content = html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) );
   152 
   160 
   153 			if ( post_password_required( $post ) ) {
   161 			if ( post_password_required( $post ) ) {
   154 				$post_content = __( 'This content is password protected.' );
   162 				$post_content = __( 'This content is password protected.' );
   155 			}
   163 			}
   156 
   164 
   157 			$list_items_markup .= sprintf(
   165 			$list_items_markup .= sprintf(
   158 				'<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
   166 				'<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
   159 				$post_content
   167 				wp_kses_post( $post_content )
   160 			);
   168 			);
   161 		}
   169 		}
   162 
   170 
   163 		$list_items_markup .= "</li>\n";
   171 		$list_items_markup .= "</li>\n";
   164 	}
   172 	}