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

Filters are incorrectly applied in the __experimental/menu-items controller #34857

Merged
merged 7 commits into from
Sep 21, 2021

Conversation

anton-vlasenko
Copy link
Contributor

@anton-vlasenko anton-vlasenko commented Sep 15, 2021

Description

This PR fixes an issue with the filters in the WP_REST_Menu_Items_Controller::prepare_item_for_database method.
We explicitly apply title_save_pre, exceprt_save_pre, content_save_pre filters in there.
But later, we apply the same filters in the wp_insert_post function, so we don't need to apply them in the WP_REST_Menu_Items_Controller::prepare_item_for_database method.
Fixes #25095

How has this been tested?

Two regression unit tests have been written to cover this case. So I would say no testing is needed.
However, If you still want/need to test this, you can do it as described below.
Prerequisite: you will need XDebug to test it.

  1. Go to the Gutenberg -> Experiments page and enable Navigation screen feature.
  2. Save the changes.
  3. Open /wp-includes/plugin.php file in your IDE and place a conditional breakpoint at the beginning of the apply_filters function (somewhere around line 166).
  4. Use the following condition for your breakpoint:
$hook_name === 'content_save_pre'
  1. Go back to your browser, and open the Gutenberg -> Navigation page.
  2. Run the following JavaScript code in your browser's console:
wp.apiFetch( {
    path: '/__experimental/menu-items',
    method: 'POST',
    data: { title: 'Some Link', url: 'https://my.test.url' },
} ).then( ( res ) => {
    console.log( res );
} );
  1. Make sure that apply_filters function gets called only once (with $hook_name === 'content_save_pre').

Screenshots

No screenshots are needed for this issue.

Types of changes

Bug fix.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@anton-vlasenko anton-vlasenko changed the title [WIP] Fix/menus route filters applied twice Filters are incorrectly applied in the experimental/menu-items controller Sep 16, 2021
@anton-vlasenko anton-vlasenko changed the title Filters are incorrectly applied in the experimental/menu-items controller Filters are incorrectly applied in the __experimental/menu-items controller Sep 16, 2021
@spacedmonkey
Copy link
Member

Have you validated we don't need these filters?

I also, think, we do not need to add a unit test here. These menu endpoints are still in development and not stable yet, so removing filters doesn't need a unit test.

Comment on lines -545 to -556
// Apply the same filters as when calling wp_insert_post().

/** This filter is documented in wp-includes/post.php */
$prepared_nav_item['menu-item-title'] = wp_unslash( apply_filters( 'title_save_pre', wp_slash( $prepared_nav_item['menu-item-title'] ) ) );

/** This filter is documented in wp-includes/post.php */
$prepared_nav_item['menu-item-attr-title'] = wp_unslash( apply_filters( 'excerpt_save_pre', wp_slash( $prepared_nav_item['menu-item-attr-title'] ) ) );

/** This filter is documented in wp-includes/post.php */
$prepared_nav_item['menu-item-description'] = wp_unslash( apply_filters( 'content_save_pre', wp_slash( $prepared_nav_item['menu-item-description'] ) ) );

Copy link
Member

Choose a reason for hiding this comment

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

This is copied from here.

Do you know why they are there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you know why they are there?

I guess they are there to call apply_filters function before saving values to the database.
But that is not needed in lib/class-wp-rest-menu-items-controller.php file because we apply those filters later (yes, I've checked that, please see How has this been tested? section).

@anton-vlasenko
Copy link
Contributor Author

Have you validated we don't need these filters?

Yes, I've validated that. Please see How has this been tested?. We do apply the same filters later. I believe we don't want to apply these filters twice.

I also, think, we do not need to add a unit test here. These menu endpoints are still in development and not stable yet, so removing filters doesn't need a unit test.

We don't want to apply the same filters twice, and my unit test ensures that it will not happen again. It's a regression unit test.
If we change something in the endpoint we can always refactor the unit test.
But if we remove the unit test the same bug could appear again undetected.

@spacedmonkey
Copy link
Member

We don't want to apply the same filters twice

I was the one who originally added the filter and wrote this whole endpoint. I am happy to remove them. But the unit test is pointless and should be removed. The only way that the filter would be double applied, if someone adds it. Remove the tests, I will approve the PR.

@TimothyBJacobs
Copy link
Member

Code changes look good to me, thanks for the fix @anton-vlasenko! I agree the unit tests here aren't strictly necessary, but I don't think there is much of a cost to having them. I'll leave the final decision up to @spacedmonkey as he's been the main driver of this endpoint.

@spacedmonkey
Copy link
Member

Can you remove the tests for now @anton-vlasenko.

I have created a breakout issue, #34992 for improving test coverage for these endpoints. If it makes sense, we can add these tests back in that ticket. Agreed?

@anton-vlasenko anton-vlasenko force-pushed the fix/menus-route-filters-applied-twice branch from f30f0eb to 5b6c2d2 Compare September 21, 2021 16:29
@anton-vlasenko
Copy link
Contributor Author

@spacedmonkey

But the unit test is pointless and should be removed

Let me disagree with that. It's a valid regression unit test and it's not pointless.
Having more unit tests doesn't harm anything.
Even a "bad" unit test is better than no unit tests at all.

The only way that the filter would be double applied, if someone adds it.

Yes, and it can happen again.
The probability of this is not zero.
How can someone guarantee it will not happen again in, e.g., four years?
Unit tests are the only way to ensure that.

Can you remove the tests for now @anton-vlasenko.

Yes, I will remove it, but I haven't heard clear arguments against it.
So, hopefully, we can add it back as part of the #34992.

@spacedmonkey spacedmonkey merged commit d13c648 into trunk Sep 21, 2021
@spacedmonkey spacedmonkey deleted the fix/menus-route-filters-applied-twice branch September 21, 2021 21:32
@github-actions github-actions bot added this to the Gutenberg 11.6 milestone Sep 21, 2021
@talldan talldan added the [Type] Bug An existing feature does not function as intended label Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

REST API: Audit menus route filters.
4 participants