From f93008fbaf0bf1973f02c1ce3dee71db65880705 Mon Sep 17 00:00:00 2001 From: wpalani Date: Sun, 3 Nov 2024 16:33:52 -0700 Subject: [PATCH 1/7] Redirect interceptor --- includes/Application.php | 1 + includes/ExternalRedirectInterceptor.php | 38 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 includes/ExternalRedirectInterceptor.php diff --git a/includes/Application.php b/includes/Application.php index 47754709d..05aecab97 100644 --- a/includes/Application.php +++ b/includes/Application.php @@ -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() ) { diff --git a/includes/ExternalRedirectInterceptor.php b/includes/ExternalRedirectInterceptor.php new file mode 100644 index 000000000..10c052ff9 --- /dev/null +++ b/includes/ExternalRedirectInterceptor.php @@ -0,0 +1,38 @@ + Date: Sun, 3 Nov 2024 17:20:56 -0700 Subject: [PATCH 2/7] Fix lint --- includes/ExternalRedirectInterceptor.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/ExternalRedirectInterceptor.php b/includes/ExternalRedirectInterceptor.php index 10c052ff9..b26e29576 100644 --- a/includes/ExternalRedirectInterceptor.php +++ b/includes/ExternalRedirectInterceptor.php @@ -10,8 +10,11 @@ * The only allowed redirect is to the brand plugin page. */ class ExternalRedirectInterceptor { + /** + * Constructor. + */ public function __construct() { - if ( ! isset( $_GET['page'] ) || WP_Admin::$slug !== \sanitize_text_field( $_GET['page'] ) ) { + if ( ! isset( $_GET['page'] ) || \sanitize_text_field( $_GET['page'] ) !== WP_Admin::$slug ) { return; } @@ -23,7 +26,7 @@ public function __construct() { * * @param string $location The location to redirect to. */ - public function wp_redirect($location): string { + public function wp_redirect( $location ): string { $runtime_data = Data::runtime(); $brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage']; From 607c492c64b9ca1ffabaabd4400323a623ac93a9 Mon Sep 17 00:00:00 2001 From: wpalani Date: Mon, 4 Nov 2024 15:22:29 -0700 Subject: [PATCH 3/7] Make redirect logic more readible --- includes/ExternalRedirectInterceptor.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/includes/ExternalRedirectInterceptor.php b/includes/ExternalRedirectInterceptor.php index b26e29576..91585a99f 100644 --- a/includes/ExternalRedirectInterceptor.php +++ b/includes/ExternalRedirectInterceptor.php @@ -27,11 +27,12 @@ public function __construct() { * @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']; + $runtime_data = Data::runtime(); + $brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage']; + $location_is_brand_plugin_url = strpos( $location, $brand_plugin_url ); // Intercept if the redirect is going anywhere other than the brand plugin page. - if ( strpos( $location, $brand_plugin_url ) !== 0 ) { + if ( false === $location_is_brand_plugin_url || 0 !== $location_is_brand_plugin_url ) { return ''; } From ca5e6533f31c23ef81fd81fb446a91ff036623b0 Mon Sep 17 00:00:00 2001 From: wpalani Date: Mon, 4 Nov 2024 15:47:21 -0700 Subject: [PATCH 4/7] Make logic more defensive to prevent fatal errors if a value is not present --- includes/ExternalRedirectInterceptor.php | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/includes/ExternalRedirectInterceptor.php b/includes/ExternalRedirectInterceptor.php index 91585a99f..dfaa57c66 100644 --- a/includes/ExternalRedirectInterceptor.php +++ b/includes/ExternalRedirectInterceptor.php @@ -28,7 +28,30 @@ public function __construct() { */ public function wp_redirect( $location ): string { $runtime_data = Data::runtime(); - $brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage']; + $brand_plugin_url = ''; + + /* + * Get the brand plugin page URL from the runtime data. + */ + // Check if the current brand is set and is an array. + if ( + isset( $runtime_data['currentBrand'] ) && + is_array( $runtime_data['currentBrand'] ) + ) { + // Check if the pluginDashboardPage key is set and is a string. + if ( + isset( $runtime_data['currentBrand']['pluginDashboardPage'] ) && + is_string( $runtime_data['currentBrand']['pluginDashboardPage'] ) + ) { + // Set the brand plugin page URL. + $brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage']; + } + } + + // Intercept if the redirect if the brand plugin page URL is empty. + if ( empty( $brand_plugin_url ) ) { + return ''; + } $location_is_brand_plugin_url = strpos( $location, $brand_plugin_url ); // Intercept if the redirect is going anywhere other than the brand plugin page. From 4d75d4104d016e60782bcaf0a11c0db47ff83783 Mon Sep 17 00:00:00 2001 From: wpalani Date: Mon, 4 Nov 2024 16:11:05 -0700 Subject: [PATCH 5/7] Redirect if the brand plugin page URL is empty --- includes/ExternalRedirectInterceptor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/ExternalRedirectInterceptor.php b/includes/ExternalRedirectInterceptor.php index dfaa57c66..9ced47aa0 100644 --- a/includes/ExternalRedirectInterceptor.php +++ b/includes/ExternalRedirectInterceptor.php @@ -48,9 +48,9 @@ public function wp_redirect( $location ): string { } } - // Intercept if the redirect if the brand plugin page URL is empty. + // Redirect if the brand plugin page URL is empty. if ( empty( $brand_plugin_url ) ) { - return ''; + return $location; } $location_is_brand_plugin_url = strpos( $location, $brand_plugin_url ); From 7b68b5762542b29c39332ef472f758e48f47195b Mon Sep 17 00:00:00 2001 From: "A. Alani" <38976631+wpalani@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:30:09 -0700 Subject: [PATCH 6/7] Update includes/ExternalRedirectInterceptor.php Co-authored-by: Micah Wood --- includes/ExternalRedirectInterceptor.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/includes/ExternalRedirectInterceptor.php b/includes/ExternalRedirectInterceptor.php index 9ced47aa0..519b5abed 100644 --- a/includes/ExternalRedirectInterceptor.php +++ b/includes/ExternalRedirectInterceptor.php @@ -35,13 +35,8 @@ public function wp_redirect( $location ): string { */ // Check if the current brand is set and is an array. if ( - isset( $runtime_data['currentBrand'] ) && - is_array( $runtime_data['currentBrand'] ) - ) { - // Check if the pluginDashboardPage key is set and is a string. - if ( - isset( $runtime_data['currentBrand']['pluginDashboardPage'] ) && - is_string( $runtime_data['currentBrand']['pluginDashboardPage'] ) + isset( $runtime_data['currentBrand'], $runtime_data['currentBrand']['pluginDashboardPage'] ) && + is_string( $runtime_data['currentBrand']['pluginDashboardPage'] ) ) { // Set the brand plugin page URL. $brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage']; From a413f792ba8b05f08ebd424ccbe5201ab1ae4ff5 Mon Sep 17 00:00:00 2001 From: wpalani Date: Mon, 4 Nov 2024 16:34:30 -0700 Subject: [PATCH 7/7] Lint/errors --- includes/ExternalRedirectInterceptor.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/includes/ExternalRedirectInterceptor.php b/includes/ExternalRedirectInterceptor.php index 519b5abed..850089b3a 100644 --- a/includes/ExternalRedirectInterceptor.php +++ b/includes/ExternalRedirectInterceptor.php @@ -30,17 +30,13 @@ public function wp_redirect( $location ): string { $runtime_data = Data::runtime(); $brand_plugin_url = ''; - /* - * Get the brand plugin page URL from the runtime data. - */ - // Check if the current brand is set and is an array. + // Get the brand plugin page URL from the runtime data. if ( isset( $runtime_data['currentBrand'], $runtime_data['currentBrand']['pluginDashboardPage'] ) && is_string( $runtime_data['currentBrand']['pluginDashboardPage'] ) - ) { + ) { // Set the brand plugin page URL. $brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage']; - } } // Redirect if the brand plugin page URL is empty.