cameronterry / dark-matter

A highly opinionated domain mapping plugin for WordPress Networks
Other
12 stars 2 forks source link

Retrieving REST URL in a switch_to_blog() context #96

Closed cameronterry closed 2 years ago

cameronterry commented 2 years ago

Calling get_rest_url() in a switch_to_blog() context incorrectly returned the URL in certain circumstances, especially if the site had a mapped domain.

The detection of mapped domains and request is mapped (i.e. went through sunrise.php) has been tweaked to be more nuanced and better ensure the right URLs return in the right circumstances. Now, if the request is mapped and switch_to_blog() is used, then the URLs of the other website will be mapped if that site has an active primary domain.

If it does not, then the URLs are unmapped to ensure there is no cross domain request errors and warnings, especially important if get_rest_url() is called as it is likely being used for AJAX requests.

Also switched the way DM_URL detects the DOMAIN_MAPPING constant so that it can be controlled through a property as well as the constant, to improve the ability to test more effectively in unit tests. Also opens some new flexibility for developers to control the deeper logic of the URL mapping / unmapping process.

Last but not least, fixed the URL for the REST URL unit test as it incorrectly contained the site folder name (which is removed for a mapped domain).

Resolves unit test:

3) MappingDomainsTest::test_rest_url
REST API URL
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'https://darkmatter.test/siteone/wp-json/'
+'https://mappeddomain1.test/siteone/wp-json/'

/home/runner/work/dark-matter/dark-matter/tests/phpunit/domain-mapping/MappingDomainsTest.php:195

Fixes: https://github.com/cameronterry/dark-matter/issues/92