Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ExternalRedirectInterceptor | |
0.00% |
0 / 22 |
|
0.00% |
0 / 3 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| wp_redirect | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
56 | |||
| url_has_whitelisted_params | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | // Check if the location contains any whitelisted params. |
| 34 | $location_has_whitelisted_params = $this->url_has_whitelisted_params( $location ); |
| 35 | |
| 36 | // Get the brand plugin page URL from the runtime data. |
| 37 | if ( |
| 38 | isset( $runtime_data['currentBrand'], $runtime_data['currentBrand']['pluginDashboardPage'] ) && |
| 39 | is_string( $runtime_data['currentBrand']['pluginDashboardPage'] ) |
| 40 | ) { |
| 41 | // Set the brand plugin page URL. |
| 42 | $brand_plugin_url = $runtime_data['currentBrand']['pluginDashboardPage']; |
| 43 | } |
| 44 | |
| 45 | // Allow the redirect if it has whitelisted params or the brand plugin page URL is empty. |
| 46 | if ( $location_has_whitelisted_params || empty( $brand_plugin_url ) ) { |
| 47 | return $location; |
| 48 | } |
| 49 | |
| 50 | // Check if the location is the brand plugin page. |
| 51 | $location_is_brand_plugin_url = strpos( $location, $brand_plugin_url ); |
| 52 | // Intercept if the redirect is going anywhere other than the brand plugin page. |
| 53 | if ( false === $location_is_brand_plugin_url || 0 !== $location_is_brand_plugin_url ) { |
| 54 | return ''; |
| 55 | } |
| 56 | |
| 57 | // Allow the redirect if it's going to the brand plugin page. |
| 58 | return $location; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Check if the URL has any whitelisted params. |
| 63 | * |
| 64 | * @param string $url The URL to check. |
| 65 | * @return bool True if the URL has any whitelisted params, false otherwise. |
| 66 | */ |
| 67 | private function url_has_whitelisted_params( string $url ): bool { |
| 68 | $whitelisted_params = array( |
| 69 | 'referrer' => WP_Admin::$slug, |
| 70 | ); |
| 71 | |
| 72 | foreach ( $whitelisted_params as $key => $value ) { |
| 73 | if ( false !== strpos( $url, $key . '=' . $value ) ) { |
| 74 | return true; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | } |