Make WordPress Core

Ticket #27272: 27272.2.diff

File 27272.2.diff, 2.9 KB (added by wonderboymusic, 11 years ago)
  • src/wp-includes/meta.php

     
    833833                                $meta_value = trim( $meta_value );
    834834                        }
    835835
     836                        switch ( $meta_type ) {
     837                                case 'SIGNED':
     838                                        $cast_string = '%d';
     839                                        break;
     840                                case 'UNSIGNED':
     841                                        $cast_string = '%u';
     842                                        break;
     843                                default:
     844                                        $cast_string = '%s';
     845                                        break;
     846                        }
     847                       
     848                        $meta_compare_string = $cast_string;
     849
    836850                        if ( 'IN' == substr( $meta_compare, -2) ) {
    837                                 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
     851                                $meta_compare_string = '(' . substr( str_repeat( ", $cast_string", count( $meta_value ) ), 1 ) . ')';
    838852                        } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
    839853                                $meta_value = array_slice( $meta_value, 0, 2 );
    840                                 $meta_compare_string = '%s AND %s';
     854                                $meta_compare_string = "$cast_string AND $cast_string";
    841855                        } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
    842856                                $meta_value = '%' . like_escape( $meta_value ) . '%';
    843                                 $meta_compare_string = '%s';
    844                         } else {
    845                                 $meta_compare_string = '%s';
    846857                        }
    847858
    848859                        if ( ! empty( $where[$k] ) )
  • tests/phpunit/tests/meta/query.php

     
    117117                ) );
    118118                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    119119
    120                 $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = '')" ) );
     120                $this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) );
    121121        }
    122122
    123123        /**
     
    194194                $this->assertContains( "{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where'] );
    195195                $this->assertNotContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
    196196        }
     197
     198        function test_cast_both_sides() {
     199                global $wpdb;
     200
     201                $query = new WP_Meta_Query( array(
     202                        array(
     203                                'key'       => 'number_string',
     204                                'value'     => '1000000',
     205                                'compare'   => '<=',
     206                                'type'      => 'NUMERIC',
     207                        ),
     208                ) );
     209
     210                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     211                $this->assertNotContains( "CAST({$wpdb->postmeta}.meta_value AS SIGNED) <= '1000000'", $sql['where'] );
     212                $this->assertContains( "CAST({$wpdb->postmeta}.meta_value AS SIGNED) <= 1000000", $sql['where'] );
     213        }
    197214}
  • tests/phpunit/tests/meta.php

     
    180180                ) );
    181181
    182182                $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
     183                $this->assertNotContains( "> '0'", $posts->request );
     184                $this->assertContains( "> 0", $posts->request );
    183185                $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
    184186        }
    185187