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

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