better handle multi_meta props

This commit is contained in:
Leo Germani 2018-05-02 15:29:42 -03:00
parent 689b67b553
commit be2c91c5db
1 changed files with 18 additions and 7 deletions

View File

@ -163,14 +163,25 @@ abstract class Repository {
update_post_meta( $obj->get_id(), $prop, $this->maybe_add_slashes( $obj->get_mapped_property( $prop ) ) );
} elseif ( $map[ $prop ]['map'] == 'meta_multi' ) {
$values = $obj->get_mapped_property( $prop );
delete_post_meta( $obj->get_id(), $prop );
if ( is_array( $values ) ) {
foreach ( $values as $value ) {
add_post_meta( $obj->get_id(), $prop, $this->maybe_add_slashes( $value ) );
}
$current_values = get_post_meta( $obj->get_id(), $prop );
if (empty($values) || !is_array($values))
$values = [];
if (empty($current_values) || !is_array($current_values))
$current_values = [];
$deleted = array_diff( $current_values, $values );
$added = array_diff( $values, $current_values );
foreach ($deleted as $del) {
delete_post_meta( $obj->get_id(), $prop, $del );
}
foreach ($added as $add) {
add_post_meta( $obj->get_id(), $prop, $this->maybe_add_slashes( $add ) );
}
}
}