Completed
Push — update/enable-frontend-upgrade... ( 4d6f9a )
by
unknown
380:03 queued 372:03
created

access-check.php ➔ membership_checks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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 block should be rendered. Returns true
31
 * if the block passes all required checks, or if the user is
32
 * an editor.
33
 *
34
 * @return bool Whether the block should be rendered.
35
 */
36
function pre_render_checks() {
37
	return ( current_user_can_edit() || membership_checks() );
38
}
39
40
/**
41
 * Determines whether the current user can edit.
42
 *
43
 * @return bool Whether the user can edit.
44
 */
45
function current_user_can_edit() {
46
    $user = wp_get_current_user();
47
48
    return 0 !== $user->ID && current_user_can( 'edit_post', get_the_ID() );
49
}
50
51
/**
52
 * Determines if the current user can view the protected content of the given block.
53
 *
54
 * @param array  $attributes Block attributes.
55
 * @param object $block Block to check.
56
 *
57
 * @return bool Whether the use can view the content.
58
 */
59
function current_visitor_can_access( $attributes, $block ) {
60
	/**
61
	 * If the current WordPress install has as signed in user
62
	 * they can see the content.
63
	 */
64
	if ( current_user_can_edit() ) {
65
		return true;
66
	}
67
68
	$selected_plan_id = null;
69
70
	if ( isset( $attributes['selectedPlanId'] ) ) {
71
		$selected_plan_id = (int) $attributes['selectedPlanId'];
72
	}
73
74
	if ( isset( $block ) && isset( $block->context['premium-content/planId'] ) ) {
75
		$selected_plan_id = (int) $block->context['premium-content/planId'];
76
	}
77
78
	if ( empty( $selected_plan_id ) ) {
79
		return false;
80
	}
81
82
	$paywall  = subscription_service();
83
	$can_view = $paywall->visitor_can_view_content( array( $selected_plan_id ) );
84
85
	if ( $can_view ) {
86
		do_action( 'earn_remove_cache_headers' );
87
	}
88
89
	return $can_view;
90
}
91