resolve_destination_url( $source ); // Safely permit destination host if external. if ( Fetcher::is_remote( $destination ) ) { $parsed_host = wp_parse_url( $destination, PHP_URL_HOST ); if ( ! empty( $parsed_host ) ) { add_filter( 'allowed_redirect_hosts', function( $hosts ) use ( $parsed_host ) { $hosts[] = $parsed_host; return $hosts; } ); } } // Strict HTTP 301 using wp_safe_redirect. wp_safe_redirect( $destination, 301 ); exit; } /** * Renders an informative notification banner for logged-in Admins/Editors. */ public function render_admin_preview_banner() { if ( ! is_page() || ! current_user_can( 'edit_pages' ) || $this->banner_rendered ) { return; } $post_id = get_queried_object_id(); if ( '1' !== get_post_meta( $post_id, Mapper::META_MANAGED, true ) ) { return; } $source = get_post_meta( $post_id, Mapper::META_SOURCE_URL, true ); $destination = $this->resolve_destination_url( $source ); $this->banner_rendered = true; ?>
banner_rendered ) { $this->render_admin_preview_banner(); } } /** * Resolves relative or remote destination URL. * * @param string $source * @return string */ public function resolve_destination_url( $source ) { if ( Fetcher::is_remote( $source ) ) { return $source; } return home_url( '/' . ltrim( $source, '/' ) ); } }