From 77038af60183db5fe809aa4a5047182885e83c04 Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Wed, 4 Jun 2025 15:05:58 +0200 Subject: [PATCH 1/6] feat: faust integration --- .../hwp-previews/src/Hooks/Preview_Hooks.php | 4 +- .../src/Integration/Faust_Integration.php | 154 ++++++++++++++++++ plugins/hwp-previews/src/Plugin.php | 2 + 3 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 plugins/hwp-previews/src/Integration/Faust_Integration.php diff --git a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php index fe6c2d49..e9889bea 100644 --- a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php +++ b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php @@ -77,8 +77,8 @@ public static function add_hook_actions(): void { // iframe preview functionality. add_filter( 'template_include', [ self::class, 'add_iframe_preview_template' ], 10, 1 ); - // Preview link functionality. - add_filter( 'preview_post_link', [ self::class, 'update_preview_post_link' ], 10, 2 ); + // Preview link functionality. Extra priority to ensure it runs after the Faust preview link filter. + add_filter( 'preview_post_link', [ self::class, 'update_preview_post_link' ], 1001, 2 ); /** diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php new file mode 100644 index 00000000..01698354 --- /dev/null +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -0,0 +1,154 @@ +get_cached_settings(); + + if( empty($plugin_settings) ) { + $setting_preview_key = self::$settings_group->get_settings_key_preview_url(); + $setting_enabled_key = self::$settings_group->get_settings_key_enabled(); + + $default_settings = array(); + + foreach ( self::$types_config->get_public_post_types() as $key => $label ) { + $default_settings[$key] = array( + $setting_enabled_key => true, + $setting_preview_key => self::get_faust_preview_url(), + ); + } + + update_option(HWP_PREVIEWS_SETTINGS_KEY, $default_settings); + } + } + + /** + * If Faust is enabled, show an admin notice about the migration on the settings page. + * TODO make the notice dismissible. + */ + public static function faust_admin_notice(): void { + + // Exit if Faust is not enabled. + if( ! self::$faust_enabled ) { + return; + } + + add_action( 'admin_notices', function(): void { + $screen = get_current_screen(); + + // Exit if not this plugin's settings page. + if ( 'settings_page_hwp-previews' !== $screen->id ) { + return; + } + ?> + +
+

+
+ + Date: Wed, 4 Jun 2025 15:10:48 +0200 Subject: [PATCH 2/6] fix: function name and return type --- plugins/hwp-previews/src/Integration/Faust_Integration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 01698354..8fbc991e 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -39,7 +39,7 @@ class Faust_Integration { */ public static function init(): void { self::init_class_properties(); - self::add_filters_actions(); + self::configure_faust(); } public static function init_class_properties(): void { @@ -55,7 +55,7 @@ public static function init_class_properties(): void { self::$faust_enabled = self::is_faust_enabled(); } - public static function add_filters_actions() { + public static function configure_faust(): void { if(self::$faust_enabled) { self::set_default_faust_settings(); From 045db1bd4849f345db2d30c110a8049d0bfa9f29 Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Wed, 4 Jun 2025 15:22:01 +0200 Subject: [PATCH 3/6] fix: phpstan fixes and simplification --- .../src/Integration/Faust_Integration.php | 55 +++++-------------- 1 file changed, 14 insertions(+), 41 deletions(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 8fbc991e..67a7a632 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -2,33 +2,11 @@ namespace HWP\Previews\Integration; -use HWP\Previews\Admin\Settings\Helper\Settings_Helper; use HWP\Previews\Admin\Settings\Helper\Settings_Group; use HWP\Previews\Post\Type\Contracts\Post_Types_Config_Interface; use HWP\Previews\Post\Type\Post_Types_Config_Registry; class Faust_Integration { - /** - * Post types configuration. - * - * @var Post_Types_Config_Interface|null - */ - protected static ?Post_Types_Config_Interface $types_config = null; - - /** - * Settings helper instance that provides access to plugin settings. - * - * @var Settings_Helper|null - */ - protected static ?Settings_Helper $settings_helper = null; - - /** - * Settings group instance that provides access to plugin settings. - * - * @var Settings_Group|null - */ - protected static ?Settings_Group $settings_group = null; - /** * Whether Faust is enabled. */ @@ -38,21 +16,9 @@ class Faust_Integration { * Initialize the hooks for the preview functionality. */ public static function init(): void { - self::init_class_properties(); - self::configure_faust(); - } - - public static function init_class_properties(): void { - self::$settings_helper = Settings_Helper::get_instance(); + self::$faust_enabled = self::is_faust_enabled(); - self::$settings_group = Settings_Group::get_instance(); - - self::$types_config = apply_filters( - 'hwp_previews_hooks_post_type_config', - Post_Types_Config_Registry::get_post_type_config() - ); - - self::$faust_enabled = self::is_faust_enabled(); + self::configure_faust(); } public static function configure_faust(): void { @@ -105,15 +71,22 @@ public static function get_faust_preview_url(): string { * Sets default Faust settings if there are no existing settings. */ public static function set_default_faust_settings(): void { - $plugin_settings = self::$settings_group->get_cached_settings(); + $settings_group = Settings_Group::get_instance(); + $types_config = apply_filters( + 'hwp_previews_hooks_post_type_config', + Post_Types_Config_Registry::get_post_type_config() + ); + + + $plugin_settings = $settings_group->get_cached_settings(); if( empty($plugin_settings) ) { - $setting_preview_key = self::$settings_group->get_settings_key_preview_url(); - $setting_enabled_key = self::$settings_group->get_settings_key_enabled(); + $setting_preview_key = $settings_group->get_settings_key_preview_url(); + $setting_enabled_key = $settings_group->get_settings_key_enabled(); $default_settings = array(); - foreach ( self::$types_config->get_public_post_types() as $key => $label ) { + foreach ( $types_config->get_public_post_types() as $key => $label ) { $default_settings[$key] = array( $setting_enabled_key => true, $setting_preview_key => self::get_faust_preview_url(), @@ -139,7 +112,7 @@ public static function faust_admin_notice(): void { $screen = get_current_screen(); // Exit if not this plugin's settings page. - if ( 'settings_page_hwp-previews' !== $screen->id ) { + if ( ! is_object( $screen ) || 'settings_page_hwp-previews' !== $screen->id ) { return; } ?> From 6fa9c6af2e8dc8065245e97f8f2577890a35b679 Mon Sep 17 00:00:00 2001 From: ahuseyn Date: Wed, 4 Jun 2025 15:26:35 +0200 Subject: [PATCH 4/6] fix: psalm fix --- plugins/hwp-previews/src/Integration/Faust_Integration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 67a7a632..e066489e 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -122,6 +122,6 @@ public static function faust_admin_notice(): void { Date: Wed, 4 Jun 2025 15:30:34 +0200 Subject: [PATCH 5/6] fix: linter fixes --- .../src/Integration/Faust_Integration.php | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index e066489e..916c5d35 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -1,16 +1,17 @@ get_cached_settings(); + $plugin_settings = $settings_group->get_cached_settings(); - if( empty($plugin_settings) ) { - $setting_preview_key = $settings_group->get_settings_key_preview_url(); - $setting_enabled_key = $settings_group->get_settings_key_enabled(); + if ( ! empty( $plugin_settings ) ) { + return; + } + + $setting_preview_key = $settings_group->get_settings_key_preview_url(); + $setting_enabled_key = $settings_group->get_settings_key_enabled(); - $default_settings = array(); - - foreach ( $types_config->get_public_post_types() as $key => $label ) { - $default_settings[$key] = array( - $setting_enabled_key => true, - $setting_preview_key => self::get_faust_preview_url(), - ); - } + $default_settings = []; + + foreach ( $types_config->get_public_post_types() as $key => $label ) { + $default_settings[ $key ] = [ + $setting_enabled_key => true, + $setting_preview_key => self::get_faust_preview_url(), + ]; + } - update_option(HWP_PREVIEWS_SETTINGS_KEY, $default_settings); - } + update_option( HWP_PREVIEWS_SETTINGS_KEY, $default_settings ); } /** @@ -104,11 +110,11 @@ public static function set_default_faust_settings(): void { public static function faust_admin_notice(): void { // Exit if Faust is not enabled. - if( ! self::$faust_enabled ) { + if ( ! self::$faust_enabled ) { return; } - add_action( 'admin_notices', function(): void { + add_action( 'admin_notices', static function (): void { $screen = get_current_screen(); // Exit if not this plugin's settings page. @@ -118,7 +124,7 @@ public static function faust_admin_notice(): void { ?>
-

+

Date: Thu, 5 Jun 2025 11:21:43 +0200 Subject: [PATCH 6/6] feat: make admin notice dismissible --- .../src/Integration/Faust_Integration.php | 76 ++++++++++++++++--- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/plugins/hwp-previews/src/Integration/Faust_Integration.php b/plugins/hwp-previews/src/Integration/Faust_Integration.php index 916c5d35..07d3f550 100644 --- a/plugins/hwp-previews/src/Integration/Faust_Integration.php +++ b/plugins/hwp-previews/src/Integration/Faust_Integration.php @@ -8,10 +8,17 @@ use HWP\Previews\Post\Type\Post_Types_Config_Registry; class Faust_Integration { + /** + * The key for the admin notice. + * + * @var string + */ + public const FAUST_NOTICE_KEY = 'hwp_previews_faust_notice'; + /** * Whether Faust is enabled. */ - protected static bool $faust_enabled = false; + public static bool $faust_enabled = false; /** * Initialize the hooks for the preview functionality. @@ -32,7 +39,7 @@ public static 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 ); - self::faust_admin_notice(); + self::display_faust_admin_notice(); } } @@ -104,16 +111,16 @@ public static function set_default_faust_settings(): void { } /** - * If Faust is enabled, show an admin notice about the migration on the settings page. - * TODO make the notice dismissible. + * Dismiss the Faust admin notice. */ - public static function faust_admin_notice(): void { - - // Exit if Faust is not enabled. - if ( ! self::$faust_enabled ) { - return; - } + public static function dismiss_faust_admin_notice(): void { + update_user_meta( get_current_user_id(), self::FAUST_NOTICE_KEY, 1 ); + } + /** + * Register admin notice to inform users about Faust integration. + */ + public static function register_faust_admin_notice(): void { add_action( 'admin_notices', static function (): void { $screen = get_current_screen(); @@ -121,13 +128,58 @@ public static function faust_admin_notice(): void { if ( ! is_object( $screen ) || 'settings_page_hwp-previews' !== $screen->id ) { return; } + + $ajax_nonce = wp_create_nonce( self::FAUST_NOTICE_KEY ); ?> -
-

+
+

+ +