| @@ -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,100 +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 outside the front-end, such as within emails or the API. | |
| 51 | - if ( ! jetpack_is_frontend() ) { | |
| 52 | - return; | |
| 53 | - } | |
| 54 | - | |
| 55 | - // For when Stats has been disabled subsequent to inserting the block. | |
| 56 | - if ( ! \Jetpack::is_module_active( 'stats' ) ) { | |
| 57 | - if ( current_user_can( 'edit_theme_options' ) ) { | |
| 58 | - return sprintf( | |
| 59 | - '<p>%s</p>', | |
| 60 | - wp_kses( | |
| 61 | - sprintf( | |
| 62 | - /* translators: placeholder %s is a link to enable Jetpack Stats.. */ | |
| 63 | - __( 'Please <a href="%s">enable Jetpack Stats</a> to use this block.', 'jetpack' ), | |
| 64 | - esc_url( admin_url( 'admin.php?page=jetpack_modules&module_tag=Jetpack%20Stats' ) ) | |
| 65 | - ), | |
| 66 | - array( 'a' => array( 'href' => array() ) ) | |
| 67 | - ) | |
| 68 | - ); | |
| 69 | - } | |
| 70 | - | |
| 71 | - return; | |
| 72 | - } | |
| 73 | - | |
| 74 | - // For when there's no post ID - eg. search pages. | |
| 75 | - if ( $attributes['statsOption'] === 'post' && ! get_the_ID() ) { | |
| 76 | - if ( current_user_can( 'edit_theme_options' ) ) { | |
| 77 | - return sprintf( | |
| 78 | - '<p>%s</p>', | |
| 79 | - esc_html( __( 'There are no stats to display for this post.', 'jetpack' ) ) | |
| 80 | - ); | |
| 81 | - } | |
| 82 | - | |
| 83 | - return; | |
| 84 | - } | |
| 85 | - | |
| 86 | - $stats = 0; | |
| 87 | - $wpcom_stats = new WPCOM_Stats(); | |
| 88 | - | |
| 89 | - if ( $attributes['statsOption'] === 'post' ) { | |
| 90 | - // Cache in post meta to prevent wp_options blowing up when retrieving views | |
| 91 | - // for multiple posts simultaneously (eg. when inserted into template). | |
| 92 | - $cache_in_meta = true; | |
| 93 | - $data = $wpcom_stats->convert_stats_array_to_object( | |
| 94 | - $wpcom_stats->get_post_views( | |
| 95 | - get_the_ID(), | |
| 96 | - array( 'fields' => 'views' ), | |
| 97 | - $cache_in_meta | |
| 98 | - ) | |
| 99 | - ); | |
| 100 | - | |
| 101 | - if ( isset( $data->views ) ) { | |
| 102 | - $stats = $data->views; | |
| 103 | - } | |
| 104 | - } else { | |
| 105 | - $data = $wpcom_stats->convert_stats_array_to_object( | |
| 106 | - $wpcom_stats->get_stats( array( 'fields' => 'stats' ) ) | |
| 107 | - ); | |
| 108 | - | |
| 109 | - if ( $attributes['statsData'] === 'views' && isset( $data->stats->views ) ) { | |
| 110 | - $stats = $data->stats->views; | |
| 111 | - } | |
| 112 | - | |
| 113 | - if ( $attributes['statsData'] === 'visitors' && isset( $data->stats->visitors ) ) { | |
| 114 | - $stats = $data->stats->visitors; | |
| 115 | - } | |
| 116 | - } | |
| 117 | - | |
| 118 | - $fallback_label = $attributes['statsData'] === 'visitors' ? esc_html( | |
| 119 | - /* Translators: Number of visitors */ | |
| 120 | - _n( 'visitor', 'visitors', $stats, 'jetpack' ) | |
| 121 | - ) : esc_html( | |
| 122 | - /* Translators: Number of views */ | |
| 123 | - _n( 'hit', 'hits', $stats, 'jetpack' ) | |
| 124 | - ); | |
| 125 | - | |
| 126 | - $label = empty( $attributes['label'] ) ? $fallback_label : $attributes['label']; | |
| 127 | - | |
| 128 | - $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); | |
| 129 | - | |
| 130 | - return sprintf( | |
| 131 | - '<div class="jetpack-blog-stats%s%s"%s><p>%s %s</p></div>', | |
| 132 | - ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '', | |
| 133 | - ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '', | |
| 134 | - ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '', | |
| 135 | - esc_html( number_format_i18n( $stats ) ), | |
| 136 | - wp_kses_post( $label ) | |
| 137 | - ); | |
| 63 | + require_once __DIR__ . '/render.php'; | |
| 64 | + return render_implementation( $attributes ); | |
| 138 | 65 | } |