Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules/related-...
File: jetpack-related-posts.php
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* The Jetpack_RelatedPosts class.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
use Automattic\Jetpack\Assets;
[7] Fix | Delete
use Automattic\Jetpack\Blocks;
[8] Fix | Delete
use Automattic\Jetpack\Post_Media\Images;
[9] Fix | Delete
use Automattic\Jetpack\Status\Request;
[10] Fix | Delete
use Automattic\Jetpack\Sync\Settings;
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* The Jetpack_RelatedPosts class.
[14] Fix | Delete
*/
[15] Fix | Delete
class Jetpack_RelatedPosts {
[16] Fix | Delete
const VERSION = '20240116';
[17] Fix | Delete
const SHORTCODE = 'jetpack-related-posts';
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Instance of the class.
[21] Fix | Delete
*
[22] Fix | Delete
* @var Jetpack_RelatedPosts
[23] Fix | Delete
*/
[24] Fix | Delete
private static $instance = null;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Instance of the raw class (?).
[28] Fix | Delete
*
[29] Fix | Delete
* @var Jetpack_RelatedPosts
[30] Fix | Delete
*/
[31] Fix | Delete
private static $instance_raw = null;
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Creates and returns a static instance of Jetpack_RelatedPosts.
[35] Fix | Delete
*
[36] Fix | Delete
* @return Jetpack_RelatedPosts
[37] Fix | Delete
*/
[38] Fix | Delete
public static function init() {
[39] Fix | Delete
if ( ! self::$instance ) {
[40] Fix | Delete
if ( class_exists( 'WPCOM_RelatedPosts' ) && method_exists( 'WPCOM_RelatedPosts', 'init' ) ) {
[41] Fix | Delete
self::$instance = WPCOM_RelatedPosts::init();
[42] Fix | Delete
} else {
[43] Fix | Delete
self::$instance = new Jetpack_RelatedPosts();
[44] Fix | Delete
}
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
return self::$instance;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Creates and returns a static instance of Jetpack_RelatedPosts_Raw.
[52] Fix | Delete
*
[53] Fix | Delete
* @return Jetpack_RelatedPosts
[54] Fix | Delete
*/
[55] Fix | Delete
public static function init_raw() {
[56] Fix | Delete
if ( ! self::$instance_raw ) {
[57] Fix | Delete
if ( class_exists( 'WPCOM_RelatedPosts' ) && method_exists( 'WPCOM_RelatedPosts', 'init_raw' ) ) {
[58] Fix | Delete
self::$instance_raw = WPCOM_RelatedPosts::init_raw();
[59] Fix | Delete
} else {
[60] Fix | Delete
self::$instance_raw = new Jetpack_RelatedPosts_Raw();
[61] Fix | Delete
}
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
return self::$instance_raw;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Options.
[69] Fix | Delete
*
[70] Fix | Delete
* @var array $options
[71] Fix | Delete
*/
[72] Fix | Delete
protected $options;
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Allow feature toggle variable.
[76] Fix | Delete
*
[77] Fix | Delete
* @var bool
[78] Fix | Delete
*/
[79] Fix | Delete
protected $allow_feature_toggle;
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Blog character set.
[83] Fix | Delete
*
[84] Fix | Delete
* @var mixed
[85] Fix | Delete
*/
[86] Fix | Delete
protected $blog_charset;
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Convert character set.
[90] Fix | Delete
*
[91] Fix | Delete
* @var bool
[92] Fix | Delete
*/
[93] Fix | Delete
protected $convert_charset;
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Previous Post ID
[97] Fix | Delete
*
[98] Fix | Delete
* @var int
[99] Fix | Delete
*/
[100] Fix | Delete
protected $previous_post_id;
[101] Fix | Delete
[102] Fix | Delete
/**
[103] Fix | Delete
* Shortcode usage.
[104] Fix | Delete
*
[105] Fix | Delete
* @var bool
[106] Fix | Delete
*/
[107] Fix | Delete
protected $found_shortcode = false;
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Constructor for Jetpack_RelatedPosts.
[111] Fix | Delete
*
[112] Fix | Delete
* @uses get_option, add_action, apply_filters
[113] Fix | Delete
*/
[114] Fix | Delete
public function __construct() {
[115] Fix | Delete
$this->blog_charset = get_option( 'blog_charset' );
[116] Fix | Delete
$this->convert_charset = ( function_exists( 'iconv' ) && ! preg_match( '/^utf\-?8$/i', $this->blog_charset ) );
[117] Fix | Delete
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
[118] Fix | Delete
add_action( 'wp', array( $this, 'action_frontend_init' ) );
[119] Fix | Delete
[120] Fix | Delete
if ( ! class_exists( 'Jetpack_Media_Summary' ) ) {
[121] Fix | Delete
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media-summary.php';
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
// Add Related Posts to the REST API Post response.
[125] Fix | Delete
add_action( 'rest_api_init', array( $this, 'rest_register_related_posts' ) );
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Get the blog ID.
[130] Fix | Delete
*
[131] Fix | Delete
* @return mixed current blog id.
[132] Fix | Delete
*/
[133] Fix | Delete
protected function get_blog_id() {
[134] Fix | Delete
return Jetpack_Options::get_option( 'id' );
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* =================
[139] Fix | Delete
* ACTIONS & FILTERS
[140] Fix | Delete
* =================
[141] Fix | Delete
*/
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Add a checkbox field to Settings > Reading for enabling related posts.
[145] Fix | Delete
*
[146] Fix | Delete
* @action admin_init
[147] Fix | Delete
* @uses add_settings_field, __, register_setting, add_action
[148] Fix | Delete
*/
[149] Fix | Delete
public function action_admin_init() {
[150] Fix | Delete
[151] Fix | Delete
// Add the setting field [jetpack_relatedposts] and place it in Settings > Reading.
[152] Fix | Delete
add_settings_field( 'jetpack_relatedposts', '<span id="jetpack_relatedposts">' . __( 'Related posts', 'jetpack' ) . '</span>', array( $this, 'print_setting_html' ), 'reading' );
[153] Fix | Delete
register_setting( 'reading', 'jetpack_relatedposts', array( $this, 'parse_options' ) );
[154] Fix | Delete
add_action( 'admin_head', array( $this, 'print_setting_head' ) );
[155] Fix | Delete
[156] Fix | Delete
if ( 'options-reading.php' === $GLOBALS['pagenow'] ) {
[157] Fix | Delete
// Enqueue style for live preview on the reading settings page.
[158] Fix | Delete
$this->enqueue_assets( false, true );
[159] Fix | Delete
}
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Load related posts assets if it's an eligible front end page or execute search and return JSON if it's an endpoint request.
[164] Fix | Delete
*
[165] Fix | Delete
* @global $_GET
[166] Fix | Delete
* @action wp
[167] Fix | Delete
* @uses add_shortcode, get_the_ID
[168] Fix | Delete
*/
[169] Fix | Delete
public function action_frontend_init() {
[170] Fix | Delete
// Add a shortcode handler that outputs nothing, this gets overridden later if we can display related content.
[171] Fix | Delete
add_shortcode( self::SHORTCODE, array( $this, 'get_client_rendered_html_unsupported' ) );
[172] Fix | Delete
[173] Fix | Delete
if ( ! $this->enabled_for_request() ) {
[174] Fix | Delete
return;
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
if ( isset( $_GET['relatedposts'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading and checking if we need to generate a list of excuded posts, does not update anything on the site.
[178] Fix | Delete
$excludes = $this->parse_numeric_get_arg( 'relatedposts_exclude' );
[179] Fix | Delete
$this->action_frontend_init_ajax( $excludes );
[180] Fix | Delete
} else {
[181] Fix | Delete
if ( isset( $_GET['relatedposts_hit'] ) && isset( $_GET['relatedposts_origin'] ) && isset( $_GET['relatedposts_position'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- checking if fields are set to setup tracking, nothing is changing on the site.
[182] Fix | Delete
$this->previous_post_id = (int) $_GET['relatedposts_origin']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- fetching a previous post ID for tracking, nothing is changing on the site.
[183] Fix | Delete
$this->log_click( $this->previous_post_id, get_the_ID(), sanitize_text_field( wp_unslash( $_GET['relatedposts_position'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- logging the click for tracking, nothing is changing on the site.
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
$this->action_frontend_init_page();
[187] Fix | Delete
}
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Render insertion point.
[192] Fix | Delete
*
[193] Fix | Delete
* @since 4.2.0
[194] Fix | Delete
*
[195] Fix | Delete
* @return string
[196] Fix | Delete
*/
[197] Fix | Delete
public function get_headline() {
[198] Fix | Delete
$options = $this->get_options();
[199] Fix | Delete
[200] Fix | Delete
if ( ! empty( $options['show_headline'] ) ) {
[201] Fix | Delete
$headline = sprintf(
[202] Fix | Delete
/** This filter is already documented in modules/sharedaddy/sharing-service.php */
[203] Fix | Delete
apply_filters( 'jetpack_sharing_headline_html', '<h3 class="jp-relatedposts-headline"><em>%s</em></h3>', esc_html( $options['headline'] ), 'related-posts' ),
[204] Fix | Delete
esc_html( $options['headline'] )
[205] Fix | Delete
);
[206] Fix | Delete
} else {
[207] Fix | Delete
$headline = '';
[208] Fix | Delete
}
[209] Fix | Delete
return $headline;
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
/**
[213] Fix | Delete
* Adds a target to the post content to load related posts into if a shortcode for it did not already exist.
[214] Fix | Delete
* Will skip adding the target if the post content contains a Related Posts block, if the 'get_the_excerpt'
[215] Fix | Delete
* hook is in the current filter list, or if the site is running an FSE/Site Editor theme.
[216] Fix | Delete
*
[217] Fix | Delete
* @filter the_content
[218] Fix | Delete
*
[219] Fix | Delete
* @param string $content Post content.
[220] Fix | Delete
*
[221] Fix | Delete
* @return string
[222] Fix | Delete
*/
[223] Fix | Delete
public function filter_add_target_to_dom( $content ) {
[224] Fix | Delete
// Do not output related posts for ActivityPub requests.
[225] Fix | Delete
if (
[226] Fix | Delete
function_exists( '\Activitypub\is_activitypub_request' )
[227] Fix | Delete
&& \Activitypub\is_activitypub_request()
[228] Fix | Delete
) {
[229] Fix | Delete
return $content;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
if ( has_block( 'jetpack/related-posts' ) || Blocks::is_fse_theme() ) {
[233] Fix | Delete
return $content;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
if ( ! $this->found_shortcode && ! doing_filter( 'get_the_excerpt' ) ) {
[237] Fix | Delete
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
[238] Fix | Delete
$content .= "\n" . $this->get_server_rendered_html();
[239] Fix | Delete
} else {
[240] Fix | Delete
$content .= "\n" . $this->get_client_rendered_html();
[241] Fix | Delete
}
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
return $content;
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Render static markup based on the Gutenberg block code
[249] Fix | Delete
*
[250] Fix | Delete
* @return string Rendered related posts HTML.
[251] Fix | Delete
*/
[252] Fix | Delete
public function get_server_rendered_html() {
[253] Fix | Delete
$rp_settings = $this->get_options();
[254] Fix | Delete
$block_rp_settings = array(
[255] Fix | Delete
'displayThumbnails' => $rp_settings['show_thumbnails'],
[256] Fix | Delete
'showHeadline' => $rp_settings['show_headline'],
[257] Fix | Delete
'displayDate' => isset( $rp_settings['show_date'] ) ? (bool) $rp_settings['show_date'] : true,
[258] Fix | Delete
'displayContext' => isset( $rp_settings['show_context'] ) && $rp_settings['show_context'],
[259] Fix | Delete
'postLayout' => isset( $rp_settings['layout'] ) ? $rp_settings['layout'] : 'grid',
[260] Fix | Delete
'postsToShow' => isset( $rp_settings['size'] ) ? $rp_settings['size'] : 3,
[261] Fix | Delete
/** This filter is already documented in modules/related-posts/jetpack-related-posts.php */
[262] Fix | Delete
'headline' => apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() ),
[263] Fix | Delete
'isServerRendered' => true,
[264] Fix | Delete
);
[265] Fix | Delete
[266] Fix | Delete
return $this->render_block( $block_rp_settings, '' );
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
/**
[270] Fix | Delete
* Looks for our shortcode on the unfiltered content, this has to execute early.
[271] Fix | Delete
*
[272] Fix | Delete
* @filter the_content
[273] Fix | Delete
* @param string $content - content of the post.
[274] Fix | Delete
* @uses has_shortcode
[275] Fix | Delete
* @return string $content
[276] Fix | Delete
*/
[277] Fix | Delete
public function test_for_shortcode( $content ) {
[278] Fix | Delete
$this->found_shortcode = has_shortcode( $content, self::SHORTCODE );
[279] Fix | Delete
[280] Fix | Delete
return $content;
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/**
[284] Fix | Delete
* Returns the HTML for the related posts section.
[285] Fix | Delete
*
[286] Fix | Delete
* @uses esc_html__, apply_filters
[287] Fix | Delete
* @return string
[288] Fix | Delete
*/
[289] Fix | Delete
public function get_client_rendered_html() {
[290] Fix | Delete
if ( Settings::is_syncing() ) {
[291] Fix | Delete
return '';
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
/**
[295] Fix | Delete
* Filter the Related Posts headline.
[296] Fix | Delete
*
[297] Fix | Delete
* @module related-posts
[298] Fix | Delete
*
[299] Fix | Delete
* @since 3.0.0
[300] Fix | Delete
*
[301] Fix | Delete
* @param string $headline Related Posts heading.
[302] Fix | Delete
*/
[303] Fix | Delete
$headline = apply_filters( 'jetpack_relatedposts_filter_headline', $this->get_headline() );
[304] Fix | Delete
[305] Fix | Delete
if ( $this->previous_post_id ) {
[306] Fix | Delete
$exclude = "data-exclude='{$this->previous_post_id}'";
[307] Fix | Delete
} else {
[308] Fix | Delete
$exclude = '';
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
return <<<EOT
[312] Fix | Delete
<div id='jp-relatedposts' class='jp-relatedposts' $exclude>
[313] Fix | Delete
$headline
[314] Fix | Delete
</div>
[315] Fix | Delete
EOT;
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Returns the HTML for the related posts section if it's running in the loop or other instances where we don't support related posts.
[320] Fix | Delete
*
[321] Fix | Delete
* @return string
[322] Fix | Delete
*/
[323] Fix | Delete
public function get_client_rendered_html_unsupported() {
[324] Fix | Delete
if ( Settings::is_syncing() ) {
[325] Fix | Delete
return '';
[326] Fix | Delete
}
[327] Fix | Delete
return "\n\n<!-- Jetpack Related Posts is not supported in this context. -->\n\n";
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
/**
[331] Fix | Delete
* ===============
[332] Fix | Delete
* GUTENBERG BLOCK
[333] Fix | Delete
* ===============
[334] Fix | Delete
*/
[335] Fix | Delete
[336] Fix | Delete
/**
[337] Fix | Delete
* Echoes out items for the Gutenberg block
[338] Fix | Delete
*
[339] Fix | Delete
* @param array $related_post The post object.
[340] Fix | Delete
* @param array $block_attributes The block attributes.
[341] Fix | Delete
*/
[342] Fix | Delete
public function render_block_item( $related_post, $block_attributes ) {
[343] Fix | Delete
$instance_id = 'related-posts-item-' . uniqid();
[344] Fix | Delete
$label_id = $instance_id . '-label';
[345] Fix | Delete
$title = $related_post['title'];
[346] Fix | Delete
$url = $related_post['url'];
[347] Fix | Delete
$rel = $related_post['rel'];
[348] Fix | Delete
$img = '';
[349] Fix | Delete
$list = '';
[350] Fix | Delete
[351] Fix | Delete
$item_markup = sprintf(
[352] Fix | Delete
'<li id="%1$s" class="jp-related-posts-i2__post">',
[353] Fix | Delete
esc_attr( $instance_id )
[354] Fix | Delete
);
[355] Fix | Delete
[356] Fix | Delete
// Thumbnail
[357] Fix | Delete
if ( ! empty( $block_attributes['show_thumbnails'] ) && ! empty( $related_post['img']['src'] ) ) {
[358] Fix | Delete
$img = sprintf(
[359] Fix | Delete
'<img loading="lazy" class="jp-related-posts-i2__post-img" src="%1$s" alt="%2$s" %3$s/>',
[360] Fix | Delete
esc_url( $related_post['img']['src'] ),
[361] Fix | Delete
esc_attr( $related_post['img']['alt_text'] ),
[362] Fix | Delete
( ! empty( $related_post['img']['srcset'] ) ? 'srcset="' . esc_attr( $related_post['img']['srcset'] ) . '"' : '' )
[363] Fix | Delete
);
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
// Link
[367] Fix | Delete
$item_markup .= sprintf(
[368] Fix | Delete
'<a id="%1$s" href="%2$s" class="jp-related-posts-i2__post-link" %3$s>%4$s%5$s</a>',
[369] Fix | Delete
esc_attr( $label_id ),
[370] Fix | Delete
esc_url( $url ),
[371] Fix | Delete
( ! empty( $rel ) ? 'rel="' . esc_attr( $rel ) . '"' : '' ),
[372] Fix | Delete
esc_html( $title ),
[373] Fix | Delete
$img
[374] Fix | Delete
);
[375] Fix | Delete
[376] Fix | Delete
// Date
[377] Fix | Delete
if ( $block_attributes['show_date'] ) {
[378] Fix | Delete
$list .= '<dt>' . __( 'Date', 'jetpack' ) . '</dt>';
[379] Fix | Delete
$list .= '<dd class="jp-related-posts-i2__post-date">';
[380] Fix | Delete
$list .= esc_html( $related_post['date'] );
[381] Fix | Delete
$list .= '</dd>';
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
// Author
[385] Fix | Delete
if ( $block_attributes['show_author'] ) {
[386] Fix | Delete
$list .= '<dt>' . __( 'Author', 'jetpack' ) . '</dt>';
[387] Fix | Delete
$list .= '<dd class="jp-related-posts-i2__post-author">';
[388] Fix | Delete
$list .= esc_html( $related_post['author'] );
[389] Fix | Delete
$list .= '</dd>';
[390] Fix | Delete
}
[391] Fix | Delete
[392] Fix | Delete
// Context
[393] Fix | Delete
if ( ( $block_attributes['show_context'] ) && ! empty( $related_post['block_context'] ) ) {
[394] Fix | Delete
// translators: this is followed by the reason why the item is related to the current post
[395] Fix | Delete
$list .= '<dt>' . __( 'In relation to', 'jetpack' ) . '</dt>';
[396] Fix | Delete
$list .= '<dd class="jp-related-posts-i2__post-context">';
[397] Fix | Delete
[398] Fix | Delete
// Note: The original 'context' value is not used when rendering the block.
[399] Fix | Delete
// It is still generated and available for the legacy rendering code path though.
[400] Fix | Delete
// See './related-posts.js' for that usage.
[401] Fix | Delete
$block_context = $related_post['block_context'];
[402] Fix | Delete
[403] Fix | Delete
if ( ! empty( $block_context['link'] ) ) {
[404] Fix | Delete
$list .= sprintf(
[405] Fix | Delete
'<a href="%1$s">%2$s</a>',
[406] Fix | Delete
esc_url( $block_context['link'] ),
[407] Fix | Delete
esc_html( $block_context['text'] )
[408] Fix | Delete
);
[409] Fix | Delete
} else {
[410] Fix | Delete
$list .= esc_html( $block_context['text'] );
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
$list .= '</dd>';
[414] Fix | Delete
}
[415] Fix | Delete
[416] Fix | Delete
// Metadata
[417] Fix | Delete
if ( ! empty( $list ) ) {
[418] Fix | Delete
$item_markup .= '<dl class="jp-related-posts-i2__post-defs">' . $list . '</dl>';
[419] Fix | Delete
}
[420] Fix | Delete
[421] Fix | Delete
$item_markup .= '</li>';
[422] Fix | Delete
[423] Fix | Delete
return $item_markup;
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
/**
[427] Fix | Delete
* Render the list of related posts.
[428] Fix | Delete
*
[429] Fix | Delete
* @param array $posts The posts to render into the list.
[430] Fix | Delete
* @param array $block_attributes Block attributes.
[431] Fix | Delete
* @return string
[432] Fix | Delete
*/
[433] Fix | Delete
public function render_post_list( $posts, $block_attributes ) {
[434] Fix | Delete
$markup = '';
[435] Fix | Delete
[436] Fix | Delete
foreach ( $posts as $post ) {
[437] Fix | Delete
$markup .= $this->render_block_item( $post, $block_attributes );
[438] Fix | Delete
}
[439] Fix | Delete
[440] Fix | Delete
return sprintf(
[441] Fix | Delete
// role="list" is required for accessibility as VoiceOver ignores unstyled lists.
[442] Fix | Delete
'<ul class="jp-related-posts-i2__list" role="list" data-post-count="%1$s">%2$s</ul>',
[443] Fix | Delete
count( $posts ),
[444] Fix | Delete
$markup
[445] Fix | Delete
);
[446] Fix | Delete
}
[447] Fix | Delete
[448] Fix | Delete
/**
[449] Fix | Delete
* Render the related posts markup.
[450] Fix | Delete
*
[451] Fix | Delete
* @param array $attributes Block attributes.
[452] Fix | Delete
* @param string $content String containing the related Posts block content.
[453] Fix | Delete
* @param WP_Block $block The block object.
[454] Fix | Delete
* @return string
[455] Fix | Delete
*/
[456] Fix | Delete
public function render_block( $attributes, $content, $block = null ) {
[457] Fix | Delete
if ( ! Request::is_frontend() ) {
[458] Fix | Delete
return $content;
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
$wrapper_attributes = array();
[462] Fix | Delete
$post_id = get_the_ID();
[463] Fix | Delete
$block_attributes = array(
[464] Fix | Delete
'headline' => isset( $attributes['headline'] ) ? $attributes['headline'] : null,
[465] Fix | Delete
'show_thumbnails' => isset( $attributes['displayThumbnails'] ) && $attributes['displayThumbnails'],
[466] Fix | Delete
'show_author' => isset( $attributes['displayAuthor'] ) ? (bool) $attributes['displayAuthor'] : false,
[467] Fix | Delete
'show_headline' => isset( $attributes['displayHeadline'] ) ? (bool) $attributes['displayHeadline'] : false,
[468] Fix | Delete
'show_date' => isset( $attributes['displayDate'] ) ? (bool) $attributes['displayDate'] : true,
[469] Fix | Delete
'show_context' => isset( $attributes['displayContext'] ) && $attributes['displayContext'],
[470] Fix | Delete
'layout' => isset( $attributes['postLayout'] ) && 'list' === $attributes['postLayout'] ? $attributes['postLayout'] : 'grid',
[471] Fix | Delete
'size' => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3,
[472] Fix | Delete
);
[473] Fix | Delete
[474] Fix | Delete
$excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' );
[475] Fix | Delete
[476] Fix | Delete
$related_posts = $this->get_for_post_id(
[477] Fix | Delete
$post_id,
[478] Fix | Delete
array(
[479] Fix | Delete
'size' => $block_attributes['size'],
[480] Fix | Delete
'exclude_post_ids' => $excludes,
[481] Fix | Delete
)
[482] Fix | Delete
);
[483] Fix | Delete
[484] Fix | Delete
if ( empty( $related_posts ) ) {
[485] Fix | Delete
return '';
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
$list_markup = $this->render_post_list( $related_posts, $block_attributes );
[489] Fix | Delete
[490] Fix | Delete
if ( empty( $attributes['isServerRendered'] ) ) {
[491] Fix | Delete
// The get_server_rendered_html() path won't register a block,
[492] Fix | Delete
// so only apply block supports when not server rendered.
[493] Fix | Delete
$wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
$headline_markup = '';
[497] Fix | Delete
[498] Fix | Delete
if ( isset( $block ) ) {
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function