| @@ -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,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,12 +24,23 @@ | ||
| 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() { |
| 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 | + | |
| 24 | 37 | if ( |
| 25 | - ! defined( 'IS_WPCOM' ) | |
| 26 | - && ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() | |
| 27 | - && ! ( new Status() )->is_offline_mode() ) | |
| 38 | + ( new Host() )->is_wpcom_simple() | |
| 39 | + || ( | |
| 40 | + ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() | |
| 41 | + && ! ( new Status() )->is_offline_mode() | |
| 42 | + ) | |
| 28 | 43 | ) { |
| 29 | 44 | Blocks::jetpack_register_block( |
| 30 | 45 | __DIR__, |
| 31 | 46 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| @@ -36,71 +51,15 @@ | ||
| 36 | 51 | |
| 37 | 52 | /** |
| 38 | 53 | * Blog Stats block registration/dependency declaration. |
| 39 | 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 | + * | |
| 40 | 58 | * @param array $attributes Array containing the Blog Stats block attributes. |
| 41 | 59 | * |
| 42 | 60 | * @return string |
| 43 | 61 | */ |
| 44 | 62 | function load_assets( $attributes ) { |
| 45 | - Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 46 | - | |
| 47 | - $views = 0; | |
| 48 | - | |
| 49 | - // For when there's no post ID - eg. search pages. | |
| 50 | - if ( $attributes['statsOption'] === 'post' && ! get_the_ID() ) { | |
| 51 | - if ( current_user_can( 'edit_theme_options' ) ) { | |
| 52 | - return sprintf( | |
| 53 | - '<p>%s</p>', | |
| 54 | - esc_html( __( 'There are no stats to display for this post.', 'jetpack' ) ) | |
| 55 | - ); | |
| 56 | - } | |
| 57 | - | |
| 58 | - return; | |
| 59 | - } | |
| 60 | - | |
| 61 | - if ( $attributes['statsOption'] === 'post' ) { | |
| 62 | - // Cache in post meta to prevent wp_options blowing up when retrieving views | |
| 63 | - // for multiple posts simultaneously (eg. when inserted into template). | |
| 64 | - $cache_in_meta = true; | |
| 65 | - $data = convert_stats_array_to_object( | |
| 66 | - ( new WPCOM_Stats() )->get_post_views( | |
| 67 | - get_the_ID(), | |
| 68 | - array( 'fields' => 'views' ), | |
| 69 | - $cache_in_meta | |
| 70 | - ) | |
| 71 | - ); | |
| 72 | - | |
| 73 | - if ( isset( $data->views ) ) { | |
| 74 | - $views = $data->views; | |
| 75 | - } | |
| 76 | - } else { | |
| 77 | - $data = convert_stats_array_to_object( | |
| 78 | - ( new WPCOM_Stats() )->get_stats( array( 'fields' => 'stats' ) ) | |
| 79 | - ); | |
| 80 | - | |
| 81 | - if ( isset( $data->stats->views ) ) { | |
| 82 | - $views = $data->stats->views; | |
| 83 | - } | |
| 84 | - } | |
| 85 | - | |
| 86 | - $label = $attributes['label'] ? $attributes['label'] : esc_html( | |
| 87 | - /* Translators: Number of views */ | |
| 88 | - _n( | |
| 89 | - 'hit', | |
| 90 | - 'hits', | |
| 91 | - $views, | |
| 92 | - 'jetpack' | |
| 93 | - ) | |
| 94 | - ); | |
| 95 | - | |
| 96 | - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); | |
| 97 | - | |
| 98 | - return sprintf( | |
| 99 | - '<div class="jetpack-blog-stats%s%s"%s><p>%s %s</p></div>', | |
| 100 | - ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '', | |
| 101 | - ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '', | |
| 102 | - ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '', | |
| 103 | - esc_html( number_format_i18n( $views ) ), | |
| 104 | - wp_kses_post( $label ) | |
| 105 | - ); | |
| 63 | + require_once __DIR__ . '/render.php'; | |
| 64 | + return render_implementation( $attributes ); | |
| 106 | 65 | } |