|
| 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