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