11<?php
22
3- declare (strict_types=1 );
3+ declare ( strict_types=1 );
44
55namespace HWP \Previews \Integration ;
66
7- use HWP \Previews \Preview \Helper \Settings_Group ;
8- use HWP \Previews \Preview \Post \Type \Post_Types_Config_Registry ;
7+ use HWP \Previews \Admin \Settings \Fields \Settings_Field_Collection ;
8+ use HWP \Previews \Preview \Post \Post_Preview_Service ;
9+ use HWP \Previews \Preview \Post \Post_Settings_Service ;
910
1011class Faust_Integration {
1112 /**
@@ -16,51 +17,76 @@ class Faust_Integration {
1617 public const FAUST_NOTICE_KEY = 'hwp_previews_faust_notice ' ;
1718
1819 /**
19- * Whether Faust is enabled.
20+ * The instance of the Faust integration.
21+ *
22+ * @var \HWP\Previews\Integration\Faust_Integration|null
2023 */
21- public static bool $ faust_enabled = false ;
24+ protected static ? Faust_Integration $ instance = null ;
2225
2326 /**
24- * Initialize the hooks for the preview functionality .
27+ * Whether Faust is enabled .
2528 */
26- public static function init (): void {
27- self ::$ faust_enabled = self ::is_faust_enabled ();
29+ public bool $ faust_enabled = false ;
2830
29- self ::configure_faust ();
31+
32+ public function __construct () {
33+ $ this ->faust_enabled = $ this ->is_faust_enabled ();
34+
35+ if ( ! $ this ->get_faust_enabled () ) {
36+ return ;
37+ }
38+
39+ $ this ->configure_faust ();
3040 }
3141
3242 /**
33- * Configure Faust settings and remove conflicting filters .
43+ * Checks if Faust is enabled .
3444 */
35- public static function configure_faust (): void {
36- if ( self ::$ faust_enabled ) {
37- self ::set_default_faust_settings ();
45+ public function is_faust_enabled (): bool {
46+ if ( ! function_exists ( 'is_plugin_active ' ) ) {
47+ return false ;
48+ }
3849
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 );
50+ return is_plugin_active ( ' faustwp/faustwp.php ' );
51+ }
4152
42- self ::display_faust_admin_notice ();
43- }
53+ /**
54+ * Get the Faust enabled status.
55+ */
56+ public function get_faust_enabled (): bool {
57+ return $ this ->faust_enabled ;
4458 }
4559
4660 /**
47- * Checks if Faust is enabled .
61+ * Initialize the hooks for the preview functionality .
4862 */
49- public static function is_faust_enabled (): bool {
50- if ( function_exists ( ' is_plugin_active ' ) ) {
51- return is_plugin_active ( ' faustwp/faustwp.php ' );
63+ public static function init (): Faust_Integration {
64+ if ( ! isset ( self :: $ instance ) || ! ( is_a ( self :: $ instance , self ::class ) ) ) {
65+ self :: $ instance = new self ( );
5266 }
5367
54- return false ;
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 ();
5581 }
5682
5783 /**
5884 * Returns the Faust frontend URL from settings or a default value.
5985 */
60- public static function get_faust_frontend_url (): string {
86+ public function get_faust_frontend_url (): string {
6187 $ default_value = 'http://localhost:3000 ' ;
6288
63- if ( self :: $ faust_enabled && function_exists ( '\WPE\FaustWP\Settings\faustwp_get_setting ' ) ) {
89+ if ( $ this -> get_faust_enabled () && function_exists ( '\WPE\FaustWP\Settings\faustwp_get_setting ' ) ) {
6490 $ frontend_uri = \WPE \FaustWP \Settings \faustwp_get_setting ( 'frontend_uri ' , '' );
6591
6692 if ( ! empty ( $ frontend_uri ) ) {
@@ -74,34 +100,32 @@ public static function get_faust_frontend_url(): string {
74100 /**
75101 * Get default preview URL for Faust.
76102 */
77- public static function get_faust_preview_url (): string {
103+ public function get_faust_preview_url (): string {
78104 return self ::get_faust_frontend_url () . '/preview?p={ID}&preview=true&previewPathname=p{ID}&typeName={type} ' ;
79105 }
80106
81107 /**
82108 * Sets default Faust settings if there are no existing settings.
83109 */
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-
110+ public function set_default_faust_settings (): void {
91111
92- $ plugin_settings = $ settings_group ->get_cached_settings ();
112+ $ settings_helper = new Post_Settings_Service ();
113+ $ plugin_settings = $ settings_helper ->get_settings_values ();
93114
115+ // If already configured, do not overwrite.
94116 if ( ! empty ( $ plugin_settings ) ) {
95117 return ;
96118 }
97119
98- $ setting_preview_key = $ settings_group -> get_settings_key_preview_url ();
99- $ setting_enabled_key = $ settings_group -> get_settings_key_enabled ();
120+ $ post_preview_service = new Post_Preview_Service ();
121+ $ post_types = $ post_preview_service -> get_post_types ();
100122
101- $ default_settings = [];
123+ $ setting_preview_key = Settings_Field_Collection::PREVIEW_URL_FIELD_ID ;
124+ $ setting_enabled_key = Settings_Field_Collection::ENABLED_FIELD_ID ;
102125
103- foreach ( $ types_config ->get_public_post_types () as $ key => $ label ) {
104- $ default_settings [ $ key ] = [
126+ $ default_settings = [];
127+ foreach ( $ post_types as $ type => $ label ) {
128+ $ default_settings [ $ type ] = [
105129 $ setting_enabled_key => true ,
106130 $ setting_preview_key => self ::get_faust_preview_url (),
107131 ];
@@ -113,15 +137,15 @@ public static function set_default_faust_settings(): void {
113137 /**
114138 * Dismiss the Faust admin notice.
115139 */
116- public static function dismiss_faust_admin_notice (): void {
140+ public function dismiss_faust_admin_notice (): void {
117141 update_user_meta ( get_current_user_id (), self ::FAUST_NOTICE_KEY , 1 );
118142 }
119143
120144 /**
121145 * Register admin notice to inform users about Faust integration.
122146 */
123- public static function register_faust_admin_notice (): void {
124- add_action ( 'admin_notices ' , static function (): void {
147+ public function register_faust_admin_notice (): void {
148+ add_action ( 'admin_notices ' , function (): void {
125149 $ screen = get_current_screen ();
126150
127151 // Exit if not this plugin's settings page.
@@ -137,10 +161,10 @@ public static function register_faust_admin_notice(): void {
137161 </div>
138162
139163 <script>
140- window.addEventListener( 'load', function() {
141- const dismissBtn = document.querySelector( '#<?php echo esc_attr ( self ::FAUST_NOTICE_KEY ); ?> .notice-dismiss' );
164+ window.addEventListener('load', function () {
165+ const dismissBtn = document.querySelector('#<?php echo esc_attr ( self ::FAUST_NOTICE_KEY ); ?> .notice-dismiss');
142166
143- dismissBtn?.addEventListener( 'click', function( event ) {
167+ dismissBtn?.addEventListener('click', function ( event) {
144168 let postData = new FormData();
145169 postData.append('action', '<?php echo esc_attr ( self ::FAUST_NOTICE_KEY ); ?> ');
146170 postData.append('_ajax_nonce', '<?php echo esc_html ( $ ajax_nonce ); ?> ');
@@ -154,24 +178,24 @@ public static function register_faust_admin_notice(): void {
154178 </script>
155179
156180 <?php
157- }, 10 , 0 );
181+ }, 10 , 0 );
158182 }
159183
160184 /**
161185 * If Faust is enabled, show an admin notice about the migration on the settings page.
162186 */
163- public static function display_faust_admin_notice (): void {
187+ protected function display_faust_admin_notice (): void {
164188 $ is_dismissed = get_user_meta ( get_current_user_id (), self ::FAUST_NOTICE_KEY , true );
165189
166190 // Exit if Faust is not enabled or if the notice has been dismissed.
167- if ( ! self :: $ faust_enabled || (bool ) $ is_dismissed ) {
191+ if ( ! $ this -> get_faust_enabled () || (bool ) $ is_dismissed ) {
168192 return ;
169193 }
170194
171195 self ::register_faust_admin_notice ();
172196
173197 // Register the AJAX action for dismissing the notice.
174- add_action ( 'wp_ajax_ ' . self ::FAUST_NOTICE_KEY , static function (): void {
198+ add_action ( 'wp_ajax_ ' . self ::FAUST_NOTICE_KEY , function (): void {
175199 // Exit if the action is not set or does not match the expected key.
176200 if ( ! isset ( $ _POST ['action ' ] ) || esc_attr ( self ::FAUST_NOTICE_KEY ) !== $ _POST ['action ' ] ) {
177201 return ;
0 commit comments