Ticket #12891: scale.12891.2.diff
File scale.12891.2.diff, 7.0 KB (added by , 15 years ago) |
---|
-
wp-includes/taxonomy.php
460 460 * @uses $wpdb 461 461 * @uses wp_parse_args() Creates an array from string $args. 462 462 * 463 * @param mixed $terms Term id/slug/name or array of such to match against463 * @param int|array $term_ids Term id or array of term ids of terms that will be used 464 464 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names 465 * @param array|string $args 466 * 'include_children' bool Whether to include term children (hierarchical taxonomies only) 467 * 'field' string Which term field is being used. Can be 'term_id', 'slug' or 'name' 468 * 'operator' string Can be 'IN' and 'NOT IN' 469 * 'do_query' bool Whether to execute the query or return the SQL string 470 * 471 * @return WP_Error If the taxonomy does not exist 472 * @return array The list of found object_ids 473 * @return string The SQL string, if do_query is set to false 465 * @param array|string $args Change the order of the object_ids, either ASC or DESC 466 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success 467 * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found. 474 468 */ 475 function get_objects_in_term( $term s, $taxonomies, $args = array() ) {469 function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { 476 470 global $wpdb; 477 471 478 extract( wp_parse_args( $args, array( 479 'include_children' => false, 480 'field' => 'term_id', 481 'operator' => 'IN', 482 'do_query' => true, 483 ) ), EXTR_SKIP ); 472 if ( ! is_array( $term_ids ) ) 473 $term_ids = array( $term_ids ); 484 474 485 $taxonomies = (array) $taxonomies; 475 if ( ! is_array( $taxonomies ) ) 476 $taxonomies = array( $taxonomies ); 486 477 487 foreach ( $taxonomies as $taxonomy ) {478 foreach ( (array) $taxonomies as $taxonomy ) { 488 479 if ( ! taxonomy_exists( $taxonomy ) ) 489 return new WP_Error( 'invalid_taxonomy', sprintf( __( 'Invalid Taxonomy: %s' ), $taxonomy) );480 return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) ); 490 481 } 491 482 492 if ( !in_array( $field, array( 'term_id', 'slug', 'name' ) ) ) 493 $field = 'term_id'; 483 $defaults = array( 'order' => 'ASC' ); 484 $args = wp_parse_args( $args, $defaults ); 485 extract( $args, EXTR_SKIP ); 494 486 495 if ( !in_array( $operator, array( 'IN', 'NOT IN' ) ) ) 496 $operator = 'IN'; 487 $order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC'; 497 488 498 $term s = array_unique( (array) $terms );489 $term_ids = array_map('intval', $term_ids ); 499 490 500 if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) {501 $children = array();502 foreach ( $terms as $term ) {503 if ( 'term_id' != $field ) {504 if ( $term = get_term_by( $field, $term, $taxonomy ) )505 $term = $term->term_id;506 else507 continue;508 }509 $children = array_merge( $children, get_term_children( $term, $taxonomy ) );510 $children[] = $term;511 }512 $terms = $children;513 $field = 'term_id';514 }515 516 if ( empty( $terms ) )517 return $do_query ? array() : '';518 519 491 $taxonomies = "'" . implode( "', '", $taxonomies ) . "'"; 492 $term_ids = "'" . implode( "', '", $term_ids ) . "'"; 520 493 521 switch ( $field ) { 522 case 'term_id': 523 $terms = array_map( 'intval', $terms ); 494 $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order"); 524 495 525 $terms = implode( ',', $terms ); 526 $sql = " 527 SELECT object_id 528 FROM $wpdb->term_relationships 529 INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id) 530 WHERE taxonomy IN ($taxonomies) 531 AND term_id $operator ($terms) 532 "; 533 break; 496 if ( ! $object_ids ) 497 return array(); 534 498 535 case 'slug': 536 case 'name': 537 foreach ( $terms as $i => $term ) { 538 $terms[$i] = sanitize_title_for_query( $term ); 539 } 540 $terms = array_filter($terms); 541 542 $terms = "'" . implode( "','", $terms ) . "'"; 543 $sql = " 544 SELECT object_id 545 FROM $wpdb->term_relationships 546 INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id) 547 INNER JOIN $wpdb->terms USING (term_id) 548 WHERE taxonomy IN ($taxonomies) 549 AND $field $operator ($terms) 550 "; 551 break; 552 } 553 554 return $do_query ? $wpdb->get_col( $sql ) : $sql; 499 return $object_ids; 555 500 } 556 501 557 502 /* … … 579 524 580 525 $sql = array(); 581 526 foreach ( $tax_query as $query ) { 582 if ( !isset( $query['include_children'] ) ) 583 $query['include_children'] = true; 527 extract( wp_parse_args( $query, array( 528 'taxonomy' => array(), 529 'terms' => array(), 530 'include_children' => true, 531 'field' => 'term_id', 532 'operator' => 'IN', 533 ) ) ); 584 534 585 $ query['do_query'] = false;535 $taxonomies = (array) $taxonomy; 586 536 587 $sql_single = get_objects_in_term( $query['terms'], $query['taxonomy'], $query ); 537 foreach ( $taxonomies as $taxonomy ) { 538 if ( ! taxonomy_exists( $taxonomy ) ) 539 return ' AND 0 = 1'; 540 } 588 541 589 if ( empty( $sql_single ) || is_wp_error( $sql_single) )590 return ' AND 0 = 1';542 if ( !in_array( $operator, array( 'IN', 'NOT IN' ) ) ) 543 $operator = 'IN'; 591 544 592 $sql[] = $sql_single; 593 } 545 $taxonomies = "'" . implode( "', '", $taxonomies ) . "'"; 594 546 595 if ( 1 == count( $sql ) ) { 596 $ids = $wpdb->get_col( $sql[0] ); 597 } else { 598 $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1"; 599 foreach ( $sql as $query ) 600 $r .= " AND object_id IN ($query)"; 547 $terms = array_unique( (array) $terms ); 601 548 602 $ids = $wpdb->get_col( $r ); 549 switch ( $field ) { 550 case 'slug': 551 case 'name': 552 $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $terms ) ) . "'"; 553 $terms = $wpdb->get_col( " 554 SELECT term_id 555 FROM $wpdb->term_taxonomy 556 INNER JOIN $wpdb->terms USING (term_id) 557 WHERE taxonomy IN ($taxonomies) 558 AND $field IN ($terms) 559 " ); 560 break; 561 562 default: 563 $terms = implode( ',', array_map( 'intval', $terms ) ); 564 $terms = $wpdb->get_col( " 565 SELECT term_id 566 FROM $wpdb->term_taxonomy 567 WHERE taxonomy IN ($taxonomies) 568 AND term_id IN ($terms) 569 " ); 570 } 571 572 if ( empty( $terms ) ) 573 continue; 574 575 if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) { 576 $children = array(); 577 foreach ( $terms as $term ) { 578 $children = array_merge( $children, get_term_children( $term, $taxonomy ) ); 579 $children[] = $term; 580 } 581 $terms = $children; 582 $field = 'term_id'; 583 } 584 585 $terms = implode( ',', $terms ); 586 587 $sql[] = " 588 SELECT object_id 589 FROM $wpdb->term_relationships 590 INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id) 591 WHERE taxonomy IN ($taxonomies) 592 AND term_id $operator ($terms) 593 "; 603 594 } 604 595 605 if ( !empty( $ids ) ) 606 return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")"; 607 else 608 return ' AND 0 = 1'; 596 $r = ''; 597 foreach ( $sql as $query ) 598 $r .= " AND $object_id_column IN ($query)"; 599 600 return $r; 609 601 } 610 602 611 612 603 /** 613 604 * Get all Term data from database by Term ID. 614 605 *