Skip to content

Commit

Permalink
[CMSP-1140] Fix core resource URLs for non-main sites in subdirectory…
Browse files Browse the repository at this point in the history
… multisite network (#130)

* add filter to correct the path for core resources
for non-main sites on subdirectory multisites

* lower priority
so it's easier to override

* loop through filter hooks rather than explicitly adding individually

Co-authored-by: Phil Tyler <[email protected]>

* remove if/else chain and replace with an array to loop through

Co-authored-by: Phil Tyler <[email protected]>

---------

Co-authored-by: Phil Tyler <[email protected]>
  • Loading branch information
jazzsequence and pwtyler authored Jun 25, 2024
1 parent 0de23dd commit a75003b
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions web/app/mu-plugins/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,65 @@
add_filter( 'pantheon.multisite.config_filename', function ( $config_filename ) {
return 'config/application.php';
} );

/**
* Correct core resource URLs for non-main sites in a subdirectory multisite network.
*
* @param string $url The URL to fix.
* @return string The fixed URL.
*/
function fix_core_resource_urls( $url ) {
$main_site_url = trailingslashit( network_site_url( '/' ) );
$current_site_path = trailingslashit( get_blog_details()->path );

// Parse the URL to get its components.
$parsed_url = parse_url( $url );

// If there is no path in the URL, return it as is.
if ( ! isset( $parsed_url['path'] ) || $parsed_url['path'] === '/' ) {
return $url;
}

$core_paths = [ 'wp-includes/', 'wp-admin/', 'wp-content/' ];
$path_modified = false;

foreach ( $core_paths as $core_path ) {
if ( strpos( $path, $current_site_path . $core_path ) !== false ) {
$path = str_replace( $current_site_path . $core_path, $core_path, $path );
$path_modified = true;
break;
}
}

// If the path was not modified, return the original URL.
if ( ! $path_modified ) {
return $url;
}

// Prepend the main site URL to the modified path.
$new_url = $main_site_url . ltrim( $path, '/' );

// Append any query strings if they existed in the original URL.
if ( isset( $parsed_url['query'] ) ) {
$new_url .= '?' . $parsed_url['query'];
}

return $new_url;
}

// Only run the filter on non-main sites in a subdirectory multisite network.
if ( is_multisite() && ! is_subdomain_install() && ! is_main_site() ) {
$filters = [
'script_loader_src',
'style_loader_src',
'plugins_url',
'theme_file_uri',
'stylesheet_directory_uri',
'template_directory_uri',
'site_url',
'content_url'
];
foreach ( $filters as $filter ) {
add_filter( $filter, 'fix_core_resource_urls', 9 );
}
}

0 comments on commit a75003b

Please sign in to comment.