|
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 pre_render_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 current user can view the protected content of the given block. |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $attributes Block attributes. |
|
33
|
|
|
* @param object $block Block to check. |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool Whether the use can view the content. |
|
36
|
|
|
*/ |
|
37
|
|
|
function current_visitor_can_access( $attributes, $block ) { |
|
38
|
|
|
$user = wp_get_current_user(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* If the current WordPress install has as signed in user |
|
42
|
|
|
* they can see the content. |
|
43
|
|
|
*/ |
|
44
|
|
|
if ( 0 !== $user->ID && current_user_can( 'edit_post', get_the_ID() ) ) { |
|
45
|
|
|
return true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$selected_plan_id = null; |
|
49
|
|
|
|
|
50
|
|
|
if ( isset( $attributes['selectedPlanId'] ) ) { |
|
51
|
|
|
$selected_plan_id = (int) $attributes['selectedPlanId']; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ( isset( $block ) && isset( $block->context['premium-content/planId'] ) ) { |
|
55
|
|
|
$selected_plan_id = (int) $block->context['premium-content/planId']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ( empty( $selected_plan_id ) ) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$paywall = subscription_service(); |
|
63
|
|
|
$can_view = $paywall->visitor_can_view_content( array( $selected_plan_id ) ); |
|
64
|
|
|
|
|
65
|
|
|
if ( $can_view ) { |
|
66
|
|
|
/** |
|
67
|
|
|
* Fires when a visitor can view protected content on a site. |
|
68
|
|
|
* |
|
69
|
|
|
* @since 9.4.0 |
|
70
|
|
|
*/ |
|
71
|
|
|
do_action( 'jetpack_earn_remove_cache_headers' ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $can_view; |
|
75
|
|
|
} |
|
76
|
|
|
|