function bb_merge_tags($old_id, $new_id)
{
    if (!bb_current_user_can('manage_tags')) {
        return false;
    }
    $old_id = (int) $old_id;
    $new_id = (int) $new_id;
    if ($old_id == $new_id) {
        return false;
    }
    do_action('bb_pre_merge_tags', $old_id, $new_id);
    // Get all topics tagged with old tag
    $old_topics = bb_get_tagged_topic_ids($old_id);
    // Get all toics tagged with new tag
    $new_topics = bb_get_tagged_topic_ids($new_id);
    // Get intersection of those topics
    $both_topics = array_intersect($old_topics, $new_topics);
    // Discard the intersection from the old tags topics
    $old_topics = array_diff($old_topics, $both_topics);
    // Add the remainder of the old tag topics to the new tag
    if (count($old_topics)) {
        $new_tag = bb_get_tag($new_id);
        foreach ($old_topics as $old_topic) {
            bb_add_topic_tag($old_topic, $new_tag->slug);
        }
    }
    // Destroy the old tag
    $old_tag = bb_destroy_tag($old_id);
    return array('destroyed' => $old_tag, 'old_count' => count($old_topics), 'diff_count' => count($both_topics));
}
function get_tagged_topic_ids($tag_id)
{
    bb_log_deprecated('function', __FUNCTION__, 'bb_get_tagged_topic_ids');
    return bb_get_tagged_topic_ids($tag_id);
}
function bb_recount_tag_delete_empty()
{
    // Get all tags
    if (!isset($terms)) {
        $terms = $wp_taxonomy_object->get_terms('bb_topic_tag', array('hide_empty' => false));
    }
    if (!is_wp_error($terms) && is_array($terms)) {
        $message = __('Deleted tags with no topics');
        foreach ($terms as $term) {
            $topic_ids = bb_get_tagged_topic_ids($term->term_id);
            if (!is_wp_error($topic_ids) && is_array($topic_ids)) {
                if (false === $topic_ids || is_array($topic_ids) && !count($topic_ids)) {
                    bb_destroy_tag($term->term_taxonomy_id);
                }
            }
            unset($topic_ids);
        }
    }
    unset($terms, $term);
    return $message;
}
Beispiel #4
0
 function generate_post_sql($_part_of_topic_query = false)
 {
     global $bbdb;
     $q =& $this->query_vars;
     $distinct = '';
     $sql_calc_found_rows = 'found_rows' === $q['count'] ? 'SQL_CALC_FOUND_ROWS' : '';
     // unfiltered
     $fields = 'p.*';
     $index_hint = '';
     $join = '';
     $where = '';
     $group_by = '';
     $having = '';
     $order_by = '';
     $topic_where = '';
     $topic_queries = array('topic_author_id', 'topic_author', 'topic_status', 'post_count', 'tag_count', 'started', 'updated', 'open', 'sticky', 'meta_key', 'meta_value', 'topic_title');
     if (!$_part_of_topic_query && array_diff($topic_queries, $this->not_set)) {
         $join .= " JOIN {$bbdb->topics} as t ON ( t.topic_id = p.topic_id )";
         $topic_where = $this->generate_topic_sql(true);
     }
     if (!$_part_of_topic_query) {
         if ($q['post_id']) {
             $where .= $this->parse_value('p.post_id', $q['post_id']);
         }
         if ($q['topic_id']) {
             $where .= $this->parse_value('p.topic_id', $q['topic_id']);
         } elseif ($q['topic']) {
             if (!($q['topic_id'] = bb_get_id_from_slug('topic', $q['topic']))) {
                 $this->error('query_var:topic', 'No topic by that name');
             }
             $where .= " AND p.topic_id = " . $q['topic_id'];
         }
         if ($q['forum_id']) {
             $where .= $this->parse_value('p.forum_id', $q['forum_id']);
         } elseif ($q['forum']) {
             if (!($q['forum_id'] = bb_get_id_from_slug('forum', $q['forum']))) {
                 $this->error('query_var:forum', 'No forum by that name');
             }
             $where .= " AND p.forum_id = " . $q['forum_id'];
         }
         if ($q['tag'] && !is_int($q['tag_id'])) {
             $q['tag_id'] = (int) bb_get_tag_id($q['tag']);
         }
         if (is_numeric($q['tag_id'])) {
             if ($tagged_topic_ids = bb_get_tagged_topic_ids($q['tag_id'])) {
                 $where .= " AND p.topic_id IN (" . join(',', $tagged_topic_ids) . ")";
             } else {
                 $where .= " AND 0 /* No such tag */";
             }
         }
         if (is_numeric($q['favorites']) && ($f_user = bb_get_user($q['favorites']))) {
             $where .= $this->parse_value('p.topic_id', $f_user->favorites);
         }
     }
     // !_part_of_topic_query
     if ($q['post_text']) {
         $where .= ' AND ' . $this->generate_post_text_sql($q['post_text']);
         if ($this->match_query) {
             $fields .= ", {$this->match_query} AS search_score";
             if (!$q['order_by']) {
                 $q['order_by'] = 'search_score';
             }
         } else {
             $fields .= ', 0 AS search_score';
         }
     }
     if ($q['posted']) {
         $where .= $this->date('p.post_time', $q['posted']);
     }
     if ($q['post_author_id']) {
         $where .= $this->parse_value('p.poster_id', $q['post_author_id']);
     } elseif ($q['post_author']) {
         $user = bb_get_user($q['post_author'], array('by' => 'login'));
         if (!($q['post_author_id'] = (int) $user->ID)) {
             $this->error('query_var:user', 'No user by that name');
         }
         $where .= " AND p.poster_id = {$q['post_author_id']}";
     }
     if (!$q['post_status']) {
         $where .= " AND p.post_status = '0'";
     } elseif (false === strpos($q['post_status'], 'all')) {
         $stati = array('normal' => 0, 'deleted' => 1);
         $q['post_status'] = str_replace(array_keys($stati), array_values($stati), $q['post_status']);
         $where .= $this->parse_value('p.post_status', $q['post_status']);
     }
     if (false !== $q['position']) {
         $where .= $this->parse_value('p.post_position', $q['position']);
     }
     if (false !== $q['poster_ip']) {
         $where .= " AND poster_ip = '" . $q['poster_ip'] . "'";
     }
     // Just getting post part for inclusion in topic query
     if ($_part_of_topic_query) {
         return $where;
     }
     $where .= $topic_where;
     if ($where) {
         // Get rid of initial " AND " (this is pre-filters)
         $where = substr($where, 5);
     }
     if ($q['index_hint']) {
         $index_hint = $q['index_hint'];
     }
     if ($q['order_by']) {
         $order_by = $q['order_by'];
     } else {
         $order_by = 'p.post_time';
     }
     $bits = compact(array('distinct', 'sql_calc_found_rows', 'fields', 'index_hint', 'join', 'where', 'group_by', 'having', 'order_by'));
     $this->request = $this->_filter_sql($bits, "{$bbdb->posts} AS p");
     return $this->request;
 }
         }
         if (count($_terms)) {
             $wp_taxonomy_object->update_term_count($_terms, 'bb_topic_tag');
         }
     }
     unset($term, $_terms);
 }
 if (isset($_POST['tags-delete-empty']) && 1 == $_POST['tags-delete-empty']) {
     // Get all tags
     if (!isset($terms)) {
         $terms = $wp_taxonomy_object->get_terms('bb_topic_tag', array('hide_empty' => false));
     }
     if (!is_wp_error($terms) && is_array($terms)) {
         $messages[] = __('Deleted tags with no topics');
         foreach ($terms as $term) {
             $topic_ids = bb_get_tagged_topic_ids($term->term_id);
             if (!is_wp_error($topic_ids) && is_array($topic_ids)) {
                 if (false === $topic_ids || is_array($topic_ids) && !count($topic_ids)) {
                     bb_destroy_tag($term->term_taxonomy_id);
                 }
             }
             unset($topic_ids);
         }
     }
     unset($terms, $term);
 }
 if (isset($_POST['clean-favorites']) && 1 == $_POST['clean-favorites']) {
     $favorites_key = $bbdb->prefix . 'favorites';
     if ($users = $bbdb->get_results("SELECT user_id AS id, meta_value AS favorites FROM {$bbdb->usermeta} WHERE meta_key = '" . $favorites_key . "'")) {
         $messages[] = __('Removed deleted topics from users\' favorites');
         $topics = $bbdb->get_col("SELECT topic_id FROM {$bbdb->topics} WHERE topic_status = '0'");