From 3909209d0ea1131e1a9372b09aa703ac3e14eab1 Mon Sep 17 00:00:00 2001 From: tcrsavage Date: Wed, 18 Feb 2015 15:47:45 +1100 Subject: [PATCH 1/9] Fixed alignment --- sso-multinetwork.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sso-multinetwork.php b/sso-multinetwork.php index 832b9ba..1c7ca3b 100644 --- a/sso-multinetwork.php +++ b/sso-multinetwork.php @@ -78,7 +78,7 @@ function bootstrap() { add_filter( 'mercator.sso.main_domain_network', __NAMESPACE__ . '\\get_main_network' ); add_filter( 'mercator.sso.is_main_domain', __NAMESPACE__ . '\\correct_for_subdomain_networks', 10, 3 ); add_filter( 'mercator.sso.main_site_for_actions', __NAMESPACE__ . '\\set_main_site_for_actions' ); - add_action( 'muplugins_loaded', __NAMESPACE__ . '\\initialize_cookie_domain', 11 ); + add_action( 'muplugins_loaded', __NAMESPACE__ . '\\initialize_cookie_domain', 11 ); } /** From 52781b42028ad566cbb42a5d70cca7fa74c63cd2 Mon Sep 17 00:00:00 2001 From: Seth Carstens Date: Tue, 8 Dec 2015 12:45:11 -0700 Subject: [PATCH 2/9] added filter to fix functions that retrieve urls based on content_url fixes #32 --- mercator.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mercator.php b/mercator.php index d34fe5b..9562559 100644 --- a/mercator.php +++ b/mercator.php @@ -324,3 +324,29 @@ function mangle_url( $url, $path, $orig_scheme, $site_id = 0 ) { return $mangled; } + +/** + * Mangle the content URL to give our mapped domain + * + * @param string $url The complete home URL including scheme and path. + * @param string $path Path relative to the home URL. Blank string if no path is specified. + * @return string Mangled Content URL + */ +function mangle_content_url( $url, $path) { + //get site ID for use with getting mapped domain name + $site_id = get_current_blog_id(); + //pull in site aliases + $current_mapping = $GLOBALS['mercator_current_mapping']; + //make sure the current domain is a mapped domain, else return the original url + if ( empty( $current_mapping ) || $site_id !== (int) $current_mapping->get_site_id() ) { + return $url; + } + + // Replace the domain with mapped domain within the url + $domain = parse_url( $url, PHP_URL_HOST ); + $regex = '#^(\w+://)' . preg_quote( $domain, '#' ) . '#i'; + $mangled = preg_replace( $regex, '${1}' . $current_mapping->get_domain(), $url ); + + //return filtered content url + return $mangled; +} From dc2bf9901720370c5132f06212e278571a031672 Mon Sep 17 00:00:00 2001 From: Seth Carstens Date: Tue, 8 Dec 2015 13:47:54 -0700 Subject: [PATCH 3/9] add filter for content_url --- mercator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/mercator.php b/mercator.php index 9562559..7ade2e1 100644 --- a/mercator.php +++ b/mercator.php @@ -290,6 +290,7 @@ function register_mapped_filters() { $GLOBALS['mercator_current_mapping'] = $mapping; add_filter( 'site_url', __NAMESPACE__ . '\\mangle_url', -10, 4 ); add_filter( 'home_url', __NAMESPACE__ . '\\mangle_url', -10, 4 ); + add_filter( 'content_url', __NAMESPACE__ . '\\mangle_content_url', -10, 2 ); // If on network site, also filter network urls if ( is_main_site() ) { From b07bcb0a198905bb18edfc0bd0b0f861f7dcc1c1 Mon Sep 17 00:00:00 2001 From: Seth Carstens Date: Tue, 8 Dec 2015 14:53:22 -0700 Subject: [PATCH 4/9] fix password reset link --- mercator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/mercator.php b/mercator.php index 7ade2e1..4f8a636 100644 --- a/mercator.php +++ b/mercator.php @@ -291,6 +291,7 @@ function register_mapped_filters() { add_filter( 'site_url', __NAMESPACE__ . '\\mangle_url', -10, 4 ); add_filter( 'home_url', __NAMESPACE__ . '\\mangle_url', -10, 4 ); add_filter( 'content_url', __NAMESPACE__ . '\\mangle_content_url', -10, 2 ); + add_filter( 'lostpassword_url', __NAMESPACE__ . '\\mangle_content_url', -10, 2 ); // If on network site, also filter network urls if ( is_main_site() ) { From d46174a22c9e2102a22329ddff916bfb45ac9e5d Mon Sep 17 00:00:00 2001 From: Seth Carstens Date: Tue, 8 Dec 2015 16:24:13 -0700 Subject: [PATCH 5/9] add sunrise sample file with extra lines to show use case for disabling SSO --- sunrise-sample.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 sunrise-sample.php diff --git a/sunrise-sample.php b/sunrise-sample.php new file mode 100644 index 0000000..b7d9861 --- /dev/null +++ b/sunrise-sample.php @@ -0,0 +1,7 @@ + Date: Tue, 8 Dec 2015 20:15:32 -0700 Subject: [PATCH 6/9] fixed code styles, removed mangle_content_url and updated mangle_url to work with 2 params --- mercator.php | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/mercator.php b/mercator.php index 4f8a636..3d62ff5 100644 --- a/mercator.php +++ b/mercator.php @@ -290,8 +290,8 @@ function register_mapped_filters() { $GLOBALS['mercator_current_mapping'] = $mapping; add_filter( 'site_url', __NAMESPACE__ . '\\mangle_url', -10, 4 ); add_filter( 'home_url', __NAMESPACE__ . '\\mangle_url', -10, 4 ); - add_filter( 'content_url', __NAMESPACE__ . '\\mangle_content_url', -10, 2 ); - add_filter( 'lostpassword_url', __NAMESPACE__ . '\\mangle_content_url', -10, 2 ); + add_filter( 'content_url', __NAMESPACE__ . '\\mangle_url', -10, 2 ); + add_filter( 'lostpassword_url', __NAMESPACE__ . '\\mangle_url', -10, 2 ); // If on network site, also filter network urls if ( is_main_site() ) { @@ -309,12 +309,13 @@ function register_mapped_filters() { * @param int|null $site_id Blog ID, or null for the current blog. * @return string Mangled URL */ -function mangle_url( $url, $path, $orig_scheme, $site_id = 0 ) { +function mangle_url( $url, $path, $orig_scheme = null, $site_id = 0 ) { if ( empty( $site_id ) ) { $site_id = get_current_blog_id(); } $current_mapping = $GLOBALS['mercator_current_mapping']; + // If mapping isn't valid for this site, return the original URL if ( empty( $current_mapping ) || $site_id !== (int) $current_mapping->get_site_id() ) { return $url; } @@ -326,29 +327,3 @@ function mangle_url( $url, $path, $orig_scheme, $site_id = 0 ) { return $mangled; } - -/** - * Mangle the content URL to give our mapped domain - * - * @param string $url The complete home URL including scheme and path. - * @param string $path Path relative to the home URL. Blank string if no path is specified. - * @return string Mangled Content URL - */ -function mangle_content_url( $url, $path) { - //get site ID for use with getting mapped domain name - $site_id = get_current_blog_id(); - //pull in site aliases - $current_mapping = $GLOBALS['mercator_current_mapping']; - //make sure the current domain is a mapped domain, else return the original url - if ( empty( $current_mapping ) || $site_id !== (int) $current_mapping->get_site_id() ) { - return $url; - } - - // Replace the domain with mapped domain within the url - $domain = parse_url( $url, PHP_URL_HOST ); - $regex = '#^(\w+://)' . preg_quote( $domain, '#' ) . '#i'; - $mangled = preg_replace( $regex, '${1}' . $current_mapping->get_domain(), $url ); - - //return filtered content url - return $mangled; -} From 6760edd59a348f5f5910912505a1c510972f2e7c Mon Sep 17 00:00:00 2001 From: Seth Carstens Date: Tue, 8 Dec 2015 20:37:56 -0700 Subject: [PATCH 7/9] adding gitignore for use with ignoring phpstorm project files --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file From 40ddf3410f5421289616aa1d19c9bc9e359d7bc6 Mon Sep 17 00:00:00 2001 From: Seth Carstens Date: Tue, 8 Dec 2015 22:52:45 -0700 Subject: [PATCH 8/9] added feature to redirect to the primary domain of each site --- mercator.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/mercator.php b/mercator.php index 3d62ff5..d8fddb1 100644 --- a/mercator.php +++ b/mercator.php @@ -92,6 +92,7 @@ function startup() { add_action( 'admin_init', __NAMESPACE__ . '\\load_admin', -100 ); add_action( 'delete_blog', __NAMESPACE__ . '\\clear_mappings_on_delete' ); add_action( 'muplugins_loaded', __NAMESPACE__ . '\\register_mapped_filters', -10 ); + add_action( 'init', __NAMESPACE__ . '\\maybe_redirect_to_primary_domain', 5 ); // Add CLI commands if ( defined( 'WP_CLI' ) && WP_CLI ) { @@ -327,3 +328,61 @@ function mangle_url( $url, $path, $orig_scheme = null, $site_id = 0 ) { return $mangled; } + +/** + * Get primary domain from site_id + * Note that the first active alias is assumed to be primary + * @param $site_id + * @param bool|true $with_protocol + * @return string + */ +function get_primary_domain($site_id, $with_protocol = true){ + // Add object caching to remove need to lookup primary domain multiple times + $primary_domain = wp_cache_get( 'mercator_get_primary_domain_' . $site_id ); + if ( false === $primary_domain ) { + // Settings primary domain to http host for easy fallback + $primary_domain = $_SERVER['HTTP_HOST']; + $mappings = Mapping::get_by_site($site_id); + + // If no mapping are found, return the current domain + if( ! $mappings ){ + return $primary_domain; + } + + // Check for the first active alias and assume its primary + foreach ($mappings as $mapping) { + if ($mapping->is_active()) { + $primary_domain = $mapping->get_domain(); + wp_cache_set( 'mercator_get_primary_domain', $primary_domain . $site_id ); + break; + } + } + } + + // Always allow protocal modifier to change primary domain url returned (do not cache) + if($with_protocol) { + if(is_ssl()) { + $primary_domain = 'https://'.$primary_domain; + } else { + $primary_domain = 'http://'.$primary_domain; + } + } + + return $primary_domain; +} + +/** + * Determines if an alias is set on current domain and + * redirects to primary alias url if needed + */ +function maybe_redirect_to_primary_domain(){ + $primary_domain = get_primary_domain( get_current_blog_id(), false ); + + // If a primary domain is found, and we are not on the primary domain, safely 301 redirect to it + if( $primary_domain !== $_SERVER['HTTP_HOST'] ) { + //setup redirected URL to follow the full request URI for proper SEO + $redirect_url = get_primary_domain( get_current_blog_id() ) . $_SERVER['REQUEST_URI'] . '?redirected_from=' . $_SERVER['HTTP_HOST']; + wp_safe_redirect( $redirect_url , 301); + exit; + } +} \ No newline at end of file From de127bcb28f7bef406589f4fda33b0f9edca16e8 Mon Sep 17 00:00:00 2001 From: Seth Carstens Date: Tue, 8 Dec 2015 23:36:57 -0700 Subject: [PATCH 9/9] fixed issue with using wp_safe_redirect --- mercator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mercator.php b/mercator.php index d8fddb1..218a235 100644 --- a/mercator.php +++ b/mercator.php @@ -381,8 +381,9 @@ function maybe_redirect_to_primary_domain(){ // If a primary domain is found, and we are not on the primary domain, safely 301 redirect to it if( $primary_domain !== $_SERVER['HTTP_HOST'] ) { //setup redirected URL to follow the full request URI for proper SEO - $redirect_url = get_primary_domain( get_current_blog_id() ) . $_SERVER['REQUEST_URI'] . '?redirected_from=' . $_SERVER['HTTP_HOST']; - wp_safe_redirect( $redirect_url , 301); + $redirect_url = get_primary_domain( get_current_blog_id() ) . $_SERVER['REQUEST_URI']; + //echo $redirect_url; exit; + wp_redirect( $redirect_url , 301); exit; } } \ No newline at end of file