Make WordPress Core

Ticket #36949: 36949.patch

File 36949.patch, 3.7 KB (added by spacedmonkey, 9 years ago)
  • src/wp-includes/taxonomy.php

     
    18851885 *
    18861886 * @global wpdb $wpdb WordPress database abstraction object.
    18871887 *
    1888  * @param int|string $term    The term to check
     1888 * @param int|string $_term The term to check
    18891889 * @param string     $taxonomy The taxonomy name to use
    18901890 * @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
    18921893 *               if no taxonomy is specified and the term ID exists. Returns
    18931894 *               an array of the term ID and the term taxonomy ID the taxonomy
    18941895 *               is specified and the pairing exists.
    18951896 */
    18961897function term_exists( $term, $taxonomy = '', $parent = null ) {
    1897         global $wpdb;
    18981898
    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                }
    19011907
    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;
    19101909        }
    19111910
    19121911        $term = trim( wp_unslash( $term ) );
    19131912        $slug = sanitize_title( $term );
    19141913
    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        );
    19291920
    1930                 $where_fields[] = $taxonomy;
    1931                 $else_where_fields[] = $taxonomy;
     1921        if ( is_numeric( $parent ) ) {
     1922                $parent             = (int) $parent;
     1923                $defaults['parent'] = $parent;
     1924        }
    19321925
    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 );
    19351927
    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                }
    19371936        }
     1937        $_term = array_shift( $terms );
    19381938
    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;
    19431940}
    19441941
    19451942/**