Skip to content

Commit

Permalink
fix: allow Stripe Billing portal to render without RAS
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Oct 24, 2022
1 parent cd7b304 commit b6e22cd
Showing 1 changed file with 64 additions and 59 deletions.
123 changes: 64 additions & 59 deletions includes/reader-revenue/my-account/class-woocommerce-my-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ class WooCommerce_My_Account {
* @codeCoverageIgnore
*/
public static function init() {
if ( ! Reader_Activation::is_enabled() ) {
return;
}

\add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] );
\add_filter( 'woocommerce_account_menu_items', [ __CLASS__, 'my_account_menu_items' ], 1000 );
\add_action( 'woocommerce_account_' . self::BILLING_ENDPOINT . '_endpoint', [ __CLASS__, 'render_billing_template' ] );
\add_filter( 'wc_get_template', [ __CLASS__, 'wc_get_template' ], 10, 5 );
\add_action( 'init', [ __CLASS__, 'add_rewrite_endpoints' ] );
\add_action( 'template_redirect', [ __CLASS__, 'handle_password_reset_request' ] );
\add_action( 'template_redirect', [ __CLASS__, 'handle_delete_account_request' ] );
\add_action( 'template_redirect', [ __CLASS__, 'handle_delete_account' ] );
\add_action( 'template_redirect', [ __CLASS__, 'handle_magic_link_request' ] );
\add_action( 'template_redirect', [ __CLASS__, 'redirect_to_account_details' ] );
\add_action( 'template_redirect', [ __CLASS__, 'edit_account_prevent_email_update' ] );
\add_action( 'init', [ __CLASS__, 'restrict_account_content' ], 100 );
\add_filter( 'woocommerce_save_account_details_required_fields', [ __CLASS__, 'remove_required_fields' ] );
\add_action( 'logout_redirect', [ __CLASS__, 'add_param_after_logout' ] );
\add_action( 'template_redirect', [ __CLASS__, 'show_message_after_logout' ] );
\add_action( 'woocommerce_account_' . self::BILLING_ENDPOINT . '_endpoint', [ __CLASS__, 'render_billing_template' ] );
\add_filter( 'woocommerce_account_menu_items', [ __CLASS__, 'my_account_menu_items' ], 1000 );

// Reader Activation mods.
if ( Reader_Activation::is_enabled() ) {
\add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] );
\add_filter( 'wc_get_template', [ __CLASS__, 'wc_get_template' ], 10, 5 );
\add_action( 'template_redirect', [ __CLASS__, 'handle_password_reset_request' ] );
\add_action( 'template_redirect', [ __CLASS__, 'handle_delete_account_request' ] );
\add_action( 'template_redirect', [ __CLASS__, 'handle_delete_account' ] );
\add_action( 'template_redirect', [ __CLASS__, 'handle_magic_link_request' ] );
\add_action( 'template_redirect', [ __CLASS__, 'redirect_to_account_details' ] );
\add_action( 'template_redirect', [ __CLASS__, 'edit_account_prevent_email_update' ] );
\add_action( 'init', [ __CLASS__, 'restrict_account_content' ], 100 );
\add_filter( 'woocommerce_save_account_details_required_fields', [ __CLASS__, 'remove_required_fields' ] );
\add_action( 'logout_redirect', [ __CLASS__, 'add_param_after_logout' ] );
\add_action( 'template_redirect', [ __CLASS__, 'show_message_after_logout' ] );
}
}

/**
Expand All @@ -81,64 +81,69 @@ public static function my_account_menu_items( $items ) {
return $items;
}

$default_disabled_items = [];

// Rename 'Logout' action to 'Log out', for grammatical reasons.
if ( isset( $items['customer-logout'] ) ) {
$items['customer-logout'] = __( 'Log out', 'newspack' );
}

// If the reader hasn't verified their account, only show options to verify or log out.
if ( ! self::is_user_verified() ) {
$minimum_items = [ 'edit-account', 'customer-logout' ];
foreach ( $items as $key => $label ) {
if ( ! in_array( $key, $minimum_items, true ) ) {
unset( $items[ $key ] );
if ( Reader_Activation::is_enabled() ) {
// If the reader hasn't verified their account, only show options to verify or log out.
if ( ! self::is_user_verified() ) {
$minimum_items = [ 'edit-account', 'customer-logout' ];
foreach ( $items as $key => $label ) {
if ( ! in_array( $key, $minimum_items, true ) ) {
unset( $items[ $key ] );
}
}
return $items;
}
return $items;
}

$default_disabled_items = [ 'dashboard', 'members-area', 'edit-address' ];
$customer_id = \get_current_user_id();
if ( function_exists( 'wcs_user_has_subscription' ) && function_exists( 'wcs_get_subscriptions' ) ) {
$user_subscriptions = wcs_get_subscriptions( [ 'customer_id' => $customer_id ] );
$has_non_newspack_subscriptions = false;
foreach ( $user_subscriptions as $subscription ) {
if ( ! $subscription->get_meta( WooCommerce_Connection::SUBSCRIPTION_STRIPE_ID_META_KEY ) ) {
$has_non_newspack_subscriptions = true;
break;
$default_disabled_items = array_merge( $default_disabled_items, [ 'dashboard', 'members-area', 'edit-address' ] );
$customer_id = \get_current_user_id();
if ( function_exists( 'wcs_user_has_subscription' ) && function_exists( 'wcs_get_subscriptions' ) ) {
$user_subscriptions = wcs_get_subscriptions( [ 'customer_id' => $customer_id ] );
$has_non_newspack_subscriptions = false;
foreach ( $user_subscriptions as $subscription ) {
if ( ! $subscription->get_meta( WooCommerce_Connection::SUBSCRIPTION_STRIPE_ID_META_KEY ) ) {
$has_non_newspack_subscriptions = true;
break;
}
}
// Unless user has any subscriptions that aren't tied to a Stripe subscription by Newspack, hide the subscriptions link.
// The Stripe-tied subscriptions will be available for management in the "Billing" section.
if ( ! $has_non_newspack_subscriptions ) {
$default_disabled_items[] = 'subscriptions';
}
}
// Unless user has any subscriptions that aren't tied to a Stripe subscription by Newspack, hide the subscriptions link.
// The Stripe-tied subscriptions will be available for management in the "Billing" section.
if ( ! $has_non_newspack_subscriptions ) {
$default_disabled_items[] = 'subscriptions';
}
}
if ( function_exists( 'wc_get_orders' ) ) {
$wc_non_newspack_orders = array_filter(
\wc_get_orders( [ 'customer' => $customer_id ] ),
function ( $order ) {
return WooCommerce_Connection::CREATED_VIA_NAME !== $order->get_created_via();
if ( function_exists( 'wc_get_orders' ) ) {
$wc_non_newspack_orders = array_filter(
\wc_get_orders( [ 'customer' => $customer_id ] ),
function ( $order ) {
return WooCommerce_Connection::CREATED_VIA_NAME !== $order->get_created_via();
}
);
if ( empty( $wc_non_newspack_orders ) ) {
$default_disabled_items = array_merge( $default_disabled_items, [ 'orders', 'payment-methods' ] );
}
);
if ( empty( $wc_non_newspack_orders ) ) {
$default_disabled_items = array_merge( $default_disabled_items, [ 'orders', 'payment-methods' ] );
}
}
if ( function_exists( 'wc_get_customer_available_downloads' ) ) {
$wc_customer_downloads = \wc_get_customer_available_downloads( $customer_id );
if ( function_exists( 'wc_get_customer_available_downloads' ) ) {
$wc_customer_downloads = \wc_get_customer_available_downloads( $customer_id );

if ( empty( $wc_customer_downloads ) ) {
$default_disabled_items[] = 'downloads';
if ( empty( $wc_customer_downloads ) ) {
$default_disabled_items[] = 'downloads';
}
}
}
$disabled_wc_menu_items = \apply_filters( 'newspack_my_account_disabled_pages', $default_disabled_items );
foreach ( $disabled_wc_menu_items as $key ) {
if ( isset( $items[ $key ] ) ) {
unset( $items[ $key ] );
$disabled_wc_menu_items = \apply_filters( 'newspack_my_account_disabled_pages', $default_disabled_items );
foreach ( $disabled_wc_menu_items as $key ) {
if ( isset( $items[ $key ] ) ) {
unset( $items[ $key ] );
}
}
}

// Add a nav item for Stripe's billing portal.
$stripe_customer_id = self::get_current_user_stripe_id();
if ( false === $stripe_customer_id ) {
return $items;
Expand Down

0 comments on commit b6e22cd

Please sign in to comment.