|
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. If false, the site requires a |
|
32
|
|
|
* plan upgrade. |
|
33
|
|
|
* |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
|
View Code Duplication |
function required_plan_checks() { |
|
|
|
|
|
|
37
|
|
|
// For WPCOM sites. |
|
38
|
|
|
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'has_any_blog_stickers' ) ) { |
|
39
|
|
|
$site_id = get_current_blog_id(); |
|
40
|
|
|
return has_any_blog_stickers( array( 'personal-plan', 'premium-plan', 'business-plan', 'ecommerce-plan' ), $site_id ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// For Jetpack sites, only Atomic sites (with a business plan |
|
44
|
|
|
// or above) have the block, so no upgrade is required. |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Determines if the block should be rendered. Returns true |
|
50
|
|
|
* if the memberships module is set up, or if it has not been |
|
51
|
|
|
* set up but the user can edit the post. |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool Whether the block should be rendered. |
|
54
|
|
|
*/ |
|
55
|
|
|
function pre_render_checks() { |
|
56
|
|
|
return ( |
|
57
|
|
|
membership_checks() || |
|
58
|
|
|
current_user_can_edit() |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Determines if the a preview of the block with disconnected |
|
64
|
|
|
* buttons should be shown on the frontend. Returns true |
|
65
|
|
|
* user can edit the post, but the site requires an upgrade |
|
66
|
|
|
* or Stripe connection in order to support the block. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool Whether the frontend preview should be shown |
|
69
|
|
|
*/ |
|
70
|
|
|
function should_render_frontend_preview() { |
|
71
|
|
|
return ( |
|
72
|
|
|
current_user_can_edit() && |
|
73
|
|
|
( ! membership_checks() || ! required_plan_checks() ) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Determines if the current user can view the protected content of the given block. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $attributes Block attributes. |
|
81
|
|
|
* @param object $block Block to check. |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool Whether the use can view the content. |
|
84
|
|
|
*/ |
|
85
|
|
|
function current_visitor_can_access( $attributes, $block ) { |
|
86
|
|
|
/** |
|
87
|
|
|
* If the current WordPress install has as signed in user |
|
88
|
|
|
* they can see the content. |
|
89
|
|
|
*/ |
|
90
|
|
|
if ( current_user_can_edit() ) { |
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$selected_plan_id = null; |
|
95
|
|
|
|
|
96
|
|
|
if ( isset( $attributes['selectedPlanId'] ) ) { |
|
97
|
|
|
$selected_plan_id = (int) $attributes['selectedPlanId']; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ( isset( $block ) && isset( $block->context['premium-content/planId'] ) ) { |
|
101
|
|
|
$selected_plan_id = (int) $block->context['premium-content/planId']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ( empty( $selected_plan_id ) ) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$paywall = subscription_service(); |
|
109
|
|
|
$can_view = $paywall->visitor_can_view_content( array( $selected_plan_id ) ); |
|
110
|
|
|
|
|
111
|
|
|
if ( $can_view ) { |
|
112
|
|
|
do_action( 'earn_remove_cache_headers' ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $can_view; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Determines whether the current user can edit. |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool Whether the user can edit. |
|
122
|
|
|
*/ |
|
123
|
|
|
function current_user_can_edit() { |
|
124
|
|
|
$user = wp_get_current_user(); |
|
125
|
|
|
|
|
126
|
|
|
return 0 !== $user->ID && current_user_can( 'edit_post', get_the_ID() ); |
|
127
|
|
|
} |
|
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.