Skip to content

Commit f5c5966

Browse files
committed
Refactoring. Add some new services to consolidate some of the existing functionality.
1 parent 2de7fb1 commit f5c5966

14 files changed

+992
-495
lines changed

plugins/hwp-previews/ACTIONS_AND_FILTERS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `hwp_previews_template_path` - To use our own template for iframe previews
1313
- `hwp_previews_core` - Register or unregister URL parameters, and adjust types/statuses
1414
- `hwp_previews_filter_available_post_types` - Filter to modify the available post types for Previews.
15+
- `hwp_previews_filter_available_post_statuses` - Filter for post statuses for previews for Previews
1516
- `hwp_previews_settings_group_option_key` - Filter to modify the settings group option key. Default is HWP_PREVIEWS_SETTINGS_KEY
1617
- `hwp_previews_settings_group_settings_group` - Filter to modify the settings group name. Default is HWP_PREVIEWS_SETTINGS_GROUP
1718
- `hwp_previews_settings_group_settings_config` - Filter to modify the settings array. See `Settings_Group`
@@ -20,7 +21,6 @@
2021
- `hwp_previews_hooks_post_type_config` - Filter for post type config service for the Hook class
2122
- `hwp_previews_hooks_post_status_config` - Filter for post status config service for the Hook class
2223
- `hwp_previews_hooks_preview_link_service` - Filter for preview link service for the Hook class
23-
- `hwp_previews_hooks_post_statuses` - Filter for post statuses for previews for the Hook Class
2424
- `hwp_previews_settings_fields` - Allows a user to register, modify, or remove settings fields for the settings page
2525

2626
## Usage Examples

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
use HWP\Previews\Preview\Post\Status\Post_Statuses_Config;
1414
use HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface;
1515
use HWP\Previews\Preview\Post\Type\Post_Types_Config_Registry;
16-
use HWP\Previews\Preview\Template_Resolver;
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;
1720
use WP_Post;
1821
use WP_REST_Response;
1922

@@ -60,7 +63,7 @@ class Preview_Hooks {
6063
*/
6164
public function __construct() {
6265

63-
// @TODO - Refactor to use a factory or service locator pattern and ananlyze what is is actually needed.
66+
// @TODO - Refactor to use a factory or service locator pattern and analyze what is is actually needed.
6467

6568
$this->settings_helper = Settings_Helper::get_instance();
6669

@@ -125,6 +128,7 @@ public function setup(): void {
125128
* @return array<string>
126129
*/
127130
public function get_post_statuses(): array {
131+
// @TODO - Remove
128132
$post_statuses = [
129133
'publish',
130134
'future',
@@ -202,22 +206,17 @@ public function add_iframe_preview_template( string $template ): string {
202206
return $template;
203207
}
204208

205-
// @TODO - Refactor as should be part of the main preview settings.
206-
$settings_helper = Settings_Helper::get_instance();
207-
if ( ! $settings_helper->in_iframe( $post->post_type ) ) {
208-
return $template;
209-
}
210-
211-
// @TODO - Refactor as part of types and post statuses config refactor.
212-
$post_types = $this->types_config->get_post_types();
213-
$post_statuses = $this->statuses_config->get_post_statuses();
214-
$template_resolver = new Template_Resolver( $post, $post_types, $post_statuses );
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 );
215213

216-
if ( ! $template_resolver->is_allowed() ) {
214+
if ( ! $post_type_service->is_allowed_for_previews() || ! $post_type_service->is_iframe() ) {
217215
return $template;
218216
}
219217

220-
$iframe_template = $template_resolver->get_iframe_template();
218+
$template_resolver = new Template_Resolver_Service();
219+
$iframe_template = $template_resolver->get_iframe_template();
221220
if ( empty( $iframe_template ) ) {
222221
return $template;
223222
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ public static function get_instance(): self {
4444
return self::$instance;
4545
}
4646

47-
/**
48-
* Get the settings group.
49-
*
50-
* @return array<string, mixed>
51-
*/
52-
public function get_settings_config(): array {
53-
// @TODO: Remove this method when the settings group is no longer needed.
54-
return $this->settings_group->get_settings_config();
55-
}
56-
5747
/**
5848
* Get all post types that are enabled in the settings.
5949
*
@@ -99,6 +89,7 @@ public function post_statuses_as_parent( string $post_type, bool $default_value
9989
*/
10090
public function in_iframe( string $post_type, bool $default_value = false ): bool {
10191

92+
// @TODO remove as part of Post_Type_Service refactor.
10293
$key = $this->settings_group->get_settings_key_in_iframe();
10394

10495
return $this->settings_group->get_post_type_boolean_value( $key, $post_type, $default_value );
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HWP\Previews\Preview\Service;
6+
7+
class Post_Preview_Service {
8+
/**
9+
* The allowed post-types for previews.
10+
*
11+
* @var array<string>
12+
*/
13+
protected $post_types = [];
14+
15+
/**
16+
* The allowed post-statuses for previews.
17+
*
18+
* @var array<string>
19+
*/
20+
protected $post_statuses = [];
21+
22+
/**
23+
* Constructor for the Post_Preview_Service class.
24+
*
25+
* Initializes the allowed post-types and statuses for previews.
26+
*/
27+
public function __construct() {
28+
$this->set_post_types();
29+
$this->set_post_statuses();
30+
}
31+
32+
/**
33+
* @return array<string>
34+
*/
35+
public function get_allowed_post_types(): array {
36+
return $this->post_types;
37+
}
38+
39+
/**
40+
* Get the post statuses.
41+
*
42+
* @return array<string>
43+
*/
44+
public function get_post_statuses(): array {
45+
return $this->post_statuses;
46+
}
47+
48+
/**
49+
* Get the post types.
50+
*
51+
* @return array<string>
52+
*/
53+
public function get_post_types(): array {
54+
return $this->post_types;
55+
}
56+
57+
/**
58+
* Sets the allowed post types for previews.
59+
*/
60+
protected function set_post_types(): void {
61+
$post_types = get_post_types( [ 'public' => true ], 'objects' );
62+
63+
$result = [];
64+
65+
foreach ( $post_types as $post_type ) {
66+
$result[ $post_type->name ] = $post_type->label;
67+
}
68+
69+
$this->post_types = apply_filters( 'hwp_previews_filter_available_post_types', $result );
70+
}
71+
72+
/**
73+
* Sets the allowed post statuses for previews.
74+
*/
75+
protected function set_post_statuses(): void {
76+
77+
$post_statuses = [
78+
'publish',
79+
'future',
80+
'draft',
81+
'pending',
82+
'private',
83+
'auto-draft',
84+
];
85+
86+
$this->post_statuses = apply_filters( 'hwp_previews_filter_available_post_statuses', $post_statuses );
87+
}
88+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HWP\Previews\Preview\Service;
6+
7+
class Post_Settings_Service {
8+
/**
9+
* The settings value
10+
*
11+
* @var array<mixed>
12+
*/
13+
protected $settings_values = [];
14+
15+
/**
16+
* Initialize the settings service.
17+
*/
18+
public function __construct() {
19+
$this->setup();
20+
}
21+
22+
/**
23+
* @param string $post_type
24+
*
25+
* @return array<mixed>|null
26+
*/
27+
public function get_post_type_config( string $post_type ): ?array {
28+
return $this->settings_values[ $post_type ] ?? null;
29+
}
30+
31+
/**
32+
* The option key for the settings group.
33+
*/
34+
public function get_option_key(): string {
35+
return (string) apply_filters( 'hwp_previews_settings_group_option_key', HWP_PREVIEWS_SETTINGS_KEY );
36+
}
37+
38+
/**
39+
* The settings group for the options.
40+
*/
41+
public function get_settings_group(): string {
42+
return (string) apply_filters( 'hwp_previews_settings_group_settings_group', HWP_PREVIEWS_SETTINGS_GROUP );
43+
}
44+
45+
/**
46+
* Setup the settings values by retrieving them from the database or cache.
47+
* This method is called in the constructor to ensure settings are available.
48+
*/
49+
protected function setup(): void {
50+
$option_key = $this->get_option_key();
51+
$settings_group = $this->get_settings_group();
52+
53+
$value = wp_cache_get( $option_key, $settings_group );
54+
if ( is_array( $value ) ) {
55+
$this->settings_values = $value;
56+
57+
return;
58+
}
59+
60+
$this->settings_values = (array) get_option( $option_key, [] );
61+
wp_cache_set( $option_key, $value, $settings_group );
62+
}
63+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HWP\Previews\Preview\Service;
6+
7+
use WP_Post;
8+
9+
class Post_Type_Service {
10+
/**
11+
* The post-object
12+
*
13+
* @var \WP_Post
14+
*/
15+
protected WP_Post $post;
16+
17+
/**
18+
* Post-preview service that provide information about the post type for previews.
19+
*
20+
* @var \HWP\Previews\Preview\Service\Post_Preview_Service
21+
*/
22+
private Post_Preview_Service $post_preview_service;
23+
24+
/**
25+
* Settings service to retrieve post type settings.
26+
*
27+
* @var \HWP\Previews\Preview\Service\Post_Settings_Service
28+
*/
29+
private Post_Settings_Service $post_settings_service;
30+
31+
/**
32+
* @param \WP_Post $post
33+
* @param \HWP\Previews\Preview\Service\Post_Preview_Service $post_preview_service
34+
* @param \HWP\Previews\Preview\Service\Post_Settings_Service $post_settings_service
35+
*/
36+
public function __construct( WP_Post $post, Post_Preview_Service $post_preview_service, Post_Settings_Service $post_settings_service ) {
37+
$this->post = $post;
38+
$this->post_preview_service = $post_preview_service;
39+
$this->post_settings_service = $post_settings_service;
40+
}
41+
42+
/**
43+
* Checks if the post type is allowed for previews.
44+
*/
45+
public function is_allowed_for_previews(): bool {
46+
return $this->is_enabled() && $this->is_allowed_post_type() && $this->is_allowed_post_status();
47+
}
48+
49+
/**
50+
* Checks if the post-type is enabled for previews.
51+
*/
52+
public function is_enabled(): bool {
53+
$config = $this->post_settings_service->get_post_type_config( $this->post->post_type );
54+
if ( ! is_array( $config ) ) {
55+
return false;
56+
}
57+
58+
if ( ! isset( $config['enabled'] ) ) {
59+
return false;
60+
}
61+
return (bool) $config['enabled'];
62+
}
63+
64+
/**
65+
* Checks if the post-type is allowed for previews.
66+
*/
67+
public function is_allowed_post_type(): bool {
68+
$post_type = $this->post->post_type;
69+
$post_types = $this->post_preview_service->get_post_types();
70+
return array_key_exists( $post_type, $post_types );
71+
}
72+
73+
/**
74+
* Checks if the post-status is allowed for previews.
75+
*/
76+
public function is_allowed_post_status(): bool {
77+
$post_status = $this->post->post_status;
78+
$post_statuses = $this->post_preview_service->get_post_statuses();
79+
return in_array( $post_status, $post_statuses, true );
80+
}
81+
82+
/**
83+
* Checks if the post-type is allowed for iframe previews.
84+
*/
85+
public function is_iframe(): bool {
86+
$config = $this->post_settings_service->get_post_type_config( $this->post->post_type );
87+
if ( ! is_array( $config ) ) {
88+
return false;
89+
}
90+
91+
if ( ! isset( $config['in_iframe'] ) ) {
92+
return false;
93+
}
94+
return (bool) $config['in_iframe'];
95+
}
96+
}

0 commit comments

Comments
 (0)