Skip to content

Commit 4a5b99e

Browse files
authored
Merge pull request #632 from newfold-labs/add/P8-49
Redirect interceptor
2 parents 5f0d763 + e1a40e9 commit 4a5b99e

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

includes/Application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function __construct( Container $container ) {
7171
if ( Permissions::is_authorized_admin() || Permissions::rest_is_authorized_admin() ) {
7272
new RestAPI();
7373
new WP_Admin();
74+
new ExternalRedirectInterceptor();
7475
}
7576

7677
if ( Permissions::is_authorized_admin() ) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace NewfoldLabs\WP\Module\Onboarding;
3+
4+
use NewfoldLabs\WP\Module\Onboarding\Data\Data;
5+
6+
/**
7+
* Class to intercept redirect calls and filter them.
8+
*
9+
* The purpose of this class is to prevent any redirects while the user is on the onboarding page.
10+
* The only allowed redirect is to the brand plugin page.
11+
*/
12+
class ExternalRedirectInterceptor {
13+
/**
14+
* Constructor.
15+
*/
16+
public function __construct() {
17+
if ( ! isset( $_GET['page'] ) || \sanitize_text_field( $_GET['page'] ) !== WP_Admin::$slug ) {
18+
return;
19+
}
20+
21+
\add_filter( 'wp_redirect', array( $this, 'wp_redirect' ), 10, 1 );
22+
}
23+
24+
/**
25+
* Intercept wp_redirect calls and filter them.
26+
*
27+
* @param string $location The location to redirect to.
28+
*/
29+
public function wp_redirect( $location ): string {
30+
$runtime_data = Data::runtime();
31+
$brand_plugin_url = '';
32+
33+
// Get the brand plugin page URL from the runtime data.
34+
if (
35+
isset( $runtime_data['currentBrand'], $runtime_data['currentBrand']['pluginDashboardPage'] ) &&
36+
is_string( $runtime_data['currentBrand']['pluginDashboardPage'] )
37+
) {
38+
// Set the brand plugin page URL.
39+
$brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage'];
40+
}
41+
42+
// Redirect if the brand plugin page URL is empty.
43+
if ( empty( $brand_plugin_url ) ) {
44+
return $location;
45+
}
46+
47+
$location_is_brand_plugin_url = strpos( $location, $brand_plugin_url );
48+
// Intercept if the redirect is going anywhere other than the brand plugin page.
49+
if ( false === $location_is_brand_plugin_url || 0 !== $location_is_brand_plugin_url ) {
50+
return '';
51+
}
52+
53+
// Allow the redirect if it's going to the brand plugin page.
54+
return $location;
55+
}
56+
}

0 commit comments

Comments
 (0)