| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | add_action('template_redirect', function() { |
|---|
| 4 | global $wpdb; |
|---|
| 5 | |
|---|
| 6 | assert_options(ASSERT_ACTIVE, 1); |
|---|
| 7 | assert_options(ASSERT_QUIET_EVAL, 1); |
|---|
| 8 | assert_options(ASSERT_WARNING, 0); |
|---|
| 9 | assert_options(ASSERT_CALLBACK, function ($file, $line, $code) use ( &$failed ) { |
|---|
| 10 | echo "Failed: $file (line $line)<hr />"; |
|---|
| 11 | }); |
|---|
| 12 | |
|---|
| 13 | $mq = new WP_Meta_Query; |
|---|
| 14 | |
|---|
| 15 | // 'meta_key' & 'meta_value' = 0 |
|---|
| 16 | $qv = array( |
|---|
| 17 | 'meta_key' => 'foo', |
|---|
| 18 | 'meta_value' => 0, |
|---|
| 19 | ); |
|---|
| 20 | |
|---|
| 21 | $expected = array( |
|---|
| 22 | array( |
|---|
| 23 | 'key' => 'foo', |
|---|
| 24 | 'value' => '0' |
|---|
| 25 | ) |
|---|
| 26 | ); |
|---|
| 27 | |
|---|
| 28 | $mq->parse_query_vars( $qv ); |
|---|
| 29 | assert( $mq->queries == $expected ); |
|---|
| 30 | |
|---|
| 31 | $expected = " AND wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) = '0'"; |
|---|
| 32 | |
|---|
| 33 | $clauses = $mq->get_sql( 'post', $wpdb->posts, 'ID' ); |
|---|
| 34 | assert( $clauses['where'] == $expected ); |
|---|
| 35 | |
|---|
| 36 | // Only 'meta_key' (meta_value should be ignored) |
|---|
| 37 | $qv = array( |
|---|
| 38 | 'meta_key' => 'foo', |
|---|
| 39 | 'meta_value' => '', |
|---|
| 40 | ); |
|---|
| 41 | |
|---|
| 42 | $expected = array( |
|---|
| 43 | array( |
|---|
| 44 | 'key' => 'foo', |
|---|
| 45 | ) |
|---|
| 46 | ); |
|---|
| 47 | |
|---|
| 48 | $mq->parse_query_vars( $qv ); |
|---|
| 49 | assert( $mq->queries == $expected ); |
|---|
| 50 | |
|---|
| 51 | $expected = " AND wp_postmeta.meta_key = 'foo'"; |
|---|
| 52 | |
|---|
| 53 | $clauses = $mq->get_sql( 'post', $wpdb->posts, 'ID' ); |
|---|
| 54 | assert( $clauses['where'] == $expected ); |
|---|
| 55 | |
|---|
| 56 | // 'key' and 'value' (value should _not_ be ignored) |
|---|
| 57 | $meta_query = array( |
|---|
| 58 | array( |
|---|
| 59 | 'key' => 'foo', |
|---|
| 60 | 'value' => '', |
|---|
| 61 | 'compare' => '!=' |
|---|
| 62 | ) |
|---|
| 63 | ); |
|---|
| 64 | |
|---|
| 65 | $expected = " AND wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) != ''"; |
|---|
| 66 | |
|---|
| 67 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
|---|
| 68 | assert( $clauses['where'] == $expected ); |
|---|
| 69 | |
|---|
| 70 | // empty 'value' with 'IN' comparison ('value' should not be ignored) |
|---|
| 71 | $meta_query = array( |
|---|
| 72 | array( |
|---|
| 73 | 'key' => 'foo', |
|---|
| 74 | 'value' => '0', |
|---|
| 75 | 'compare' => 'NOT IN', |
|---|
| 76 | ) |
|---|
| 77 | ); |
|---|
| 78 | |
|---|
| 79 | $expected = " AND wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) NOT IN ('0')"; |
|---|
| 80 | |
|---|
| 81 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
|---|
| 82 | assert( $clauses['where'] == $expected ); |
|---|
| 83 | }); |
|---|
| 84 | |
|---|