Skip to content

Commit 2441449

Browse files
committed
feat: make admin notice dismissible
1 parent b599c77 commit 2441449

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

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

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@
88
use HWP\Previews\Post\Type\Post_Types_Config_Registry;
99

1010
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+
1118
/**
1219
* Whether Faust is enabled.
1320
*/
14-
protected static bool $faust_enabled = false;
21+
public static bool $faust_enabled = false;
1522

1623
/**
1724
* Initialize the hooks for the preview functionality.
@@ -32,7 +39,7 @@ public static function configure_faust(): void {
3239
// Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation.
3340
remove_filter( 'preview_post_link', 'WPE\FaustWP\Replacement\post_preview_link', 1000 );
3441

35-
self::faust_admin_notice();
42+
self::display_faust_admin_notice();
3643
}
3744
}
3845

@@ -104,30 +111,75 @@ public static function set_default_faust_settings(): void {
104111
}
105112

106113
/**
107-
* If Faust is enabled, show an admin notice about the migration on the settings page.
108-
* TODO make the notice dismissible.
114+
* Dismiss the Faust admin notice.
109115
*/
110-
public static function faust_admin_notice(): void {
111-
112-
// Exit if Faust is not enabled.
113-
if ( ! self::$faust_enabled ) {
114-
return;
115-
}
116+
public static function dismiss_faust_admin_notice(): void {
117+
update_user_meta( get_current_user_id(), self::FAUST_NOTICE_KEY, 1 );
118+
}
116119

120+
/**
121+
* Register admin notice to inform users about Faust integration.
122+
*/
123+
public static function register_faust_admin_notice(): void {
117124
add_action( 'admin_notices', static function (): void {
118125
$screen = get_current_screen();
119126

120127
// Exit if not this plugin's settings page.
121128
if ( ! is_object( $screen ) || 'settings_page_hwp-previews' !== $screen->id ) {
122129
return;
123130
}
131+
132+
$ajax_nonce = wp_create_nonce( self::FAUST_NOTICE_KEY );
124133
?>
125134

126-
<div class="notice notice-info">
127-
<p><?php esc_html_e( 'HWP Previews is automatically configured to support Faust previews on the front end. However, you can still customize it to fit your needs.', 'hwp-previews' ); ?></p>
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>
128137
</div>
129138

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+
130156
<?php
131157
}, 10, 0);
132158
}
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+
}
133185
}

0 commit comments

Comments
 (0)