Ticket #36949: 36949.patch
File 36949.patch, 3.7 KB (added by , 9 years ago) |
---|
-
src/wp-includes/taxonomy.php
1885 1885 * 1886 1886 * @global wpdb $wpdb WordPress database abstraction object. 1887 1887 * 1888 * @param int|string $ termThe term to check1888 * @param int|string $_term The term to check 1889 1889 * @param string $taxonomy The taxonomy name to use 1890 1890 * @param int $parent Optional. ID of parent term under which to confine the exists search. 1891 * @return mixed Returns null if the term does not exist. Returns the term ID 1891 * 1892 *@return mixed Returns null if the term does not exist. Returns the term ID 1892 1893 * if no taxonomy is specified and the term ID exists. Returns 1893 1894 * an array of the term ID and the term taxonomy ID the taxonomy 1894 1895 * is specified and the pairing exists. 1895 1896 */ 1896 1897 function term_exists( $term, $taxonomy = '', $parent = null ) { 1897 global $wpdb;1898 1898 1899 $select = "SELECT term_id FROM $wpdb->terms as t WHERE "; 1900 $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 "; 1899 if ( is_int( $term ) ) { 1900 if ( 0 == $term ) { 1901 return 0; 1902 } 1903 $_term = get_term( $term, $taxonomy ); 1904 if ( is_wp_error( $_term ) || is_null( $_term ) ) { 1905 $_term = null; 1906 } 1901 1907 1902 if ( is_int($term) ) { 1903 if ( 0 == $term ) 1904 return 0; 1905 $where = 't.term_id = %d'; 1906 if ( !empty($taxonomy) ) 1907 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A ); 1908 else 1909 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) ); 1908 return $_term->term_id; 1910 1909 } 1911 1910 1912 1911 $term = trim( wp_unslash( $term ) ); 1913 1912 $slug = sanitize_title( $term ); 1914 1913 1915 $where = 't.slug = %s'; 1916 $else_where = 't.name = %s'; 1917 $where_fields = array($slug); 1918 $else_where_fields = array($term); 1919 $orderby = 'ORDER BY t.term_id ASC'; 1920 $limit = 'LIMIT 1'; 1921 if ( !empty($taxonomy) ) { 1922 if ( is_numeric( $parent ) ) { 1923 $parent = (int) $parent; 1924 $where_fields[] = $parent; 1925 $else_where_fields[] = $parent; 1926 $where .= ' AND tt.parent = %d'; 1927 $else_where .= ' AND tt.parent = %d'; 1928 } 1914 $defaults = array( 1915 'taxonomy' => $taxonomy, 1916 'hide_empty' => false, 1917 'number' => 1, 1918 'orderby' => 'term_id' 1919 ); 1929 1920 1930 $where_fields[] = $taxonomy; 1931 $else_where_fields[] = $taxonomy; 1921 if ( is_numeric( $parent ) ) { 1922 $parent = (int) $parent; 1923 $defaults['parent'] = $parent; 1924 } 1932 1925 1933 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) ) 1934 return $result; 1926 $args = wp_parse_args( array( 'slug' => $slug ), $defaults ); 1935 1927 1936 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); 1928 $terms = get_terms( $args ); 1929 if ( empty( $terms ) || is_wp_error( $terms ) ) { 1930 $args = wp_parse_args( array( 'name' => $term ), $defaults ); 1931 1932 $terms = get_terms( $args ); 1933 if ( empty( $terms ) || is_wp_error( $terms ) ) { 1934 return null; 1935 } 1937 1936 } 1937 $_term = array_shift( $terms ); 1938 1938 1939 if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) ) 1940 return $result; 1941 1942 return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) ); 1939 return $_term->term_id; 1943 1940 } 1944 1941 1945 1942 /**