Skip to content

Commit

Permalink
Add an option to use home URL as root for Bedrock support
Browse files Browse the repository at this point in the history
We don't want to do this by default yet, because this is a breaking
change.
  • Loading branch information
john-shaffer committed Feb 18, 2022
1 parent 6cce684 commit f18c15c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/CoreOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/DetectSitemapsURLs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
13 changes: 4 additions & 9 deletions src/SimpleRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ), '/' ),
];

Expand Down
10 changes: 9 additions & 1 deletion src/SiteInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,29 @@ 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,

/*
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(),

Expand Down
1 change: 1 addition & 0 deletions views/advanced-options-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<?php echo $row( 'crawlConcurrency' ); ?>
<?php echo $row( 'skipURLRewrite' ); ?>
<?php echo $row( 'hostsToRewrite' ); ?>
<?php echo $row( 'useHomeUrlAsRoot' ); ?>
</tbody>
</table>

Expand Down

0 comments on commit f18c15c

Please sign in to comment.