Fieldmanager_Field::save_index PHP Method

save_index() protected method

Is called multiple times for multi-fields (e.g. limit => 0)
protected save_index ( array $values, $current_values ) : void
$values array
return void
    protected function save_index($values, $current_values)
    {
        if ($this->data_type != 'post' || empty($this->data_id)) {
            return;
        }
        // Must delete current values specifically, then add new ones, to support a scenario where the
        // same field in repeating groups with limit = 1 is going to create more than one entry here, and
        // if we called update_post_meta() we would overwrite the index with each new group.
        if (!empty($current_values) && is_array($current_values)) {
            foreach ($current_values as $old_value) {
                if (!is_array($old_value)) {
                    $old_value = array($old_value);
                }
                foreach ($old_value as $value) {
                    $value = $this->process_index_value($value);
                    if (empty($value)) {
                        $value = 0;
                    }
                    // false or null should be saved as 0 to prevent duplicates
                    delete_post_meta($this->data_id, $this->index, $value);
                }
            }
        }
        // add new values
        if (!empty($values) && is_array($values)) {
            foreach ($values as $new_value) {
                if (!is_array($new_value)) {
                    $new_value = array($new_value);
                }
                foreach ($new_value as $value) {
                    $value = $this->process_index_value($value);
                    if (isset($value)) {
                        if (empty($value)) {
                            $value = 0;
                        }
                        // false or null should be saved as 0 to prevent duplicates
                        add_post_meta($this->data_id, $this->index, $value);
                    }
                }
            }
        }
    }