Failed Conditions
Push — develop ( e1552d...426688 )
by Remco
06:49
created

src/Extension.php (11 issues)

1
<?php
2
/**
3
 * Extension
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Extensions\MemberPress
9
 */
10
11
namespace Pronamic\WordPress\Pay\Extensions\MemberPress;
12
13
use MeprDb;
14
use MeprOptions;
15
use MeprProduct;
16
use MeprSubscription;
17
use MeprTransaction;
18
use MeprUtils;
19
use Pronamic\WordPress\Pay\AbstractPluginIntegration;
20
use Pronamic\WordPress\Pay\Payments\PaymentStatus;
21
use Pronamic\WordPress\Pay\Extensions\MemberPress\Gateways\Gateway;
22
use Pronamic\WordPress\Pay\Payments\Payment;
23
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
24
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus;
25
26
/**
27
 * WordPress pay MemberPress extension
28
 *
29
 * @author  Remco Tolsma
30
 * @version 2.2.3
31
 * @since   1.0.0
32
 */
33
class Extension extends AbstractPluginIntegration {
34
	/**
35
	 * The slug of this addon
36
	 *
37
	 * @var string
38
	 */
39
	const SLUG = 'memberpress';
40
41
	/**
42
	 * Construct MemberPress plugin integration.
43
	 * 
44
	 * @param array $args Arguments.
45
	 */
46
	public function __construct( $args = array() ) {
47
		$args['name'] = __( 'MemberPress', 'pronamic_ideal' );
48
49
		parent::__construct( $args );
50
51
		// Dependencies.
52
		$dependencies = $this->get_dependencies();
53
54
		$dependencies->add( new MemberPressDependency() );
55
56
		// Upgrades.
57
		$upgrades = $this->get_upgrades();
58
59
		$upgrades->set_executable( true );
60
61
		$upgrades->add( new Upgrade310() );
62
	}
63
64
	/**
65
	 * Setup.
66
	 */
67
	public function setup() {
68
		\add_filter( 'pronamic_subscription_source_description_memberpress_transaction', array( $this, 'subscription_source_description' ), 10, 2 );
69
		\add_filter( 'pronamic_payment_source_description_memberpress_transaction', array( $this, 'source_description' ), 10, 2 );
70
71
		// Check if dependencies are met and integration is active.
72
		if ( ! $this->is_active() ) {
73
			return;
74
		}
75
76
		// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50
77
		\add_filter( 'mepr-gateway-paths', array( $this, 'gateway_paths' ) );
78
79
		\add_filter( 'pronamic_payment_redirect_url_memberpress_transaction', array( $this, 'redirect_url' ), 10, 2 );
80
		\add_action( 'pronamic_payment_status_update_memberpress_transaction', array( $this, 'status_update' ), 10, 1 );
81
82
		\add_action( 'pronamic_pay_new_payment', array( $this, 'maybe_create_memberpress_transaction' ), 10, 1 );
83
84
		\add_filter( 'pronamic_subscription_source_text_memberpress_subscription', array( $this, 'subscription_source_text' ), 10, 2 );
85
		\add_filter( 'pronamic_subscription_source_url_memberpress_subscription', array( $this, 'subscription_source_url' ), 10, 2 );
86
		\add_filter( 'pronamic_payment_source_text_memberpress_transaction', array( $this, 'source_text' ), 10, 2 );
87
		\add_filter( 'pronamic_payment_source_url_memberpress_transaction', array( $this, 'source_url' ), 10, 2 );
88
89
		\add_action( 'mepr_subscription_pre_delete', array( $this, 'subscription_pre_delete' ), 10, 1 );
90
91
		\add_action( 'mepr_subscription_transition_status', array( $this, 'memberpress_subscription_transition_status' ), 10, 3 );
92
93
		// MemberPress subscription email parameters.
94
		\add_filter( 'mepr_subscription_email_params', array( $this, 'subscription_email_params' ), 10, 2 );
95
		\add_filter( 'mepr_transaction_email_params', array( $this, 'transaction_email_params' ), 10, 2 );
96
97
		// Hide MemberPress columns for payments and subscriptions.
98
		\add_action( 'registered_post_type', array( $this, 'post_type_columns_hide' ), 15, 1 );
99
100
		if ( \is_admin() ) {
101
			$admin_subscriptions = new Admin\AdminSubscriptions();
102
			$admin_transactions  = new Admin\AdminTransactions();
103
104
			$admin_subscriptions->setup();
105
			$admin_transactions->setup();
106
		}
107
	}
108
109
	/**
110
	 * Gateway paths.
111
	 *
112
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprGatewayFactory.php#L49
113
	 * @param string[] $paths Array with gateway paths.
114
	 * @return string[]
115
	 */
116
	public function gateway_paths( $paths ) {
117
		$paths[] = dirname( __FILE__ ) . '/../gateways/';
118
119
		return $paths;
120
	}
121
122
	/**
123
	 * Hide MemberPress columns for payments and subscriptions.
124
	 *
125
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/controllers/MeprAppCtrl.php#L129-146
126
	 *
127
	 * @param string $post_type Registered post type.
128
	 *
129
	 * @return void
130
	 */
131
	public function post_type_columns_hide( $post_type ) {
132
		if ( ! in_array( $post_type, array( 'pronamic_payment', 'pronamic_pay_subscr' ), true ) ) {
133
			return;
134
		}
135
136
		remove_filter( 'manage_edit-' . $post_type . '_columns', 'MeprAppCtrl::columns' );
137
	}
138
139
	/**
140
	 * Payment redirect URL filter.
141
	 *
142
	 * @since 1.0.1
143
	 *
144
	 * @param string  $url     Payment redirect URL.
145
	 * @param Payment $payment Payment to redirect for.
146
	 *
147
	 * @return string
148
	 */
149
	public function redirect_url( $url, Payment $payment ) {
150
		global $transaction;
151
152
		$transaction_id = $payment->get_source_id();
153
154
		$transaction = new MeprTransaction( $transaction_id );
155
156
		switch ( $payment->get_status() ) {
157
			case PaymentStatus::CANCELLED:
158
			case PaymentStatus::EXPIRED:
159
			case PaymentStatus::FAILURE:
160
				$product = $transaction->product();
161
162
				$url = add_query_arg(
163
					array(
164
						'action'   => 'payment_form',
165
						'txn'      => $transaction->trans_num,
166
						'errors'   => array(
167
							__( 'Payment failed. Please try again.', 'pronamic_ideal' ),
168
						),
169
						'_wpnonce' => wp_create_nonce( 'mepr_payment_form' ),
170
					),
171
					$product->url()
172
				);
173
174
				break;
175
			case PaymentStatus::SUCCESS:
176
				// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782
177
				$mepr_options = MeprOptions::fetch();
178
179
				$product         = new MeprProduct( $transaction->product_id );
180
				$sanitized_title = sanitize_title( $product->post_title );
181
182
				$args = array(
183
					'membership_id' => $product->ID,
184
					'membership'    => $sanitized_title,
185
					'trans_num'     => $transaction->trans_num,
186
				);
187
188
				$url = $mepr_options->thankyou_page_url( http_build_query( $args ) );
189
190
				break;
191
			case PaymentStatus::OPEN:
192
			default:
193
				break;
194
		}
195
196
		return $url;
197
	}
198
199
	/**
200
	 * Maybe create create MemberPress transaction for the Pronamic payment.
201
	 * 
202
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php
203
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
204
	 * @param Payment $payment Payment.
205
	 * @return void
206
	 * @throws \Exception Throws an exception when the MemberPress subscription cannot be found.
207
	 */
208
	public function maybe_create_memberpress_transaction( Payment $payment ) {
209
		if ( 'memberpress_subscription' !== $payment->get_source() ) {
210
			return;
211
		}
212
213
		$memberpress_subscription_id = $payment->get_source_id();
214
215
		$memberpress_subscription = MemberPress::get_subscription_by_id( $memberpress_subscription_id );
216
217
		if ( null === $memberpress_subscription ) {
218
			throw new \Exception(
219
				\sprintf(
220
					'Could not find MemberPress subscription with ID: %s.',
221
					$memberpress_subscription_id
222
				)
223
			);
224
		}
225
226
		/**
227
		 * If the payment method is changed we have to update the MemberPress
228
		 * subscription.
229
		 * 
230
		 * @link https://github.com/wp-pay-extensions/memberpress/commit/3631bcb24f376fb637c1317e15f540cb1f9136f4#diff-6f62438f6bf291e85f644dbdbb14b2a71a9a7ed205b01ce44290ed85abe2aa07L259-L290
231
		 */
232
		$memberpress_gateways = MeprOptions::fetch()->payment_methods();
233
234
		foreach ( $memberpress_gateways as $memberpress_gateway ) {
235
			if ( ! $memberpress_gateway instanceof Gateway ) {
236
				continue;
237
			}
238
239
			if ( $memberpress_gateway->get_payment_method() === $payment->get_method() ) {
240
				$memberpress_subscription->gateway = $memberpress_gateway->id;
241
			}
242
		}
243
244
		/**
245
		 * Payment method.
246
		 * 
247
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
248
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
249
		 */
250
		$memberpress_gateway = $memberpress_subscription->payment_method();
251
252
		if ( ! $memberpress_gateway instanceof Gateway ) {
253
			return;
254
		}
255
256
		/**
257
		 * At this point we should call `MeprBaseRealGateway->record_subscription_payment`.
258
		 * 
259
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
260
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L205-L255
261
		 */
262
		$memberpress_gateway->record_subscription_payment();
263
264
		$memberpress_transaction = new MeprTransaction();
265
266
		$memberpress_transaction->user_id         = $memberpress_subscription->user_id;
267
		$memberpress_transaction->product_id      = $memberpress_subscription->product_id;
0 ignored issues
show
Bug Best Practice introduced by
The property product_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
268
		$memberpress_transaction->txn_type        = MeprTransaction::$payment_str;
0 ignored issues
show
Bug Best Practice introduced by
The property txn_type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
269
		$memberpress_transaction->status          = MeprTransaction::$pending_str;
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
270
		$memberpress_transaction->coupon_id       = $memberpress_subscription->coupon_id;
0 ignored issues
show
Bug Best Practice introduced by
The property coupon_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
271
		$memberpress_transaction->trans_num       = $payment->get_transaction_id();
0 ignored issues
show
Bug Best Practice introduced by
The property trans_num does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
272
		$memberpress_transaction->subscription_id = $memberpress_subscription->id;
0 ignored issues
show
Bug Best Practice introduced by
The property subscription_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
273
		$memberpress_transaction->gateway         = $memberpress_gateway->id;
0 ignored issues
show
Bug Best Practice introduced by
The property gateway does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
274
275
		$end_date = $payment->get_end_date();
276
277
		if ( null !== $end_date ) {
278
			$memberpress_transaction->expires_at = MeprUtils::ts_to_mysql_date( $end_date->getTimestamp(), 'Y-m-d 23:59:59' );
0 ignored issues
show
Bug Best Practice introduced by
The property expires_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
279
		}
280
281
		/**
282
		 * Gross.
283
		 * 
284
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L1013-L1021
285
		 */
286
		$memberpress_transaction->set_gross( $payment->get_total_amount()->get_value() );
287
288
		$memberpress_transaction->store();
289
290
		/**
291
		 * Store the MemberPress subscription in case of gateway changes.
292
		 */
293
		$memberpress_subscription->store();
294
295
		/**
296
		 * Update payment source.
297
		 * 
298
		 * @link https://github.com/wp-pay-extensions/restrict-content-pro/blob/3.0.0/src/Extension.php#L770-L776
299
		 */
300
		$payment->source    = 'memberpress_transaction';
301
		$payment->source_id = $memberpress_transaction->id;
302
303
		$payment->save();
304
	}
305
306
	/**
307
	 * Update lead status of the specified payment.
308
	 *
309
	 * @param Payment $payment The payment whose status is updated.
310
	 * @return void
311
	 */
312
	public function status_update( Payment $payment ) {
313
		$payment_source_id = $payment->get_source_id();
314
315
		$memberpress_transaction = MemberPress::get_transaction_by_id( $payment_source_id );
316
317
		/**
318
		 * If we can't find a MemberPress transaction by the payment source ID
319
		 * we can't update the MemberPress transaction, bail out early.
320
		 */
321
		if ( null === $memberpress_transaction ) {
322
			return;
323
		}
324
325
		/**
326
		 * We don't update MemberPress transactions that already have the
327
		 * status 'failed' or 'complete'.
328
		 */
329
		if ( MemberPress::transaction_has_status(
330
			$memberpress_transaction,
331
			array(
332
				MeprTransaction::$failed_str,
333
				MeprTransaction::$complete_str,
334
			)
335
		) ) {
336
			return;
337
		}
338
339
		/**
340
		 * Payment method.
341
		 * 
342
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
343
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
344
		 */
345
		$memberpress_gateway = $memberpress_transaction->payment_method();
346
347
		if ( ! $memberpress_gateway instanceof Gateway ) {
348
			return;
349
		}
350
351
		/**
352
		 * Ok.
353
		 */
354
		switch ( $payment->get_status() ) {
355
			case PaymentStatus::FAILURE:
356
			case PaymentStatus::CANCELLED:
357
			case PaymentStatus::EXPIRED:
358
				$memberpress_gateway->record_payment_failure();
359
360
				$memberpress_transaction->status = MeprTransaction::$failed_str;
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
361
		
362
				$memberpress_transaction->store();
363
364
				/**
365
				 * MemberPress subscription.
366
				 * 
367
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
368
				 */
369
				$memberpress_subscription = $memberpress_transaction->subscription();
370
371
				if ( $memberpress_subscription instanceof MeprSubscription ) {
372
					$memberpress_subscription->expire_txns();
373
				
374
					$memberpress_subscription->store();
375
				}
376
377
				/**
378
				 * Send failed transaction notices.
379
				 * 
380
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528
381
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299
382
				 */
383
				MeprUtils::send_failed_txn_notices( $memberpress_transaction );
384
385
				break;
386
			case PaymentStatus::SUCCESS:
387
				$memberpress_gateway->record_payment();
388
389
				$memberpress_transaction->trans_num = $payment->get_transaction_id();
0 ignored issues
show
Bug Best Practice introduced by
The property trans_num does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
390
				$memberpress_transaction->status    = MeprTransaction::$complete_str;
391
392
				/**
393
				 * Upgrade/downgrade magic.
394
				 * 
395
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprPayPalProGateway.php#L350-L354
396
				 */
397
				$upgrade   = $memberpress_transaction->is_upgrade();
398
				$downgrade = $memberpress_transaction->is_downgrade();
399
400
				$event_txn = $memberpress_transaction->maybe_cancel_old_sub();
401
		
402
				$memberpress_transaction->store();
403
404
				/**
405
				 * Send signup notices.
406
				 * 
407
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390
408
				 */
409
				MeprUtils::send_signup_notices( $memberpress_transaction );
410
411
				/**
412
				 * Send transaction receipt notices.
413
				 * 
414
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
415
				 */
416
				MeprUtils::send_transaction_receipt_notices( $memberpress_transaction );
417
418
				break;
419
			case PaymentStatus::OPEN:
420
			default:
421
				break;
422
		}
423
	}
424
425
	/**
426
	 * Subscription deleted.
427
	 *
428
	 * @param int $subscription_id MemberPress subscription id.
429
	 * @return void
430
	 */
431
	public function subscription_pre_delete( $subscription_id ) {
432
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $subscription_id );
433
434
		if ( ! $subscription ) {
435
			return;
436
		}
437
438
		// Add note.
439
		$note = sprintf(
440
			/* translators: %s: MemberPress */
441
			__( '%s subscription deleted.', 'pronamic_ideal' ),
442
			__( 'MemberPress', 'pronamic_ideal' )
443
		);
444
445
		$subscription->add_note( $note );
446
447
		// The status of canceled or completed subscriptions will not be changed automatically.
448
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
449
			$subscription->set_status( SubscriptionStatus::CANCELLED );
450
451
			$subscription->save();
452
		}
453
	}
454
455
	/**
456
	 * Subscription email parameters.
457
	 *
458
	 * @param array<string, string> $params                   Email parameters.
459
	 * @param MeprSubscription      $memberpress_subscription MemberPress subscription.
460
	 * @return array<string, string>
461
	 */
462
	public function subscription_email_params( $params, MeprSubscription $memberpress_subscription ) {
463
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', $memberpress_subscription->id );
464
465
		if ( empty( $subscriptions ) ) {
466
			return $params;
467
		}
468
469
		$subscription = reset( $subscriptions );
470
471
		// Add parameters.
472
		$next_payment_date = $subscription->get_next_payment_date();
473
474
		return \array_merge(
475
			$params,
476
			array(
477
				'pronamic_subscription_id'           => (string) $subscription->get_id(),
478
				'pronamic_subscription_cancel_url'   => $subscription->get_cancel_url(),
479
				'pronamic_subscription_renewal_url'  => $subscription->get_renewal_url(),
480
				'pronamic_subscription_renewal_date' => null === $next_payment_date ? '' : \date_i18n( \get_option( 'date_format' ), $next_payment_date->getTimestamp() ),
0 ignored issues
show
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

480
				'pronamic_subscription_renewal_date' => null === $next_payment_date ? '' : \date_i18n( /** @scrutinizer ignore-type */ \get_option( 'date_format' ), $next_payment_date->getTimestamp() ),
Loading history...
481
			)
482
		);
483
	}
484
485
	/**
486
	 * Transaction email parameters.
487
	 *
488
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/helpers/MeprTransactionsHelper.php#L233
489
	 * @param array<string, string> $params      Parameters.
490
	 * @param MeprTransaction       $transaction MemberPress transaction.
491
	 * @return array<string, string>
492
	 */
493
	public function transaction_email_params( $params, MeprTransaction $transaction ) {
494
		$payments = \get_pronamic_payments_by_source( 'memberpress_transaction', $transaction->id );
495
496
		if ( null === $payments ) {
497
			return $params;
498
		}
499
500
		$payment = \reset( $payments );
501
502
		if ( false === $payment ) {
503
			return $params;
504
		}
505
506
		// Get subscription.
507
		$periods = $payment->get_periods();
508
509
		if ( null === $periods ) {
510
			return $params;
511
		}
512
513
		$period = \reset( $periods );
514
515
		if ( false === $period ) {
516
			return $params;
517
		}
518
519
		$subscription = $period->get_phase()->get_subscription();
520
521
		// Add parameters.
522
		$memberpress_subscription = new MeprSubscription( $subscription->get_source_id() );
523
524
		return $this->subscription_email_params( $params, $memberpress_subscription );
525
	}
526
527
	/**
528
	 * Source text.
529
	 *
530
	 * @param string  $text    Source text.
531
	 * @param Payment $payment Payment to create the source text for.
532
	 *
533
	 * @return string
534
	 */
535
	public function source_text( $text, Payment $payment ) {
536
		$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />';
537
538
		$text .= sprintf(
539
			'<a href="%s">%s</a>',
540
			add_query_arg(
541
				array(
542
					'page'   => 'memberpress-trans',
543
					'action' => 'edit',
544
					'id'     => $payment->source_id,
545
				),
546
				admin_url( 'admin.php' )
547
			),
548
			/* translators: %s: payment source id */
549
			sprintf( __( 'Transaction %s', 'pronamic_ideal' ), $payment->source_id )
550
		);
551
552
		return $text;
553
	}
554
555
	/**
556
	 * Subscription source text.
557
	 *
558
	 * @param string       $text         Source text.
559
	 * @param Subscription $subscription Subscription to create the source text for.
560
	 *
561
	 * @return string
562
	 */
563
	public function subscription_source_text( $text, Subscription $subscription ) {
564
		$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />';
565
566
		$text .= sprintf(
567
			'<a href="%s">%s</a>',
568
			add_query_arg(
569
				array(
570
					'page'         => 'memberpress-subscriptions',
571
					'subscription' => $subscription->source_id,
572
				),
573
				admin_url( 'admin.php' )
574
			),
575
			/* translators: %s: payment source id */
576
			sprintf( __( 'Subscription %s', 'pronamic_ideal' ), $subscription->source_id )
577
		);
578
579
		return $text;
580
	}
581
582
	/**
583
	 * Source description.
584
	 *
585
	 * @param string  $description Description.
586
	 * @param Payment $payment     Payment to create the description for.
587
	 *
588
	 * @return string
589
	 */
590
	public function source_description( $description, Payment $payment ) {
591
		return __( 'MemberPress Transaction', 'pronamic_ideal' );
592
	}
593
594
	/**
595
	 * Subscription source description.
596
	 *
597
	 * @param string       $description  Description.
598
	 * @param Subscription $subscription Subscription to create the description for.
599
	 *
600
	 * @return string
601
	 */
602
	public function subscription_source_description( $description, Subscription $subscription ) {
603
		return __( 'MemberPress Subscription', 'pronamic_ideal' );
604
	}
605
606
	/**
607
	 * Source URL.
608
	 *
609
	 * @param string  $url     URL.
610
	 * @param Payment $payment The payment to create the source URL for.
611
	 *
612
	 * @return string
613
	 */
614
	public function source_url( $url, Payment $payment ) {
615
		$url = add_query_arg(
616
			array(
617
				'page'   => 'memberpress-trans',
618
				'action' => 'edit',
619
				'id'     => $payment->source_id,
620
			),
621
			admin_url( 'admin.php' )
622
		);
623
624
		return $url;
625
	}
626
627
	/**
628
	 * Subscription source URL.
629
	 *
630
	 * @param string       $url          URL.
631
	 * @param Subscription $subscription Subscription.
632
	 *
633
	 * @return string
634
	 */
635
	public function subscription_source_url( $url, Subscription $subscription ) {
636
		$url = add_query_arg(
637
			array(
638
				'page'         => 'memberpress-subscriptions',
639
				'subscription' => $subscription->source_id,
640
			),
641
			admin_url( 'admin.php' )
642
		);
643
644
		return $url;
645
	}
646
647
	/**
648
	 * MemberPress update subscription.
649
	 *
650
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprSubscriptionsCtrl.php#L92-L111
651
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L100-L123
652
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L112
653
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php#L122
654
	 * @param string           $status_old               Old status identifier.
655
	 * @param string           $status_new               New status identifier.
656
	 * @param MeprSubscription $memberpress_subscription MemberPress subscription object.
657
	 * @return void
658
	 */
659
	public function memberpress_subscription_transition_status( $status_old, $status_new, $memberpress_subscription ) {
660
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $memberpress_subscription->id );
661
662
		if ( empty( $subscription ) ) {
663
			return;
664
		}
665
666
		$status = SubscriptionStatuses::transform( $status_new );
667
668
		if ( null === $status ) {
669
			return;
670
		}
671
672
		$subscription->set_status( $status );
673
674
		$subscription->save();
675
	}
676
}
677