Skip to content

Commit

Permalink
fix: use my-renewals endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Nov 14, 2024
1 parent 80e4d44 commit eabb518
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 60 deletions.
126 changes: 70 additions & 56 deletions includes/plugins/class-woocommerce-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,101 +15,115 @@
*/
class WooCommerce_Subscriptions {
/**
* Renewal URL query parameter.
* Renewal endpoint.
*
* @var string
*/
const RENEWAL_QUERY_PARAM = 'np_renewal';
const RENEWALS_ENDPOINT = 'my-renewals';

/**
* Initialize hooks and filters.
*/
public static function init() {
add_action( 'init', [ __CLASS__, 'maybe_redirect_to_renewals' ] );
add_action( 'init', [ __CLASS__, 'add_renewals_endpoint' ] );
if ( ! is_admin() ) {
add_filter( 'woocommerce_get_query_vars', [ __CLASS__, 'add_renewals_query_var' ] );
add_filter( 'pre_get_posts', [ __CLASS__, 'maybe_redirect_renewals_endpoint' ] );
}
}

/**
* Get my account subscriptions url.
* Add renewals endpoint.
*/
public static function add_renewals_endpoint() {
if ( self::is_active() ) {
add_rewrite_endpoint( self::RENEWALS_ENDPOINT, EP_ROOT | EP_PAGES );
}
}

/**
* Add renewals query var.
*
* @param bool $add_renewal_param Whether to add the renewal query parameter. Default false.
* @param array $query_vars Query vars.
*
* @return string My account subscriptions URL.
* @return array
*/
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
);
public static function add_renewals_query_var( $query_vars ) {
if ( self::is_active() ) {
$query_vars[ self::RENEWALS_ENDPOINT ] = self::RENEWALS_ENDPOINT;
}
return $url;
return $query_vars;
}

/**
* Determine whether WC and WC Subscriptions are active.
* Get the URL for the My Account > Subscriptions page.
*
* @return string
*/
public static function get_subscriptions_url() {
return wc_get_account_endpoint_url( 'subscriptions' );
}

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

/**
* Whether the request is a renewal request.
*
* @param bool $logged_in_only Whether to check for logged out renewal param value. Default false.
* Returns true when on the My Account > Subscriptions front end page.
*
* @return bool True if the request is a renewal request.
* @return bool
*/
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 ) {
public static function is_subscriptions_page() {
if ( ! self::is_active() ) {
return false;
}
if ( $logged_in_only ) {
return ! is_numeric( $np_renewal ) || 1 === (int) $np_renewal;
}
return true;
return is_wc_endpoint_url( 'subscriptions' );
}

/**
* Redirect to subscriptions pending renewals my account page.
* Conditionally redirects the renewals endpoint url.
*
* @param \WP_Query $query Query object.
*/
public static function maybe_redirect_to_renewals() {
if ( ! self::is_active() || ! self::is_renewal_request( true ) ) {
public static function maybe_redirect_renewals_endpoint( $query ) { // phpcs:ignore WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.VoidReturn, WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.MissingReturnStatement
if (
! self::is_active() ||
! $query->is_main_query() ||
! isset( $query->query_vars[ self::RENEWALS_ENDPOINT ] )
) {
return;
}
$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();
$redirect_url = wc_get_account_endpoint_url( 'dashboard' );
if ( self::is_active() ) {
$redirect_url = wc_get_account_endpoint_url( 'subscriptions' );
if ( is_user_logged_in() ) {
$pending_renewals = wcs_get_subscriptions(
[
'customer_id' => get_current_user_id(),
'subscription_status' => [
'pending',
'on-hold',
],
]
);
if ( count( $pending_renewals ) === 1 ) {
$orders = $pending_renewals[0]->get_related_orders( 'all', 'renewal' );
foreach ( $orders as $order ) {
if ( $order->needs_payment() ) {
$redirect_url = $order->get_checkout_payment_url();
break;
}
}
}
}
}
if ( $redirect_url ) {
wp_safe_redirect( $redirect_url );
exit;
}
wp_safe_redirect( $redirect_url );
exit();
}
}
WooCommerce_Subscriptions::init();
2 changes: 1 addition & 1 deletion includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ public static function render_auth_form( $is_inline = false ) {
$is_account_page = function_exists( '\wc_get_page_id' ) ? \get_the_ID() === \wc_get_page_id( 'myaccount' ) : false;
$referer = \wp_parse_url( \wp_get_referer() );
$redirect = '';
if ( WooCommerce_Subscriptions::is_renewal_request() ) {
if ( WooCommerce_Subscriptions::is_subscriptions_page() ) {
$redirect = WooCommerce_Subscriptions::get_subscriptions_url();
} elseif ( $is_account_page ) {
$redirect = \wc_get_account_endpoint_url( 'dashboard' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,6 @@ public static function is_user_verified() {
public static function redirect_to_account_details() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$is_resubscribe_request = isset( $_REQUEST['resubscribe'] ) ? 'shop_subscription' === \get_post_type( absint( $_REQUEST['resubscribe'] ) ) : false;
$is_renewal_request = isset( $_REQUEST['subscription_renewal'] ) ? true : false;
$is_cancel_membership_request = isset( $_REQUEST['cancel_membership'] ) ? true : false;
$is_checkout_request = isset( $_REQUEST['my_account_checkout'] ) ? true : false;
// phpcs:enable WordPress.Security.NonceVerification.Recommended
Expand All @@ -377,10 +376,9 @@ public static function redirect_to_account_details() {
Reader_Activation::is_enabled() &&
function_exists( 'wc_get_page_permalink' ) &&
! $is_resubscribe_request &&
! $is_renewal_request &&
! $is_cancel_membership_request &&
! $is_checkout_request
) {
) {
global $wp;
$current_url = \home_url( $wp->request );
$my_account_page_permalink = \wc_get_page_permalink( 'myaccount' );
Expand Down

0 comments on commit eabb518

Please sign in to comment.