Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new filter "render_block_core_navigation_link_allowed_post_status" #63181

Merged
merged 3 commits into from
Dec 12, 2024

Conversation

Chrico
Copy link
Contributor

@Chrico Chrico commented Jul 5, 2024

This PR introduces a new Filter for rendering the "Navigation Link"-Block to whitelist post_status to not restrict the output to only "public" post_status.

See more information in: #33215

Why?

Since Gutenberg 9.8.0 (see commit: 1936a04 | issue: #27207) the "Navigation Link"-Block limits the output to only "public" post_status.

While it is possible to hook into WP_Query and the WP_REST_Post_Search_Handler via rest_post_search_query to extend the $query_args with custom post_status in the REST Response, the rendering part will still limit to post_status = "publish.

With this new filter render_block_core_navigation_link_allowed_post_status it is now possible to also whitelist multiple post_status for rendering in frontend.

Testing Instructions

  1. You need to have at least 1 Post which is post_status = "private".
  2. Add a new mu-plugin which hooks into following:
add_filter( 
    'render_block_core_navigation_link_allowed_post_status', 
    static function(array $postStatus): array {
        $postStatus[] = 'private';

        return $postStatus;
} );

add_filter( 
    'rest_post_search_query', 
    static function( array $queryArgs): array {
        $postStatus = $queryArgs['post_status'] ?? [];
        $postStatus[] = 'private';

        $queryArgs['post_status'] = $postStatus;

        return $queryArgs;
} );
  1. Create a new Page with "Navigation Block" and search for 0. the Post with post_status = "private" and insert
  2. Save Page and go to frontend -> "private Post"-link is rendered.

@Chrico Chrico requested a review from ajitbohra as a code owner July 5, 2024 10:27
Copy link

github-actions bot commented Jul 5, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Chrico <[email protected]>
Co-authored-by: getdave <[email protected]>
Co-authored-by: draganescu <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@akasunil akasunil added [Type] Enhancement A suggestion for improvement. [Block] Navigation Link Affects the Navigation Link Block labels Jul 5, 2024
@ndiego ndiego requested a review from getdave July 13, 2024 18:38
@getdave
Copy link
Contributor

getdave commented Nov 20, 2024

Apologies I missed this ping. I'll add this to my list of things to test. Seems reasonable to bring this in.

Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR.

I tested it and it works as described.

I had to adjust the testing instructions to account for post status being a string and I also included "Draft" posts so I could check it worked for more statuses:

add_filter(
	'rest_post_search_query',
	static function( array $queryArgs ): array {
		$postStatus = $queryArgs['post_status'] ?? array();

		if ( is_string( $postStatus ) ) {
			$postStatus = array( $postStatus );
		}

		$postStatus[] = 'private';
		$postStatus[] = 'draft';

		$queryArgs['post_status'] = $postStatus;

		return $queryArgs;
	}
);

Overall I'd say it's useful to open up this flexibility for developers.

@draganescu How do you feel about this one?

@getdave getdave added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Dec 3, 2024
@getdave
Copy link
Contributor

getdave commented Dec 3, 2024

Added the "Needs Dev Note" label as when it comes to WP 6.8 it will be useful to highlight this in the miscellaneous section.

@Chrico
Copy link
Contributor Author

Chrico commented Dec 3, 2024

@getdave any action from my end needed to complete this? :-)

Copy link
Contributor

@draganescu draganescu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me as well and is in line with where the discussion ended 👏🏻 well done.

@getdave getdave enabled auto-merge (squash) December 3, 2024 10:00
@getdave
Copy link
Contributor

getdave commented Dec 3, 2024

@draganescu This appears to be stalling on the 8.3 check. I think that check was probably added after the PR was created or something. I don't think that should block this PR.

Are you ok to green light me force merging this one?

@getdave getdave disabled auto-merge December 3, 2024 11:02
@getdave
Copy link
Contributor

getdave commented Dec 3, 2024

@Chrico I wonder if you be able to rebase this PR branch against upstream trunk? That should allow all the tests to go green without us having to force anything.

@getdave
Copy link
Contributor

getdave commented Dec 12, 2024

All the tests are passing here with the exception of React Native tests which I know to be currently problematic and have been disabled on trunk. Therefore I'm going to go ahead and force merge.

@getdave getdave merged commit 29978b7 into WordPress:trunk Dec 12, 2024
58 of 60 checks passed
@github-actions github-actions bot added this to the Gutenberg 20.0 milestone Dec 12, 2024
@getdave
Copy link
Contributor

getdave commented Dec 12, 2024

Dev Note

The Navigation block now supports filtering of the post statuses of Posts shown in the Navigation on the front of the site. The new filter render_block_core_navigation_link_allowed_post_status defaults to publish but that list can be extended via the hook:

add_filter( 
    'render_block_core_navigation_link_allowed_post_status', 
    static function(array $postStatus): array {
        $postStatus[] = 'private'; // append statuses to the array of default statuses.
        return $postStatus;
} );

yogeshbhutkar pushed a commit to yogeshbhutkar/gutenberg that referenced this pull request Dec 18, 2024
…status" (WordPress#63181)

* navigation-link // introduce new filter "render_block_core_navigation_link_allowed_post_status" to align with WP_Query whitelisting post_status in frontend.

* navigation-link // add $attributes and $block as filter params.

* Update @SInCE comment

---------

Co-authored-by: Chrico <[email protected]>
Co-authored-by: getdave <[email protected]>
Co-authored-by: draganescu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Link Affects the Navigation Link Block Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants