Completed
Push — improve/rename-anti-spam-in-si... ( e5a567...f08131 )
by
unknown
75:27 queued 67:29
created

Heartbeat::generate_stats_array()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack Heartbeat package.
4
 *
5
 * @package  automattic/jetpack-heartbeat
6
 */
7
8
namespace Automattic\Jetpack;
9
10
use Automattic\Jetpack\Connection\Manager;
11
use Automattic\Jetpack\A8c_Mc_Stats;
12
13
/**
14
 * Heartbeat sends a batch of stats to wp.com once a day
15
 */
16
class Heartbeat {
17
18
	/**
19
	 * Holds the singleton instance of this class
20
	 *
21
	 * @since 2.3.3
22
	 * @var Heartbeat
23
	 */
24
	private static $instance = false;
25
26
	/**
27
	 * Cronjob identifier
28
	 *
29
	 * @var string
30
	 */
31
	private $cron_name = 'jetpack_v2_heartbeat';
32
33
	/**
34
	 * Singleton
35
	 *
36
	 * @since 2.3.3
37
	 * @static
38
	 * @return Heartbeat
39
	 */
40
	public static function init() {
41
		if ( ! self::$instance ) {
42
			self::$instance = new Heartbeat();
43
		}
44
45
		return self::$instance;
46
	}
47
48
	/**
49
	 * Constructor for singleton
50
	 *
51
	 * @since 2.3.3
52
	 * @return Heartbeat
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
53
	 */
54 View Code Duplication
	private function __construct() {
55
56
		// Schedule the task.
57
		add_action( $this->cron_name, array( $this, 'cron_exec' ) );
58
59
		if ( ! wp_next_scheduled( $this->cron_name ) ) {
60
			// Deal with the old pre-3.0 weekly one.
61
			$timestamp = wp_next_scheduled( 'jetpack_heartbeat' );
62
			if ( $timestamp ) {
63
				wp_unschedule_event( $timestamp, 'jetpack_heartbeat' );
64
			}
65
66
			wp_schedule_event( time(), 'daily', $this->cron_name );
67
		}
68
69
		add_filter( 'jetpack_xmlrpc_methods', array( __CLASS__, 'jetpack_xmlrpc_methods' ) );
70
	}
71
72
	/**
73
	 * Method that gets executed on the wp-cron call
74
	 *
75
	 * @since 2.3.3
76
	 * @global string $wp_version
77
	 */
78 View Code Duplication
	public function cron_exec() {
79
80
		$a8c_mc_stats = new A8c_Mc_Stats();
81
82
		/*
83
		 * This should run daily.  Figuring in for variances in
84
		 * WP_CRON, don't let it run more than every 23 hours at most.
85
		 *
86
		 * i.e. if it ran less than 23 hours ago, fail out.
87
		 */
88
		$last = (int) \Jetpack_Options::get_option( 'last_heartbeat' );
89
		if ( $last && ( $last + DAY_IN_SECONDS - HOUR_IN_SECONDS > time() ) ) {
90
			return;
91
		}
92
93
		/*
94
		 * Check for an identity crisis
95
		 *
96
		 * If one exists:
97
		 * - Bump stat for ID crisis
98
		 * - Email site admin about potential ID crisis
99
		 */
100
101
		// Coming Soon!
102
103
		foreach ( self::generate_stats_array( 'v2-' ) as $key => $value ) {
104
			$a8c_mc_stats->add( $key, $value );
105
		}
106
107
		\Jetpack_Options::update_option( 'last_heartbeat', time() );
108
109
		$a8c_mc_stats->do_server_side_stats();
110
111
		/**
112
		 * Fires when we synchronize all registered options on heartbeat.
113
		 *
114
		 * @since 3.3.0
115
		 */
116
		do_action( 'jetpack_heartbeat' );
117
	}
118
119
	/**
120
	 * Generates heartbeat stats data.
121
	 *
122
	 * @param string $prefix Prefix to add before stats identifier.
123
	 *
124
	 * @return array The stats array.
125
	 */
126
	public static function generate_stats_array( $prefix = '' ) {
127
128
		/**
129
		 * This filter is used to build the array of stats that are bumped once a day by Jetpack Heartbeat.
130
		 *
131
		 * Filter the array and add key => value pairs where
132
		 * * key is the stat group name
133
		 * * value is the stat name.
134
		 *
135
		 * Example:
136
		 * add_filter( 'jetpack_heartbeat_stats_array', function( $stats ) {
137
		 *    $stats['is-https'] = is_ssl() ? 'https' : 'http';
138
		 * });
139
		 *
140
		 * This will bump the stats for the 'is-https/https' or 'is-https/http' stat.
141
		 *
142
		 * @param array  $stats The stats to be filtered.
143
		 * @param string $prefix The prefix that will automatically be added at the begining at each stat group name.
144
		 */
145
		$stats  = apply_filters( 'jetpack_heartbeat_stats_array', array(), $prefix );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $prefix.

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...
146
		$return = array();
147
148
		// Apply prefix to stats.
149
		foreach ( $stats as $stat => $value ) {
150
			$return[ "$prefix$stat" ] = $value;
151
		}
152
153
		return $return;
154
155
	}
156
157
	/**
158
	 * Registers jetpack.getHeartbeatData xmlrpc method
159
	 *
160
	 * @param array $methods The list of methods to be filtered.
161
	 * @return array $methods
162
	 */
163
	public static function jetpack_xmlrpc_methods( $methods ) {
164
		$methods['jetpack.getHeartbeatData'] = array( __CLASS__, 'xmlrpc_data_response' );
165
		return $methods;
166
	}
167
168
	/**
169
	 * Handles the response for the jetpack.getHeartbeatData xmlrpc method
170
	 *
171
	 * @param array $params The parameters received in the request.
172
	 * @return array $params all the stats that hearbeat handles.
173
	 */
174
	public static function xmlrpc_data_response( $params = array() ) {
175
		// The WordPress XML-RPC server sets a default param of array()
176
		// if no argument is passed on the request and the method handlers get this array in $params.
177
		// generate_stats_array() needs a string as first argument.
178
		$params = empty( $params ) ? '' : $params;
179
		return self::generate_stats_array( $params );
0 ignored issues
show
Bug introduced by
It seems like $params defined by empty($params) ? '' : $params on line 178 can also be of type array; however, Automattic\Jetpack\Heart...:generate_stats_array() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
180
	}
181
182
	/**
183
	 * Clear scheduled events
184
	 *
185
	 * @return void
186
	 */
187 View Code Duplication
	public function deactivate() {
188
		// Deal with the old pre-3.0 weekly one.
189
		$timestamp = wp_next_scheduled( 'jetpack_heartbeat' );
190
		if ( $timestamp ) {
191
			wp_unschedule_event( $timestamp, 'jetpack_heartbeat' );
192
		}
193
194
		$timestamp = wp_next_scheduled( $this->cron_name );
195
		wp_unschedule_event( $timestamp, $this->cron_name );
196
	}
197
198
}
199