public function update($data)
{
global $wpdb;
$data = (array) $data;
$fields = array();
$formats = array();
// Were we given a domain (and is it not the current one)?
if (!empty($data['domain']) && $this->data->domain !== $data['domain']) {
// Does this domain exist already?
$existing = static::get_by_domain($data['domain']);
if (is_wp_error($existing)) {
return $existing;
}
if (!empty($existing) && $existing->get_site_id() !== $data['site']) {
// Domain exists already and points to another site
return new \WP_Error('mercator.mapping.domain_exists');
}
$fields['domain'] = $data['domain'];
$formats[] = '%s';
}
// Were we given an active flag (and is it not current)?
if (isset($data['active']) && $this->is_active() !== (bool) $data['active']) {
$fields['active'] = (bool) $data['active'];
$formats[] = '%d';
}
// Do we have things to update?
if (empty($fields)) {
return false;
}
$where = array('id' => $this->get_id());
$where_format = array('%d');
$result = $wpdb->update($wpdb->dmtable, $fields, $where, $formats, $where_format);
if (empty($result) && !empty($wpdb->last_error)) {
return new \WP_Error('mercator.mapping.update_failed');
}
$old_mapping = clone $this;
// Update internal state
foreach ($fields as $key => $val) {
$this->data->{$key} = $val;
}
// Update the cache
wp_cache_delete('id:' . $this->get_site_id(), 'domain_mapping');
wp_cache_delete('domain:' . $old_mapping->get_domain(), 'domain_mapping');
wp_cache_set('domain:' . $this->get_domain(), $this->data, 'domain_mapping');
/**
* Fires after a mapping has been updated.
*
* @param Mercator\Mapping $mapping The mapping object.
* @param Mercator\Mapping $mapping The previous mapping object.
*/
do_action('mercator.mapping.updated', $this, $old_mapping);
return true;
}