|
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
|
|
|
// Show upgrade nudge. |
|
65
|
|
|
if ( ! required_plan_checks() && current_user_can_edit() |
|
66
|
|
|
) { |
|
67
|
|
|
$upgrade_nudge = render_upgrade_nudge(); |
|
68
|
|
|
return $upgrade_nudge . $content; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Stripe connection nudge. |
|
72
|
|
|
if ( ! membership_checks() && current_user_can_edit() ) { |
|
73
|
|
|
$stripe_nudge = render_stripe_nudge(); |
|
74
|
|
|
return $stripe_nudge . $content; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); |
|
78
|
|
|
return $content; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Server-side rendering for the upgrade nudge. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string Final content to render. |
|
85
|
|
|
*/ |
|
86
|
|
|
function render_upgrade_nudge() { |
|
87
|
|
|
$required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'personal-bundle' : 'jetpack_personal'; |
|
88
|
|
|
|
|
89
|
|
|
jetpack_require_lib( 'components' ); |
|
90
|
|
|
|
|
91
|
|
|
return \Jetpack_Components::render_upgrade_nudge( |
|
92
|
|
|
array( |
|
93
|
|
|
'plan' => $required_plan, |
|
94
|
|
|
) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Server-side rendering for the stripe connection nudge. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string Final content to render. |
|
102
|
|
|
*/ |
|
103
|
|
|
function render_stripe_nudge() { |
|
104
|
|
|
jetpack_require_lib( 'components' ); |
|
105
|
|
|
|
|
106
|
|
|
return \Jetpack_Components::render_component( |
|
107
|
|
|
'stripe-nudge', |
|
108
|
|
|
array( |
|
109
|
|
|
'blockName' => 'premium-content', |
|
110
|
|
|
'postId' => get_the_ID(), |
|
111
|
|
|
'stripeConnectUrl' => null, |
|
112
|
|
|
) |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
return ''; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.