Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #32 and fixes #51 #59

Closed
wants to merge 12 commits into from
Closed
28 changes: 28 additions & 0 deletions mercator.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +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 );

// If on network site, also filter network urls
if ( is_main_site() ) {
Expand Down Expand Up @@ -324,3 +326,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs an extra space after $path

//get site ID for use with getting mapped domain name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments need a space after the //, and capital letter to start

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'd drop this comment and the next (pull in site aliases) as they're not really adding much on top of the code itself here.

$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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reword? Something like 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;
}

// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd drop this comment too, I think return $mangled is self-evident already, and we don't want to overcomment :)

return $mangled;
}
7 changes: 7 additions & 0 deletions sunrise-sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
// Default mu-plugins directory if you haven't set it
defined( 'WPMU_PLUGIN_DIR' ) or define( 'WPMU_PLUGIN_DIR', WP_CONTENT_DIR . '/mu-plugins' );
//uncomment the two lines below to disable SSO (single sign on)
//add_filter( 'mercator.sso.enabled', '__return_false' );
//add_action( 'muplugins_loaded', 'Mercator\\SSO\\initialize_cookie_domain' );
require WPMU_PLUGIN_DIR . '/mercator/mercator.php';