-
Notifications
You must be signed in to change notification settings - Fork 3
/
SubscriptionsNotificationsController.php
315 lines (263 loc) · 7.77 KB
/
SubscriptionsNotificationsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* Subscriptions Notifications Controller
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2023 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Subscriptions
*/
namespace Pronamic\WordPress\Pay\Subscriptions;
use Pronamic\WordPress\DateTime\DateTime;
use WP_Post;
use WP_Query;
/**
* Subscriptions Notifications Controller
*/
class SubscriptionsNotificationsController {
/**
* Setup.
*
* @return void
*/
public function setup() {
\add_action( 'init', [ $this, 'maybe_schedule_actions' ] );
\add_action( 'pronamic_pay_schedule_subscriptions_notification', [ $this, 'schedule_all' ] );
\add_action( 'pronamic_pay_schedule_paged_subscriptions_notification', [ $this, 'schedule_paged' ] );
\add_action( 'pronamic_pay_send_subscription_renewal_notification', [ $this, 'action_send_subscription_renewal_notification' ] );
}
/**
* Maybe schedule actions.
*
* @link https://actionscheduler.org/
* @return void
*/
public function maybe_schedule_actions() {
if ( false === \as_next_scheduled_action( 'pronamic_pay_schedule_subscriptions_notification', [], 'pronamic-pay' ) ) {
\as_schedule_cron_action( \time(), '0 0 * * *', 'pronamic_pay_schedule_subscriptions_notification', [], 'pronamic-pay' );
}
}
/**
* Schedule all.
*
* @return void
*/
public function schedule_all() {
if ( $this->is_processing_disabled() ) {
return;
}
$query = $this->get_subscriptions_wp_query_that_require_notification();
if ( 0 === $query->max_num_pages ) {
return;
}
$pages = \range( $query->max_num_pages, 1 );
foreach ( $pages as $page ) {
$this->schedule_page( $page );
}
}
/**
* Schedule page.
*
* @param int $page Page.
* @return int
*/
private function schedule_page( $page ) {
return \as_enqueue_async_action(
'pronamic_pay_schedule_paged_subscriptions_notification',
[
'page' => $page,
],
'pronamic-pay'
);
}
/**
* Schedule paged.
*
* @param int $page Page.
* @return void
*/
public function schedule_paged( $page ) {
$query = $this->get_subscriptions_wp_query_that_require_notification(
[
'paged' => $page,
]
);
$posts = \array_filter(
$query->posts,
function( $post ) {
return ( $post instanceof WP_Post );
}
);
$subscriptions = [];
foreach ( $posts as $post ) {
$subscription = \get_pronamic_subscription( $post->ID );
if ( null !== $subscription ) {
$subscriptions[] = $subscription;
}
}
foreach ( $subscriptions as $subscription ) {
$this->schedule_subscription_notification( $subscription );
}
}
/**
* Test if the subscription meets the notification requirements.
*
* @param Subscription $subscription Subscription.
* @return bool True if meets requirements, false otherwise.
*/
private function meets_notification_requirements( Subscription $subscription ) {
/**
* If a subscription does not have a next payment date, it makes no sense to
* send a notification.
*/
$next_payment_date = $subscription->get_next_payment_date();
if ( null === $next_payment_date ) {
return false;
}
/**
* If the current date is greater than the next payment date, it no longer makes
* sense to send a notification.
*/
$date = new \DateTimeImmutable();
if ( $date > $next_payment_date ) {
return false;
}
/**
* If a notification has already been sent in the past week, it no longer makes
* sense to send a notification.
*/
$notification_date_string = $subscription->get_meta( 'notification_date_1_week' );
if ( $notification_date_string ) {
$notification_date = new DateTime( $notification_date_string );
$threshold_date = new DateTime( 'midnight -1 week' );
if ( $notification_date > $threshold_date ) {
return false;
}
}
return true;
}
/**
* Schedule subscription follow-up payment.
*
* @param Subscription $subscription Subscription.
* @return int|null
*/
private function schedule_subscription_notification( Subscription $subscription ) {
if ( ! $this->meets_notification_requirements( $subscription ) ) {
return null;
}
$action_id = $subscription->get_meta( 'send_subscription_renewal_notification_action_id' );
if ( ! empty( $action_id ) ) {
return $action_id;
}
$actions_args = [
'subscription_id' => $subscription->get_id(),
];
if ( false !== \as_next_scheduled_action( 'pronamic_pay_send_subscription_renewal_notification', $actions_args, 'pronamic-pay' ) ) {
return null;
}
$action_id = \as_enqueue_async_action(
'pronamic_pay_send_subscription_renewal_notification',
$actions_args,
'pronamic-pay'
);
$subscription->set_meta( 'send_subscription_renewal_notification_action_id', $action_id );
$subscription->save();
return $action_id;
}
/**
* Action send subscription renewal notification.
*
* @param int $subscription_id Subscription ID.
* @return void
* @throws \Exception Throws exception when unable to load subscription.
*/
public function action_send_subscription_renewal_notification( $subscription_id ) {
// Check subscription.
$subscription = \get_pronamic_subscription( (int) $subscription_id );
if ( null === $subscription ) {
throw new \Exception(
\sprintf(
'Unable to load subscription from post ID: %d.',
$subscription_id
)
);
}
$this->send_subscription_renewal_notification( $subscription );
$subscription->set_meta( 'send_subscription_renewal_notification_action_id', null );
$subscription->save();
}
/**
* Send subscription renewal notification.
*
* @param Subscription $subscription Subscription.
* @return void
* @throws \Exception Throws exception when gateway not found.
*/
public function send_subscription_renewal_notification( Subscription $subscription ) {
if ( ! $this->meets_notification_requirements( $subscription ) ) {
return;
}
$source = $subscription->get_source();
/**
* Send renewal notice for source.
*
* [`{$source}`](https://github.com/pronamic/wp-pronamic-pay/wiki#sources)
*
* @param Subscription $subscription Subscription.
*/
\do_action( 'pronamic_subscription_renewal_notice_' . $source, $subscription );
$subscription->set_meta( 'notification_date_1_week', \gmdate( DATE_ATOM ) );
}
/**
* Get WordPress query for subscriptions that require a notification.
*
* @param array $args Arguments.
* @return WP_Query
*/
private function get_subscriptions_wp_query_that_require_notification( $args = [] ) {
$start_date = new \DateTimeImmutable( 'midnight +1 week', new \DateTimeZone( 'GMT' ) );
$end_date = new \DateTimeImmutable( 'tomorrow +1 week', new \DateTimeZone( 'GMT' ) );
$query_args = [
'post_type' => 'pronamic_pay_subscr',
/**
* Posts per page is set to 100, higher could result in performance issues.
*
* @link https://github.com/WordPress/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#wp-postsperpage-post-limit
*/
'posts_per_page' => 100,
'post_status' => [
'subscr_active',
],
'meta_query' => [
[
[
'key' => '_pronamic_subscription_next_payment',
'compare' => 'BETWEEN',
'value' => [
$start_date->format( 'Y-m-d H:i:s' ),
$end_date->format( 'Y-m-d H:i:s' ),
],
'type' => 'DATETIME',
],
],
],
'order' => 'DESC',
'orderby' => 'ID',
];
if ( \array_key_exists( 'paged', $args ) ) {
$query_args['paged'] = $args['paged'];
$query_args['no_found_rows'] = true;
}
$query = new WP_Query( $query_args );
return $query;
}
/**
* Is subscriptions processing disabled.
*
* @return bool True if processing recurring payment is disabled, false otherwise.
*/
private function is_processing_disabled() {
return (bool) \get_option( 'pronamic_pay_subscriptions_processing_disabled', false );
}
}