Issues (54)

src/Extension.php (7 issues)

1
<?php
2
/**
3
 * Extension
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 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
		\add_filter( 'mepr_subscription_email_vars', array( $this, 'email_variables' ), 10 );
98
		\add_filter( 'mepr_transaction_email_vars', array( $this, 'email_variables' ), 10 );
99
100
		// Hide MemberPress columns for payments and subscriptions.
101
		\add_action( 'registered_post_type', array( $this, 'post_type_columns_hide' ), 15, 1 );
102
103
		if ( \is_admin() ) {
104
			$admin_subscriptions = new Admin\AdminSubscriptions();
105
			$admin_transactions  = new Admin\AdminTransactions();
106
107
			$admin_subscriptions->setup();
108
			$admin_transactions->setup();
109
		}
110
	}
111
112
	/**
113
	 * Gateway paths.
114
	 *
115
	 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprGatewayFactory.php#L49
116
	 * @param string[] $paths Array with gateway paths.
117
	 * @return string[]
118
	 */
119
	public function gateway_paths( $paths ) {
120
		$paths[] = dirname( __FILE__ ) . '/../gateways/';
121
122
		return $paths;
123
	}
124
125
	/**
126
	 * Hide MemberPress columns for payments and subscriptions.
127
	 *
128
	 * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/controllers/MeprAppCtrl.php#L129-146
129
	 * @param string $post_type Registered post type.
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 ( ! in_array(
211
			$payment->get_source(),
212
			array(
213
				'memberpress_subscription',
214
				/**
215
				 * Before version `3.1` we used 'memberpress' as source.
216
				 * The upgrade 3.1.0 script corrects this, but for backward
217
				 * compatibility we also accept 'memberpress'.
218
				 * 
219
				 * @link https://github.com/wp-pay-extensions/memberpress/blob/3.0.3/src/Pronamic.php#L128
220
				 * @link https://github.com/pronamic/wp-pay-core/blob/3.0.1/src/Subscriptions/SubscriptionHelper.php#L98-L102
221
				 * @link https://github.com/pronamic/wp-pay-core/blob/3.0.1/src/Subscriptions/SubscriptionsModule.php#L446-L447
222
				 */
223
				'memberpress',
224
			),
225
			true
226
		) ) {
227
			return;
228
		}
229
230
		$memberpress_subscription_id = $payment->get_source_id();
231
232
		$memberpress_subscription = MemberPress::get_subscription_by_id( $memberpress_subscription_id );
233
234
		if ( null === $memberpress_subscription ) {
235
			throw new \Exception(
236
				\sprintf(
237
					'Could not find MemberPress subscription with ID: %s.',
238
					$memberpress_subscription_id
239
				)
240
			);
241
		}
242
243
		/**
244
		 * If the payment method is changed we have to update the MemberPress
245
		 * subscription.
246
		 * 
247
		 * @link https://github.com/wp-pay-extensions/memberpress/commit/3631bcb24f376fb637c1317e15f540cb1f9136f4#diff-6f62438f6bf291e85f644dbdbb14b2a71a9a7ed205b01ce44290ed85abe2aa07L259-L290
248
		 */
249
		$memberpress_gateways = MeprOptions::fetch()->payment_methods();
250
251
		foreach ( $memberpress_gateways as $memberpress_gateway ) {
252
			if ( ! $memberpress_gateway instanceof Gateway ) {
253
				continue;
254
			}
255
256
			if ( $memberpress_gateway->get_payment_method() === $payment->get_payment_method() ) {
257
				$memberpress_subscription->gateway = $memberpress_gateway->id;
258
			}
259
		}
260
261
		/**
262
		 * Payment method.
263
		 * 
264
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
265
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
266
		 */
267
		$memberpress_gateway = $memberpress_subscription->payment_method();
268
269
		if ( ! $memberpress_gateway instanceof Gateway ) {
270
			return;
271
		}
272
273
		/**
274
		 * At this point we should call `MeprBaseRealGateway->record_subscription_payment`.
275
		 * 
276
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprStripeGateway.php#L587-L714
277
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L205-L255
278
		 */
279
		$memberpress_gateway->record_subscription_payment();
280
281
		$memberpress_transaction = new MeprTransaction();
282
283
		$memberpress_transaction->user_id         = $memberpress_subscription->user_id;
284
		$memberpress_transaction->product_id      = $memberpress_subscription->product_id;
285
		$memberpress_transaction->txn_type        = MeprTransaction::$subscription_confirmation_str;
286
		$memberpress_transaction->status          = MeprTransaction::$confirmed_str;
287
		$memberpress_transaction->coupon_id       = $memberpress_subscription->coupon_id;
288
		$memberpress_transaction->trans_num       = $payment->get_transaction_id();
289
		$memberpress_transaction->subscription_id = $memberpress_subscription->id;
290
		$memberpress_transaction->gateway         = $memberpress_gateway->id;
291
292
		$periods = $payment->get_periods();
293
294
		if ( null !== $periods ) {
295
			$end_date = null;
296
297
			foreach ( $periods as $period ) {
298
				$end_date = \max( $end_date, $period->get_end_date() );
299
			}
300
301
			if ( null !== $end_date ) {
302
				$memberpress_transaction->expires_at = MeprUtils::ts_to_mysql_date( $end_date->getTimestamp(), 'Y-m-d 23:59:59' );
303
			}
304
		}
305
306
		/**
307
		 * Gross.
308
		 * 
309
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L1013-L1021
310
		 */
311
		$memberpress_transaction->set_gross( $payment->get_total_amount()->get_value() );
312
313
		$memberpress_transaction->store();
314
315
		/**
316
		 * Store the MemberPress subscription in case of gateway changes.
317
		 */
318
		$memberpress_subscription->store();
319
320
		/**
321
		 * Update payment source.
322
		 * 
323
		 * @link https://github.com/wp-pay-extensions/restrict-content-pro/blob/3.0.0/src/Extension.php#L770-L776
324
		 */
325
		$payment->source    = 'memberpress_transaction';
326
		$payment->source_id = $memberpress_transaction->id;
327
328
		$payment->save();
329
	}
330
331
	/**
332
	 * Process transition.
333
	 * 
334
	 * @param MeprTransaction|MeprSubscription $memberpress_item    Item.
335
	 * @param Gateway                          $memberpress_gateway Gateway.
336
	 * @return void
337
	 */
338
	private function process_transition( $memberpress_item, Gateway $memberpress_gateway ) {
339
		/**
340
		 * Upgrade/downgrade magic.
341
		 * 
342
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprPayPalProGateway.php#L350-L354
343
		 */
344
		$is_upgrade   = $memberpress_item->is_upgrade();
345
		$is_downgrade = $memberpress_item->is_downgrade();
346
347
		$event_txn = $memberpress_item->maybe_cancel_old_sub();
348
349
		if ( $is_upgrade ) {
350
			/**
351
			 * Upgrade subscription.
352
			 * 
353
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L602-L611
354
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
355
			 */
356
			$memberpress_gateway->upgraded_sub( $memberpress_item, $event_txn );
357
		} elseif ( $is_downgrade ) {
358
			/**
359
			 * Downgraded subscription.
360
			 * 
361
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L613-L622
362
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprArtificialGateway.php#L109-L122
363
			 */
364
			$memberpress_gateway->downgraded_sub( $memberpress_item, $event_txn );
365
		} else {
366
			/**
367
			 * New subscription.
368
			 * 
369
			 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprBaseGateway.php#L624-L634
370
			 */
371
			$memberpress_gateway->new_sub( $memberpress_item );
372
		}
373
	}
374
375
	/**
376
	 * Update lead status of the specified payment.
377
	 *
378
	 * @param Payment $payment The payment whose status is updated.
379
	 * @return void
380
	 */
381
	public function status_update( Payment $payment ) {
382
		$payment_source_id = $payment->get_source_id();
383
384
		$memberpress_transaction = MemberPress::get_transaction_by_id( $payment_source_id );
385
386
		/**
387
		 * If we can't find a MemberPress transaction by the payment source ID
388
		 * we can't update the MemberPress transaction, bail out early.
389
		 */
390
		if ( null === $memberpress_transaction ) {
391
			return;
392
		}
393
394
		/**
395
		 * We don't update MemberPress transactions that already have the
396
		 * status 'failed' or 'complete'.
397
		 */
398
		if ( MemberPress::transaction_has_status(
399
			$memberpress_transaction,
400
			array(
401
				MeprTransaction::$failed_str,
402
				MeprTransaction::$complete_str,
403
			)
404
		) ) {
405
			return;
406
		}
407
408
		/**
409
		 * Payment method.
410
		 * 
411
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L634-L637
412
		 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprOptions.php#L798-L811
413
		 */
414
		$memberpress_gateway = $memberpress_transaction->payment_method();
415
416
		if ( ! $memberpress_gateway instanceof Gateway ) {
417
			return;
418
		}
419
420
		/**
421
		 * Ok.
422
		 */
423
		switch ( $payment->get_status() ) {
424
			case PaymentStatus::FAILURE:
425
			case PaymentStatus::CANCELLED:
426
			case PaymentStatus::EXPIRED:
427
				$memberpress_gateway->record_payment_failure();
428
429
				$memberpress_transaction->txn_type = MeprTransaction::$payment_str;
430
				$memberpress_transaction->status   = MeprTransaction::$failed_str;
431
		
432
				$memberpress_transaction->store();
433
434
				/**
435
				 * MemberPress subscription.
436
				 * 
437
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
438
				 */
439
				$memberpress_subscription = $memberpress_transaction->subscription();
440
441
				if ( $memberpress_subscription instanceof MeprSubscription ) {
442
					$memberpress_subscription->expire_txns();
443
				
444
					$memberpress_subscription->store();
445
				}
446
447
				/**
448
				 * Send failed transaction notices.
449
				 * 
450
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1515-L1528
451
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/gateways/MeprAuthorizeGateway.php#L299
452
				 */
453
				MeprUtils::send_failed_txn_notices( $memberpress_transaction );
454
455
				break;
456
			case PaymentStatus::SUCCESS:
457
				$memberpress_gateway->record_payment();
458
459
				$memberpress_transaction->trans_num = $payment->get_transaction_id();
460
				$memberpress_transaction->txn_type  = MeprTransaction::$payment_str;
461
				$memberpress_transaction->status    = MeprTransaction::$complete_str;
462
463
				$this->process_transition( $memberpress_transaction, $memberpress_gateway );
464
465
				$memberpress_transaction->store();
466
467
				/**
468
				 * MemberPress subscription.
469
				 * 
470
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/models/MeprTransaction.php#L605-L620
471
				 */
472
				$memberpress_subscription = $memberpress_transaction->subscription();
473
474
				if ( $memberpress_subscription instanceof MeprSubscription ) {
475
					$memberpress_subscription->status = MeprSubscription::$active_str;
476
477
					$this->process_transition( $memberpress_subscription, $memberpress_gateway );
478
				
479
					$memberpress_subscription->store();
480
				}
481
482
				/**
483
				 * Send signup notices.
484
				 * 
485
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1361-L1390
486
				 */
487
				MeprUtils::send_signup_notices( $memberpress_transaction );
488
489
				/**
490
				 * Send transaction receipt notices.
491
				 * 
492
				 * @link https://github.com/wp-premium/memberpress/blob/1.9.21/app/lib/MeprUtils.php#L1396-L1418
493
				 */
494
				MeprUtils::send_transaction_receipt_notices( $memberpress_transaction );
495
496
				break;
497
			case PaymentStatus::OPEN:
498
			default:
499
				break;
500
		}
501
	}
502
503
	/**
504
	 * Subscription deleted.
505
	 *
506
	 * @param int $subscription_id MemberPress subscription id.
507
	 * @return void
508
	 */
509
	public function subscription_pre_delete( $subscription_id ) {
510
		$subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', (string) $subscription_id );
511
512
		if ( ! $subscription ) {
513
			return;
514
		}
515
516
		// Add note.
517
		$note = sprintf(
518
			/* translators: %s: MemberPress */
519
			__( '%s subscription deleted.', 'pronamic_ideal' ),
520
			__( 'MemberPress', 'pronamic_ideal' )
521
		);
522
523
		$subscription->add_note( $note );
524
525
		// The status of canceled or completed subscriptions will not be changed automatically.
526
		if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) {
527
			$subscription->set_status( SubscriptionStatus::CANCELLED );
528
529
			$subscription->save();
530
		}
531
	}
532
533
	/**
534
	 * Filter email variables.
535
	 *
536
	 * @param string[] $variables Email variables.
537
	 * @return string[]
538
	 */
539
	public function email_variables( $variables ) {
540
		return \array_merge(
541
			$variables,
542
			array(
543
				'pronamic_subscription_id',
544
				'pronamic_subscription_cancel_url',
545
				'pronamic_subscription_renewal_url',
546
				'pronamic_subscription_renewal_date',
547
			)
548
		);
549
	}
550
551
	/**
552
	 * Subscription email parameters.
553
	 *
554
	 * @param array<string, string> $params                   Email parameters.
555
	 * @param MeprSubscription      $memberpress_subscription MemberPress subscription.
556
	 * @return array<string, string>
557
	 */
558
	public function subscription_email_params( $params, MeprSubscription $memberpress_subscription ) {
559
		$subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress_subscription', $memberpress_subscription->id );
560
561
		if ( empty( $subscriptions ) ) {
562
			return $params;
563
		}
564
565
		$subscription = reset( $subscriptions );
566
567
		// Add parameters.
568
		$next_payment_date = $subscription->get_next_payment_date();
569
570
		return \array_merge(
571
			$params,
572
			array(
573
				'pronamic_subscription_id'           => (string) $subscription->get_id(),
574
				'pronamic_subscription_cancel_url'   => $subscription->get_cancel_url(),
575
				'pronamic_subscription_renewal_url'  => $subscription->get_renewal_url(),
576
				'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

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