Skip to content

Commit

Permalink
Redirect interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
wpalani committed Nov 3, 2024
1 parent 92f4bbe commit f93008f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions includes/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function __construct( Container $container ) {
if ( Permissions::is_authorized_admin() || Permissions::rest_is_authorized_admin() ) {
new RestAPI();
new WP_Admin();
new ExternalRedirectInterceptor();
}

if ( Permissions::is_authorized_admin() ) {
Expand Down
38 changes: 38 additions & 0 deletions includes/ExternalRedirectInterceptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace NewfoldLabs\WP\Module\Onboarding;

use NewfoldLabs\WP\Module\Onboarding\Data\Data;

/**
* Class to intercept redirect calls and filter them.
*
* The purpose of this class is to prevent any redirects while the user is on the onboarding page.
* The only allowed redirect is to the brand plugin page.
*/
class ExternalRedirectInterceptor {
public function __construct() {
if ( ! isset( $_GET['page'] ) || WP_Admin::$slug !== \sanitize_text_field( $_GET['page'] ) ) {
return;
}

\add_filter( 'wp_redirect', array( $this, 'wp_redirect' ), 10, 1 );
}

/**
* Intercept wp_redirect calls and filter them.
*
* @param string $location The location to redirect to.
*/
public function wp_redirect($location): string {
$runtime_data = Data::runtime();
$brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage'];

// Intercept if the redirect is going anywhere other than the brand plugin page.
if ( strpos( $location, $brand_plugin_url ) !== 0 ) {
return '';
}

// Allow the redirect if it's going to the brand plugin page.
return $location;
}
}

0 comments on commit f93008f

Please sign in to comment.