Skip to content

Commit c7b6206

Browse files
committed
Refactored post parent functionality. Added constants for default fields. Added more tests.
1 parent 030de0a commit c7b6206

File tree

10 files changed

+179
-159
lines changed

10 files changed

+179
-159
lines changed

plugins/hwp-previews/ACTIONS_AND_FILTERS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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.
1515
- `hwp_previews_filter_available_post_statuses` - Filter for post statuses for previews for Previews
16+
- `hwp_previews_filter_available_parent_post_statuses` - Filter for parent post statuses for Previews
1617
- `hwp_previews_settings_group_option_key` - Filter to modify the settings group option key. Default is HWP_PREVIEWS_SETTINGS_KEY
1718
- `hwp_previews_settings_group_settings_group` - Filter to modify the settings group name. Default is HWP_PREVIEWS_SETTINGS_GROUP
1819
- `hwp_previews_settings_group_settings_config` - Filter to modify the settings array. See `Settings_Group`
@@ -25,6 +26,9 @@
2526

2627
## Usage Examples
2728

29+
@TODO - Redo
30+
31+
2832
### Filter: Post Types List
2933

3034
Modify which post types appear in the settings UI:

plugins/hwp-previews/src/Admin/Settings/Fields/Settings_Field_Collection.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@
88
use HWP\Previews\Admin\Settings\Fields\Field\URL_Input_Field;
99

1010
class Settings_Field_Collection {
11+
/**
12+
* The ID of the field that enables or disables the previews.
13+
*
14+
* @var string
15+
*/
16+
public const ENABLED_FIELD_ID = 'enabled';
17+
18+
19+
/**
20+
* The ID of the field that allows using post-statuses as parent.
21+
*
22+
* @var string
23+
*/
24+
public const POST_STATUSES_AS_PARENT_FIELD_ID = 'post_statuses_as_parent';
25+
26+
27+
/**
28+
* The ID of the field that indicates whether the preview is rendered in an iframe.
29+
*
30+
* @var string
31+
*/
32+
public const IN_IFRAME_FIELD_ID = 'in_iframe';
33+
34+
/**
35+
* The ID of the field that contains the preview URL.
36+
*
37+
* @var string
38+
*/
39+
public const PREVIEW_URL_FIELD_ID = 'preview_url';
40+
1141
/**
1242
* Array of fields
1343
*
@@ -66,7 +96,7 @@ protected function initialize_fields(): void {
6696

6797
$this->add_field(
6898
new Checkbox_Field(
69-
'enabled',
99+
self::ENABLED_FIELD_ID,
70100
false,
71101
__( 'Enable Previews', 'hwp-previews' ),
72102
__( 'Enable previews for post type.', 'hwp-previews' )
@@ -75,7 +105,7 @@ protected function initialize_fields(): void {
75105

76106
$this->add_field(
77107
new Checkbox_Field(
78-
'post_statuses_as_parent',
108+
self::POST_STATUSES_AS_PARENT_FIELD_ID,
79109
true,
80110
__( 'Allow all post statuses in parents option', 'hwp-previews' ),
81111
__( 'By default WordPress only allows published posts to be parents. This option allows posts of all statuses to be used as parent within hierarchical post types.', 'hwp-previews' )
@@ -84,7 +114,7 @@ protected function initialize_fields(): void {
84114

85115
$this->add_field(
86116
new Checkbox_Field(
87-
'in_iframe',
117+
self::IN_IFRAME_FIELD_ID,
88118
false,
89119
__( 'Use iframe to render previews', 'hwp-previews' ),
90120
__( 'With this option enabled, headless previews will be displayed inside an iframe on the preview page, without leaving WordPress.', 'hwp-previews' )
@@ -93,7 +123,7 @@ protected function initialize_fields(): void {
93123

94124
$this->add_field(
95125
new URL_Input_Field(
96-
'preview_url',
126+
self::PREVIEW_URL_FIELD_ID,
97127
false,
98128
__( 'Preview URL', 'hwp-previews' ),
99129
__( 'Construct your preview URL using the tags on the right. You can add any parameters needed to support headless previews.', 'hwp-previews' ),

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

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace HWP\Previews\Hooks;
66

7-
use HWP\Previews\Preview\Helper\Settings_Helper;
7+
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
88
use HWP\Previews\Preview\Parameter\Preview_Parameter_Registry;
9-
use HWP\Previews\Preview\Post\Parent\Post_Parent_Manager;
109
use HWP\Previews\Preview\Post\Post_Preview_Service;
1110
use HWP\Previews\Preview\Post\Post_Settings_Service;
1211
use HWP\Previews\Preview\Post\Post_Type_Service;
@@ -20,13 +19,6 @@
2019
use WP_REST_Response;
2120

2221
class Preview_Hooks {
23-
/**
24-
* Settings helper instance that provides access to plugin settings.
25-
*
26-
* @var \HWP\Previews\Preview\Helper\Settings_Helper
27-
*/
28-
protected Settings_Helper $settings_helper;
29-
3022
/**
3123
* Post types configuration.
3224
*
@@ -71,8 +63,6 @@ public function __construct() {
7163

7264
// @TODO - Refactor to use a factory or service locator pattern and analyze what is is actually needed.
7365

74-
$this->settings_helper = Settings_Helper::get_instance();
75-
7666
$this->types_config = apply_filters(
7767
'hwp_previews_hooks_post_type_config',
7868
Post_Types_Config_Registry::get_post_type_config()
@@ -153,26 +143,47 @@ public function get_post_statuses(): array {
153143
* @link https://developer.wordpress.org/reference/hooks/quick_edit_dropdown_pages_args/
154144
*/
155145
public function enable_post_statuses_as_parent( array $args ): array {
146+
if ( ! $this->should_enable_post_statuses_as_parent( $args ) ) {
147+
return $args;
148+
}
156149

157-
if ( empty( $args['post_type'] ) ) {
150+
$parent_statuses = $this->post_preview_service->get_parent_post_statuses();
151+
$post_statuses = $this->post_preview_service->get_post_statuses();
152+
$post_statuses = array_intersect( $parent_statuses, $post_statuses );
153+
154+
if ( empty( $post_statuses ) ) {
158155
return $args;
159156
}
160157

161-
$post_parent_manager = new Post_Parent_Manager( $this->types_config, $this->statuses_config );
158+
$args['post_status'] = $post_statuses;
159+
160+
return $args;
161+
}
162+
163+
/**
164+
* Whether post-statuses should be enabled as parent for the given post-type.
165+
*
166+
* @param array<mixed> $args
167+
*/
168+
public function should_enable_post_statuses_as_parent( array $args ): bool {
169+
if ( empty( $args['post_type'] ) ) {
170+
return false;
171+
}
162172

163173
$post_type = (string) $args['post_type'];
164174

165-
// Check if the correspondent setting is enabled.
166-
if ( ! $this->settings_helper->post_statuses_as_parent( $post_type ) ) {
167-
return $args;
175+
if ( ! is_post_type_hierarchical( $post_type ) ) {
176+
return false;
168177
}
169178

170-
$post_statuses = $post_parent_manager->get_post_statuses_as_parent( $post_type );
171-
if ( ! empty( $post_statuses ) ) {
172-
$args['post_status'] = $post_statuses;
179+
$config = $this->post_settings_service->get_post_type_config( $post_type );
180+
if ( ! is_array( $config ) || empty( $config ) ) {
181+
return false;
173182
}
174183

175-
return $args;
184+
$field_id = Settings_Field_Collection::POST_STATUSES_AS_PARENT_FIELD_ID;
185+
186+
return isset( $config[ $field_id ] ) && (bool) $config[ $field_id ];
176187
}
177188

178189
/**

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,4 @@ public function post_types_enabled( array $default_value = [] ): array {
6868
}
6969
return $default_value;
7070
}
71-
72-
/**
73-
* Get Post Statuses as Parent setting value for the given post type.
74-
*
75-
* @param string $post_type The post type to get the setting for.
76-
* @param bool $default_value The default value to return if the setting is not set.
77-
*/
78-
public function post_statuses_as_parent( string $post_type, bool $default_value = false ): bool {
79-
$key = $this->settings_group->get_settings_key_post_statuses_as_parent();
80-
81-
return $this->settings_group->get_post_type_boolean_value( $key, $post_type, $default_value );
82-
}
8371
}

plugins/hwp-previews/src/Preview/Post/Parent/Contracts/Post_Parent_Manager_Interface.php

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

plugins/hwp-previews/src/Preview/Post/Parent/Post_Parent_Manager.php

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

plugins/hwp-previews/src/Preview/Post/Post_Preview_Service.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class Post_Preview_Service {
1919
*/
2020
protected $post_statuses = [];
2121

22+
/**
23+
* The allowed post-statuses for parent post-types (hierarchical).
24+
*
25+
* @var array<string>
26+
*/
27+
protected $parent_post_statuses = [];
28+
2229
/**
2330
* Constructor for the Post_Preview_Service class.
2431
*
@@ -27,6 +34,7 @@ class Post_Preview_Service {
2734
public function __construct() {
2835
$this->set_post_types();
2936
$this->set_post_statuses();
37+
$this->set_post_parent_statuses();
3038
}
3139

3240
/**
@@ -54,6 +62,15 @@ public function get_post_types(): array {
5462
return $this->post_types;
5563
}
5664

65+
/**
66+
* Get the parent post statuses
67+
*
68+
* @return array<string>
69+
*/
70+
public function get_parent_post_statuses(): array {
71+
return $this->parent_post_statuses;
72+
}
73+
5774
/**
5875
* Sets the allowed post types for previews.
5976
*/
@@ -85,4 +102,20 @@ protected function set_post_statuses(): void {
85102

86103
$this->post_statuses = apply_filters( 'hwp_previews_filter_available_post_statuses', $post_statuses );
87104
}
105+
106+
/**
107+
* Sets the allowed post statuses for parent post-types (hierarchael).
108+
*/
109+
protected function set_post_parent_statuses(): void {
110+
111+
$post_statuses = [
112+
'publish',
113+
'future',
114+
'draft',
115+
'pending',
116+
'private'
117+
];
118+
119+
$this->parent_post_statuses = apply_filters( 'hwp_previews_filter_available_parent_post_statuses', $post_statuses );
120+
}
88121
}

plugins/hwp-previews/src/Preview/Post/Post_Type_Service.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace HWP\Previews\Preview\Post;
66

7+
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
78
use WP_Post;
89

910
class Post_Type_Service {
@@ -55,11 +56,13 @@ public function is_enabled(): bool {
5556
return false;
5657
}
5758

58-
if ( ! isset( $config['enabled'] ) ) {
59+
$field_id = Settings_Field_Collection::ENABLED_FIELD_ID;
60+
61+
if ( ! isset( $config[ $field_id ] ) ) {
5962
return false;
6063
}
6164

62-
return (bool) $config['enabled'];
65+
return (bool) $config[ $field_id ];
6366
}
6467

6568
/**
@@ -91,12 +94,12 @@ public function is_iframe(): bool {
9194
return false;
9295
}
9396

94-
// @TODO - Key resolution.
95-
if ( ! isset( $config['in_iframe'] ) ) {
97+
$field_id = Settings_Field_Collection::IN_IFRAME_FIELD_ID;
98+
if ( ! isset( $config[ $field_id ] ) ) {
9699
return false;
97100
}
98101

99-
return (bool) $config['in_iframe'];
102+
return (bool) $config[ $field_id ];
100103
}
101104

102105
/**
@@ -110,10 +113,11 @@ public function get_preview_url(): ?string {
110113
return null;
111114
}
112115

113-
if ( ! isset( $config['preview_url'] ) ) {
116+
$field_id = Settings_Field_Collection::PREVIEW_URL_FIELD_ID;
117+
if ( ! isset( $config[ $field_id ] ) ) {
114118
return null;
115119
}
116120

117-
return (string) $config['preview_url'];
121+
return (string) $config[ $field_id ];
118122
}
119123
}

0 commit comments

Comments
 (0)