Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/hwp-previews-wordpress-plugin": patch
---

Fixes priority issue with allowed post types set before ACF registers post types.
21 changes: 14 additions & 7 deletions plugins/hwp-previews/src/Hooks/Preview_Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ public function setup(): void {
add_filter( 'page_attributes_dropdown_pages_args', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );
add_filter( 'quick_edit_dropdown_pages_args', [ $this, 'enable_post_statuses_as_parent' ], 10, 1 );

// iframe preview functionality.
add_filter( 'template_include', [ $this, 'add_iframe_preview_template' ], 10, 1 );

// Preview link functionality. Extra priority to ensure it runs after the Faust preview link filter.
add_filter( 'preview_post_link', [ $this, 'update_preview_post_link' ], 1001, 2 );

// Setup the post-type dependent hooks.
add_action( 'init', [ $this, 'setup_post_type_hooks' ], 20, 0 );
}

/**
* Setup the post-type dependent hooks.`
*/
public function setup_post_type_hooks(): void {
$post_editor_service = new Post_Editor_Service();
$post_types = $this->post_preview_service->get_post_types();

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

// iframe preview functionality.
add_filter( 'template_include', [ $this, 'add_iframe_preview_template' ], 10, 1 );

// Preview link functionality. Extra priority to ensure it runs after the Faust preview link filter.
add_filter( 'preview_post_link', [ $this, 'update_preview_post_link' ], 1001, 2 );


/**
* Hack Function that changes the preview link for draft articles,
* this must be removed when properly fixed https://github.com/WordPress/gutenberg/issues/13998.
Expand Down
13 changes: 3 additions & 10 deletions plugins/hwp-previews/src/Preview/Post/Post_Preview_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,14 @@ class Post_Preview_Service {
*/
protected $parent_post_statuses = [];

/**
* @return array<string>
*/
public function get_allowed_post_types(): array {
return $this->get_post_types();
}

/**
* Get the post-statuses.
*
* @return array<string>
*/
public function get_post_statuses(): array {
$post_statuses = $this->post_statuses;
if ([] !== $post_statuses) {
if ( [] !== $post_statuses ) {
return $post_statuses;
}
$this->set_post_statuses();
Expand All @@ -63,7 +56,7 @@ public function get_post_statuses(): array {
*/
public function get_post_types(): array {
$post_types = $this->post_types;
if ([] !== $post_types) {
if ( [] !== $post_types ) {
return $post_types;
}
$this->set_post_types();
Expand All @@ -77,7 +70,7 @@ public function get_post_types(): array {
*/
public function get_parent_post_statuses(): array {
$parent_post_statuses = $this->parent_post_statuses;
if ([] !== $parent_post_statuses) {
if ( [] !== $parent_post_statuses ) {
return $parent_post_statuses;
}
$this->set_post_parent_statuses();
Expand Down
41 changes: 41 additions & 0 deletions plugins/hwp-previews/tests/wpunit/Hooks/PreviewHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,45 @@ public function test_filter_rest_prepare_link_no_link_previews_no_preview_url()
$this->assertArrayNotHasKey( 'link', $data );
$this->assertEquals( $original_response, $response );
}

public function test_setup_post_type_hooks_adds_filters_for_enabled_post_types() {
// Create a custom post type to test with
register_post_type( 'test_cpt', [
'public' => true,
'show_in_rest' => true,
'supports' => [ 'editor' ], // This enables Gutenberg
] );

$test_config = [
'post' => [
Settings_Field_Collection::ENABLED_FIELD_ID => true,
Settings_Field_Collection::PREVIEW_URL_FIELD_ID => 'https://localhost:3000/post?preview=true&post_id={ID}&status={status}',
],
'test_cpt' => [
Settings_Field_Collection::ENABLED_FIELD_ID => true,
Settings_Field_Collection::PREVIEW_URL_FIELD_ID => 'https://localhost:3000/cpt?preview=true&post_id={ID}&status={status}',
]
];
update_option( $this->test_option_key, $test_config );

// Remove any existing filters to have a clean slate
remove_all_filters( 'rest_post_query' );
remove_all_filters( 'rest_test_cpt_query' );
remove_all_filters( 'rest_prepare_post' );
remove_all_filters( 'rest_prepare_test_cpt' );

$preview_hooks = new Preview_Hooks();

// Call the method we're testing
$preview_hooks->setup_post_type_hooks();

// Assert that the filters were added for post types that support Gutenberg
$this->assertTrue( has_filter( 'rest_post_query', [ $preview_hooks, 'enable_post_statuses_as_parent' ] ) !== false );
$this->assertTrue( has_filter( 'rest_test_cpt_query', [ $preview_hooks, 'enable_post_statuses_as_parent' ] ) !== false );
$this->assertTrue( has_filter( 'rest_prepare_post', [ $preview_hooks, 'filter_rest_prepare_link' ] ) !== false );
$this->assertTrue( has_filter( 'rest_prepare_test_cpt', [ $preview_hooks, 'filter_rest_prepare_link' ] ) !== false );

// Clean up
unregister_post_type( 'test_cpt' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ public function remove_all_filters() {
remove_all_filters( 'hwp_previews_filter_available_post_statuses' );
}

public function test_get_allowed_post_types_returns_array(): void {
$result = $this->service->get_allowed_post_types();

$this->assertIsArray( $result );
$this->assertNotEmpty( $result );
}

public function test_get_post_statuses_returns_default_statuses(): void {
$result = $this->service->get_post_statuses();

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

public function test_get_post_types_returns_same_as_get_allowed_post_types(): void {
$allowed_types = $this->service->get_allowed_post_types();
$post_types = $this->service->get_post_types();

$this->assertEquals($allowed_types, $post_types);
}

public function test_post_types_filter_is_applied(): void {
$custom_post_types = ['custom_post' => 'Custom Post Type'];
add_filter('hwp_previews_filter_available_post_types', function() use ($custom_post_types) {
Expand Down
Loading