<?php
/**
 * Stats sync module.
 *
 * @package automattic/jetpack-sync
 */

namespace Automattic\Jetpack\Sync\Modules;

use Automattic\Jetpack\Heartbeat;

if ( ! defined( 'ABSPATH' ) ) {
	exit( 0 );
}

/**
 * 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 Unused - required for parent class compatibility.
	 */
	public function init_listeners( $callback ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
		add_action( 'jetpack_heartbeat', array( $this, 'sync_site_stats' ), 20 );
	}

	/**
	 * Send Heartbeat stats immediately.
	 *
	 * @access public
	 */
	public function sync_site_stats() {

		$this->send_action( 'jetpack_sync_heartbeat_stats', $this->add_stats() );
	}

	/**
	 * Retrieve the stats data for the site.
	 *
	 * @access public
	 *
	 * @return array Stats data.
	 */
	public function add_stats() {
		return array( Heartbeat::generate_stats_array() );
	}
}
