Skip to content

Commit

Permalink
fix: reorganize and add flush permalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Nov 27, 2024
1 parent f4069a6 commit dd06a16
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 131 deletions.
126 changes: 0 additions & 126 deletions includes/plugins/class-woocommerce-subscriptions.php

This file was deleted.

125 changes: 125 additions & 0 deletions includes/plugins/woocommerce-subscriptions/class-renewal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* WooCommerce Subscriptions renew class.
*
* @package Newspack
*/

namespace Newspack;

defined( 'ABSPATH' ) || exit;

/**
* Main class.
*/
class Renewal {
/**
* Renewal endpoint.
*
* @var string
*/
const RENEWAL_ENDPOINT = 'renew-subscription';

/**
* Initialize hooks and filters.
*/
public static function init() {
if ( ! WooCommerce_Subscriptions::is_active() ) {
return;
}

add_action( 'init', [ __CLASS__, 'add_renewal_endpoint' ] );
if ( ! is_admin() ) {
add_filter( 'woocommerce_get_query_vars', [ __CLASS__, 'add_renewal_query_var' ] );
add_filter( 'pre_get_posts', [ __CLASS__, 'maybe_redirect_renewal_endpoint' ] );
}
}

/**
* Add renew endpoint.
*/
public static function add_renewal_endpoint() {
add_rewrite_endpoint( self::RENEWAL_ENDPOINT, EP_ROOT | EP_PAGES );
self::flush_rewrite_rules();
}

/**
* Add renewal query var.
*
* @param array $query_vars Query vars.
*
* @return array
*/
public static function add_renewal_query_var( $query_vars ) {
$query_vars[ self::RENEWAL_ENDPOINT ] = self::RENEWAL_ENDPOINT;
return $query_vars;
}

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

/**
* Returns true when on the My Account > Subscriptions front end page.
*
* @return bool
*/
public static function is_subscriptions_page() {
if ( ! WooCommerce_Subscriptions::is_active() ) {
return false;
}
return is_wc_endpoint_url( 'subscriptions' );
}

/**
* Conditionally redirects the renew endpoint url.
*
* @param \WP_Query $query Query object.
*/
public static function maybe_redirect_renewal_endpoint( $query ) { // phpcs:ignore WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.VoidReturn, WordPressVIPMinimum.Hooks.AlwaysReturnInFilter.MissingReturnStatement
if (
! $query->is_main_query() ||
! isset( $query->query_vars[ self::RENEWAL_ENDPOINT ] )
) {
return;
}
$redirect_url = wc_get_account_endpoint_url( 'subscriptions' );
if ( is_user_logged_in() ) {
$pending_renew = wcs_get_subscriptions(
[
'customer_id' => get_current_user_id(),
'subscription_status' => [
'pending',
'on-hold',
],
]
);
if ( count( $pending_renew ) === 1 ) {
$orders = array_pop( $pending_renew )->get_related_orders( 'all', 'renewal' );
foreach ( $orders as $order ) {
if ( $order->needs_payment() ) {
$redirect_url = $order->get_checkout_payment_url();
break;
}
}
}
}
wp_safe_redirect( $redirect_url );
exit();
}

/**
* Refresh permalinks the first time this feature is enabled.
*/
private static function flush_rewrite_rules() {
if ( ! get_option( 'newspack_subscriptions_renewal_permalinks_refreshed', false ) ) {
flush_rewrite_rules(); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules
update_option( 'newspack_subscriptions_renewal_permalinks_refreshed', true );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@ class WooCommerce_Subscriptions {
* Initialize hooks and filters.
*/
public static function init() {
add_action( 'plugins_loaded', [ __CLASS__, 'woocommerce_subscriptions_integration_init' ] );
}

/**
* Initialize WooCommerce Subscriptions Integration.
*/
public static function woocommerce_subscriptions_integration_init() {
// To be included only if WooCommerce Subscriptions Integration is enabled.
// See is_enabled() method.
if ( self::is_enabled() ) {
include_once __DIR__ . '/class-on-hold-duration.php';

On_Hold_Duration::init();
}

include_once __DIR__ . '/class-renewal.php';
Renewal::init();
}


/**
* Check if WooCommerce Subscriptions is active.
*
Expand All @@ -36,12 +48,15 @@ public static function is_active() {
/**
* Check if WooCommerce Subscriptions Integration is enabled.
*
* Enalbed if Reader activation is enabled and the feature flag is defined.
* True if:
* - WooCommerce Subscriptions is active and,
* - Reader Activation is enabled and,
* - The NEWSPACK_SUBSCRIPTIONS_EXPIRATION feature flag is defined
*
* @return bool
*/
public static function is_enabled() {
return Reader_Activation::is_enabled() && defined( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION' ) && NEWSPACK_SUBSCRIPTIONS_EXPIRATION;
return self::is_active() && Reader_Activation::is_enabled() && defined( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION' ) && NEWSPACK_SUBSCRIPTIONS_EXPIRATION;
}
}
WooCommerce_Subscriptions::init();
5 changes: 3 additions & 2 deletions includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Newspack\Recaptcha;
use Newspack\Reader_Activation\Sync;
use Newspack\Renewal;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -1143,8 +1144,8 @@ public static function render_auth_form( $is_inline = false ) {
$referer = \wp_parse_url( \wp_get_referer() );
$redirect = '';

if ( WooCommerce_Subscriptions::is_subscriptions_page() ) {
$redirect = WooCommerce_Subscriptions::get_subscriptions_url();
if ( Renewal::is_subscriptions_page() ) {
$redirect = Renewal::get_subscriptions_url();
} elseif ( function_exists( '\wc_get_page_id' ) && \get_the_ID() === \wc_get_page_id( 'myaccount' ) ) {
$redirect = \wc_get_account_endpoint_url( 'dashboard' );
}
Expand Down

0 comments on commit dd06a16

Please sign in to comment.