Skip to content

Commit

Permalink
Update RCP membership status on subscription status update (fix #13).
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Mar 12, 2024
1 parent 72bea74 commit 7fe6938
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
73 changes: 53 additions & 20 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Pronamic\WordPress\Pay\Payments\PaymentStatus as Core_PaymentStatus;
use Pronamic\WordPress\Pay\Payments\Payment;
use Pronamic\WordPress\Pay\Subscriptions\Subscription;
use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus;
use RCP_Membership;
use RCP_Payments;
use WP_Query;
Expand Down Expand Up @@ -70,17 +71,17 @@ public function __construct( $args = [] ) {
* @return void
*/
public function setup() {
add_filter( 'pronamic_payment_source_description', [ $this, 'payment_source_description' ], 10, 2 );
add_filter( 'pronamic_payment_source_url', [ $this, 'payment_source_url' ], 10, 2 );
add_filter( 'pronamic_subscription_source_description', [ $this, 'subscription_source_description' ], 10, 2 );
add_filter( 'pronamic_subscription_source_url', [ $this, 'subscription_source_url' ], 10, 2 );
\add_filter( 'pronamic_payment_source_description', [ $this, 'payment_source_description' ], 10, 2 );
\add_filter( 'pronamic_payment_source_url', [ $this, 'payment_source_url' ], 10, 2 );
\add_filter( 'pronamic_subscription_source_description', [ $this, 'subscription_source_description' ], 10, 2 );
\add_filter( 'pronamic_subscription_source_url', [ $this, 'subscription_source_url' ], 10, 2 );

/*
* The Restrict Content Pro plugin gets bootstrapped with priority `4`.
*
* @link https://gitlab.com/pronamic-plugins/restrict-content-pro/-/blob/3.3.3/restrict-content-pro.php#L119
*/
add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ], 5 );
\add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ], 5 );
}

/**
Expand All @@ -101,25 +102,26 @@ public function plugins_loaded() {
* @link https://gitlab.com/pronamic-plugins/restrict-content-pro/blob/3.2.3/includes/class-restrict-content-pro.php#L199-215
* @link https://github.com/wp-pay/core/blob/2.2.0/src/Admin/Install.php#L65
*/
add_action( 'admin_init', [ $this, 'admin_init_upgrades_executable' ], 4 );
\add_action( 'admin_init', [ $this, 'admin_init_upgrades_executable' ], 4 );

add_filter( 'rcp_payment_gateways', [ $this, 'register_pronamic_gateways' ] );
add_action( 'rcp_payments_settings', [ $this, 'payments_settings' ] );
add_action( 'rcp_transition_membership_status', [ $this, 'rcp_transition_membership_status' ], 10, 3 );
\add_filter( 'rcp_payment_gateways', [ $this, 'register_pronamic_gateways' ] );
\add_action( 'rcp_payments_settings', [ $this, 'payments_settings' ] );
\add_action( 'rcp_transition_membership_status', [ $this, 'rcp_transition_membership_status' ], 10, 3 );

add_filter( 'rcp_membership_can_cancel', [ $this, 'rcp_membership_can_cancel' ], 10, 3 );
add_filter( 'rcp_membership_payment_profile_cancelled', [ $this, 'rcp_membership_payment_profile_cancelled' ], 10, 5 );
\add_filter( 'rcp_membership_can_cancel', [ $this, 'rcp_membership_can_cancel' ], 10, 3 );
\add_filter( 'rcp_membership_payment_profile_cancelled', [ $this, 'rcp_membership_payment_profile_cancelled' ], 10, 5 );

add_action( 'pronamic_payment_status_update_rcp_payment', [ $this, 'payment_status_update' ], 10, 1 );
add_filter( 'pronamic_payment_redirect_url', [ $this, 'payment_redirect_url' ], 10, 2 );
add_filter( 'pronamic_payment_source_text_rcp_payment', [ $this, 'payment_source_text' ], 10, 2 );
\add_action( 'pronamic_payment_status_update_rcp_payment', [ $this, 'payment_status_update' ], 10, 1 );
\add_filter( 'pronamic_payment_redirect_url', [ $this, 'payment_redirect_url' ], 10, 2 );
\add_filter( 'pronamic_payment_source_text_rcp_payment', [ $this, 'payment_source_text' ], 10, 2 );

add_filter( 'pronamic_subscription_source_text_rcp_membership', [ $this, 'subscription_source_text' ], 10, 2 );
\add_action( 'pronamic_subscription_status_update_rcp_membership', [ $this, 'subscription_status_update' ], 10, 1 );
\add_filter( 'pronamic_subscription_source_text_rcp_membership', [ $this, 'subscription_source_text' ], 10, 2 );

add_action( 'pronamic_pay_new_payment', [ $this, 'new_payment' ] );
\add_action( 'pronamic_pay_new_payment', [ $this, 'new_payment' ] );

add_action( 'rcp_edit_membership_after', [ $this, 'rcp_edit_membership_after' ] );
add_action( 'rcp_edit_payment_after', [ $this, 'rcp_edit_payment_after' ] );
\add_action( 'rcp_edit_membership_after', [ $this, 'rcp_edit_membership_after' ] );
\add_action( 'rcp_edit_payment_after', [ $this, 'rcp_edit_payment_after' ] );

/**
* Filter the subscription next payment delivery date.
Expand All @@ -143,7 +145,7 @@ private function are_upgrades_executable() {
*
* @link https://gitlab.com/pronamic-plugins/restrict-content-pro/blob/3.2.3/includes/class-restrict-content-pro.php#L199-215
*/
if ( ! is_admin() ) {
if ( ! \is_admin() ) {
throw new \Exception( 'Can not run `are_upgrades_executable` function outside the WordPress admin environment.' );
}

Expand Down Expand Up @@ -385,7 +387,7 @@ public function payment_status_update( Payment $payment ) {
$rcp_payments->update( $rcp_payment_id, $rcp_payment_data );

// Renew membership if not active.
$rcp_membership = rcp_get_membership( $rcp_payment->membership_id );
$rcp_membership = \rcp_get_membership( $rcp_payment->membership_id );

if ( MembershipStatus::ACTIVE !== $rcp_membership->get_status() ) {
$expiration = '';
Expand Down Expand Up @@ -414,6 +416,37 @@ public function payment_status_update( Payment $payment ) {
}
}

/**
* Update RCP membership status on subscription status update.
*
* @param Subscription $pronamic_subscription Subscription.
* @return void
*/
public function subscription_status_update( Subscription $pronamic_subscription ) {
// Check status.

This comment has been minimized.

Copy link
@remcotolsma

remcotolsma Mar 12, 2024

Member

This comment has no added value?

This comment has been minimized.

Copy link
@rvdsteege

rvdsteege Mar 13, 2024

Author Member

Removed in f2abf33.

if ( SubscriptionStatus::COMPLETED === $pronamic_subscription->get_status() ) {
return;

This comment has been minimized.

Copy link
@remcotolsma

remcotolsma Mar 12, 2024

Member

It's not directly clear why we don't update status in this case? Should we add a comment?

This comment has been minimized.

Copy link
@rvdsteege

rvdsteege Mar 13, 2024

Author Member

Removed and documented in 43a2db0.

}

// Get Restrict Content Pro membership.

This comment has been minimized.

Copy link
@remcotolsma

remcotolsma Mar 12, 2024

Member

This comment has no added value?

This comment has been minimized.

Copy link
@rvdsteege

rvdsteege Mar 13, 2024

Author Member

Removed in f2abf33.

$membership_id = $pronamic_subscription->get_source_id();

$rcp_membership = \rcp_get_membership( $membership_id );

if ( false === $rcp_membership ) {
return;
}

// Set status.

This comment has been minimized.

Copy link
@remcotolsma

remcotolsma Mar 12, 2024

Member

This comment has no added value?

This comment has been minimized.

Copy link
@rvdsteege

rvdsteege Mar 13, 2024

Author Member

Removed in f2abf33.

$status = MembershipStatus::transform_from_pronamic( $pronamic_subscription->get_status() );

if ( null === $status ) {
return;
}

$rcp_membership->set_status( $status );
}

/**
* Restrict Content Pro transition membership status.
*
Expand Down
29 changes: 29 additions & 0 deletions src/MembershipStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,33 @@ public static function to_core_subscription_status( $rcp_status ) {
return null;
}
}

/**
* Transform a Restrict Content Pro subscription status to a WordPress Pay subscription status.
*
* @link https://github.com/wp-premium/memberpress-basic/blob/master/app/models/MeprSubscription.php#L5-L9
*
* @param string $status WordPress Pay status value.
* @return string|null
*/
public static function transform_from_pronamic( $status ) {
switch ( $status ) {
case SubscriptionStatus::ACTIVE:
return self::ACTIVE;
case SubscriptionStatus::CANCELLED:
return self::CANCELLED;
case SubscriptionStatus::EXPIRED:
return self::EXPIRED;
case SubscriptionStatus::FAILURE:
case SubscriptionStatus::ON_HOLD:
case SubscriptionStatus::OPEN:
return self::PENDING;
case SubscriptionStatus::COMPLETED:
return null;
default:
return null;
}

return null;
}
}

0 comments on commit 7fe6938

Please sign in to comment.