his->full_uri ); if ( null === $pre || ! is_array( $pre ) ) { return; } $this->matched = $pre; $this->redirect_to = $pre['url_to']; } /** * Search from cache. */ private function from_cache() { $redirections = Cache::get_by_object_id_or_url( (int) get_queried_object_id(), $this->get_current_object_type(), $this->uri ); foreach ( $redirections as $redirection ) { if ( empty( $redirection->object_id ) ) { $this->cache = true; $this->set_redirection( $redirection->redirection_id ); return; } if ( trim( $redirection->from_url, '/' ) === $this->uri ) { $this->cache = true; $this->set_redirection( $redirection->redirection_id ); return; } } } /** * Search for everything rules. */ private function everything() { $redirection = DB::match_redirections( $this->uri ); if ( ! $redirection && $this->uri !== $this->full_uri ) { $redirection = DB::match_redirections( $this->full_uri ); } if ( $redirection ) { Cache::add( [ 'from_url' => $this->uri, 'redirection_id' => $redirection['id'], 'object_id' => 0, 'object_type' => 'any', 'is_redirected' => '1', ] ); $this->set_redirection( $redirection ); } } /** * Do the fallback strategy here. */ private function fallback() { if ( ! $this->can_run_fallback() ) { return; } $behavior = Helper::get_settings( 'general.redirections_fallback' ); if ( 'default' === $behavior ) { return; } if ( 'homepage' === $behavior ) { $this->matched = []; $this->redirect_to = home_url(); return; } $custom_url = Helper::get_settings( 'general.redirections_custom_url' ); if ( ! empty( $custom_url ) ) { $this->matched = []; $this->redirect_to = $custom_url; } } /** * Show debugging interstitial if enabled. */ private function do_debugging() { if ( ! Helper::get_settings( 'general.redirections_debug' ) || ! Helper::has_cap( 'redirections' ) ) { return; } new Debugger( get_object_vars( $this ) ); } /** * Set redirection by ID. * * @param integer $redirection Redirection ID to set for. */ private function set_redirection( $redirection ) { if ( ! is_array( $redirection ) ) { $redirection = DB::get_redirection_by_id( $redirection, 'active' ); } $custom_match = $this->do_filter( 'redirection/redirection_match', false, $redirection ); if ( false === $redirection || ( ! DB::compare_sources( $redirection['sources'], $this->uri ) && ! $custom_match ) ) { return; } if ( isset( $redirection['url_to'] ) ) { $this->matched = $redirection; $this->set_redirect_to(); } if ( $this->is_amp_endpoint() ) { $this->redirect_to = $this->redirect_to . amp_get_slug() . '/'; } } /** * Set redirect to. */ private function set_redirect_to() { $this->redirect_to = $this->matched['url_to']; foreach ( $this->matched['sources'] as $source ) { $this->set_redirect_to_regex( $source ); } } /** * Set redirect to by replacing using regex. * * @param array $source Source to check. */ private function set_redirect_to_regex( $source ) { if ( 'regex' !== $source['comparison'] ) { return; } $pattern = DB::get_clean_pattern( $source['pattern'], $source['comparison'] ); if ( Str::comparison( $pattern, $this->uri, $source['comparison'] ) ) { $this->redirect_to = preg_replace( $pattern, $this->redirect_to, $this->uri ); } } /** * Sets the wp_query to 404 when this is an object. */ private function set_404() { global $wp_query; $wp_query = is_object( $wp_query ) ? $wp_query : new WP_Query(); $wp_query->is_404 = true; } /** * Get the object type for the current page. * * @return string object type name. */ private function get_current_object_type() { $hash = [ 'WP_Post' => 'post', 'WP_Term' => 'term', 'WP_User' => 'user', ]; $object = get_queried_object(); if ( ! $object ) { return 'none'; } $object = get_class( $object ); return isset( $hash[ $object ] ) ? $hash[ $object ] : 'none'; } /** * Get header code. * 1. From matched redirection. * 2. From optgeneral options. * * @return int */ private function get_header_code() { $header_code = isset( $this->matched['header_code'] ) ? $this->matched['header_code'] : Helper::get_settings( 'general.redirections_header_code' ); return absint( $header_code ); } /** * Get redirect header. * * @return string */ private function get_redirect_header() { return true === $this->do_filter( 'redirection/add_redirect_header', true ) ? 'Rank Math' : 'WordPress'; } /** * Is AMP url. * * @return bool */ private function is_amp_endpoint() { return \function_exists( 'is_amp_endpoint' ) && \function_exists( 'amp_is_canonical' ) && is_amp_endpoint() && ! amp_is_canonical(); } /** * Gets the post id for the redirections' fallback. * * @return int|void */ private static function get_redirections_fallback_post_id() { $fall_back = Helper::get_settings( 'general.redirections_fallback' ); if ( in_array( $fall_back, [ 'default', 'homepage' ], true ) ) { return (int) get_option( 'page_on_front' ); } if ( Helper::get_settings( 'general.redirections_custom_url' ) ) { return url_to_postid( Helper::get_settings( 'general.redirections_custom_url' ) ); } } /** * Check if the fall_back redirect can run in the current contexts. * * @return bool */ private function can_run_fallback() { if ( ! is_404() ) { return false; } if ( ! $this->uri && $this->query_string && Str::starts_with( 'p=', trim( $this->query_string ) ) ) { $this->query_string = ''; return true; } $wp_redirect_admin_locations = $this->do_filter( 'redirection/fallback_exclude_locations', [ 'login', 'admin', 'dashboard' ] ); return $this->uri && ! in_array( $this->uri, $wp_redirect_admin_locations, true ); } }