Last active
June 4, 2022 15:03
-
-
Save wpmudev-sls/8d87d559dfd539bff29cbdd80c3bbe79 to your computer and use it in GitHub Desktop.
[SmartCrawl] Intercept Redirections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: [SmartCrawl] Intercept Redirections | |
| * Plugin URI: https://premium.wpmudev.org/ | |
| * Description: Keeps user activation key (generated by Forminator) and performs other tweaks on redirect. | |
| * Author: Glauber Silva @ WPMUDEV | |
| * Author URI: https://premium.wpmudev.org/ | |
| * Task: SLS-1872 | |
| * License: GPLv2 or later | |
| * | |
| * @package WPMUDEV_SmartCrawl_Intercept_Redirections | |
| */ | |
| defined( 'ABSPATH' ) || exit; | |
| if ( ! class_exists( 'WPMUDEV_SmartCrawl_Intercept_Redirections' ) ) { | |
| /** | |
| * Main class of the plugin. | |
| */ | |
| class WPMUDEV_SmartCrawl_Intercept_Redirections { | |
| /** | |
| * Determines if should keep the query strings on the redirections. | |
| * | |
| * @var Property | |
| */ | |
| private $keep_query = false; | |
| /** | |
| * Stores the source URL from the redirection. | |
| * | |
| * @var Property | |
| */ | |
| private $current_url = null; | |
| /** | |
| * Stores the main instance of the class. | |
| * | |
| * @var Property | |
| */ | |
| private static $instance = null; | |
| /** | |
| * Returns the main instance of the class. | |
| * | |
| * @return WPMUDEV_SmartCrawl_Intercept_Redirections | |
| */ | |
| public static function get_instance() { | |
| if ( is_null( self::$instance ) ) { | |
| self::$instance = new WPMUDEV_SmartCrawl_Intercept_Redirections(); | |
| } | |
| return self::$instance; | |
| } | |
| /** | |
| * Constructor of the class. | |
| */ | |
| private function __construct() { | |
| $this->init(); | |
| } | |
| /** | |
| * Loads the functions of the class in the apropriete hooks. | |
| */ | |
| public function init() { | |
| add_filter( 'wds-model-redirection-current_url', array( $this, 'check_source_url_before_redirect' ) ); | |
| add_filter( 'wds-model-redirection-get-all', array( $this, 'smartcrawl_intercept_redirections' ) ); | |
| add_filter( 'wp_redirect', array( $this, 'keeps_forminator_user_activation_key_on_redirect' ) ); | |
| } | |
| public function check_source_url_before_redirect( $source ) { | |
| $this->current_url = $source; | |
| return $source; | |
| } | |
| public function smartcrawl_intercept_redirections( $redirections ) { | |
| if ( wp_doing_ajax() && ! empty( $_GET ) ) { | |
| if ( isset( $_GET['page'] ) && 'forminator_activation' === $_GET['page'] && isset( $_GET['key'] ) ) { | |
| return $redirections; | |
| } | |
| // Prevent problems with "/?wc-ajax=update_order_review" on the checkout page and also with other ajax functions from other plugins. | |
| $redirections = array(); | |
| } elseif ( $this->keep_query && isset( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], '?' ) ) { // Keeps the query strings on redirect. | |
| foreach ( $redirections as $key => $value ) { | |
| $redirections[ $key ] = $value . '?' . explode( '?', $_SERVER['REQUEST_URI'], 2 )[1]; | |
| } | |
| } | |
| return $redirections; | |
| } | |
| public function keeps_forminator_user_activation_key_on_redirect( $location ) { | |
| if ( isset( $_GET['page'] ) && 'forminator_activation' === $_GET['page'] && isset( $_GET['key'] ) ) { | |
| if ( false === strpos( $location, '?') ) { | |
| $location .= '?page=forminator_activation&key=' . $_GET['key']; | |
| } | |
| } | |
| return $location; | |
| } | |
| } | |
| add_action( | |
| 'plugins_loaded', | |
| function() { | |
| return WPMUDEV_SmartCrawl_Intercept_Redirections::get_instance(); | |
| } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment