From f18c15c967df84feaf5dd430dae71143aa0bc10d Mon Sep 17 00:00:00 2001 From: John Shaffer Date: Thu, 17 Feb 2022 20:47:51 -0600 Subject: [PATCH] Add an option to use home URL as root for Bedrock support We don't want to do this by default yet, because this is a breaking change. --- phpstan.neon | 2 +- src/CoreOptions.php | 16 ++++++++++++++++ src/Crawler.php | 2 +- src/DetectSitemapsURLs.php | 2 +- src/SimpleRewriter.php | 13 ++++--------- src/SiteInfo.php | 10 +++++++++- views/advanced-options-page.php | 1 + 7 files changed, 33 insertions(+), 13 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index dadb1d02..862027b3 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -22,7 +22,7 @@ parameters: count: 6 - message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#' path: src/CoreOptions.php - count: 25 + count: 26 - message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#' path: src/ViewRenderer.php count: 32 diff --git a/src/CoreOptions.php b/src/CoreOptions.php index f43942ef..c1c90118 100755 --- a/src/CoreOptions.php +++ b/src/CoreOptions.php @@ -333,6 +333,16 @@ public static function optionSpecs() : array { 'Don\'t rewrite any URLs. This may give a slight speed-up when the' . ' deployment URL is the same as WordPress\'s URL.' ), + // TODO: Enable by default for new sites in a future release. + self::makeOptionSpec( + 'boolean', + 'useHomeUrlAsRoot', + '0', + 'Use WP_HOME as Crawl Root', + 'This gives better results when the WordPress home URL and site URL' + . ' are different, as in when WordPress is installed in a subdirectory.' + . ' Use this option if you are using Bedrock.' + ), ]; $ret = []; @@ -805,6 +815,12 @@ public static function savePosted( string $screen = 'core' ) : void { [ 'value' => isset( $_POST['skipURLRewrite'] ) ? 1 : 0 ], [ 'name' => 'skipURLRewrite' ] ); + + $wpdb->update( + $table_name, + [ 'value' => isset( $_POST['useHomeUrlAsRoot'] ) ? 1 : 0 ], + [ 'name' => 'useHomeUrlAsRoot' ] + ); break; } } diff --git a/src/Crawler.php b/src/Crawler.php index c79ed67e..a181de38 100755 --- a/src/Crawler.php +++ b/src/Crawler.php @@ -114,7 +114,7 @@ public function crawlSite( string $static_site_path ) : void { foreach ( $crawlable_paths as $root_relative_path ) { $absolute_uri = new URL( - rtrim( SiteInfo::getURL( 'home' ), '/' ) . $root_relative_path + rtrim( SiteInfo::getUrl( 'crawl_root' ), '/' ) . $root_relative_path ); $urls[] = [ 'url' => $absolute_uri->get(), diff --git a/src/DetectSitemapsURLs.php b/src/DetectSitemapsURLs.php index 1602b825..542beb0b 100644 --- a/src/DetectSitemapsURLs.php +++ b/src/DetectSitemapsURLs.php @@ -32,7 +32,7 @@ public static function detect() : array { null ); - $wp_site_url = SiteInfo::getURL( 'home' ); + $wp_site_url = SiteInfo::getUrl( 'crawl_root' ); $base_uri = rtrim( $wp_site_url, '/' ); if ( $port_override ) { diff --git a/src/SimpleRewriter.php b/src/SimpleRewriter.php index 9ed73edf..050dd7b3 100644 --- a/src/SimpleRewriter.php +++ b/src/SimpleRewriter.php @@ -50,21 +50,16 @@ public static function rewriteFileContents( string $file_contents ) : string CoreOptions::getValue( 'deploymentURL' ) ); - $wordpress_home_url = apply_filters( - 'wp2static_set_wordpress_home_url', - untrailingslashit( SiteInfo::getUrl( 'home' ) ) - ); - - $wordpress_home_url = untrailingslashit( $wordpress_home_url ); + $crawl_root = untrailingslashit( SiteInfo::getUrl( 'crawl_root' ) ); $destination_url = untrailingslashit( $destination_url ); $destination_url_rel = URLHelper::getProtocolRelativeURL( $destination_url ); $destination_url_rel_c = addcslashes( $destination_url_rel, '/' ); $replacement_patterns = [ - $wordpress_home_url => $destination_url, - URLHelper::getProtocolRelativeURL( $wordpress_home_url ) => + $crawl_root => $destination_url, + URLHelper::getProtocolRelativeURL( $crawl_root ) => URLHelper::getProtocolRelativeURL( $destination_url ), - addcslashes( URLHelper::getProtocolRelativeURL( $wordpress_home_url ), '/' ) => + addcslashes( URLHelper::getProtocolRelativeURL( $crawl_root ), '/' ) => addcslashes( URLHelper::getProtocolRelativeURL( $destination_url ), '/' ), ]; diff --git a/src/SiteInfo.php b/src/SiteInfo.php index 7acea3a7..8f620e48 100755 --- a/src/SiteInfo.php +++ b/src/SiteInfo.php @@ -27,13 +27,21 @@ class SiteInfo { */ public function __construct() { $upload_path_and_url = wp_upload_dir(); + $home_url = trailingslashit( get_home_url() ); $site_url = trailingslashit( site_url() ); + if ( (int) CoreOptions::getValue( 'useHomeUrlAsRoot' ) === 1 ) { + $crawl_root_url = $home_url; + } else { + $crawl_root_url = $site_url; + } + // properties which should not change during plugin execution self::$info = apply_filters( 'wp2static_siteinfo', [ // Core + 'crawl_root_url' => $crawl_root_url, 'site_path' => ABSPATH, 'site_url' => $site_url, @@ -41,7 +49,7 @@ public function __construct() { Note: 'home_path' => get_home_path(), // errors trying to find it in WP2Static\get_home_path()... */ - 'home_url' => trailingslashit( get_home_url() ), + 'home_url' => $home_url, 'includes_path' => trailingslashit( ABSPATH . WPINC ), 'includes_url' => includes_url(), diff --git a/views/advanced-options-page.php b/views/advanced-options-page.php index f1662e18..f3c6f6bd 100644 --- a/views/advanced-options-page.php +++ b/views/advanced-options-page.php @@ -43,6 +43,7 @@ +