diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index 357c12c112..82cb49144a 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -707,7 +707,7 @@ class WP_Meta_Query { $meta_type = strtoupper( $type ); - if ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', 'NUMERIC' ) ) ) + if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) ) return 'CHAR'; if ( 'NUMERIC' == $meta_type ) diff --git a/tests/phpunit/tests/meta/query.php b/tests/phpunit/tests/meta/query.php index 2b60662d1f..ef668ab04a 100644 --- a/tests/phpunit/tests/meta/query.php +++ b/tests/phpunit/tests/meta/query.php @@ -108,7 +108,7 @@ class Tests_Meta_Query extends WP_UnitTestCase { /** * @ticket 22967 - */ + */ function test_null_value_sql() { global $wpdb; @@ -119,4 +119,37 @@ class Tests_Meta_Query extends WP_UnitTestCase { $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) ); } + + /** + * @ticket 23033 + */ + function test_get_cast_for_type() { + $query = new WP_Meta_Query(); + $this->assertEquals( 'BINARY', $query->get_cast_for_type( 'BINARY' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'CHAR' ) ); + $this->assertEquals( 'DATE', $query->get_cast_for_type( 'DATE' ) ); + $this->assertEquals( 'DATETIME', $query->get_cast_for_type( 'DATETIME' ) ); + $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'SIGNED' ) ); + $this->assertEquals( 'UNSIGNED', $query->get_cast_for_type( 'UNSIGNED' ) ); + $this->assertEquals( 'TIME', $query->get_cast_for_type( 'TIME' ) ); + $this->assertEquals( 'SIGNED', $query->get_cast_for_type( 'NUMERIC' ) ); + $this->assertEquals( 'NUMERIC(10)', $query->get_cast_for_type( 'NUMERIC(10)' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10)' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10 )' ) ); + $this->assertEquals( 'NUMERIC(10, 5)', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5)' ) ); + $this->assertEquals( 'NUMERIC(10,5)', $query->get_cast_for_type( 'NUMERIC(10,5)' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC( 10, 5 )' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'NUMERIC(10, 5 )' ) ); + $this->assertEquals( 'DECIMAL', $query->get_cast_for_type( 'DECIMAL' ) ); + $this->assertEquals( 'DECIMAL(10)', $query->get_cast_for_type( 'DECIMAL(10)' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10 )' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL( 10)' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10 )' ) ); + $this->assertEquals( 'DECIMAL(10, 5)', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) ); + $this->assertEquals( 'DECIMAL(10,5)', $query->get_cast_for_type( 'DECIMAL(10,5)' ) ); + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'DECIMAL(10, 5)' ) ); + + $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) ); + } } diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index 9f42485a5d..1295ac5bc8 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -199,6 +199,139 @@ class Tests_Post_Query extends WP_UnitTestCase { $this->assertEquals( 0, count( $posts ) ); } + /** + * @ticket 23033 + */ + function test_meta_query_decimal_results() { + $post_1 = $this->factory->post->create(); + $post_2 = $this->factory->post->create(); + $post_3 = $this->factory->post->create(); + $post_4 = $this->factory->post->create(); + + update_post_meta( $post_1, 'decimal_value', '-0.3' ); + update_post_meta( $post_2, 'decimal_value', '0.23409844' ); + update_post_meta( $post_3, 'decimal_value', '0.3' ); + update_post_meta( $post_4, 'decimal_value', '0.4' ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '.300', + 'compare' => '=', + 'type' => 'DECIMAL(10,2)' + ) + ), + ) ); + $this->assertEquals( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0.35', + 'compare' => '>', + 'type' => 'DECIMAL(10,2)' + ) + ), + ) ); + $this->assertEquals( array( $post_4 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0.3', + 'compare' => '>=', + 'type' => 'DECIMAL(10,2)' + ) + ), + ) ); + $this->assertEquals( array( $post_3, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0', + 'compare' => '<', + 'type' => 'DECIMAL(10,2)' + ) + ), + ) ); + $this->assertEquals( array( $post_1 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0.3', + 'compare' => '<=', + 'type' => 'DECIMAL(10,2)' + ) + ), + + ) ); + $this->assertEquals( array( $post_1, $post_2, $post_3 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => array( 0.23409845, .31 ), + 'compare' => 'BETWEEN', + 'type' => 'DECIMAL(10, 10)' + ) + ), + ) ); + $this->assertEquals( array( $post_3 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => array( 0.23409845, .31 ), + 'compare' => 'NOT BETWEEN', + 'type' => 'DECIMAL(10,10)' + ) + ), + ) ); + $this->assertEquals( array( $post_1, $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '.3', + 'compare' => 'LIKE', + 'type' => 'DECIMAL(10,2)' + ) + ), + ) ); + $this->assertEquals( array( $post_1, $post_3 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '.3', + 'compare' => 'NOT LIKE', + 'type' => 'DECIMAL(10,2)' + ) + ), + ) ); + $this->assertEquals( array( $post_2, $post_4 ), wp_list_pluck( $query->posts, 'ID' ) ); + + $query = new WP_Query( array( + 'orderby' => 'meta_value', + 'order' => 'DESC', + 'meta_key' => 'decimal_value', + 'meta_type' => 'DECIMAL(10, 2)' + ) ); + $this->assertEquals( array( $post_4, $post_3, $post_2, $post_1 ), wp_list_pluck( $query->posts, 'ID' ) ); + + } + /** * @ticket 20604 */