Make WordPress Core

Ticket #36949: 36949.2.diff

File 36949.2.diff, 5.0 KB (added by spacedmonkey, 6 years ago)
  • src/wp-includes/class-wp-term-query.php

     
    204204                        'parent'                 => '',
    205205                        'childless'              => false,
    206206                        'cache_domain'           => 'core',
     207                        'cached_results'         => true,
    207208                        'update_term_meta_cache' => true,
    208209                        'meta_query'             => '',
    209210                        'meta_key'               => '',
     
    676677                $key          = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request );
    677678                $last_changed = wp_cache_get_last_changed( 'terms' );
    678679                $cache_key    = "get_terms:$key:$last_changed";
    679                 $cache        = wp_cache_get( $cache_key, 'terms' );
     680                $cache        = false;
     681                if ( $args['cached_results'] ) {
     682                        $cache = wp_cache_get( $cache_key, 'terms' );
     683                }
    680684                if ( false !== $cache ) {
    681685                        if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    682686                                $cache = $this->populate_terms( $cache );
  • src/wp-includes/taxonomy.php

     
    13541354 *
    13551355 * @since 3.0.0
    13561356 *
    1357  * @global wpdb $wpdb WordPress database abstraction object.
    13581357 *
    13591358 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
    13601359 * @param string     $taxonomy The taxonomy name to use
     
    13651364 *               is specified and the pairing exists.
    13661365 */
    13671366function term_exists( $term, $taxonomy = '', $parent = null ) {
    1368         global $wpdb;
    1369 
    1370         $select     = "SELECT term_id FROM $wpdb->terms as t WHERE ";
    1371         $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 ";
    1372 
    13731367        if ( is_int( $term ) ) {
    13741368                if ( 0 == $term ) {
    13751369                        return 0;
    13761370                }
    1377                 $where = 't.term_id = %d';
     1371                $_term = get_term( $term, $taxonomy );
     1372                if ( is_wp_error( $_term ) || is_null( $_term ) ) {
     1373                        return null;
     1374                }
    13781375                if ( ! empty( $taxonomy ) ) {
    1379                         return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . ' AND tt.taxonomy = %s', $term, $taxonomy ), ARRAY_A );
    1380                 } else {
    1381                         return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
     1376                        return array(
     1377                                'term_id'          => (string) $_term->term_id,
     1378                                'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
     1379                        );
    13821380                }
     1381                return (string) $_term->term_id;
    13831382        }
    13841383
    13851384        $term = trim( wp_unslash( $term ) );
    1386         $slug = sanitize_title( $term );
     1385        if ( empty( $term ) ) {
     1386                return null;
     1387        }
    13871388
    1388         $where             = 't.slug = %s';
    1389         $else_where        = 't.name = %s';
    1390         $where_fields      = array( $slug );
    1391         $else_where_fields = array( $term );
    1392         $orderby           = 'ORDER BY t.term_id ASC';
    1393         $limit             = 'LIMIT 1';
     1389        $defaults = array(
     1390                'get'                    => 'all',
     1391                'hide_empty'             => false,
     1392                'number'                 => 1,
     1393                'update_term_meta_cache' => false,
     1394                'orderby'                => 'term_id',
     1395                'suppress_filter'        => true,
     1396                'cached_results'         => false,
     1397        );
     1398
     1399        if ( is_numeric( $parent ) ) {
     1400                $defaults['parent'] = (int) $parent;
     1401        }
    13941402        if ( ! empty( $taxonomy ) ) {
    1395                 if ( is_numeric( $parent ) ) {
    1396                         $parent              = (int) $parent;
    1397                         $where_fields[]      = $parent;
    1398                         $else_where_fields[] = $parent;
    1399                         $where              .= ' AND tt.parent = %d';
    1400                         $else_where         .= ' AND tt.parent = %d';
     1403                $defaults['taxonomy'] = $taxonomy;
     1404        }
     1405        $args  = wp_parse_args( array( 'slug' => $term ), $defaults );
     1406        $terms = get_terms( $args );
     1407        if ( empty( $terms ) || is_wp_error( $terms ) ) {
     1408                $args  = wp_parse_args( array( 'name' => $term ), $defaults );
     1409                $terms = get_terms( $args );
     1410                if ( empty( $terms ) || is_wp_error( $terms ) ) {
     1411                        return null;
    14011412                }
    1402 
    1403                 $where_fields[]      = $taxonomy;
    1404                 $else_where_fields[] = $taxonomy;
    1405 
    1406                 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 ) ) {
    1407                         return $result;
    1408                 }
    1409 
    1410                 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 );
    14111413        }
     1414        $_term = array_shift( $terms );
    14121415
    1413         if ( $result = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields ) ) ) {
    1414                 return $result;
     1416        if ( ! empty( $taxonomy ) ) {
     1417                return array(
     1418                        'term_id'          => (string) $_term->term_id,
     1419                        'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
     1420                );
    14151421        }
    14161422
    1417         return $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields ) );
     1423        return (string) $_term->term_id;
    14181424}
    14191425
    14201426/**