Skip to content

Commit 8aa657e

Browse files
committed
Fixed issues with preview links not registering ACF post types. Refactored so setting the initial allowed post types fires after ACF registers post types.
1 parent 08e8ea4 commit 8aa657e

File tree

4 files changed

+58
-31
lines changed

4 files changed

+58
-31
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ public function setup(): void {
7878
add_filter( 'page_attributes_dropdown_pages_args', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );
7979
add_filter( 'quick_edit_dropdown_pages_args', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );
8080

81+
// iframe preview functionality.
82+
add_filter( 'template_include', [ $this, 'add_iframe_preview_template' ], 10, 1 );
83+
84+
// Preview link functionality. Extra priority to ensure it runs after the Faust preview link filter.
85+
add_filter( 'preview_post_link', [ $this, 'update_preview_post_link' ], 1001, 2 );
86+
87+
// Setup the post-type dependent hooks.
88+
add_action( 'init', [ $this, 'setup_post_type_hooks' ], 20, 0 );
89+
}
90+
91+
/**
92+
* Setup the post-type dependent hooks.`
93+
*/
94+
public function setup_post_type_hooks(): void {
8195
$post_editor_service = new Post_Editor_Service();
8296
$post_types = $this->post_preview_service->get_post_types();
8397

@@ -90,13 +104,6 @@ public function setup(): void {
90104
add_filter( 'rest_' . $post_type . '_query', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );
91105
}
92106

93-
// iframe preview functionality.
94-
add_filter( 'template_include', [ $this, 'add_iframe_preview_template' ], 10, 1 );
95-
96-
// Preview link functionality. Extra priority to ensure it runs after the Faust preview link filter.
97-
add_filter( 'preview_post_link', [ $this, 'update_preview_post_link' ], 1001, 2 );
98-
99-
100107
/**
101108
* Hack Function that changes the preview link for draft articles,
102109
* this must be removed when properly fixed https://github.com/WordPress/gutenberg/issues/13998.

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,14 @@ class Post_Preview_Service {
3535
*/
3636
protected $parent_post_statuses = [];
3737

38-
/**
39-
* @return array<string>
40-
*/
41-
public function get_allowed_post_types(): array {
42-
return $this->get_post_types();
43-
}
44-
4538
/**
4639
* Get the post-statuses.
4740
*
4841
* @return array<string>
4942
*/
5043
public function get_post_statuses(): array {
5144
$post_statuses = $this->post_statuses;
52-
if ([] !== $post_statuses) {
45+
if ( [] !== $post_statuses ) {
5346
return $post_statuses;
5447
}
5548
$this->set_post_statuses();
@@ -63,7 +56,7 @@ public function get_post_statuses(): array {
6356
*/
6457
public function get_post_types(): array {
6558
$post_types = $this->post_types;
66-
if ([] !== $post_types) {
59+
if ( [] !== $post_types ) {
6760
return $post_types;
6861
}
6962
$this->set_post_types();
@@ -77,7 +70,7 @@ public function get_post_types(): array {
7770
*/
7871
public function get_parent_post_statuses(): array {
7972
$parent_post_statuses = $this->parent_post_statuses;
80-
if ([] !== $parent_post_statuses) {
73+
if ( [] !== $parent_post_statuses ) {
8174
return $parent_post_statuses;
8275
}
8376
$this->set_post_parent_statuses();

plugins/hwp-previews/tests/wpunit/Hooks/PreviewHooksTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,45 @@ public function test_filter_rest_prepare_link_no_link_previews_no_preview_url()
574574
$this->assertArrayNotHasKey( 'link', $data );
575575
$this->assertEquals( $original_response, $response );
576576
}
577+
578+
public function test_setup_post_type_hooks_adds_filters_for_enabled_post_types() {
579+
// Create a custom post type to test with
580+
register_post_type( 'test_cpt', [
581+
'public' => true,
582+
'show_in_rest' => true,
583+
'supports' => [ 'editor' ], // This enables Gutenberg
584+
] );
585+
586+
$test_config = [
587+
'post' => [
588+
Settings_Field_Collection::ENABLED_FIELD_ID => true,
589+
Settings_Field_Collection::PREVIEW_URL_FIELD_ID => 'https://localhost:3000/post?preview=true&post_id={ID}&status={status}',
590+
],
591+
'test_cpt' => [
592+
Settings_Field_Collection::ENABLED_FIELD_ID => true,
593+
Settings_Field_Collection::PREVIEW_URL_FIELD_ID => 'https://localhost:3000/cpt?preview=true&post_id={ID}&status={status}',
594+
]
595+
];
596+
update_option( $this->test_option_key, $test_config );
597+
598+
// Remove any existing filters to have a clean slate
599+
remove_all_filters( 'rest_post_query' );
600+
remove_all_filters( 'rest_test_cpt_query' );
601+
remove_all_filters( 'rest_prepare_post' );
602+
remove_all_filters( 'rest_prepare_test_cpt' );
603+
604+
$preview_hooks = new Preview_Hooks();
605+
606+
// Call the method we're testing
607+
$preview_hooks->setup_post_type_hooks();
608+
609+
// Assert that the filters were added for post types that support Gutenberg
610+
$this->assertTrue( has_filter( 'rest_post_query', [ $preview_hooks, 'enable_post_statuses_as_parent' ] ) !== false );
611+
$this->assertTrue( has_filter( 'rest_test_cpt_query', [ $preview_hooks, 'enable_post_statuses_as_parent' ] ) !== false );
612+
$this->assertTrue( has_filter( 'rest_prepare_post', [ $preview_hooks, 'filter_rest_prepare_link' ] ) !== false );
613+
$this->assertTrue( has_filter( 'rest_prepare_test_cpt', [ $preview_hooks, 'filter_rest_prepare_link' ] ) !== false );
614+
615+
// Clean up
616+
unregister_post_type( 'test_cpt' );
617+
}
577618
}

plugins/hwp-previews/tests/wpunit/Preview/Post/PostPreviewServiceTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ public function remove_all_filters() {
3131
remove_all_filters( 'hwp_previews_filter_available_post_statuses' );
3232
}
3333

34-
public function test_get_allowed_post_types_returns_array(): void {
35-
$result = $this->service->get_allowed_post_types();
36-
37-
$this->assertIsArray( $result );
38-
$this->assertNotEmpty( $result );
39-
}
40-
4134
public function test_get_post_statuses_returns_default_statuses(): void {
4235
$result = $this->service->get_post_statuses();
4336

@@ -65,13 +58,6 @@ public function test_get_parent_post_statuses_returns_default_statuses(): void {
6558
$this->assertEquals($expected, $result);
6659
}
6760

68-
public function test_get_post_types_returns_same_as_get_allowed_post_types(): void {
69-
$allowed_types = $this->service->get_allowed_post_types();
70-
$post_types = $this->service->get_post_types();
71-
72-
$this->assertEquals($allowed_types, $post_types);
73-
}
74-
7561
public function test_post_types_filter_is_applied(): void {
7662
$custom_post_types = ['custom_post' => 'Custom Post Type'];
7763
add_filter('hwp_previews_filter_available_post_types', function() use ($custom_post_types) {

0 commit comments

Comments
 (0)