public function set_object_terms($object_type, $id, $terms = array())
{
// Make sure we have an array
$terms = new Terms($terms);
$new_terms = array();
// Make sure we have terms and they're in the database.
// Key the terms to their id while we're at it.
foreach ($terms as $term) {
$new_term = $this->get_term($term);
if (!$new_term instanceof Term) {
$new_term = $this->add_term($term);
}
if ($new_term != false && !array_key_exists($new_term->id, $new_terms)) {
$new_terms[$new_term->id] = $new_term;
}
}
// Get the current terms
$old_terms = $this->get_object_terms($object_type, $id);
$keys = array_keys($new_terms);
foreach ($old_terms as $term) {
// If the old term isn't in the new terms, dissociate it from the object
if (!in_array($term->id, $keys)) {
$term->dissociate($object_type, $id);
}
}
// Associate the new terms
foreach ($new_terms as $term) {
$term->associate($object_type, $id);
}
return true;
}