Skip to content

Commit 0f3b0bf

Browse files
committed
Refactored checking if classic editor is available. Added tests.
1 parent 595af07 commit 0f3b0bf

File tree

8 files changed

+208
-427
lines changed

8 files changed

+208
-427
lines changed

plugins/hwp-previews/src/Admin/Settings_Page.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use HWP\Previews\Admin\Settings\Menu\Menu_Page;
99
use HWP\Previews\Admin\Settings\Settings_Form_Manager;
1010
use HWP\Previews\Preview\Parameter\Preview_Parameter_Registry;
11-
use HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface;
12-
use HWP\Previews\Preview\Post\Type\Post_Types_Config_Registry;
11+
use HWP\Previews\Preview\Post\Post_Preview_Service;
1312

1413
class Settings_Page {
1514
/**
@@ -23,9 +22,11 @@ class Settings_Page {
2322
protected Preview_Parameter_Registry $parameters;
2423

2524
/**
26-
* @var \HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface The post types available for previews.
25+
* Post-preview service to get post types and statuses for the settings page.
26+
*
27+
* @var \HWP\Previews\Preview\Post\Post_Preview_Service
2728
*/
28-
protected Post_Types_Config_Interface $types_config;
29+
protected Post_Preview_Service $post_preview_service;
2930

3031
/**
3132
* The instance of the plugin.
@@ -40,8 +41,8 @@ class Settings_Page {
4041
* Initializes the settings page, registers settings fields, and loads scripts and styles.
4142
*/
4243
public function __construct() {
43-
$this->parameters = Preview_Parameter_Registry::get_instance();
44-
$this->types_config = Post_Types_Config_Registry::get_post_type_config();
44+
$this->parameters = Preview_Parameter_Registry::get_instance();
45+
$this->post_preview_service = new Post_Preview_Service();
4546

4647
$this->register_settings_pages();
4748
$this->register_settings_fields();
@@ -72,9 +73,9 @@ public static function init(): Settings_Page {
7273
public function register_settings_pages(): void {
7374
add_action( 'admin_menu', function (): void {
7475

75-
// Note: We didn't initalize in the constructor because we need to ensure
76-
// the post types are registered before we can use them.
77-
$post_types = $this->types_config->get_public_post_types();
76+
// Note: We didn't initalise in the constructor because we need to ensure
77+
// the post-types are registered before we can use them.
78+
$post_types = $this->post_preview_service->get_post_types();
7879

7980
$page = new Menu_Page(
8081
__( 'HWP Previews Settings', 'hwp-previews' ),
@@ -100,7 +101,7 @@ public function register_settings_pages(): void {
100101
public function register_settings_fields(): void {
101102
add_action( 'admin_init', function (): void {
102103
$settings_manager = new Settings_Form_Manager(
103-
$this->types_config->get_public_post_types(),
104+
$this->post_preview_service->get_post_types(),
104105
new Settings_Field_Collection()
105106
);
106107
$settings_manager->render_form();

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66

77
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
88
use HWP\Previews\Preview\Parameter\Preview_Parameter_Registry;
9+
use HWP\Previews\Preview\Post\Post_Editor_Service;
910
use HWP\Previews\Preview\Post\Post_Preview_Service;
1011
use HWP\Previews\Preview\Post\Post_Settings_Service;
1112
use HWP\Previews\Preview\Post\Post_Type_Service;
12-
use HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface;
13-
use HWP\Previews\Preview\Post\Type\Post_Types_Config_Registry;
1413
use HWP\Previews\Preview\Template\Template_Resolver_Service;
1514
use HWP\Previews\Preview\Url\Preview_Url_Resolver_Service;
1615
use WP_Post;
1716
use WP_REST_Response;
1817

1918
class Preview_Hooks {
20-
/**
21-
* Post types configuration.
22-
*
23-
* @var \HWP\Previews\Preview\Post\Type\Contracts\Post_Types_Config_Interface
24-
*/
25-
protected Post_Types_Config_Interface $types_config;
26-
2719
/**
2820
* Post-settings service that provides access to post-settings.
2921
*
@@ -51,14 +43,6 @@ class Preview_Hooks {
5143
* Initializes the settings helper, post types and statuses configurations, and the preview link service.
5244
*/
5345
public function __construct() {
54-
55-
// @TODO - Refactor to use a factory or service locator pattern and analyze what is is actually needed.
56-
57-
$this->types_config = apply_filters(
58-
'hwp_previews_hooks_post_type_config',
59-
Post_Types_Config_Registry::get_post_type_config()
60-
);
61-
6246
$this->post_preview_service = new Post_Preview_Service();
6347
$this->post_settings_service = new Post_Settings_Service();
6448
}
@@ -72,11 +56,15 @@ public function setup(): void {
7256
add_filter( 'page_attributes_dropdown_pages_args', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );
7357
add_filter( 'quick_edit_dropdown_pages_args', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );
7458

75-
foreach ( $this->types_config->get_post_types() as $post_type ) {
76-
if ( ! $this->types_config->gutenberg_editor_enabled( $post_type ) ) {
59+
$post_editor_service = new Post_Editor_Service();
60+
$post_types = $this->post_preview_service->get_post_types();
61+
62+
// Enable post-statuses as parent for the post-types specified in the post-types config.
63+
foreach ( $post_types as $post_type => $label ) {
64+
if ( ! $post_editor_service->gutenberg_editor_enabled( $post_type ) ) {
7765
continue;
7866
}
79-
// @TODO - Add unit tests for this filter.
67+
8068
add_filter( 'rest_' . $post_type . '_query', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );
8169
}
8270

@@ -91,13 +79,13 @@ public function setup(): void {
9179
* Hack Function that changes the preview link for draft articles,
9280
* this must be removed when properly fixed https://github.com/WordPress/gutenberg/issues/13998.
9381
*/
94-
foreach ( $this->types_config->get_public_post_types() as $key => $label ) {
95-
add_filter( 'rest_prepare_' . $key, [ $this, 'filter_rest_prepare_link' ], 10, 2 );
82+
foreach ( $post_types as $post_type => $label ) {
83+
add_filter( 'rest_prepare_' . $post_type, [ $this, 'filter_rest_prepare_link' ], 10, 2 );
9684
}
9785
}
9886

9987
/**
100-
* Enable post statuses as parent for the post types specified in the post types config.
88+
* Enable post-statuses as parent for the post types specified in the post types config.
10189
*
10290
* @param array<mixed> $args The arguments for the dropdown pages.
10391
*
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HWP\Previews\Preview\Post;
6+
7+
use WP_Post_Type;
8+
9+
class Post_Editor_Service {
10+
/**
11+
* Check if the post-type supports Gutenberg editor and if the classic editor is not being forced.
12+
*
13+
* @param string $post_type Post Type slug.
14+
*/
15+
public function gutenberg_editor_enabled(string $post_type): bool {
16+
$post_type_object = get_post_type_object( $post_type );
17+
if ( ! $post_type_object instanceof WP_Post_Type ) {
18+
return false;
19+
}
20+
21+
return $this->is_gutenberg_supported( $post_type_object ) &&
22+
! $this->is_classic_editor_forced( $post_type );
23+
}
24+
25+
/**
26+
* Checks if the post type supports Gutenberg.
27+
*
28+
* @param \WP_Post_Type $post_type Post Type object.
29+
*/
30+
public function is_gutenberg_supported( WP_Post_Type $post_type ): bool {
31+
32+
if (empty($post_type->show_in_rest)) {
33+
return false;
34+
}
35+
36+
if (!post_type_supports($post_type->name, 'editor')) {
37+
return false;
38+
}
39+
40+
return $post_type->show_in_rest;
41+
}
42+
43+
/**
44+
* Checks if the post type is supported by Classic Editor.
45+
*/
46+
public function is_classic_editor_forced(string $post_type): bool {
47+
if (
48+
! function_exists( 'is_plugin_active' ) ||
49+
! is_plugin_active( 'classic-editor/classic-editor.php' )
50+
) {
51+
return false;
52+
}
53+
54+
// If this post type is listed in Classic Editor settings, Gutenberg is disabled.
55+
$settings = (array) get_option( 'classic-editor-settings', [] );
56+
57+
return ! empty( $settings['post_types'] ) &&
58+
is_array( $settings['post_types'] ) &&
59+
in_array( $post_type, $settings['post_types'], true );
60+
}
61+
}

plugins/hwp-previews/src/Preview/Post/Status/Contracts/Post_Statuses_Config_Interface.php

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

plugins/hwp-previews/src/Preview/Post/Status/Post_Statuses_Config.php

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

0 commit comments

Comments
 (0)