Make WordPress Core

Ticket #4109: 4109.diff

File 4109.diff, 2.0 KB (added by rob1n, 19 years ago)
  • wp-includes/post.php

     
    784784        return wp_update_post(array('post_status' => 'publish', 'ID' => $post_id, 'no_filter' => true));
    785785}
    786786
    787 function wp_set_post_tags( $post_id = 0, $tags = '' ) {
     787function wp_add_post_tags($post_id = 0, $tags = '') {
     788        return wp_set_post_tags($post_id, $tags, true);
     789}
     790
     791function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
     792        /* $append - true = don't delete existing tags, just add on, false = replace the tags with the new tags */
    788793        global $wpdb;
    789794       
    790795        $post_id = (int) $post_id;
    791796       
    792797        if ( !$post_id )
    793798                return false;
    794 
    795         $tags = explode( ',', $tags );
     799       
     800        // prevent warnings for unintialized variables
     801        $tag_ids = array();
     802       
     803        $tags = (is_array($tags)) ? $tags : explode( ',', $tags );
     804       
    796805        foreach ( $tags as $tag ) {
    797806                $tag = trim( $tag );
    798807                if ( !$tag_slug = sanitize_title( $tag ) )
     
    801810                        $tag_id = wp_create_tag( $tag );
    802811                $tag_ids[] = $tag_id;
    803812        }
    804 
    805         if ( !is_array( $tag_ids ) )
     813       
     814        if ( empty($tag_ids) )
    806815                return false;
    807 
     816       
    808817        $tag_ids = array_unique( $tag_ids );
    809 
     818       
    810819        // First the old tags
    811820        $old_tags = $wpdb->get_col("
    812821                SELECT category_id
    813822                FROM $wpdb->post2cat
    814823                WHERE post_id = '$post_id' AND rel_type = 'tag'");
    815 
     824       
    816825        if ( !$old_tags ) {
    817826                $old_tags = array();
    818827        } else {
    819828                $old_tags = array_unique( $old_tags );
    820829        }
    821 
     830       
    822831        // Delete any?
    823832        $delete_tags = array_diff( $old_tags, $tag_ids);
    824         if ( $delete_tags ) {
     833        if ( $delete_tags && !$append ) {
    825834                foreach ( $delete_tags as $del ) {
    826835                        $wpdb->query("
    827836                                DELETE FROM $wpdb->post2cat
     
    831840                                ");
    832841                }
    833842        }
    834 
     843       
    835844        // Add any?
    836845        $add_tags = array_diff( $tag_ids, $old_tags );
    837846        if ( $add_tags ) {
     
    843852                                        VALUES ('$post_id', '$new_tag', 'tag')");
    844853                }
    845854        }
    846 
     855       
    847856        // Update category counts.
    848857        $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) );
    849858        foreach ( $all_affected_tags as $tag_id ) {