Skip to content

Conversation

@mahyarrezghi
Copy link
Contributor

@mahyarrezghi mahyarrezghi commented Dec 1, 2025

  • Track original primary state in Domain model to detect changes
  • Unset other primary domains when setting a new primary
  • Ensure consistency across API, admin, and creation flows

Summary by CodeRabbit

  • New Features
    • Implemented automatic cleanup of previous primary domain assignments when a new primary domain is set for a site
    • Enhanced domain state tracking to accurately detect primary domain status changes
    • Automatic asynchronous processing ensures seamless background updates when primary domains change, preventing conflicts and maintaining one primary domain per site

✏️ Tip: You can customize this high-level summary in your review settings.

- Track original primary state in Domain model to detect changes
- Unset other primary domains when setting a new primary
- Ensure consistency across API, admin, and creation flows
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 1, 2025

Walkthrough

The Domain model is enhanced with change tracking for the primary_domain field. A new protected property original_primary stores the initial primary domain value, enabling detection of transitions. When a domain becomes the primary for a site (either newly created or promoted), an asynchronous cleanup action is triggered to unset other primary domains.

Changes

Cohort / File(s) Summary
Domain Change Tracking & Async Primary Domain Cleanup
inc/models/class-domain.php
Added original_primary property to track state changes. Enhanced primary_domain setter to initialize original_primary when modifying existing records. Modified save flow to compute was_new status before persisting. Integrated async cleanup action that triggers when a domain is promoted to primary, dispatching wu_async_remove_old_primary_domains to remove conflicting primary domains for the same site.

Sequence Diagram

sequenceDiagram
    actor User
    participant Domain Model
    participant Save Logic
    participant Async Queue
    participant Cleanup Handler
    participant Database

    User->>Domain Model: Set primary_domain = true
    Domain Model->>Domain Model: Initialize original_primary<br/>(if not already set)
    User->>Domain Model: Call save()
    Domain Model->>Save Logic: Compute was_new status
    Save Logic->>Database: Persist domain record
    Database-->>Save Logic: Success
    
    alt Domain is primary AND (newly created OR promoted from non-primary)
        Save Logic->>Async Queue: Dispatch wu_async_remove_old_primary_domains
        Async Queue-->>Cleanup Handler: Execute async action
        Cleanup Handler->>Database: Fetch old primary domains<br/>for same site
        Database-->>Cleanup Handler: Return old primaries
        Cleanup Handler->>Database: Unset primary status
        Database-->>Cleanup Handler: Success
    end
    
    Cleanup Handler-->>User: Cleanup complete
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • State initialization logic: Verify original_primary is only set once and correctly on existing records
  • Conditional save flow: Ensure was_new computation and the promotion detection logic (is_new || changed from non-primary to primary) handles all edge cases
  • Async action integration: Confirm the cleanup action is properly dispatched with correct parameters and handles the database queries safely

Poem

🐰 A domain stands tall, now marked as the primary crown,
We track what it was before the change came down,
When rivals emerge, our async broom sweeps clean,
Making room for the one—the finest domain we've seen! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: preventing multiple primary domains per site, which directly matches the core functionality added to detect and handle primary domain changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
inc/models/class-domain.php (1)

494-499: Consider clarifying variable naming for readability.

The naming of $new_domain (line 494) and $was_new (line 498) have opposite semantics:

  • $new_domain = $this->exists() — true when domain exists
  • $was_new = ! $this->exists() — true when domain does not exist

This can be confusing for future maintainers. Consider renaming $new_domain to $existed_before_save for consistency with $was_new.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 165ab6e and 0ec8334.

📒 Files selected for processing (1)
  • inc/models/class-domain.php (4 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
inc/models/class-domain.php (4)
inc/models/class-site.php (1)
  • exists (1243-1246)
inc/database/engine/class-table.php (1)
  • exists (65-72)
inc/objects/class-billing-address.php (1)
  • exists (95-98)
inc/functions/domain.php (1)
  • wu_get_domains (35-38)
🪛 GitHub Check: Code Quality Checks
inc/models/class-domain.php

[failure] 307-307:
Expected 1 space after "!"; 0 found


[failure] 534-534:
Whitespace found at end of line

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: cypress (8.1, chrome)
  • GitHub Check: cypress (8.2, chrome)
🔇 Additional comments (2)
inc/models/class-domain.php (2)

57-64: LGTM!

The new $original_primary property is well-documented and appropriately typed. It correctly enables change tracking for the primary domain state.


542-557: Verify the async action handler exists and handles empty arrays gracefully.

The logic for detecting when a domain becomes primary is correct. However, ensure:

  1. The wu_async_remove_old_primary_domains action handler is registered elsewhere.
  2. The handler gracefully handles an empty array (when no other primaries exist).

Comment on lines +307 to +309
if ($this->id > 0 && !isset($this->original_primary)) {
$this->original_primary = $this->primary_domain;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix code style: add space after negation operator.

Static analysis flagged a missing space after !.

Apply this diff:

-		if ($this->id > 0 && !isset($this->original_primary)) {
+		if ($this->id > 0 && ! isset($this->original_primary)) {
🧰 Tools
🪛 GitHub Check: Code Quality Checks

[failure] 307-307:
Expected 1 space after "!"; 0 found

🤖 Prompt for AI Agents
In inc/models/class-domain.php around lines 307 to 309, the negation operator is
missing a space; change the conditional to add a space after the '!' so it reads
with '! isset(...)' (i.e. ensure there is a space between the '!' and the
following token) to satisfy the code style/static analysis.

do_action_deprecated('mercator.mapping.updated', $deprecated_args, '2.0.0', 'wu_domain_post_save');
}

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Remove trailing whitespace.

Static analysis flagged trailing whitespace at this line.

🧰 Tools
🪛 GitHub Check: Code Quality Checks

[failure] 534-534:
Whitespace found at end of line

🤖 Prompt for AI Agents
In inc/models/class-domain.php at line 534, there is trailing whitespace at the
end of the line; remove any extra spaces or tabs after the code on that line so
the line ends immediately after the last visible character, then save the file
to eliminate the static-analysis warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant