/** * Save a repeatable group */ public static function save_group($args) { if (!isset($args['id'], $args['fields'], $_POST[$args['id']]) || !is_array($args['fields'])) { return; } $field_group = new cmb_Meta_Box_field($args); $base_id = $field_group->id(); $old = $field_group->get_data(); $group_vals = $_POST[$base_id]; $saved = array(); $is_updated = false; $field_group->index = 0; // $group_vals[0]['color'] = '333'; foreach (array_values($field_group->fields()) as $field_args) { $field = new cmb_Meta_Box_field($field_args, $field_group); $sub_id = $field->id(true); foreach ((array) $group_vals as $field_group->index => $post_vals) { // Get value $new_val = isset($group_vals[$field_group->index][$sub_id]) ? $group_vals[$field_group->index][$sub_id] : false; // Sanitize $new_val = self::sanitize_field($field, $new_val, $field_group->index); if ('file' == $field->type() && is_array($new_val)) { // Add image ID to the array stack $saved[$field_group->index][$new_val['field_id']] = $new_val['attach_id']; // Reset var to url string $new_val = $new_val['url']; } // Get old value $old_val = is_array($old) && isset($old[$field_group->index][$sub_id]) ? $old[$field_group->index][$sub_id] : false; $is_updated = !empty($new_val) && $new_val != $old_val; $is_removed = empty($new_val) && !empty($old_val); // Compare values and add to `$updated` array if ($is_updated || $is_removed) { self::$updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id; } // Add to `$saved` array $saved[$field_group->index][$sub_id] = $new_val; } $saved[$field_group->index] = array_filter($saved[$field_group->index]); } $saved = array_filter($saved); $field_group->update_data($saved, true); }
/** * Peforms saving of `file` attachement's ID * @since 1.1.0 * @param string $value File url */ public function _save_file_id($value) { $group = $this->field->group; $args = $this->field->args(); $args['id'] = $args['_id'] . '_id'; unset($args['_id'], $args['_name']); // And get new field object $field = new cmb_Meta_Box_field($args, $group); $id_key = $field->_id(); $id_val_old = $field->escaped_value('absint'); if ($group) { // Check group $_POST data $i = $group->index; $base_id = $group->_id(); $id_val = isset($_POST[$base_id][$i][$id_key]) ? absint($_POST[$base_id][$i][$id_key]) : 0; } else { // Check standard $_POST data $id_val = isset($_POST[$field->id()]) ? $_POST[$field->id()] : null; } // If there is no ID saved yet, try to get it from the url if ($value && !$id_val) { $id_val = cmb_Meta_Box::image_id_from_url($value); } if ($group) { return array('attach_id' => $id_val, 'field_id' => $id_key); } if ($id_val && $id_val != $id_val_old) { return $field->update_data($id_val); } elseif (empty($id_val) && $id_val_old) { return $field->remove_data($old); } }