Ticket #33369: 33369.01.patch
File 33369.01.patch, 11.6 KB (added by , 9 years ago) |
---|
-
src/wp-includes/taxonomy.php
1657 1657 */ 1658 1658 function get_terms( $taxonomies, $args = '' ) { 1659 1659 global $wpdb; 1660 $empty_array = array();1661 1660 1662 $single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );1663 1661 if ( ! is_array( $taxonomies ) ) { 1664 1662 $taxonomies = array( $taxonomies ); 1665 1663 } … … 1675 1673 'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false, 1676 1674 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '', 1677 1675 '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'] ); 1681 1679 1682 1680 // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. 1683 1681 $has_hierarchical_tax = false; … … 1688 1686 } 1689 1687 1690 1688 if ( ! $has_hierarchical_tax ) { 1691 $ args['hierarchical'] = false;1692 $ args['pad_counts'] = false;1689 $r['hierarchical'] = false; 1690 $r['pad_counts'] = false; 1693 1691 } 1694 1692 1695 1693 // '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; 1698 1696 } 1699 1697 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; 1706 1704 } 1707 1705 1708 1706 /** … … 1710 1708 * 1711 1709 * @since 3.1.0 1712 1710 * 1713 * @param array $ args An array of get_term() arguments.1711 * @param array $r An array of modified get_terms() arguments. 1714 1712 * @param array $taxonomies An array of taxonomies. 1713 * @param array $args An array of original get_terms() arguments. 1715 1714 */ 1716 $ args = apply_filters( 'get_terms_args', $args, $taxonomies );1715 $r = apply_filters( 'get_terms_args', $r, $taxonomies, $args ); 1717 1716 1718 1717 // 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']; 1721 1720 1722 1721 if ( $child_of ) { 1723 1722 $_parent = $child_of; … … 1738 1737 } 1739 1738 1740 1739 if ( ! $in_hierarchy ) { 1741 return $empty_array;1740 return array(); 1742 1741 } 1743 1742 } 1744 1743 1745 // $ argscan 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. 1746 1745 $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 ); 1748 1747 $last_changed = wp_cache_get( 'last_changed', 'terms' ); 1749 1748 if ( ! $last_changed ) { 1750 1749 $last_changed = microtime(); … … 1761 1760 * 1762 1761 * @param array $cache Cached array of terms for the given taxonomy. 1763 1762 * @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. 1765 1765 */ 1766 return apply_filters( 'get_terms', $cache, $taxonomies, $ args );1766 return apply_filters( 'get_terms', $cache, $taxonomies, $r, $args ); 1767 1767 } 1768 1768 1769 $_orderby = strtolower( $ args['orderby'] );1769 $_orderby = strtolower( $r['orderby'] ); 1770 1770 if ( 'count' == $_orderby ) { 1771 1771 $orderby = 'tt.count'; 1772 1772 } elseif ( 'name' == $_orderby ) { 1773 1773 $orderby = 't.name'; 1774 1774 } elseif ( 'slug' == $_orderby ) { 1775 1775 $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'] ) ); 1778 1778 $orderby = "FIELD( t.term_id, $include )"; 1779 1779 } elseif ( 'term_group' == $_orderby ) { 1780 1780 $orderby = 't.term_group'; … … 1794 1794 * @since 2.8.0 1795 1795 * 1796 1796 * @param string $orderby `ORDERBY` clause of the terms query. 1797 * @param array $ args An array of terms queryarguments.1797 * @param array $r An array of modified get_terms() arguments. 1798 1798 * @param array $taxonomies An array of taxonomies. 1799 * @param array $args An array of original get_terms() arguments. 1799 1800 */ 1800 $orderby = apply_filters( 'get_terms_orderby', $orderby, $ args, $taxonomies );1801 $orderby = apply_filters( 'get_terms_orderby', $orderby, $r, $taxonomies, $args ); 1801 1802 1802 $order = strtoupper( $ args['order'] );1803 $order = strtoupper( $r['order'] ); 1803 1804 if ( ! empty( $orderby ) ) { 1804 1805 $orderby = "ORDER BY $orderby"; 1805 1806 } else { … … 1812 1813 1813 1814 $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; 1814 1815 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']; 1818 1819 1819 1820 $inclusions = ''; 1820 1821 if ( ! empty( $include ) ) { … … 1846 1847 } 1847 1848 1848 1849 // 'childless' terms are those without an entry in the flattened term hierarchy. 1849 $childless = (bool) $ args['childless'];1850 $childless = (bool) $r['childless']; 1850 1851 if ( $childless ) { 1851 1852 foreach ( $taxonomies as $_tax ) { 1852 1853 $term_hierarchy = _get_term_hierarchy( $_tax ); … … 1866 1867 * @since 2.3.0 1867 1868 * 1868 1869 * @param string $exclusions `NOT IN` clause of the terms query. 1869 * @param array $ args An array of terms queryarguments.1870 * @param array $r An array of modified get_terms() arguments. 1870 1871 * @param array $taxonomies An array of taxonomies. 1872 * @param array $args An array of original get_terms() arguments. 1871 1873 */ 1872 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $ args, $taxonomies );1874 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $r, $taxonomies, $args ); 1873 1875 1874 1876 if ( ! empty( $exclusions ) ) { 1875 1877 $where .= $exclusions; 1876 1878 } 1877 1879 1878 if ( ! empty( $ args['name'] ) ) {1879 $names = (array) $ args['name'];1880 if ( ! empty( $r['name'] ) ) { 1881 $names = (array) $r['name']; 1880 1882 foreach ( $names as &$_name ) { 1881 1883 $_name = sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' ); 1882 1884 } … … 1884 1886 $where .= " AND t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')"; 1885 1887 } 1886 1888 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'] ); 1890 1892 $where .= " AND t.slug IN ('" . implode( "', '", $slug ) . "')"; 1891 1893 } else { 1892 $slug = sanitize_title( $ args['slug'] );1894 $slug = sanitize_title( $r['slug'] ); 1893 1895 $where .= " AND t.slug = '$slug'"; 1894 1896 } 1895 1897 } 1896 1898 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'] ) . '%' ); 1899 1901 } 1900 1902 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'] ) . '%' ); 1903 1905 } 1904 1906 1905 1907 if ( '' !== $parent ) { … … 1907 1909 $where .= " AND tt.parent = '$parent'"; 1908 1910 } 1909 1911 1910 $hierarchical = $ args['hierarchical'];1911 if ( 'count' == $ args['fields'] ) {1912 $hierarchical = $r['hierarchical']; 1913 if ( 'count' == $r['fields'] ) { 1912 1914 $hierarchical = false; 1913 1915 } 1914 if ( $ args['hide_empty'] && !$hierarchical ) {1916 if ( $r['hide_empty'] && !$hierarchical ) { 1915 1917 $where .= ' AND tt.count > 0'; 1916 1918 } 1917 1919 1918 $number = $ args['number'];1919 $offset = $ args['offset'];1920 $number = $r['number']; 1921 $offset = $r['offset']; 1920 1922 1921 1923 // Don't limit the query results when we have to descend the family tree. 1922 1924 if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) { … … 1929 1931 $limits = ''; 1930 1932 } 1931 1933 1932 if ( ! empty( $ args['search'] ) ) {1933 $like = '%' . $wpdb->esc_like( $ args['search'] ) . '%';1934 if ( ! empty( $r['search'] ) ) { 1935 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%'; 1934 1936 $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like ); 1935 1937 } 1936 1938 1937 1939 $selects = array(); 1938 switch ( $ args['fields'] ) {1940 switch ( $r['fields'] ) { 1939 1941 case 'all': 1940 1942 $selects = array( 't.*', 'tt.*' ); 1941 1943 break; … … 1959 1961 break; 1960 1962 } 1961 1963 1962 $_fields = $ args['fields'];1964 $_fields = $r['fields']; 1963 1965 1964 1966 /** 1965 1967 * Filter the fields to select in the terms query. … … 1974 1976 * @since 2.8.0 1975 1977 * 1976 1978 * @param array $selects An array of fields to select for the terms query. 1977 * @param array $ args An array of term queryarguments.1979 * @param array $r An array of modified get_terms() arguments. 1978 1980 * @param array $taxonomies An array of taxonomies. 1981 * @param array $args An array of original get_terms() arguments. 1979 1982 */ 1980 $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $ args, $taxonomies ) );1983 $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $r, $taxonomies, $args ) ); 1981 1984 1982 1985 $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; 1983 1986 … … 1990 1993 * 1991 1994 * @param array $pieces Terms query SQL clauses. 1992 1995 * @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. 1994 1998 */ 1995 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $ args );1999 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $r, $args ); 1996 2000 1997 2001 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; 1998 2002 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; … … 2016 2020 wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS ); 2017 2021 2018 2022 /** 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 ); 2020 2024 } 2021 2025 2022 2026 if ( $child_of ) { … … 2029 2033 } 2030 2034 2031 2035 // Update term counts to include children. 2032 if ( $ args['pad_counts'] && 'all' == $_fields ) {2036 if ( $r['pad_counts'] && 'all' == $_fields ) { 2033 2037 foreach ( $taxonomies as $_tax ) { 2034 2038 _pad_term_counts( $terms, $_tax ); 2035 2039 } 2036 2040 } 2037 2041 2038 2042 // 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 ) ) { 2040 2044 foreach ( $terms as $k => $term ) { 2041 2045 if ( ! $term->count ) { 2042 2046 $children = get_term_children( $term->term_id, $term->taxonomy ); … … 2089 2093 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); 2090 2094 2091 2095 /** 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 ); 2093 2097 } 2094 2098 2095 2099 /**