Completed
Branch BUG-9647-cpt-queries (303307)
by
unknown
201:19 queued 184:10
created

EE_Messages_Scheduler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 108
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B initiate_scheduled_non_blocking_request() 0 23 6
A __construct() 0 15 3
A custom_schedules() 0 7 1
A register_scheduled_tasks() 0 5 1
A batch_generation() 0 3 1
A batch_sending() 0 3 1
1
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); }
2
3
/**
4
 * This class is used for setting scheduled tasks related to the EE_messages system.
5
 *
6
 * @package    Event Espresso
7
 * @subpackage messages
8
 * @author     Darren Ethier
9
 * @since      4.9.0
10
 */
11
class EE_Messages_Scheduler extends EE_BASE {
12
13
	/**
14
	 * Number of seconds between batch sends/generates on the cron job.
15
	 * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
16
	 * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
17
	 * @type int
18
	 */
19
	const message_cron_schedule = 300;
20
21
	/**
22
	 * Constructor
23
	 */
24
	public function __construct() {
25
		//register tasks (and make sure only registered once).
26
		if ( ! has_action( 'FHEE__EEH_Activation__get_cron_tasks', array( $this, 'register_scheduled_tasks' ) ) ) {
27
			add_action( 'FHEE__EEH_Activation__get_cron_tasks', array( $this, 'register_scheduled_tasks' ), 10 );
28
		}
29
30
		//register callbacks for scheduled events (but make sure they are set only once).
31
		if ( ! has_action( 'AHEE__EE_Messages_Scheduler__generation', array( 'EE_Messages_Scheduler', 'batch_generation' ) ) ) {
32
			add_action( 'AHEE__EE_Messages_Scheduler__generation', array( 'EE_Messages_Scheduler', 'batch_generation') );
33
			add_action( 'AHEE__EE_Messages_Scheduler__sending', array( 'EE_Messages_Scheduler', 'batch_sending' ) );
34
		}
35
36
		//add custom schedules
37
		add_filter( 'cron_schedules', array( $this, 'custom_schedules' ) );
38
	}
39
40
41
42
43
	/**
44
	 * Add custom schedules for wp_cron
45
	 * @param $schedules
46
	 */
47
	public function custom_schedules( $schedules ) {
48
		$schedules['ee_message_cron'] = array(
49
			'interval' => self::message_cron_schedule,
50
			'display' => __( 'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)', 'event_espresso' )
51
		);
52
		return $schedules;
53
	}
54
55
56
	/**
57
	 * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and remove.
58
	 * @param array $tasks  already existing scheduled tasks
59
	 * @return array
60
	 */
61
	public function register_scheduled_tasks( $tasks ) {
62
		$tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
63
		$tasks['AHEE__EE_Messages_Scheduler__sending'] = 'ee_message_cron';
64
		return $tasks;
65
	}
66
67
68
	/**
69
	 * This initiates a non-blocking separate request to execute on a scheduled task.
70
	 * Note: The EED_Messages module has the handlers for these requests.
71
	 * @param string $task  The task the request is being generated for.
72
	 */
73
	public static function initiate_scheduled_non_blocking_request( $task ) {
74
		//transient is used for flood control on msg_cron_trigger requests
75
		$transient_key = 'ee_trans_' . uniqid( $task );
76
		set_transient( $transient_key, 1, 5 * MINUTE_IN_SECONDS );
77
		$request_url = add_query_arg(
78
			array(
79
				'ee' => 'msg_cron_trigger',
80
				'type' => $task,
81
				'key' => $transient_key,
82
			),
83
			site_url()
84
		);
85
		$request_args = array(
86
			'timeout' => 300,
87
			'blocking' => ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? true : false,
88
			'sslverify' => false,
89
			'redirection' => 10,
90
		);
91
		$response = wp_remote_get( $request_url, $request_args );
92
		if ( is_wp_error( $response ) ) {
93
			trigger_error( $response->get_error_message() );
94
		}
95
	}
96
97
98
99
100
101
	/**
102
	 * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
103
	 */
104
	public static function batch_generation() {
105
		EE_Messages_Scheduler::initiate_scheduled_non_blocking_request( 'generate' );
106
	}
107
108
109
110
111
	/**
112
	 * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
113
	 */
114
	public static function batch_sending() {
115
		EE_Messages_Scheduler::initiate_scheduled_non_blocking_request( 'send' );
116
	}
117
118
} //end EE_Messages_Scheduler