Completed
Branch BUG/check-message-crons (b42914)
by
unknown
08:54 queued 45s
created
core/libraries/messages/EE_Messages_Scheduler.lib.php 2 patches
Indentation   +204 added lines, -204 removed lines patch added patch discarded remove patch
@@ -11,208 +11,208 @@
 block discarded – undo
11 11
 class EE_Messages_Scheduler extends EE_Base
12 12
 {
13 13
 
14
-    /**
15
-     * Number of seconds between batch sends/generates on the cron job.
16
-     * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
17
-     * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
18
-     *
19
-     * @type int
20
-     */
21
-    const message_cron_schedule = 300;
22
-
23
-    /**
24
-     * Constructor
25
-     */
26
-    public function __construct()
27
-    {
28
-        // register tasks (and make sure only registered once).
29
-        if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
30
-            add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
31
-        }
32
-
33
-        // register callbacks for scheduled events (but make sure they are set only once).
34
-        if (! has_action(
35
-            'AHEE__EE_Messages_Scheduler__generation',
36
-            array('EE_Messages_Scheduler', 'batch_generation')
37
-        )) {
38
-            add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
39
-        }
40
-        if (! has_action(
41
-            'AHEE__EE_Messages_Scheduler__sending',
42
-            array('EE_Messages_Scheduler', 'batch_sending')
43
-        )) {
44
-            add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
45
-        }
46
-        if (! has_action(
47
-            'AHEE__EE_Messages_Scheduler__cleanup',
48
-            array('EE_Messages_Scheduler', 'cleanup')
49
-        )) {
50
-            add_action('AHEE__EE_Messages_Scheduler__cleanup', array('EE_Messages_Scheduler', 'cleanup'));
51
-        }
52
-
53
-        // add custom schedules
54
-        add_filter('cron_schedules', array($this, 'custom_schedules'));
55
-    }
56
-
57
-
58
-    /**
59
-     * Add custom schedules for wp_cron
60
-     *
61
-     * @param $schedules
62
-     */
63
-    public function custom_schedules($schedules)
64
-    {
65
-        $schedules['ee_message_cron'] = array(
66
-            'interval' => self::message_cron_schedule,
67
-            'display'  => __(
68
-                'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)',
69
-                'event_espresso'
70
-            ),
71
-        );
72
-        return $schedules;
73
-    }
74
-
75
-
76
-    /**
77
-     * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and
78
-     * remove.
79
-     *
80
-     * @param array $tasks already existing scheduled tasks
81
-     * @return array
82
-     */
83
-    public function register_scheduled_tasks($tasks)
84
-    {
85
-        EE_Registry::instance()->load_helper('DTT_Helper');
86
-        $tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
87
-        $tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
88
-        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
89
-        return $tasks;
90
-    }
91
-
92
-
93
-    /**
94
-     * This initiates a non-blocking separate request to execute on a scheduled task.
95
-     * Note: The EED_Messages module has the handlers for these requests.
96
-     *
97
-     * @param string $task The task the request is being generated for.
98
-     */
99
-    public static function initiate_scheduled_non_blocking_request($task)
100
-    {
101
-        if (apply_filters(
102
-            'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
103
-            true
104
-        )) {
105
-            $request_url  = add_query_arg(
106
-                array_merge(
107
-                    array('ee' => 'msg_cron_trigger'),
108
-                    EE_Messages_Scheduler::get_request_params($task)
109
-                ),
110
-                site_url()
111
-            );
112
-            $request_args = array(
113
-                'timeout'     => 300,
114
-                'blocking'    => (defined('DOING_CRON') && DOING_CRON) || (defined('DOING_AJAX') && DOING_AJAX) ? true : false,
115
-                'sslverify'   => false,
116
-                'redirection' => 10,
117
-            );
118
-            $response     = wp_remote_get($request_url, $request_args);
119
-            if (is_wp_error($response)) {
120
-                trigger_error($response->get_error_message());
121
-            }
122
-        } else {
123
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron($task);
124
-        }
125
-    }
126
-
127
-
128
-    /**
129
-     * This returns
130
-     * the request params used for a scheduled message task request.
131
-     *
132
-     * @param string $task The task the request is for.
133
-     * @return array
134
-     */
135
-    public static function get_request_params($task)
136
-    {
137
-        // transient is used for flood control on msg_cron_trigger requests
138
-        $transient_key = 'ee_trans_' . uniqid($task);
139
-        set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
140
-        return array(
141
-            'type' => $task,
142
-            'key'  => $transient_key,
143
-        );
144
-    }
145
-
146
-
147
-    /**
148
-     * This is used to execute an immediate call to the run_cron task performed by EED_Messages
149
-     *
150
-     * @param string $task The task the request is being generated for.
151
-     */
152
-    public static function initiate_immediate_request_on_cron($task)
153
-    {
154
-        $request_args = EE_Messages_Scheduler::get_request_params($task);
155
-        // set those request args in the request so it gets picked up
156
-        foreach ($request_args as $request_key => $request_value) {
157
-            EE_Registry::instance()->REQ->set($request_key, $request_value);
158
-        }
159
-        EED_Messages::instance()->run_cron();
160
-    }
161
-
162
-
163
-    /**
164
-     * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
165
-     */
166
-    public static function batch_generation()
167
-    {
168
-        /**
169
-         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
170
-         */
171
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
172
-            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
173
-        ) {
174
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
175
-        }
176
-    }
177
-
178
-
179
-    /**
180
-     * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
181
-     */
182
-    public static function batch_sending()
183
-    {
184
-        /**
185
-         * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
186
-         */
187
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
188
-            || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
189
-        ) {
190
-            EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
191
-        }
192
-    }
193
-
194
-
195
-    /**
196
-     * This is the callback for the `AHEE__EE_Messages_Scheduler__cleanup` scheduled event action.
197
-     * This runs once a day and if cleanup is active (set via messages settings), it will (by default) delete permanently
198
-     * from the database messages that have a MSG_modified date older than 30 days.
199
-     */
200
-    public static function cleanup()
201
-    {
202
-        // first check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
203
-        // until the next scheduled event.
204
-        if (! EE_Registry::instance()->CFG->messages->delete_threshold
205
-            || ! EE_Maintenance_Mode::instance()->models_can_query()
206
-        ) {
207
-            return;
208
-        }
209
-
210
-        /**
211
-         * This filter switch allows other code (such as the EE_Worker_Queue add-on) to replace this with its own handling
212
-         * of deleting messages.
213
-         */
214
-        if (apply_filters('FHEE__EE_Messages_Scheduler__cleanup__handle_cleanup_on_cron', true)) {
215
-            EEM_Message::instance()->delete_old_messages(EE_Registry::instance()->CFG->messages->delete_threshold);
216
-        }
217
-    }
14
+	/**
15
+	 * Number of seconds between batch sends/generates on the cron job.
16
+	 * Defaults to 5 minutes in seconds.  If you want to change this interval, you can use the native WordPress
17
+	 * `cron_schedules` filter and modify the existing custom `ee_message_cron` schedule interval added.
18
+	 *
19
+	 * @type int
20
+	 */
21
+	const message_cron_schedule = 300;
22
+
23
+	/**
24
+	 * Constructor
25
+	 */
26
+	public function __construct()
27
+	{
28
+		// register tasks (and make sure only registered once).
29
+		if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
30
+			add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
31
+		}
32
+
33
+		// register callbacks for scheduled events (but make sure they are set only once).
34
+		if (! has_action(
35
+			'AHEE__EE_Messages_Scheduler__generation',
36
+			array('EE_Messages_Scheduler', 'batch_generation')
37
+		)) {
38
+			add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
39
+		}
40
+		if (! has_action(
41
+			'AHEE__EE_Messages_Scheduler__sending',
42
+			array('EE_Messages_Scheduler', 'batch_sending')
43
+		)) {
44
+			add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
45
+		}
46
+		if (! has_action(
47
+			'AHEE__EE_Messages_Scheduler__cleanup',
48
+			array('EE_Messages_Scheduler', 'cleanup')
49
+		)) {
50
+			add_action('AHEE__EE_Messages_Scheduler__cleanup', array('EE_Messages_Scheduler', 'cleanup'));
51
+		}
52
+
53
+		// add custom schedules
54
+		add_filter('cron_schedules', array($this, 'custom_schedules'));
55
+	}
56
+
57
+
58
+	/**
59
+	 * Add custom schedules for wp_cron
60
+	 *
61
+	 * @param $schedules
62
+	 */
63
+	public function custom_schedules($schedules)
64
+	{
65
+		$schedules['ee_message_cron'] = array(
66
+			'interval' => self::message_cron_schedule,
67
+			'display'  => __(
68
+				'This is the cron time interval for EE Message schedules (defaults to once every 5 minutes)',
69
+				'event_espresso'
70
+			),
71
+		);
72
+		return $schedules;
73
+	}
74
+
75
+
76
+	/**
77
+	 * Callback for FHEE__EEH_Activation__get_cron_tasks that is used to retrieve scheduled Cron events to add and
78
+	 * remove.
79
+	 *
80
+	 * @param array $tasks already existing scheduled tasks
81
+	 * @return array
82
+	 */
83
+	public function register_scheduled_tasks($tasks)
84
+	{
85
+		EE_Registry::instance()->load_helper('DTT_Helper');
86
+		$tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
87
+		$tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
88
+		$tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
89
+		return $tasks;
90
+	}
91
+
92
+
93
+	/**
94
+	 * This initiates a non-blocking separate request to execute on a scheduled task.
95
+	 * Note: The EED_Messages module has the handlers for these requests.
96
+	 *
97
+	 * @param string $task The task the request is being generated for.
98
+	 */
99
+	public static function initiate_scheduled_non_blocking_request($task)
100
+	{
101
+		if (apply_filters(
102
+			'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
103
+			true
104
+		)) {
105
+			$request_url  = add_query_arg(
106
+				array_merge(
107
+					array('ee' => 'msg_cron_trigger'),
108
+					EE_Messages_Scheduler::get_request_params($task)
109
+				),
110
+				site_url()
111
+			);
112
+			$request_args = array(
113
+				'timeout'     => 300,
114
+				'blocking'    => (defined('DOING_CRON') && DOING_CRON) || (defined('DOING_AJAX') && DOING_AJAX) ? true : false,
115
+				'sslverify'   => false,
116
+				'redirection' => 10,
117
+			);
118
+			$response     = wp_remote_get($request_url, $request_args);
119
+			if (is_wp_error($response)) {
120
+				trigger_error($response->get_error_message());
121
+			}
122
+		} else {
123
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron($task);
124
+		}
125
+	}
126
+
127
+
128
+	/**
129
+	 * This returns
130
+	 * the request params used for a scheduled message task request.
131
+	 *
132
+	 * @param string $task The task the request is for.
133
+	 * @return array
134
+	 */
135
+	public static function get_request_params($task)
136
+	{
137
+		// transient is used for flood control on msg_cron_trigger requests
138
+		$transient_key = 'ee_trans_' . uniqid($task);
139
+		set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
140
+		return array(
141
+			'type' => $task,
142
+			'key'  => $transient_key,
143
+		);
144
+	}
145
+
146
+
147
+	/**
148
+	 * This is used to execute an immediate call to the run_cron task performed by EED_Messages
149
+	 *
150
+	 * @param string $task The task the request is being generated for.
151
+	 */
152
+	public static function initiate_immediate_request_on_cron($task)
153
+	{
154
+		$request_args = EE_Messages_Scheduler::get_request_params($task);
155
+		// set those request args in the request so it gets picked up
156
+		foreach ($request_args as $request_key => $request_value) {
157
+			EE_Registry::instance()->REQ->set($request_key, $request_value);
158
+		}
159
+		EED_Messages::instance()->run_cron();
160
+	}
161
+
162
+
163
+	/**
164
+	 * Callback for scheduled AHEE__EE_Messages_Scheduler__generation wp cron event
165
+	 */
166
+	public static function batch_generation()
167
+	{
168
+		/**
169
+		 * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
170
+		 */
171
+		if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
172
+			|| ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
173
+		) {
174
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
175
+		}
176
+	}
177
+
178
+
179
+	/**
180
+	 * Callback for scheduled AHEE__EE_Messages_Scheduler__sending
181
+	 */
182
+	public static function batch_sending()
183
+	{
184
+		/**
185
+		 * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
186
+		 */
187
+		if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
188
+			|| ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
189
+		) {
190
+			EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
191
+		}
192
+	}
193
+
194
+
195
+	/**
196
+	 * This is the callback for the `AHEE__EE_Messages_Scheduler__cleanup` scheduled event action.
197
+	 * This runs once a day and if cleanup is active (set via messages settings), it will (by default) delete permanently
198
+	 * from the database messages that have a MSG_modified date older than 30 days.
199
+	 */
200
+	public static function cleanup()
201
+	{
202
+		// first check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
203
+		// until the next scheduled event.
204
+		if (! EE_Registry::instance()->CFG->messages->delete_threshold
205
+			|| ! EE_Maintenance_Mode::instance()->models_can_query()
206
+		) {
207
+			return;
208
+		}
209
+
210
+		/**
211
+		 * This filter switch allows other code (such as the EE_Worker_Queue add-on) to replace this with its own handling
212
+		 * of deleting messages.
213
+		 */
214
+		if (apply_filters('FHEE__EE_Messages_Scheduler__cleanup__handle_cleanup_on_cron', true)) {
215
+			EEM_Message::instance()->delete_old_messages(EE_Registry::instance()->CFG->messages->delete_threshold);
216
+		}
217
+	}
218 218
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -26,24 +26,24 @@  discard block
 block discarded – undo
26 26
     public function __construct()
27 27
     {
28 28
         // register tasks (and make sure only registered once).
29
-        if (! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
29
+        if ( ! has_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'))) {
30 30
             add_action('FHEE__EEH_Activation__get_cron_tasks', array($this, 'register_scheduled_tasks'), 10);
31 31
         }
32 32
 
33 33
         // register callbacks for scheduled events (but make sure they are set only once).
34
-        if (! has_action(
34
+        if ( ! has_action(
35 35
             'AHEE__EE_Messages_Scheduler__generation',
36 36
             array('EE_Messages_Scheduler', 'batch_generation')
37 37
         )) {
38 38
             add_action('AHEE__EE_Messages_Scheduler__generation', array('EE_Messages_Scheduler', 'batch_generation'));
39 39
         }
40
-        if (! has_action(
40
+        if ( ! has_action(
41 41
             'AHEE__EE_Messages_Scheduler__sending',
42 42
             array('EE_Messages_Scheduler', 'batch_sending')
43 43
         )) {
44 44
             add_action('AHEE__EE_Messages_Scheduler__sending', array('EE_Messages_Scheduler', 'batch_sending'));
45 45
         }
46
-        if (! has_action(
46
+        if ( ! has_action(
47 47
             'AHEE__EE_Messages_Scheduler__cleanup',
48 48
             array('EE_Messages_Scheduler', 'cleanup')
49 49
         )) {
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
         EE_Registry::instance()->load_helper('DTT_Helper');
86 86
         $tasks['AHEE__EE_Messages_Scheduler__generation'] = 'ee_message_cron';
87 87
         $tasks['AHEE__EE_Messages_Scheduler__sending']    = 'ee_message_cron';
88
-        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array( EEH_DTT_Helper::tomorrow(), 'daily');
88
+        $tasks['AHEE__EE_Messages_Scheduler__cleanup'] = array(EEH_DTT_Helper::tomorrow(), 'daily');
89 89
         return $tasks;
90 90
     }
91 91
 
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
             'EE_Messages_Scheduler__initiate_scheduled_non_blocking_request__do_separate_request',
103 103
             true
104 104
         )) {
105
-            $request_url  = add_query_arg(
105
+            $request_url = add_query_arg(
106 106
                 array_merge(
107 107
                     array('ee' => 'msg_cron_trigger'),
108 108
                     EE_Messages_Scheduler::get_request_params($task)
@@ -115,7 +115,7 @@  discard block
 block discarded – undo
115 115
                 'sslverify'   => false,
116 116
                 'redirection' => 10,
117 117
             );
118
-            $response     = wp_remote_get($request_url, $request_args);
118
+            $response = wp_remote_get($request_url, $request_args);
119 119
             if (is_wp_error($response)) {
120 120
                 trigger_error($response->get_error_message());
121 121
             }
@@ -135,7 +135,7 @@  discard block
 block discarded – undo
135 135
     public static function get_request_params($task)
136 136
     {
137 137
         // transient is used for flood control on msg_cron_trigger requests
138
-        $transient_key = 'ee_trans_' . uniqid($task);
138
+        $transient_key = 'ee_trans_'.uniqid($task);
139 139
         set_transient($transient_key, 1, 5 * MINUTE_IN_SECONDS);
140 140
         return array(
141 141
             'type' => $task,
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
         /**
169 169
          * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
170 170
          */
171
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
171
+        if ( ! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
172 172
             || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
173 173
         ) {
174 174
             EE_Messages_Scheduler::initiate_immediate_request_on_cron('generate');
@@ -184,7 +184,7 @@  discard block
 block discarded – undo
184 184
         /**
185 185
          * @see filter usage in EE_Messages_Queue::initiate_request_by_priority()
186 186
          */
187
-        if (! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
187
+        if ( ! apply_filters('FHEE__EE_Messages_Processor__initiate_request_by_priority__do_immediate_processing', false)
188 188
             || ! EE_Registry::instance()->NET_CFG->core->do_messages_on_same_request
189 189
         ) {
190 190
             EE_Messages_Scheduler::initiate_immediate_request_on_cron('send');
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
     {
202 202
         // first check if user has cleanup turned on or if we're in maintenance mode.  If in maintenance mode we'll wait
203 203
         // until the next scheduled event.
204
-        if (! EE_Registry::instance()->CFG->messages->delete_threshold
204
+        if ( ! EE_Registry::instance()->CFG->messages->delete_threshold
205 205
             || ! EE_Maintenance_Mode::instance()->models_can_query()
206 206
         ) {
207 207
             return;
Please login to merge, or discard this patch.