Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Blog_Stats\register_block
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
Automattic\Jetpack\Extensions\Blog_Stats\load_assets
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Blog Stats Block.
4 *
5 * @since 13.0
6 *
7 * @package automattic/jetpack
8 */
9
10namespace Automattic\Jetpack\Extensions\Blog_Stats;
11
12use Automattic\Jetpack\Blocks;
13use Automattic\Jetpack\Connection\Manager as Connection_Manager;
14use Automattic\Jetpack\Modules;
15use Automattic\Jetpack\Status;
16use Automattic\Jetpack\Status\Host;
17
18if ( ! defined( 'ABSPATH' ) ) {
19    exit( 0 );
20}
21
22/**
23 * Registers the block for use in Gutenberg.
24 * This is done via an action so that we can disable
25 * registration if we need to.
26 */
27function register_block() {
28    /*
29     * The block is available even when the module is not active,
30     * so we can display a nudge to activate the module instead of the block.
31     * However, since non-admins cannot activate modules, we do not display the empty block for them.
32     */
33    if ( ! ( new Modules() )->is_active( 'stats' ) && ! current_user_can( 'jetpack_activate_modules' ) ) {
34        \Jetpack_Gutenberg::set_extension_unavailable( 'blog-stats', 'missing_module', array( 'module' => 'stats' ) );
35        return;
36    }
37
38    if (
39        ( new Host() )->is_wpcom_simple()
40        || (
41            ( new Connection_Manager( 'jetpack' ) )->has_connected_owner()
42            && ! ( new Status() )->is_offline_mode()
43        )
44    ) {
45        Blocks::jetpack_register_block(
46            __DIR__,
47            array( 'render_callback' => __NAMESPACE__ . '\load_assets' )
48        );
49    }
50}
51add_action( 'init', __NAMESPACE__ . '\register_block' );
52
53/**
54 * Blog Stats block registration/dependency declaration.
55 *
56 * The render implementation lives in render.php and is only loaded when the
57 * block is actually rendered, keeping it out of the eager front-end path.
58 *
59 * @param array $attributes Array containing the Blog Stats block attributes.
60 *
61 * @return string
62 */
63function load_assets( $attributes ) {
64    require_once __DIR__ . '/render.php';
65    return render_implementation( $attributes );
66}