wp-pay-extensions /
memberpress
| 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 | public function __construct() { |
||||
| 45 | parent::__construct( |
||||
| 46 | array( |
||||
| 47 | 'name' => __( 'MemberPress', 'pronamic_ideal' ), |
||||
| 48 | ) |
||||
| 49 | ); |
||||
| 50 | |||||
| 51 | // Dependencies. |
||||
| 52 | $dependencies = $this->get_dependencies(); |
||||
| 53 | |||||
| 54 | $dependencies->add( new MemberPressDependency() ); |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * Setup. |
||||
| 59 | */ |
||||
| 60 | public function setup() { |
||||
| 61 | \add_filter( 'pronamic_subscription_source_description_' . self::SLUG, array( $this, 'subscription_source_description' ), 10, 2 ); |
||||
| 62 | \add_filter( 'pronamic_payment_source_description_' . self::SLUG, array( $this, 'source_description' ), 10, 2 ); |
||||
| 63 | |||||
| 64 | // Check if dependencies are met and integration is active. |
||||
| 65 | if ( ! $this->is_active() ) { |
||||
| 66 | return; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | // @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50 |
||||
| 70 | \add_filter( 'mepr-gateway-paths', array( $this, 'gateway_paths' ) ); |
||||
| 71 | |||||
| 72 | \add_filter( 'pronamic_payment_redirect_url_' . self::SLUG, array( $this, 'redirect_url' ), 10, 2 ); |
||||
| 73 | \add_action( 'pronamic_payment_status_update_' . self::SLUG, array( $this, 'status_update' ), 10, 1 ); |
||||
| 74 | |||||
| 75 | \add_filter( 'pronamic_subscription_source_text_' . self::SLUG, array( $this, 'subscription_source_text' ), 10, 2 ); |
||||
| 76 | \add_filter( 'pronamic_subscription_source_url_' . self::SLUG, array( $this, 'subscription_source_url' ), 10, 2 ); |
||||
| 77 | \add_filter( 'pronamic_payment_source_text_' . self::SLUG, array( $this, 'source_text' ), 10, 2 ); |
||||
| 78 | \add_filter( 'pronamic_payment_source_url_' . self::SLUG, array( $this, 'source_url' ), 10, 2 ); |
||||
| 79 | |||||
| 80 | \add_action( 'mepr_subscription_pre_delete', array( $this, 'subscription_pre_delete' ), 10, 1 ); |
||||
| 81 | |||||
| 82 | \add_action( 'mepr_subscription_transition_status', array( $this, 'memberpress_subscription_transition_status' ), 10, 3 ); |
||||
| 83 | |||||
| 84 | // MemberPress subscription email parameters. |
||||
| 85 | \add_filter( 'mepr_subscription_email_params', array( $this, 'subscription_email_params' ), 10, 2 ); |
||||
| 86 | \add_filter( 'mepr_transaction_email_params', array( $this, 'transaction_email_params' ), 10, 2 ); |
||||
| 87 | |||||
| 88 | // Hide MemberPress columns for payments and subscriptions. |
||||
| 89 | \add_action( 'registered_post_type', array( $this, 'post_type_columns_hide' ), 15, 1 ); |
||||
| 90 | |||||
| 91 | if ( \is_admin() ) { |
||||
| 92 | $this->admin_subscriptions = new Admin\AdminSubscriptions(); |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 93 | $this->admin_transactions = new Admin\AdminTransactions(); |
||||
|
0 ignored issues
–
show
|
|||||
| 94 | |||||
| 95 | $this->admin_subscriptions->setup(); |
||||
| 96 | $this->admin_transactions->setup(); |
||||
| 97 | } |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * Gateway paths. |
||||
| 102 | * |
||||
| 103 | * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50 |
||||
| 104 | * |
||||
| 105 | * @param array $paths Array with gateway paths. |
||||
| 106 | * @return array |
||||
| 107 | */ |
||||
| 108 | public function gateway_paths( $paths ) { |
||||
| 109 | $paths[] = dirname( __FILE__ ) . '/../gateways/'; |
||||
| 110 | |||||
| 111 | return $paths; |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * Hide MemberPress columns for payments and subscriptions. |
||||
| 116 | * |
||||
| 117 | * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/controllers/MeprAppCtrl.php#L129-146 |
||||
| 118 | * |
||||
| 119 | * @param string $post_type Registered post type. |
||||
| 120 | * |
||||
| 121 | * @return void |
||||
| 122 | */ |
||||
| 123 | public function post_type_columns_hide( $post_type ) { |
||||
| 124 | if ( ! in_array( $post_type, array( 'pronamic_payment', 'pronamic_pay_subscr' ), true ) ) { |
||||
| 125 | return; |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | remove_filter( 'manage_edit-' . $post_type . '_columns', 'MeprAppCtrl::columns' ); |
||||
| 129 | } |
||||
| 130 | |||||
| 131 | /** |
||||
| 132 | * Payment redirect URL filter. |
||||
| 133 | * |
||||
| 134 | * @since 1.0.1 |
||||
| 135 | * |
||||
| 136 | * @param string $url Payment redirect URL. |
||||
| 137 | * @param Payment $payment Payment to redirect for. |
||||
| 138 | * |
||||
| 139 | * @return string |
||||
| 140 | */ |
||||
| 141 | public function redirect_url( $url, Payment $payment ) { |
||||
| 142 | global $transaction; |
||||
| 143 | |||||
| 144 | $transaction_id = $payment->get_source_id(); |
||||
| 145 | |||||
| 146 | $transaction = new MeprTransaction( $transaction_id ); |
||||
| 147 | |||||
| 148 | switch ( $payment->get_status() ) { |
||||
| 149 | case PaymentStatus::CANCELLED: |
||||
| 150 | case PaymentStatus::EXPIRED: |
||||
| 151 | case PaymentStatus::FAILURE: |
||||
| 152 | $product = $transaction->product(); |
||||
| 153 | |||||
| 154 | $url = add_query_arg( |
||||
| 155 | array( |
||||
| 156 | 'action' => 'payment_form', |
||||
| 157 | 'txn' => $transaction->trans_num, |
||||
| 158 | 'errors' => array( |
||||
| 159 | __( 'Payment failed. Please try again.', 'pronamic_ideal' ), |
||||
| 160 | ), |
||||
| 161 | '_wpnonce' => wp_create_nonce( 'mepr_payment_form' ), |
||||
| 162 | ), |
||||
| 163 | $product->url() |
||||
| 164 | ); |
||||
| 165 | |||||
| 166 | break; |
||||
| 167 | case PaymentStatus::SUCCESS: |
||||
| 168 | // @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782 |
||||
| 169 | $mepr_options = MeprOptions::fetch(); |
||||
| 170 | |||||
| 171 | $product = new MeprProduct( $transaction->product_id ); |
||||
| 172 | $sanitized_title = sanitize_title( $product->post_title ); |
||||
| 173 | |||||
| 174 | $args = array( |
||||
| 175 | 'membership_id' => $product->ID, |
||||
| 176 | 'membership' => $sanitized_title, |
||||
| 177 | 'trans_num' => $transaction->trans_num, |
||||
| 178 | ); |
||||
| 179 | |||||
| 180 | $url = $mepr_options->thankyou_page_url( http_build_query( $args ) ); |
||||
| 181 | |||||
| 182 | break; |
||||
| 183 | case PaymentStatus::OPEN: |
||||
| 184 | default: |
||||
| 185 | break; |
||||
| 186 | } |
||||
| 187 | |||||
| 188 | return $url; |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | /** |
||||
| 192 | * Update lead status of the specified payment. |
||||
| 193 | * |
||||
| 194 | * @link https://github.com/Charitable/Charitable/blob/1.1.4/includes/gateways/class-charitable-gateway-paypal.php#L229-L357 |
||||
| 195 | * |
||||
| 196 | * @param Payment $payment The payment whose status is updated. |
||||
| 197 | */ |
||||
| 198 | public function status_update( Payment $payment ) { |
||||
| 199 | $transaction = new MeprTransaction( $payment->get_source_id() ); |
||||
| 200 | |||||
| 201 | if ( $payment->get_recurring() || empty( $transaction->id ) ) { |
||||
| 202 | $subscription_id = $payment->get_subscription()->get_source_id(); |
||||
| 203 | $subscription = new MeprSubscription( $subscription_id ); |
||||
| 204 | |||||
| 205 | // Same source ID and first transaction ID for recurring payment means we need to add a new transaction. |
||||
| 206 | if ( $payment->get_source_id() === $subscription->id ) { |
||||
| 207 | // First transaction. |
||||
| 208 | $first_txn = $subscription->first_txn(); |
||||
| 209 | |||||
| 210 | if ( false === $first_txn || ! ( $first_txn instanceof MeprTransaction ) ) { |
||||
| 211 | $first_txn = new MeprTransaction(); |
||||
| 212 | $first_txn->user_id = $subscription->user_id; |
||||
| 213 | $first_txn->product_id = $subscription->product_id; |
||||
| 214 | $first_txn->coupon_id = $subscription->coupon_id; |
||||
| 215 | $first_txn->gateway = null; |
||||
| 216 | } |
||||
| 217 | |||||
| 218 | // Transaction number. |
||||
| 219 | $trans_num = $payment->get_transaction_id(); |
||||
| 220 | |||||
| 221 | if ( empty( $trans_num ) ) { |
||||
| 222 | $trans_num = uniqid(); |
||||
| 223 | } |
||||
| 224 | |||||
| 225 | // New transaction. |
||||
| 226 | $end_date = $payment->get_end_date(); |
||||
| 227 | |||||
| 228 | if ( null === $end_date ) { |
||||
| 229 | $periods = $payment->get_periods(); |
||||
| 230 | |||||
| 231 | if ( ! empty( $periods ) ) { |
||||
| 232 | $end_date = $periods[0]->get_end_date(); |
||||
| 233 | } |
||||
| 234 | } |
||||
| 235 | |||||
| 236 | $expires_at = MeprUtils::ts_to_mysql_date( $end_date->getTimestamp(), 'Y-m-d 23:59:59' ); |
||||
| 237 | |||||
| 238 | // Determine gateway (can be different from original, i.e. via mandate update). |
||||
| 239 | $old_gateway = $subscription->gateway; |
||||
| 240 | |||||
| 241 | $new_gateway = $old_gateway; |
||||
| 242 | |||||
| 243 | $mepr_options = MeprOptions::fetch(); |
||||
| 244 | |||||
| 245 | $mepr_gateways = $mepr_options->payment_methods(); |
||||
| 246 | |||||
| 247 | foreach ( $mepr_gateways as $mepr_gateway ) { |
||||
| 248 | // Check valid gateway. |
||||
| 249 | if ( ! ( $mepr_gateway instanceof \MeprBaseRealGateway ) ) { |
||||
| 250 | continue; |
||||
| 251 | } |
||||
| 252 | |||||
| 253 | // Check payment method. |
||||
| 254 | $payment_method = \str_replace( 'pronamic_pay_', '', $mepr_gateway->key ); |
||||
| 255 | |||||
| 256 | if ( $payment_method !== $payment->get_method() ) { |
||||
| 257 | continue; |
||||
| 258 | } |
||||
| 259 | |||||
| 260 | // Set gateway, as payment method matches with this gateway. |
||||
| 261 | $new_gateway = $mepr_gateway->id; |
||||
| 262 | |||||
| 263 | $subscription->gateway = $new_gateway; |
||||
| 264 | |||||
| 265 | $subscription->store(); |
||||
| 266 | |||||
| 267 | // Set expires at to subscription expiration date. |
||||
| 268 | $expires_at = $subscription->expires_at; |
||||
| 269 | } |
||||
| 270 | |||||
| 271 | $transaction = new MeprTransaction(); |
||||
| 272 | $transaction->created_at = $payment->post->post_date_gmt; |
||||
| 273 | $transaction->user_id = $first_txn->user_id; |
||||
| 274 | $transaction->product_id = $first_txn->product_id; |
||||
| 275 | $transaction->coupon_id = $first_txn->coupon_id; |
||||
| 276 | $transaction->gateway = $new_gateway; |
||||
| 277 | $transaction->trans_num = $trans_num; |
||||
| 278 | $transaction->txn_type = MeprTransaction::$payment_str; |
||||
| 279 | $transaction->status = MeprTransaction::$pending_str; |
||||
| 280 | $transaction->expires_at = $expires_at; |
||||
|
0 ignored issues
–
show
|
|||||
| 281 | $transaction->subscription_id = $subscription->id; |
||||
| 282 | |||||
| 283 | $transaction->set_gross( $payment->get_total_amount()->get_value() ); |
||||
| 284 | |||||
| 285 | $transaction->store(); |
||||
| 286 | |||||
| 287 | // Set source ID. |
||||
| 288 | $payment->set_meta( 'source_id', $transaction->id ); |
||||
| 289 | |||||
| 290 | $payment->source_id = $transaction->id; |
||||
| 291 | |||||
| 292 | if ( MeprSubscription::$active_str === $subscription->status && $old_gateway === $new_gateway ) { |
||||
| 293 | /* |
||||
| 294 | * We create a 'confirmed' 'subscription_confirmation' |
||||
| 295 | * transaction for a grace period of 15 days. |
||||
| 296 | * |
||||
| 297 | * Transactions of type "subscription_confirmation" with a |
||||
| 298 | * status of "confirmed" are hidden in the UI, and are used |
||||
| 299 | * as a way to provide free trial periods and the 24 hour |
||||
| 300 | * grace period on a recurring subscription signup. |
||||
| 301 | * |
||||
| 302 | * @link https://docs.memberpress.com/article/219-where-is-data-stored. |
||||
| 303 | */ |
||||
| 304 | $subscription_confirmation = new MeprTransaction(); |
||||
| 305 | $subscription_confirmation->created_at = $payment->post->post_date_gmt; |
||||
| 306 | $subscription_confirmation->user_id = $first_txn->user_id; |
||||
| 307 | $subscription_confirmation->product_id = $first_txn->product_id; |
||||
| 308 | $subscription_confirmation->coupon_id = $first_txn->coupon_id; |
||||
| 309 | $subscription_confirmation->gateway = $new_gateway; |
||||
| 310 | $subscription_confirmation->trans_num = $trans_num; |
||||
| 311 | $subscription_confirmation->txn_type = MeprTransaction::$subscription_confirmation_str; |
||||
| 312 | $subscription_confirmation->status = MeprTransaction::$confirmed_str; |
||||
| 313 | $subscription_confirmation->subscription_id = $subscription->id; |
||||
| 314 | $subscription_confirmation->expires_at = MeprUtils::ts_to_mysql_date( strtotime( $payment->post->post_date_gmt ) + MeprUtils::days( 15 ), 'Y-m-d 23:59:59' ); |
||||
| 315 | |||||
| 316 | $subscription_confirmation->set_subtotal( 0.00 ); |
||||
| 317 | |||||
| 318 | $subscription_confirmation->store(); |
||||
| 319 | } |
||||
| 320 | } |
||||
| 321 | } |
||||
| 322 | |||||
| 323 | $should_update = ! MemberPress::transaction_has_status( |
||||
| 324 | $transaction, |
||||
| 325 | array( |
||||
| 326 | MeprTransaction::$failed_str, |
||||
| 327 | MeprTransaction::$complete_str, |
||||
| 328 | ) |
||||
| 329 | ); |
||||
| 330 | |||||
| 331 | // Allow successful recurring payments to update failed transaction. |
||||
| 332 | if ( $payment->get_recurring() && PaymentStatus::SUCCESS === $payment->get_status() && MeprTransaction::$failed_str === $transaction->status ) { |
||||
| 333 | $should_update = true; |
||||
| 334 | } |
||||
| 335 | |||||
| 336 | if ( $should_update ) { |
||||
| 337 | $gateway = new Gateway(); |
||||
| 338 | |||||
| 339 | $gateway->pronamic_payment = $payment; |
||||
| 340 | $gateway->mp_txn = $transaction; |
||||
| 341 | |||||
| 342 | switch ( $payment->get_status() ) { |
||||
| 343 | case PaymentStatus::FAILURE: |
||||
| 344 | $gateway->record_payment_failure(); |
||||
| 345 | |||||
| 346 | // Set subscription 'On Hold' to prevent subsequent |
||||
| 347 | // successful subscriptions from awaking subscription. |
||||
| 348 | if ( ! $payment->get_recurring() ) { |
||||
|
0 ignored issues
–
show
The expression
$payment->get_recurring() of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.
If an expression can have both $a = canBeFalseAndNull();
// Instead of
if ( ! $a) { }
// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
|
|||||
| 349 | $subscription = $payment->get_subscription(); |
||||
| 350 | |||||
| 351 | if ( null !== $subscription ) { |
||||
| 352 | $subscription->set_status( SubscriptionStatus::ON_HOLD ); |
||||
| 353 | } |
||||
| 354 | } |
||||
| 355 | |||||
| 356 | break; |
||||
| 357 | case PaymentStatus::CANCELLED: |
||||
| 358 | case PaymentStatus::EXPIRED: |
||||
| 359 | $gateway->record_payment_failure(); |
||||
| 360 | |||||
| 361 | break; |
||||
| 362 | case PaymentStatus::SUCCESS: |
||||
| 363 | if ( $payment->get_recurring() ) { |
||||
| 364 | $gateway->record_subscription_payment(); |
||||
| 365 | } else { |
||||
| 366 | $gateway->record_payment(); |
||||
| 367 | } |
||||
| 368 | |||||
| 369 | break; |
||||
| 370 | case PaymentStatus::OPEN: |
||||
| 371 | default: |
||||
| 372 | break; |
||||
| 373 | } |
||||
| 374 | } |
||||
| 375 | } |
||||
| 376 | |||||
| 377 | /** |
||||
| 378 | * Subscription deleted. |
||||
| 379 | * |
||||
| 380 | * @param int $subscription_id MemberPress subscription id. |
||||
| 381 | */ |
||||
| 382 | public function subscription_pre_delete( $subscription_id ) { |
||||
| 383 | $subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $subscription_id ); |
||||
| 384 | |||||
| 385 | if ( ! $subscription ) { |
||||
| 386 | return; |
||||
| 387 | } |
||||
| 388 | |||||
| 389 | // Add note. |
||||
| 390 | $note = sprintf( |
||||
| 391 | /* translators: %s: MemberPress */ |
||||
| 392 | __( '%s subscription deleted.', 'pronamic_ideal' ), |
||||
| 393 | __( 'MemberPress', 'pronamic_ideal' ) |
||||
| 394 | ); |
||||
| 395 | |||||
| 396 | $subscription->add_note( $note ); |
||||
| 397 | |||||
| 398 | // The status of canceled or completed subscriptions will not be changed automatically. |
||||
| 399 | if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) { |
||||
| 400 | $subscription->set_status( SubscriptionStatus::CANCELLED ); |
||||
| 401 | |||||
| 402 | $subscription->save(); |
||||
| 403 | } |
||||
| 404 | } |
||||
| 405 | |||||
| 406 | /** |
||||
| 407 | * Subscription email parameters. |
||||
| 408 | * |
||||
| 409 | * @param array $params Email parameters. |
||||
| 410 | * @param MeprSubscription $memberpress_subscription MemberPress subscription. |
||||
| 411 | * @return array |
||||
| 412 | */ |
||||
| 413 | public function subscription_email_params( $params, MeprSubscription $memberpress_subscription ) { |
||||
| 414 | $subscriptions = \get_pronamic_subscriptions_by_source( 'memberpress', $memberpress_subscription->id ); |
||||
| 415 | |||||
| 416 | if ( empty( $subscriptions ) ) { |
||||
| 417 | return $params; |
||||
| 418 | } |
||||
| 419 | |||||
| 420 | $subscription = reset( $subscriptions ); |
||||
| 421 | |||||
| 422 | // Add parameters. |
||||
| 423 | $next_payment_date = $subscription->get_next_payment_date(); |
||||
| 424 | |||||
| 425 | return \array_merge( |
||||
| 426 | $params, |
||||
| 427 | array( |
||||
| 428 | 'pronamic_subscription_id' => $subscription->get_id(), |
||||
| 429 | 'pronamic_subscription_cancel_url' => $subscription->get_cancel_url(), |
||||
| 430 | 'pronamic_subscription_renewal_url' => $subscription->get_renewal_url(), |
||||
| 431 | '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
Loading history...
|
|||||
| 432 | ) |
||||
| 433 | ); |
||||
| 434 | } |
||||
| 435 | |||||
| 436 | /** |
||||
| 437 | * Transaction email parameters. |
||||
| 438 | * |
||||
| 439 | * @param array $params Parameters. |
||||
| 440 | * @param MeprTransaction $transaction MemberPress transaction. |
||||
| 441 | * @return array |
||||
| 442 | */ |
||||
| 443 | public function transaction_email_params( $params, MeprTransaction $transaction ) { |
||||
| 444 | $payments = \get_pronamic_payments_by_source( 'memberpress', $transaction->id ); |
||||
| 445 | |||||
| 446 | if ( null === $payments ) { |
||||
| 447 | return $params; |
||||
| 448 | } |
||||
| 449 | |||||
| 450 | $payment = \reset( $payments ); |
||||
| 451 | |||||
| 452 | if ( false === $payment ) { |
||||
| 453 | return $params; |
||||
| 454 | } |
||||
| 455 | |||||
| 456 | // Get subscription. |
||||
| 457 | $periods = $payment->get_periods(); |
||||
| 458 | |||||
| 459 | if ( null === $periods ) { |
||||
| 460 | return $params; |
||||
| 461 | } |
||||
| 462 | |||||
| 463 | $period = \reset( $periods ); |
||||
| 464 | |||||
| 465 | if ( false === $period ) { |
||||
| 466 | return $params; |
||||
| 467 | } |
||||
| 468 | |||||
| 469 | $subscription = $period->get_phase()->get_subscription(); |
||||
| 470 | |||||
| 471 | // Add parameters. |
||||
| 472 | $memberpress_subscription = new MeprSubscription( $subscription->get_source_id() ); |
||||
| 473 | |||||
| 474 | return $this->subscription_email_params( $params, $memberpress_subscription ); |
||||
| 475 | } |
||||
| 476 | |||||
| 477 | /** |
||||
| 478 | * Source text. |
||||
| 479 | * |
||||
| 480 | * @param string $text Source text. |
||||
| 481 | * @param Payment $payment Payment to create the source text for. |
||||
| 482 | * |
||||
| 483 | * @return string |
||||
| 484 | */ |
||||
| 485 | public function source_text( $text, Payment $payment ) { |
||||
| 486 | $text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />'; |
||||
| 487 | |||||
| 488 | $text .= sprintf( |
||||
| 489 | '<a href="%s">%s</a>', |
||||
| 490 | add_query_arg( |
||||
| 491 | array( |
||||
| 492 | 'page' => 'memberpress-trans', |
||||
| 493 | 'action' => 'edit', |
||||
| 494 | 'id' => $payment->source_id, |
||||
| 495 | ), |
||||
| 496 | admin_url( 'admin.php' ) |
||||
| 497 | ), |
||||
| 498 | /* translators: %s: payment source id */ |
||||
| 499 | sprintf( __( 'Transaction %s', 'pronamic_ideal' ), $payment->source_id ) |
||||
| 500 | ); |
||||
| 501 | |||||
| 502 | return $text; |
||||
| 503 | } |
||||
| 504 | |||||
| 505 | /** |
||||
| 506 | * Subscription source text. |
||||
| 507 | * |
||||
| 508 | * @param string $text Source text. |
||||
| 509 | * @param Subscription $subscription Subscription to create the source text for. |
||||
| 510 | * |
||||
| 511 | * @return string |
||||
| 512 | */ |
||||
| 513 | public function subscription_source_text( $text, Subscription $subscription ) { |
||||
| 514 | $text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />'; |
||||
| 515 | |||||
| 516 | $text .= sprintf( |
||||
| 517 | '<a href="%s">%s</a>', |
||||
| 518 | add_query_arg( |
||||
| 519 | array( |
||||
| 520 | 'page' => 'memberpress-subscriptions', |
||||
| 521 | 'subscription' => $subscription->source_id, |
||||
| 522 | ), |
||||
| 523 | admin_url( 'admin.php' ) |
||||
| 524 | ), |
||||
| 525 | /* translators: %s: payment source id */ |
||||
| 526 | sprintf( __( 'Subscription %s', 'pronamic_ideal' ), $subscription->source_id ) |
||||
| 527 | ); |
||||
| 528 | |||||
| 529 | return $text; |
||||
| 530 | } |
||||
| 531 | |||||
| 532 | /** |
||||
| 533 | * Source description. |
||||
| 534 | * |
||||
| 535 | * @param string $description Description. |
||||
| 536 | * @param Payment $payment Payment to create the description for. |
||||
| 537 | * |
||||
| 538 | * @return string |
||||
| 539 | */ |
||||
| 540 | public function source_description( $description, Payment $payment ) { |
||||
| 541 | return __( 'MemberPress Transaction', 'pronamic_ideal' ); |
||||
| 542 | } |
||||
| 543 | |||||
| 544 | /** |
||||
| 545 | * Subscription source description. |
||||
| 546 | * |
||||
| 547 | * @param string $description Description. |
||||
| 548 | * @param Subscription $subscription Subscription to create the description for. |
||||
| 549 | * |
||||
| 550 | * @return string |
||||
| 551 | */ |
||||
| 552 | public function subscription_source_description( $description, Subscription $subscription ) { |
||||
| 553 | return __( 'MemberPress Subscription', 'pronamic_ideal' ); |
||||
| 554 | } |
||||
| 555 | |||||
| 556 | /** |
||||
| 557 | * Source URL. |
||||
| 558 | * |
||||
| 559 | * @param string $url URL. |
||||
| 560 | * @param Payment $payment The payment to create the source URL for. |
||||
| 561 | * |
||||
| 562 | * @return string |
||||
| 563 | */ |
||||
| 564 | public function source_url( $url, Payment $payment ) { |
||||
| 565 | $url = add_query_arg( |
||||
| 566 | array( |
||||
| 567 | 'page' => 'memberpress-trans', |
||||
| 568 | 'action' => 'edit', |
||||
| 569 | 'id' => $payment->source_id, |
||||
| 570 | ), |
||||
| 571 | admin_url( 'admin.php' ) |
||||
| 572 | ); |
||||
| 573 | |||||
| 574 | return $url; |
||||
| 575 | } |
||||
| 576 | |||||
| 577 | /** |
||||
| 578 | * Subscription source URL. |
||||
| 579 | * |
||||
| 580 | * @param string $url URL. |
||||
| 581 | * @param Subscription $subscription Subscription. |
||||
| 582 | * |
||||
| 583 | * @return string |
||||
| 584 | */ |
||||
| 585 | public function subscription_source_url( $url, Subscription $subscription ) { |
||||
| 586 | $url = add_query_arg( |
||||
| 587 | array( |
||||
| 588 | 'page' => 'memberpress-subscriptions', |
||||
| 589 | 'subscription' => $subscription->source_id, |
||||
| 590 | ), |
||||
| 591 | admin_url( 'admin.php' ) |
||||
| 592 | ); |
||||
| 593 | |||||
| 594 | return $url; |
||||
| 595 | } |
||||
| 596 | |||||
| 597 | /** |
||||
| 598 | * MemberPress update subscription. |
||||
| 599 | * |
||||
| 600 | * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprSubscriptionsCtrl.php#L92-L111 |
||||
| 601 | * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L100-L123 |
||||
| 602 | * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L112 |
||||
| 603 | * |
||||
| 604 | * @param string $status_old Old status identifier. |
||||
| 605 | * @param string $status_new New status identifier. |
||||
| 606 | * @param MeprSubscription $memberpress_subscription MemberPress subscription object. |
||||
| 607 | */ |
||||
| 608 | public function memberpress_subscription_transition_status( $status_old, $status_new, $memberpress_subscription ) { |
||||
| 609 | $subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $memberpress_subscription->id ); |
||||
| 610 | |||||
| 611 | if ( empty( $subscription ) ) { |
||||
| 612 | return; |
||||
| 613 | } |
||||
| 614 | |||||
| 615 | $status = SubscriptionStatuses::transform( $status_new ); |
||||
| 616 | |||||
| 617 | if ( null === $status ) { |
||||
| 618 | return; |
||||
| 619 | } |
||||
| 620 | |||||
| 621 | $subscription->set_status( $status ); |
||||
| 622 | |||||
| 623 | $subscription->save(); |
||||
| 624 | } |
||||
| 625 | } |
||||
| 626 |