Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Paywall\register_block
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
Automattic\Jetpack\Extensions\Paywall\render_block
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
Automattic\Jetpack\Extensions\Paywall\excerpt_allowed_blocks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Paywall Block.
4 *
5 * @since 12.5
6 *
7 * @package automattic/jetpack
8 */
9
10namespace Automattic\Jetpack\Extensions\Paywall;
11
12use Automattic\Jetpack\Blocks;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18const FEATURE_NAME      = 'paywall';
19const BLOCK_NAME        = 'jetpack/' . FEATURE_NAME;
20const BLOCK_HTML        = '<!-- wp:' . BLOCK_NAME . ' /-->';
21const THE_EXCERPT_BLOCK = '[[[[[' . BLOCK_NAME . ']]]]]';
22
23/**
24 * Registers the block for use in Gutenberg
25 * This is done via an action so that we can disable
26 * registration if we need to.
27 */
28function register_block() {
29    if ( ! \Jetpack::is_module_active( 'subscriptions' ) ) {
30        \Jetpack_Gutenberg::set_extension_unavailable( 'paywall', 'missing_module', array( 'module' => 'subscriptions' ) );
31        return;
32    }
33    if ( ! class_exists( '\Jetpack_Memberships' ) ) {
34        return;
35    }
36
37    Blocks::jetpack_register_block(
38        __DIR__,
39        array(
40            'render_callback' => __NAMESPACE__ . '\render_block',
41        )
42    );
43}
44add_action( 'init', __NAMESPACE__ . '\register_block' );
45
46/**
47 * Paywall block render callback.
48 *
49 * @return string
50 */
51function render_block() {
52    if ( doing_filter( 'get_the_excerpt' ) ) {
53        if ( \Jetpack_Memberships::user_can_view_post() ) {
54            return '';
55        }
56        return THE_EXCERPT_BLOCK;
57    }
58    return '';
59}
60
61/**
62 * Adds the Paywall block to excerpt allowed blocks.
63 *
64 * @param array $allowed_blocks The allowed blocks.
65 *
66 * @return array The allowed blocks.
67 */
68function excerpt_allowed_blocks( $allowed_blocks ) {
69    return array_merge( $allowed_blocks, array( Blocks::get_block_name( __DIR__ ) ) );
70}
71add_filter( 'excerpt_allowed_blocks', __NAMESPACE__ . '\excerpt_allowed_blocks' );