From 89997e4c8039cc233e19a0370b2cae886854846a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 4 Sep 2013 19:40:17 +0000 Subject: [PATCH] Introduce `description__like` arg to `get_terms()`. Make `description__like` and `name__like` perform `LIKE`s with a wildcard on both sides of passed string. Previously, strings had to match the beginning of the name, so searching for `burrito` in `This is a burrito` would fail. Adds unit tests. Props aaroncampbell for the original patch, 5 years ago. Fixes #8214. git-svn-id: https://develop.svn.wordpress.org/trunk@25241 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 14 ++++++++++--- tests/phpunit/tests/term/getTerms.php | 29 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 0d06c51a09..c5af198cc7 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1166,9 +1166,12 @@ function get_term_to_edit( $id, $taxonomy ) { * search - Returned terms' names will contain the value of 'search', * case-insensitive. Default is an empty string. * - * name__like - Returned terms' names will begin with the value of 'name__like', + * name__like - Returned terms' names will contain the value of 'name__like', * case-insensitive. Default is empty string. * + * description__like - Returned terms' descriptions will contain the value of + * 'description__like', case-insensitive. Default is empty string. + * * The argument 'pad_counts', if set to true will include the quantity of a term's * children in the quantity of each term's "count" object variable. * @@ -1222,7 +1225,7 @@ function get_terms($taxonomies, $args = '') { $defaults = array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(), 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', - 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', + 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' ); $args = wp_parse_args( $args, $defaults ); $args['number'] = absint( $args['number'] ); @@ -1345,7 +1348,12 @@ function get_terms($taxonomies, $args = '') { if ( !empty($name__like) ) { $name__like = like_escape( $name__like ); - $where .= $wpdb->prepare( " AND t.name LIKE %s", $name__like . '%' ); + $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $name__like . '%' ); + } + + if ( ! empty( $description__like ) ) { + $description__like = like_escape( $description__like ); + $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $description__like . '%' ); } if ( '' !== $parent ) { diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 2a946be693..aec88e462c 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -150,4 +150,33 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'search' => 'bur', 'fields' => 'ids' ) ); $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms ); } + + function test_get_terms_like() { + $term_id1 = $this->factory->tag->create( array( 'name' => 'burrito', 'description' => 'This is a burrito.' ) ); + $term_id2 = $this->factory->tag->create( array( 'name' => 'taco', 'description' => 'Burning man.' ) ); + + $terms = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'bur', 'fields' => 'ids' ) ); + $this->assertEqualSets( array( $term_id1 ), $terms ); + + $terms2 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'bur', 'fields' => 'ids' ) ); + $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms2 ); + + $terms3 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'Bur', 'fields' => 'ids' ) ); + $this->assertEqualSets( array( $term_id1 ), $terms3 ); + + $terms4 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'Bur', 'fields' => 'ids' ) ); + $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms4 ); + + $terms5 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'ENCHILADA', 'fields' => 'ids' ) ); + $this->assertEmpty( $terms5 ); + + $terms6 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => 'ENCHILADA', 'fields' => 'ids' ) ); + $this->assertEmpty( $terms6 ); + + $terms7 = get_terms( 'post_tag', array( 'hide_empty' => false, 'name__like' => 'o', 'fields' => 'ids' ) ); + $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms7 ); + + $terms8 = get_terms( 'post_tag', array( 'hide_empty' => false, 'description__like' => '.', 'fields' => 'ids' ) ); + $this->assertEqualSets( array( $term_id1, $term_id2 ), $terms8 ); + } }