Domain mapping: apply primary mapped domain to home_url/site_url for other sites, not only the current request host

Đang mở
#2,080 5 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@superdav42 đang làm issue này rồi.

Từ ngày 4/11/2026.

  • #2085 của @superdav42 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
php, wordpress
Lĩnh vực
backend, databases

Hướng nghiên cứu

Start with register_mapped_filters() and mangle_url() in inc/class-domain-mapping.php, then read get_primary_mapped_domain() in inc/models/class-site.php and the relevant domain mapping tests under tests/. Verify the main site context returns the primary mapped domain for another site, an unmapped site stays unchanged, and admin on a non-primary host still works with force_admin_redirect = both; check cache behavior across repeated lookups.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug origin:interactive status:in-review

Summary

A site's primary mapped domain is only applied to home_url / site_url when the current request's host is a mapping of that same site. Everywhere else (main site, network admin, "My Sites", cross-site loops calling get_home_url($blog_id), cron, CLI) WordPress returns the raw stored home/siteurl option, i.e. the site's original network address. If that address is a domain the customer no longer wants, links built from another site send users to the old host, where admin, uploads and REST keep working on the old domain.

Observed in production (anonymised)

  • Site N has wp_blogs.domain = old.example.com, mapping A new.example.net (active=1, primary_domain=1), mapping B old.example.com (active=1, primary_domain=0). force_admin_redirect = both.
  • Front end on the old host correctly 301s to the primary (Primary_Domain::redirect_to_primary_domain).
  • A network-wide SSO/plugin on the main site builds per-site links with get_home_url($id) → returns https://old.example.com because mangle_url() bails when $site_id differs from the current request's mapping.
  • Editors follow those links, stay in wp-admin on the old host (no admin redirect under both), and every upload gets an old-domain URL (43 attachments in a week).
  • Only fix was changing the site's network address itself, which defeats the purpose of a primary mapping.

Root cause

inc/class-domain-mapping.php:

  • register_mapped_filters() (~line 517) registers the URL filters only when $_SERVER['HTTP_HOST'] matches a mapping, and stores that mapping as current_mapping.
  • replace_url() (~line 612) rewrites to current_mapping->get_domain() — the request's mapping, not the site's primary mapping.
  • mangle_url() (~line 682) returns the URL unchanged when current_mapping->get_blog_id() !== $site_id, so other sites are never mapped.

Proposed change

  1. Register home_url / site_url (with $blog_id/$site_id args) filters whenever domain mapping is enabled, not only when the request host is mapped.
  2. In mangle_url(), when there is no current mapping or $site_id is a different site, resolve that site's primary mapping via the existing \WP_Ultimo\Models\Site::get_primary_mapped_domain() (inc/models/class-site.php:1334) and pass it to replace_url($url, $mapping) (the second parameter already exists for this). Cache the per-site lookup (existing domain_mappings global cache group) so loops over many sites do not add a query per call.
  3. Keep current behaviour for the current site on a mapped request host (respects force_admin_redirect = both, so admin on an alternate mapped domain keeps working).
  4. Do not change raw option_home/option_siteurl reads for other sites unless needed; the home_url/site_url filters cover get_home_url()/get_site_url()/get_admin_url().

Optional follow-up (separate PR is fine): when the request host is a non-primary mapping and force_admin_redirect = force_map, ensure admin redirects to the primary mapping rather than the request mapping.

Files Scope
  • inc/class-domain-mapping.php
  • tests/WP_Ultimo/Domain_Mapping_Test.php

Reference only: inc/models/class-site.php (get_primary_mapped_domain()) and inc/domain-mapping/class-primary-domain.php (redirect behaviour to stay consistent).

Acceptance / verification

  • From the main site context (wp --url=<main-site> eval 'echo get_home_url(<N>);'), a site with an active primary mapping returns the primary mapped domain.
  • Network admin → Sites list and "My Sites" show the primary mapped domain.
  • A site without mappings is unchanged.
  • Visiting wp-admin on a non-primary mapped host with force_admin_redirect = both still works and keeps that host (no regression).
  • No extra DB query per get_home_url() call in a loop over sites after the first lookup (object cache hit).
  • vendor/bin/phpcs clean on changed files; existing PHPUnit suite passes.

aidevops.sh v3.39.119 plugin for OpenCode

Ngôn ngữ chính
PHP
Star
218
Fork
88
Merge trung bình
1 ngày 5 giờ
Pull request đã merge (30 ngày)
94

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Ultimate-Multisite/ultimate-multisite

Tất cả issue của Ultimate-Multisite/ultimate-multisite

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.