Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
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 / 10
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        return;
31    }
32    if ( ! class_exists( '\Jetpack_Memberships' ) ) {
33        return;
34    }
35
36    Blocks::jetpack_register_block(
37        __DIR__,
38        array(
39            'render_callback' => __NAMESPACE__ . '\render_block',
40        )
41    );
42}
43add_action( 'init', __NAMESPACE__ . '\register_block' );
44
45/**
46 * Paywall block render callback.
47 *
48 * @return string
49 */
50function render_block() {
51    if ( doing_filter( 'get_the_excerpt' ) ) {
52        if ( \Jetpack_Memberships::user_can_view_post() ) {
53            return '';
54        }
55        return THE_EXCERPT_BLOCK;
56    }
57    return '';
58}
59
60/**
61 * Adds the Paywall block to excerpt allowed blocks.
62 *
63 * @param array $allowed_blocks The allowed blocks.
64 *
65 * @return array The allowed blocks.
66 */
67function excerpt_allowed_blocks( $allowed_blocks ) {
68    return array_merge( $allowed_blocks, array( Blocks::get_block_name( __DIR__ ) ) );
69}
70add_filter( 'excerpt_allowed_blocks', __NAMESPACE__ . '\excerpt_allowed_blocks' );