Skip to content

Commit 030de0a

Browse files
committed
Refactoring URL resolver functionality.
1 parent 6cef674 commit 030de0a

File tree

14 files changed

+119
-185
lines changed

14 files changed

+119
-185
lines changed

plugins/hwp-previews/src/Hooks/Preview_Hooks.php

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
namespace HWP\Previews\Hooks;
66

77
use 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;
108
use HWP\Previews\Preview\Parameter\Preview_Parameter_Registry;
119
use 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;
1213
use HWP\Previews\Preview\Post\Status\Contracts\Post_Statuses_Config_Interface;
1314
use HWP\Previews\Preview\Post\Status\Post_Statuses_Config;
1415
use HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface;
1516
use 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;
2019
use WP_Post;
2120
use 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
/**

plugins/hwp-previews/src/Preview/Helper/Settings_Group.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,6 @@ public function get_post_type_boolean_value( string $name, string $post_type, bo
135135
return $default_value;
136136
}
137137

138-
/**
139-
* Gets a string value for a specific setting in a post type.
140-
*
141-
* @param string $name The setting name.
142-
* @param string $post_type The post type slug.
143-
* @param string $default_value The default value to return if the setting is not found.
144-
*/
145-
public function get_post_type_string_value( string $name, string $post_type, string $default_value = '' ): string {
146-
$settings = $this->get_cached_settings();
147-
$type = $this->settings_config[ $name ] ?? null;
148-
if ( 'string' === $type && isset( $settings[ $post_type ][ $name ] ) ) {
149-
return (string) $settings[ $post_type ][ $name ];
150-
}
151-
152-
return $default_value;
153-
}
154-
155138
/**
156139
* Gets the option key for the settings group.
157140
*/

plugins/hwp-previews/src/Preview/Helper/Settings_Helper.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,4 @@ public function post_statuses_as_parent( string $post_type, bool $default_value
8080

8181
return $this->settings_group->get_post_type_boolean_value( $key, $post_type, $default_value );
8282
}
83-
84-
/**
85-
* Show In iframe value for the given post type.
86-
*
87-
* @param string $post_type The post type to get the setting for.
88-
* @param bool $default_value The default value to return if the setting is not set.
89-
*/
90-
public function in_iframe( string $post_type, bool $default_value = false ): bool {
91-
92-
// @TODO remove as part of Post_Type_Service refactor.
93-
$key = $this->settings_group->get_settings_key_in_iframe();
94-
95-
return $this->settings_group->get_post_type_boolean_value( $key, $post_type, $default_value );
96-
}
97-
98-
/**
99-
* URL template setting value for the given post type.
100-
*
101-
* @param string $post_type The post type to get the setting for.
102-
* @param string $default_value The default value to return if the setting is not set.
103-
*/
104-
public function url_template( string $post_type, string $default_value = '' ): string {
105-
$key = $this->settings_group->get_settings_key_preview_url();
106-
107-
return $this->settings_group->get_post_type_string_value( $key, $post_type, $default_value );
108-
}
10983
}

plugins/hwp-previews/src/Preview/Link/Preview_Link_Service.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

plugins/hwp-previews/src/Preview/Service/Post_Preview_Service.php renamed to plugins/hwp-previews/src/Preview/Post/Post_Preview_Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace HWP\Previews\Preview\Service;
5+
namespace HWP\Previews\Preview\Post;
66

77
class Post_Preview_Service {
88
/**

plugins/hwp-previews/src/Preview/Service/Post_Settings_Service.php renamed to plugins/hwp-previews/src/Preview/Post/Post_Settings_Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace HWP\Previews\Preview\Service;
5+
namespace HWP\Previews\Preview\Post;
66

77
class Post_Settings_Service {
88
/**

0 commit comments

Comments
 (0)