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