| 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 | |
|---|
| 30 | assert( $mq->queries == $expected ); |
|---|
| 31 | |
|---|
| 32 | $expected = " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) = '0') )"; |
|---|
| 33 | |
|---|
| 34 | $clauses = $mq->get_sql( 'post', $wpdb->posts, 'ID' ); |
|---|
| 35 | assert( $clauses['where'] == $expected ); |
|---|
| 36 | |
|---|
| 37 | // Only 'meta_key' (meta_value should be ignored) |
|---|
| 38 | $qv = array( |
|---|
| 39 | 'meta_key' => 'foo', |
|---|
| 40 | 'meta_value' => '', |
|---|
| 41 | ); |
|---|
| 42 | |
|---|
| 43 | $expected = array( |
|---|
| 44 | array( |
|---|
| 45 | 'key' => 'foo', |
|---|
| 46 | ) |
|---|
| 47 | ); |
|---|
| 48 | |
|---|
| 49 | $mq->parse_query_vars( $qv ); |
|---|
| 50 | assert( $mq->queries == $expected ); |
|---|
| 51 | |
|---|
| 52 | $expected = " AND (wp_postmeta.meta_key = 'foo' )"; |
|---|
| 53 | |
|---|
| 54 | $clauses = $mq->get_sql( 'post', $wpdb->posts, 'ID' ); |
|---|
| 55 | assert( $clauses['where'] == $expected ); |
|---|
| 56 | |
|---|
| 57 | // 'key' and 'value' (value should _not_ be ignored) |
|---|
| 58 | $meta_query = array( |
|---|
| 59 | array( |
|---|
| 60 | 'key' => 'foo', |
|---|
| 61 | 'value' => '', |
|---|
| 62 | 'compare' => '!=' |
|---|
| 63 | ) |
|---|
| 64 | ); |
|---|
| 65 | |
|---|
| 66 | $expected = " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) != '') )"; |
|---|
| 67 | |
|---|
| 68 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
|---|
| 69 | assert( $clauses['where'] == $expected ); |
|---|
| 70 | |
|---|
| 71 | // empty 'value' with 'IN' comparison ('value' should not be ignored) |
|---|
| 72 | $meta_query = array( |
|---|
| 73 | array( |
|---|
| 74 | 'key' => 'foo', |
|---|
| 75 | 'value' => '0', |
|---|
| 76 | 'compare' => 'NOT IN', |
|---|
| 77 | ) |
|---|
| 78 | ); |
|---|
| 79 | |
|---|
| 80 | $expected = " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) NOT IN ('0')) )"; |
|---|
| 81 | |
|---|
| 82 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
|---|
| 83 | assert( $clauses['where'] == $expected ); |
|---|
| 84 | |
|---|
| 85 | // 'relation' => 'AND' (default) |
|---|
| 86 | $meta_query = array( |
|---|
| 87 | array( |
|---|
| 88 | 'key' => 'foo', |
|---|
| 89 | 'value' => '0', |
|---|
| 90 | 'compare' => 'NOT IN', |
|---|
| 91 | ), |
|---|
| 92 | |
|---|
| 93 | array( |
|---|
| 94 | 'key' => 'bar', |
|---|
| 95 | ), |
|---|
| 96 | ); |
|---|
| 97 | |
|---|
| 98 | $expected = " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) NOT IN ('0')) AND mt1.meta_key = 'bar' )"; |
|---|
| 99 | |
|---|
| 100 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
|---|
| 101 | assert( $clauses['where'] == $expected ); |
|---|
| 102 | |
|---|
| 103 | // 'relation' => 'OR' |
|---|
| 104 | $meta_query = array( |
|---|
| 105 | 'relation' => 'or', |
|---|
| 106 | array( |
|---|
| 107 | 'key' => 'foo', |
|---|
| 108 | 'value' => '0', |
|---|
| 109 | 'compare' => 'NOT IN', |
|---|
| 110 | ), |
|---|
| 111 | |
|---|
| 112 | array( |
|---|
| 113 | 'key' => 'bar', |
|---|
| 114 | ), |
|---|
| 115 | ); |
|---|
| 116 | |
|---|
| 117 | $expected = " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) NOT IN ('0')) OR mt1.meta_key = 'bar' )"; |
|---|
| 118 | |
|---|
| 119 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
|---|
| 120 | assert( $clauses['where'] == $expected ); |
|---|
| 121 | }); |
|---|
| 122 | |
|---|