wp/wp-includes/blocks/latest-posts.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
     9  * The excerpt length set by the Latest Posts core block
     9  * The excerpt length set by the Latest Posts core block
    10  * set at render time and used by the block itself.
    10  * set at render time and used by the block itself.
    11  *
    11  *
    12  * @var int
    12  * @var int
    13  */
    13  */
       
    14 global $block_core_latest_posts_excerpt_length;
    14 $block_core_latest_posts_excerpt_length = 0;
    15 $block_core_latest_posts_excerpt_length = 0;
    15 
    16 
    16 /**
    17 /**
    17  * Callback for the excerpt_length filter used by
    18  * Callback for the excerpt_length filter used by
    18  * the Latest Posts block at render time.
    19  * the Latest Posts block at render time.
    56 	$recent_posts = get_posts( $args );
    57 	$recent_posts = get_posts( $args );
    57 
    58 
    58 	$list_items_markup = '';
    59 	$list_items_markup = '';
    59 
    60 
    60 	foreach ( $recent_posts as $post ) {
    61 	foreach ( $recent_posts as $post ) {
       
    62 		$post_link = esc_url( get_permalink( $post ) );
    61 
    63 
    62 		$list_items_markup .= '<li>';
    64 		$list_items_markup .= '<li>';
    63 
    65 
    64 		if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
    66 		if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
    65 			$image_style = '';
    67 			$image_style = '';
    73 			$image_classes = 'wp-block-latest-posts__featured-image';
    75 			$image_classes = 'wp-block-latest-posts__featured-image';
    74 			if ( isset( $attributes['featuredImageAlign'] ) ) {
    76 			if ( isset( $attributes['featuredImageAlign'] ) ) {
    75 				$image_classes .= ' align' . $attributes['featuredImageAlign'];
    77 				$image_classes .= ' align' . $attributes['featuredImageAlign'];
    76 			}
    78 			}
    77 
    79 
       
    80 			$featured_image = get_the_post_thumbnail(
       
    81 				$post,
       
    82 				$attributes['featuredImageSizeSlug'],
       
    83 				array(
       
    84 					'style' => $image_style,
       
    85 				)
       
    86 			);
       
    87 			if ( $attributes['addLinkToFeaturedImage'] ) {
       
    88 				$featured_image = sprintf(
       
    89 					'<a href="%1$s">%2$s</a>',
       
    90 					$post_link,
       
    91 					$featured_image
       
    92 				);
       
    93 			}
    78 			$list_items_markup .= sprintf(
    94 			$list_items_markup .= sprintf(
    79 				'<div class="%1$s">%2$s</div>',
    95 				'<div class="%1$s">%2$s</div>',
    80 				$image_classes,
    96 				$image_classes,
    81 				get_the_post_thumbnail(
    97 				$featured_image
    82 					$post,
       
    83 					$attributes['featuredImageSizeSlug'],
       
    84 					array(
       
    85 						'style' => $image_style,
       
    86 					)
       
    87 				)
       
    88 			);
    98 			);
    89 		}
    99 		}
    90 
   100 
    91 		$title = get_the_title( $post );
   101 		$title = get_the_title( $post );
    92 		if ( ! $title ) {
   102 		if ( ! $title ) {
    93 			$title = __( '(no title)' );
   103 			$title = __( '(no title)' );
    94 		}
   104 		}
    95 		$list_items_markup .= sprintf(
   105 		$list_items_markup .= sprintf(
    96 			'<a href="%1$s">%2$s</a>',
   106 			'<a href="%1$s">%2$s</a>',
    97 			esc_url( get_permalink( $post ) ),
   107 			$post_link,
    98 			$title
   108 			$title
    99 		);
   109 		);
   100 
   110 
   101 		if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   111 		if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   102 			$author_display_name = get_the_author_meta( 'display_name', $post->post_author );
   112 			$author_display_name = get_the_author_meta( 'display_name', $post->post_author );
   123 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   133 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   124 			&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
   134 			&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
   125 
   135 
   126 			$trimmed_excerpt = get_the_excerpt( $post );
   136 			$trimmed_excerpt = get_the_excerpt( $post );
   127 
   137 
       
   138 			if ( post_password_required( $post ) ) {
       
   139 				$trimmed_excerpt = __( 'This content is password protected.' );
       
   140 			}
       
   141 
   128 			$list_items_markup .= sprintf(
   142 			$list_items_markup .= sprintf(
   129 				'<div class="wp-block-latest-posts__post-excerpt">%1$s</div>',
   143 				'<div class="wp-block-latest-posts__post-excerpt">%1$s</div>',
   130 				$trimmed_excerpt
   144 				$trimmed_excerpt
   131 			);
   145 			);
   132 		}
   146 		}
   133 
   147 
   134 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   148 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
   135 			&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
   149 			&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
       
   150 
       
   151 			$post_content = wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) );
       
   152 
       
   153 			if ( post_password_required( $post ) ) {
       
   154 				$post_content = __( 'This content is password protected.' );
       
   155 			}
       
   156 
   136 			$list_items_markup .= sprintf(
   157 			$list_items_markup .= sprintf(
   137 				'<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
   158 				'<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
   138 				wp_kses_post( html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) ) )
   159 				$post_content
   139 			);
   160 			);
   140 		}
   161 		}
   141 
   162 
   142 		$list_items_markup .= "</li>\n";
   163 		$list_items_markup .= "</li>\n";
   143 	}
   164 	}
   144 
   165 
   145 	remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
   166 	remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
   146 
   167 
   147 	$class = 'wp-block-latest-posts wp-block-latest-posts__list';
   168 	$class = 'wp-block-latest-posts__list';
   148 	if ( isset( $attributes['align'] ) ) {
       
   149 		$class .= ' align' . $attributes['align'];
       
   150 	}
       
   151 
   169 
   152 	if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
   170 	if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
   153 		$class .= ' is-grid';
   171 		$class .= ' is-grid';
   154 	}
   172 	}
   155 
   173 
   163 
   181 
   164 	if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   182 	if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
   165 		$class .= ' has-author';
   183 		$class .= ' has-author';
   166 	}
   184 	}
   167 
   185 
   168 	if ( isset( $attributes['className'] ) ) {
   186 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
   169 		$class .= ' ' . $attributes['className'];
       
   170 	}
       
   171 
   187 
   172 	return sprintf(
   188 	return sprintf(
   173 		'<ul class="%1$s">%2$s</ul>',
   189 		'<ul %1$s>%2$s</ul>',
   174 		esc_attr( $class ),
   190 		$wrapper_attributes,
   175 		$list_items_markup
   191 		$list_items_markup
   176 	);
   192 	);
   177 }
   193 }
   178 
   194 
   179 /**
   195 /**