Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions plugins/faustwp/includes/deny-public-access/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ function deny_public_access() {
return;
}

$frontend_uri = trailingslashit( $frontend_uri );

// Get the request uri with query params.
$request_uri = home_url( add_query_arg( null, null ) );

$response_code = apply_filters( 'faustwp_public_redirect_status_code', 302 );
$redirect_url = str_replace( trailingslashit( get_home_url() ), $frontend_uri, $request_uri );
$redirect_url = get_public_redirect_url( add_query_arg( null, null ), $frontend_uri );
$protocols = array( 'http', 'https' );

header( 'X-Redirect-By: WP Engine Headless plugin' ); // For support teams. See https://developer.yoast.com/blog/x-redirect-by-header/.
Expand Down
29 changes: 29 additions & 0 deletions plugins/faustwp/includes/deny-public-access/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,32 @@ function doing_file_editor_save() {

return true;
}

/**
* Builds the front-end URL to redirect a public request to.
*
* Strips the WordPress home path from the request first, so a site installed in
* a subdirectory (such as https://example.com/wp) redirects to the matching
* front-end path instead of repeating the subdirectory.
*
* @param string $request_uri The request path and query string, such as `/wp/sample-page/?p=1`.
* @param string $frontend_uri The front-end site URL.
*
* @return string
*/
function get_public_redirect_url( $request_uri, $frontend_uri ) {
$home_path = untrailingslashit( (string) wp_parse_url( home_url(), PHP_URL_PATH ) );

if (
'' !== $home_path &&
(
$request_uri === $home_path ||
0 === strpos( $request_uri, $home_path . '/' ) ||
0 === strpos( $request_uri, $home_path . '?' )
)
) {
$request_uri = substr( $request_uri, strlen( $home_path ) );
}

return trailingslashit( $frontend_uri ) . ltrim( $request_uri, '/' );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Class DenyPublicAccessFunctionsTests
*
* @package FaustWP
*/

namespace WPE\FaustWP\Tests\Integration;

use function WPE\FaustWP\Deny_Public_Access\get_public_redirect_url;

class DenyPublicAccessFunctionsTests extends \WP_UnitTestCase {
protected $frontend_uri = 'http://localhost:3000';

public function test_get_public_redirect_url_keeps_path_and_query_for_root_install() {
update_option( 'home', 'http://example.org' );

$this->assertSame(
'http://localhost:3000/sample-page/?foo=bar',
get_public_redirect_url( '/sample-page/?foo=bar', $this->frontend_uri )
);
}

public function test_get_public_redirect_url_strips_subdirectory_install_path() {
update_option( 'home', 'http://example.org/wp' );

$this->assertSame(
'http://localhost:3000/sample-page/',
get_public_redirect_url( '/wp/sample-page/', $this->frontend_uri )
);
}

public function test_get_public_redirect_url_maps_subdirectory_root_to_frontend_root() {
update_option( 'home', 'http://example.org/wp/' );

$this->assertSame( 'http://localhost:3000/', get_public_redirect_url( '/wp/', $this->frontend_uri ) );
$this->assertSame( 'http://localhost:3000/', get_public_redirect_url( '/wp', $this->frontend_uri ) );
$this->assertSame( 'http://localhost:3000/?p=1', get_public_redirect_url( '/wp?p=1', $this->frontend_uri ) );
}

public function test_get_public_redirect_url_does_not_strip_a_partial_path_match() {
update_option( 'home', 'http://example.org/wp' );

$this->assertSame(
'http://localhost:3000/wpfoo/page/',
get_public_redirect_url( '/wpfoo/page/', $this->frontend_uri )
);
}

public function test_get_public_redirect_url_keeps_frontend_path() {
update_option( 'home', 'http://example.org/wp' );

$this->assertSame(
'https://example.com/app/sample-page/',
get_public_redirect_url( '/wp/sample-page/', 'https://example.com/app' )
);
}
}
Loading