Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created November 26, 2020 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/3ceb1fe2314c053cb5fe52a43bd032a3 to your computer and use it in GitHub Desktop.
Save trepmal/3ceb1fe2314c053cb5fe52a43bd032a3 to your computer and use it in GitHub Desktop.
Testing scenarios for domain masking via sunrise.php in WordPress
<?php
// Mask Domain testing
$clientslug_custom_sunrise_domains = [
'mask.test',
];
// quickly switch testing scenarios
$test_case = 'two';
switch ( $test_case ) {
case 'one';
/**
* origin domain must not have a path
*
* home_url filter:
* - load full site
* - links stay in mask domain
*
* redirect_canonical filter:
* - mask works on initial load only
*
* neither filter:
* - redirects to origin domain
*/
if ( false !== ( $key = array_search( $_SERVER['HTTP_HOST'], $clientslug_custom_sunrise_domains, true ) ) ) {
$current_blog = get_site( 75 ); // alabama.test
$current_site = get_network( 1 ); // This should always be 1, unless you are running multiple WordPress networks.
$mask_domain = $clientslug_custom_sunrise_domains[ $key ];
$origin_domain = $current_blog->domain;
add_filter( 'redirect_canonical', '__return_false' );
// add_filter( 'home_url', function( $url ) use ( $mask_domain, $origin_domain ) {
// return str_replace( $origin_domain, $mask_domain, $url );
// } );
}
break;
case 'two';
/**
* serve only REST API over mask
* works on origins with or without path
*
* no filter:
* - serve origin domain in response data
*
* home_url filter:
* - can be used like other scenarios to change the domain in the response data
* likely unwanted given context of not serving front-end with masked domain
*
*/
if (
false !== ( $key = array_search( $_SERVER['HTTP_HOST'], $clientslug_custom_sunrise_domains, true ) )
&& 0 === strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-json' )
) {
$current_blog = get_site( 75 ); // alabama.test
$current_site = get_network( 1 ); // This should always be 1, unless you are running multiple WordPress networks.
}
break;
case 'three';
/**
* origin domain has path
*
* home_url filter:
* - load full site with or without path, depending on str_replace
*
* redirect_canonical filter:
* - mask works on initial load only (no effect)
*
* no filter:
* - mask works on initial load only
* - redirect halted due to "Protect against chained redirects." check
*/
if ( false !== ( $key = array_search( $_SERVER['HTTP_HOST'], $clientslug_custom_sunrise_domains, true ) ) ) {
$current_blog = get_site( 77 ); // ms.test/arizona/
$current_site = get_network( 1 ); // This should always be 1, unless you are running multiple WordPress networks.
$mask_domain = $clientslug_custom_sunrise_domains[ $key ];
$origin_domain = $current_blog->domain;
// add_filter( 'redirect_canonical', '__return_false' );
// A) loads full site at mask.test/arizona (WITH origin path)
add_filter( 'home_url', function( $url ) use ( $mask_domain, $origin_domain ) {
return str_replace( $origin_domain, $mask_domain, $url );
} );
// B) load full site at mask.test (WITHOUT origin path)
// $origin_domain .= untrailingslashit( $current_blog->path );
// add_filter( 'home_url', function( $url ) use ( $mask_domain, $origin_domain ) {
// return str_replace( $origin_domain, $mask_domain, $url );
// } );
// C) load full site at mask.test/party (WITHOUT origin path). mask.test does not redirect to mask.test/party
// $mask_domain .= untrailingslashit( '/party' );
// $origin_domain .= untrailingslashit( $current_blog->path );
// add_filter( 'home_url', function( $url ) use ( $mask_domain, $origin_domain ) {
// return str_replace( $origin_domain, $mask_domain, $url );
// } );
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment