Skip to content

Commit f18c15c

Browse files
committed
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.
1 parent 6cce684 commit f18c15c

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ parameters:
2222
count: 6
2323
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
2424
path: src/CoreOptions.php
25-
count: 25
25+
count: 26
2626
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
2727
path: src/ViewRenderer.php
2828
count: 32

src/CoreOptions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ public static function optionSpecs() : array {
333333
'Don\'t rewrite any URLs. This may give a slight speed-up when the'
334334
. ' deployment URL is the same as WordPress\'s URL.'
335335
),
336+
// TODO: Enable by default for new sites in a future release.
337+
self::makeOptionSpec(
338+
'boolean',
339+
'useHomeUrlAsRoot',
340+
'0',
341+
'Use WP_HOME as Crawl Root',
342+
'This gives better results when the WordPress home URL and site URL'
343+
. ' are different, as in when WordPress is installed in a subdirectory.'
344+
. ' Use this option if you are using Bedrock.'
345+
),
336346
];
337347

338348
$ret = [];
@@ -805,6 +815,12 @@ public static function savePosted( string $screen = 'core' ) : void {
805815
[ 'value' => isset( $_POST['skipURLRewrite'] ) ? 1 : 0 ],
806816
[ 'name' => 'skipURLRewrite' ]
807817
);
818+
819+
$wpdb->update(
820+
$table_name,
821+
[ 'value' => isset( $_POST['useHomeUrlAsRoot'] ) ? 1 : 0 ],
822+
[ 'name' => 'useHomeUrlAsRoot' ]
823+
);
808824
break;
809825
}
810826
}

src/Crawler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function crawlSite( string $static_site_path ) : void {
114114

115115
foreach ( $crawlable_paths as $root_relative_path ) {
116116
$absolute_uri = new URL(
117-
rtrim( SiteInfo::getURL( 'home' ), '/' ) . $root_relative_path
117+
rtrim( SiteInfo::getUrl( 'crawl_root' ), '/' ) . $root_relative_path
118118
);
119119
$urls[] = [
120120
'url' => $absolute_uri->get(),

src/DetectSitemapsURLs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function detect() : array {
3232
null
3333
);
3434

35-
$wp_site_url = SiteInfo::getURL( 'home' );
35+
$wp_site_url = SiteInfo::getUrl( 'crawl_root' );
3636
$base_uri = rtrim( $wp_site_url, '/' );
3737

3838
if ( $port_override ) {

src/SimpleRewriter.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,16 @@ public static function rewriteFileContents( string $file_contents ) : string
5050
CoreOptions::getValue( 'deploymentURL' )
5151
);
5252

53-
$wordpress_home_url = apply_filters(
54-
'wp2static_set_wordpress_home_url',
55-
untrailingslashit( SiteInfo::getUrl( 'home' ) )
56-
);
57-
58-
$wordpress_home_url = untrailingslashit( $wordpress_home_url );
53+
$crawl_root = untrailingslashit( SiteInfo::getUrl( 'crawl_root' ) );
5954
$destination_url = untrailingslashit( $destination_url );
6055
$destination_url_rel = URLHelper::getProtocolRelativeURL( $destination_url );
6156
$destination_url_rel_c = addcslashes( $destination_url_rel, '/' );
6257

6358
$replacement_patterns = [
64-
$wordpress_home_url => $destination_url,
65-
URLHelper::getProtocolRelativeURL( $wordpress_home_url ) =>
59+
$crawl_root => $destination_url,
60+
URLHelper::getProtocolRelativeURL( $crawl_root ) =>
6661
URLHelper::getProtocolRelativeURL( $destination_url ),
67-
addcslashes( URLHelper::getProtocolRelativeURL( $wordpress_home_url ), '/' ) =>
62+
addcslashes( URLHelper::getProtocolRelativeURL( $crawl_root ), '/' ) =>
6863
addcslashes( URLHelper::getProtocolRelativeURL( $destination_url ), '/' ),
6964
];
7065

src/SiteInfo.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,29 @@ class SiteInfo {
2727
*/
2828
public function __construct() {
2929
$upload_path_and_url = wp_upload_dir();
30+
$home_url = trailingslashit( get_home_url() );
3031
$site_url = trailingslashit( site_url() );
3132

33+
if ( (int) CoreOptions::getValue( 'useHomeUrlAsRoot' ) === 1 ) {
34+
$crawl_root_url = $home_url;
35+
} else {
36+
$crawl_root_url = $site_url;
37+
}
38+
3239
// properties which should not change during plugin execution
3340
self::$info = apply_filters(
3441
'wp2static_siteinfo',
3542
[
3643
// Core
44+
'crawl_root_url' => $crawl_root_url,
3745
'site_path' => ABSPATH,
3846
'site_url' => $site_url,
3947

4048
/*
4149
Note: 'home_path' => get_home_path(),
4250
// errors trying to find it in WP2Static\get_home_path()...
4351
*/
44-
'home_url' => trailingslashit( get_home_url() ),
52+
'home_url' => $home_url,
4553
'includes_path' => trailingslashit( ABSPATH . WPINC ),
4654
'includes_url' => includes_url(),
4755

views/advanced-options-page.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<?php echo $row( 'crawlConcurrency' ); ?>
4444
<?php echo $row( 'skipURLRewrite' ); ?>
4545
<?php echo $row( 'hostsToRewrite' ); ?>
46+
<?php echo $row( 'useHomeUrlAsRoot' ); ?>
4647
</tbody>
4748
</table>
4849

0 commit comments

Comments
 (0)