diff --git a/.changeset/twenty-snails-flow.md b/.changeset/twenty-snails-flow.md new file mode 100644 index 00000000..73c311bd --- /dev/null +++ b/.changeset/twenty-snails-flow.md @@ -0,0 +1,6 @@ +--- +"@wpengine/hwp-previews-wordpress-plugin": patch +--- + +1. Disables Faust front-end redirects for preview url's to solve the iframe conflict. +2. Introduced methods in Faust_Integration to replace Faust-generated preview URLs with the site’s home URL as needed. diff --git a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php index 6465051f..3eac1f4f 100644 --- a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php +++ b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php @@ -5,6 +5,7 @@ namespace HWP\Previews\Hooks; use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection; +use HWP\Previews\Integration\Faust_Integration; use HWP\Previews\Preview\Parameter\Preview_Parameter_Registry; use HWP\Previews\Preview\Post\Post_Editor_Service; use HWP\Previews\Preview\Post\Post_Preview_Service; @@ -226,6 +227,13 @@ public function update_preview_post_link( string $preview_link, WP_Post $post ): // If the iframe option is enabled, we need to resolve preview on the template redirect level. if ( $post_type_service->is_iframe() ) { + $faust_helper = new Faust_Integration(); + + // If Faust post & category rewrites enabled, we should revert the preview link rewrites. + if ( $faust_helper->is_faust_rewrites_enabled() ) { + return $faust_helper->replace_faust_preview_rewrite( $preview_link ); + } + return $preview_link; } diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 83393701..4b78ee1e 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -181,6 +181,41 @@ public function register_faust_admin_notice(): void { }, 10, 0 ); } + /** + * Check if Faust rewrites are enabled. + */ + public function is_faust_rewrites_enabled(): bool { + if ( $this->get_faust_enabled() && function_exists( '\WPE\FaustWP\Settings\is_rewrites_enabled' ) ) { + return (bool) \WPE\FaustWP\Settings\is_rewrites_enabled(); + } + + return false; + } + + /** + * Replace Faust preview rewrites with the home URL. + * + * @param string $url The URL to be rewritten. + */ + public function replace_faust_preview_rewrite($url): string { + if ( ! function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) { + return $url; + } + + $frontend_uri = \WPE\FaustWP\Settings\faustwp_get_setting( 'frontend_uri' ); + + // Return the URL as is if frontend uri is empty. + if ( ! $frontend_uri ) { + return $url; + } + + $frontend_uri = trailingslashit( $frontend_uri ); + $home_url = trailingslashit( get_home_url() ); + + + return str_replace( $frontend_uri, $home_url, $url ); + } + /** * Dismiss the Faust admin notice. */ @@ -202,9 +237,26 @@ protected function configure_faust(): void { // Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation. remove_filter( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link', 1000 ); + // Prevent Faust from redirecting preview URLs to the frontend in iframe mode. + $this->disable_faust_redirects(); + $this->display_faust_admin_notice(); } + /** + * Disable Faust's redirect functionality for preview URLs. + */ + protected function disable_faust_redirects(): void { + add_action( 'template_redirect', static function (): void { + // Only run for preview URLs (e.g., ?p=ID&preview=true). + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification not required for disabling front-end redirects. + if ( isset( $_GET['preview'] ) && 'true' === $_GET['preview'] ) { + // Remove Faust's redirect callback. + remove_action( 'template_redirect', 'WPE\FaustWP\Deny_Public_Access\deny_public_access', 99 ); + } + }, 10, 0 ); + } + /** * If Faust is enabled, show an admin notice about the migration on the settings page. */