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 -75 13.2.4 → 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,74 +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 - Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
53 -
54 - /*
55 - * We cannot rely on obtaining posts from the block because
56 - * top posts might have changed since then. As such, we must
57 - * check for updated stats.
58 - */
59 - $period = $attributes['period'];
60 - $number = $attributes['postsToShow'];
61 - $types = implode( ',', array_keys( array_filter( $attributes['postTypes'] ) ) );
62 -
63 - $data = Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types );
64 -
65 - if ( ! is_array( $data ) ) {
66 - return;
67 - }
68 -
69 - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
70 -
71 - $output = sprintf(
72 - '<div class="jetpack-top-posts%s%s%s"%sdata-item-count="%s"><div class="jetpack-top-posts-wrapper">',
73 - ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
74 - ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
75 - ' is-' . esc_attr( $attributes['layout'] ) . '-layout',
76 - ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
77 - count( $data )
78 - );
79 -
80 - foreach ( $data as $item ) {
81 - $output .= '<div class="jetpack-top-posts-item">';
82 -
83 - if ( $attributes['displayThumbnail'] ) {
84 - $output .= '<a class="jetpack-top-posts-thumbnail-link" href="' . esc_url( $item['href'] ) . '">';
85 -
86 - if ( ! empty( $item['thumbnail'] ) ) {
87 - $output .= '<img class="jetpack-top-posts-thumbnail" src="' . esc_url( $item['thumbnail'] ) . '" alt="' . esc_attr( $item['title'] ) . '">';
88 - } else {
89 - $output .= '<div class="jetpack-top-posts-mock-thumbnail"></div>';
90 - }
91 -
92 - $output .= '</a>';
93 - }
94 -
95 - $output .= '<span class="jetpack-top-posts-title"><a href="' . esc_url( $item['href'] ) . '">' . esc_html( $item['title'] ) . '</a></span>';
96 -
97 - if ( $attributes['displayDate'] ) {
98 - $output .= '<span class="jetpack-top-posts-date has-small-font-size">' . esc_html( $item['date'] ) . '</span>';
99 - }
100 -
101 - if ( $attributes['displayAuthor'] ) {
102 - $output .= '<span class="jetpack-top-posts-author has-small-font-size">' . esc_html( $item['author'] ) . '</span>';
103 - }
104 -
105 - if ( $attributes['displayContext'] && ! empty( $item['context'] ) && is_array( $item['context'] ) ) {
106 - $context = reset( $item['context'] );
107 - $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>';
108 - }
109 -
110 - $output .= '</div>';
111 - }
112 -
113 - $output .= '</div></div>';
114 -
115 - return $output;
57 + require_once __DIR__ . '/render.php';
58 + return load_assets_implementation( $attributes );
116 59 }