Completed
Push — add/product-ratings-to-search ( 91683c...b05261 )
by
unknown
344:08 queued 334:42
created

access-check.php ➔ current_visitor_can_access()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 13
nop 2
dl 0
loc 39
rs 8.0515
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 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