Failed Conditions
Push — develop ( dcdad5...3d8e5d )
by Remco
06:27
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<string, mixed> $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_subscription', 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
87
		\add_filter( 'pronamic_payment_source_text_memberpress_transaction', array( $this, 'source_text' ), 10, 2 );
88
		\add_filter( 'pronamic_payment_source_url_memberpress_transaction', array( $this, 'source_url' ), 10, 2 );
89
90
		\add_action( 'mepr_subscription_pre_delete', array( $this, 'subscription_pre_delete' ), 10, 1 );
91
92
		\add_action( 'mepr_subscription_transition_status', array( $this, 'memberpress_subscription_transition_status' ), 10, 3 );
93
94
		// MemberPress subscription email parameters.
95
		\add_filter( 'mepr_subscription_email_params', array( $this, 'subscription_email_params' ), 10, 2 );
96
		\add_filter( 'mepr_transaction_email_params', array( $this, 'transaction_email_params' ), 10, 2 );
97
98
		// Hide MemberPress columns for payments and subscriptions.
99
		\add_action( 'registered_post_type', array( $this, 'post_type_columns_hide' ), 15, 1 );
100
101
		if ( \is_admin() ) {
102
			$admin_subscriptions = new Admin\AdminSubscriptions();
103
			$admin_transactions  = new Admin\AdminTransactions();
104
105
			$admin_subscriptions->setup();
106
			$admin_transactions->setup();
107
		}
108
	}
109
110
	/**
111
	 * Gateway paths.
112
	 *
113
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprGatewayFactory.php#L49
114
	 * @param string[] $paths Array with gateway paths.
115
	 * @return string[]
116
	 */
117
	public function gateway_paths( $paths ) {
118
		$paths[] = dirname( __FILE__ ) . '/../gateways/';
119
120
		return $paths;
121
	}
122
123
	/**
124
	 * Hide MemberPress columns for payments and subscriptions.
125
	 *
126
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/controllers/MeprAppCtrl.php#L129-146
127
	 *
128
	 * @param string $post_type Registered post type.
129
	 *
130
	 * @return void
131
	 */
132
	public function post_type_columns_hide( $post_type ) {
133
		if ( ! in_array( $post_type, array( 'pronamic_payment', 'pronamic_pay_subscr' ), true ) ) {
134
			return;
135
		}
136
137
		remove_filter( 'manage_edit-' . $post_type . '_columns', 'MeprAppCtrl::columns' );
138
	}
139
140
	/**
141
	 * Payment redirect URL filter.
142
	 *
143
	 * @since 1.0.1
144
	 *
145
	 * @param string  $url     Payment redirect URL.
146
	 * @param Payment $payment Payment to redirect for.
147
	 *
148
	 * @return string
149
	 */
150
	public function redirect_url( $url, Payment $payment ) {
151
		global $transaction;
152
153
		$transaction_id = $payment->get_source_id();
154
155
		$transaction = new MeprTransaction( $transaction_id );
156
157
		switch ( $payment->get_status() ) {
158
			case PaymentStatus::CANCELLED:
159
			case PaymentStatus::EXPIRED:
160
			case PaymentStatus::FAILURE:
161
				$product = $transaction->product();
162
163
				$url = add_query_arg(
164
					array(
165
						'action'   => 'payment_form',
166
						'txn'      => $transaction->trans_num,
167
						'errors'   => array(
168
							__( 'Payment failed. Please try again.', 'pronamic_ideal' ),
169
						),
170
						'_wpnonce' => wp_create_nonce( 'mepr_payment_form' ),
171
					),
172
					$product->url()
173
				);
174
175
				break;
176
			case PaymentStatus::SUCCESS:
177
				// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782
178
				$mepr_options = MeprOptions::fetch();
179
180
				$product         = new MeprProduct( $transaction->product_id );
181
				$sanitized_title = sanitize_title( $product->post_title );
182
183
				$args = array(
184
					'membership_id' => $product->ID,
185
					'membership'    => $sanitized_title,
186
					'trans_num'     => $transaction->trans_num,
187
				);
188
189
				$url = $mepr_options->thankyou_page_url( http_build_query( $args ) );
190
191
				break;
192
			case PaymentStatus::OPEN:
193
			default:
194
				break;
195
		}
196
197
		return $url;
198
	}
199
200
	/**
201
	 * Maybe create create MemberPress transaction for the Pronamic payment.
202
	 * 
203
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php
204
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
205
	 * @param Payment $payment Payment.
206
	 * @return void
207
	 * @throws \Exception Throws an exception when the MemberPress subscription cannot be found.
208
	 */
209
	public function maybe_create_memberpress_transaction( Payment $payment ) {
210
		if ( 'memberpress_subscription' !== $payment->get_source() ) {
211
			return;
212
		}
213
214
		$memberpress_subscription_id = $payment->get_source_id();
215
216
		$memberpress_subscription = MemberPress::get_subscription_by_id( $memberpress_subscription_id );
217
218
		if ( null === $memberpress_subscription ) {
219
			throw new \Exception(
220
				\sprintf(
221
					'Could not find MemberPress subscription with ID: %s.',
222
					$memberpress_subscription_id
223
				)
224
			);
225
		}
226
227
		/**
228
		 * If the payment method is changed we have to update the MemberPress
229
		 * subscription.
230
		 * 
231
		 * @link https://github.com/wp-pay-extensions/memberpress/commit/3631bcb24f376fb637c1317e15f540cb1f9136f4#diff-6f62438f6bf291e85f644dbdbb14b2a71a9a7ed205b01ce44290ed85abe2aa07L259-L290
232
		 */
233
		$memberpress_gateways = MeprOptions::fetch()->payment_methods();
234
235
		foreach ( $memberpress_gateways as $memberpress_gateway ) {
236
			if ( ! $memberpress_gateway instanceof Gateway ) {
237
				continue;
238
			}
239
240
			if ( $memberpress_gateway->get_payment_method() === $payment->get_method() ) {
241
				$memberpress_subscription->gateway = $memberpress_gateway->id;
242
			}
243
		}
244
245
		/**
246
		 * Payment method.
247
		 * 
248
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
249
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
250
		 */
251
		$memberpress_gateway = $memberpress_subscription->payment_method();
252
253
		if ( ! $memberpress_gateway instanceof Gateway ) {
254
			return;
255
		}
256
257
		/**
258
		 * At this point we should call `MeprBaseRealGateway->record_subscription_payment`.
259
		 * 
260
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
261
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L205-L255
262
		 */
263
		$memberpress_gateway->record_subscription_payment();
264
265
		$memberpress_transaction = new MeprTransaction();
266
267
		$memberpress_transaction->user_id         = $memberpress_subscription->user_id;
268
		$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...
269
		$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...
270
		$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...
271
		$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...
272
		$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...
273
		$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...
274
		$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...
275
276
		$end_date = $payment->get_end_date();
277
278
		if ( null !== $end_date ) {
279
			$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...
280
		}
281
282
		/**
283
		 * Gross.
284
		 * 
285
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L1013-L1021
286
		 */
287
		$memberpress_transaction->set_gross( $payment->get_total_amount()->get_value() );
288
289
		$memberpress_transaction->store();
290
291
		/**
292
		 * Store the MemberPress subscription in case of gateway changes.
293
		 */
294
		$memberpress_subscription->store();
295
296
		/**
297
		 * Update payment source.
298
		 * 
299
		 * @link https://github.com/wp-pay-extensions/restrict-content-pro/blob/3.0.0/src/Extension.php#L770-L776
300
		 */
301
		$payment->source    = 'memberpress_transaction';
302
		$payment->source_id = $memberpress_transaction->id;
303
304
		$payment->save();
305
	}
306
307
	/**
308
	 * Update lead status of the specified payment.
309
	 *
310
	 * @param Payment $payment The payment whose status is updated.
311
	 * @return void
312
	 */
313
	public function status_update( Payment $payment ) {
314
		$payment_source_id = $payment->get_source_id();
315
316
		$memberpress_transaction = MemberPress::get_transaction_by_id( $payment_source_id );
317
318
		/**
319
		 * If we can't find a MemberPress transaction by the payment source ID
320
		 * we can't update the MemberPress transaction, bail out early.
321
		 */
322
		if ( null === $memberpress_transaction ) {
323
			return;
324
		}
325
326
		/**
327
		 * We don't update MemberPress transactions that already have the
328
		 * status 'failed' or 'complete'.
329
		 */
330
		if ( MemberPress::transaction_has_status(
331
			$memberpress_transaction,
332
			array(
333
				MeprTransaction::$failed_str,
334
				MeprTransaction::$complete_str,
335
			)
336
		) ) {
337
			return;
338
		}
339
340
		/**
341
		 * Payment method.
342
		 * 
343
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
344
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
345
		 */
346
		$memberpress_gateway = $memberpress_transaction->payment_method();
347
348
		if ( ! $memberpress_gateway instanceof Gateway ) {
349
			return;
350
		}
351
352
		/**
353
		 * Ok.
354
		 */
355
		switch ( $payment->get_status() ) {
356
			case PaymentStatus::FAILURE:
357
			case PaymentStatus::CANCELLED:
358
			case PaymentStatus::EXPIRED:
359
				$memberpress_gateway->record_payment_failure();
360
361
				$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...
362
		
363
				$memberpress_transaction->store();
364
365
				/**
366
				 * MemberPress subscription.
367
				 * 
368
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
369
				 */
370
				$memberpress_subscription = $memberpress_transaction->subscription();
371
372
				if ( $memberpress_subscription instanceof MeprSubscription ) {
373
					$memberpress_subscription->expire_txns();
374
				
375
					$memberpress_subscription->store();
376
				}
377
378
				/**
379
				 * Send failed transaction notices.
380
				 * 
381
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528
382
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299
383
				 */
384
				MeprUtils::send_failed_txn_notices( $memberpress_transaction );
385
386
				break;
387
			case PaymentStatus::SUCCESS:
388
				$memberpress_gateway->record_payment();
389
390
				$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...
391
				$memberpress_transaction->status    = MeprTransaction::$complete_str;
392
393
				/**
394
				 * Upgrade/downgrade magic.
395
				 * 
396
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprPayPalProGateway.php#L350-L354
397
				 */
398
				$is_upgrade   = $memberpress_transaction->is_upgrade();
399
				$is_downgrade = $memberpress_transaction->is_downgrade();
400
401
				$event_txn = $memberpress_transaction->maybe_cancel_old_sub();
402
403
				if ( $is_upgrade ) {
404
					/**
405
					 * Upgrade subscription.
406
					 * 
407
					 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L602-L611
408
					 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
409
					 */
410
					$memberpress_gateway->upgraded_sub( $memberpress_transaction, $event_txn );
411
				} elseif ( $is_downgrade ) {
412
					/**
413
					 * Downgraded subscription.
414
					 * 
415
					 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L613-L622
416
					 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
417
					 */
418
					$memberpress_gateway->downgraded_sub( $memberpress_transaction, $event_txn );
419
				} else {
420
					/**
421
					 * New subscription.
422
					 * 
423
					 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L624-L634
424
					 */
425
					$memberpress_gateway->new_sub( $memberpress_transaction );
426
				}
427
428
				$memberpress_transaction->store();
429
430
				/**
431
				 * MemberPress subscription.
432
				 * 
433
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
434
				 */
435
				$memberpress_subscription = $memberpress_transaction->subscription();
436
437
				if ( $memberpress_subscription instanceof MeprSubscription ) {
438
					$memberpress_subscription->status = MeprSubscription::$active_str;
439
				
440
					$memberpress_subscription->store();
441
				}
442
443
				/**
444
				 * Send signup notices.
445
				 * 
446
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390
447
				 */
448
				MeprUtils::send_signup_notices( $memberpress_transaction );
449
450
				/**
451
				 * Send transaction receipt notices.
452
				 * 
453
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
454
				 */
455
				MeprUtils::send_transaction_receipt_notices( $memberpress_transaction );
456
457
				break;
458
			case PaymentStatus::OPEN:
459
			default:
460
				break;
461
		}
462
	}
463
464
	/**
465
	 * Subscription deleted.
466
	 *
467
	 * @param int $subscription_id MemberPress subscription id.
468
	 * @return void
469
	 */
470
	public function subscription_pre_delete( $subscription_id ) {
471
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', (string) $subscription_id );
472
473
		if ( ! $subscription ) {
474
			return;
475
		}
476
477
		// Add note.
478
		$note = sprintf(
479
			/* translators: %s: MemberPress */
480
			__( '%s subscription deleted.', 'pronamic_ideal' ),
481
			__( 'MemberPress', 'pronamic_ideal' )
482
		);
483
484
		$subscription->add_note( $note );
485
486
		// The status of canceled or completed subscriptions will not be changed automatically.
487
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
488
			$subscription->set_status( SubscriptionStatus::CANCELLED );
489
490
			$subscription->save();
491
		}
492
	}
493
494
	/**
495
	 * Subscription email parameters.
496
	 *
497
	 * @param array<string, string> $params                   Email parameters.
498
	 * @param MeprSubscription      $memberpress_subscription MemberPress subscription.
499
	 * @return array<string, string>
500
	 */
501
	public function subscription_email_params( $params, MeprSubscription $memberpress_subscription ) {
502
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', $memberpress_subscription->id );
503
504
		if ( empty( $subscriptions ) ) {
505
			return $params;
506
		}
507
508
		$subscription = reset( $subscriptions );
509
510
		// Add parameters.
511
		$next_payment_date = $subscription->get_next_payment_date();
512
513
		return \array_merge(
514
			$params,
515
			array(
516
				'pronamic_subscription_id'           => (string) $subscription->get_id(),
517
				'pronamic_subscription_cancel_url'   => $subscription->get_cancel_url(),
518
				'pronamic_subscription_renewal_url'  => $subscription->get_renewal_url(),
519
				'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

519
				'pronamic_subscription_renewal_date' => null === $next_payment_date ? '' : \date_i18n( /** @scrutinizer ignore-type */ \get_option( 'date_format' ), $next_payment_date->getTimestamp() ),
Loading history...
520
			)
521
		);
522
	}
523
524
	/**
525
	 * Transaction email parameters.
526
	 *
527
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/helpers/MeprTransactionsHelper.php#L233
528
	 * @param array<string, string> $params      Parameters.
529
	 * @param MeprTransaction       $transaction MemberPress transaction.
530
	 * @return array<string, string>
531
	 */
532
	public function transaction_email_params( $params, MeprTransaction $transaction ) {
533
		$payments = \get_pronamic_payments_by_source( 'memberpress_transaction', $transaction->id );
534
535
		if ( null === $payments ) {
536
			return $params;
537
		}
538
539
		$payment = \reset( $payments );
540
541
		if ( false === $payment ) {
542
			return $params;
543
		}
544
545
		// Get subscription.
546
		$periods = $payment->get_periods();
547
548
		if ( null === $periods ) {
549
			return $params;
550
		}
551
552
		$period = \reset( $periods );
553
554
		if ( false === $period ) {
555
			return $params;
556
		}
557
558
		$subscription = $period->get_phase()->get_subscription();
559
560
		// Add parameters.
561
		$memberpress_subscription = new MeprSubscription( $subscription->get_source_id() );
562
563
		return $this->subscription_email_params( $params, $memberpress_subscription );
564
	}
565
566
	/**
567
	 * Source text.
568
	 *
569
	 * @param string  $text    Source text.
570
	 * @param Payment $payment Payment to create the source text for.
571
	 *
572
	 * @return string
573
	 */
574
	public function source_text( $text, Payment $payment ) {
575
		$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />';
576
577
		$text .= sprintf(
578
			'<a href="%s">%s</a>',
579
			add_query_arg(
580
				array(
581
					'page'   => 'memberpress-trans',
582
					'action' => 'edit',
583
					'id'     => $payment->source_id,
584
				),
585
				admin_url( 'admin.php' )
586
			),
587
			/* translators: %s: payment source id */
588
			sprintf( __( 'Transaction %s', 'pronamic_ideal' ), $payment->source_id )
589
		);
590
591
		return $text;
592
	}
593
594
	/**
595
	 * Subscription source text.
596
	 *
597
	 * @param string       $text         Source text.
598
	 * @param Subscription $subscription Subscription to create the source text for.
599
	 *
600
	 * @return string
601
	 */
602
	public function subscription_source_text( $text, Subscription $subscription ) {
603
		$text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />';
604
605
		$text .= sprintf(
606
			'<a href="%s">%s</a>',
607
			add_query_arg(
608
				array(
609
					'page'         => 'memberpress-subscriptions',
610
					'subscription' => $subscription->source_id,
611
				),
612
				admin_url( 'admin.php' )
613
			),
614
			/* translators: %s: payment source id */
615
			sprintf( __( 'Subscription %s', 'pronamic_ideal' ), $subscription->source_id )
616
		);
617
618
		return $text;
619
	}
620
621
	/**
622
	 * Source description.
623
	 *
624
	 * @param string  $description Description.
625
	 * @param Payment $payment     Payment to create the description for.
626
	 *
627
	 * @return string
628
	 */
629
	public function source_description( $description, Payment $payment ) {
630
		return __( 'MemberPress Transaction', 'pronamic_ideal' );
631
	}
632
633
	/**
634
	 * Subscription source description.
635
	 *
636
	 * @param string       $description  Description.
637
	 * @param Subscription $subscription Subscription to create the description for.
638
	 *
639
	 * @return string
640
	 */
641
	public function subscription_source_description( $description, Subscription $subscription ) {
642
		return __( 'MemberPress Subscription', 'pronamic_ideal' );
643
	}
644
645
	/**
646
	 * Source URL.
647
	 *
648
	 * @param string  $url     URL.
649
	 * @param Payment $payment The payment to create the source URL for.
650
	 *
651
	 * @return string
652
	 */
653
	public function source_url( $url, Payment $payment ) {
654
		$url = add_query_arg(
655
			array(
656
				'page'   => 'memberpress-trans',
657
				'action' => 'edit',
658
				'id'     => $payment->source_id,
659
			),
660
			admin_url( 'admin.php' )
661
		);
662
663
		return $url;
664
	}
665
666
	/**
667
	 * Subscription source URL.
668
	 *
669
	 * @param string       $url          URL.
670
	 * @param Subscription $subscription Subscription.
671
	 *
672
	 * @return string
673
	 */
674
	public function subscription_source_url( $url, Subscription $subscription ) {
675
		$url = add_query_arg(
676
			array(
677
				'page'         => 'memberpress-subscriptions',
678
				'subscription' => $subscription->source_id,
679
			),
680
			admin_url( 'admin.php' )
681
		);
682
683
		return $url;
684
	}
685
686
	/**
687
	 * MemberPress update subscription.
688
	 *
689
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprSubscriptionsCtrl.php#L92-L111
690
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L100-L123
691
	 * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L112
692
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php#L122
693
	 * @param string           $status_old               Old status identifier.
694
	 * @param string           $status_new               New status identifier.
695
	 * @param MeprSubscription $memberpress_subscription MemberPress subscription object.
696
	 * @return void
697
	 */
698
	public function memberpress_subscription_transition_status( $status_old, $status_new, $memberpress_subscription ) {
699
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $memberpress_subscription->id );
700
701
		if ( empty( $subscription ) ) {
702
			return;
703
		}
704
705
		$status = SubscriptionStatuses::transform( $status_new );
706
707
		if ( null === $status ) {
708
			return;
709
		}
710
711
		$subscription->set_status( $status );
712
713
		$subscription->save();
714
	}
715
}
716