PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 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 All 507 releases
← All changes | extensions/blocks/blog-stats/blog-stats.php +27 -93 13.7.2 → 16.3-beta View file →
@@ -10,12 +10,16 @@
10 10 namespace Automattic\Jetpack\Extensions\Blog_Stats;
11 11
12 12 use Automattic\Jetpack\Blocks;
13 13 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
14 -use Automattic\Jetpack\Stats\WPCOM_Stats;
14 +use Automattic\Jetpack\Modules;
15 15 use Automattic\Jetpack\Status;
16 -use Jetpack_Gutenberg;
16 +use Automattic\Jetpack\Status\Host;
17 17
18 +if ( ! defined( 'ABSPATH' ) ) {
19 + exit( 0 );
20 +}
21 +
18 22 /**
19 23 * Registers the block for use in Gutenberg.
20 24 * This is done via an action so that we can disable
21 25 * registration if we need to.
@@ -20,9 +24,24 @@
20 24 * This is done via an action so that we can disable
21 25 * registration if we need to.
22 26 */
23 27 function register_block() {
24 - 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 (
38 + ( new Host() )->is_wpcom_simple()
39 + || (
40 + ( new Connection_Manager( 'jetpack' ) )->has_connected_owner()
41 + && ! ( new Status() )->is_offline_mode()
42 + )
43 + ) {
25 44 Blocks::jetpack_register_block(
26 45 __DIR__,
27 46 array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
28 47 );
@@ -32,100 +51,15 @@
32 51
33 52 /**
34 53 * Blog Stats block registration/dependency declaration.
35 54 *
55 + * The render implementation lives in render.php and is only loaded when the
56 + * block is actually rendered, keeping it out of the eager front-end path.
57 + *
36 58 * @param array $attributes Array containing the Blog Stats block attributes.
37 59 *
38 60 * @return string
39 61 */
40 62 function load_assets( $attributes ) {
41 - Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
42 -
43 - // For outside the front-end, such as within emails or the API.
44 - if ( ! jetpack_is_frontend() ) {
45 - return;
46 - }
47 -
48 - // For when Stats has been disabled subsequent to inserting the block.
49 - if ( ! \Jetpack::is_module_active( 'stats' ) ) {
50 - if ( current_user_can( 'edit_theme_options' ) ) {
51 - return sprintf(
52 - '<p>%s</p>',
53 - wp_kses(
54 - sprintf(
55 - /* translators: placeholder %s is a link to enable Jetpack Stats.. */
56 - __( 'Please <a href="%s">enable Jetpack Stats</a> to use this block.', 'jetpack' ),
57 - esc_url( admin_url( 'admin.php?page=jetpack_modules&module_tag=Jetpack%20Stats' ) )
58 - ),
59 - array( 'a' => array( 'href' => array() ) )
60 - )
61 - );
62 - }
63 -
64 - return;
65 - }
66 -
67 - // For when there's no post ID - eg. search pages.
68 - if ( $attributes['statsOption'] === 'post' && ! get_the_ID() ) {
69 - if ( current_user_can( 'edit_theme_options' ) ) {
70 - return sprintf(
71 - '<p>%s</p>',
72 - esc_html( __( 'There are no stats to display for this post.', 'jetpack' ) )
73 - );
74 - }
75 -
76 - return;
77 - }
78 -
79 - $stats = 0;
80 - $wpcom_stats = new WPCOM_Stats();
81 -
82 - if ( $attributes['statsOption'] === 'post' ) {
83 - // Cache in post meta to prevent wp_options blowing up when retrieving views
84 - // for multiple posts simultaneously (eg. when inserted into template).
85 - $cache_in_meta = true;
86 - $data = $wpcom_stats->convert_stats_array_to_object(
87 - $wpcom_stats->get_post_views(
88 - get_the_ID(),
89 - array( 'fields' => 'views' ),
90 - $cache_in_meta
91 - )
92 - );
93 -
94 - if ( isset( $data->views ) ) {
95 - $stats = $data->views;
96 - }
97 - } else {
98 - $data = $wpcom_stats->convert_stats_array_to_object(
99 - $wpcom_stats->get_stats( array( 'fields' => 'stats' ) )
100 - );
101 -
102 - if ( $attributes['statsData'] === 'views' && isset( $data->stats->views ) ) {
103 - $stats = $data->stats->views;
104 - }
105 -
106 - if ( $attributes['statsData'] === 'visitors' && isset( $data->stats->visitors ) ) {
107 - $stats = $data->stats->visitors;
108 - }
109 - }
110 -
111 - $fallback_label = $attributes['statsData'] === 'visitors' ? esc_html(
112 - /* Translators: Number of visitors */
113 - _n( 'visitor', 'visitors', $stats, 'jetpack' )
114 - ) : esc_html(
115 - /* Translators: Number of views */
116 - _n( 'hit', 'hits', $stats, 'jetpack' )
117 - );
118 -
119 - $label = empty( $attributes['label'] ) ? $fallback_label : $attributes['label'];
120 -
121 - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
122 -
123 - return sprintf(
124 - '<div class="jetpack-blog-stats%s%s"%s><p>%s %s</p></div>',
125 - ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
126 - ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
127 - ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
128 - esc_html( number_format_i18n( $stats ) ),
129 - wp_kses_post( $label )
130 - );
63 + require_once __DIR__ . '/render.php';
64 + return render_implementation( $attributes );
131 65 }