Skip to content

Commit 7b210af

Browse files
authored
Merge pull request #240 from wpengine/feat-enable-faust-migration
feat: Enable Faust integration
2 parents cc32dea + 2441449 commit 7b210af

File tree

3 files changed

+189
-2
lines changed

3 files changed

+189
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public static function add_hook_actions(): void {
7777
// iframe preview functionality.
7878
add_filter( 'template_include', [ self::class, 'add_iframe_preview_template' ], 10, 1 );
7979

80-
// Preview link functionality.
81-
add_filter( 'preview_post_link', [ self::class, 'update_preview_post_link' ], 10, 2 );
80+
// Preview link functionality. Extra priority to ensure it runs after the Faust preview link filter.
81+
add_filter( 'preview_post_link', [ self::class, 'update_preview_post_link' ], 1001, 2 );
8282

8383

8484
/**
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HWP\Previews\Integration;
6+
7+
use HWP\Previews\Admin\Settings\Helper\Settings_Group;
8+
use HWP\Previews\Post\Type\Post_Types_Config_Registry;
9+
10+
class Faust_Integration {
11+
/**
12+
* The key for the admin notice.
13+
*
14+
* @var string
15+
*/
16+
public const FAUST_NOTICE_KEY = 'hwp_previews_faust_notice';
17+
18+
/**
19+
* Whether Faust is enabled.
20+
*/
21+
public static bool $faust_enabled = false;
22+
23+
/**
24+
* Initialize the hooks for the preview functionality.
25+
*/
26+
public static function init(): void {
27+
self::$faust_enabled = self::is_faust_enabled();
28+
29+
self::configure_faust();
30+
}
31+
32+
/**
33+
* Configure Faust settings and remove conflicting filters.
34+
*/
35+
public static function configure_faust(): void {
36+
if ( self::$faust_enabled ) {
37+
self::set_default_faust_settings();
38+
39+
// Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation.
40+
remove_filter( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link', 1000 );
41+
42+
self::display_faust_admin_notice();
43+
}
44+
}
45+
46+
/**
47+
* Checks if Faust is enabled.
48+
*/
49+
public static function is_faust_enabled(): bool {
50+
if ( function_exists( 'is_plugin_active' ) ) {
51+
return is_plugin_active( 'faustwp/faustwp.php' );
52+
}
53+
54+
return false;
55+
}
56+
57+
/**
58+
* Returns the Faust frontend URL from settings or a default value.
59+
*/
60+
public static function get_faust_frontend_url(): string {
61+
$default_value = 'http://localhost:3000';
62+
63+
if ( self::$faust_enabled && function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) {
64+
$frontend_uri = \WPE\FaustWP\Settings\faustwp_get_setting( 'frontend_uri', '' );
65+
66+
if ( ! empty( $frontend_uri ) ) {
67+
return $frontend_uri;
68+
}
69+
}
70+
71+
return $default_value;
72+
}
73+
74+
/**
75+
* Get default preview URL for Faust.
76+
*/
77+
public static function get_faust_preview_url(): string {
78+
return self::get_faust_frontend_url() . '/preview?p={ID}&preview=true&previewPathname=p{ID}&typeName={type}';
79+
}
80+
81+
/**
82+
* Sets default Faust settings if there are no existing settings.
83+
*/
84+
public static function set_default_faust_settings(): void {
85+
$settings_group = Settings_Group::get_instance();
86+
$types_config = apply_filters(
87+
'hwp_previews_hooks_post_type_config',
88+
Post_Types_Config_Registry::get_post_type_config()
89+
);
90+
91+
92+
$plugin_settings = $settings_group->get_cached_settings();
93+
94+
if ( ! empty( $plugin_settings ) ) {
95+
return;
96+
}
97+
98+
$setting_preview_key = $settings_group->get_settings_key_preview_url();
99+
$setting_enabled_key = $settings_group->get_settings_key_enabled();
100+
101+
$default_settings = [];
102+
103+
foreach ( $types_config->get_public_post_types() as $key => $label ) {
104+
$default_settings[ $key ] = [
105+
$setting_enabled_key => true,
106+
$setting_preview_key => self::get_faust_preview_url(),
107+
];
108+
}
109+
110+
update_option( HWP_PREVIEWS_SETTINGS_KEY, $default_settings );
111+
}
112+
113+
/**
114+
* Dismiss the Faust admin notice.
115+
*/
116+
public static function dismiss_faust_admin_notice(): void {
117+
update_user_meta( get_current_user_id(), self::FAUST_NOTICE_KEY, 1 );
118+
}
119+
120+
/**
121+
* Register admin notice to inform users about Faust integration.
122+
*/
123+
public static function register_faust_admin_notice(): void {
124+
add_action( 'admin_notices', static function (): void {
125+
$screen = get_current_screen();
126+
127+
// Exit if not this plugin's settings page.
128+
if ( ! is_object( $screen ) || 'settings_page_hwp-previews' !== $screen->id ) {
129+
return;
130+
}
131+
132+
$ajax_nonce = wp_create_nonce( self::FAUST_NOTICE_KEY );
133+
?>
134+
135+
<div id="<?php echo esc_attr( self::FAUST_NOTICE_KEY ); ?>" class="notice notice-info is-dismissible">
136+
<p><?php esc_html_e( 'HWP Previews is automatically configured to support Faust previews. However, you can still customize it to fit your needs.', 'hwp-previews' ); ?></p>
137+
</div>
138+
139+
<script>
140+
window.addEventListener( 'load', function() {
141+
const dismissBtn = document.querySelector( '#<?php echo esc_attr( self::FAUST_NOTICE_KEY ); ?> .notice-dismiss' );
142+
143+
dismissBtn?.addEventListener( 'click', function( event ) {
144+
let postData = new FormData();
145+
postData.append('action', '<?php echo esc_attr( self::FAUST_NOTICE_KEY ); ?>');
146+
postData.append('_ajax_nonce', '<?php echo esc_html( $ajax_nonce ); ?>');
147+
148+
window.fetch('<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', {
149+
method: 'POST',
150+
body: postData,
151+
})
152+
});
153+
});
154+
</script>
155+
156+
<?php
157+
}, 10, 0);
158+
}
159+
160+
/**
161+
* If Faust is enabled, show an admin notice about the migration on the settings page.
162+
*/
163+
public static function display_faust_admin_notice(): void {
164+
$is_dismissed = get_user_meta( get_current_user_id(), self::FAUST_NOTICE_KEY, true );
165+
166+
// Exit if Faust is not enabled or if the notice has been dismissed.
167+
if ( ! self::$faust_enabled || (bool) $is_dismissed ) {
168+
return;
169+
}
170+
171+
self::register_faust_admin_notice();
172+
173+
// Register the AJAX action for dismissing the notice.
174+
add_action( 'wp_ajax_' . self::FAUST_NOTICE_KEY, static function (): void {
175+
// Exit if the action is not set or does not match the expected key.
176+
if ( ! isset( $_POST['action'] ) || esc_attr( self::FAUST_NOTICE_KEY ) !== $_POST['action'] ) {
177+
return;
178+
}
179+
180+
check_ajax_referer( self::FAUST_NOTICE_KEY );
181+
182+
self::dismiss_faust_admin_notice();
183+
}, 10, 0 );
184+
}
185+
}

plugins/hwp-previews/src/Plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use HWP\Previews\Admin\Settings_Page;
88
use HWP\Previews\Hooks\Preview_Hooks;
9+
use HWP\Previews\Integration\Faust_Integration;
910

1011
if ( ! class_exists( Plugin::class ) ) :
1112

@@ -52,6 +53,7 @@ public function setup(): void {
5253
}
5354

5455
Preview_Hooks::init();
56+
Faust_Integration::init();
5557
}
5658

5759
/**

0 commit comments

Comments
 (0)