Make WordPress Core

Ticket #41266: 41266.patch

File 41266.patch, 2.3 KB (added by noisysocks, 7 years ago)

A patch based on Approach 1 that includes unit tests

  • src/wp-includes/class-wp-meta-query.php

     
    9595        protected $has_or_relation = false;
    9696
    9797        /**
     98         * The default meta table alias.
     99         *
     100         * @var string
     101         */
     102        protected $default_meta_table_alias = 'mt';
     103
     104        /**
    98105         * Constructor.
    99106         *
    100107         * @since 3.2.0
     
    511518                $alias = $this->find_compatible_table_alias( $clause, $parent_query );
    512519                if ( false === $alias ) {
    513520                        $i = count( $this->table_aliases );
    514                         $alias = $i ? 'mt' . $i : $this->meta_table;
     521                        $alias = $i ? $this->default_meta_table_alias . $i : $this->meta_table;
    515522
    516523                        // JOIN clauses for NOT EXISTS have their own syntax.
    517524                        if ( 'NOT EXISTS' === $meta_compare ) {
     
    727734        public function has_or_relation() {
    728735                return $this->has_or_relation;
    729736        }
     737
     738        /**
     739         * Sets default meta table alias.
     740         *
     741         * @param string $default_meta_table_alias The default meta table alias.
     742         *
     743         * @return void
     744         */
     745        public function set_default_meta_table_alias( $default_meta_table_alias ) {
     746                $this->default_meta_table_alias = $default_meta_table_alias;
     747        }
    730748}
  • tests/phpunit/tests/meta/query.php

     
    884884
    885885                $this->assertTrue( $q->has_or_relation() );
    886886        }
     887
     888        /**
     889         * Test that a custom default_meta_table_alias can be set.
     890         * @ticket 41266
     891         */
     892        public function test_set_default_meta_table_alias() {
     893                global $wpdb;
     894
     895                $query = new WP_Meta_Query();
     896
     897                $query->set_default_meta_table_alias( 'myalias' );
     898
     899                $the_complex_query[ 'meta_query' ] = array(
     900                        array( 'key' => 'my_first_key', 'value' => 'my_amazing_value' ),
     901                        array( 'key' => 'my_second_key', 'compare' => 'NOT EXISTS' ),
     902                        array( 'key' => 'my_third_key', 'value' => array(), 'compare' => 'IN' ),
     903                );
     904
     905                $query->parse_query_vars( $the_complex_query );
     906
     907                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     908
     909                $this->assertContains( 'AS myalias1', $sql[ 'join' ] );
     910                $this->assertContains( 'AS myalias2', $sql[ 'join' ] );
     911        }
    887912}