55namespace HWP \Previews \Hooks ;
66
77use HWP \Previews \Preview \Helper \Settings_Helper ;
8- use HWP \Previews \Preview \Link \Preview_Link_Placeholder_Resolver ;
9- use HWP \Previews \Preview \Link \Preview_Link_Service ;
108use HWP \Previews \Preview \Parameter \Preview_Parameter_Registry ;
119use HWP \Previews \Preview \Post \Parent \Post_Parent_Manager ;
10+ use HWP \Previews \Preview \Post \Post_Preview_Service ;
11+ use HWP \Previews \Preview \Post \Post_Settings_Service ;
12+ use HWP \Previews \Preview \Post \Post_Type_Service ;
1213use HWP \Previews \Preview \Post \Status \Contracts \Post_Statuses_Config_Interface ;
1314use HWP \Previews \Preview \Post \Status \Post_Statuses_Config ;
1415use HWP \Previews \Preview \Post \Type \Contracts \Post_Types_Config_Interface ;
1516use HWP \Previews \Preview \Post \Type \Post_Types_Config_Registry ;
16- use HWP \Previews \Preview \Service \Post_Preview_Service ;
17- use HWP \Previews \Preview \Service \Post_Settings_Service ;
18- use HWP \Previews \Preview \Service \Post_Type_Service ;
19- use HWP \Previews \Preview \Service \Template_Resolver_Service ;
17+ use HWP \Previews \Preview \Template \Template_Resolver_Service ;
18+ use HWP \Previews \Preview \Url \Preview_Url_Resolver_Service ;
2019use WP_Post ;
2120use WP_REST_Response ;
2221
@@ -43,11 +42,11 @@ class Preview_Hooks {
4342 protected Post_Statuses_Config_Interface $ statuses_config ;
4443
4544 /**
46- * Preview link service class that handles the generation of preview links .
45+ * Post-settings service that provides access to post-settings .
4746 *
48- * @var \HWP\Previews\Preview\Link\Preview_Link_Service
47+ * @var \HWP\Previews\Preview\Post\Post_Preview_Service
4948 */
50- protected Preview_Link_Service $ link_service ;
49+ protected Post_Preview_Service $ post_preview_service ;
5150
5251 /**
5352 * The instance of the Preview_Hooks class.
@@ -56,6 +55,13 @@ class Preview_Hooks {
5655 */
5756 protected static ?Preview_Hooks $ instance = null ;
5857
58+ /**
59+ * Post-settings service that provides access to post-settings.
60+ *
61+ * @var \HWP\Previews\Preview\Post\Post_Settings_Service
62+ */
63+ private Post_Settings_Service $ post_settings_service ;
64+
5965 /**
6066 * Constructor for the Preview_Hooks class.
6167 *
@@ -78,15 +84,9 @@ public function __construct() {
7884 ( new Post_Statuses_Config () )->set_post_statuses ( $ this ->get_post_statuses () )
7985 );
8086
81- // Initialize the preview link service.
82- $ this ->link_service = apply_filters (
83- 'hwp_previews_hooks_preview_link_service ' ,
84- new Preview_Link_Service (
85- $ this ->types_config ,
86- $ this ->statuses_config ,
87- new Preview_Link_Placeholder_Resolver ( Preview_Parameter_Registry::get_instance () )
88- )
89- );
87+
88+ $ this ->post_preview_service = new Post_Preview_Service ();
89+ $ this ->post_settings_service = new Post_Settings_Service ();
9090 }
9191
9292 /**
@@ -154,12 +154,12 @@ public function get_post_statuses(): array {
154154 */
155155 public function enable_post_statuses_as_parent ( array $ args ): array {
156156
157- $ post_parent_manager = new Post_Parent_Manager ( $ this ->types_config , $ this ->statuses_config );
158-
159157 if ( empty ( $ args ['post_type ' ] ) ) {
160158 return $ args ;
161159 }
162160
161+ $ post_parent_manager = new Post_Parent_Manager ( $ this ->types_config , $ this ->statuses_config );
162+
163163 $ post_type = (string ) $ args ['post_type ' ];
164164
165165 // Check if the correspondent setting is enabled.
@@ -180,7 +180,13 @@ public function enable_post_statuses_as_parent( array $args ): array {
180180 */
181181 public function filter_rest_prepare_link ( WP_REST_Response $ response , WP_Post $ post ): WP_REST_Response {
182182
183- if ( $ this ->settings_helper ->in_iframe ( $ post ->post_type ) ) {
183+ $ post_type_service = new Post_Type_Service ( $ post , $ this ->post_preview_service , $ this ->post_settings_service );
184+
185+ if ( ! $ post_type_service ->is_allowed_for_previews () ) {
186+ return $ response ;
187+ }
188+
189+ if ( $ post_type_service ->is_iframe () ) {
184190 return $ response ;
185191 }
186192
@@ -202,27 +208,28 @@ public function add_iframe_preview_template( string $template ): string {
202208 }
203209
204210 $ post = get_post ();
205- if ( ! is_object ( $ post ) || ! ( $ post instanceof WP_Post ) ) {
211+ if ( ! ( $ post instanceof WP_Post ) ) {
206212 return $ template ;
207213 }
208214
209- // @TODO - Move main classes into properties
210- $ post_preview_service = new Post_Preview_Service ();
211- $ post_settings_service = new Post_Settings_Service ();
212- $ post_type_service = new Post_Type_Service ( $ post , $ post_preview_service , $ post_settings_service );
215+ $ post_type_service = new Post_Type_Service ( $ post , $ this ->post_preview_service , $ this ->post_settings_service );
213216
214- if ( ! $ post_type_service ->is_allowed_for_previews () || ! $ post_type_service ->is_iframe () ) {
217+ if (
218+ ! $ post_type_service ->is_allowed_for_previews () ||
219+ ! $ post_type_service ->is_iframe ()
220+ ) {
215221 return $ template ;
216222 }
217223
218224 $ template_resolver = new Template_Resolver_Service ();
219225 $ iframe_template = $ template_resolver ->get_iframe_template ();
220- if ( empty ( $ iframe_template ) ) {
226+ $ url = self ::generate_preview_url ( $ post );
227+
228+ if ( empty ( $ iframe_template ) || empty ( $ url ) ) {
221229 return $ template ;
222230 }
223231
224- // @TODO - Refactor this as part of URL generation refactor.
225- $ template_resolver ->set_query_variable ( self ::generate_preview_url ( $ post ) );
232+ $ template_resolver ->set_query_variable ( $ url );
226233
227234 return $ iframe_template ;
228235 }
@@ -234,10 +241,14 @@ public function add_iframe_preview_template( string $template ): string {
234241 */
235242 public function update_preview_post_link ( string $ preview_link , WP_Post $ post ): string {
236243
237- // @TODO - Need to do more testing and add e2e tests for this filter.
244+ $ post_type_service = new Post_Type_Service ( $ post , $ this ->post_preview_service , $ this ->post_settings_service );
245+
246+ if ( ! $ post_type_service ->is_allowed_for_previews () ) {
247+ return $ preview_link ;
248+ }
238249
239- // If iframe option is enabled, we need to resolve preview on the template redirect level.
240- if ( $ this -> settings_helper -> in_iframe ( $ post -> post_type ) ) {
250+ // If the iframe option is enabled, we need to resolve preview on the template redirect level.
251+ if ( $ post_type_service -> is_iframe ( ) ) {
241252 return $ preview_link ;
242253 }
243254
@@ -258,14 +269,24 @@ public function update_preview_post_link( string $preview_link, WP_Post $post ):
258269 */
259270 public function generate_preview_url ( WP_Post $ post ): string {
260271
261- // Check if the correspondent setting is enabled.
262- $ url = $ this ->settings_helper ->url_template ( $ post ->post_type );
272+ $ post_type_service = new Post_Type_Service ( $ post , $ this ->post_preview_service , $ this ->post_settings_service );
263273
274+ if ( ! $ post_type_service ->is_allowed_for_previews () ) {
275+ return '' ;
276+ }
277+
278+ $ url = $ post_type_service ->get_preview_url ();
279+ if ( empty ( $ url ) ) {
280+ return '' ;
281+ }
282+
283+ $ service = new Preview_Url_Resolver_Service ( Preview_Parameter_Registry::get_instance () );
284+ $ url = $ service ->resolve ( $ post , $ url );
264285 if ( empty ( $ url ) ) {
265286 return '' ;
266287 }
267288
268- return $ this -> link_service -> generate_preview_post_link ( $ url, $ post ) ;
289+ return $ url ;
269290 }
270291
271292 /**
0 commit comments