Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion inc/checkout/class-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* @var object
* @readonly
*/
private \stdClass $attributes;

Check warning on line 51 in inc/checkout/class-cart.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Class WP_Ultimo\Checkout\Cart has an uninitialized @readonly property $attributes. Assign it in the constructor.

/**
* Type of registration: new, renewal, upgrade, downgrade, retry, and display.
Expand Down Expand Up @@ -261,6 +261,18 @@
* @param array $args An array containing the cart arguments.
*/
public function __construct($args) {
/*
* Guard against instantiation in sovereign tenant context.
* Checkout should only run on the main site.
*/
if (defined('WU_MT_SOVEREIGN_TENANT') && WU_MT_SOVEREIGN_TENANT) {
$this->errors = new \WP_Error(
'sovereign_checkout_disabled',
__('Checkout is disabled in sovereign tenant context.', 'ultimate-multisite')
);
return;
}

/*
* Why are we using shortcode atts, you might ask?
*
Expand Down Expand Up @@ -854,7 +866,6 @@
];

if (in_array($membership->get_status(), $inactive_statuses, true)) {

$this->cart_type = 'reactivation';

/*
Expand Down Expand Up @@ -2242,7 +2253,7 @@
*/
public function has_trial() {

/*

Check warning on line 2256 in inc/checkout/class-cart.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Empty line not required before block comment
* Reactivation carts never get a trial period.
*
* A customer reactivating a cancelled membership has already used
Expand Down
53 changes: 52 additions & 1 deletion inc/checkout/class-checkout-pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,58 @@

add_action('save_post', [$this, 'handle_compat_mode_setting']);
}

/*
* In sovereign tenant context, redirect checkout URLs to the main site.
*/
if (defined('WU_MT_SOVEREIGN_TENANT') && WU_MT_SOVEREIGN_TENANT) {
add_filter('post_type_link', [$this, 'redirect_checkout_urls_in_sovereign_context'], 10, 2);
add_filter('page_link', [$this, 'redirect_checkout_urls_in_sovereign_context'], 10, 2);
}
}

/**
* Gets the main site checkout URL.
*
* Used to redirect sovereign tenants to the main site for checkout.
*
* @since 2.5.2
* @return string The main site checkout URL.
*/
public function get_main_site_checkout_url(): string {

$main_site = get_blog_details(get_network()->site_id);

if (! $main_site) {
return network_site_url('/register/');
}

return trailingslashit($main_site->siteurl) . 'register/';
}

/**
* Redirects checkout URLs to the main site in sovereign tenant context.
*
* @since 2.5.2
*
* @param string $permalink The post permalink.
* @param \WP_Post $post The post object.
* @return string The modified permalink.
*/
public function redirect_checkout_urls_in_sovereign_context($permalink, $post) {

if (! is_a($post, '\WP_Post')) {
return $permalink;
}

$signup_pages = $this->get_signup_pages();

// Check if this post is a checkout-related page
if (in_array($post->ID, array_filter($signup_pages), true)) {
return $this->get_main_site_checkout_url();
}

return $permalink;
}

/**
Expand Down Expand Up @@ -410,7 +462,7 @@

if (is_main_site()) {

/*

Check warning on line 465 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Empty line not required before block comment
* On the main site, point the reset URL at the custom login
* page so users land on the network's branded login screen.
*/
Expand All @@ -425,10 +477,9 @@
);

$new_url = set_url_scheme($new_url, null);

} else {

/*

Check warning on line 482 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Empty line not required before block comment
* On a subsite, rewrite the URL so the reset flow stays on the
* subsite's own domain. The default wp-login.php URL points to
* the main network site, which:
Expand Down Expand Up @@ -473,7 +524,7 @@
* @param string $user_login The user login.
* @param WP_User $user_data The user data.
*/
$new_url = apply_filters('wu_subsite_password_reset_url', $new_url, $key, $user_login, $user_data);

Check warning on line 527 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

@param WP_Ultimo\Checkout\WP_User $user_data does not accept actual type of parameter: array.

$new_url = set_url_scheme($new_url, null);
}
Expand Down Expand Up @@ -547,7 +598,7 @@
* @param string $blogname The site name.
* @return array
*/
public function rewrite_new_user_notification_email($email, $user, $blogname) {

Check warning on line 601 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

The method parameter $blogname is never used

Check warning on line 601 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

The method parameter $user is never used

if (empty($email['message']) || ! is_array($email)) {
return $email;
Expand Down Expand Up @@ -579,7 +630,7 @@
* @param WP_User $user_data WP_User object.
* @return array
*/
public function rewrite_password_notification_email($defaults, $key, $user_login, $user_data) {

Check warning on line 633 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

The method parameter $user_data is never used

Check warning on line 633 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

The method parameter $user_login is never used

Check warning on line 633 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

The method parameter $key is never used

if (empty($defaults['message']) || ! is_array($defaults)) {
return $defaults;
Expand Down Expand Up @@ -611,7 +662,7 @@
* @param array $new_user_email Data on the changed email.
* @return string
*/
public function rewrite_email_change_content($email_text, $new_user_email) {

Check warning on line 665 in inc/checkout/class-checkout-pages.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

The method parameter $new_user_email is never used

if (empty($email_text) || ! is_string($email_text)) {
return $email_text;
Expand Down
97 changes: 85 additions & 12 deletions inc/checkout/class-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,25 @@
add_action('wu_checkout_errors', [$this, 'maybe_display_checkout_errors']);
}

/**
* Gets the main site checkout URL.
*
* Used to redirect sovereign tenants to the main site for checkout.
*
* @since 2.5.2
* @return string The main site checkout URL.
*/
public function get_main_site_checkout_url(): string {

$main_site = get_blog_details(get_network()->site_id);

if (! $main_site) {
return network_site_url('/register/');
}

return trailingslashit($main_site->siteurl) . 'register/';
}

/**
* Add checkout rewrite rules.
*
Expand Down Expand Up @@ -563,6 +582,19 @@
*/
public function maybe_handle_order_submission(): void {

if (defined('WU_MT_SOVEREIGN_TENANT') && WU_MT_SOVEREIGN_TENANT) {
wp_send_json_error(
[
'code' => 'sovereign_checkout_disabled',
'message' => __('Checkout runs on the main site.', 'ultimate-multisite'),
'main_site_url' => $this->get_main_site_checkout_url(),
],
400
);

return;
}

$this->setup_checkout();

check_ajax_referer('wu_checkout');
Expand Down Expand Up @@ -750,15 +782,17 @@
$existing_customer = wu_get_current_customer();

if ($existing_customer) {
$active_memberships = wu_get_memberships([
'customer_id' => $existing_customer->get_id(),
'status__in' => [
Membership_Status::ACTIVE,
Membership_Status::TRIALING,
Membership_Status::ON_HOLD,
],
'number' => 1,
]);
$active_memberships = wu_get_memberships(
[
'customer_id' => $existing_customer->get_id(),
'status__in' => [
Membership_Status::ACTIVE,
Membership_Status::TRIALING,
Membership_Status::ON_HOLD,
],
'number' => 1,
]
);

if ( ! empty($active_memberships)) {
$existing_membership = reset($active_memberships);
Expand Down Expand Up @@ -1183,7 +1217,7 @@
'email' => wp_get_current_user()->user_email,
'email_verification' => 'verified',
];
} elseif (isset($customer_data['email']) && ($existing_wp_user = get_user_by('email', $customer_data['email']))) {

Check warning on line 1220 in inc/checkout/class-checkout.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Variable assignment found within a condition. Did you mean to do a comparison ?
/*
* A WP user already exists with this email.
*
Expand Down Expand Up @@ -1838,6 +1872,19 @@
*/
public function create_order(): void {

if (defined('WU_MT_SOVEREIGN_TENANT') && WU_MT_SOVEREIGN_TENANT) {
wp_send_json_error(
[
'code' => 'sovereign_checkout_disabled',
'message' => __('Checkout runs on the main site.', 'ultimate-multisite'),
'main_site_url' => $this->get_main_site_checkout_url(),
],
400
);

return;
}

$this->setup_checkout();

// Set billing address to be used on the order
Expand Down Expand Up @@ -1898,6 +1945,19 @@
*/
public function check_user_exists(): void {

if (defined('WU_MT_SOVEREIGN_TENANT') && WU_MT_SOVEREIGN_TENANT) {
wp_send_json_error(
[
'code' => 'sovereign_checkout_disabled',
'message' => __('Checkout runs on the main site.', 'ultimate-multisite'),
'main_site_url' => $this->get_main_site_checkout_url(),
],
400
);

return;
}

check_ajax_referer('wu_checkout');

$field_type = wu_request('field_type');
Expand Down Expand Up @@ -1950,6 +2010,19 @@
*/
public function handle_inline_login(): void {

if (defined('WU_MT_SOVEREIGN_TENANT') && WU_MT_SOVEREIGN_TENANT) {
wp_send_json_error(
[
'code' => 'sovereign_checkout_disabled',
'message' => __('Checkout runs on the main site.', 'ultimate-multisite'),
'main_site_url' => $this->get_main_site_checkout_url(),
],
400
);

return;
}

check_ajax_referer('wu_checkout');

$username_or_email = sanitize_text_field(wu_request('username_or_email'));
Expand Down Expand Up @@ -2091,15 +2164,15 @@
/* translators: %s: field label */
'field_invalid_email' => __('%s must be a valid email address.', 'ultimate-multisite'),
/* translators: 1: field label, 2: minimum character count */
'field_min_length' => __('%s must be at least %d characters.', 'ultimate-multisite'),
'field_min_length' => __('%1$s must be at least %2$d characters.', 'ultimate-multisite'),
/* translators: 1: field label, 2: maximum character count */
'field_max_length' => __('%s must not exceed %d characters.', 'ultimate-multisite'),
'field_max_length' => __('%1$s must not exceed %2$d characters.', 'ultimate-multisite'),
/* translators: %s: field label */
'field_alpha_dash' => __('%s may only contain letters, numbers, dashes, and underscores.', 'ultimate-multisite'),
/* translators: %s: field label */
'field_lowercase' => __('%s must be lowercase.', 'ultimate-multisite'),
/* translators: 1: field label, 2: other field label */
'field_same' => __('%s must match %s.', 'ultimate-multisite'),
'field_same' => __('%1$s must match %2$s.', 'ultimate-multisite'),
/* translators: %s: field label */
'field_integer' => __('%s must be a whole number.', 'ultimate-multisite'),
/* translators: %s: field label */
Expand Down
12 changes: 12 additions & 0 deletions inc/ui/class-checkout-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,18 @@ public function inject_inline_auto_submittable_field($auto_submittable_field): v
*/
public function output($atts, $content = null) {

// In sovereign tenant context, render a link to the main site checkout instead
if (defined('WU_MT_SOVEREIGN_TENANT') && WU_MT_SOVEREIGN_TENANT) {
$checkout_pages = \WP_Ultimo\Checkout\Checkout_Pages::get_instance();
$main_site_url = $checkout_pages->get_main_site_checkout_url();
?>
<a href="<?php echo esc_url($main_site_url); ?>" class="wu-sovereign-checkout-link">
<?php esc_html_e('Upgrade your plan on the main site', 'ultimate-multisite'); ?>
</a>
<?php
return;
}

if (wu_is_update_page()) {
$atts = [
'slug' => apply_filters('wu_membership_update_form', 'wu-checkout'),
Expand Down
Loading
Loading