Make WordPress Core

Ticket #33369: 33369.01.patch

File 33369.01.patch, 11.6 KB (added by johnjamesjacoby, 9 years ago)

Pretty invasive $r vs. $args replacement to get_terms()

  • src/wp-includes/taxonomy.php

     
    16571657 */
    16581658function get_terms( $taxonomies, $args = '' ) {
    16591659        global $wpdb;
    1660         $empty_array = array();
    16611660
    1662         $single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );
    16631661        if ( ! is_array( $taxonomies ) ) {
    16641662                $taxonomies = array( $taxonomies );
    16651663        }
     
    16751673                'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false,
    16761674                'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
    16771675                'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
    1678         $args = wp_parse_args( $args, $defaults );
    1679         $args['number'] = absint( $args['number'] );
    1680         $args['offset'] = absint( $args['offset'] );
     1676        $r = wp_parse_args( $args, $defaults );
     1677        $r['number'] = absint( $r['number'] );
     1678        $r['offset'] = absint( $r['offset'] );
    16811679
    16821680        // Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
    16831681        $has_hierarchical_tax = false;
     
    16881686        }
    16891687
    16901688        if ( ! $has_hierarchical_tax ) {
    1691                 $args['hierarchical'] = false;
    1692                 $args['pad_counts'] = false;
     1689                $r['hierarchical'] = false;
     1690                $r['pad_counts'] = false;
    16931691        }
    16941692
    16951693        // 'parent' overrides 'child_of'.
    1696         if ( 0 < intval( $args['parent'] ) ) {
    1697                 $args['child_of'] = false;
     1694        if ( 0 < intval( $r['parent'] ) ) {
     1695                $r['child_of'] = false;
    16981696        }
    16991697
    1700         if ( 'all' == $args['get'] ) {
    1701                 $args['childless'] = false;
    1702                 $args['child_of'] = 0;
    1703                 $args['hide_empty'] = 0;
    1704                 $args['hierarchical'] = false;
    1705                 $args['pad_counts'] = false;
     1698        if ( 'all' == $r['get'] ) {
     1699                $r['childless'] = false;
     1700                $r['child_of'] = 0;
     1701                $r['hide_empty'] = 0;
     1702                $r['hierarchical'] = false;
     1703                $r['pad_counts'] = false;
    17061704        }
    17071705
    17081706        /**
     
    17101708         *
    17111709         * @since 3.1.0
    17121710         *
    1713          * @param array $args       An array of get_term() arguments.
     1711         * @param array $r          An array of modified get_terms() arguments.
    17141712         * @param array $taxonomies An array of taxonomies.
     1713         * @param array $args       An array of original get_terms() arguments.
    17151714         */
    1716         $args = apply_filters( 'get_terms_args', $args, $taxonomies );
     1715        $r = apply_filters( 'get_terms_args', $r, $taxonomies, $args );
    17171716
    17181717        // Avoid the query if the queried parent/child_of term has no descendants.
    1719         $child_of = $args['child_of'];
    1720         $parent   = $args['parent'];
     1718        $child_of = $r['child_of'];
     1719        $parent   = $r['parent'];
    17211720
    17221721        if ( $child_of ) {
    17231722                $_parent = $child_of;
     
    17381737                }
    17391738
    17401739                if ( ! $in_hierarchy ) {
    1741                         return $empty_array;
     1740                        return array();
    17421741                }
    17431742        }
    17441743
    1745         // $args can be whatever, only use the args defined in defaults to compute the key.
     1744        // $r can be whatever, only use the args defined in defaults to compute the key.
    17461745        $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    1747         $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
     1746        $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
    17481747        $last_changed = wp_cache_get( 'last_changed', 'terms' );
    17491748        if ( ! $last_changed ) {
    17501749                $last_changed = microtime();
     
    17611760                 *
    17621761                 * @param array $cache      Cached array of terms for the given taxonomy.
    17631762                 * @param array $taxonomies An array of taxonomies.
    1764                  * @param array $args       An array of get_terms() arguments.
     1763                 * @param array $r          An array of modified get_terms() arguments.
     1764                 * @param array $args       An array of original get_terms() arguments.
    17651765                 */
    1766                 return apply_filters( 'get_terms', $cache, $taxonomies, $args );
     1766                return apply_filters( 'get_terms', $cache, $taxonomies, $r, $args );
    17671767        }
    17681768
    1769         $_orderby = strtolower( $args['orderby'] );
     1769        $_orderby = strtolower( $r['orderby'] );
    17701770        if ( 'count' == $_orderby ) {
    17711771                $orderby = 'tt.count';
    17721772        } elseif ( 'name' == $_orderby ) {
    17731773                $orderby = 't.name';
    17741774        } elseif ( 'slug' == $_orderby ) {
    17751775                $orderby = 't.slug';
    1776         } elseif ( 'include' == $_orderby && ! empty( $args['include'] ) ) {
    1777                 $include = implode( ',', array_map( 'absint', $args['include'] ) );
     1776        } elseif ( 'include' == $_orderby && ! empty( $r['include'] ) ) {
     1777                $include = implode( ',', array_map( 'absint', $r['include'] ) );
    17781778                $orderby = "FIELD( t.term_id, $include )";
    17791779        } elseif ( 'term_group' == $_orderby ) {
    17801780                $orderby = 't.term_group';
     
    17941794         * @since 2.8.0
    17951795         *
    17961796         * @param string $orderby    `ORDERBY` clause of the terms query.
    1797          * @param array  $args       An array of terms query arguments.
     1797         * @param array  $r          An array of modified get_terms() arguments.
    17981798         * @param array  $taxonomies An array of taxonomies.
     1799         * @param array  $args       An array of original get_terms() arguments.
    17991800         */
    1800         $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
     1801        $orderby = apply_filters( 'get_terms_orderby', $orderby, $r, $taxonomies, $args );
    18011802
    1802         $order = strtoupper( $args['order'] );
     1803        $order = strtoupper( $r['order'] );
    18031804        if ( ! empty( $orderby ) ) {
    18041805                $orderby = "ORDER BY $orderby";
    18051806        } else {
     
    18121813
    18131814        $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
    18141815
    1815         $exclude = $args['exclude'];
    1816         $exclude_tree = $args['exclude_tree'];
    1817         $include = $args['include'];
     1816        $exclude = $r['exclude'];
     1817        $exclude_tree = $r['exclude_tree'];
     1818        $include = $r['include'];
    18181819
    18191820        $inclusions = '';
    18201821        if ( ! empty( $include ) ) {
     
    18461847        }
    18471848
    18481849        // 'childless' terms are those without an entry in the flattened term hierarchy.
    1849         $childless = (bool) $args['childless'];
     1850        $childless = (bool) $r['childless'];
    18501851        if ( $childless ) {
    18511852                foreach ( $taxonomies as $_tax ) {
    18521853                        $term_hierarchy = _get_term_hierarchy( $_tax );
     
    18661867         * @since 2.3.0
    18671868         *
    18681869         * @param string $exclusions `NOT IN` clause of the terms query.
    1869          * @param array  $args       An array of terms query arguments.
     1870         * @param array  $r          An array of modified get_terms() arguments.
    18701871         * @param array  $taxonomies An array of taxonomies.
     1872         * @param array  $args       An array of original get_terms() arguments.
    18711873         */
    1872         $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
     1874        $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $r, $taxonomies, $args );
    18731875
    18741876        if ( ! empty( $exclusions ) ) {
    18751877                $where .= $exclusions;
    18761878        }
    18771879
    1878         if ( ! empty( $args['name'] ) ) {
    1879                 $names = (array) $args['name'];
     1880        if ( ! empty( $r['name'] ) ) {
     1881                $names = (array) $r['name'];
    18801882                foreach ( $names as &$_name ) {
    18811883                        $_name = sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' );
    18821884                }
     
    18841886                $where .= " AND t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')";
    18851887        }
    18861888
    1887         if ( ! empty( $args['slug'] ) ) {
    1888                 if ( is_array( $args['slug'] ) ) {
    1889                         $slug = array_map( 'sanitize_title', $args['slug'] );
     1889        if ( ! empty( $r['slug'] ) ) {
     1890                if ( is_array( $r['slug'] ) ) {
     1891                        $slug = array_map( 'sanitize_title', $r['slug'] );
    18901892                        $where .= " AND t.slug IN ('" . implode( "', '", $slug ) . "')";
    18911893                } else {
    1892                         $slug = sanitize_title( $args['slug'] );
     1894                        $slug = sanitize_title( $r['slug'] );
    18931895                        $where .= " AND t.slug = '$slug'";
    18941896                }
    18951897        }
    18961898
    1897         if ( ! empty( $args['name__like'] ) ) {
    1898                 $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
     1899        if ( ! empty( $r['name__like'] ) ) {
     1900                $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $wpdb->esc_like( $r['name__like'] ) . '%' );
    18991901        }
    19001902
    1901         if ( ! empty( $args['description__like'] ) ) {
    1902                 $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
     1903        if ( ! empty( $r['description__like'] ) ) {
     1904                $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $wpdb->esc_like( $r['description__like'] ) . '%' );
    19031905        }
    19041906
    19051907        if ( '' !== $parent ) {
     
    19071909                $where .= " AND tt.parent = '$parent'";
    19081910        }
    19091911
    1910         $hierarchical = $args['hierarchical'];
    1911         if ( 'count' == $args['fields'] ) {
     1912        $hierarchical = $r['hierarchical'];
     1913        if ( 'count' == $r['fields'] ) {
    19121914                $hierarchical = false;
    19131915        }
    1914         if ( $args['hide_empty'] && !$hierarchical ) {
     1916        if ( $r['hide_empty'] && !$hierarchical ) {
    19151917                $where .= ' AND tt.count > 0';
    19161918        }
    19171919
    1918         $number = $args['number'];
    1919         $offset = $args['offset'];
     1920        $number = $r['number'];
     1921        $offset = $r['offset'];
    19201922
    19211923        // Don't limit the query results when we have to descend the family tree.
    19221924        if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
     
    19291931                $limits = '';
    19301932        }
    19311933
    1932         if ( ! empty( $args['search'] ) ) {
    1933                 $like = '%' . $wpdb->esc_like( $args['search'] ) . '%';
     1934        if ( ! empty( $r['search'] ) ) {
     1935                $like = '%' . $wpdb->esc_like( $r['search'] ) . '%';
    19341936                $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
    19351937        }
    19361938
    19371939        $selects = array();
    1938         switch ( $args['fields'] ) {
     1940        switch ( $r['fields'] ) {
    19391941                case 'all':
    19401942                        $selects = array( 't.*', 'tt.*' );
    19411943                        break;
     
    19591961                        break;
    19601962        }
    19611963
    1962         $_fields = $args['fields'];
     1964        $_fields = $r['fields'];
    19631965
    19641966        /**
    19651967         * Filter the fields to select in the terms query.
     
    19741976         * @since 2.8.0
    19751977         *
    19761978         * @param array $selects    An array of fields to select for the terms query.
    1977          * @param array $args       An array of term query arguments.
     1979         * @param array $r          An array of modified get_terms() arguments.
    19781980         * @param array $taxonomies An array of taxonomies.
     1981         * @param array $args       An array of original get_terms() arguments.
    19791982         */
    1980         $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
     1983        $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $r, $taxonomies, $args ) );
    19811984
    19821985        $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
    19831986
     
    19901993         *
    19911994         * @param array $pieces     Terms query SQL clauses.
    19921995         * @param array $taxonomies An array of taxonomies.
    1993          * @param array $args       An array of terms query arguments.
     1996         * @param array $r          An array of modified get_terms() arguments.
     1997         * @param array $args       An array of original get_terms() arguments.
    19941998         */
    1995         $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
     1999        $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $r, $args );
    19962000
    19972001        $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
    19982002        $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
     
    20162020                wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
    20172021
    20182022                /** This filter is documented in wp-includes/taxonomy.php */
    2019                 return apply_filters( 'get_terms', array(), $taxonomies, $args );
     2023                return apply_filters( 'get_terms', array(), $taxonomies, $r, $args );
    20202024        }
    20212025
    20222026        if ( $child_of ) {
     
    20292033        }
    20302034
    20312035        // Update term counts to include children.
    2032         if ( $args['pad_counts'] && 'all' == $_fields ) {
     2036        if ( $r['pad_counts'] && 'all' == $_fields ) {
    20332037                foreach ( $taxonomies as $_tax ) {
    20342038                        _pad_term_counts( $terms, $_tax );
    20352039                }
    20362040        }
    20372041
    20382042        // Make sure we show empty categories that have children.
    2039         if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
     2043        if ( $hierarchical && $r['hide_empty'] && is_array( $terms ) ) {
    20402044                foreach ( $terms as $k => $term ) {
    20412045                        if ( ! $term->count ) {
    20422046                                $children = get_term_children( $term->term_id, $term->taxonomy );
     
    20892093        wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
    20902094
    20912095        /** This filter is documented in wp-includes/taxonomy */
    2092         return apply_filters( 'get_terms', $terms, $taxonomies, $args );
     2096        return apply_filters( 'get_terms', $terms, $taxonomies, $r, $args );
    20932097}
    20942098
    20952099/**