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