Completed
Push — update/connect-podcast-player-... ( eb8dbb...ca5bd7 )
by
unknown
36:16 queued 25:36
created

premium-content.php ➔ stripe_nudge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 10
rs 9.9332
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
	// Only load this block on WordPress.com.
28
	if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || jetpack_is_atomic_site() ) {
29
		// Determine required `context` key based on Gutenberg version.
30
		$deprecated = function_exists( 'gutenberg_get_post_from_context' );
31
		$provides   = $deprecated ? 'providesContext' : 'provides_context';
32
33
		Blocks::jetpack_register_block(
34
			FEATURE_NAME,
35
			array(
36
				'render_callback' => __NAMESPACE__ . '\render_block',
37
				'plan_check'      => true,
38
				'attributes'      => array(
39
					'isPremiumContentChild' => array(
40
						'type'    => 'boolean',
41
						'default' => true,
42
					),
43
				),
44
				$provides         => array(
45
					'premium-content/planId' => 'selectedPlanId',
46
					'isPremiumContentChild'  => 'isPremiumContentChild',
47
				),
48
			)
49
		);
50
	}
51
}
52
add_action( 'init', __NAMESPACE__ . '\register_block' );
53
54
/**
55
 * Render callback.
56
 *
57
 * @param array  $attributes Array containing the block attributes.
58
 * @param string $content    String containing the block content.
59
 *
60
 * @return string
61
 */
62
function render_block( $attributes, $content ) {
63
	if ( ! pre_render_checks() ) {
64
		return '';
65
	}
66
67
	if (
68
		! membership_checks()
69
		// Only display Stripe nudge if Upgrade nudge isn't displaying.
70
		&& required_plan_checks()
71
	) {
72
		$stripe_nudge = render_stripe_nudge();
73
		return $stripe_nudge . $content;
74
	}
75
76
	Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
77
	return $content;
78
}
79
80
/**
81
 * Server-side rendering for the stripe connection nudge.
82
 *
83
 * @return string Final content to render.
84
 */
85
function render_stripe_nudge() {
86
	if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
87
		\jetpack_require_lib( 'memberships' );
88
		$blog_id  = get_current_blog_id();
89
		$settings = (array) \get_memberships_settings_for_site( $blog_id );
90
91
		return stripe_nudge(
92
			$settings['connect_url'],
93
			__( 'Connect to Stripe to use this block on your site.', 'jetpack' ),
94
			__( 'Connect', 'jetpack' )
95
		);
96
	} elseif ( jetpack_is_atomic_site() ) {
97
		// On Atomic sites, the Stripe connection url is not easily available
98
		// server-side, so we redirect them to the post in the editor in order
99
		// to connect.
100
		return stripe_nudge(
101
			get_edit_post_link( get_the_ID() ),
102
			__( 'Connect to Stripe in the editor to use this block on your site.', 'jetpack' ),
103
			__( 'Edit post', 'jetpack' )
104
		);
105
	}
106
107
	// The Premium Content block is not supported on Jetpack sites.
108
	return '';
109
}
110
111
/**
112
 * Render the stripe nudge.
113
 *
114
 * @param string $checkout_url Url for the CTA.
115
 * @param string $description  Text of the nudge.
116
 * @param string $button_text  Text of the button.
117
 *
118
 * @return string Final content to render.
119
 */
120
function stripe_nudge( $checkout_url, $description, $button_text ) {
121
	\jetpack_require_lib( 'components' );
122
	return \Jetpack_Components::render_frontend_nudge(
123
		array(
124
			'checkoutUrl' => $checkout_url,
125
			'description' => $description,
126
			'buttonText'  => $button_text,
127
		)
128
	);
129
}
130