Habari\Vocabulary::merge PHP Метод

merge() публичный Метод

If the master term exists, the terms will be merged with it. If not, it will be created first.
public merge ( mixed $master, Array $tags, $object_type = 'post' )
$master mixed The Term to which they should be renamed, or the slug, text or id of it
$tags Array The tag text, slugs or ids to be renamed
    public function merge($master, $tags, $object_type = 'post')
    {
        $type_id = Vocabulary::object_type_id($object_type);
        $post_ids = array();
        $tag_names = array();
        // get the master term
        $master_term = $this->get_term($master);
        if (!isset($master_term->term)) {
            // it didn't exist, so we assume it's tag text and create it
            $master_term = $this->add_term($master);
            if (!$master_term) {
                return;
            }
            $master_ids = array();
        } else {
            // get the posts the tag is already on so we don't duplicate them
            $master_ids = $master_term->objects($object_type);
        }
        // get array of existing tags first to make sure we don't conflict with a new master tag
        foreach ($tags as $tag) {
            // if this is the master tag, there's nothing to do
            if ($tag == $master) {
                continue;
            }
            $term = $this->get_term($tag);
            // get all the post ID's tagged with this tag
            $posts = $term->objects($object_type);
            $ok_to_delete = true;
            // if there actually are posts, let's link those up with the new tag now
            if (count($posts) > 0) {
                // only try and add the master tag to posts it's not already on
                $post_ids = array_diff($posts, $master_ids);
                foreach ($post_ids as $post_id) {
                    $r = $master_term->associate($object_type, $post_id);
                    // if we failed linking this post, we can keep trying others, but don't delete this tag when finished
                    if ($r == false) {
                        $ok_to_delete = false;
                    } else {
                        // otherwise, we did in fact merge a tag - make sure the tag is in the list of ones we merged
                        $tag_names[$tag] = $tag;
                        // and disassociate this post from the existing tag
                        $term->dissociate($object_type, $post_id);
                    }
                }
            }
            // if it's still ok to delete the tag entirely, do so
            if ($ok_to_delete) {
                $this->delete_term($term->id);
            } else {
                // otherwise, log a special message that we didn't delete it
                EventLog::log(_t('Not all posts tagged "%1$s" could be reassigned to "%2$s". They have been left alone.', array($tag, $master)), 'err', 'vocabulary', 'habari');
            }
        }
        EventLog::log(sprintf(_n('Term %1$s in the %2$s vocabulary has been renamed to %3$s.', 'Terms %1$s in the %2$s vocabulary have been renamed to %3$s.', count($tags)), implode(', ', $tag_names), $this->name, $master), 'info', 'vocabulary', 'habari');
    }