Skip to content

Commit 1c84e07

Browse files
committed
fix: solve conflict with Faust redirect/rewrites and HWP Previews iframe mode
1 parent e8bd4f2 commit 1c84e07

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use HWP\Previews\Preview\Post\Post_Type_Service;
1313
use HWP\Previews\Preview\Template\Template_Resolver_Service;
1414
use HWP\Previews\Preview\Url\Preview_Url_Resolver_Service;
15+
use HWP\Previews\Integration\Faust_Integration;
1516
use WP_Post;
1617
use WP_REST_Response;
1718

@@ -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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,62 @@ protected function configure_faust(): void {
202202
// Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation.
203203
remove_filter( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link', 1000 );
204204

205+
// Prevent Faust from redirecting preview URLs to the frontend in iframe mode.
206+
$this->disable_faust_redirects();
207+
205208
$this->display_faust_admin_notice();
206209
}
207210

211+
/**
212+
* Disable Faust's redirect functionality for preview URLs.
213+
*/
214+
protected function disable_faust_redirects(): void {
215+
add_action( 'template_redirect', function(): void {
216+
// Only run for preview URLs (e.g., ?p=ID&preview=true)
217+
if ( isset( $_GET['preview'] ) && $_GET['preview'] === 'true' ) {
218+
// Remove Faust's redirect callback
219+
remove_action( 'template_redirect', 'WPE\FaustWP\Deny_Public_Access\deny_public_access', 99 );
220+
}
221+
}, 10 );
222+
}
223+
224+
/**
225+
* Check if Faust rewrites are enabled.
226+
*
227+
* @return bool
228+
*/
229+
public function is_faust_rewrites_enabled(): bool {
230+
return $this->get_faust_enabled()
231+
&& function_exists('\WPE\FaustWP\Settings\is_rewrites_enabled')
232+
&& \WPE\FaustWP\Settings\is_rewrites_enabled();
233+
}
234+
235+
/**
236+
* Replace Faust preview rewrites with the home URL.
237+
*
238+
* @param string $url The URL to be rewritten.
239+
*
240+
* @return string
241+
*/
242+
public function replace_faust_preview_rewrite($url): string {
243+
if ( function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) {
244+
$frontend_uri = \WPE\FaustWP\Settings\faustwp_get_setting( 'frontend_uri' );
245+
246+
// Return the URL as is if frontend uri is empty.
247+
if ( ! $frontend_uri ) {
248+
return $url;
249+
}
250+
251+
$frontend_uri = trailingslashit( $frontend_uri );
252+
$home_url = trailingslashit( get_home_url() );
253+
254+
255+
return str_replace( $frontend_uri, get_home_url(), $url );
256+
}
257+
258+
return $url;
259+
}
260+
208261
/**
209262
* If Faust is enabled, show an admin notice about the migration on the settings page.
210263
*/

0 commit comments

Comments
 (0)