Skip to content

Commit 9db62d5

Browse files
authored
Refactor core resource URL filtering and multisite handling (#157)
* move the is login url check up to simplify the main site check later * apply the filter to all sites * tabs * move multisite handling into the function * bump filters version * add docblock
1 parent e5e245f commit 9db62d5

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

web/app/mu-plugins/filters.php

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Pantheon WordPress Filters
44
* Plugin URI: https://github.com/pantheon-systems/wordpress-composer-managed
55
* Description: Filters for Composer-managed WordPress sites on Pantheon.
6-
* Version: 1.2.0
6+
* Version: 1.2.1
77
* Author: Pantheon Systems
88
* Author URI: https://pantheon.io/
99
* License: MIT License
@@ -49,16 +49,16 @@
4949
*/
5050
function fix_core_resource_urls( string $url ) : string {
5151
global $current_blog;
52-
$main_site_url = trailingslashit( network_site_url( '/' ) );
52+
$main_site_url = trailingslashit( is_multisite() ? network_site_url( '/' ) : home_url() );
5353

5454
// Get the current site path. Covers a variety of scenarios since we're using this function on a bunch of different filters.
55-
$current_site_path = '/'; // Define a default path.
56-
if ( isset( $current_blog ) && ! empty( $current_blog->path ) ) {
57-
$current_site_path = trailingslashit( $current_blog->path );
58-
} elseif ( function_exists( 'get_blog_details' ) ) {
59-
$current_site_path = trailingslashit( get_blog_details()->path );
60-
} else {
61-
$current_site_path = trailingslashit( parse_url( get_home_url(), PHP_URL_PATH ) );
55+
$current_site_path = trailingslashit( parse_url( get_home_url(), PHP_URL_PATH ) ); // Define a default path.
56+
if ( is_multisite() ) {
57+
if ( isset( $current_blog ) && ! empty( $current_blog->path ) ) {
58+
$current_site_path = trailingslashit( $current_blog->path );
59+
} elseif ( function_exists( 'get_blog_details' ) ) {
60+
$current_site_path = trailingslashit( get_blog_details()->path );
61+
}
6262
}
6363

6464
// Parse the URL to get its components.
@@ -77,7 +77,7 @@ function fix_core_resource_urls( string $url ) : string {
7777
if ( strpos( $path, $current_site_path . $core_path ) !== false ) {
7878
$path = str_replace( $current_site_path . $core_path, $core_path, $path );
7979
$path_modified = true;
80-
break;
80+
break;
8181
}
8282
}
8383

@@ -97,22 +97,28 @@ function fix_core_resource_urls( string $url ) : string {
9797
return __normalize_wp_url( $new_url );
9898
}
9999

100-
// Only run the filter on non-main sites in a subdirectory multisite network.
101-
if ( is_multisite() && ! is_subdomain_install() && ! is_main_site() ) {
102-
$filters = [
103-
'script_loader_src',
104-
'style_loader_src',
105-
'plugins_url',
106-
'theme_file_uri',
107-
'stylesheet_directory_uri',
108-
'template_directory_uri',
109-
'site_url',
110-
'content_url',
111-
];
112-
foreach ( $filters as $filter ) {
113-
add_filter( $filter, __NAMESPACE__ . '\\fix_core_resource_urls', 9 );
114-
}
100+
/**
101+
* Filters to run fix_core_resource_urls on to fix the core resource URLs.
102+
*
103+
* @since 1.2.1
104+
* @see fix_core_resource_urls
105+
*/
106+
function filter_core_resource_urls() {
107+
$filters = [
108+
'script_loader_src',
109+
'style_loader_src',
110+
'plugins_url',
111+
'theme_file_uri',
112+
'stylesheet_directory_uri',
113+
'template_directory_uri',
114+
'site_url',
115+
'content_url',
116+
];
117+
foreach ( $filters as $filter ) {
118+
add_filter( $filter, __NAMESPACE__ . '\\fix_core_resource_urls', 9 );
119+
}
115120
}
121+
add_action( 'init', __NAMESPACE__ . '\\filter_core_resource_urls' );
116122

117123
/**
118124
* Prepopulate GraphQL endpoint URL with default value if unset.
@@ -146,7 +152,7 @@ function prepopulate_graphql_endpoint_url() {
146152
* @return string The filtered URL.
147153
*/
148154
function adjust_main_site_urls( string $url ) : string {
149-
if ( doing_action( 'graphql_init' ) ) {
155+
if ( doing_action( 'graphql_init' ) || __is_login_url( $url ) ) {
150156
return $url;
151157
}
152158

@@ -156,7 +162,7 @@ function adjust_main_site_urls( string $url ) : string {
156162
}
157163

158164
// If this is the main site, drop the /wp.
159-
if ( is_main_site() && ! __is_login_url( $url ) ) {
165+
if ( is_main_site() ) {
160166
$url = str_replace( '/wp/', '/', $url );
161167
}
162168

0 commit comments

Comments
 (0)