Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
Automattic\Jetpack\Extensions\Premium_Content\register_subscriber_view_block
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
Automattic\Jetpack\Extensions\Premium_Content\render_subscriber_view_block
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Premium Content Subscriber View Child Block.
4 *
5 * @package automattic/jetpack
6 */
7
8namespace Automattic\Jetpack\Extensions\Premium_Content;
9
10use Automattic\Jetpack\Blocks;
11use Jetpack_Gutenberg;
12
13const SUBSCRIBER_VIEW_NAME = 'premium-content/subscriber-view';
14
15require_once dirname( __DIR__ ) . '/_inc/access-check.php';
16
17/**
18 * Registers the block for use in Gutenberg
19 * This is done via an action so that we can disable
20 * registration if we need to.
21 */
22function register_subscriber_view_block() {
23    Blocks::jetpack_register_block(
24        SUBSCRIBER_VIEW_NAME,
25        array(
26            'render_callback' => __NAMESPACE__ . '\render_subscriber_view_block',
27            'uses_context'    => array( 'premium-content/planId', 'premium-content/planIds' ),
28        )
29    );
30}
31add_action( 'init', __NAMESPACE__ . '\register_subscriber_view_block' );
32
33/**
34 * Render callback.
35 *
36 * @param array  $attributes Array containing the block attributes.
37 * @param string $content    String containing the block content.
38 * @param object $block      Object containing the full block.
39 *
40 * @return string
41 */
42function render_subscriber_view_block( $attributes, $content, $block = null ) {
43    if ( ! pre_render_checks() ) {
44        return '';
45    }
46
47    $visitor_has_access = current_visitor_can_access( $attributes, $block );
48
49    if ( $visitor_has_access ) {
50        Jetpack_Gutenberg::load_styles_as_required( SUBSCRIBER_VIEW_NAME );
51
52        // The viewer has access to premium content, so the viewer can see the subscriber view content.
53        return $content;
54    }
55
56    return '';
57}