Completed
Branch decaf-fixes/main (e0ac96)
by
unknown
09:44 queued 07:50
created
core/services/request/CurrentPage.php 1 patch
Indentation   +314 added lines, -314 removed lines patch added patch discarded remove patch
@@ -23,318 +23,318 @@
 block discarded – undo
23 23
  */
24 24
 class CurrentPage
25 25
 {
26
-    /**
27
-     * @var EE_CPT_Strategy
28
-     */
29
-    private $cpt_strategy;
30
-
31
-    /**
32
-     * @var bool
33
-     */
34
-    private $initialized;
35
-
36
-    /**
37
-     * @var bool
38
-     */
39
-    private $is_espresso_page = false;
40
-
41
-    /**
42
-     * @var int
43
-     */
44
-    private $post_id = 0;
45
-
46
-    /**
47
-     * @var string
48
-     */
49
-    private $post_name = '';
50
-
51
-    /**
52
-     * @var array
53
-     */
54
-    private $post_type = [];
55
-
56
-    /**
57
-     * @var RequestInterface $request
58
-     */
59
-    private $request;
60
-
61
-
62
-    /**
63
-     * CurrentPage constructor.
64
-     *
65
-     * @param EE_CPT_Strategy  $cpt_strategy
66
-     * @param RequestInterface $request
67
-     */
68
-    public function __construct(EE_CPT_Strategy $cpt_strategy, RequestInterface $request)
69
-    {
70
-        $this->cpt_strategy = $cpt_strategy;
71
-        $this->request      = $request;
72
-        $this->initialized  = is_admin();
73
-        // analyse the incoming WP request
74
-        add_action('parse_request', [$this, 'parseQueryVars'], 2, 1);
75
-    }
76
-
77
-
78
-    /**
79
-     * @param WP $WP
80
-     * @return void
81
-     */
82
-    public function parseQueryVars(WP $WP = null)
83
-    {
84
-        if ($this->initialized) {
85
-            return;
86
-        }
87
-        // if somebody forgot to provide us with WP, that's ok because its global
88
-        if (! $WP instanceof WP) {
89
-            global $WP;
90
-        }
91
-        $this->post_id   = $this->getPostId($WP);
92
-        $this->post_name = $this->getPostName($WP);
93
-        $this->post_type = $this->getPostType($WP);
94
-        // true or false ? is this page being used by EE ?
95
-        $this->setEspressoPage();
96
-        remove_action('parse_request', [$this, 'parseRequest'], 2);
97
-        $this->initialized = true;
98
-    }
99
-
100
-
101
-    /**
102
-     * Just a helper method for getting the url for the displayed page.
103
-     *
104
-     * @param WP|null $WP
105
-     * @return string
106
-     */
107
-    public function getPermalink(WP $WP = null)
108
-    {
109
-        $post_id = $this->post_id ?: $this->getPostId($WP);
110
-        if ($post_id) {
111
-            return get_permalink($post_id);
112
-        }
113
-        if (! $WP instanceof WP) {
114
-            global $WP;
115
-        }
116
-        if ($WP instanceof WP && $WP->request) {
117
-            return site_url($WP->request);
118
-        }
119
-        return esc_url_raw(site_url($_SERVER['REQUEST_URI']));
120
-    }
121
-
122
-
123
-    /**
124
-     * @return array
125
-     */
126
-    public function espressoPostType()
127
-    {
128
-        return array_filter(
129
-            $this->post_type,
130
-            function ($post_type) {
131
-                return strpos($post_type, 'espresso_') === 0;
132
-            }
133
-        );
134
-    }
135
-
136
-
137
-    /**
138
-     * pokes and prods the WP object query_vars in an attempt to shake out a page/post ID
139
-     *
140
-     * @param WP $WP
141
-     * @return int
142
-     */
143
-    private function getPostId(WP $WP = null)
144
-    {
145
-        $post_id = null;
146
-        if ($WP instanceof WP) {
147
-            // look for the post ID in the aptly named 'p' query var
148
-            if (isset($WP->query_vars['p'])) {
149
-                $post_id = $WP->query_vars['p'];
150
-            }
151
-            // not a post? what about a page?
152
-            if (! $post_id && isset($WP->query_vars['page_id'])) {
153
-                $post_id = $WP->query_vars['page_id'];
154
-            }
155
-            // ok... maybe pretty permalinks are off and the ID is set in the raw request...
156
-            // but hasn't been processed yet ie: this method is being called too early :\
157
-            if (! $post_id && $WP->request !== null && is_numeric(basename($WP->request))) {
158
-                $post_id = basename($WP->request);
159
-            }
160
-        }
161
-        // none of the above? ok what about an explicit "post_id" URL parameter?
162
-        if (! $post_id && $this->request->requestParamIsSet('post_id')) {
163
-            $post_id = $this->request->getRequestParam('post_id');
164
-        }
165
-        return $post_id;
166
-    }
167
-
168
-
169
-    /**
170
-     * similar to getPostId() above but attempts to obtain the "name" for the current page/post
171
-     *
172
-     * @param WP $WP
173
-     * @return string
174
-     */
175
-    private function getPostName(WP $WP = null)
176
-    {
177
-        global $wpdb;
178
-        $post_name = null;
179
-        if ($WP instanceof WP) {
180
-            // if this is a post, then is the post name set?
181
-            if (isset($WP->query_vars['name']) && ! empty($WP->query_vars['name'])) {
182
-                $post_name = $WP->query_vars['name'];
183
-            }
184
-            // what about the page name?
185
-            if (! $post_name && isset($WP->query_vars['pagename']) && ! empty($WP->query_vars['pagename'])) {
186
-                $post_name = $WP->query_vars['pagename'];
187
-            }
188
-            // this stinks but let's run a query to try and get the post name from the URL
189
-            // (assuming pretty permalinks are on)
190
-            if (! $post_name && $WP->request !== null && ! empty($WP->request)) {
191
-                $possible_post_name = basename($WP->request);
192
-                if (! is_numeric($possible_post_name)) {
193
-                    $SQL                = "SELECT ID from {$wpdb->posts}";
194
-                    $SQL                .= " WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash')";
195
-                    $SQL                .= ' AND post_name=%s';
196
-                    $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $possible_post_name));
197
-                    if ($possible_post_name) {
198
-                        $post_name = $possible_post_name;
199
-                    }
200
-                }
201
-            }
202
-        }
203
-        // ug... ok... nothing yet... but do we have a post ID?
204
-        // if so then... sigh... run a query to get the post name :\
205
-        if (! $post_name && $this->post_id) {
206
-            $SQL                = "SELECT post_name from {$wpdb->posts}";
207
-            $SQL                .= " WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash')";
208
-            $SQL                .= ' AND ID=%d';
209
-            $possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $this->post_id));
210
-            if ($possible_post_name) {
211
-                $post_name = $possible_post_name;
212
-            }
213
-        }
214
-        // still nothing? ok what about an explicit 'post_name' URL parameter?
215
-        if (! $post_name && $this->request->requestParamIsSet('post_name')) {
216
-            $post_name = $this->request->getRequestParam('post_name');
217
-        }
218
-        return $post_name;
219
-    }
220
-
221
-
222
-    /**
223
-     * also similar to getPostId() and getPostName() above but not as insane
224
-     *
225
-     * @param WP $WP
226
-     * @return array
227
-     */
228
-    private function getPostType(WP $WP = null)
229
-    {
230
-        $post_types = [];
231
-        if ($WP instanceof WP) {
232
-            $post_types = isset($WP->query_vars['post_type'])
233
-                ? (array) $WP->query_vars['post_type']
234
-                : [];
235
-        }
236
-        if (empty($post_types) && $this->request->requestParamIsSet('post_type')) {
237
-            $post_types = $this->request->getRequestParam('post_type', [], 'arrayOf|string');
238
-        }
239
-        return (array) $post_types;
240
-    }
241
-
242
-
243
-    /**
244
-     * if TRUE, then the current page is somehow utilizing EE logic
245
-     *
246
-     * @return bool
247
-     */
248
-    public function isEspressoPage()
249
-    {
250
-        return $this->is_espresso_page;
251
-    }
252
-
253
-
254
-    /**
255
-     * @return int
256
-     */
257
-    public function postId()
258
-    {
259
-        return $this->post_id;
260
-    }
261
-
262
-
263
-    /**
264
-     * @return string
265
-     */
266
-    public function postName()
267
-    {
268
-        return $this->post_name;
269
-    }
270
-
271
-
272
-    /**
273
-     * @return array
274
-     */
275
-    public function postType()
276
-    {
277
-        return $this->post_type;
278
-    }
279
-
280
-
281
-    /**
282
-     * for manually indicating the current page will utilize EE logic
283
-     *
284
-     * @param null|bool $value
285
-     * @return void
286
-     */
287
-    public function setEspressoPage($value = null)
288
-    {
289
-        $this->is_espresso_page = $value !== null
290
-            ? filter_var($value, FILTER_VALIDATE_BOOLEAN)
291
-            : $this->testForEspressoPage();
292
-    }
293
-
294
-
295
-    /**
296
-     * attempts to determine if the current page/post is an EE related page/post
297
-     * because it utilizes one of our CPT taxonomies, endpoints, or post types
298
-     *
299
-     * @return bool
300
-     */
301
-    private function testForEspressoPage()
302
-    {
303
-        // in case it has already been set
304
-        if ($this->is_espresso_page) {
305
-            return true;
306
-        }
307
-        global $WP;
308
-        $espresso_CPT_taxonomies = $this->cpt_strategy->get_CPT_taxonomies();
309
-        if (is_array($espresso_CPT_taxonomies)) {
310
-            foreach ($espresso_CPT_taxonomies as $espresso_CPT_taxonomy => $details) {
311
-                if (isset($WP->query_vars, $WP->query_vars[ $espresso_CPT_taxonomy ])) {
312
-                    return true;
313
-                }
314
-            }
315
-        }
316
-        // load espresso CPT endpoints
317
-        $espresso_CPT_endpoints  = $this->cpt_strategy->get_CPT_endpoints();
318
-        $post_type_CPT_endpoints = array_flip($espresso_CPT_endpoints);
319
-        foreach ($this->post_type as $post_type) {
320
-            // was a post name passed ?
321
-            if (isset($post_type_CPT_endpoints[ $post_type ])) {
322
-                // kk we know this is an espresso page, but is it a specific post ?
323
-                if (! $this->post_name) {
324
-                    $espresso_post_type = $this->request->getRequestParam('post_type');
325
-                    // there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events
326
-                    // this essentially sets the post_name to "events" (or whatever EE CPT)
327
-                    $post_name = isset($post_type_CPT_endpoints[ $espresso_post_type ])
328
-                        ? $post_type_CPT_endpoints[ $espresso_post_type ]
329
-                        : '';
330
-                    // if the post type matches one of ours then set the post name to the endpoint
331
-                    if ($post_name) {
332
-                        $this->post_name = $post_name;
333
-                    }
334
-                }
335
-                return true;
336
-            }
337
-        }
338
-        return false;
339
-    }
26
+	/**
27
+	 * @var EE_CPT_Strategy
28
+	 */
29
+	private $cpt_strategy;
30
+
31
+	/**
32
+	 * @var bool
33
+	 */
34
+	private $initialized;
35
+
36
+	/**
37
+	 * @var bool
38
+	 */
39
+	private $is_espresso_page = false;
40
+
41
+	/**
42
+	 * @var int
43
+	 */
44
+	private $post_id = 0;
45
+
46
+	/**
47
+	 * @var string
48
+	 */
49
+	private $post_name = '';
50
+
51
+	/**
52
+	 * @var array
53
+	 */
54
+	private $post_type = [];
55
+
56
+	/**
57
+	 * @var RequestInterface $request
58
+	 */
59
+	private $request;
60
+
61
+
62
+	/**
63
+	 * CurrentPage constructor.
64
+	 *
65
+	 * @param EE_CPT_Strategy  $cpt_strategy
66
+	 * @param RequestInterface $request
67
+	 */
68
+	public function __construct(EE_CPT_Strategy $cpt_strategy, RequestInterface $request)
69
+	{
70
+		$this->cpt_strategy = $cpt_strategy;
71
+		$this->request      = $request;
72
+		$this->initialized  = is_admin();
73
+		// analyse the incoming WP request
74
+		add_action('parse_request', [$this, 'parseQueryVars'], 2, 1);
75
+	}
76
+
77
+
78
+	/**
79
+	 * @param WP $WP
80
+	 * @return void
81
+	 */
82
+	public function parseQueryVars(WP $WP = null)
83
+	{
84
+		if ($this->initialized) {
85
+			return;
86
+		}
87
+		// if somebody forgot to provide us with WP, that's ok because its global
88
+		if (! $WP instanceof WP) {
89
+			global $WP;
90
+		}
91
+		$this->post_id   = $this->getPostId($WP);
92
+		$this->post_name = $this->getPostName($WP);
93
+		$this->post_type = $this->getPostType($WP);
94
+		// true or false ? is this page being used by EE ?
95
+		$this->setEspressoPage();
96
+		remove_action('parse_request', [$this, 'parseRequest'], 2);
97
+		$this->initialized = true;
98
+	}
99
+
100
+
101
+	/**
102
+	 * Just a helper method for getting the url for the displayed page.
103
+	 *
104
+	 * @param WP|null $WP
105
+	 * @return string
106
+	 */
107
+	public function getPermalink(WP $WP = null)
108
+	{
109
+		$post_id = $this->post_id ?: $this->getPostId($WP);
110
+		if ($post_id) {
111
+			return get_permalink($post_id);
112
+		}
113
+		if (! $WP instanceof WP) {
114
+			global $WP;
115
+		}
116
+		if ($WP instanceof WP && $WP->request) {
117
+			return site_url($WP->request);
118
+		}
119
+		return esc_url_raw(site_url($_SERVER['REQUEST_URI']));
120
+	}
121
+
122
+
123
+	/**
124
+	 * @return array
125
+	 */
126
+	public function espressoPostType()
127
+	{
128
+		return array_filter(
129
+			$this->post_type,
130
+			function ($post_type) {
131
+				return strpos($post_type, 'espresso_') === 0;
132
+			}
133
+		);
134
+	}
135
+
136
+
137
+	/**
138
+	 * pokes and prods the WP object query_vars in an attempt to shake out a page/post ID
139
+	 *
140
+	 * @param WP $WP
141
+	 * @return int
142
+	 */
143
+	private function getPostId(WP $WP = null)
144
+	{
145
+		$post_id = null;
146
+		if ($WP instanceof WP) {
147
+			// look for the post ID in the aptly named 'p' query var
148
+			if (isset($WP->query_vars['p'])) {
149
+				$post_id = $WP->query_vars['p'];
150
+			}
151
+			// not a post? what about a page?
152
+			if (! $post_id && isset($WP->query_vars['page_id'])) {
153
+				$post_id = $WP->query_vars['page_id'];
154
+			}
155
+			// ok... maybe pretty permalinks are off and the ID is set in the raw request...
156
+			// but hasn't been processed yet ie: this method is being called too early :\
157
+			if (! $post_id && $WP->request !== null && is_numeric(basename($WP->request))) {
158
+				$post_id = basename($WP->request);
159
+			}
160
+		}
161
+		// none of the above? ok what about an explicit "post_id" URL parameter?
162
+		if (! $post_id && $this->request->requestParamIsSet('post_id')) {
163
+			$post_id = $this->request->getRequestParam('post_id');
164
+		}
165
+		return $post_id;
166
+	}
167
+
168
+
169
+	/**
170
+	 * similar to getPostId() above but attempts to obtain the "name" for the current page/post
171
+	 *
172
+	 * @param WP $WP
173
+	 * @return string
174
+	 */
175
+	private function getPostName(WP $WP = null)
176
+	{
177
+		global $wpdb;
178
+		$post_name = null;
179
+		if ($WP instanceof WP) {
180
+			// if this is a post, then is the post name set?
181
+			if (isset($WP->query_vars['name']) && ! empty($WP->query_vars['name'])) {
182
+				$post_name = $WP->query_vars['name'];
183
+			}
184
+			// what about the page name?
185
+			if (! $post_name && isset($WP->query_vars['pagename']) && ! empty($WP->query_vars['pagename'])) {
186
+				$post_name = $WP->query_vars['pagename'];
187
+			}
188
+			// this stinks but let's run a query to try and get the post name from the URL
189
+			// (assuming pretty permalinks are on)
190
+			if (! $post_name && $WP->request !== null && ! empty($WP->request)) {
191
+				$possible_post_name = basename($WP->request);
192
+				if (! is_numeric($possible_post_name)) {
193
+					$SQL                = "SELECT ID from {$wpdb->posts}";
194
+					$SQL                .= " WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash')";
195
+					$SQL                .= ' AND post_name=%s';
196
+					$possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $possible_post_name));
197
+					if ($possible_post_name) {
198
+						$post_name = $possible_post_name;
199
+					}
200
+				}
201
+			}
202
+		}
203
+		// ug... ok... nothing yet... but do we have a post ID?
204
+		// if so then... sigh... run a query to get the post name :\
205
+		if (! $post_name && $this->post_id) {
206
+			$SQL                = "SELECT post_name from {$wpdb->posts}";
207
+			$SQL                .= " WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash')";
208
+			$SQL                .= ' AND ID=%d';
209
+			$possible_post_name = $wpdb->get_var($wpdb->prepare($SQL, $this->post_id));
210
+			if ($possible_post_name) {
211
+				$post_name = $possible_post_name;
212
+			}
213
+		}
214
+		// still nothing? ok what about an explicit 'post_name' URL parameter?
215
+		if (! $post_name && $this->request->requestParamIsSet('post_name')) {
216
+			$post_name = $this->request->getRequestParam('post_name');
217
+		}
218
+		return $post_name;
219
+	}
220
+
221
+
222
+	/**
223
+	 * also similar to getPostId() and getPostName() above but not as insane
224
+	 *
225
+	 * @param WP $WP
226
+	 * @return array
227
+	 */
228
+	private function getPostType(WP $WP = null)
229
+	{
230
+		$post_types = [];
231
+		if ($WP instanceof WP) {
232
+			$post_types = isset($WP->query_vars['post_type'])
233
+				? (array) $WP->query_vars['post_type']
234
+				: [];
235
+		}
236
+		if (empty($post_types) && $this->request->requestParamIsSet('post_type')) {
237
+			$post_types = $this->request->getRequestParam('post_type', [], 'arrayOf|string');
238
+		}
239
+		return (array) $post_types;
240
+	}
241
+
242
+
243
+	/**
244
+	 * if TRUE, then the current page is somehow utilizing EE logic
245
+	 *
246
+	 * @return bool
247
+	 */
248
+	public function isEspressoPage()
249
+	{
250
+		return $this->is_espresso_page;
251
+	}
252
+
253
+
254
+	/**
255
+	 * @return int
256
+	 */
257
+	public function postId()
258
+	{
259
+		return $this->post_id;
260
+	}
261
+
262
+
263
+	/**
264
+	 * @return string
265
+	 */
266
+	public function postName()
267
+	{
268
+		return $this->post_name;
269
+	}
270
+
271
+
272
+	/**
273
+	 * @return array
274
+	 */
275
+	public function postType()
276
+	{
277
+		return $this->post_type;
278
+	}
279
+
280
+
281
+	/**
282
+	 * for manually indicating the current page will utilize EE logic
283
+	 *
284
+	 * @param null|bool $value
285
+	 * @return void
286
+	 */
287
+	public function setEspressoPage($value = null)
288
+	{
289
+		$this->is_espresso_page = $value !== null
290
+			? filter_var($value, FILTER_VALIDATE_BOOLEAN)
291
+			: $this->testForEspressoPage();
292
+	}
293
+
294
+
295
+	/**
296
+	 * attempts to determine if the current page/post is an EE related page/post
297
+	 * because it utilizes one of our CPT taxonomies, endpoints, or post types
298
+	 *
299
+	 * @return bool
300
+	 */
301
+	private function testForEspressoPage()
302
+	{
303
+		// in case it has already been set
304
+		if ($this->is_espresso_page) {
305
+			return true;
306
+		}
307
+		global $WP;
308
+		$espresso_CPT_taxonomies = $this->cpt_strategy->get_CPT_taxonomies();
309
+		if (is_array($espresso_CPT_taxonomies)) {
310
+			foreach ($espresso_CPT_taxonomies as $espresso_CPT_taxonomy => $details) {
311
+				if (isset($WP->query_vars, $WP->query_vars[ $espresso_CPT_taxonomy ])) {
312
+					return true;
313
+				}
314
+			}
315
+		}
316
+		// load espresso CPT endpoints
317
+		$espresso_CPT_endpoints  = $this->cpt_strategy->get_CPT_endpoints();
318
+		$post_type_CPT_endpoints = array_flip($espresso_CPT_endpoints);
319
+		foreach ($this->post_type as $post_type) {
320
+			// was a post name passed ?
321
+			if (isset($post_type_CPT_endpoints[ $post_type ])) {
322
+				// kk we know this is an espresso page, but is it a specific post ?
323
+				if (! $this->post_name) {
324
+					$espresso_post_type = $this->request->getRequestParam('post_type');
325
+					// there's no specific post name set, so maybe it's one of our endpoints like www.domain.com/events
326
+					// this essentially sets the post_name to "events" (or whatever EE CPT)
327
+					$post_name = isset($post_type_CPT_endpoints[ $espresso_post_type ])
328
+						? $post_type_CPT_endpoints[ $espresso_post_type ]
329
+						: '';
330
+					// if the post type matches one of ours then set the post name to the endpoint
331
+					if ($post_name) {
332
+						$this->post_name = $post_name;
333
+					}
334
+				}
335
+				return true;
336
+			}
337
+		}
338
+		return false;
339
+	}
340 340
 }
Please login to merge, or discard this patch.
core/libraries/messages/EE_Messages_Processor.lib.php 1 patch
Indentation   +587 added lines, -587 removed lines patch added patch discarded remove patch
@@ -16,595 +16,595 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * @type EE_Message_Resource_Manager $_message_resource_manager
21
-     */
22
-    protected $_message_resource_manager;
23
-
24
-    /**
25
-     * @type EE_Messages_Queue
26
-     */
27
-    protected $_queue;
28
-
29
-    /**
30
-     * @type  EE_Messages_Generator
31
-     */
32
-    protected $_generator;
33
-
34
-
35
-
36
-
37
-    /**
38
-     * constructor
39
-     *
40
-     * @param EE_Message_Resource_Manager $message_resource_manager
41
-     */
42
-    public function __construct(EE_Message_Resource_Manager $message_resource_manager)
43
-    {
44
-        $this->_message_resource_manager = $message_resource_manager;
45
-        $this->_init_queue_and_generator();
46
-    }
47
-
48
-
49
-
50
-
51
-    /**
52
-     * This method sets (or resets) the various properties for use.
53
-     *
54
-     * - $_queue = holds the messages queue
55
-     * - $_generator = holds the messages generator
56
-     */
57
-    protected function _init_queue_and_generator()
58
-    {
59
-        $this->_generator = EE_Registry::factory('EE_Messages_Generator');
60
-        $this->_queue = $this->_generator->generation_queue();
61
-    }
62
-
63
-
64
-
65
-
66
-    /**
67
-     * This returns the current set queue.
68
-     * @return EE_Messages_Queue
69
-     */
70
-    public function get_queue()
71
-    {
72
-        return $this->_queue;
73
-    }
74
-
75
-
76
-    /**
77
-     * This method can be utilized to process messages from a queue and they will be processed immediately on the same
78
-     * request. Please note that this method alone does not bypass the usual "locks" for generation/sending (it assumes
79
-     * client code has already filtered those if necessary).
80
-     *
81
-     * @param EE_Messages_Queue $queue_to_process
82
-     * @return bool  true for success false for error.
83
-     * @throws EE_Error
84
-     * @throws EE_Error
85
-     */
86
-    public function process_immediately_from_queue(EE_Messages_Queue $queue_to_process)
87
-    {
88
-        $success = false;
89
-        $messages_to_send = array();
90
-        $messages_to_generate = array();
91
-        // loop through and setup the various messages from the queue so we know what is being processed
92
-        $queue_to_process->get_message_repository()->rewind();
93
-        foreach ($queue_to_process->get_message_repository() as $message) {
94
-            if ($message->STS_ID() === EEM_Message::status_incomplete) {
95
-                $messages_to_generate[] = $message;
96
-                continue;
97
-            }
98
-
99
-            if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) {
100
-                $messages_to_send[] = $message;
101
-                continue;
102
-            }
103
-        }
104
-
105
-        // do generation/sends
106
-        if ($messages_to_generate) {
107
-            $success = $this->batch_generate_from_queue($messages_to_generate, true);
108
-        }
109
-
110
-        if ($messages_to_send) {
111
-            $sent = $this->batch_send_from_queue($messages_to_send, true);
112
-            // if there was messages to generate and it failed, then we override any success value for the sending process
113
-            // otherwise we just use the return from batch send.  The intent is that there is a simple response for success/fail.
114
-            // Either everything was successful or we consider it a fail.  To be clear, this is a limitation of doing
115
-            // all messages processing on the same request.
116
-            $success = $messages_to_generate && ! $success ? false : $sent;
117
-        }
118
-        return $success;
119
-    }
120
-
121
-
122
-    /**
123
-     * Calls the EE_Messages_Queue::get_batch_to_generate() method and sends to EE_Messages_Generator.
124
-     *
125
-     * @param  EE_Message[] $messages    Array of EE_Message objects (optional) to build the queue with.
126
-     * @param  bool         $clear_queue Whether to ensure a fresh queue or not.
127
-     *
128
-     * @return bool|EE_Messages_Queue return false if nothing generated.  This returns a new EE_Message_Queue with
129
-     *                                   generated messages.
130
-     */
131
-    public function batch_generate_from_queue($messages = array(), $clear_queue = false)
132
-    {
133
-        if ($this->_build_queue_for_generation($messages, $clear_queue)) {
134
-            $new_queue = $this->_generator->generate();
135
-            if ($new_queue instanceof EE_Messages_Queue) {
136
-                // unlock queue
137
-                $this->_queue->unlock_queue();
138
-                $new_queue->initiate_request_by_priority('send');
139
-                return $new_queue;
140
-            }
141
-        }
142
-        $this->_queue->unlock_queue();
143
-        return false;
144
-    }
145
-
146
-
147
-
148
-    /**
149
-     * This method preps a queue for generation.
150
-     *
151
-     * @since    4.9.0
152
-     *
153
-     * @param EE_Message[] $messages    Array of EE_Message objects to build the queue with
154
-     *
155
-     * @param   bool       $clear_queue This indicates whether the existing queue should be dumped or not.
156
-     *
157
-     * @return bool true means queue prepped, false means there was a lock so no generation please.
158
-     */
159
-    protected function _build_queue_for_generation($messages = array(), $clear_queue = false)
160
-    {
161
-
162
-        if ($clear_queue) {
163
-            $this->_init_queue_and_generator();
164
-        }
165
-
166
-        if ($messages) {
167
-            // if generation is locked then get out now because that means processing is already happening.
168
-            if ($this->_queue->is_locked()) {
169
-                return false;
170
-            }
171
-
172
-            $this->_queue->lock_queue();
173
-            $messages = is_array($messages) ? $messages : array( $messages );
174
-            foreach ($messages as $message) {
175
-                if ($message instanceof EE_Message) {
176
-                    $data = $message->all_extra_meta_array();
177
-                    $this->_queue->add($message, $data);
178
-                }
179
-            }
180
-            return true;
181
-        } else {
182
-            return $this->_queue->get_batch_to_generate();
183
-        }
184
-    }
185
-
186
-
187
-    /**
188
-     * This method preps a queue for sending.
189
-     *
190
-     * @param EE_Message[] $messages
191
-     * @param bool  $clear_queue Used to indicate whether to start with a fresh queue or not.
192
-     *
193
-     * @return bool true means queue prepped, false means there was a lock so no queue prepped.
194
-     */
195
-    protected function _build_queue_for_sending($messages, $clear_queue = false)
196
-    {
197
-        // if sending is locked then get out now because that means processing is already happening.
198
-        if ($this->_queue->is_locked(EE_Messages_Queue::action_sending)) {
199
-            return false;
200
-        }
201
-
202
-        $this->_queue->lock_queue(EE_Messages_Queue::action_sending);
203
-
204
-        if ($clear_queue) {
205
-            $this->_init_queue_and_generator();
206
-        }
207
-
208
-        $messages = is_array($messages) ? $messages : array( $messages );
209
-
210
-        foreach ($messages as $message) {
211
-            $this->_queue->add($message);
212
-        }
213
-        return true;
214
-    }
215
-
216
-
217
-    /**
218
-     * Calls the EE_Message_Queue::get_to_send_batch_and_send() method and then immediately just calls EE_Message_Queue::execute()
219
-     * to iterate and send unsent messages.
220
-     *
221
-     * @param EE_Message[] $messages    If an array of messages is sent in then use it.
222
-     *
223
-     * @param bool         $clear_queue Whether to initialize a new queue or keep the existing one.
224
-     *
225
-     * @return EE_Messages_Queue
226
-     */
227
-    public function batch_send_from_queue($messages = array(), $clear_queue = false)
228
-    {
229
-
230
-        if ($messages && $this->_build_queue_for_sending($messages, $clear_queue)) {
231
-            $this->_queue->execute();
232
-            $this->_queue->unlock_queue(EE_Messages_Queue::action_sending);
233
-        } else {
234
-            // get messages to send and execute.
235
-            $this->_queue->get_to_send_batch_and_send();
236
-        }
237
-        // note: callers can use the EE_Messages_Queue::count_STS_in_queue() method to find out if there were any failed
238
-        // messages in the queue and decide how to handle at that point.
239
-        return $this->_queue;
240
-    }
241
-
242
-
243
-
244
-
245
-
246
-
247
-    /**
248
-     * This immediately generates messages using the given array of EE_Message_To_Generate objects and returns the
249
-     * EE_Message_Queue with the generated messages for the caller to work with.  Note, this does NOT save the generated
250
-     * messages in the queue, leaving it up to the caller to do so.
251
-     *
252
-     * @param EE_Message_To_Generate[] $messages_to_generate
253
-     * @return EE_Messages_Queue
254
-     */
255
-    public function generate_and_return($messages_to_generate)
256
-    {
257
-        $this->_init_queue_and_generator();
258
-        $this->_queue_for_generation_loop($messages_to_generate);
259
-        return $this->_generator->generate(false);
260
-    }
261
-
262
-
263
-
264
-
265
-    /**
266
-     * Executes the generator generate method on the current internal queue, and returns the generated queue.
267
-     * @param  bool     $persist    Indicate whether to instruct the generator to persist the generated queue (true) or not (false).
268
-     * @return EE_Messages_Queue
269
-     */
270
-    public function generate_queue($persist = true)
271
-    {
272
-        return $this->_generator->generate($persist);
273
-    }
19
+	/**
20
+	 * @type EE_Message_Resource_Manager $_message_resource_manager
21
+	 */
22
+	protected $_message_resource_manager;
23
+
24
+	/**
25
+	 * @type EE_Messages_Queue
26
+	 */
27
+	protected $_queue;
28
+
29
+	/**
30
+	 * @type  EE_Messages_Generator
31
+	 */
32
+	protected $_generator;
33
+
34
+
35
+
36
+
37
+	/**
38
+	 * constructor
39
+	 *
40
+	 * @param EE_Message_Resource_Manager $message_resource_manager
41
+	 */
42
+	public function __construct(EE_Message_Resource_Manager $message_resource_manager)
43
+	{
44
+		$this->_message_resource_manager = $message_resource_manager;
45
+		$this->_init_queue_and_generator();
46
+	}
47
+
48
+
49
+
50
+
51
+	/**
52
+	 * This method sets (or resets) the various properties for use.
53
+	 *
54
+	 * - $_queue = holds the messages queue
55
+	 * - $_generator = holds the messages generator
56
+	 */
57
+	protected function _init_queue_and_generator()
58
+	{
59
+		$this->_generator = EE_Registry::factory('EE_Messages_Generator');
60
+		$this->_queue = $this->_generator->generation_queue();
61
+	}
62
+
63
+
64
+
65
+
66
+	/**
67
+	 * This returns the current set queue.
68
+	 * @return EE_Messages_Queue
69
+	 */
70
+	public function get_queue()
71
+	{
72
+		return $this->_queue;
73
+	}
74
+
75
+
76
+	/**
77
+	 * This method can be utilized to process messages from a queue and they will be processed immediately on the same
78
+	 * request. Please note that this method alone does not bypass the usual "locks" for generation/sending (it assumes
79
+	 * client code has already filtered those if necessary).
80
+	 *
81
+	 * @param EE_Messages_Queue $queue_to_process
82
+	 * @return bool  true for success false for error.
83
+	 * @throws EE_Error
84
+	 * @throws EE_Error
85
+	 */
86
+	public function process_immediately_from_queue(EE_Messages_Queue $queue_to_process)
87
+	{
88
+		$success = false;
89
+		$messages_to_send = array();
90
+		$messages_to_generate = array();
91
+		// loop through and setup the various messages from the queue so we know what is being processed
92
+		$queue_to_process->get_message_repository()->rewind();
93
+		foreach ($queue_to_process->get_message_repository() as $message) {
94
+			if ($message->STS_ID() === EEM_Message::status_incomplete) {
95
+				$messages_to_generate[] = $message;
96
+				continue;
97
+			}
98
+
99
+			if (in_array($message->STS_ID(), EEM_Message::instance()->stati_indicating_to_send())) {
100
+				$messages_to_send[] = $message;
101
+				continue;
102
+			}
103
+		}
104
+
105
+		// do generation/sends
106
+		if ($messages_to_generate) {
107
+			$success = $this->batch_generate_from_queue($messages_to_generate, true);
108
+		}
109
+
110
+		if ($messages_to_send) {
111
+			$sent = $this->batch_send_from_queue($messages_to_send, true);
112
+			// if there was messages to generate and it failed, then we override any success value for the sending process
113
+			// otherwise we just use the return from batch send.  The intent is that there is a simple response for success/fail.
114
+			// Either everything was successful or we consider it a fail.  To be clear, this is a limitation of doing
115
+			// all messages processing on the same request.
116
+			$success = $messages_to_generate && ! $success ? false : $sent;
117
+		}
118
+		return $success;
119
+	}
120
+
121
+
122
+	/**
123
+	 * Calls the EE_Messages_Queue::get_batch_to_generate() method and sends to EE_Messages_Generator.
124
+	 *
125
+	 * @param  EE_Message[] $messages    Array of EE_Message objects (optional) to build the queue with.
126
+	 * @param  bool         $clear_queue Whether to ensure a fresh queue or not.
127
+	 *
128
+	 * @return bool|EE_Messages_Queue return false if nothing generated.  This returns a new EE_Message_Queue with
129
+	 *                                   generated messages.
130
+	 */
131
+	public function batch_generate_from_queue($messages = array(), $clear_queue = false)
132
+	{
133
+		if ($this->_build_queue_for_generation($messages, $clear_queue)) {
134
+			$new_queue = $this->_generator->generate();
135
+			if ($new_queue instanceof EE_Messages_Queue) {
136
+				// unlock queue
137
+				$this->_queue->unlock_queue();
138
+				$new_queue->initiate_request_by_priority('send');
139
+				return $new_queue;
140
+			}
141
+		}
142
+		$this->_queue->unlock_queue();
143
+		return false;
144
+	}
145
+
146
+
147
+
148
+	/**
149
+	 * This method preps a queue for generation.
150
+	 *
151
+	 * @since    4.9.0
152
+	 *
153
+	 * @param EE_Message[] $messages    Array of EE_Message objects to build the queue with
154
+	 *
155
+	 * @param   bool       $clear_queue This indicates whether the existing queue should be dumped or not.
156
+	 *
157
+	 * @return bool true means queue prepped, false means there was a lock so no generation please.
158
+	 */
159
+	protected function _build_queue_for_generation($messages = array(), $clear_queue = false)
160
+	{
161
+
162
+		if ($clear_queue) {
163
+			$this->_init_queue_and_generator();
164
+		}
165
+
166
+		if ($messages) {
167
+			// if generation is locked then get out now because that means processing is already happening.
168
+			if ($this->_queue->is_locked()) {
169
+				return false;
170
+			}
171
+
172
+			$this->_queue->lock_queue();
173
+			$messages = is_array($messages) ? $messages : array( $messages );
174
+			foreach ($messages as $message) {
175
+				if ($message instanceof EE_Message) {
176
+					$data = $message->all_extra_meta_array();
177
+					$this->_queue->add($message, $data);
178
+				}
179
+			}
180
+			return true;
181
+		} else {
182
+			return $this->_queue->get_batch_to_generate();
183
+		}
184
+	}
185
+
186
+
187
+	/**
188
+	 * This method preps a queue for sending.
189
+	 *
190
+	 * @param EE_Message[] $messages
191
+	 * @param bool  $clear_queue Used to indicate whether to start with a fresh queue or not.
192
+	 *
193
+	 * @return bool true means queue prepped, false means there was a lock so no queue prepped.
194
+	 */
195
+	protected function _build_queue_for_sending($messages, $clear_queue = false)
196
+	{
197
+		// if sending is locked then get out now because that means processing is already happening.
198
+		if ($this->_queue->is_locked(EE_Messages_Queue::action_sending)) {
199
+			return false;
200
+		}
201
+
202
+		$this->_queue->lock_queue(EE_Messages_Queue::action_sending);
203
+
204
+		if ($clear_queue) {
205
+			$this->_init_queue_and_generator();
206
+		}
207
+
208
+		$messages = is_array($messages) ? $messages : array( $messages );
209
+
210
+		foreach ($messages as $message) {
211
+			$this->_queue->add($message);
212
+		}
213
+		return true;
214
+	}
215
+
216
+
217
+	/**
218
+	 * Calls the EE_Message_Queue::get_to_send_batch_and_send() method and then immediately just calls EE_Message_Queue::execute()
219
+	 * to iterate and send unsent messages.
220
+	 *
221
+	 * @param EE_Message[] $messages    If an array of messages is sent in then use it.
222
+	 *
223
+	 * @param bool         $clear_queue Whether to initialize a new queue or keep the existing one.
224
+	 *
225
+	 * @return EE_Messages_Queue
226
+	 */
227
+	public function batch_send_from_queue($messages = array(), $clear_queue = false)
228
+	{
229
+
230
+		if ($messages && $this->_build_queue_for_sending($messages, $clear_queue)) {
231
+			$this->_queue->execute();
232
+			$this->_queue->unlock_queue(EE_Messages_Queue::action_sending);
233
+		} else {
234
+			// get messages to send and execute.
235
+			$this->_queue->get_to_send_batch_and_send();
236
+		}
237
+		// note: callers can use the EE_Messages_Queue::count_STS_in_queue() method to find out if there were any failed
238
+		// messages in the queue and decide how to handle at that point.
239
+		return $this->_queue;
240
+	}
241
+
242
+
243
+
244
+
245
+
246
+
247
+	/**
248
+	 * This immediately generates messages using the given array of EE_Message_To_Generate objects and returns the
249
+	 * EE_Message_Queue with the generated messages for the caller to work with.  Note, this does NOT save the generated
250
+	 * messages in the queue, leaving it up to the caller to do so.
251
+	 *
252
+	 * @param EE_Message_To_Generate[] $messages_to_generate
253
+	 * @return EE_Messages_Queue
254
+	 */
255
+	public function generate_and_return($messages_to_generate)
256
+	{
257
+		$this->_init_queue_and_generator();
258
+		$this->_queue_for_generation_loop($messages_to_generate);
259
+		return $this->_generator->generate(false);
260
+	}
261
+
262
+
263
+
264
+
265
+	/**
266
+	 * Executes the generator generate method on the current internal queue, and returns the generated queue.
267
+	 * @param  bool     $persist    Indicate whether to instruct the generator to persist the generated queue (true) or not (false).
268
+	 * @return EE_Messages_Queue
269
+	 */
270
+	public function generate_queue($persist = true)
271
+	{
272
+		return $this->_generator->generate($persist);
273
+	}
274 274
 
275 275
 
276 276
 
277 277
 
278
-    /**
279
-     * Queue for generation.  Note this does NOT persist to the db.  Client code should call get_message_repository()->save() if desire
280
-     * to persist.  This method is provided to client code to decide what it wants to do with queued messages for generation.
281
-     * @param EE_Message_To_Generate $message_to_generate
282
-     * @param bool                   $test_send             Whether this item is for a test send or not.
283
-     * @return  EE_Messages_Queue
284
-     */
285
-    public function queue_for_generation(EE_Message_To_Generate $message_to_generate, $test_send = false)
286
-    {
287
-        if ($message_to_generate->valid()) {
288
-            $this->_generator->create_and_add_message_to_queue($message_to_generate, $test_send);
289
-        }
290
-    }
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-    /**
299
-     * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message adds them to the generation queue
300
-     * and then persists to storage.
301
-     *
302
-     * @param EE_Message_To_Generate[] $messages_to_generate
303
-     */
304
-    public function batch_queue_for_generation_and_persist($messages_to_generate)
305
-    {
306
-        $this->_init_queue_and_generator();
307
-        $this->_queue_for_generation_loop($messages_to_generate);
308
-        $this->_queue->save();
309
-    }
310
-
311
-
312
-
313
-
314
-
315
-
316
-    /**
317
-     * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message and adds them to the generation
318
-     * queue.  Does NOT persist to storage (unless there is an error.
319
-     * Client code can retrieve the generated queue by calling EEM_Messages_Processor::get_queue()
320
-     *
321
-     * @param EE_Message_To_Generate[]  $messages_to_generate
322
-     */
323
-    public function batch_queue_for_generation_no_persist($messages_to_generate)
324
-    {
325
-        $this->_init_queue_and_generator();
326
-        $this->_queue_for_generation_loop($messages_to_generate);
327
-    }
328
-
329
-
330
-
331
-
332
-    /**
333
-     * Simply loops through the given array of EE_Message_To_Generate objects and adds them to the _queue as EE_Message
334
-     * objects.
335
-     *
336
-     * @param EE_Message_To_Generate[] $messages_to_generate
337
-     */
338
-    protected function _queue_for_generation_loop($messages_to_generate)
339
-    {
340
-        // make sure is in an array.
341
-        if (! is_array($messages_to_generate)) {
342
-            $messages_to_generate = array( $messages_to_generate );
343
-        }
344
-
345
-        foreach ($messages_to_generate as $message_to_generate) {
346
-            if ($message_to_generate instanceof EE_Message_To_Generate && $message_to_generate->valid()) {
347
-                $this->queue_for_generation($message_to_generate);
348
-            }
349
-        }
350
-    }
351
-
352
-
353
-
354
-
355
-
356
-    /**
357
-     * Receives an array of EE_Message_To_Generate objects and generates the EE_Message objects, then persists (so its
358
-     * queued for sending).
359
-     * @param  EE_Message_To_Generate[]
360
-     * @return EE_Messages_Queue
361
-     */
362
-    public function generate_and_queue_for_sending($messages_to_generate)
363
-    {
364
-        $this->_init_queue_and_generator();
365
-        $this->_queue_for_generation_loop($messages_to_generate);
366
-        return $this->_generator->generate(true);
367
-    }
368
-
369
-
370
-
371
-
372
-
373
-    /**
374
-     * Generate for preview and execute right away.
375
-     *
376
-     * @param   EE_Message_To_Generate $message_to_generate
377
-     * @param   bool                   $test_send                Whether this is a test send or not.
378
-     * @return  EE_Messages_Queue | bool   false if unable to generate otherwise the generated queue.
379
-     */
380
-    public function generate_for_preview(EE_Message_To_Generate $message_to_generate, $test_send = false)
381
-    {
382
-        if (! $message_to_generate->valid()) {
383
-            EE_Error::add_error(
384
-                esc_html__('Unable to generate preview because of invalid data', 'event_espresso'),
385
-                __FILE__,
386
-                __FUNCTION__,
387
-                __LINE__
388
-            );
389
-            return false;
390
-        }
391
-        // just make sure preview is set on the $message_to_generate (in case client forgot)
392
-        $message_to_generate->set_preview(true);
393
-        $this->_init_queue_and_generator();
394
-        $this->queue_for_generation($message_to_generate, $test_send);
395
-        $generated_queue = $this->_generator->generate(false);
396
-        if ($generated_queue->execute(false)) {
397
-            // the first queue item should be the preview
398
-            $generated_queue->get_message_repository()->rewind();
399
-            if (! $generated_queue->get_message_repository()->valid()) {
400
-                return $generated_queue;
401
-            }
402
-            return $generated_queue;
403
-        } else {
404
-            return false;
405
-        }
406
-    }
407
-
408
-
409
-    /**
410
-     * This queues for sending.
411
-     * The messenger send now method is also verified to see if sending immediately is requested.
412
-     * otherwise its just saved to the queue.
413
-     *
414
-     * @param EE_Message_To_Generate $message_to_generate
415
-     * @return bool true or false for success.
416
-     * @throws EE_Error
417
-     * @throws EE_Error
418
-     */
419
-    public function queue_for_sending(EE_Message_To_Generate $message_to_generate)
420
-    {
421
-        if (! $message_to_generate->valid()) {
422
-            return false;
423
-        }
424
-        $this->_init_queue_and_generator();
425
-        $message = $message_to_generate->get_EE_Message();
426
-        $this->_queue->add($message);
427
-        if ($message->send_now()) {
428
-            $this->_queue->execute(false);
429
-        } else {
430
-            $this->_queue->save();
431
-        }
432
-        return true;
433
-    }
434
-
435
-
436
-    /**
437
-     * This generates and sends from the given EE_Message_To_Generate class immediately.
438
-     * @param EE_Message_To_Generate $message_to_generate
439
-     * @return EE_Messages_Queue | null
440
-     */
441
-    public function generate_and_send_now(EE_Message_To_Generate $message_to_generate)
442
-    {
443
-        if (! $message_to_generate->valid()) {
444
-            return null;
445
-        }
446
-        // is there supposed to be a sending messenger for this message?
447
-        if ($message_to_generate instanceof EEI_Has_Sending_Messenger) {
448
-            // make sure it's valid, but if it's not,
449
-            // then set the value of $sending_messenger to an EE_Error object
450
-            // so that downstream code can easily see that things went wrong.
451
-            $sending_messenger = $message_to_generate->sending_messenger() instanceof EE_messenger
452
-                ? $message_to_generate->sending_messenger()
453
-                : new EE_Error(
454
-                    esc_html__(
455
-                        'There was a specific sending messenger requested for the send action, but it was either invalid or not active at time of sending.',
456
-                        'event_espresso'
457
-                    )
458
-                );
459
-        } else {
460
-            $sending_messenger = null;
461
-        }
462
-
463
-        if ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_idle) {
464
-            $this->_init_queue_and_generator();
465
-            $this->_queue->add($message_to_generate->get_EE_Message());
466
-            $this->_queue->execute(false, $sending_messenger);
467
-            return $this->_queue;
468
-        } elseif ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_incomplete) {
469
-            $generated_queue = $this->generate_and_return(array( $message_to_generate ));
470
-            $generated_queue->execute(false, $sending_messenger);
471
-            return $generated_queue;
472
-        }
473
-        return null;
474
-    }
475
-
476
-
477
-
478
-
479
-    /**
480
-     * Creates mtg objects for all active messengers and queues for generation.
481
-     * This method also calls the execute by priority method on the queue which will optionally kick off a new non-blocking
482
-     * request to complete the action if the priority for the message requires immediate action.
483
-     * @param string $message_type
484
-     * @param mixed  $data   The data being used for generation.
485
-     * @param bool   $persist   Whether to persist the queued messages to the db or not.
486
-     */
487
-    public function generate_for_all_active_messengers($message_type, $data, $persist = true)
488
-    {
489
-        $messages_to_generate = $this->setup_mtgs_for_all_active_messengers($message_type, $data);
490
-        if ($persist) {
491
-            $this->batch_queue_for_generation_and_persist($messages_to_generate);
492
-            $this->_queue->initiate_request_by_priority();
493
-        } else {
494
-            $this->batch_queue_for_generation_no_persist($messages_to_generate);
495
-        }
496
-    }
497
-
498
-
499
-
500
-
501
-    /**
502
-     * This simply loops through all active messengers and takes care of setting up the
503
-     * EE_Message_To_Generate objects.
504
-     * @param $message_type
505
-     * @param $data
506
-     *
507
-     * @return EE_Message_To_Generate[]
508
-     */
509
-    public function setup_mtgs_for_all_active_messengers($message_type, $data)
510
-    {
511
-        $messages_to_generate = array();
512
-        foreach ($this->_message_resource_manager->active_messengers() as $messenger_slug => $messenger_object) {
513
-            $message_to_generate = new EE_Message_To_Generate($messenger_slug, $message_type, $data);
514
-            if ($message_to_generate->valid()) {
515
-                $messages_to_generate[] = $message_to_generate;
516
-            }
517
-        }
518
-        return $messages_to_generate;
519
-    }
520
-
521
-
522
-    /**
523
-     * This accepts an array of EE_Message::MSG_ID values and will use that to retrieve the objects from the database
524
-     * and send.
525
-     *
526
-     * @param array $message_ids
527
-     * @throws EE_Error
528
-     * @throws EE_Error
529
-     */
530
-    public function setup_messages_from_ids_and_send($message_ids)
531
-    {
532
-        $this->_init_queue_and_generator();
533
-        $messages = EEM_Message::instance()->get_all(array(
534
-            array(
535
-                'MSG_ID' => array( 'IN', $message_ids ),
536
-                'STS_ID' => array(
537
-                    'IN',
538
-                    array_merge(
539
-                        EEM_Message::instance()->stati_indicating_sent(),
540
-                        array( EEM_Message::status_retry )
541
-                    ),
542
-                ),
543
-            ),
544
-        ));
545
-        // set the Messages to resend.
546
-        foreach ($messages as $message) {
547
-            if ($message instanceof EE_Message) {
548
-                $message->set_STS_ID(EEM_Message::status_resend);
549
-                $this->_queue->add($message);
550
-            }
551
-        }
552
-
553
-        $this->_queue->initiate_request_by_priority('send');
554
-    }
555
-
556
-
557
-    /**
558
-     * This method checks for registration IDs in the request via the given key and creates the messages to generate
559
-     * objects from them, then returns the array of messages to generate objects.
560
-     * Note, this sets up registrations for the registration family of message types.
561
-     *
562
-     * @param string $registration_ids_key This is used to indicate what represents the registration ids in the request.
563
-     *
564
-     * @return EE_Message_To_Generate[]|bool
565
-     * @throws EE_Error
566
-     */
567
-    public function setup_messages_to_generate_from_registration_ids_in_request($registration_ids_key = '_REG_ID')
568
-    {
569
-        /** @var RequestInterface $request */
570
-        $request = LoaderFactory::getLoader()->getShared(RequestInterface::class);
571
-        $regs_to_send = array();
572
-        $regIDs = $request->getRequestParam($registration_ids_key, [], 'arrayOf|int');
573
-        if (empty($regIDs)) {
574
-            EE_Error::add_error(esc_html__('Something went wrong because we\'re missing the registration ID', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
575
-            return false;
576
-        }
577
-
578
-        // make sure is an array
579
-        $regIDs = is_array($regIDs) ? $regIDs : array( $regIDs );
580
-
581
-        foreach ($regIDs as $regID) {
582
-            $reg = EEM_Registration::instance()->get_one_by_ID($regID);
583
-            if (! $reg instanceof EE_Registration) {
584
-                EE_Error::add_error(sprintf(esc_html__('Unable to retrieve a registration object for the given reg id (%s)', 'event_espresso'), $regID));
585
-                return false;
586
-            }
587
-            $regs_to_send[ $reg->transaction_ID() ][ $reg->status_ID() ][] = $reg;
588
-        }
589
-
590
-        $messages_to_generate = array();
591
-
592
-        foreach ($regs_to_send as $status_group) {
593
-            foreach ($status_group as $status_id => $registrations) {
594
-                $message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id);
595
-                if (! $message_type) {
596
-                    continue;
597
-                }
598
-                $messages_to_generate = array_merge(
599
-                    $messages_to_generate,
600
-                    $this->setup_mtgs_for_all_active_messengers(
601
-                        $message_type,
602
-                        array( $registrations, $status_id )
603
-                    )
604
-                );
605
-            }
606
-        }
607
-
608
-        return $messages_to_generate;
609
-    }
278
+	/**
279
+	 * Queue for generation.  Note this does NOT persist to the db.  Client code should call get_message_repository()->save() if desire
280
+	 * to persist.  This method is provided to client code to decide what it wants to do with queued messages for generation.
281
+	 * @param EE_Message_To_Generate $message_to_generate
282
+	 * @param bool                   $test_send             Whether this item is for a test send or not.
283
+	 * @return  EE_Messages_Queue
284
+	 */
285
+	public function queue_for_generation(EE_Message_To_Generate $message_to_generate, $test_send = false)
286
+	{
287
+		if ($message_to_generate->valid()) {
288
+			$this->_generator->create_and_add_message_to_queue($message_to_generate, $test_send);
289
+		}
290
+	}
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+	/**
299
+	 * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message adds them to the generation queue
300
+	 * and then persists to storage.
301
+	 *
302
+	 * @param EE_Message_To_Generate[] $messages_to_generate
303
+	 */
304
+	public function batch_queue_for_generation_and_persist($messages_to_generate)
305
+	{
306
+		$this->_init_queue_and_generator();
307
+		$this->_queue_for_generation_loop($messages_to_generate);
308
+		$this->_queue->save();
309
+	}
310
+
311
+
312
+
313
+
314
+
315
+
316
+	/**
317
+	 * This receives an array of EE_Message_To_Generate objects, converts them to EE_Message and adds them to the generation
318
+	 * queue.  Does NOT persist to storage (unless there is an error.
319
+	 * Client code can retrieve the generated queue by calling EEM_Messages_Processor::get_queue()
320
+	 *
321
+	 * @param EE_Message_To_Generate[]  $messages_to_generate
322
+	 */
323
+	public function batch_queue_for_generation_no_persist($messages_to_generate)
324
+	{
325
+		$this->_init_queue_and_generator();
326
+		$this->_queue_for_generation_loop($messages_to_generate);
327
+	}
328
+
329
+
330
+
331
+
332
+	/**
333
+	 * Simply loops through the given array of EE_Message_To_Generate objects and adds them to the _queue as EE_Message
334
+	 * objects.
335
+	 *
336
+	 * @param EE_Message_To_Generate[] $messages_to_generate
337
+	 */
338
+	protected function _queue_for_generation_loop($messages_to_generate)
339
+	{
340
+		// make sure is in an array.
341
+		if (! is_array($messages_to_generate)) {
342
+			$messages_to_generate = array( $messages_to_generate );
343
+		}
344
+
345
+		foreach ($messages_to_generate as $message_to_generate) {
346
+			if ($message_to_generate instanceof EE_Message_To_Generate && $message_to_generate->valid()) {
347
+				$this->queue_for_generation($message_to_generate);
348
+			}
349
+		}
350
+	}
351
+
352
+
353
+
354
+
355
+
356
+	/**
357
+	 * Receives an array of EE_Message_To_Generate objects and generates the EE_Message objects, then persists (so its
358
+	 * queued for sending).
359
+	 * @param  EE_Message_To_Generate[]
360
+	 * @return EE_Messages_Queue
361
+	 */
362
+	public function generate_and_queue_for_sending($messages_to_generate)
363
+	{
364
+		$this->_init_queue_and_generator();
365
+		$this->_queue_for_generation_loop($messages_to_generate);
366
+		return $this->_generator->generate(true);
367
+	}
368
+
369
+
370
+
371
+
372
+
373
+	/**
374
+	 * Generate for preview and execute right away.
375
+	 *
376
+	 * @param   EE_Message_To_Generate $message_to_generate
377
+	 * @param   bool                   $test_send                Whether this is a test send or not.
378
+	 * @return  EE_Messages_Queue | bool   false if unable to generate otherwise the generated queue.
379
+	 */
380
+	public function generate_for_preview(EE_Message_To_Generate $message_to_generate, $test_send = false)
381
+	{
382
+		if (! $message_to_generate->valid()) {
383
+			EE_Error::add_error(
384
+				esc_html__('Unable to generate preview because of invalid data', 'event_espresso'),
385
+				__FILE__,
386
+				__FUNCTION__,
387
+				__LINE__
388
+			);
389
+			return false;
390
+		}
391
+		// just make sure preview is set on the $message_to_generate (in case client forgot)
392
+		$message_to_generate->set_preview(true);
393
+		$this->_init_queue_and_generator();
394
+		$this->queue_for_generation($message_to_generate, $test_send);
395
+		$generated_queue = $this->_generator->generate(false);
396
+		if ($generated_queue->execute(false)) {
397
+			// the first queue item should be the preview
398
+			$generated_queue->get_message_repository()->rewind();
399
+			if (! $generated_queue->get_message_repository()->valid()) {
400
+				return $generated_queue;
401
+			}
402
+			return $generated_queue;
403
+		} else {
404
+			return false;
405
+		}
406
+	}
407
+
408
+
409
+	/**
410
+	 * This queues for sending.
411
+	 * The messenger send now method is also verified to see if sending immediately is requested.
412
+	 * otherwise its just saved to the queue.
413
+	 *
414
+	 * @param EE_Message_To_Generate $message_to_generate
415
+	 * @return bool true or false for success.
416
+	 * @throws EE_Error
417
+	 * @throws EE_Error
418
+	 */
419
+	public function queue_for_sending(EE_Message_To_Generate $message_to_generate)
420
+	{
421
+		if (! $message_to_generate->valid()) {
422
+			return false;
423
+		}
424
+		$this->_init_queue_and_generator();
425
+		$message = $message_to_generate->get_EE_Message();
426
+		$this->_queue->add($message);
427
+		if ($message->send_now()) {
428
+			$this->_queue->execute(false);
429
+		} else {
430
+			$this->_queue->save();
431
+		}
432
+		return true;
433
+	}
434
+
435
+
436
+	/**
437
+	 * This generates and sends from the given EE_Message_To_Generate class immediately.
438
+	 * @param EE_Message_To_Generate $message_to_generate
439
+	 * @return EE_Messages_Queue | null
440
+	 */
441
+	public function generate_and_send_now(EE_Message_To_Generate $message_to_generate)
442
+	{
443
+		if (! $message_to_generate->valid()) {
444
+			return null;
445
+		}
446
+		// is there supposed to be a sending messenger for this message?
447
+		if ($message_to_generate instanceof EEI_Has_Sending_Messenger) {
448
+			// make sure it's valid, but if it's not,
449
+			// then set the value of $sending_messenger to an EE_Error object
450
+			// so that downstream code can easily see that things went wrong.
451
+			$sending_messenger = $message_to_generate->sending_messenger() instanceof EE_messenger
452
+				? $message_to_generate->sending_messenger()
453
+				: new EE_Error(
454
+					esc_html__(
455
+						'There was a specific sending messenger requested for the send action, but it was either invalid or not active at time of sending.',
456
+						'event_espresso'
457
+					)
458
+				);
459
+		} else {
460
+			$sending_messenger = null;
461
+		}
462
+
463
+		if ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_idle) {
464
+			$this->_init_queue_and_generator();
465
+			$this->_queue->add($message_to_generate->get_EE_Message());
466
+			$this->_queue->execute(false, $sending_messenger);
467
+			return $this->_queue;
468
+		} elseif ($message_to_generate->get_EE_Message()->STS_ID() === EEM_Message::status_incomplete) {
469
+			$generated_queue = $this->generate_and_return(array( $message_to_generate ));
470
+			$generated_queue->execute(false, $sending_messenger);
471
+			return $generated_queue;
472
+		}
473
+		return null;
474
+	}
475
+
476
+
477
+
478
+
479
+	/**
480
+	 * Creates mtg objects for all active messengers and queues for generation.
481
+	 * This method also calls the execute by priority method on the queue which will optionally kick off a new non-blocking
482
+	 * request to complete the action if the priority for the message requires immediate action.
483
+	 * @param string $message_type
484
+	 * @param mixed  $data   The data being used for generation.
485
+	 * @param bool   $persist   Whether to persist the queued messages to the db or not.
486
+	 */
487
+	public function generate_for_all_active_messengers($message_type, $data, $persist = true)
488
+	{
489
+		$messages_to_generate = $this->setup_mtgs_for_all_active_messengers($message_type, $data);
490
+		if ($persist) {
491
+			$this->batch_queue_for_generation_and_persist($messages_to_generate);
492
+			$this->_queue->initiate_request_by_priority();
493
+		} else {
494
+			$this->batch_queue_for_generation_no_persist($messages_to_generate);
495
+		}
496
+	}
497
+
498
+
499
+
500
+
501
+	/**
502
+	 * This simply loops through all active messengers and takes care of setting up the
503
+	 * EE_Message_To_Generate objects.
504
+	 * @param $message_type
505
+	 * @param $data
506
+	 *
507
+	 * @return EE_Message_To_Generate[]
508
+	 */
509
+	public function setup_mtgs_for_all_active_messengers($message_type, $data)
510
+	{
511
+		$messages_to_generate = array();
512
+		foreach ($this->_message_resource_manager->active_messengers() as $messenger_slug => $messenger_object) {
513
+			$message_to_generate = new EE_Message_To_Generate($messenger_slug, $message_type, $data);
514
+			if ($message_to_generate->valid()) {
515
+				$messages_to_generate[] = $message_to_generate;
516
+			}
517
+		}
518
+		return $messages_to_generate;
519
+	}
520
+
521
+
522
+	/**
523
+	 * This accepts an array of EE_Message::MSG_ID values and will use that to retrieve the objects from the database
524
+	 * and send.
525
+	 *
526
+	 * @param array $message_ids
527
+	 * @throws EE_Error
528
+	 * @throws EE_Error
529
+	 */
530
+	public function setup_messages_from_ids_and_send($message_ids)
531
+	{
532
+		$this->_init_queue_and_generator();
533
+		$messages = EEM_Message::instance()->get_all(array(
534
+			array(
535
+				'MSG_ID' => array( 'IN', $message_ids ),
536
+				'STS_ID' => array(
537
+					'IN',
538
+					array_merge(
539
+						EEM_Message::instance()->stati_indicating_sent(),
540
+						array( EEM_Message::status_retry )
541
+					),
542
+				),
543
+			),
544
+		));
545
+		// set the Messages to resend.
546
+		foreach ($messages as $message) {
547
+			if ($message instanceof EE_Message) {
548
+				$message->set_STS_ID(EEM_Message::status_resend);
549
+				$this->_queue->add($message);
550
+			}
551
+		}
552
+
553
+		$this->_queue->initiate_request_by_priority('send');
554
+	}
555
+
556
+
557
+	/**
558
+	 * This method checks for registration IDs in the request via the given key and creates the messages to generate
559
+	 * objects from them, then returns the array of messages to generate objects.
560
+	 * Note, this sets up registrations for the registration family of message types.
561
+	 *
562
+	 * @param string $registration_ids_key This is used to indicate what represents the registration ids in the request.
563
+	 *
564
+	 * @return EE_Message_To_Generate[]|bool
565
+	 * @throws EE_Error
566
+	 */
567
+	public function setup_messages_to_generate_from_registration_ids_in_request($registration_ids_key = '_REG_ID')
568
+	{
569
+		/** @var RequestInterface $request */
570
+		$request = LoaderFactory::getLoader()->getShared(RequestInterface::class);
571
+		$regs_to_send = array();
572
+		$regIDs = $request->getRequestParam($registration_ids_key, [], 'arrayOf|int');
573
+		if (empty($regIDs)) {
574
+			EE_Error::add_error(esc_html__('Something went wrong because we\'re missing the registration ID', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
575
+			return false;
576
+		}
577
+
578
+		// make sure is an array
579
+		$regIDs = is_array($regIDs) ? $regIDs : array( $regIDs );
580
+
581
+		foreach ($regIDs as $regID) {
582
+			$reg = EEM_Registration::instance()->get_one_by_ID($regID);
583
+			if (! $reg instanceof EE_Registration) {
584
+				EE_Error::add_error(sprintf(esc_html__('Unable to retrieve a registration object for the given reg id (%s)', 'event_espresso'), $regID));
585
+				return false;
586
+			}
587
+			$regs_to_send[ $reg->transaction_ID() ][ $reg->status_ID() ][] = $reg;
588
+		}
589
+
590
+		$messages_to_generate = array();
591
+
592
+		foreach ($regs_to_send as $status_group) {
593
+			foreach ($status_group as $status_id => $registrations) {
594
+				$message_type = EEH_MSG_Template::convert_reg_status_to_message_type($status_id);
595
+				if (! $message_type) {
596
+					continue;
597
+				}
598
+				$messages_to_generate = array_merge(
599
+					$messages_to_generate,
600
+					$this->setup_mtgs_for_all_active_messengers(
601
+						$message_type,
602
+						array( $registrations, $status_id )
603
+					)
604
+				);
605
+			}
606
+		}
607
+
608
+		return $messages_to_generate;
609
+	}
610 610
 }
Please login to merge, or discard this patch.
core/domain/services/admin/ajax/WordpressHeartbeat.php 1 patch
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -17,52 +17,52 @@
 block discarded – undo
17 17
 class WordpressHeartbeat
18 18
 {
19 19
 
20
-    const RESPONSE_KEY_THANK_YOU_PAGE = 'espresso_thank_you_page';
20
+	const RESPONSE_KEY_THANK_YOU_PAGE = 'espresso_thank_you_page';
21 21
 
22
-    /**
23
-     * @var LoaderInterface $loader
24
-     */
25
-    protected $loader;
22
+	/**
23
+	 * @var LoaderInterface $loader
24
+	 */
25
+	protected $loader;
26 26
 
27
-    /**
28
-     * @var RequestInterface $request
29
-     */
30
-    protected $request;
27
+	/**
28
+	 * @var RequestInterface $request
29
+	 */
30
+	protected $request;
31 31
 
32 32
 
33
-    /**
34
-     * WordpressHeartbeat constructor.
35
-     *
36
-     * @param LoaderInterface  $loader
37
-     * @param RequestInterface $request
38
-     */
39
-    public function __construct(
40
-        LoaderInterface $loader,
41
-        RequestInterface $request
42
-    ) {
43
-        $this->loader = $loader;
44
-        $this->request = $request;
45
-        do_action('AHEE__EventEspresso_core_domain_services_admin_ajax_WordpressHeartbeat__constructor', $this);
46
-        add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'resolveRoutes'));
47
-    }
33
+	/**
34
+	 * WordpressHeartbeat constructor.
35
+	 *
36
+	 * @param LoaderInterface  $loader
37
+	 * @param RequestInterface $request
38
+	 */
39
+	public function __construct(
40
+		LoaderInterface $loader,
41
+		RequestInterface $request
42
+	) {
43
+		$this->loader = $loader;
44
+		$this->request = $request;
45
+		do_action('AHEE__EventEspresso_core_domain_services_admin_ajax_WordpressHeartbeat__constructor', $this);
46
+		add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'resolveRoutes'));
47
+	}
48 48
 
49 49
 
50
-    /**
51
-     * @since 4.9.76.p
52
-     * @throws InvalidClassException
53
-     */
54
-    public function resolveRoutes()
55
-    {
56
-        $screenID = $this->request->getRequestParam('screen_id');
57
-        $heartbeat_data = $this->request->getRequestParam('data', [], 'arrayOf|string');
58
-        if ($screenID === 'espresso_events') {
59
-            $this->loader->getShared(
60
-                'EventEspresso\core\domain\services\admin\ajax\EventEditorHeartbeat'
61
-            );
62
-        } elseif ($screenID === 'front' && ! empty($heartbeat_data[ self::RESPONSE_KEY_THANK_YOU_PAGE ])) {
63
-            $this->loader->getShared(
64
-                'EventEspresso\core\domain\services\admin\ajax\ThankYouPageIpnMonitor'
65
-            );
66
-        }
67
-    }
50
+	/**
51
+	 * @since 4.9.76.p
52
+	 * @throws InvalidClassException
53
+	 */
54
+	public function resolveRoutes()
55
+	{
56
+		$screenID = $this->request->getRequestParam('screen_id');
57
+		$heartbeat_data = $this->request->getRequestParam('data', [], 'arrayOf|string');
58
+		if ($screenID === 'espresso_events') {
59
+			$this->loader->getShared(
60
+				'EventEspresso\core\domain\services\admin\ajax\EventEditorHeartbeat'
61
+			);
62
+		} elseif ($screenID === 'front' && ! empty($heartbeat_data[ self::RESPONSE_KEY_THANK_YOU_PAGE ])) {
63
+			$this->loader->getShared(
64
+				'EventEspresso\core\domain\services\admin\ajax\ThankYouPageIpnMonitor'
65
+			);
66
+		}
67
+	}
68 68
 }
Please login to merge, or discard this patch.