1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Subscriptions Module |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2018 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Subscriptions |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Subscriptions; |
12
|
|
|
|
13
|
|
|
use DateInterval; |
14
|
|
|
use DatePeriod; |
15
|
|
|
use Pronamic\WordPress\Pay\Plugin; |
16
|
|
|
use Pronamic\WordPress\Pay\Core\Gateway; |
17
|
|
|
use Pronamic\WordPress\Pay\Core\Statuses; |
18
|
|
|
use Pronamic\WordPress\Pay\Payments\Payment; |
19
|
|
|
use WP_CLI; |
|
|
|
|
20
|
|
|
use WP_Query; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Title: Subscriptions module |
24
|
|
|
* Description: |
25
|
|
|
* Copyright: Copyright (c) 2005 - 2018 |
26
|
|
|
* Company: Pronamic |
27
|
|
|
* |
28
|
|
|
* @see https://woocommerce.com/2017/04/woocommerce-3-0-release/ |
29
|
|
|
* @see https://woocommerce.wordpress.com/2016/10/27/the-new-crud-classes-in-woocommerce-2-7/ |
30
|
|
|
* @author Remco Tolsma |
31
|
|
|
* @version 3.7.0 |
32
|
|
|
* @since 3.7.0 |
33
|
|
|
*/ |
34
|
|
|
class SubscriptionsModule { |
35
|
|
|
/** |
36
|
|
|
* Plugin. |
37
|
|
|
* |
38
|
|
|
* @var Plugin $plugin |
39
|
|
|
*/ |
40
|
|
|
public $plugin; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Construct and initialize a subscriptions module object. |
44
|
|
|
* |
45
|
|
|
* @param Plugin $plugin The plugin. |
46
|
|
|
*/ |
47
|
|
|
public function __construct( Plugin $plugin ) { |
48
|
|
|
$this->plugin = $plugin; |
49
|
|
|
|
50
|
|
|
// Actions. |
51
|
|
|
add_action( 'wp_loaded', array( $this, 'handle_subscription' ) ); |
52
|
|
|
|
53
|
|
|
add_action( 'plugins_loaded', array( $this, 'maybe_schedule_subscription_payments' ), 5 ); |
54
|
|
|
|
55
|
|
|
// Exclude payment and subscription notes. |
56
|
|
|
add_filter( 'comments_clauses', array( $this, 'exclude_subscription_comment_notes' ), 10, 2 ); |
57
|
|
|
|
58
|
|
|
add_action( 'pronamic_pay_new_payment', array( $this, 'maybe_create_subscription' ) ); |
59
|
|
|
|
60
|
|
|
// The 'pronamic_pay_update_subscription_payments' hook adds subscription payments and sends renewal notices. |
61
|
|
|
add_action( 'pronamic_pay_update_subscription_payments', array( $this, 'update_subscription_payments' ) ); |
62
|
|
|
|
63
|
|
|
// Listen to payment status changes so we can update related subscriptions. |
64
|
|
|
add_action( 'pronamic_payment_status_update', array( $this, 'payment_status_update' ) ); |
65
|
|
|
|
66
|
|
|
// Listen to subscription status changes so we can log these in a note. |
67
|
|
|
add_action( 'pronamic_subscription_status_update', array( $this, 'log_subscription_status_update' ), 10, 4 ); |
68
|
|
|
|
69
|
|
|
// WordPress CLI. |
70
|
|
|
// @see https://github.com/woocommerce/woocommerce/blob/3.3.1/includes/class-woocommerce.php#L365-L369. |
71
|
|
|
// @see https://github.com/woocommerce/woocommerce/blob/3.3.1/includes/class-wc-cli.php. |
72
|
|
|
// @see https://make.wordpress.org/cli/handbook/commands-cookbook/. |
73
|
|
|
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
|
|
|
74
|
|
|
WP_CLI::add_command( 'pay subscriptions test', array( $this, 'cli_subscriptions_test' ) ); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Handle subscription actions. |
80
|
|
|
* |
81
|
|
|
* Extensions like Gravity Forms can send action links in for example |
82
|
|
|
* email notifications so users can cancel or renew their subscription. |
83
|
|
|
*/ |
84
|
|
|
public function handle_subscription() { |
85
|
|
|
if ( ! filter_has_var( INPUT_GET, 'subscription' ) ) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ( ! filter_has_var( INPUT_GET, 'action' ) ) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ( ! filter_has_var( INPUT_GET, 'key' ) ) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$subscription_id = filter_input( INPUT_GET, 'subscription', FILTER_SANITIZE_STRING ); |
98
|
|
|
$subscription = get_pronamic_subscription( $subscription_id ); |
99
|
|
|
|
100
|
|
|
$action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING ); |
101
|
|
|
|
102
|
|
|
$key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_STRING ); |
103
|
|
|
|
104
|
|
|
// Check if subscription is valid. |
105
|
|
|
if ( ! $subscription ) { |
|
|
|
|
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Check if subscription key is valid. |
110
|
|
|
if ( $key !== $subscription->get_key() ) { |
111
|
|
|
wp_redirect( home_url() ); |
112
|
|
|
|
113
|
|
|
exit; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Check if we should redirect. |
117
|
|
|
$should_redirect = true; |
118
|
|
|
|
119
|
|
|
switch ( $action ) { |
120
|
|
|
case 'cancel': |
121
|
|
|
if ( Statuses::CANCELLED !== $subscription->get_status() ) { |
122
|
|
|
$subscription->set_status( Statuses::CANCELLED ); |
123
|
|
|
|
124
|
|
|
$this->update_subscription( $subscription, $should_redirect ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
break; |
128
|
|
|
case 'renew': |
129
|
|
|
$gateway = Plugin::get_gateway( $subscription->config_id ); |
130
|
|
|
|
131
|
|
|
if ( Statuses::SUCCESS !== $subscription->get_status() ) { |
132
|
|
|
$payment = $this->start_recurring( $subscription, $gateway, true ); |
133
|
|
|
|
134
|
|
|
if ( ! $gateway->has_error() ) { |
135
|
|
|
// Redirect. |
136
|
|
|
$gateway->redirect( $payment ); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
wp_redirect( home_url() ); |
141
|
|
|
|
142
|
|
|
exit; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Start a recurring payment at the specified gateway for the specified subscription. |
148
|
|
|
* |
149
|
|
|
* @param Subscription $subscription The subscription to start a recurring payment for. |
150
|
|
|
* @param Gateway $gateway The gateway to start the recurring payment at. |
151
|
|
|
* @param boolean $renewal Flag for renewal payment. |
152
|
|
|
*/ |
153
|
|
|
public function start_recurring( Subscription $subscription, Gateway $gateway, $renewal = false ) { |
154
|
|
|
// If next payment date is after the subscription end date unset the next payment date. |
155
|
|
|
if ( isset( $subscription->end_date, $subscription->next_payment ) && $subscription->end_date <= $subscription->next_payment ) { |
156
|
|
|
$subscription->next_payment = null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// If there is no next payment date change the subscription status to completed. |
160
|
|
|
if ( empty( $subscription->next_payment ) ) { |
161
|
|
|
$subscription->status = Statuses::COMPLETED; |
162
|
|
|
$subscription->expiry_date = $subscription->end_date; |
163
|
|
|
|
164
|
|
|
$result = $this->plugin->subscriptions_data_store->update( $subscription ); |
|
|
|
|
165
|
|
|
|
166
|
|
|
// @todo |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Calculate payment start and end dates. |
171
|
|
|
$start_date = clone $subscription->next_payment; |
172
|
|
|
|
173
|
|
|
$end_date = clone $start_date; |
174
|
|
|
$end_date->add( $subscription->get_date_interval() ); |
175
|
|
|
|
176
|
|
|
$subscription->next_payment = $end_date; |
177
|
|
|
|
178
|
|
|
// Create follow up payment. |
179
|
|
|
$payment = new Payment(); |
180
|
|
|
|
181
|
|
|
$payment->config_id = $subscription->config_id; |
182
|
|
|
$payment->user_id = $subscription->user_id; |
|
|
|
|
183
|
|
|
$payment->source = $subscription->source; |
184
|
|
|
$payment->source_id = $subscription->source_id; |
185
|
|
|
$payment->description = $subscription->description; |
186
|
|
|
$payment->order_id = $subscription->order_id; |
187
|
|
|
$payment->currency = $subscription->currency; |
188
|
|
|
$payment->email = $subscription->email; |
189
|
|
|
$payment->customer_name = $subscription->customer_name; |
190
|
|
|
$payment->address = $subscription->address; |
191
|
|
|
$payment->address = $subscription->address; |
192
|
|
|
$payment->city = $subscription->city; |
193
|
|
|
$payment->zip = $subscription->zip; |
194
|
|
|
$payment->country = $subscription->country; |
195
|
|
|
$payment->telephone_number = $subscription->telephone_number; |
196
|
|
|
$payment->method = $subscription->payment_method; |
197
|
|
|
$payment->subscription = $subscription; |
198
|
|
|
$payment->subscription_id = $subscription->get_id(); |
199
|
|
|
$payment->amount = $subscription->amount; |
200
|
|
|
$payment->start_date = $start_date; |
201
|
|
|
$payment->end_date = $end_date; |
202
|
|
|
$payment->recurring_type = 'recurring'; |
203
|
|
|
$payment->recurring = ! $renewal; |
204
|
|
|
|
205
|
|
|
// Start payment. |
206
|
|
|
$payment = Plugin::start_payment( $payment, $gateway ); |
207
|
|
|
|
208
|
|
|
// Update subscription. |
209
|
|
|
$result = $this->plugin->subscriptions_data_store->update( $subscription ); |
|
|
|
|
210
|
|
|
|
211
|
|
|
return $payment; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Update the specified subscription and redirect if allowed. |
216
|
|
|
* |
217
|
|
|
* @param Subscription $subscription The updated subscription. |
218
|
|
|
* @param boolean $can_redirect Flag to redirect or not. |
219
|
|
|
*/ |
220
|
|
|
public function update_subscription( $subscription = null, $can_redirect = true ) { |
221
|
|
|
if ( empty( $subscription ) ) { |
222
|
|
|
return; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$this->plugin->subscriptions_data_store->update( $subscription ); |
226
|
|
|
|
227
|
|
|
if ( defined( 'DOING_CRON' ) && empty( $subscription->status ) ) { |
228
|
|
|
$can_redirect = false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ( $can_redirect ) { |
232
|
|
|
wp_redirect( home_url() ); |
233
|
|
|
|
234
|
|
|
exit; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Comments clauses. |
240
|
|
|
* |
241
|
|
|
* @param array $clauses The database query clauses. |
242
|
|
|
* @param WP_Comment_Query $query The WordPress comment query object. |
|
|
|
|
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
|
|
public function exclude_subscription_comment_notes( $clauses, $query ) { |
246
|
|
|
$type = $query->query_vars['type']; |
247
|
|
|
|
248
|
|
|
// Ignore subscription notes comments if it's not specifically requested. |
249
|
|
|
if ( 'subscription_note' !== $type ) { |
250
|
|
|
$clauses['where'] .= " AND comment_type != 'subscription_note'"; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $clauses; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Maybe schedule subscription payments. |
258
|
|
|
*/ |
259
|
|
|
public function maybe_schedule_subscription_payments() { |
260
|
|
|
if ( wp_next_scheduled( 'pronamic_pay_update_subscription_payments' ) ) { |
261
|
|
|
return; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
wp_schedule_event( time(), 'hourly', 'pronamic_pay_update_subscription_payments' ); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Maybe create subscription for the specified payment. |
269
|
|
|
* |
270
|
|
|
* @param Payment $payment The new payment. |
271
|
|
|
*/ |
272
|
|
|
public function maybe_create_subscription( $payment ) { |
273
|
|
|
// Check if there is already subscription attached to the payment. |
274
|
|
|
$subscription_id = $payment->get_subscription_id(); |
275
|
|
|
|
276
|
|
|
if ( ! empty( $subscription_id ) ) { |
277
|
|
|
// Subscription already created. |
278
|
|
|
return; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
// Check if there is a subscription object attached to the payment. |
282
|
|
|
$subscription_data = $payment->subscription; |
283
|
|
|
|
284
|
|
|
if ( empty( $subscription_data ) ) { |
285
|
|
|
return; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
// New subscription. |
289
|
|
|
$subscription = new Subscription(); |
290
|
|
|
|
291
|
|
|
$subscription->config_id = $payment->config_id; |
292
|
|
|
$subscription->user_id = $payment->user_id; |
|
|
|
|
293
|
|
|
$subscription->title = sprintf( __( 'Subscription for %s', 'pronamic_ideal' ), $payment->title ); |
|
|
|
|
294
|
|
|
$subscription->frequency = $subscription_data->get_frequency(); |
295
|
|
|
$subscription->interval = $subscription_data->get_interval(); |
296
|
|
|
$subscription->interval_period = $subscription_data->get_interval_period(); |
297
|
|
|
$subscription->currency = $subscription_data->get_currency(); |
298
|
|
|
$subscription->amount = $subscription_data->get_amount(); |
299
|
|
|
$subscription->key = uniqid( 'subscr_' ); |
300
|
|
|
$subscription->source = $payment->source; |
301
|
|
|
$subscription->source_id = $payment->source_id; |
302
|
|
|
$subscription->description = $payment->description; |
303
|
|
|
$subscription->email = $payment->email; |
304
|
|
|
$subscription->customer_name = $payment->customer_name; |
305
|
|
|
$subscription->payment_method = $payment->method; |
306
|
|
|
$subscription->first_payment = $payment->date; |
|
|
|
|
307
|
|
|
$subscription->status = Statuses::OPEN; |
308
|
|
|
|
309
|
|
|
// @todo |
310
|
|
|
// Calculate dates |
311
|
|
|
// @see https://github.com/pronamic/wp-pronamic-ideal/blob/4.7.0/classes/Pronamic/WP/Pay/Plugin.php#L883-L964 |
312
|
|
|
$interval = $subscription->get_date_interval(); |
313
|
|
|
|
314
|
|
|
$start_date = clone $payment->date; |
315
|
|
|
$expiry_date = clone $start_date; |
316
|
|
|
|
317
|
|
|
$next_date = clone $start_date; |
318
|
|
|
$next_date->add( $interval ); |
319
|
|
|
|
320
|
|
|
$end_date = null; |
321
|
|
|
|
322
|
|
|
if ( $subscription_data->frequency ) { |
323
|
|
|
// @see https://stackoverflow.com/a/10818981/6411283 |
324
|
|
|
$period = new DatePeriod( $start_date, $interval, $subscription_data->frequency ); |
325
|
|
|
|
326
|
|
|
$dates = iterator_to_array( $period ); |
327
|
|
|
|
328
|
|
|
$end_date = end( $dates ); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$subscription->start_date = $start_date; |
332
|
|
|
$subscription->end_date = $end_date; |
333
|
|
|
$subscription->expiry_date = $expiry_date; |
334
|
|
|
$subscription->next_payment = $next_date; |
335
|
|
|
|
336
|
|
|
// Create. |
337
|
|
|
$result = $this->plugin->subscriptions_data_store->create( $subscription ); |
338
|
|
|
|
339
|
|
|
if ( $result ) { |
340
|
|
|
$payment->subscription_id = $subscription->get_id(); |
341
|
|
|
|
342
|
|
|
$payment->recurring_type = 'first'; |
343
|
|
|
$payment->start_date = $start_date; |
344
|
|
|
$payment->end_date = $next_date; |
345
|
|
|
|
346
|
|
|
$this->plugin->payments_data_store->update( $payment ); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get expiring subscriptions. |
352
|
|
|
* |
353
|
|
|
* @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L715-L746 |
354
|
|
|
* @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L652-L712 |
355
|
|
|
* |
356
|
|
|
* @param DateTime $start_date The start date of the period to check for expiring subscriptions. |
357
|
|
|
* @param DateTime $end_date The end date of the period to check for expiring subscriptions. |
358
|
|
|
* @return array |
359
|
|
|
*/ |
360
|
|
|
public function get_expiring_subscription_posts( DateTime $start_date, DateTime $end_date ) { |
|
|
|
|
361
|
|
|
$args = array( |
362
|
|
|
'post_type' => 'pronamic_pay_subscr', |
363
|
|
|
'nopaging' => true, |
364
|
|
|
'orderby' => 'post_date', |
365
|
|
|
'order' => 'ASC', |
366
|
|
|
'post_status' => array( |
367
|
|
|
'subscr_pending', |
368
|
|
|
'subscr_expired', |
369
|
|
|
'subscr_failed', |
370
|
|
|
'subscr_active', |
371
|
|
|
), |
372
|
|
|
'meta_query' => array( |
373
|
|
|
array( |
374
|
|
|
'key' => '_pronamic_subscription_expiry_date', |
375
|
|
|
'value' => array( |
376
|
|
|
$start_date->format( DateTime::MYSQL ), |
377
|
|
|
$end_date->format( DateTime::MYSQL ), |
378
|
|
|
), |
379
|
|
|
'compare' => 'BETWEEN', |
380
|
|
|
'type' => 'DATETIME', |
381
|
|
|
), |
382
|
|
|
), |
383
|
|
|
); |
384
|
|
|
|
385
|
|
|
$query = new WP_Query( $args ); |
386
|
|
|
|
387
|
|
|
return $query->posts; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Update subscription payments. |
392
|
|
|
*/ |
393
|
|
|
public function update_subscription_payments() { |
394
|
|
|
$this->send_subscription_renewal_notices(); |
395
|
|
|
|
396
|
|
|
// Don't create payments for sources which schedule payments. |
397
|
|
|
$sources = array( |
398
|
|
|
'woocommerce', |
399
|
|
|
); |
400
|
|
|
|
401
|
|
|
$args = array( |
402
|
|
|
'post_type' => 'pronamic_pay_subscr', |
403
|
|
|
'nopaging' => true, |
404
|
|
|
'orderby' => 'post_date', |
405
|
|
|
'order' => 'ASC', |
406
|
|
|
'post_status' => array( |
407
|
|
|
'subscr_pending', |
408
|
|
|
'subscr_expired', |
409
|
|
|
'subscr_failed', |
410
|
|
|
'subscr_active', |
411
|
|
|
), |
412
|
|
|
'meta_query' => array( |
413
|
|
|
array( |
414
|
|
|
'key' => '_pronamic_subscription_source', |
415
|
|
|
'compare' => 'NOT IN', |
416
|
|
|
'value' => $sources, |
417
|
|
|
), |
418
|
|
|
array( |
419
|
|
|
'key' => '_pronamic_subscription_next_payment', |
420
|
|
|
'compare' => '<=', |
421
|
|
|
'value' => current_time( 'mysql', true ), |
422
|
|
|
'type' => 'DATETIME', |
423
|
|
|
), |
424
|
|
|
), |
425
|
|
|
); |
426
|
|
|
|
427
|
|
|
$query = new WP_Query( $args ); |
428
|
|
|
|
429
|
|
|
foreach ( $query->posts as $post ) { |
430
|
|
|
$subscription = new Subscription( $post->ID ); |
431
|
|
|
|
432
|
|
|
$gateway = Plugin::get_gateway( $subscription->config_id ); |
433
|
|
|
|
434
|
|
|
$payment = $this->start_recurring( $subscription, $gateway ); |
435
|
|
|
|
436
|
|
|
if ( $payment ) { |
437
|
|
|
Plugin::update_payment( $payment, false ); |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Send renewal notices. |
444
|
|
|
* |
445
|
|
|
* @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L652-L712 |
446
|
|
|
* @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/license-renewals.php#L715-L746 |
447
|
|
|
* @see https://github.com/wp-premium/edd-software-licensing/blob/3.5.23/includes/classes/class-sl-emails.php#L41-L126 |
448
|
|
|
*/ |
449
|
|
|
public function send_subscription_renewal_notices() { |
450
|
|
|
$interval = new DateInterval( 'P1W' ); // 1 week |
451
|
|
|
|
452
|
|
|
$start_date = new DateTime( 'midnight', new DateTimeZone( 'UTC' ) ); |
|
|
|
|
453
|
|
|
|
454
|
|
|
$end_date = clone $start_date; |
455
|
|
|
$end_date->add( $interval ); |
456
|
|
|
|
457
|
|
|
$expiring_subscription_posts = $this->get_expiring_subscription_posts( $start_date, $end_date ); |
458
|
|
|
|
459
|
|
|
foreach ( $expiring_subscription_posts as $post ) { |
460
|
|
|
$subscription = new Subscription( $post->ID ); |
461
|
|
|
|
462
|
|
|
$expiry_date = $subscription->get_expiry_date(); |
463
|
|
|
|
464
|
|
|
$sent_date_string = get_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week', true ); |
465
|
|
|
|
466
|
|
|
if ( $sent_date_string ) { |
467
|
|
|
$first_date = clone $expiry_date; |
468
|
|
|
$first_date->sub( $interval ); |
469
|
|
|
|
470
|
|
|
$sent_date = new DateTime( $sent_date_string, new DateTimeZone( 'UTC' ) ); |
471
|
|
|
|
472
|
|
|
if ( $sent_date > $first_date ) { |
473
|
|
|
// Prevent renewal notices from being sent more than once. |
474
|
|
|
continue; |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
delete_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week' ); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
do_action( 'pronamic_subscription_renewal_notice_' . $subscription->get_source(), $subscription ); |
481
|
|
|
|
482
|
|
|
update_post_meta( $post->ID, '_pronamic_subscription_renewal_sent_1week', $start_date->format( DateTime::MYSQL ) ); |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Payment status update. |
488
|
|
|
* |
489
|
|
|
* @param Payment $payment The status updated payment. |
490
|
|
|
*/ |
491
|
|
|
public function payment_status_update( $payment ) { |
492
|
|
|
// Check if the payment is connected to a subscription. |
493
|
|
|
$subscription = $payment->get_subscription(); |
494
|
|
|
|
495
|
|
|
if ( empty( $subscription ) ) { |
496
|
|
|
// Payment not connected to a subscription, nothing to do. |
497
|
|
|
return; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
// Status. |
501
|
|
|
$status_before = $subscription->get_status(); |
502
|
|
|
$status_update = $status_before; |
503
|
|
|
|
504
|
|
|
switch ( $payment->get_status() ) { |
505
|
|
|
case Statuses::OPEN: |
506
|
|
|
// @todo |
507
|
|
|
break; |
508
|
|
|
case Statuses::SUCCESS: |
509
|
|
|
$status_update = Statuses::ACTIVE; |
510
|
|
|
|
511
|
|
|
if ( isset( $subscription->expiry_date, $payment->end_date ) && $subscription->expiry_date < $payment->end_date ) { |
512
|
|
|
$subscription->expiry_date = clone $payment->end_date; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
break; |
516
|
|
|
case Statuses::FAILURE: |
517
|
|
|
case Statuses::CANCELLED: |
518
|
|
|
case Statuses::EXPIRED: |
519
|
|
|
$status_update = Statuses::CANCELLED; |
520
|
|
|
|
521
|
|
|
break; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
// The status of canceled or completed subscriptions will not be changed automatically. |
525
|
|
|
if ( ! in_array( $status_before, array( Statuses::CANCELLED, Statuses::COMPLETED ), true ) ) { |
526
|
|
|
$subscription->set_status( $status_update ); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
// Update. |
530
|
|
|
$result = $this->plugin->subscriptions_data_store->update( $subscription ); |
|
|
|
|
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Subscription status update. |
535
|
|
|
* |
536
|
|
|
* @param Subscription $subscription The status updated subscirption. |
537
|
|
|
*/ |
538
|
|
|
public function log_subscription_status_update( $subscription, $can_redirect, $old_status, $new_status ) { |
539
|
|
|
$subscription->add_note( sprintf( |
540
|
|
|
__( 'Subscription status changed from "%1$s" to "%2$s".', 'pronamic_ideal' ), |
541
|
|
|
esc_html( $this->plugin->subscriptions_data_store->get_meta_status_label( $old_status ) ), |
542
|
|
|
esc_html( $this->plugin->subscriptions_data_store->get_meta_status_label( $new_status ) ) |
543
|
|
|
) ); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* CLI subscriptions test. |
548
|
|
|
*/ |
549
|
|
|
public function cli_subscriptions_test() { |
550
|
|
|
$args = array( |
551
|
|
|
'post_type' => 'pronamic_pay_subscr', |
552
|
|
|
'nopaging' => true, |
553
|
|
|
'orderby' => 'post_date', |
554
|
|
|
'order' => 'ASC', |
555
|
|
|
'post_status' => array( |
556
|
|
|
'subscr_pending', |
557
|
|
|
'subscr_expired', |
558
|
|
|
'subscr_failed', |
559
|
|
|
'subscr_active', |
560
|
|
|
), |
561
|
|
|
'meta_query' => array( |
562
|
|
|
array( |
563
|
|
|
'key' => '_pronamic_subscription_source', |
564
|
|
|
'compare' => 'NOT IN', |
565
|
|
|
'value' => array( |
566
|
|
|
// Don't create payments for sources which schedule payments. |
567
|
|
|
'woocommerce', |
568
|
|
|
), |
569
|
|
|
), |
570
|
|
|
), |
571
|
|
|
); |
572
|
|
|
|
573
|
|
|
$query = new WP_Query( $args ); |
574
|
|
|
|
575
|
|
|
foreach ( $query->posts as $post ) { |
576
|
|
|
WP_CLI::log( sprintf( 'Processing post `%d` - "%s"…', $post->ID, get_the_title( $post ) ) ); |
577
|
|
|
|
578
|
|
|
$subscription = new Subscription( $post->ID ); |
579
|
|
|
|
580
|
|
|
$gateway = Plugin::get_gateway( $subscription->config_id ); |
581
|
|
|
|
582
|
|
|
$payment = $this->start_recurring( $subscription, $gateway ); |
583
|
|
|
|
584
|
|
|
if ( $payment ) { |
585
|
|
|
Plugin::update_payment( $payment, false ); |
586
|
|
|
} |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
WP_CLI::success( 'Pronamic Pay subscriptions test.' ); |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths