Make WordPress Core

Ticket #18448: 18448.patch.diff

File 18448.patch.diff, 6.7 KB (added by Dvvarf, 14 years ago)

Replaces wp_insert_category with wp_insert_term/wp_update_term accordingly

  • wp-includes/taxonomy.php

     
    19681968 * @param string $term The term to add or update.
    19691969 * @param string $taxonomy The taxonomy to which to add the term
    19701970 * @param array|string $args Change the values of the inserted term
     1971 * @param bool $wp_error Allow to return WP_Error or not
    19711972 * @return array|WP_Error The Term ID and Term Taxonomy ID
    19721973 */
    1973 function wp_insert_term( $term, $taxonomy, $args = array() ) {
     1974function wp_insert_term( $term, $taxonomy, $args = array(), $wp_error = true ) {
    19741975        global $wpdb;
    19751976
    19761977        if ( ! taxonomy_exists($taxonomy) )
    1977                 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     1978                if($wp_error) {
     1979                        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     1980                } else {
     1981                        return array('term_id' => 0, 'term_taxonomy_id' => 0);
     1982                }
    19781983
    19791984        $term = apply_filters( 'pre_insert_term', $term, $taxonomy );
    19801985                if ( is_wp_error( $term ) )
    19811986                        return $term;
    19821987
    19831988        if ( is_int($term) && 0 == $term )
    1984                 return new WP_Error('invalid_term_id', __('Invalid term ID'));
     1989                if($wp_error) {
     1990                        return new WP_Error('invalid_term_id', __('Invalid term ID'));
     1991                } else {
     1992                        return array('term_id' => 0, 'term_taxonomy_id' => 0);
     1993                }
    19851994
    19861995        if ( '' == trim($term) )
    1987                 return new WP_Error('empty_term_name', __('A name is required for this term'));
     1996                if($wp_error) {
     1997                        return new WP_Error('empty_term_name', __('A name is required for this term'));
     1998                } else {
     1999                        return array('term_id' => 0, 'term_taxonomy_id' => 0);
     2000                }
    19882001
    19892002        $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
    19902003        $args = wp_parse_args($args, $defaults);
     
    20262039                        } else {
    20272040                                $slug = wp_unique_term_slug($slug, (object) $args);
    20282041                                if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    2029                                         return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     2042                                        if($wp_error) {
     2043                                                return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     2044                                        } else {
     2045                                                return array('term_id' => 0, 'term_taxonomy_id' => 0);
     2046                                        }
    20302047                                $term_id = (int) $wpdb->insert_id;
    20312048                        }
    20322049                } elseif ( $existing_term['name'] != $name ) {
    20332050                        // We've got an existing term, with a different name, Create the new term.
    20342051                        $slug = wp_unique_term_slug($slug, (object) $args);
    20352052                        if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    2036                                 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     2053                                if($wp_error) {
     2054                                        return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     2055                                } else {
     2056                                        return array('term_id' => 0, 'term_taxonomy_id' => 0);
     2057                                }
    20372058                        $term_id = (int) $wpdb->insert_id;
    20382059                } elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) )  {
    20392060                        // Same name, same slug.
    2040                         return new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']);
     2061                        if($wp_error) {
     2062                                return new WP_Error('term_exists', __('A term with the name provided already exists.'), $exists['term_id']);
     2063                        } else {
     2064                                return array('term_id' => 0, 'term_taxonomy_id' => 0);
     2065                        }
    20412066                }
    20422067        } else {
    20432068                // This term does not exist at all in the database, Create it.
    20442069                $slug = wp_unique_term_slug($slug, (object) $args);
    20452070                if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) )
    2046                         return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     2071                        if($wp_error) {
     2072                                return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
     2073                        } else {
     2074                                return array('term_id' => 0, 'term_taxonomy_id' => 0);
     2075                        }
    20472076                $term_id = (int) $wpdb->insert_id;
    20482077        }
    20492078
     
    22702299 * @param int $term_id The ID of the term
    22712300 * @param string $taxonomy The context in which to relate the term to the object.
    22722301 * @param array|string $args Overwrite term field values
     2302 * @param bool $wp_error Allow to return WP_Error or not
    22732303 * @return array|WP_Error Returns Term ID and Taxonomy Term ID
    22742304 */
    2275 function wp_update_term( $term_id, $taxonomy, $args = array() ) {
     2305function wp_update_term( $term_id, $taxonomy, $args = array(), $wp_error = true ) {
    22762306        global $wpdb;
    22772307
    22782308        if ( ! taxonomy_exists($taxonomy) )
    2279                 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     2309                if($wp_error) {
     2310                        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     2311                } else {
     2312                        return array('term_id' => 0, 'term_taxonomy_id' => 0);
     2313                }
    22802314
    22812315        $term_id = (int) $term_id;
    22822316
     
    23022336        $description = stripslashes($description);
    23032337
    23042338        if ( '' == trim($name) )
    2305                 return new WP_Error('empty_term_name', __('A name is required for this term'));
     2339                if($wp_error) {
     2340                        return new WP_Error('empty_term_name', __('A name is required for this term'));
     2341                } else {
     2342                        return array('term_id' => 0, 'term_taxonomy_id' => 0);;
     2343                }
    23062344
    23072345        $empty_slug = false;
    23082346        if ( empty($slug) ) {
     
    23352373                if ( $empty_slug || ( $parent != $term['parent']) )
    23362374                        $slug = wp_unique_term_slug($slug, (object) $args);
    23372375                else
    2338                         return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
     2376                        if($wp_error) {
     2377                                return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
     2378                        } else {
     2379                                return array('term_id' => 0, 'term_taxonomy_id' => 0);
     2380                        }
    23392381        }
    23402382        do_action( 'edit_terms', $term_id );
    23412383        $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
  • wp-admin/includes/taxonomy.php

     
    5151        if ( $id = category_exists($cat_name, $parent) )
    5252                return $id;
    5353
    54         return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
     54        $cat = wp_insert_term( $cat_name, 'category', array('name' => $name, 'parent' => $parent), false );
     55        return $cat['term_id'];
    5556}
    5657
    5758/**
     
    162163
    163164        // Merge old and new fields with new fields overwriting old ones.
    164165        $catarr = array_merge($category, $catarr);
     166       
     167        $cat = wp_update_term( $catarr['cat_ID'], 'category', array('name' => $catarr['cat_name'], 'slug' => $catarr['category_nicename'], 'parent' => $catarr['category_parent'], 'description' => $catarr['category_description']), false);
    165168
    166         return wp_insert_category($catarr);
     169        return $cat['term_id'];
    167170}
    168171
    169172//