Completed
Push — update/add-frontend-stripe-nud... ( 6c39f6...9d2fe1 )
by
unknown
110:24 queued 98:33
created

premium-content.php ➔ render_stripe_nudge()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
/**
3
 * Premium Content Block.
4
 *
5
 * @package Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Extensions\Premium_Content;
9
10
use Automattic\Jetpack\Blocks;
11
use Jetpack_Gutenberg;
12
13
require_once __DIR__ . '/_inc/access-check.php';
14
require_once __DIR__ . '/logged-out-view/logged-out-view.php';
15
require_once __DIR__ . '/subscriber-view/subscriber-view.php';
16
require_once __DIR__ . '/buttons/buttons.php';
17
require_once __DIR__ . '/login-button/login-button.php';
18
19
const FEATURE_NAME = 'premium-content/container';
20
21
/**
22
 * Registers the block for use in Gutenberg
23
 * This is done via an action so that we can disable
24
 * registration if we need to.
25
 */
26
function register_block() {
27
	// Determine required `context` key based on Gutenberg version.
28
	$deprecated = function_exists( 'gutenberg_get_post_from_context' );
29
	$provides   = $deprecated ? 'providesContext' : 'provides_context';
30
31
	Blocks::jetpack_register_block(
32
		FEATURE_NAME,
33
		array(
34
			'render_callback' => __NAMESPACE__ . '\render_block',
35
			'plan_check'      => true,
36
			'attributes'      => array(
37
				'isPremiumContentChild' => array(
38
					'type'    => 'boolean',
39
					'default' => true,
40
				),
41
			),
42
			$provides         => array(
43
				'premium-content/planId' => 'selectedPlanId',
44
				'isPremiumContentChild'  => 'isPremiumContentChild',
45
			),
46
		)
47
	);
48
}
49
add_action( 'init', __NAMESPACE__ . '\register_block' );
50
51
/**
52
 * Render callback.
53
 *
54
 * @param array  $attributes Array containing the block attributes.
55
 * @param string $content    String containing the block content.
56
 *
57
 * @return string
58
 */
59
function render_block( $attributes, $content ) {
60
	if ( ! pre_render_checks() ) {
61
		return '';
62
	}
63
64
	if (
65
		! membership_checks()
66
		// Only display Stripe nudge if Upgrade nudge isn't displaying
67
		&& required_plan_checks()
68
	) {
69
		$stripe_nudge = render_stripe_nudge();
70
		return $stripe_nudge . $content;
71
	}
72
73
	Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
74
	return $content;
75
}
76
77
/**
78
 * Server-side rendering for the stripe connection nudge.
79
 *
80
 * @return string Final content to render.
81
 */
82
function render_stripe_nudge() {
83
	if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
84
		// The Premium Content block should not be supported on Jetpack sites;
85
		// check just in case.
86
		return '';
87
	}
88
89
	jetpack_require_lib( 'memberships' );
90
	$blog_id = get_current_blog_id();
91
	$settings = (array) get_memberships_settings_for_site($blog_id);
92
93
	jetpack_require_lib('components');
94
	return \Jetpack_Components::render_frontend_nudge(
95
		array(
96
			'checkoutUrl' => $settings['connect_url'],
97
			'description' => _('Connect to Stripe to use this block on your site.', 'jetpack'),
98
			'buttonText'  => _('Connect', 'jetpack'),
99
		)
100
	);
101
}
102