Edit File by line
/home/zeestwma/richards.../wp-inclu.../blocks
File: latest-posts.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Server-side rendering of the `core/latest-posts` block.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* The excerpt length set by the Latest Posts core block
[8] Fix | Delete
* set at render time and used by the block itself.
[9] Fix | Delete
*
[10] Fix | Delete
* @var int
[11] Fix | Delete
*/
[12] Fix | Delete
global $block_core_latest_posts_excerpt_length;
[13] Fix | Delete
$block_core_latest_posts_excerpt_length = 0;
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Callback for the excerpt_length filter used by
[17] Fix | Delete
* the Latest Posts block at render time.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 5.4.0
[20] Fix | Delete
*
[21] Fix | Delete
* @return int Returns the global $block_core_latest_posts_excerpt_length variable
[22] Fix | Delete
* to allow the excerpt_length filter respect the Latest Block setting.
[23] Fix | Delete
*/
[24] Fix | Delete
function block_core_latest_posts_get_excerpt_length() {
[25] Fix | Delete
global $block_core_latest_posts_excerpt_length;
[26] Fix | Delete
return $block_core_latest_posts_excerpt_length;
[27] Fix | Delete
}
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* Renders the `core/latest-posts` block on server.
[31] Fix | Delete
*
[32] Fix | Delete
* @since 5.0.0
[33] Fix | Delete
*
[34] Fix | Delete
* @param array $attributes The block attributes.
[35] Fix | Delete
*
[36] Fix | Delete
* @return string Returns the post content with latest posts added.
[37] Fix | Delete
*/
[38] Fix | Delete
function render_block_core_latest_posts( $attributes ) {
[39] Fix | Delete
global $post, $block_core_latest_posts_excerpt_length;
[40] Fix | Delete
[41] Fix | Delete
$args = array(
[42] Fix | Delete
'posts_per_page' => $attributes['postsToShow'],
[43] Fix | Delete
'post_status' => 'publish',
[44] Fix | Delete
'order' => $attributes['order'],
[45] Fix | Delete
'orderby' => $attributes['orderBy'],
[46] Fix | Delete
'ignore_sticky_posts' => true,
[47] Fix | Delete
'no_found_rows' => true,
[48] Fix | Delete
);
[49] Fix | Delete
[50] Fix | Delete
$block_core_latest_posts_excerpt_length = $attributes['excerptLength'];
[51] Fix | Delete
add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
[52] Fix | Delete
[53] Fix | Delete
if ( ! empty( $attributes['categories'] ) ) {
[54] Fix | Delete
$args['category__in'] = array_column( $attributes['categories'], 'id' );
[55] Fix | Delete
}
[56] Fix | Delete
if ( isset( $attributes['selectedAuthor'] ) ) {
[57] Fix | Delete
$args['author'] = $attributes['selectedAuthor'];
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
$query = new WP_Query();
[61] Fix | Delete
$recent_posts = $query->query( $args );
[62] Fix | Delete
[63] Fix | Delete
if ( isset( $attributes['displayFeaturedImage'] ) && $attributes['displayFeaturedImage'] ) {
[64] Fix | Delete
update_post_thumbnail_cache( $query );
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
$list_items_markup = '';
[68] Fix | Delete
[69] Fix | Delete
foreach ( $recent_posts as $post ) {
[70] Fix | Delete
$post_link = esc_url( get_permalink( $post ) );
[71] Fix | Delete
$title = get_the_title( $post );
[72] Fix | Delete
[73] Fix | Delete
if ( ! $title ) {
[74] Fix | Delete
$title = __( '(no title)' );
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
$list_items_markup .= '<li>';
[78] Fix | Delete
[79] Fix | Delete
if ( $attributes['displayFeaturedImage'] && has_post_thumbnail( $post ) ) {
[80] Fix | Delete
$image_style = '';
[81] Fix | Delete
if ( isset( $attributes['featuredImageSizeWidth'] ) ) {
[82] Fix | Delete
$image_style .= sprintf( 'max-width:%spx;', $attributes['featuredImageSizeWidth'] );
[83] Fix | Delete
}
[84] Fix | Delete
if ( isset( $attributes['featuredImageSizeHeight'] ) ) {
[85] Fix | Delete
$image_style .= sprintf( 'max-height:%spx;', $attributes['featuredImageSizeHeight'] );
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
$image_classes = 'wp-block-latest-posts__featured-image';
[89] Fix | Delete
if ( isset( $attributes['featuredImageAlign'] ) ) {
[90] Fix | Delete
$image_classes .= ' align' . $attributes['featuredImageAlign'];
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
$featured_image = get_the_post_thumbnail(
[94] Fix | Delete
$post,
[95] Fix | Delete
$attributes['featuredImageSizeSlug'],
[96] Fix | Delete
array(
[97] Fix | Delete
'style' => esc_attr( $image_style ),
[98] Fix | Delete
)
[99] Fix | Delete
);
[100] Fix | Delete
if ( $attributes['addLinkToFeaturedImage'] ) {
[101] Fix | Delete
$featured_image = sprintf(
[102] Fix | Delete
'<a href="%1$s" aria-label="%2$s">%3$s</a>',
[103] Fix | Delete
esc_url( $post_link ),
[104] Fix | Delete
esc_attr( $title ),
[105] Fix | Delete
$featured_image
[106] Fix | Delete
);
[107] Fix | Delete
}
[108] Fix | Delete
$list_items_markup .= sprintf(
[109] Fix | Delete
'<div class="%1$s">%2$s</div>',
[110] Fix | Delete
esc_attr( $image_classes ),
[111] Fix | Delete
$featured_image
[112] Fix | Delete
);
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
$list_items_markup .= sprintf(
[116] Fix | Delete
'<a class="wp-block-latest-posts__post-title" href="%1$s">%2$s</a>',
[117] Fix | Delete
esc_url( $post_link ),
[118] Fix | Delete
$title
[119] Fix | Delete
);
[120] Fix | Delete
[121] Fix | Delete
if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
[122] Fix | Delete
$author_display_name = get_the_author_meta( 'display_name', $post->post_author );
[123] Fix | Delete
[124] Fix | Delete
/* translators: byline. %s: current author. */
[125] Fix | Delete
$byline = sprintf( __( 'by %s' ), $author_display_name );
[126] Fix | Delete
[127] Fix | Delete
if ( ! empty( $author_display_name ) ) {
[128] Fix | Delete
$list_items_markup .= sprintf(
[129] Fix | Delete
'<div class="wp-block-latest-posts__post-author">%1$s</div>',
[130] Fix | Delete
$byline
[131] Fix | Delete
);
[132] Fix | Delete
}
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
[136] Fix | Delete
$list_items_markup .= sprintf(
[137] Fix | Delete
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
[138] Fix | Delete
esc_attr( get_the_date( 'c', $post ) ),
[139] Fix | Delete
get_the_date( '', $post )
[140] Fix | Delete
);
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
[144] Fix | Delete
&& isset( $attributes['displayPostContentRadio'] ) && 'excerpt' === $attributes['displayPostContentRadio'] ) {
[145] Fix | Delete
[146] Fix | Delete
$trimmed_excerpt = get_the_excerpt( $post );
[147] Fix | Delete
[148] Fix | Delete
/*
[149] Fix | Delete
* Adds a "Read more" link with screen reader text.
[150] Fix | Delete
* [&hellip;] is the default excerpt ending from wp_trim_excerpt() in Core.
[151] Fix | Delete
*/
[152] Fix | Delete
if ( str_ends_with( $trimmed_excerpt, ' [&hellip;]' ) ) {
[153] Fix | Delete
/** This filter is documented in wp-includes/formatting.php */
[154] Fix | Delete
$excerpt_length = (int) apply_filters( 'excerpt_length', $block_core_latest_posts_excerpt_length );
[155] Fix | Delete
if ( $excerpt_length <= $block_core_latest_posts_excerpt_length ) {
[156] Fix | Delete
$trimmed_excerpt = substr( $trimmed_excerpt, 0, -11 );
[157] Fix | Delete
$trimmed_excerpt .= sprintf(
[158] Fix | Delete
/* translators: 1: A URL to a post, 2: Hidden accessibility text: Post title */
[159] Fix | Delete
__( '… <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>' ),
[160] Fix | Delete
esc_url( $post_link ),
[161] Fix | Delete
esc_html( $title )
[162] Fix | Delete
);
[163] Fix | Delete
}
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
if ( post_password_required( $post ) ) {
[167] Fix | Delete
$trimmed_excerpt = __( 'This content is password protected.' );
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
$list_items_markup .= sprintf(
[171] Fix | Delete
'<div class="wp-block-latest-posts__post-excerpt">%1$s</div>',
[172] Fix | Delete
$trimmed_excerpt
[173] Fix | Delete
);
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent']
[177] Fix | Delete
&& isset( $attributes['displayPostContentRadio'] ) && 'full_post' === $attributes['displayPostContentRadio'] ) {
[178] Fix | Delete
[179] Fix | Delete
$post_content = html_entity_decode( $post->post_content, ENT_QUOTES, get_option( 'blog_charset' ) );
[180] Fix | Delete
[181] Fix | Delete
if ( post_password_required( $post ) ) {
[182] Fix | Delete
$post_content = __( 'This content is password protected.' );
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
$list_items_markup .= sprintf(
[186] Fix | Delete
'<div class="wp-block-latest-posts__post-full-content">%1$s</div>',
[187] Fix | Delete
wp_kses_post( $post_content )
[188] Fix | Delete
);
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
$list_items_markup .= "</li>\n";
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
remove_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 );
[195] Fix | Delete
[196] Fix | Delete
$classes = array( 'wp-block-latest-posts__list' );
[197] Fix | Delete
if ( isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
[198] Fix | Delete
$classes[] = 'is-grid';
[199] Fix | Delete
}
[200] Fix | Delete
if ( isset( $attributes['columns'] ) && 'grid' === $attributes['postLayout'] ) {
[201] Fix | Delete
$classes[] = 'columns-' . $attributes['columns'];
[202] Fix | Delete
}
[203] Fix | Delete
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
[204] Fix | Delete
$classes[] = 'has-dates';
[205] Fix | Delete
}
[206] Fix | Delete
if ( isset( $attributes['displayAuthor'] ) && $attributes['displayAuthor'] ) {
[207] Fix | Delete
$classes[] = 'has-author';
[208] Fix | Delete
}
[209] Fix | Delete
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
[210] Fix | Delete
$classes[] = 'has-link-color';
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
[214] Fix | Delete
[215] Fix | Delete
return sprintf(
[216] Fix | Delete
'<ul %1$s>%2$s</ul>',
[217] Fix | Delete
$wrapper_attributes,
[218] Fix | Delete
$list_items_markup
[219] Fix | Delete
);
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Registers the `core/latest-posts` block on server.
[224] Fix | Delete
*
[225] Fix | Delete
* @since 5.0.0
[226] Fix | Delete
*/
[227] Fix | Delete
function register_block_core_latest_posts() {
[228] Fix | Delete
register_block_type_from_metadata(
[229] Fix | Delete
__DIR__ . '/latest-posts',
[230] Fix | Delete
array(
[231] Fix | Delete
'render_callback' => 'render_block_core_latest_posts',
[232] Fix | Delete
)
[233] Fix | Delete
);
[234] Fix | Delete
}
[235] Fix | Delete
add_action( 'init', 'register_block_core_latest_posts' );
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* Handles outdated versions of the `core/latest-posts` block by converting
[239] Fix | Delete
* attribute `categories` from a numeric string to an array with key `id`.
[240] Fix | Delete
*
[241] Fix | Delete
* This is done to accommodate the changes introduced in #20781 that sought to
[242] Fix | Delete
* add support for multiple categories to the block. However, given that this
[243] Fix | Delete
* block is dynamic, the usual provisions for block migration are insufficient,
[244] Fix | Delete
* as they only act when a block is loaded in the editor.
[245] Fix | Delete
*
[246] Fix | Delete
* TODO: Remove when and if the bottom client-side deprecation for this block
[247] Fix | Delete
* is removed.
[248] Fix | Delete
*
[249] Fix | Delete
* @since 5.5.0
[250] Fix | Delete
*
[251] Fix | Delete
* @param array $block A single parsed block object.
[252] Fix | Delete
*
[253] Fix | Delete
* @return array The migrated block object.
[254] Fix | Delete
*/
[255] Fix | Delete
function block_core_latest_posts_migrate_categories( $block ) {
[256] Fix | Delete
if (
[257] Fix | Delete
'core/latest-posts' === $block['blockName'] &&
[258] Fix | Delete
! empty( $block['attrs']['categories'] ) &&
[259] Fix | Delete
is_string( $block['attrs']['categories'] )
[260] Fix | Delete
) {
[261] Fix | Delete
$block['attrs']['categories'] = array(
[262] Fix | Delete
array( 'id' => absint( $block['attrs']['categories'] ) ),
[263] Fix | Delete
);
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
return $block;
[267] Fix | Delete
}
[268] Fix | Delete
add_filter( 'render_block_data', 'block_core_latest_posts_migrate_categories' );
[269] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function