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

abbreviate_subscriptions()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 1
dl 17
loc 17
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * This subscription service is used when a subscriber is offline and a token is not available.
4
 * This subscription service will be used when rendering content in email and reader on WPCOM only.
5
 * When content is being rendered, the current user and site are set.
6
 * This allows us to lookup a users subscriptions and determine if the
7
 * offline visitor can view content that has been deemed "Premium content".
8
 *
9
 * @package Automattic\Jetpack\Extensions\Premium_Content
10
 */
11
12
namespace Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service;
13
14
/**
15
 * Class WPCOM_Offline_Subscription_Service
16
 *
17
 * @package Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service
18
 */
19
class WPCOM_Offline_Subscription_Service extends WPCOM_Token_Subscription_Service {
20
21
	/**
22
	 * Is available()
23
	 *
24
	 * @return bool
25
	 */
26
	public static function available() {
27
		// Return available if the user is logged in and either
28
		// running a job (sending email subscription) OR
29
		// handling API request on WPCOM (reader).
30
		return (
31
			( defined( 'WPCOM_JOBS' ) && WPCOM_JOBS ) ||
32
			( defined( 'IS_WPCOM' ) && IS_WPCOM === true && ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) )
33
			) && is_user_logged_in();
34
	}
35
36
	/**
37
	 * Lookup users subscriptions for a site and determine if the user has a valid subscription to match the plan ID
38
	 *
39
	 * @param array $valid_plan_ids .
40
	 * @return bool
41
	 */
42
	public function visitor_can_view_content( $valid_plan_ids ) {
43
		/** This filter is already documented in projects/plugins/jetpack/extensions/blocks/premium-content/_inc/subscription-service/class-token-subscription-service.php */
44
		$subscriptions = apply_filters( 'earn_get_user_subscriptions_for_site_id', array(), wp_get_current_user()->ID, $this->get_site_id() );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with wp_get_current_user()->ID.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
		if ( empty( $subscriptions ) ) {
46
			return false;
47
		}
48
		// format the subscriptions so that they can be validated.
49
		$subscriptions = self::abbreviate_subscriptions( $subscriptions );
50
		return $this->validate_subscriptions( $valid_plan_ids, $subscriptions );
51
	}
52
53
	/**
54
	 * Report the subscriptions as an ID => [ 'end_date' => ]. mapping
55
	 *
56
	 * @param array $subscriptions_from_bd .
57
	 *
58
	 * @return array<int, array>
0 ignored issues
show
Documentation introduced by
The doc-type array<int, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
59
	 */
60 View Code Duplication
	public static function abbreviate_subscriptions( $subscriptions_from_bd ) {
61
		$subscriptions = array();
62
		foreach ( $subscriptions_from_bd as $subscription ) {
63
			// We are picking the expiry date that is the most in the future.
64
			if (
65
				'active' === $subscription['status'] && (
66
					! isset( $subscriptions[ $subscription['product_id'] ] ) ||
67
					empty( $subscription['end_date'] ) || // Special condition when subscription has no expiry date - we will default to a year from now for the purposes of the token.
68
					strtotime( $subscription['end_date'] ) > strtotime( (string) $subscriptions[ $subscription['product_id'] ]['end_date'] )
69
				)
70
			) {
71
				$subscriptions[ $subscription['product_id'] ]           = new \stdClass();
72
				$subscriptions[ $subscription['product_id'] ]->end_date = empty( $subscription['end_date'] ) ? ( time() + 365 * 24 * 3600 ) : $subscription['end_date'];
73
			}
74
		}
75
		return $subscriptions;
76
	}
77
}
78