PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
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 +19 -74 13.7.2 → 16.3-a.7 View file →
@@ -10,14 +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 -use Jetpack_Gutenberg;
16 -use Jetpack_Top_Posts_Helper;
16 +use Automattic\Jetpack\Status\Host;
17 17
18 -if ( ! class_exists( 'Jetpack_Top_Posts_Helper' ) ) {
19 - require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-top-posts-helper.php';
18 +if ( ! defined( 'ABSPATH' ) ) {
19 + exit( 0 );
20 20 }
21 21
22 22 /**
23 23 * Registers the block for use in Gutenberg
@@ -24,9 +24,18 @@
24 24 * This is done via an action so that we can disable
25 25 * registration if we need to.
26 26 */
27 27 function register_block() {
28 - if ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) {
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() ) {
29 38 Blocks::jetpack_register_block(
30 39 __DIR__,
31 40 array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
32 41 );
@@ -36,79 +45,15 @@
36 45
37 46 /**
38 47 * Top Posts block registration/dependency declaration.
39 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 + *
40 52 * @param array $attributes Array containing the Top Posts block attributes.
41 53 *
42 54 * @return string
43 55 */
44 56 function load_assets( $attributes ) {
45 - // Do not render in contexts outside the front-end (eg. emails, API).
46 - if ( ! jetpack_is_frontend() ) {
47 - return;
48 - }
49 -
50 - Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
51 -
52 - /*
53 - * We cannot rely on obtaining posts from the block because
54 - * top posts might have changed since then. As such, we must
55 - * check for updated stats.
56 - */
57 - $period = $attributes['period'];
58 - $number = $attributes['postsToShow'];
59 - $types = implode( ',', array_keys( array_filter( $attributes['postTypes'] ) ) );
60 -
61 - $data = Jetpack_Top_Posts_Helper::get_top_posts( $period, $number, $types );
62 -
63 - if ( ! is_array( $data ) ) {
64 - return;
65 - }
66 -
67 - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
68 -
69 - $output = sprintf(
70 - '<div class="jetpack-top-posts%s%s%s"%sdata-item-count="%s"><div class="jetpack-top-posts-wrapper">',
71 - ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
72 - ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
73 - ' is-' . esc_attr( $attributes['layout'] ) . '-layout',
74 - ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
75 - count( $data )
76 - );
77 -
78 - foreach ( $data as $item ) {
79 - $output .= '<div class="jetpack-top-posts-item">';
80 -
81 - if ( $attributes['displayThumbnail'] ) {
82 - $output .= '<a class="jetpack-top-posts-thumbnail-link" href="' . esc_url( $item['href'] ) . '">';
83 -
84 - if ( ! empty( $item['thumbnail'] ) ) {
85 - $output .= '<img class="jetpack-top-posts-thumbnail" src="' . esc_url( $item['thumbnail'] ) . '" alt="' . esc_attr( $item['title'] ) . '">';
86 - } else {
87 - $output .= '<div class="jetpack-top-posts-mock-thumbnail"></div>';
88 - }
89 -
90 - $output .= '</a>';
91 - }
92 -
93 - $output .= '<span class="jetpack-top-posts-title"><a href="' . esc_url( $item['href'] ) . '">' . esc_html( $item['title'] ) . '</a></span>';
94 -
95 - if ( $attributes['displayDate'] ) {
96 - $output .= '<span class="jetpack-top-posts-date has-small-font-size">' . esc_html( $item['date'] ) . '</span>';
97 - }
98 -
99 - if ( $attributes['displayAuthor'] ) {
100 - $output .= '<span class="jetpack-top-posts-author has-small-font-size">' . esc_html( $item['author'] ) . '</span>';
101 - }
102 -
103 - if ( $attributes['displayContext'] && ! empty( $item['context'] ) && is_array( $item['context'] ) ) {
104 - $context = reset( $item['context'] );
105 - $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>';
106 - }
107 -
108 - $output .= '</div>';
109 - }
110 -
111 - $output .= '</div></div>';
112 -
113 - return $output;
57 + require_once __DIR__ . '/render.php';
58 + return load_assets_implementation( $attributes );
114 59 }