Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/extensio.../blocks/top-post...
File: top-posts.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Top Posts Block.
[2] Fix | Delete
*
[3] Fix | Delete
* @since 13.0
[4] Fix | Delete
*
[5] Fix | Delete
* @package automattic/jetpack
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
namespace Automattic\Jetpack\Extensions\Top_Posts;
[9] Fix | Delete
[10] Fix | Delete
use Automattic\Jetpack\Blocks;
[11] Fix | Delete
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
[12] Fix | Delete
use Automattic\Jetpack\Modules;
[13] Fix | Delete
use Automattic\Jetpack\Status;
[14] Fix | Delete
use Automattic\Jetpack\Status\Host;
[15] Fix | Delete
use Automattic\Jetpack\Status\Request;
[16] Fix | Delete
use Jetpack_Gutenberg;
[17] Fix | Delete
use Jetpack_Top_Posts_Helper;
[18] Fix | Delete
[19] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[20] Fix | Delete
exit( 0 );
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
if ( ! class_exists( 'Jetpack_Top_Posts_Helper' ) ) {
[24] Fix | Delete
require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-top-posts-helper.php';
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Registers the block for use in Gutenberg
[29] Fix | Delete
* This is done via an action so that we can disable
[30] Fix | Delete
* registration if we need to.
[31] Fix | Delete
*/
[32] Fix | Delete
function register_block() {
[33] Fix | Delete
/*
[34] Fix | Delete
* The block is available even when the module is not active,
[35] Fix | Delete
* so we can display a nudge to activate the module instead of the block.
[36] Fix | Delete
* However, since non-admins cannot activate modules, we do not display the empty block for them.
[37] Fix | Delete
*/
[38] Fix | Delete
if ( ! ( new Modules() )->is_active( 'stats' ) && ! current_user_can( 'jetpack_activate_modules' ) ) {
[39] Fix | Delete
return;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
if ( ( new Host() )->is_wpcom_simple() || ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) {
[43] Fix | Delete
Blocks::jetpack_register_block(
[44] Fix | Delete
__DIR__,
[45] Fix | Delete
array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
[46] Fix | Delete
);
[47] Fix | Delete
}
[48] Fix | Delete
}
[49] Fix | Delete
add_action( 'init', __NAMESPACE__ . '\register_block' );
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Top Posts block registration/dependency declaration.
[53] Fix | Delete
*
[54] Fix | Delete
* @param array $attributes Array containing the Top Posts block attributes.
[55] Fix | Delete
*
[56] Fix | Delete
* @return string
[57] Fix | Delete
*/
[58] Fix | Delete
function load_assets( $attributes ) {
[59] Fix | Delete
// Do not render anything when the Stats module is not active.
[60] Fix | Delete
if ( ! ( new Modules() )->is_active( 'stats' ) ) {
[61] Fix | Delete
return;
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
// Do not render in contexts outside the front-end (eg. emails, API).
[65] Fix | Delete
if ( ! Request::is_frontend() ) {
[66] Fix | Delete
return;
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
[70] Fix | Delete
[71] Fix | Delete
/*
[72] Fix | Delete
* We cannot rely on obtaining posts from the block because
[73] Fix | Delete
* top posts might have changed since then. As such, we must
[74] Fix | Delete
* check for updated stats.
[75] Fix | Delete
*/
[76] Fix | Delete
$period = $attributes['period'];
[77] Fix | Delete
$number = $attributes['postsToShow'];
[78] Fix | Delete
$types = implode( ',', array_keys( array_filter( $attributes['postTypes'] ) ) );
[79] Fix | Delete
[80] Fix | Delete
$data = Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types );
[81] Fix | Delete
[82] Fix | Delete
if ( ! is_array( $data ) ) {
[83] Fix | Delete
return;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
$wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
[87] Fix | Delete
[88] Fix | Delete
$output = sprintf(
[89] Fix | Delete
'<div class="jetpack-top-posts%s%s%s"%sdata-item-count="%s"><div class="jetpack-top-posts-wrapper">',
[90] Fix | Delete
! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
[91] Fix | Delete
! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
[92] Fix | Delete
' is-' . esc_attr( $attributes['layout'] ) . '-layout',
[93] Fix | Delete
! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
[94] Fix | Delete
count( $data )
[95] Fix | Delete
);
[96] Fix | Delete
[97] Fix | Delete
foreach ( $data as $item ) {
[98] Fix | Delete
$output .= '<div class="jetpack-top-posts-item">';
[99] Fix | Delete
[100] Fix | Delete
if ( $attributes['displayThumbnail'] ) {
[101] Fix | Delete
$output .= '<a class="jetpack-top-posts-thumbnail-link" href="' . esc_url( $item['href'] ) . '">';
[102] Fix | Delete
[103] Fix | Delete
if ( ! empty( $item['thumbnail'] ) ) {
[104] Fix | Delete
$output .= '<img class="jetpack-top-posts-thumbnail" src="' . esc_url( $item['thumbnail'] ) . '" alt="' . esc_attr( $item['title'] ) . '">';
[105] Fix | Delete
} else {
[106] Fix | Delete
$output .= '<div class="jetpack-top-posts-mock-thumbnail"></div>';
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
$output .= '</a>';
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
$output .= '<span class="jetpack-top-posts-title"><a href="' . esc_url( $item['href'] ) . '">' . esc_html( $item['title'] ) . '</a></span>';
[113] Fix | Delete
[114] Fix | Delete
if ( $attributes['displayDate'] ) {
[115] Fix | Delete
$output .= '<span class="jetpack-top-posts-date has-small-font-size">' . esc_html( $item['date'] ) . '</span>';
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
if ( $attributes['displayAuthor'] ) {
[119] Fix | Delete
$output .= '<span class="jetpack-top-posts-author has-small-font-size">' . esc_html( $item['author'] ) . '</span>';
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
if ( $attributes['displayContext'] && ! empty( $item['context'] ) && is_array( $item['context'] ) ) {
[123] Fix | Delete
$context = reset( $item['context'] );
[124] Fix | Delete
$output .= '<span class="jetpack-top-posts-context has-small-font-size"><a href="' . esc_url( get_category_link( $context->term_id ) ) . '">' . esc_html( $context->name ) . '</a></span>';
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
$output .= '</div>';
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
$output .= '</div></div>';
[131] Fix | Delete
[132] Fix | Delete
return $output;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function