function SetMetaValues($postId)
{
global $wpdb;
$customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
//delete file
if (!empty($_POST['magicfields_remove_files'])) {
$files = preg_split('/\\|\\|\\|/', $_POST['magicfields_remove_files']);
foreach ($files as $file) {
do_action('mf_before_delete_file', $file);
@unlink(MF_FILES_PATH . $file);
}
}
if (empty($_POST['magicfields'])) {
return true;
}
$customfields = $_POST['magicfields'];
if ($the_post = wp_is_post_revision($postId)) {
$postId = $the_post;
}
if (!empty($customWritePanelId)) {
// --- Delete old values
foreach ($customfields as $name => $field) {
delete_post_meta($postId, $name);
}
$wpdb->delete(MF_TABLE_POST_META, array('post_id' => $postId), array('%d'));
//Creating the new values
//Iterating the custom fields
foreach ($customfields as $name => $groups) {
$groups_index = 1;
//Iterating the groups
foreach ($groups as $group_id => $fields) {
$index = 1;
//Iterating the duplicates
foreach ($fields as $value) {
// Adding field value meta data
add_post_meta($postId, $name, $value);
$fieldMetaID = $wpdb->insert_id;
$wpdb->insert(MF_TABLE_POST_META, array('id' => $fieldMetaID, 'field_name' => $name, 'group_count' => $groups_index, 'field_count' => $index, 'post_id' => $postId, 'order_id' => $groups_index), array('%d', '%s', '%d', '%d', '%d', '%d'));
//pre save value
do_action('mf_presave', $fieldMetaID, $name, $groups_index, $index, $postId, $value, $customWritePanelId);
$index++;
}
$groups_index++;
}
}
if (MF_GET_CACHE_IS_ON) {
purge_cache_dir(MF_GET_CACHE_DIR . $postId);
}
}
}