Make WordPress Core

Ticket #31045: 31045.patch

File 31045.patch, 4.5 KB (added by Funkatronic, 10 years ago)

Fixed a lapse in coding judgement by me

  • src/wp-includes/meta.php

     
    934934        protected $table_aliases = array();
    935935
    936936        /**
     937         * A flat list of clauses, keyed by table aliases.
     938         *
     939         * @since 4.2.0
     940         * @var array
     941         */
     942        protected $clauses = array();
     943
     944        /**
    937945         * Constructor.
    938946         *
    939947         * @since 3.2.0
     
    13741382                // Save the alias to this clause, for future siblings to find.
    13751383                $clause['alias'] = $alias;
    13761384
     1385                // Determine the data type.
     1386                $meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
     1387                $clause['cast'] = $meta_type;
     1388
     1389                // Store the clause in our flat array.
     1390                $clause_name = isset( $clause['name'] ) ? $clause['name'] : $clause['alias'];
     1391                $this->clauses[ $clause_name ] =& $clause;
     1392
    13771393                // Next, build the WHERE clause.
    13781394
    13791395                // meta_key.
     
    13881404                // meta_value.
    13891405                if ( array_key_exists( 'value', $clause ) ) {
    13901406                        $meta_value = $clause['value'];
    1391                         $meta_type = $this->get_cast_for_type( isset( $clause['type'] ) ? $clause['type'] : '' );
    13921407
    13931408                        if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
    13941409                                if ( ! is_array( $meta_value ) ) {
     
    14501465                return $sql_chunks;
    14511466        }
    14521467
     1468        public function get_clauses() {
     1469                return $this->clauses;
     1470        }
     1471
    14531472        /**
    14541473         * Identify an existing table alias that is compatible with the current
    14551474         * query clause.
  • src/wp-includes/query.php

     
    22332233
    22342234                $primary_meta_key = '';
    22352235                $primary_meta_query = false;
    2236                 if ( ! empty( $this->meta_query->queries ) ) {
    2237                         $primary_meta_query = reset( $this->meta_query->queries );
     2236                $meta_clauses = $this->meta_query->get_clauses();
     2237                if ( ! empty( $meta_clauses ) ) {
     2238                        $primary_meta_query = reset( $meta_clauses );
    22382239
    22392240                        if ( ! empty( $primary_meta_query['key'] ) ) {
    22402241                                $primary_meta_key = $primary_meta_query['key'];
     
    22432244
    22442245                        $allowed_keys[] = 'meta_value';
    22452246                        $allowed_keys[] = 'meta_value_num';
     2247                        $allowed_keys   = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
    22462248                }
    22472249
    22482250                if ( ! in_array( $orderby, $allowed_keys ) ) {
     
    22602262                        case 'ID':
    22612263                        case 'menu_order':
    22622264                        case 'comment_count':
    2263                                 $orderby = "$wpdb->posts.{$orderby}";
     2265                                $orderby_clause = "$wpdb->posts.{$orderby}";
    22642266                                break;
    22652267                        case 'rand':
    2266                                 $orderby = 'RAND()';
     2268                                $orderby_clause = 'RAND()';
    22672269                                break;
    22682270                        case $primary_meta_key:
    22692271                        case 'meta_value':
    22702272                                if ( ! empty( $primary_meta_query['type'] ) ) {
    2271                                         $sql_type = $this->meta_query->get_cast_for_type( $primary_meta_query['type'] );
    2272                                         $orderby = "CAST($wpdb->postmeta.meta_value AS {$sql_type})";
     2273                                        $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
    22732274                                } else {
    2274                                         $orderby = "$wpdb->postmeta.meta_value";
     2275                                        $orderby_clause = "{$primary_meta_query['alias']}.meta_value";
    22752276                                }
    22762277                                break;
    22772278                        case 'meta_value_num':
    2278                                 $orderby = "$wpdb->postmeta.meta_value+0";
     2279                                $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
    22792280                                break;
    22802281                        default:
    2281                                 $orderby = "$wpdb->posts.post_" . $orderby;
     2282                                if( array_key_exists( $orderby, $meta_clauses ) ){
     2283                                        $meta_clause = $meta_clauses[ $orderby ];
     2284                                        $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
     2285                                } else {
     2286                                        $orderby_clause = "$wpdb->posts.post_" . $orderby;
     2287                                }
    22822288                                break;
    22832289                }
    22842290
    2285                 return $orderby;
     2291                return apply_filters( 'parse_orderby', $orderby_clause, $orderby );
    22862292        }
    22872293
    22882294        /**
     
    28132819
    28142820                $where .= $search . $whichauthor . $whichmimetype;
    28152821
     2822                if ( ! empty( $this->meta_query->queries ) ) {
     2823                        $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     2824                        $join   .= $clauses['join'];
     2825                        $where  .= $clauses['where'];
     2826                }
     2827
    28162828                $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
    28172829                if ( ! isset( $q['order'] ) ) {
    28182830                        $q['order'] = $rand ? '' : 'DESC';
     
    30303042                        $where .= ')';
    30313043                }
    30323044
    3033                 if ( !empty( $this->meta_query->queries ) ) {
    3034                         $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
    3035                         $join .= $clauses['join'];
    3036                         $where .= $clauses['where'];
    3037                 }
    3038 
    30393045                /*
    30403046                 * Apply filters on where and join prior to paging so that any
    30413047                 * manipulations to them are reflected in the paging by day queries.