Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Blog_Stats\render_implementation
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2/**
3 * Blog Stats block render implementation.
4 *
5 * Loaded lazily from blog-stats.php only when the block is rendered, to keep
6 * the render body out of the eager front-end PHP/opcache footprint.
7 *
8 * @package automattic/jetpack
9 */
10
11namespace Automattic\Jetpack\Extensions\Blog_Stats;
12
13use Automattic\Jetpack\Modules;
14use Automattic\Jetpack\Status\Request;
15use Jetpack_Blog_Stats_Helper;
16use Jetpack_Gutenberg;
17
18if ( ! defined( 'ABSPATH' ) ) {
19    exit( 0 );
20}
21
22if ( ! class_exists( 'Jetpack_Blog_Stats_Helper' ) ) {
23    require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-blog-stats-helper.php';
24}
25
26/**
27 * Dynamic rendering of the block.
28 *
29 * @param array $attributes Array containing the Blog Stats block attributes.
30 *
31 * @return string
32 */
33function render_implementation( $attributes ) {
34    Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
35
36    // For outside the front-end, such as within emails or the API.
37    if ( ! Request::is_frontend() ) {
38        return;
39    }
40
41    // For when Stats has been disabled subsequent to inserting the block.
42    if ( ! ( new Modules() )->is_active( 'stats' ) ) {
43        if ( current_user_can( 'edit_theme_options' ) ) {
44            return sprintf(
45                '<p>%s</p>',
46                wp_kses(
47                    sprintf(
48                        /* translators: placeholder %s is a link to enable Jetpack Stats.. */
49                        __( 'Please <a href="%s">enable Jetpack Stats</a> to use this block.', 'jetpack' ),
50                        esc_url( admin_url( 'admin.php?page=jetpack_modules&module_tag=Jetpack%20Stats' ) )
51                    ),
52                    array( 'a' => array( 'href' => array() ) )
53                )
54            );
55        }
56
57        return;
58    }
59
60    // For when there's no post ID - eg. search pages.
61    if ( $attributes['statsOption'] === 'post' && ! get_the_ID() ) {
62        if ( current_user_can( 'edit_theme_options' ) ) {
63            return sprintf(
64                '<p>%s</p>',
65                esc_html( __( 'There are no stats to display for this post.', 'jetpack' ) )
66            );
67        }
68
69        return;
70    }
71
72    $stats = Jetpack_Blog_Stats_Helper::get_stats( $attributes );
73
74    $fallback_label = $attributes['statsData'] === 'visitors' ? esc_html(
75        /* Translators: Number of visitors */
76        _n( 'visitor', 'visitors', $stats, 'jetpack' )
77    ) : esc_html(
78        /* Translators: Number of views */
79        _n( 'hit', 'hits', $stats, 'jetpack' )
80    );
81
82    $label = empty( $attributes['label'] ) ? $fallback_label : $attributes['label'];
83
84    $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
85
86    return sprintf(
87        '<div class="jetpack-blog-stats%s%s"%s><p>%s %s</p></div>',
88        ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
89        ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
90        ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
91        esc_html( number_format_i18n( $stats ) ),
92        wp_kses_post( $label )
93    );
94}