Skip to content

Commit 804fc41

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@41346 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5c3d713 commit 804fc41

2 files changed

Lines changed: 85 additions & 4 deletions

File tree

src/wp-includes/class-wp-editor.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,6 @@ public static function wp_link_query( $args = array() ) {
16231623
// Do main query.
16241624
$get_posts = new WP_Query;
16251625
$posts = $get_posts->query( $query );
1626-
// Check if any posts were found.
1627-
if ( ! $get_posts->post_count )
1628-
return false;
16291626

16301627
// Build results.
16311628
$results = array();
@@ -1665,7 +1662,9 @@ public static function wp_link_query( $args = array() ) {
16651662
* }
16661663
* @param array $query An array of WP_Query arguments.
16671664
*/
1668-
return apply_filters( 'wp_link_query', $results, $query );
1665+
$results = apply_filters( 'wp_link_query', $results, $query );
1666+
1667+
return ! empty( $results ) ? $results : false;
16691668
}
16701669

16711670
/**
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
if ( ! class_exists( '_WP_Editors', false ) ) {
4+
require_once ABSPATH . WPINC . '/class-wp-editor.php';
5+
}
6+
7+
/**
8+
* @group editor
9+
*/
10+
class Tests_WP_Editors extends WP_UnitTestCase {
11+
public function wp_link_query_callback( $results ) {
12+
return array_merge( $results, array(
13+
array(
14+
'ID' => 123,
15+
'title' => 'foo',
16+
'permalink' => 'bar',
17+
'info' => 'baz',
18+
),
19+
) );
20+
}
21+
22+
public function test_wp_link_query_returns_false_when_nothing_found() {
23+
$actual = _WP_Editors::wp_link_query( array( 's' => 'foobarbaz' ) );
24+
25+
$this->assertFalse( $actual );
26+
}
27+
28+
public function test_wp_link_query_returns_search_results() {
29+
$post = self::factory()->post->create_and_get( array( 'post_status' => 'publish' ) );
30+
$actual = _WP_Editors::wp_link_query( array( 's' => $post->post_title ) );
31+
32+
$this->assertEqualSets( array(
33+
array(
34+
'ID' => $post->ID,
35+
'title' => $post->post_title,
36+
'permalink' => get_permalink( $post->ID ),
37+
'info' => mysql2date( __( 'Y/m/d' ), $post->post_date ),
38+
),
39+
), $actual );
40+
}
41+
42+
/**
43+
* @ticket 41825
44+
*/
45+
public function test_wp_link_query_returns_filtered_result_when_nothing_found() {
46+
add_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) );
47+
$actual = _WP_Editors::wp_link_query( array( 's' => 'foobarbaz' ) );
48+
remove_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) );
49+
50+
$this->assertEqualSets( array(
51+
array(
52+
'ID' => 123,
53+
'title' => 'foo',
54+
'permalink' => 'bar',
55+
'info' => 'baz',
56+
),
57+
), $actual );
58+
}
59+
60+
public function test_wp_link_query_returns_filtered_search_results() {
61+
$post = self::factory()->post->create_and_get( array( 'post_status' => 'publish' ) );
62+
63+
add_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) );
64+
$actual = _WP_Editors::wp_link_query( array( 's' => $post->post_title ) );
65+
remove_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) );
66+
67+
$this->assertEqualSets( array(
68+
array(
69+
'ID' => $post->ID,
70+
'title' => $post->post_title,
71+
'permalink' => get_permalink( $post->ID ),
72+
'info' => mysql2date( __( 'Y/m/d' ), $post->post_date ),
73+
),
74+
array(
75+
'ID' => 123,
76+
'title' => 'foo',
77+
'permalink' => 'bar',
78+
'info' => 'baz',
79+
),
80+
), $actual );
81+
}
82+
}

0 commit comments

Comments
 (0)