Make WordPress Core

Ticket #36949: 36949.3.patch

File 36949.3.patch, 5.2 KB (added by boonebgorges, 8 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index f1c1bce307..9880368d64 100644
    function update_termmeta_cache( $term_ids ) { 
    12581258 *               is specified and the pairing exists.
    12591259 */
    12601260function term_exists( $term, $taxonomy = '', $parent = null ) {
    1261         global $wpdb;
     1261        if ( is_int( $term ) ) {
     1262                if ( 0 == $term ) {
     1263                        return 0;
     1264                }
    12621265
    1263         $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
    1264         $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE ";
     1266                $_term = get_term( $term, $taxonomy );
     1267                if ( is_wp_error( $_term ) || is_null( $_term ) ) {
     1268                        return null;
     1269                }
     1270                if ( ! empty( $taxonomy ) ) {
     1271                        return array(
     1272                                'term_id' => (string) $_term->term_id,
     1273                                'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
     1274                        );
     1275                }
    12651276
    1266         if ( is_int($term) ) {
    1267                 if ( 0 == $term )
    1268                         return 0;
    1269                 $where = 't.term_id = %d';
    1270                 if ( !empty($taxonomy) )
    1271                         return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
    1272                 else
    1273                         return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
     1277                return (string) $_term->term_id;
    12741278        }
    12751279
    12761280        $term = trim( wp_unslash( $term ) );
    1277         $slug = sanitize_title( $term );
    12781281
    1279         $where = 't.slug = %s';
    1280         $else_where = 't.name = %s';
    1281         $where_fields = array($slug);
    1282         $else_where_fields = array($term);
    1283         $orderby = 'ORDER BY t.term_id ASC';
    1284         $limit = 'LIMIT 1';
    1285         if ( !empty($taxonomy) ) {
    1286                 if ( is_numeric( $parent ) ) {
    1287                         $parent = (int) $parent;
    1288                         $where_fields[] = $parent;
    1289                         $else_where_fields[] = $parent;
    1290                         $where .= ' AND tt.parent = %d';
    1291                         $else_where .= ' AND tt.parent = %d';
    1292                 }
     1282        if ( empty( $term ) ) {
     1283                return null;
     1284        }
    12931285
    1294                 $where_fields[] = $taxonomy;
    1295                 $else_where_fields[] = $taxonomy;
     1286        $slug = sanitize_title( $term );
    12961287
    1297                 if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields), ARRAY_A) )
    1298                         return $result;
     1288        $defaults = array(
     1289                'get'                    => 'all',
     1290                'hide_empty'                     => false,
     1291                'number'                 => 1,
     1292                'update_term_meta_cache' => false,
     1293                'orderby'                => 'term_id',
     1294                'suppress_filter'        => true,
     1295        );
     1296
     1297        if ( is_numeric( $parent ) ) {
     1298                $defaults['parent'] = (int) $parent;
     1299        }
     1300        if ( ! empty( $taxonomy ) ) {
     1301                $defaults['taxonomy'] = $taxonomy;
     1302        }
     1303        $args  = wp_parse_args( array( 'slug' => $term ), $defaults );
     1304        $terms = get_terms( $args );
     1305        if ( empty( $terms ) || is_wp_error( $terms ) ) {
     1306                $args = wp_parse_args( array( 'name' => $term ), $defaults );
    12991307
    1300                 return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields), ARRAY_A);
     1308                $terms = get_terms( $args );
     1309                if ( empty( $terms ) || is_wp_error( $terms ) ) {
     1310                        return null;
     1311                }
    13011312        }
     1313        $_term = array_shift( $terms );
    13021314
    1303         if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) )
    1304                 return $result;
     1315        if ( ! empty( $taxonomy ) ) {
     1316                return array(
     1317                        'term_id' => (string) $_term->term_id,
     1318                        'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
     1319                );
     1320        }
    13051321
    1306         return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );
     1322        return (string) $_term->term_id;
    13071323}
    13081324
    13091325/**
    function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { 
    22272243        foreach ( (array) $terms as $term) {
    22282244                if ( !strlen(trim($term)) )
    22292245                        continue;
    2230 
    22312246                if ( !$term_info = term_exists($term, $taxonomy) ) {
     2247
    22322248                        // Skip if a non-existent term ID is passed.
    22332249                        if ( is_int($term) )
    22342250                                continue;
  • tests/phpunit/tests/term/getTerm.php

    diff --git tests/phpunit/tests/term/getTerm.php tests/phpunit/tests/term/getTerm.php
    index 7aeb3f3a5b..62931616f9 100644
    class Tests_Term_GetTerm extends WP_UnitTestCase { 
    3030                        array( '%d' )
    3131                );
    3232
     33                clean_term_cache( $t1['term_id'], 'wptests_tax' );
     34
    3335                return array(
    3436                        array(
    3537                                'term_id' => $t1['term_id'],
  • tests/phpunit/tests/term/wpGetObjectTerms.php

    diff --git tests/phpunit/tests/term/wpGetObjectTerms.php tests/phpunit/tests/term/wpGetObjectTerms.php
    index f68e4f674d..03d3daec2d 100644
    class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { 
    338338                $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100006 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
    339339                $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100005 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
    340340
     341                clean_term_cache( array( $term_1->term_id, $term_2->term_id, $term_3->term_id ), $this->taxonomy );
     342
    341343                $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
    342344
    343345                $found = wp_get_object_terms( $p, $this->taxonomy, array(