Skip to content

Commit 61de798

Browse files
authored
Merge pull request #333 from wpengine/fix-hwp-previews-faust-iframe-conflict
fix: solve conflict with Faust redirect/rewrites and HWP Previews iframe mode
2 parents e8bd4f2 + f838634 commit 61de798

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

.changeset/twenty-snails-flow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@wpengine/hwp-previews-wordpress-plugin": patch
3+
---
4+
5+
1. Disables Faust front-end redirects for preview url's to solve the iframe conflict.
6+
2. Introduced methods in Faust_Integration to replace Faust-generated preview URLs with the site’s home URL as needed.

plugins/hwp-previews/src/Hooks/Preview_Hooks.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace HWP\Previews\Hooks;
66

77
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
8+
use HWP\Previews\Integration\Faust_Integration;
89
use HWP\Previews\Preview\Parameter\Preview_Parameter_Registry;
910
use HWP\Previews\Preview\Post\Post_Editor_Service;
1011
use HWP\Previews\Preview\Post\Post_Preview_Service;
@@ -226,6 +227,13 @@ public function update_preview_post_link( string $preview_link, WP_Post $post ):
226227

227228
// If the iframe option is enabled, we need to resolve preview on the template redirect level.
228229
if ( $post_type_service->is_iframe() ) {
230+
$faust_helper = new Faust_Integration();
231+
232+
// If Faust post & category rewrites enabled, we should revert the preview link rewrites.
233+
if ( $faust_helper->is_faust_rewrites_enabled() ) {
234+
return $faust_helper->replace_faust_preview_rewrite( $preview_link );
235+
}
236+
229237
return $preview_link;
230238
}
231239

plugins/hwp-previews/src/Integration/Faust_Integration.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,41 @@ public function register_faust_admin_notice(): void {
181181
}, 10, 0 );
182182
}
183183

184+
/**
185+
* Check if Faust rewrites are enabled.
186+
*/
187+
public function is_faust_rewrites_enabled(): bool {
188+
if ( $this->get_faust_enabled() && function_exists( '\WPE\FaustWP\Settings\is_rewrites_enabled' ) ) {
189+
return (bool) \WPE\FaustWP\Settings\is_rewrites_enabled();
190+
}
191+
192+
return false;
193+
}
194+
195+
/**
196+
* Replace Faust preview rewrites with the home URL.
197+
*
198+
* @param string $url The URL to be rewritten.
199+
*/
200+
public function replace_faust_preview_rewrite($url): string {
201+
if ( ! function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) {
202+
return $url;
203+
}
204+
205+
$frontend_uri = \WPE\FaustWP\Settings\faustwp_get_setting( 'frontend_uri' );
206+
207+
// Return the URL as is if frontend uri is empty.
208+
if ( ! $frontend_uri ) {
209+
return $url;
210+
}
211+
212+
$frontend_uri = trailingslashit( $frontend_uri );
213+
$home_url = trailingslashit( get_home_url() );
214+
215+
216+
return str_replace( $frontend_uri, $home_url, $url );
217+
}
218+
184219
/**
185220
* Dismiss the Faust admin notice.
186221
*/
@@ -202,9 +237,26 @@ protected function configure_faust(): void {
202237
// Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation.
203238
remove_filter( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link', 1000 );
204239

240+
// Prevent Faust from redirecting preview URLs to the frontend in iframe mode.
241+
$this->disable_faust_redirects();
242+
205243
$this->display_faust_admin_notice();
206244
}
207245

246+
/**
247+
* Disable Faust's redirect functionality for preview URLs.
248+
*/
249+
protected function disable_faust_redirects(): void {
250+
add_action( 'template_redirect', static function (): void {
251+
// Only run for preview URLs (e.g., ?p=ID&preview=true).
252+
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification not required for disabling front-end redirects.
253+
if ( isset( $_GET['preview'] ) && 'true' === $_GET['preview'] ) {
254+
// Remove Faust's redirect callback.
255+
remove_action( 'template_redirect', 'WPE\FaustWP\Deny_Public_Access\deny_public_access', 99 );
256+
}
257+
}, 10, 0 );
258+
}
259+
208260
/**
209261
* If Faust is enabled, show an admin notice about the migration on the settings page.
210262
*/

0 commit comments

Comments
 (0)