Skip to content

Commit baaf821

Browse files
committed
Fixed issues after refactoring Faust Integration. e2e and code quality caught the issues.
1 parent d86ad04 commit baaf821

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare( strict_types=1 );
3+
declare(strict_types=1);
44

55
namespace HWP\Previews\Integration;
66

@@ -16,6 +16,11 @@ class Faust_Integration {
1616
*/
1717
public const FAUST_NOTICE_KEY = 'hwp_previews_faust_notice';
1818

19+
/**
20+
* Whether Faust is enabled.
21+
*/
22+
public bool $faust_enabled = false;
23+
1924
/**
2025
* The instance of the Faust integration.
2126
*
@@ -24,11 +29,10 @@ class Faust_Integration {
2429
protected static ?Faust_Integration $instance = null;
2530

2631
/**
27-
* Whether Faust is enabled.
32+
* Faust_Integration constructor.
33+
*
34+
* Initializes the Faust integration if Faust is enabled.
2835
*/
29-
public bool $faust_enabled = false;
30-
31-
3236
public function __construct() {
3337
$this->faust_enabled = $this->is_faust_enabled();
3438

@@ -39,6 +43,17 @@ public function __construct() {
3943
$this->configure_faust();
4044
}
4145

46+
/**
47+
* Initialize the hooks for the preview functionality.
48+
*/
49+
public static function init(): Faust_Integration {
50+
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {
51+
self::$instance = new self();
52+
}
53+
54+
return self::$instance;
55+
}
56+
4257
/**
4358
* Checks if Faust is enabled.
4459
*/
@@ -57,29 +72,6 @@ public function get_faust_enabled(): bool {
5772
return $this->faust_enabled;
5873
}
5974

60-
/**
61-
* Initialize the hooks for the preview functionality.
62-
*/
63-
public static function init(): Faust_Integration {
64-
if ( ! isset( self::$instance ) || ! ( is_a( self::$instance, self::class ) ) ) {
65-
self::$instance = new self();
66-
}
67-
68-
return self::$instance;
69-
}
70-
71-
/**
72-
* Configure Faust settings and remove conflicting filters.
73-
*/
74-
protected function configure_faust(): void {
75-
$this->set_default_faust_settings();
76-
77-
// Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation.
78-
remove_filter( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link', 1000 );
79-
80-
$this->display_faust_admin_notice();
81-
}
82-
8375
/**
8476
* Returns the Faust frontend URL from settings or a default value.
8577
*/
@@ -134,18 +126,11 @@ public function set_default_faust_settings(): void {
134126
update_option( HWP_PREVIEWS_SETTINGS_KEY, $default_settings );
135127
}
136128

137-
/**
138-
* Dismiss the Faust admin notice.
139-
*/
140-
public function dismiss_faust_admin_notice(): void {
141-
update_user_meta( get_current_user_id(), self::FAUST_NOTICE_KEY, 1 );
142-
}
143-
144129
/**
145130
* Register admin notice to inform users about Faust integration.
146131
*/
147132
public function register_faust_admin_notice(): void {
148-
add_action( 'admin_notices', function (): void {
133+
add_action( 'admin_notices', static function (): void {
149134
$screen = get_current_screen();
150135

151136
// Exit if not this plugin's settings page.
@@ -181,6 +166,25 @@ public function register_faust_admin_notice(): void {
181166
}, 10, 0 );
182167
}
183168

169+
/**
170+
* Dismiss the Faust admin notice.
171+
*/
172+
public static function dismiss_faust_admin_notice(): void {
173+
update_user_meta( get_current_user_id(), self::FAUST_NOTICE_KEY, 1 );
174+
}
175+
176+
/**
177+
* Configure Faust settings and remove conflicting filters.
178+
*/
179+
protected function configure_faust(): void {
180+
$this->set_default_faust_settings();
181+
182+
// Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation.
183+
remove_filter( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link', 1000 );
184+
185+
$this->display_faust_admin_notice();
186+
}
187+
184188
/**
185189
* If Faust is enabled, show an admin notice about the migration on the settings page.
186190
*/
@@ -195,7 +199,7 @@ protected function display_faust_admin_notice(): void {
195199
self::register_faust_admin_notice();
196200

197201
// Register the AJAX action for dismissing the notice.
198-
add_action( 'wp_ajax_' . self::FAUST_NOTICE_KEY, function (): void {
202+
add_action( 'wp_ajax_' . self::FAUST_NOTICE_KEY, static function (): void {
199203
// Exit if the action is not set or does not match the expected key.
200204
if ( ! isset( $_POST['action'] ) || esc_attr( self::FAUST_NOTICE_KEY ) !== $_POST['action'] ) {
201205
return;

plugins/hwp-previews/src/Templates/admin.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use HWP\Previews\Integration\Faust_Integration;
66

7-
$hwp_previews_tabs_config = (array) get_query_var( 'hwp_previews_main_page_config' );
8-
$hwp_previews_current_tab = (string) ( $hwp_previews_tabs_config['current_tab'] ?? '' );
9-
$hwp_previews_tabs = (array) ( $hwp_previews_tabs_config['tabs'] ?? [] );
10-
$hwp_previews_params = (array) ( $hwp_previews_tabs_config['params'] ?? [] );
7+
$hwp_previews_tabs_config = (array) get_query_var( 'hwp_previews_main_page_config' );
8+
$hwp_previews_current_tab = (string) ( $hwp_previews_tabs_config['current_tab'] ?? '' );
9+
$hwp_previews_tabs = (array) ( $hwp_previews_tabs_config['tabs'] ?? [] );
10+
$hwp_previews_params = (array) ( $hwp_previews_tabs_config['params'] ?? [] );
11+
$hwp_previews_faust_integration = Faust_Integration::init();
1112

1213
?>
1314

@@ -61,11 +62,11 @@
6162
<div class="postbox">
6263
<h2><?php esc_html_e( 'Get Started With HWP Previews', 'hwp-previews' ); ?></h2>
6364
<div class="inside hwp-previews-docs">
64-
65+
6566
<ul>
6667
<li><a href="https://github.com/wpengine/hwptoolkit/tree/main/plugins/hwp-previews#getting-started" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Getting Started', 'hwp-previews' ); ?></a></li>
6768
<li><a href="https://github.com/wpengine/hwptoolkit/tree/main/plugins/hwp-previews#front-end-integration" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Front-End Integration', 'hwp-previews' ); ?></a></li>
68-
<?php if ( Faust_Integration::$faust_enabled ) : ?>
69+
<?php if ( $hwp_previews_faust_integration->get_faust_enabled() ) : ?>
6970
<li><a href="https://github.com/wpengine/hwptoolkit/tree/main/plugins/hwp-previews#using-with-faustjs" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Using With Faust.js', 'hwp-previews' ); ?></a></li>
7071
<?php endif; ?>
7172
<li><a href="https://github.com/wpengine/hwptoolkit/tree/main/plugins/hwp-previews#extending-the-functionality" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Extending the Functionality', 'hwp-previews' ); ?></a></li>

0 commit comments

Comments
 (0)