blog-stats.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Blog Stats Block. |
| 4 | * |
| 5 | * @since 13.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Blog_Stats; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 14 | use Automattic\Jetpack\Modules; |
| 15 | use Automattic\Jetpack\Status; |
| 16 | use Automattic\Jetpack\Status\Host; |
| 17 | use Automattic\Jetpack\Status\Request; |
| 18 | use Jetpack_Blog_Stats_Helper; |
| 19 | use Jetpack_Gutenberg; |
| 20 | |
| 21 | if ( ! defined( 'ABSPATH' ) ) { |
| 22 | exit( 0 ); |
| 23 | } |
| 24 | |
| 25 | if ( ! class_exists( 'Jetpack_Blog_Stats_Helper' ) ) { |
| 26 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-blog-stats-helper.php'; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Registers the block for use in Gutenberg. |
| 31 | * This is done via an action so that we can disable |
| 32 | * registration if we need to. |
| 33 | */ |
| 34 | function register_block() { |
| 35 | /* |
| 36 | * The block is available even when the module is not active, |
| 37 | * so we can display a nudge to activate the module instead of the block. |
| 38 | * However, since non-admins cannot activate modules, we do not display the empty block for them. |
| 39 | */ |
| 40 | if ( ! ( new Modules() )->is_active( 'stats' ) && ! current_user_can( 'jetpack_activate_modules' ) ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if ( |
| 45 | ( new Host() )->is_wpcom_simple() |
| 46 | || ( |
| 47 | ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() |
| 48 | && ! ( new Status() )->is_offline_mode() |
| 49 | ) |
| 50 | ) { |
| 51 | Blocks::jetpack_register_block( |
| 52 | __DIR__, |
| 53 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 58 | |
| 59 | /** |
| 60 | * Blog Stats block registration/dependency declaration. |
| 61 | * |
| 62 | * @param array $attributes Array containing the Blog Stats block attributes. |
| 63 | * |
| 64 | * @return string |
| 65 | */ |
| 66 | function load_assets( $attributes ) { |
| 67 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 68 | |
| 69 | // For outside the front-end, such as within emails or the API. |
| 70 | if ( ! Request::is_frontend() ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // For when Stats has been disabled subsequent to inserting the block. |
| 75 | if ( ! ( new Modules() )->is_active( 'stats' ) ) { |
| 76 | if ( current_user_can( 'edit_theme_options' ) ) { |
| 77 | return sprintf( |
| 78 | '<p>%s</p>', |
| 79 | wp_kses( |
| 80 | sprintf( |
| 81 | /* translators: placeholder %s is a link to enable Jetpack Stats.. */ |
| 82 | __( 'Please <a href="%s">enable Jetpack Stats</a> to use this block.', 'jetpack' ), |
| 83 | esc_url( admin_url( 'admin.php?page=jetpack_modules&module_tag=Jetpack%20Stats' ) ) |
| 84 | ), |
| 85 | array( 'a' => array( 'href' => array() ) ) |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // For when there's no post ID - eg. search pages. |
| 94 | if ( $attributes['statsOption'] === 'post' && ! get_the_ID() ) { |
| 95 | if ( current_user_can( 'edit_theme_options' ) ) { |
| 96 | return sprintf( |
| 97 | '<p>%s</p>', |
| 98 | esc_html( __( 'There are no stats to display for this post.', 'jetpack' ) ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | $stats = Jetpack_Blog_Stats_Helper::get_stats( $attributes ); |
| 106 | |
| 107 | $fallback_label = $attributes['statsData'] === 'visitors' ? esc_html( |
| 108 | /* Translators: Number of visitors */ |
| 109 | _n( 'visitor', 'visitors', $stats, 'jetpack' ) |
| 110 | ) : esc_html( |
| 111 | /* Translators: Number of views */ |
| 112 | _n( 'hit', 'hits', $stats, 'jetpack' ) |
| 113 | ); |
| 114 | |
| 115 | $label = empty( $attributes['label'] ) ? $fallback_label : $attributes['label']; |
| 116 | |
| 117 | $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports(); |
| 118 | |
| 119 | return sprintf( |
| 120 | '<div class="jetpack-blog-stats%s%s"%s><p>%s %s</p></div>', |
| 121 | ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '', |
| 122 | ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '', |
| 123 | ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '', |
| 124 | esc_html( number_format_i18n( $stats ) ), |
| 125 | wp_kses_post( $label ) |
| 126 | ); |
| 127 | } |
| 128 |