Completed
Push — refactor/subscriptions ( 2c6651 )
by Jeremy
23:44 queued 13:28
created

Helpers::fetch_subscriber_count()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 10
nop 0
dl 0
loc 40
rs 8.0355
c 0
b 0
f 0
1
<?php
2
/**
3
 * A collection of helper functions used in the SSO module.
4
 *
5
 * @package Jetpack
6
 */
7
8
namespace Automattic\Jetpack\Subscriptions;
9
10
use Blog_Subscription;
11
use Blog_Subscriber;
12
use Jetpack_IXR_Client;
13
14
/**
15
 * A collection of helper functions used in the SSO module.
16
 *
17
 * @since 8.6.0
18
 */
19
class Helpers {
20
	/**
21
	 * Is this script running in the wordpress.com environment?
22
	 *
23
	 * @return bool
24
	 */
25
	public static function is_wpcom() {
26
		return defined( 'IS_WPCOM' ) && IS_WPCOM;
27
	}
28
29
	/**
30
	 * Is this script running in a self-hosted environment?
31
	 *
32
	 * @return bool
33
	 */
34
	public static function is_jetpack() {
35
		return ! self::is_wpcom();
36
	}
37
38
	/**
39
	 * Get the Subscriptions name,
40
	 * based on the platform we're on.
41
	 *
42
	 * @return string
43
	 */
44
	public static function widget_classname() {
45
		if ( self::is_wpcom() ) {
46
			return 'Automattic\Jetpack\Subscriptions\Widget';
47
		} else {
48
			return 'Jetpack_Subscriptions_Widget';
49
		}
50
	}
51
52
	/**
53
	 * Determines if the current user is subscribed to the blog.
54
	 *
55
	 * @return bool Is the person already subscribed.
56
	 */
57
	public static function is_current_user_subscribed() {
58
		$subscribed = isset( $_GET['subscribe'] ) && 'success' === $_GET['subscribe']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
59
60
		if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
61
			$subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
62
		}
63
64
		return $subscribed;
65
	}
66
67
	/**
68
	 * Determine the amount of folks currently subscribed to the blog.
69
	 *
70
	 * @return int|array
71
	 */
72
	public static function fetch_subscriber_count() {
73
		$subs_count = 0;
74
75
		if ( self::is_jetpack() ) {
76
			$subs_count = get_transient( 'wpcom_subscribers_total' );
77
			if ( false === $subs_count || 'failed' === $subs_count['status'] ) {
78
				$xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER ) );
79
80
				$xml->query( 'jetpack.fetchSubscriberCount' );
81
82
				/*
83
				 * if we get an error from .com,
84
				 * set the status to failed
85
				 * so that we will try again next time the data is requested
86
				 */
87
				if ( $xml->isError() ) {
88
					$subs_count = array(
89
						'status'  => 'failed',
90
						'code'    => $xml->getErrorCode(),
91
						'message' => $xml->getErrorMessage(),
92
						'value'   => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
93
					);
94
				} else {
95
					$subs_count = array(
96
						'status' => 'success',
97
						'value'  => $xml->getResponse(),
98
					);
99
				}
100
101
				// try to cache the result for at least 1 hour.
102
				set_transient( 'wpcom_subscribers_total', $subs_count, 3600 );
103
			}
104
		}
105
106
		if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) {
107
			$subs_count = wpcom_reach_total_for_blog();
108
		}
109
110
		return $subs_count;
111
	}
112
113
	/**
114
	 * Used to determine if there is a valid status slug within the wordpress.com environment.
115
	 *
116
	 * @return bool
117
	 */
118
	public static function has_status_message() {
119
		return isset( $_GET['blogsub'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
120
			&& in_array(
121
				$_GET['blogsub'], // phpcs:ignore WordPress.Security.NonceVerification.Recommended
122
				array(
123
					'confirming',
124
					'blocked',
125
					'flooded',
126
					'spammed',
127
					'subscribed',
128
					'pending',
129
					'confirmed',
130
				),
131
				true
132
			);
133
	}
134
}
135