Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/twenty-snails-flow.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions plugins/hwp-previews/src/Hooks/Preview_Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
52 changes: 52 additions & 0 deletions plugins/hwp-previews/src/Integration/Faust_Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down
Loading