wp/wp-includes/blocks/latest-posts.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
       
     9  * The excerpt length set by the Latest Posts core block
       
    10  * set at render time and used by the block itself.
       
    11  *
       
    12  * @var int
       
    13  */
       
    14 $block_core_latest_posts_excerpt_length = 0;
       
    15 
       
    16 /**
       
    17  * Callback for the excerpt_length filter used by
       
    18  * the Latest Posts block at render time.
       
    19  *
       
    20  * @return int Returns the global $block_core_latest_posts_excerpt_length variable
       
    21  *             to allow the excerpt_length filter respect the Latest Block setting.
       
    22  */
       
    23 function block_core_latest_posts_get_excerpt_length() {
       
    24 	global $block_core_latest_posts_excerpt_length;
       
    25 	return $block_core_latest_posts_excerpt_length;
       
    26 }
       
    27 
       
    28 /**
     9  * Renders the `core/latest-posts` block on server.
    29  * Renders the `core/latest-posts` block on server.
    10  *
    30  *
    11  * @param array $attributes The block attributes.
    31  * @param array $attributes The block attributes.
    12  *
    32  *
    13  * @return string Returns the post content with latest posts added.
    33  * @return string Returns the post content with latest posts added.
    14  */
    34  */
    15 function render_block_core_latest_posts( $attributes ) {
    35 function render_block_core_latest_posts( $attributes ) {
       
    36 	global $post, $block_core_latest_posts_excerpt_length;
       
    37 
    16 	$args = array(
    38 	$args = array(
    17 		'posts_per_page'   => $attributes['postsToShow'],
    39 		'posts_per_page'   => $attributes['postsToShow'],
    18 		'post_status'      => 'publish',
    40 		'post_status'      => 'publish',
    19 		'order'            => $attributes['order'],
    41 		'order'            => $attributes['order'],
    20 		'orderby'          => $attributes['orderBy'],
    42 		'orderby'          => $attributes['orderBy'],
    21 		'suppress_filters' => false,
    43 		'suppress_filters' => false,
    22 	);
    44 	);
    23 
    45 
       
    46 	$block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
       
    47 	add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
       
    48 
    24 	if ( isset( $attributes['categories'] ) ) {
    49 	if ( isset( $attributes['categories'] ) ) {
    25 		$args['category'] = $attributes['categories'];
    50 		$args['category__in'] = array_column( $attributes['categories'], 'id' );
       
    51 	}
       
    52 	if ( isset( $attributes['selectedAuthor'] ) ) {
       
    53 		$args['author'] = $attributes['selectedAuthor'];
    26 	}
    54 	}
    27 
    55 
    28 	$recent_posts = get_posts( $args );
    56 	$recent_posts = get_posts( $args );
    29 
    57 
    30 	$list_items_markup = '';
    58 	$list_items_markup = '';
    31 
    59 
    32 	foreach ( $recent_posts as $post ) {
    60 	foreach ( $recent_posts as $post ) {
       
    61 
       
    62 		$list_items_markup .= '<li>';
       
    63 
       
    64 		if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
       
    65 			$image_style = '';
       
    66 			if ( isset( $attributes['featuredImageSizeWidth'] ) ) {
       
    67 				$image_style .= sprintf( 'max-width:%spx;', $attributes['featuredImageSizeWidth'] );
       
    68 			}
       
    69 			if ( isset( $attributes['featuredImageSizeHeight'] ) ) {
       
    70 				$image_style .= sprintf( 'max-height:%spx;', $attributes['featuredImageSizeHeight'] );
       
    71 			}
       
    72 
       
    73 			$image_classes = 'wp-block-latest-posts__featured-image';
       
    74 			if ( isset( $attributes['featuredImageAlign'] ) ) {
       
    75 				$image_classes .= ' align' . $attributes['featuredImageAlign'];
       
    76 			}
       
    77 
       
    78 			$list_items_markup .= sprintf(
       
    79 				'<div class="%1$s">%2$s</div>',
       
    80 				$image_classes,
       
    81 				get_the_post_thumbnail(
       
    82 					$post,
       
    83 					$attributes['featuredImageSizeSlug'],
       
    84 					array(
       
    85 						'style' => $image_style,
       
    86 					)
       
    87 				)
       
    88 			);
       
    89 		}
       
    90 
    33 		$title = get_the_title( $post );
    91 		$title = get_the_title( $post );
    34 		if ( ! $title ) {
    92 		if ( ! $title ) {
    35 			$title = __( '(Untitled)' );
    93 			$title = __( '(no title)' );
    36 		}
    94 		}
    37 		$list_items_markup .= sprintf(
    95 		$list_items_markup .= sprintf(
    38 			'<li><a href="%1$s">%2$s</a>',
    96 			'<a href="%1$s">%2$s</a>',
    39 			esc_url( get_permalink( $post ) ),
    97 			esc_url( get_permalink( $post ) ),
    40 			$title
    98 			$title
    41 		);
    99 		);
    42 
   100 
       
   101 		if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
       
   102 			$author_display_name = get_the_author_meta( 'display_name', $post->post_author );
       
   103 
       
   104 			/* translators: byline. %s: current author. */
       
   105 			$byline = sprintf( __( 'by %s' ), $author_display_name );
       
   106 
       
   107 			if ( ! empty( $author_display_name ) ) {
       
   108 				$list_items_markup .= sprintf(
       
   109 					'<div class="wp-block-latest-posts__post-author">%1$s</div>',
       
   110 					esc_html( $byline )
       
   111 				);
       
   112 			}
       
   113 		}
       
   114 
    43 		if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
   115 		if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
    44 			$list_items_markup .= sprintf(
   116 			$list_items_markup .= sprintf(
    45 				'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
   117 				'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
    46 				esc_attr( get_the_date( 'c', $post ) ),
   118 				esc_attr( get_the_date( 'c', $post ) ),
    47 				esc_html( get_the_date( '', $post ) )
   119 				esc_html( get_the_date( '', $post ) )
    48 			);
   120 			);
    49 		}
   121 		}
    50 
   122 
       
   123 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
       
   124 			&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
       
   125 
       
   126 			$trimmed_excerpt = get_the_excerpt( $post );
       
   127 
       
   128 			$list_items_markup .= sprintf(
       
   129 				'<div class="wp-block-latest-posts__post-excerpt">%1$s</div>',
       
   130 				$trimmed_excerpt
       
   131 			);
       
   132 		}
       
   133 
       
   134 		if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
       
   135 			&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
       
   136 			$list_items_markup .= sprintf(
       
   137 				'<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' ) ) )
       
   139 			);
       
   140 		}
       
   141 
    51 		$list_items_markup .= "</li>\n";
   142 		$list_items_markup .= "</li>\n";
    52 	}
   143 	}
    53 
   144 
    54 	$class = 'wp-block-latest-posts';
   145 	remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
       
   146 
       
   147 	$class = 'wp-block-latest-posts wp-block-latest-posts__list';
    55 	if ( isset( $attributes['align'] ) ) {
   148 	if ( isset( $attributes['align'] ) ) {
    56 		$class .= ' align' . $attributes['align'];
   149 		$class .= ' align' . $attributes['align'];
    57 	}
   150 	}
    58 
   151 
    59 	if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
   152 	if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
    66 
   159 
    67 	if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
   160 	if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
    68 		$class .= ' has-dates';
   161 		$class .= ' has-dates';
    69 	}
   162 	}
    70 
   163 
       
   164 	if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
       
   165 		$class .= ' has-author';
       
   166 	}
       
   167 
    71 	if ( isset( $attributes['className'] ) ) {
   168 	if ( isset( $attributes['className'] ) ) {
    72 		$class .= ' ' . $attributes['className'];
   169 		$class .= ' ' . $attributes['className'];
    73 	}
   170 	}
    74 
   171 
    75 	$block_content = sprintf(
   172 	return sprintf(
    76 		'<ul class="%1$s">%2$s</ul>',
   173 		'<ul class="%1$s">%2$s</ul>',
    77 		esc_attr( $class ),
   174 		esc_attr( $class ),
    78 		$list_items_markup
   175 		$list_items_markup
    79 	);
   176 	);
    80 
       
    81 	return $block_content;
       
    82 }
   177 }
    83 
   178 
    84 /**
   179 /**
    85  * Registers the `core/latest-posts` block on server.
   180  * Registers the `core/latest-posts` block on server.
    86  */
   181  */
    87 function register_block_core_latest_posts() {
   182 function register_block_core_latest_posts() {
    88 	register_block_type(
   183 	register_block_type_from_metadata(
    89 		'core/latest-posts',
   184 		__DIR__ . '/latest-posts',
    90 		array(
   185 		array(
    91 			'attributes'      => array(
       
    92 				'categories'      => array(
       
    93 					'type' => 'string',
       
    94 				),
       
    95 				'className'       => array(
       
    96 					'type' => 'string',
       
    97 				),
       
    98 				'postsToShow'     => array(
       
    99 					'type'    => 'number',
       
   100 					'default' => 5,
       
   101 				),
       
   102 				'displayPostDate' => array(
       
   103 					'type'    => 'boolean',
       
   104 					'default' => false,
       
   105 				),
       
   106 				'postLayout'      => array(
       
   107 					'type'    => 'string',
       
   108 					'default' => 'list',
       
   109 				),
       
   110 				'columns'         => array(
       
   111 					'type'    => 'number',
       
   112 					'default' => 3,
       
   113 				),
       
   114 				'align'           => array(
       
   115 					'type' => 'string',
       
   116 				),
       
   117 				'order'           => array(
       
   118 					'type'    => 'string',
       
   119 					'default' => 'desc',
       
   120 				),
       
   121 				'orderBy'         => array(
       
   122 					'type'    => 'string',
       
   123 					'default' => 'date',
       
   124 				),
       
   125 			),
       
   126 			'render_callback' => 'render_block_core_latest_posts',
   186 			'render_callback' => 'render_block_core_latest_posts',
   127 		)
   187 		)
   128 	);
   188 	);
   129 }
   189 }
   130 
       
   131 add_action( 'init', 'register_block_core_latest_posts' );
   190 add_action( 'init', 'register_block_core_latest_posts' );
       
   191 
       
   192 /**
       
   193  * Handles outdated versions of the `core/latest-posts` block by converting
       
   194  * attribute `categories` from a numeric string to an array with key `id`.
       
   195  *
       
   196  * This is done to accommodate the changes introduced in #20781 that sought to
       
   197  * add support for multiple categories to the block. However, given that this
       
   198  * block is dynamic, the usual provisions for block migration are insufficient,
       
   199  * as they only act when a block is loaded in the editor.
       
   200  *
       
   201  * TODO: Remove when and if the bottom client-side deprecation for this block
       
   202  * is removed.
       
   203  *
       
   204  * @param array $block A single parsed block object.
       
   205  *
       
   206  * @return array The migrated block object.
       
   207  */
       
   208 function block_core_latest_posts_migrate_categories( $block ) {
       
   209 	if (
       
   210 		'core/latest-posts' === $block['blockName'] &&
       
   211 		! empty( $block['attrs']['categories'] ) &&
       
   212 		is_string( $block['attrs']['categories'] )
       
   213 	) {
       
   214 		$block['attrs']['categories'] = array(
       
   215 			array( 'id' => absint( $block['attrs']['categories'] ) ),
       
   216 		);
       
   217 	}
       
   218 
       
   219 	return $block;
       
   220 }
       
   221 add_filter( 'render_block_data', 'block_core_latest_posts_migrate_categories' );