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

未关闭
#2,080 5 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@superdav42 已经在做这个了。

开始于 2026年11月4日。

  • #2085 来自 @superdav42 —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
php, wordpress
领域
backend, databases

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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

主要语言
PHP
星标
218
派生
88
平均合并
1 天 5 小时
30 天内合并 PR
94

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Ultimate-Multisite/ultimate-multisite 的其他 Issue

查看 Ultimate-Multisite/ultimate-multisite 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。