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
12 changes: 11 additions & 1 deletion inc/checkout/class-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,8 +1312,18 @@ protected function maybe_create_membership() {

/*
* Important dates.
*
* For free, non-recurring products the billing start date is null,
* meaning there is no next charge — the membership should be
* treated as lifetime. Passing null into gmdate() silently uses
* the current timestamp, which sets the expiration to *today*
* and causes the membership to expire within hours/days.
*/
$membership_data['date_expiration'] = gmdate('Y-m-d 23:59:59', $this->order->get_billing_start_date());
$billing_start_date = $this->order->get_billing_start_date();

$membership_data['date_expiration'] = $billing_start_date
? gmdate('Y-m-d 23:59:59', $billing_start_date)
: null;
Comment on lines +1324 to +1326
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Use an explicit null check for date_expiration assignment.

Using a truthy check can incorrectly treat 0/'0' as “no date” and set a lifetime membership. Compare against null explicitly.

Suggested fix
-		$membership_data['date_expiration'] = $billing_start_date
-			? gmdate('Y-m-d 23:59:59', $billing_start_date)
+		$membership_data['date_expiration'] = null !== $billing_start_date
+			? gmdate('Y-m-d 23:59:59', (int) $billing_start_date)
 			: null;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$membership_data['date_expiration'] = $billing_start_date
? gmdate('Y-m-d 23:59:59', $billing_start_date)
: null;
$membership_data['date_expiration'] = null !== $billing_start_date
? gmdate('Y-m-d 23:59:59', (int) $billing_start_date)
: null;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@inc/checkout/class-checkout.php` around lines 1324 - 1326, The ternary for
assigning $membership_data['date_expiration'] uses a truthy check on
$billing_start_date which treats 0/'0' as no date; change the condition to an
explicit null check (e.g. compare $billing_start_date !== null) so that when
$billing_start_date is zero it still calls gmdate('Y-m-d 23:59:59',
$billing_start_date) and only assigns null when $billing_start_date is actually
null; update the expression where $membership_data['date_expiration'] is set
(the line that calls gmdate with $billing_start_date) accordingly.


$membership = wu_create_membership($membership_data);

Expand Down
84 changes: 84 additions & 0 deletions tests/WP_Ultimo/Checkout/Checkout_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3510,6 +3510,90 @@ public function test_maybe_create_membership_creates_new(): void {
$order_prop->setValue($checkout, null);
}

/**
* Test maybe_create_membership sets null expiration for free products.
*
* Free, non-recurring products should produce a lifetime membership
* (date_expiration = null). Before the fix, gmdate() was called with
* null which resolved to "today 23:59:59", causing the membership to
* expire at end-of-day.
*/
public function test_maybe_create_membership_free_product_has_null_expiration(): void {

$customer = self::$customer;

$free_plan = wu_create_product([
'name' => 'Free Test Plan',
'slug' => 'free-test-plan-' . wp_rand(1000, 9999),
'amount' => 0,
'recurring' => false,
'duration' => 1,
'duration_unit' => 'month',
'type' => 'plan',
'pricing_type' => 'free',
'active' => true,
]);

if (is_wp_error($free_plan)) {
$this->markTestSkipped('Product creation failed: ' . $free_plan->get_error_message());
}

$checkout = Checkout::get_instance();
$reflection = new \ReflectionClass($checkout);
$method = $reflection->getMethod('maybe_create_membership');

if (PHP_VERSION_ID < 80100) {
$method->setAccessible(true);
}

$cart = new Cart(['products' => [$free_plan->get_id()]]);

// Verify the cart recognises this as free
$this->assertTrue($cart->is_free(), 'Cart should be free for a zero-cost plan');
$this->assertNull($cart->get_billing_start_date(), 'Billing start date should be null for free plan');

$order_prop = $this->get_order_prop($reflection);
$order_prop->setValue($checkout, $cart);

$customer_prop = $reflection->getProperty('customer');
if (PHP_VERSION_ID < 80100) {
$customer_prop->setAccessible(true);
}
$customer_prop->setValue($checkout, $customer);

$gateway_prop = $reflection->getProperty('gateway_id');
if (PHP_VERSION_ID < 80100) {
$gateway_prop->setAccessible(true);
}
$gateway_prop->setValue($checkout, 'free');

$result = $method->invoke($checkout);

if (is_wp_error($result)) {
$this->markTestSkipped('Membership creation failed: ' . $result->get_error_message());
}

$this->assertInstanceOf(\WP_Ultimo\Models\Membership::class, $result);

// The critical assertion: free membership must NOT have an expiration date
$this->assertNull(
$result->get_date_expiration(),
'Free membership must have null date_expiration (lifetime). ' .
'Got: ' . var_export($result->get_date_expiration(), true)
);

// Consequently, the membership should be identified as lifetime
$this->assertTrue(
$result->is_lifetime(),
'Free membership must be recognised as lifetime'
);

// Cleanup
$result->delete();
$free_plan->delete();
$order_prop->setValue($checkout, null);
}

// -------------------------------------------------------------------------
// maybe_create_payment — create new payment path
// -------------------------------------------------------------------------
Expand Down
Loading