From 804fc41181f8ce8e957c03f2bf2ba7e6a160f42e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 8 Sep 2017 14:02:43 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-editor.php | 7 +- tests/phpunit/tests/editor/wpEditors.php | 82 ++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/tests/editor/wpEditors.php diff --git a/src/wp-includes/class-wp-editor.php b/src/wp-includes/class-wp-editor.php index a61f239788..1a402beeb7 100644 --- a/src/wp-includes/class-wp-editor.php +++ b/src/wp-includes/class-wp-editor.php @@ -1623,9 +1623,6 @@ final class _WP_Editors { // Do main query. $get_posts = new WP_Query; $posts = $get_posts->query( $query ); - // Check if any posts were found. - if ( ! $get_posts->post_count ) - return false; // Build results. $results = array(); @@ -1665,7 +1662,9 @@ final class _WP_Editors { * } * @param array $query An array of WP_Query arguments. */ - return apply_filters( 'wp_link_query', $results, $query ); + $results = apply_filters( 'wp_link_query', $results, $query ); + + return ! empty( $results ) ? $results : false; } /** diff --git a/tests/phpunit/tests/editor/wpEditors.php b/tests/phpunit/tests/editor/wpEditors.php new file mode 100644 index 0000000000..edaf698466 --- /dev/null +++ b/tests/phpunit/tests/editor/wpEditors.php @@ -0,0 +1,82 @@ + 123, + 'title' => 'foo', + 'permalink' => 'bar', + 'info' => 'baz', + ), + ) ); + } + + public function test_wp_link_query_returns_false_when_nothing_found() { + $actual = _WP_Editors::wp_link_query( array( 's' => 'foobarbaz' ) ); + + $this->assertFalse( $actual ); + } + + public function test_wp_link_query_returns_search_results() { + $post = self::factory()->post->create_and_get( array( 'post_status' => 'publish' ) ); + $actual = _WP_Editors::wp_link_query( array( 's' => $post->post_title ) ); + + $this->assertEqualSets( array( + array( + 'ID' => $post->ID, + 'title' => $post->post_title, + 'permalink' => get_permalink( $post->ID ), + 'info' => mysql2date( __( 'Y/m/d' ), $post->post_date ), + ), + ), $actual ); + } + + /** + * @ticket 41825 + */ + public function test_wp_link_query_returns_filtered_result_when_nothing_found() { + add_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) ); + $actual = _WP_Editors::wp_link_query( array( 's' => 'foobarbaz' ) ); + remove_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) ); + + $this->assertEqualSets( array( + array( + 'ID' => 123, + 'title' => 'foo', + 'permalink' => 'bar', + 'info' => 'baz', + ), + ), $actual ); + } + + public function test_wp_link_query_returns_filtered_search_results() { + $post = self::factory()->post->create_and_get( array( 'post_status' => 'publish' ) ); + + add_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) ); + $actual = _WP_Editors::wp_link_query( array( 's' => $post->post_title ) ); + remove_filter( 'wp_link_query', array( $this, 'wp_link_query_callback' ) ); + + $this->assertEqualSets( array( + array( + 'ID' => $post->ID, + 'title' => $post->post_title, + 'permalink' => get_permalink( $post->ID ), + 'info' => mysql2date( __( 'Y/m/d' ), $post->post_date ), + ), + array( + 'ID' => 123, + 'title' => 'foo', + 'permalink' => 'bar', + 'info' => 'baz', + ), + ), $actual ); + } +}