PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | extensions/blocks/top-posts/top-posts.php +18 -80 13.4.5 → 16.3-a.5 View file →
@@ -10,15 +10,14 @@
10 10 namespace Automattic\Jetpack\Extensions\Top_Posts;
11 11
12 12 use Automattic\Jetpack\Blocks;
13 13 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
14 +use Automattic\Jetpack\Modules;
14 15 use Automattic\Jetpack\Status;
15 16 use Automattic\Jetpack\Status\Host;
16 -use Jetpack_Gutenberg;
17 -use Jetpack_Top_Posts_Helper;
18 17
19 -if ( ! class_exists( 'Jetpack_Top_Posts_Helper' ) ) {
20 - require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-top-posts-helper.php';
18 +if ( ! defined( 'ABSPATH' ) ) {
19 + exit( 0 );
21 20 }
22 21
23 22 /**
24 23 * Registers the block for use in Gutenberg
@@ -25,15 +24,18 @@
25 24 * This is done via an action so that we can disable
26 25 * registration if we need to.
27 26 */
28 27 function register_block() {
29 - if (
30 - ( new Host() )->is_wpcom_simple()
31 - || (
32 - ( new Connection_Manager( 'jetpack' ) )->has_connected_owner()
33 - && ! ( new Status() )->is_offline_mode()
34 - )
35 - ) {
28 + /*
29 + * The block is available even when the module is not active,
30 + * so we can display a nudge to activate the module instead of the block.
31 + * However, since non-admins cannot activate modules, we do not display the empty block for them.
32 + */
33 + if ( ! ( new Modules() )->is_active( 'stats' ) && ! current_user_can( 'jetpack_activate_modules' ) ) {
34 + return;
35 + }
36 +
37 + if ( ( new Host() )->is_wpcom_simple() || ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) {
36 38 Blocks::jetpack_register_block(
37 39 __DIR__,
38 40 array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
39 41 );
@@ -43,79 +45,15 @@
43 45
44 46 /**
45 47 * Top Posts block registration/dependency declaration.
46 48 *
49 + * The render implementation lives in render.php and is only loaded when the
50 + * block is actually rendered, keeping it out of the eager front-end path.
51 + *
47 52 * @param array $attributes Array containing the Top Posts block attributes.
48 53 *
49 54 * @return string
50 55 */
51 56 function load_assets( $attributes ) {
52 - // Do not render in contexts outside the front-end (eg. emails, API).
53 - if ( ! jetpack_is_frontend() ) {
54 - return;
55 - }
56 -
57 - Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
58 -
59 - /*
60 - * We cannot rely on obtaining posts from the block because
61 - * top posts might have changed since then. As such, we must
62 - * check for updated stats.
63 - */
64 - $period = $attributes['period'];
65 - $number = $attributes['postsToShow'];
66 - $types = implode( ',', array_keys( array_filter( $attributes['postTypes'] ) ) );
67 -
68 - $data = Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types );
69 -
70 - if ( ! is_array( $data ) ) {
71 - return;
72 - }
73 -
74 - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
75 -
76 - $output = sprintf(
77 - '<div class="jetpack-top-posts%s%s%s"%sdata-item-count="%s"><div class="jetpack-top-posts-wrapper">',
78 - ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
79 - ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
80 - ' is-' . esc_attr( $attributes['layout'] ) . '-layout',
81 - ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
82 - count( $data )
83 - );
84 -
85 - foreach ( $data as $item ) {
86 - $output .= '<div class="jetpack-top-posts-item">';
87 -
88 - if ( $attributes['displayThumbnail'] ) {
89 - $output .= '<a class="jetpack-top-posts-thumbnail-link" href="' . esc_url( $item['href'] ) . '">';
90 -
91 - if ( ! empty( $item['thumbnail'] ) ) {
92 - $output .= '<img class="jetpack-top-posts-thumbnail" src="' . esc_url( $item['thumbnail'] ) . '" alt="' . esc_attr( $item['title'] ) . '">';
93 - } else {
94 - $output .= '<div class="jetpack-top-posts-mock-thumbnail"></div>';
95 - }
96 -
97 - $output .= '</a>';
98 - }
99 -
100 - $output .= '<span class="jetpack-top-posts-title"><a href="' . esc_url( $item['href'] ) . '">' . esc_html( $item['title'] ) . '</a></span>';
101 -
102 - if ( $attributes['displayDate'] ) {
103 - $output .= '<span class="jetpack-top-posts-date has-small-font-size">' . esc_html( $item['date'] ) . '</span>';
104 - }
105 -
106 - if ( $attributes['displayAuthor'] ) {
107 - $output .= '<span class="jetpack-top-posts-author has-small-font-size">' . esc_html( $item['author'] ) . '</span>';
108 - }
109 -
110 - if ( $attributes['displayContext'] && ! empty( $item['context'] ) && is_array( $item['context'] ) ) {
111 - $context = reset( $item['context'] );
112 - $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>';
113 - }
114 -
115 - $output .= '</div>';
116 - }
117 -
118 - $output .= '</div></div>';
119 -
120 - return $output;
57 + require_once __DIR__ . '/render.php';
58 + return load_assets_implementation( $attributes );
121 59 }