|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Determine access to premium content. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Automattic\Jetpack\Extensions\Premium_Content |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Extensions\Premium_Content; |
|
9
|
|
|
|
|
10
|
|
|
require __DIR__ . '/subscription-service/include.php'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Determines if the memberships module is set up. |
|
14
|
|
|
* |
|
15
|
|
|
* @return bool Whether the memberships module is set up. |
|
16
|
|
|
*/ |
|
17
|
|
|
function membership_checks() { |
|
18
|
|
|
// If Jetpack is not yet configured, don't show anything ... |
|
19
|
|
|
if ( ! class_exists( '\Jetpack_Memberships' ) ) { |
|
20
|
|
|
return false; |
|
21
|
|
|
} |
|
22
|
|
|
// if stripe not connected don't show anything... |
|
23
|
|
|
if ( empty( \Jetpack_Memberships::get_connected_account_id() ) ) { |
|
24
|
|
|
return false; |
|
25
|
|
|
} |
|
26
|
|
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Determines if the site has a plan that supports the |
|
31
|
|
|
* Premium Content block. |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
function required_plan_checks() { |
|
36
|
|
|
$availability = \Jetpack_Gutenberg::get_availability(); |
|
37
|
|
|
$slug = 'premium-content/container'; |
|
38
|
|
|
return ( isset( $availability[ $slug ] ) && $availability[ $slug ]['available'] ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Determines if the block should be rendered. Returns true |
|
43
|
|
|
* if the block passes all required checks, or if the user is |
|
44
|
|
|
* an editor. |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool Whether the block should be rendered. |
|
47
|
|
|
*/ |
|
48
|
|
|
function pre_render_checks() { |
|
49
|
|
|
return ( current_user_can_edit() || membership_checks() ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Determines whether the current user can edit. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool Whether the user can edit. |
|
56
|
|
|
*/ |
|
57
|
|
|
function current_user_can_edit() { |
|
58
|
|
|
$user = wp_get_current_user(); |
|
59
|
|
|
|
|
60
|
|
|
return 0 !== $user->ID && current_user_can( 'edit_post', get_the_ID() ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Determines if the current user can view the protected content of the given block. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $attributes Block attributes. |
|
67
|
|
|
* @param object $block Block to check. |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool Whether the use can view the content. |
|
70
|
|
|
*/ |
|
71
|
|
|
function current_visitor_can_access( $attributes, $block ) { |
|
72
|
|
|
/** |
|
73
|
|
|
* If the current WordPress install has as signed in user |
|
74
|
|
|
* they can see the content. |
|
75
|
|
|
*/ |
|
76
|
|
|
if ( current_user_can_edit() ) { |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$selected_plan_id = null; |
|
81
|
|
|
|
|
82
|
|
|
if ( isset( $attributes['selectedPlanId'] ) ) { |
|
83
|
|
|
$selected_plan_id = (int) $attributes['selectedPlanId']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ( isset( $block ) && isset( $block->context['premium-content/planId'] ) ) { |
|
87
|
|
|
$selected_plan_id = (int) $block->context['premium-content/planId']; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ( empty( $selected_plan_id ) ) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$paywall = subscription_service(); |
|
95
|
|
|
$can_view = $paywall->visitor_can_view_content( array( $selected_plan_id ) ); |
|
96
|
|
|
|
|
97
|
|
|
if ( $can_view ) { |
|
98
|
|
|
do_action( 'earn_remove_cache_headers' ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $can_view; |
|
102
|
|
|
} |
|
103
|
|
|
|