Test Failed
Push — develop ( 3409b4...cc333d )
by Remco
05:50
created

src/Extension.php (8 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;
0 ignored issues
show
The type MeprDb was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use MeprOptions;
0 ignored issues
show
The type MeprOptions was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use MeprProduct;
0 ignored issues
show
The type MeprProduct was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use MeprSubscription;
0 ignored issues
show
The type MeprSubscription was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use MeprTransaction;
0 ignored issues
show
The type MeprTransaction was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use MeprUtils;
0 ignored issues
show
The type MeprUtils was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 3.1.0
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
	 * @param string $post_type Registered post type.
128
	 * @return void
129
	 */
130
	public function post_type_columns_hide( $post_type ) {
131
		if ( ! in_array( $post_type, array( 'pronamic_payment', 'pronamic_pay_subscr' ), true ) ) {
132
			return;
133
		}
134
135
		remove_filter( 'manage_edit-' . $post_type . '_columns', 'MeprAppCtrl::columns' );
136
	}
137
138
	/**
139
	 * Payment redirect URL filter.
140
	 *
141
	 * @since 1.0.1
142
	 *
143
	 * @param string  $url     Payment redirect URL.
144
	 * @param Payment $payment Payment to redirect for.
145
	 *
146
	 * @return string
147
	 */
148
	public function redirect_url( $url, Payment $payment ) {
149
		global $transaction;
150
151
		$transaction_id = $payment->get_source_id();
152
153
		$transaction = new MeprTransaction( $transaction_id );
154
155
		switch ( $payment->get_status() ) {
156
			case PaymentStatus::CANCELLED:
157
			case PaymentStatus::EXPIRED:
158
			case PaymentStatus::FAILURE:
159
				$product = $transaction->product();
160
161
				$url = add_query_arg(
162
					array(
163
						'action'   => 'payment_form',
164
						'txn'      => $transaction->trans_num,
165
						'errors'   => array(
166
							__( 'Payment failed. Please try again.', 'pronamic_ideal' ),
167
						),
168
						'_wpnonce' => wp_create_nonce( 'mepr_payment_form' ),
169
					),
170
					$product->url()
171
				);
172
173
				break;
174
			case PaymentStatus::SUCCESS:
175
				// @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782
176
				$mepr_options = MeprOptions::fetch();
177
178
				$product         = new MeprProduct( $transaction->product_id );
179
				$sanitized_title = sanitize_title( $product->post_title );
180
181
				$args = array(
182
					'membership_id' => $product->ID,
183
					'membership'    => $sanitized_title,
184
					'trans_num'     => $transaction->trans_num,
185
				);
186
187
				$url = $mepr_options->thankyou_page_url( http_build_query( $args ) );
188
189
				break;
190
			case PaymentStatus::OPEN:
191
			default:
192
				break;
193
		}
194
195
		return $url;
196
	}
197
198
	/**
199
	 * Maybe create create MemberPress transaction for the Pronamic payment.
200
	 * 
201
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprSubscription.php
202
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
203
	 * @param Payment $payment Payment.
204
	 * @return void
205
	 * @throws \Exception Throws an exception when the MemberPress subscription cannot be found.
206
	 */
207
	public function maybe_create_memberpress_transaction( Payment $payment ) {
208
		if ( ! in_array(
209
			$payment->get_source(),
210
			array(
211
				'memberpress_subscription',
212
				/**
213
				 * Before version `3.1` we used 'memberpress' as source.
214
				 * The upgrade 3.1.0 script corrects this, but for backward
215
				 * compatibility we also accept 'memberpress'.
216
				 * 
217
				 * @link https://github.com/wp-pay-extensions/memberpress/blob/3.0.3/src/Pronamic.php#L128
218
				 * @link https://github.com/pronamic/wp-pay-core/blob/3.0.1/src/Subscriptions/SubscriptionHelper.php#L98-L102
219
				 * @link https://github.com/pronamic/wp-pay-core/blob/3.0.1/src/Subscriptions/SubscriptionsModule.php#L446-L447
220
				 */
221
				'memberpress',
222
			),
223
			true
224
		) ) {
225
			return;
226
		}
227
228
		$memberpress_subscription_id = $payment->get_source_id();
229
230
		$memberpress_subscription = MemberPress::get_subscription_by_id( $memberpress_subscription_id );
231
232
		if ( null === $memberpress_subscription ) {
233
			throw new \Exception(
234
				\sprintf(
235
					'Could not find MemberPress subscription with ID: %s.',
236
					$memberpress_subscription_id
237
				)
238
			);
239
		}
240
241
		/**
242
		 * If the payment method is changed we have to update the MemberPress
243
		 * subscription.
244
		 * 
245
		 * @link https://github.com/wp-pay-extensions/memberpress/commit/3631bcb24f376fb637c1317e15f540cb1f9136f4#diff-6f62438f6bf291e85f644dbdbb14b2a71a9a7ed205b01ce44290ed85abe2aa07L259-L290
246
		 */
247
		$memberpress_gateways = MeprOptions::fetch()->payment_methods();
248
249
		foreach ( $memberpress_gateways as $memberpress_gateway ) {
250
			if ( ! $memberpress_gateway instanceof Gateway ) {
251
				continue;
252
			}
253
254
			if ( $memberpress_gateway->get_payment_method() === $payment->get_payment_method() ) {
0 ignored issues
show
The method get_payment_method() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

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

254
			if ( $memberpress_gateway->get_payment_method() === $payment->/** @scrutinizer ignore-call */ get_payment_method() ) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
255
				$memberpress_subscription->gateway = $memberpress_gateway->id;
256
			}
257
		}
258
259
		/**
260
		 * Payment method.
261
		 * 
262
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
263
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
264
		 */
265
		$memberpress_gateway = $memberpress_subscription->payment_method();
266
267
		if ( ! $memberpress_gateway instanceof Gateway ) {
268
			return;
269
		}
270
271
		/**
272
		 * At this point we should call `MeprBaseRealGateway->record_subscription_payment`.
273
		 * 
274
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
275
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L205-L255
276
		 */
277
		$memberpress_gateway->record_subscription_payment();
278
279
		$memberpress_transaction = new MeprTransaction();
280
281
		$memberpress_transaction->user_id         = $memberpress_subscription->user_id;
282
		$memberpress_transaction->product_id      = $memberpress_subscription->product_id;
283
		$memberpress_transaction->txn_type        = MeprTransaction::$subscription_confirmation_str;
284
		$memberpress_transaction->status          = MeprTransaction::$confirmed_str;
285
		$memberpress_transaction->coupon_id       = $memberpress_subscription->coupon_id;
286
		$memberpress_transaction->trans_num       = $payment->get_transaction_id();
287
		$memberpress_transaction->subscription_id = $memberpress_subscription->id;
288
		$memberpress_transaction->gateway         = $memberpress_gateway->id;
289
290
		$periods = $payment->get_periods();
291
292
		if ( null !== $periods ) {
293
			$end_date = null;
294
295
			foreach ( $periods as $period ) {
296
				$end_date = \max( $end_date, $period->get_end_date() );
297
			}
298
299
			if ( null !== $end_date ) {
300
				$memberpress_transaction->expires_at = MeprUtils::ts_to_mysql_date( $end_date->getTimestamp(), 'Y-m-d 23:59:59' );
301
			}
302
		}
303
304
		/**
305
		 * Gross.
306
		 * 
307
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L1013-L1021
308
		 */
309
		$memberpress_transaction->set_gross( $payment->get_total_amount()->get_value() );
310
311
		$memberpress_transaction->store();
312
313
		/**
314
		 * Store the MemberPress subscription in case of gateway changes.
315
		 */
316
		$memberpress_subscription->store();
317
318
		/**
319
		 * Update payment source.
320
		 * 
321
		 * @link https://github.com/wp-pay-extensions/restrict-content-pro/blob/3.0.0/src/Extension.php#L770-L776
322
		 */
323
		$payment->source    = 'memberpress_transaction';
324
		$payment->source_id = $memberpress_transaction->id;
325
326
		$payment->save();
327
	}
328
329
	/**
330
	 * Process transition.
331
	 * 
332
	 * @param MeprTransaction|MeprSubscription $memberpress_item    Item.
333
	 * @param Gateway                          $memberpress_gateway Gateway.
334
	 * @return void
335
	 */
336
	private function process_transition( $memberpress_item, Gateway $memberpress_gateway ) {
337
		/**
338
		 * Upgrade/downgrade magic.
339
		 * 
340
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprPayPalProGateway.php#L350-L354
341
		 */
342
		$is_upgrade   = $memberpress_item->is_upgrade();
343
		$is_downgrade = $memberpress_item->is_downgrade();
344
345
		$event_txn = $memberpress_item->maybe_cancel_old_sub();
346
347
		if ( $is_upgrade ) {
348
			/**
349
			 * Upgrade subscription.
350
			 * 
351
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L602-L611
352
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
353
			 */
354
			$memberpress_gateway->upgraded_sub( $memberpress_item, $event_txn );
355
		} elseif ( $is_downgrade ) {
356
			/**
357
			 * Downgraded subscription.
358
			 * 
359
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L613-L622
360
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
361
			 */
362
			$memberpress_gateway->downgraded_sub( $memberpress_item, $event_txn );
363
		} else {
364
			/**
365
			 * New subscription.
366
			 * 
367
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L624-L634
368
			 */
369
			$memberpress_gateway->new_sub( $memberpress_item );
370
		}
371
	}
372
373
	/**
374
	 * Update lead status of the specified payment.
375
	 *
376
	 * @param Payment $payment The payment whose status is updated.
377
	 * @return void
378
	 */
379
	public function status_update( Payment $payment ) {
380
		$payment_source_id = $payment->get_source_id();
381
382
		$memberpress_transaction = MemberPress::get_transaction_by_id( $payment_source_id );
383
384
		/**
385
		 * If we can't find a MemberPress transaction by the payment source ID
386
		 * we can't update the MemberPress transaction, bail out early.
387
		 */
388
		if ( null === $memberpress_transaction ) {
389
			return;
390
		}
391
392
		/**
393
		 * We don't update MemberPress transactions that already have the
394
		 * status 'failed' or 'complete'.
395
		 */
396
		if ( MemberPress::transaction_has_status(
397
			$memberpress_transaction,
398
			array(
399
				MeprTransaction::$failed_str,
400
				MeprTransaction::$complete_str,
401
			)
402
		) ) {
403
			return;
404
		}
405
406
		/**
407
		 * Payment method.
408
		 * 
409
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
410
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
411
		 */
412
		$memberpress_gateway = $memberpress_transaction->payment_method();
413
414
		if ( ! $memberpress_gateway instanceof Gateway ) {
415
			return;
416
		}
417
418
		/**
419
		 * Ok.
420
		 */
421
		switch ( $payment->get_status() ) {
422
			case PaymentStatus::FAILURE:
423
			case PaymentStatus::CANCELLED:
424
			case PaymentStatus::EXPIRED:
425
				$memberpress_gateway->record_payment_failure();
426
427
				$memberpress_transaction->txn_type = MeprTransaction::$payment_str;
428
				$memberpress_transaction->status   = MeprTransaction::$failed_str;
429
		
430
				$memberpress_transaction->store();
431
432
				/**
433
				 * MemberPress subscription.
434
				 * 
435
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
436
				 */
437
				$memberpress_subscription = $memberpress_transaction->subscription();
438
439
				if ( $memberpress_subscription instanceof MeprSubscription ) {
440
					$memberpress_subscription->expire_txns();
441
				
442
					$memberpress_subscription->store();
443
				}
444
445
				/**
446
				 * Send failed transaction notices.
447
				 * 
448
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528
449
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299
450
				 */
451
				MeprUtils::send_failed_txn_notices( $memberpress_transaction );
452
453
				break;
454
			case PaymentStatus::SUCCESS:
455
				$memberpress_gateway->record_payment();
456
457
				$memberpress_transaction->trans_num = $payment->get_transaction_id();
458
				$memberpress_transaction->txn_type  = MeprTransaction::$payment_str;
459
				$memberpress_transaction->status    = MeprTransaction::$complete_str;
460
461
				$this->process_transition( $memberpress_transaction, $memberpress_gateway );
462
463
				$memberpress_transaction->store();
464
465
				/**
466
				 * MemberPress subscription.
467
				 * 
468
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
469
				 */
470
				$memberpress_subscription = $memberpress_transaction->subscription();
471
472
				if ( $memberpress_subscription instanceof MeprSubscription ) {
473
					$memberpress_subscription->status = MeprSubscription::$active_str;
474
475
					$this->process_transition( $memberpress_subscription, $memberpress_gateway );
476
				
477
					$memberpress_subscription->store();
478
				}
479
480
				/**
481
				 * Send signup notices.
482
				 * 
483
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390
484
				 */
485
				MeprUtils::send_signup_notices( $memberpress_transaction );
486
487
				/**
488
				 * Send transaction receipt notices.
489
				 * 
490
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
491
				 */
492
				MeprUtils::send_transaction_receipt_notices( $memberpress_transaction );
493
494
				break;
495
			case PaymentStatus::OPEN:
496
			default:
497
				break;
498
		}
499
	}
500
501
	/**
502
	 * Subscription deleted.
503
	 *
504
	 * @param int $subscription_id MemberPress subscription id.
505
	 * @return void
506
	 */
507
	public function subscription_pre_delete( $subscription_id ) {
508
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', (string) $subscription_id );
509
510
		if ( ! $subscription ) {
511
			return;
512
		}
513
514
		// Add note.
515
		$note = sprintf(
516
			/* translators: %s: MemberPress */
517
			__( '%s subscription deleted.', 'pronamic_ideal' ),
518
			__( 'MemberPress', 'pronamic_ideal' )
519
		);
520
521
		$subscription->add_note( $note );
522
523
		// The status of canceled or completed subscriptions will not be changed automatically.
524
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
525
			$subscription->set_status( SubscriptionStatus::CANCELLED );
526
527
			$subscription->save();
528
		}
529
	}
530
531
	/**
532
	 * Subscription email parameters.
533
	 *
534
	 * @param array<string, string> $params                   Email parameters.
535
	 * @param MeprSubscription      $memberpress_subscription MemberPress subscription.
536
	 * @return array<string, string>
537
	 */
538
	public function subscription_email_params( $params, MeprSubscription $memberpress_subscription ) {
539
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', $memberpress_subscription->id );
540
541
		if ( empty( $subscriptions ) ) {
542
			return $params;
543
		}
544
545
		$subscription = reset( $subscriptions );
546
547
		// Add parameters.
548
		$next_payment_date = $subscription->get_next_payment_date();
549
550
		return \array_merge(
551
			$params,
552
			array(
553
				'pronamic_subscription_id'           => (string) $subscription->get_id(),
554
				'pronamic_subscription_cancel_url'   => $subscription->get_cancel_url(),
555
				'pronamic_subscription_renewal_url'  => $subscription->get_renewal_url(),
556
				'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

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