Skip to content

Commit

Permalink
fix: allow login then redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Nov 6, 2024
1 parent 624b5ce commit 3492424
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 41 deletions.
111 changes: 73 additions & 38 deletions includes/plugins/class-woocommerce-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,102 @@
* Main class.
*/
class WooCommerce_Subscriptions {
/**
* Renewal URL query parameter.
*/
const RENEWAL_QUERY_PARAM = 'np_renewal';

/**
* Initialize hooks and filters.
*/
public static function init() {
add_action( 'init', [ __CLASS__, 'maybe_redirect_to_checkout_payment_page' ] );
add_action( 'init', [ __CLASS__, 'maybe_redirect_to_renewals' ] );
}

/**
* Determine whether WC Subscriptions is active.
* Get my account subscriptions url.
*
* @param bool $add_renewal_param Whether to add the renewal query parameter. Default false.
*
* @return string My account subscriptions URL.
*/
public static function get_subscriptions_url( $add_renewal_param = false ) {
$url = wc_get_account_endpoint_url( 'subscriptions' );
if ( $add_renewal_param ) {
$url = add_query_arg(
[
self::RENEWAL_QUERY_PARAM => is_user_logged_in() ? 1 : 0,
],
$url
);
}
return $url;
}

/**
* Determine whether WC and WC Subscriptions are active.
*
* @return bool
*/
public static function is_active() {
return class_exists( 'WC_Subscriptions' );
return function_exists( 'WC' ) && class_exists( 'WC_Subscriptions' );
}

/**
* Redirect to subscriptions pending renewals my account page.
* Whether the request is a renewal request.
*
* @param bool $logged_in_only Whether to check for logged in renewal param value. Default false.
*
* @return bool True if the request is a renewal request.
*/
public static function maybe_redirect_to_checkout_payment_page() {
if ( ! self::is_active() || ! isset( $_GET['np_renewal'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return;
public static function is_renewal_request( $logged_in_only = false ) {
$np_renewal = filter_input( INPUT_GET, self::RENEWAL_QUERY_PARAM, FILTER_SANITIZE_NUMBER_INT );
if ( null === $np_renewal ) {
return false;
}

if ( ! is_user_logged_in() ) {
if ( ! is_account_page() ) {
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
exit;
}
return;
if ( $logged_in_only ) {
return ! is_numeric( $np_renewal ) || 1 === (int) $np_renewal;
}
return true;
}

$user_id = get_current_user_id();
if ( ! wcs_user_has_subscription( $user_id ) ) {
/**
* Redirect to subscriptions pending renewals my account page.
*/
public static function maybe_redirect_to_renewals() {
if ( ! self::is_active() || ! self::is_renewal_request( true ) ) {
return;
}
$pending_renewals = [];
$subscriptions = wcs_get_subscriptions(
[
'customer_id' => $user_id,
'subscription_status' => [ 'pending', 'on-hold' ],
]
);
foreach ( $subscriptions as $subscription ) {
if ( $subscription->needs_payment() ) {
$pending_renewals[] = $subscription;
}
}
foreach ( $pending_renewals as $subscription ) {
if ( count( $pending_renewals ) > 1 ) {
wp_safe_redirect( wc_get_account_endpoint_url( 'subscriptions' ) );
exit;
}
$renewal_orders = $subscription->get_related_orders( 'all', 'renewal' );
foreach ( $renewal_orders as $renewal_order ) {
if ( $renewal_order->needs_payment() ) {
wp_safe_redirect( $renewal_order->get_checkout_payment_url() );
exit;
$redirect_url = self::get_subscriptions_url( ! is_user_logged_in() );
if ( is_user_logged_in() ) {
$subscriptions = wcs_get_subscriptions(
[
'customer_id' => get_current_user_id(),
'subscription_status' => [
'pending',
'on-hold',
],
]
);
if ( empty( $subscriptions ) ) {
// Reset redirect url if there are no pending or on-hold subscriptions.
$redirect_url = '';
} elseif ( count( $subscriptions ) === 1 ) {
foreach ( $subscriptions as $subscription ) {
$renewal_orders = $subscription->get_related_orders( 'all', 'renewal' );
foreach ( $renewal_orders as $renewal_order ) {
if ( $renewal_order->needs_payment() ) {
$redirect_url = $renewal_order->get_checkout_payment_url();
break;
}
}
}
}
}
if ( $redirect_url ) {
wp_safe_redirect( $redirect_url );
exit;
}
}
}
WooCommerce_Subscriptions::init();
7 changes: 6 additions & 1 deletion includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,13 @@ public static function render_auth_form( $is_inline = false ) {
$terms_text = self::get_setting( 'terms_text' );
$terms_url = self::get_setting( 'terms_url' );
$is_account_page = function_exists( '\wc_get_page_id' ) ? \get_the_ID() === \wc_get_page_id( 'myaccount' ) : false;
$redirect = $is_account_page ? \wc_get_account_endpoint_url( 'dashboard' ) : '';
$referer = \wp_parse_url( \wp_get_referer() );
$redirect = '';
if ( WooCommerce_Subscriptions::is_renewal_request() ) {
$redirect = WooCommerce_Subscriptions::get_subscriptions_url();
} elseif ( $is_account_page ) {
$redirect = \wc_get_account_endpoint_url( 'dashboard' );
}
global $wp;
?>
<div class="<?php echo \esc_attr( implode( ' ', $classnames ) ); ?>" data-labels="<?php echo \esc_attr( htmlspecialchars( \wp_json_encode( $labels ), ENT_QUOTES, 'UTF-8' ) ); ?>">
Expand Down
6 changes: 4 additions & 2 deletions src/reader-activation/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ window.newspackRAS.push( function ( readerActivation ) {
/** If there's a pre-auth, signing in redirects to the reader account. */
if ( reader?.email && ! reader?.authenticated ) {
link.setAttribute( 'data-redirect', link.getAttribute( 'href' ) );
redirectInput.value = link.getAttribute( 'href' );
if ( ! redirectInput?.value ) {
redirectInput.value = link.getAttribute( 'href' );
}
} else {
link.removeAttribute( 'data-redirect' );
}
Expand Down Expand Up @@ -177,7 +179,7 @@ window.newspackRAS.push( function ( readerActivation ) {
emailInput.value = reader?.email || '';
}

if ( redirectInput && ev?.target?.getAttribute( 'data-redirect' ) ) {
if ( ev?.target?.getAttribute( 'data-redirect' ) && ! redirectInput?.value ) {
redirectInput.value = ev.target.getAttribute( 'data-redirect' );
}

Expand Down

0 comments on commit 3492424

Please sign in to comment.