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
182
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\My_Jetpack\Initializer;
15use Automattic\Jetpack\Status\Request;
16use Jetpack_Blog_Stats_Helper;
17use Jetpack_Gutenberg;
18
19if ( ! defined( 'ABSPATH' ) ) {
20    exit( 0 );
21}
22
23if ( ! class_exists( 'Jetpack_Blog_Stats_Helper' ) ) {
24    require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-blog-stats-helper.php';
25}
26
27/**
28 * Dynamic rendering of the block.
29 *
30 * @param array $attributes Array containing the Blog Stats block attributes.
31 *
32 * @return string
33 */
34function render_implementation( $attributes ) {
35    Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
36
37    // For outside the front-end, such as within emails or the API.
38    if ( ! Request::is_frontend() ) {
39        return;
40    }
41
42    // For when Stats has been disabled subsequent to inserting the block.
43    if ( ! ( new Modules() )->is_active( 'stats' ) ) {
44        if ( current_user_can( 'edit_theme_options' ) ) {
45            return sprintf(
46                '<p>%s</p>',
47                wp_kses(
48                    sprintf(
49                        /* translators: placeholder %s is a link to enable Jetpack Stats.. */
50                        __( 'Please <a href="%s">enable Jetpack Stats</a> to use this block.', 'jetpack' ),
51                        esc_url( method_exists( Initializer::class, 'get_modules_management_url' ) ? Initializer::get_modules_management_url( array( 'module_tag' => 'Jetpack Stats' ) ) : admin_url( 'admin.php?page=jetpack_modules&module_tag=Jetpack%20Stats' ) )
52                    ),
53                    array( 'a' => array( 'href' => array() ) )
54                )
55            );
56        }
57
58        return;
59    }
60
61    // For when there's no post ID - eg. search pages.
62    if ( $attributes['statsOption'] === 'post' && ! get_the_ID() ) {
63        if ( current_user_can( 'edit_theme_options' ) ) {
64            return sprintf(
65                '<p>%s</p>',
66                esc_html( __( 'There are no stats to display for this post.', 'jetpack' ) )
67            );
68        }
69
70        return;
71    }
72
73    $stats = Jetpack_Blog_Stats_Helper::get_stats( $attributes );
74
75    $fallback_label = $attributes['statsData'] === 'visitors' ? esc_html(
76        /* Translators: Number of visitors */
77        _n( 'visitor', 'visitors', $stats, 'jetpack' )
78    ) : esc_html(
79        /* Translators: Number of views */
80        _n( 'hit', 'hits', $stats, 'jetpack' )
81    );
82
83    $label = empty( $attributes['label'] ) ? $fallback_label : $attributes['label'];
84
85    $wrapper_attributes = \WP_Block_Supports::get_instance()->apply_block_supports();
86
87    return sprintf(
88        '<div class="jetpack-blog-stats%s%s"%s><p>%s %s</p></div>',
89        ! empty( $attributes['className'] ) ? ' ' . esc_attr( $attributes['className'] ) : '',
90        ! empty( $wrapper_attributes['class'] ) ? ' ' . esc_attr( $wrapper_attributes['class'] ) : '',
91        ! empty( $wrapper_attributes['style'] ) ? ' style="' . esc_attr( $wrapper_attributes['style'] ) . '"' : '',
92        esc_html( number_format_i18n( $stats ) ),
93        wp_kses_post( $label )
94    );
95}