blog-stats.php
107 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Blog Stats Block. |
| 4 | * |
| 5 | * @since $$next_version$$ |
| 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 ( |
| 25 | ! defined( 'IS_WPCOM' ) |
| 26 | && ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() |
| 27 | && ! ( new Status() )->is_offline_mode() ) |
| 28 | ) { |
| 29 | Blocks::jetpack_register_block( |
| 30 | __DIR__, |
| 31 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 32 | ); |
| 33 | } |
| 34 | } |
| 35 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 36 | |
| 37 | /** |
| 38 | * Blog Stats block registration/dependency declaration. |
| 39 | * |
| 40 | * @param array $attributes Array containing the Blog Stats block attributes. |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | 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 | ); |
| 106 | } |
| 107 |