From 1c84e076fd24d09a574caf371a72d0685cf3991d Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Thu, 10 Jul 2025 16:21:10 +0200 Subject: [PATCH 1/6] fix: solve conflict with Faust redirect/rewrites and HWP Previews iframe mode --- .../hwp-previews/src/Hooks/Preview_Hooks.php | 8 +++ .../src/Integration/Faust_Integration.php | 53 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php index 6465051f..1d15c522 100644 --- a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php +++ b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php @@ -12,6 +12,7 @@ use HWP\Previews\Preview\Post\Post_Type_Service; use HWP\Previews\Preview\Template\Template_Resolver_Service; use HWP\Previews\Preview\Url\Preview_Url_Resolver_Service; +use HWP\Previews\Integration\Faust_Integration; use WP_Post; use WP_REST_Response; @@ -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..1d95de6d 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -202,9 +202,62 @@ 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', function(): void { + // Only run for preview URLs (e.g., ?p=ID&preview=true) + if ( isset( $_GET['preview'] ) && $_GET['preview'] === 'true' ) { + // Remove Faust's redirect callback + remove_action( 'template_redirect', 'WPE\FaustWP\Deny_Public_Access\deny_public_access', 99 ); + } + }, 10 ); + } + + /** + * Check if Faust rewrites are enabled. + * + * @return bool + */ + public function is_faust_rewrites_enabled(): bool { + return $this->get_faust_enabled() + && function_exists('\WPE\FaustWP\Settings\is_rewrites_enabled') + && \WPE\FaustWP\Settings\is_rewrites_enabled(); + } + + /** + * Replace Faust preview rewrites with the home URL. + * + * @param string $url The URL to be rewritten. + * + * @return string + */ + public function replace_faust_preview_rewrite($url): string { + if ( function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) { + $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, get_home_url(), $url ); + } + + return $url; + } + /** * If Faust is enabled, show an admin notice about the migration on the settings page. */ From 0096c3d3ac9f219f5122ccb7ea88a31d10675c0a Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Thu, 10 Jul 2025 16:36:31 +0200 Subject: [PATCH 2/6] fix: linter fixes --- .../hwp-previews/src/Hooks/Preview_Hooks.php | 4 +- .../src/Integration/Faust_Integration.php | 79 +++++++++---------- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php index 1d15c522..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; @@ -12,7 +13,6 @@ use HWP\Previews\Preview\Post\Post_Type_Service; use HWP\Previews\Preview\Template\Template_Resolver_Service; use HWP\Previews\Preview\Url\Preview_Url_Resolver_Service; -use HWP\Previews\Integration\Faust_Integration; use WP_Post; use WP_REST_Response; @@ -230,7 +230,7 @@ public function update_preview_post_link( string $preview_link, WP_Post $post ): $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() ) { + if ( $faust_helper->is_faust_rewrites_enabled() ) { return $faust_helper->replace_faust_preview_rewrite( $preview_link ); } diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 1d95de6d..f5a658da 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -181,6 +181,39 @@ public function register_faust_admin_notice(): void { }, 10, 0 ); } + /** + * Check if Faust rewrites are enabled. + */ + public function is_faust_rewrites_enabled(): bool { + return $this->get_faust_enabled() + && function_exists( '\WPE\FaustWP\Settings\is_rewrites_enabled' ) + && \WPE\FaustWP\Settings\is_rewrites_enabled(); + } + + /** + * 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' ) ) { + $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 ); + } + + return $url; + } + /** * Dismiss the Faust admin notice. */ @@ -212,52 +245,16 @@ protected function configure_faust(): void { * Disable Faust's redirect functionality for preview URLs. */ protected function disable_faust_redirects(): void { - add_action( 'template_redirect', function(): void { - // Only run for preview URLs (e.g., ?p=ID&preview=true) - if ( isset( $_GET['preview'] ) && $_GET['preview'] === 'true' ) { - // Remove Faust's redirect callback + 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 ); } - /** - * Check if Faust rewrites are enabled. - * - * @return bool - */ - public function is_faust_rewrites_enabled(): bool { - return $this->get_faust_enabled() - && function_exists('\WPE\FaustWP\Settings\is_rewrites_enabled') - && \WPE\FaustWP\Settings\is_rewrites_enabled(); - } - - /** - * Replace Faust preview rewrites with the home URL. - * - * @param string $url The URL to be rewritten. - * - * @return string - */ - public function replace_faust_preview_rewrite($url): string { - if ( function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) { - $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, get_home_url(), $url ); - } - - return $url; - } - /** * If Faust is enabled, show an admin notice about the migration on the settings page. */ From eb19233a164337678ff4ad06a91abb59e13d423c Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Thu, 10 Jul 2025 16:54:22 +0200 Subject: [PATCH 3/6] fix: linter and psalm fix --- .../hwp-previews/src/Integration/Faust_Integration.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index f5a658da..6ae4e6c9 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -185,9 +185,11 @@ public function register_faust_admin_notice(): void { * Check if Faust rewrites are enabled. */ public function is_faust_rewrites_enabled(): bool { - return $this->get_faust_enabled() - && function_exists( '\WPE\FaustWP\Settings\is_rewrites_enabled' ) - && \WPE\FaustWP\Settings\is_rewrites_enabled(); + if ( $this->get_faust_enabled() && function_exists( '\WPE\FaustWP\Settings\is_rewrites_enabled' ) ) { + return \WPE\FaustWP\Settings\is_rewrites_enabled(); + } + + return false; } /** @@ -252,7 +254,7 @@ protected function disable_faust_redirects(): void { // Remove Faust's redirect callback. remove_action( 'template_redirect', 'WPE\FaustWP\Deny_Public_Access\deny_public_access', 99 ); } - }, 10 ); + }, 10, 0 ); } /** From cf0a0405ae04e0355745a81bf53b3c9065f10739 Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Thu, 10 Jul 2025 17:16:57 +0200 Subject: [PATCH 4/6] chore: add changeset --- .changeset/twenty-snails-flow.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/twenty-snails-flow.md 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. From 1775f2d5577b4f67e7bacf2da047b68e3dc0deda Mon Sep 17 00:00:00 2001 From: Huseyn Aghayev Date: Fri, 11 Jul 2025 11:32:14 +0200 Subject: [PATCH 5/6] Update plugins/hwp-previews/src/Integration/Faust_Integration.php Co-authored-by: Colin Murphy --- plugins/hwp-previews/src/Integration/Faust_Integration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 6ae4e6c9..63b5c99c 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -186,7 +186,7 @@ public function register_faust_admin_notice(): void { */ public function is_faust_rewrites_enabled(): bool { if ( $this->get_faust_enabled() && function_exists( '\WPE\FaustWP\Settings\is_rewrites_enabled' ) ) { - return \WPE\FaustWP\Settings\is_rewrites_enabled(); + return (bool) \WPE\FaustWP\Settings\is_rewrites_enabled(); } return false; From f838634a2c5c26b787100a6afc7ae24356d98a2a Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Fri, 11 Jul 2025 11:41:11 +0200 Subject: [PATCH 6/6] fix: improve Faust preview rewrite handling --- .../src/Integration/Faust_Integration.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 63b5c99c..4b78ee1e 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -198,22 +198,22 @@ public function is_faust_rewrites_enabled(): bool { * @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' ) ) { - $frontend_uri = \WPE\FaustWP\Settings\faustwp_get_setting( 'frontend_uri' ); + if ( ! function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) { + return $url; + } - // Return the URL as is if frontend uri is empty. - if ( ! $frontend_uri ) { - return $url; - } + $frontend_uri = \WPE\FaustWP\Settings\faustwp_get_setting( 'frontend_uri' ); - $frontend_uri = trailingslashit( $frontend_uri ); - $home_url = trailingslashit( get_home_url() ); + // 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 ); - } - return $url; + return str_replace( $frontend_uri, $home_url, $url ); } /**