WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#41825 closed enhancement (fixed)

wp_link_query not applied if method finds nothing

Reported by: msebel Owned by: swissspidy
Milestone: 4.9 Priority: normal
Severity: normal Version: 3.7
Component: Editor Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description (last modified by swissspidy)

In class-wp-editor.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-editor.php

We have the public static function wp_link_query, which allows to search for internal links. There's also the possibility to alter the query 'wp_link_query_args' and to filter the results with 'wp_link_query'

Now the problem is, if a plugin *adds* additional search results in wp_link_query, that is possible, but if the WordPress Query itself finds nothing we return false. It would be nice to apply the filter there as well, as a plugin may be able to find more results, even if WP_Query finds nothing.

So I'd propose to change

if ( ! $get_posts->post_count )
 return false;

to

if ( ! $get_posts->post_count )
 return apply_filters( 'wp_link_query', false, $query );

Behold, as I'm planning to discuss and maybe change this with @swissspidy at a contributor day tomorrow in bern switzerland.

Attachments (6)

41825.patch (487 bytes) - added by mitraval192 2 years ago.
Added patch
41825.2.patch (3.2 KB) - added by msebel 2 years ago.
patch with docs, making sure it still returns false if filter is not used
41825.diff (3.1 KB) - added by msebel 2 years ago.
patch, documentation (ideents with tabs, still learning)
41825.2.diff (2.4 KB) - added by swissspidy 2 years ago.
41825.3.diff (5.0 KB) - added by swissspidy 2 years ago.
41825.4.diff (3.4 KB) - added by swissspidy 2 years ago.

Download all attachments as: .zip

Change History (13)

@mitraval192
2 years ago

Added patch

#1 @swissspidy
2 years ago

  • Description modified (diff)
  • Version changed from trunk to 3.7

@msebel
2 years ago

patch with docs, making sure it still returns false if filter is not used

@msebel
2 years ago

patch, documentation (ideents with tabs, still learning)

@swissspidy
2 years ago

#2 @swissspidy
2 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 4.9

@swissspidy
2 years ago

#3 @swissspidy
2 years ago

  • Keywords has-unit-tests added

@swissspidy
2 years ago

#4 @ocean90
2 years ago

  • Keywords commit added

#5 @swissspidy
2 years ago

  • Owner set to swissspidy
  • Resolution set to fixed
  • Status changed from new to closed

In 41346:

Editor: In _WP_Editors::wp_link_query, allow filtering empty results.

Previously, it was not possible to hook into the wp_link_query filter to add custom entries when the
query returned no posts.

Props mitraval192, msebel.
Fixes #41825.

#6 follow-up: @ramiy
2 years ago

@swissspidy I think the return output can be more readable.

Replace:

return ! empty( $results ) ? $results : false;

With:

return empty( $results ) ? false : $results;

#7 in reply to: ↑ 6 @SergeyBiryukov
2 years ago

Replying to ramiy:

I think the return output can be more readable.

Testing for non-empty results is generally more intuitive, per the coding standards.

We could probably just omit empty() altogether: return $results ? $results : false;

Note: See TracTickets for help on using tickets.