summaryrefslogtreecommitdiff
blob: bbd4cae605028852e3a6aca01716a421d2cfb978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
 * Stats sync module.
 *
 * @package automattic/jetpack-sync
 */

namespace Automattic\Jetpack\Sync\Modules;

/**
 * Class to handle sync for stats.
 */
class Stats extends Module {
	/**
	 * Sync module name.
	 *
	 * @access public
	 *
	 * @return string
	 */
	public function name() {
		return 'stats';
	}

	/**
	 * Initialize stats action listeners.
	 *
	 * @access public
	 *
	 * @param callable $callback Action handler callable.
	 */
	public function init_listeners( $callback ) {
		add_action( 'jetpack_heartbeat', array( $this, 'sync_site_stats' ), 20 );
		add_action( 'jetpack_sync_heartbeat_stats', $callback );
	}

	/**
	 * This namespaces the action that we sync.
	 * So that we can differentiate it from future actions.
	 *
	 * @access public
	 */
	public function sync_site_stats() {
		do_action( 'jetpack_sync_heartbeat_stats' );
	}

	/**
	 * Initialize the module in the sender.
	 *
	 * @access public
	 */
	public function init_before_send() {
		add_filter( 'jetpack_sync_before_send_jetpack_sync_heartbeat_stats', array( $this, 'add_stats' ) );
	}

	/**
	 * Retrieve the stats data for the site.
	 *
	 * @access public
	 *
	 * @return array Stats data.
	 */
	public function add_stats() {
		return array( \Jetpack::get_stat_data( false, false ) );
	}
}