blog-stats.php
132 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\Stats\WPCOM_Stats; |
| 15 | use Automattic\Jetpack\Status; |
| 16 | use Jetpack_Gutenberg; |
| 17 | |
| 18 | /** |
| 19 | * Registers the block for use in Gutenberg. |
| 20 | * This is done via an action so that we can disable |
| 21 | * registration if we need to. |
| 22 | */ |
| 23 | function register_block() { |
| 24 | if ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) { |
| 25 | Blocks::jetpack_register_block( |
| 26 | __DIR__, |
| 27 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 28 | ); |
| 29 | } |
| 30 | } |
| 31 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 32 | |
| 33 | /** |
| 34 | * Blog Stats block registration/dependency declaration. |
| 35 | * |
| 36 | * @param array $attributes Array containing the Blog Stats block attributes. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | 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 | ); |
| 131 | } |
| 132 |