Failed Conditions
Push — develop ( 12791b...c5363b )
by Remco
06:23
created

Extension   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 694
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 25
Bugs 1 Features 0
Metric Value
eloc 237
c 25
b 1
f 0
dl 0
loc 694
ccs 0
cts 318
cp 0
rs 6
wmc 55

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A gateway_paths() 0 4 1
B redirect_url() 0 48 6
A setup() 0 40 3
A post_type_columns_hide() 0 6 2
B maybe_create_memberpress_transaction() 0 96 8
A memberpress_subscription_transition_status() 0 16 3
A subscription_email_params() 0 19 3
A subscription_source_text() 0 17 1
A source_url() 0 11 1
A process_transition() 0 34 3
A subscription_source_url() 0 10 1
B status_update() 0 117 11
A source_description() 0 2 1
A subscription_pre_delete() 0 21 3
A subscription_source_description() 0 2 1
A transaction_email_params() 0 32 5
A source_text() 0 18 1

How to fix   Complexity   

Complex Class

Complex classes like Extension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Extension, and based on these observations, apply Extract Interface, too.

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
	 * Process transition.
309
	 * 
310
	 * @param MeprTransaction|MeprSubscription $memberpress_item    Item.
311
	 * @param Gateway                          $memberpress_gateway Gateway.
312
	 * @return void
313
	 */
314
	private function process_transition( $memberpress_item, Gateway $memberpress_gateway ) {
315
		/**
316
		 * Upgrade/downgrade magic.
317
		 * 
318
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprPayPalProGateway.php#L350-L354
319
		 */
320
		$is_upgrade   = $memberpress_item->is_upgrade();
321
		$is_downgrade = $memberpress_item->is_downgrade();
322
323
		$event_txn = $memberpress_item->maybe_cancel_old_sub();
324
325
		if ( $is_upgrade ) {
326
			/**
327
			 * Upgrade subscription.
328
			 * 
329
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L602-L611
330
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
331
			 */
332
			$memberpress_gateway->upgraded_sub( $memberpress_item, $event_txn );
333
		} elseif ( $is_downgrade ) {
334
			/**
335
			 * Downgraded subscription.
336
			 * 
337
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L613-L622
338
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
339
			 */
340
			$memberpress_gateway->downgraded_sub( $memberpress_item, $event_txn );
341
		} else {
342
			/**
343
			 * New subscription.
344
			 * 
345
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L624-L634
346
			 */
347
			$memberpress_gateway->new_sub( $memberpress_item );
348
		}
349
	}
350
351
	/**
352
	 * Update lead status of the specified payment.
353
	 *
354
	 * @param Payment $payment The payment whose status is updated.
355
	 * @return void
356
	 */
357
	public function status_update( Payment $payment ) {
358
		$payment_source_id = $payment->get_source_id();
359
360
		$memberpress_transaction = MemberPress::get_transaction_by_id( $payment_source_id );
361
362
		/**
363
		 * If we can't find a MemberPress transaction by the payment source ID
364
		 * we can't update the MemberPress transaction, bail out early.
365
		 */
366
		if ( null === $memberpress_transaction ) {
367
			return;
368
		}
369
370
		/**
371
		 * We don't update MemberPress transactions that already have the
372
		 * status 'failed' or 'complete'.
373
		 */
374
		if ( MemberPress::transaction_has_status(
375
			$memberpress_transaction,
376
			array(
377
				MeprTransaction::$failed_str,
378
				MeprTransaction::$complete_str,
379
			)
380
		) ) {
381
			return;
382
		}
383
384
		/**
385
		 * Payment method.
386
		 * 
387
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
388
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
389
		 */
390
		$memberpress_gateway = $memberpress_transaction->payment_method();
391
392
		if ( ! $memberpress_gateway instanceof Gateway ) {
393
			return;
394
		}
395
396
		/**
397
		 * Ok.
398
		 */
399
		switch ( $payment->get_status() ) {
400
			case PaymentStatus::FAILURE:
401
			case PaymentStatus::CANCELLED:
402
			case PaymentStatus::EXPIRED:
403
				$memberpress_gateway->record_payment_failure();
404
405
				$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...
406
		
407
				$memberpress_transaction->store();
408
409
				/**
410
				 * MemberPress subscription.
411
				 * 
412
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
413
				 */
414
				$memberpress_subscription = $memberpress_transaction->subscription();
415
416
				if ( $memberpress_subscription instanceof MeprSubscription ) {
417
					$memberpress_subscription->expire_txns();
418
				
419
					$memberpress_subscription->store();
420
				}
421
422
				/**
423
				 * Send failed transaction notices.
424
				 * 
425
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528
426
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299
427
				 */
428
				MeprUtils::send_failed_txn_notices( $memberpress_transaction );
429
430
				break;
431
			case PaymentStatus::SUCCESS:
432
				$memberpress_gateway->record_payment();
433
434
				$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...
435
				$memberpress_transaction->status    = MeprTransaction::$complete_str;
436
437
				$this->process_transition( $memberpress_transaction, $memberpress_gateway );
438
439
				$memberpress_transaction->store();
440
441
				/**
442
				 * MemberPress subscription.
443
				 * 
444
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
445
				 */
446
				$memberpress_subscription = $memberpress_transaction->subscription();
447
448
				if ( $memberpress_subscription instanceof MeprSubscription ) {
449
					$memberpress_subscription->status = MeprSubscription::$active_str;
450
451
					$this->process_transition( $memberpress_subscription, $memberpress_gateway );
452
				
453
					$memberpress_subscription->store();
454
				}
455
456
				/**
457
				 * Send signup notices.
458
				 * 
459
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390
460
				 */
461
				MeprUtils::send_signup_notices( $memberpress_transaction );
462
463
				/**
464
				 * Send transaction receipt notices.
465
				 * 
466
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
467
				 */
468
				MeprUtils::send_transaction_receipt_notices( $memberpress_transaction );
469
470
				break;
471
			case PaymentStatus::OPEN:
472
			default:
473
				break;
474
		}
475
	}
476
477
	/**
478
	 * Subscription deleted.
479
	 *
480
	 * @param int $subscription_id MemberPress subscription id.
481
	 * @return void
482
	 */
483
	public function subscription_pre_delete( $subscription_id ) {
484
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', (string) $subscription_id );
485
486
		if ( ! $subscription ) {
487
			return;
488
		}
489
490
		// Add note.
491
		$note = sprintf(
492
			/* translators: %s: MemberPress */
493
			__( '%s subscription deleted.', 'pronamic_ideal' ),
494
			__( 'MemberPress', 'pronamic_ideal' )
495
		);
496
497
		$subscription->add_note( $note );
498
499
		// The status of canceled or completed subscriptions will not be changed automatically.
500
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
501
			$subscription->set_status( SubscriptionStatus::CANCELLED );
502
503
			$subscription->save();
504
		}
505
	}
506
507
	/**
508
	 * Subscription email parameters.
509
	 *
510
	 * @param array<string, string> $params                   Email parameters.
511
	 * @param MeprSubscription      $memberpress_subscription MemberPress subscription.
512
	 * @return array<string, string>
513
	 */
514
	public function subscription_email_params( $params, MeprSubscription $memberpress_subscription ) {
515
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', $memberpress_subscription->id );
516
517
		if ( empty( $subscriptions ) ) {
518
			return $params;
519
		}
520
521
		$subscription = reset( $subscriptions );
522
523
		// Add parameters.
524
		$next_payment_date = $subscription->get_next_payment_date();
525
526
		return \array_merge(
527
			$params,
528
			array(
529
				'pronamic_subscription_id'           => (string) $subscription->get_id(),
530
				'pronamic_subscription_cancel_url'   => $subscription->get_cancel_url(),
531
				'pronamic_subscription_renewal_url'  => $subscription->get_renewal_url(),
532
				'pronamic_subscription_renewal_date' => null === $next_payment_date ? '' : \date_i18n( \get_option( 'date_format' ), $next_payment_date->getTimestamp() ),
0 ignored issues
show
Bug introduced by
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

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