public function update_item($request) { $item = $this->prepare_item_for_database($request); if (is_array($item) && count($item) > 0) { foreach ($item['data'] as $key => $value) { if (isset($item['fields'][$key]['key'])) { $field = $item['fields'][$key]; $type = $field['type']; if ($type == "true_false") { switch ($value) { case "true": $value = 1; break; case "false": $value = 0; break; } } if (function_exists('acf_update_value')) { acf_update_value($value, $item['id'], $field); } elseif (function_exists('update_field')) { update_field($field['key'], $value, $item['id']); } else { do_action('acf/update_value', $value, $item['id'], $field); } } } return new WP_REST_Response($this->get_fields($request), 200); } return new WP_Error('cant_update_item', __("Cannot update item", 'acf-to-rest-api'), array('status' => 500)); }
function save_post($post_id = 0) { // save $_POST data foreach ($_POST['acf'] as $k => $v) { // get field $field = acf_get_field($k); // update field if ($field) { acf_update_value($v, $post_id, $field); } } }
function update_value($value, $post_id, $field) { // remove acfcloneindex if (isset($value['acfcloneindex'])) { unset($value['acfcloneindex']); } // vars $order = array(); $layouts = array(); // populate $layouts foreach ($field['layouts'] as $layout) { $layouts[$layout['name']] = $layout['sub_fields']; } // update sub fields if (!empty($value)) { // $i $i = -1; // loop through rows foreach ($value as $row) { // $i $i++; // get layout $l = $row['acf_fc_layout']; // append to order $order[] = $l; // loop through sub fields if (!empty($layouts[$l])) { foreach ($layouts[$l] as $sub_field) { // value $v = false; // key (backend) if (isset($row[$sub_field['key']])) { $v = $row[$sub_field['key']]; } elseif (isset($row[$sub_field['name']])) { $v = $row[$sub_field['name']]; } else { // input is not set (hidden by conditioanl logic) continue; } // modify name for save $sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}"; // update field acf_update_value($v, $post_id, $sub_field); } // foreach } // if } // foreach } // if // remove old data $old_order = acf_get_metadata($post_id, $field['name']); $old_count = empty($old_order) ? 0 : count($old_order); $new_count = empty($order) ? 0 : count($order); if ($old_count > $new_count) { for ($i = $new_count; $i < $old_count; $i++) { // get layout $l = $old_order[$i]; // loop through sub fields if (!empty($layouts[$l])) { foreach ($layouts[$l] as $sub_field) { // modify name for delete $sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}"; // delete value acf_delete_value($post_id, $sub_field); } } } } // save false for empty value if (empty($order)) { $order = false; } // return return $order; }
function update_value($value, $post_id, $field) { // remove acfcloneindex if (isset($value['acfcloneindex'])) { unset($value['acfcloneindex']); } // vars $total = 0; // update sub fields if (!empty($value)) { // $i $i = -1; // loop through rows foreach ($value as $row) { // $i $i++; // increase total $total++; // loop through sub fields if (!empty($field['sub_fields'])) { foreach ($field['sub_fields'] as $sub_field) { // value $v = false; // key (backend) if (isset($row[$sub_field['key']])) { $v = $row[$sub_field['key']]; } elseif (isset($row[$sub_field['name']])) { $v = $row[$sub_field['name']]; } else { // input is not set (hidden by conditioanl logic) continue; } // modify name for save $sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}"; // update value acf_update_value($v, $post_id, $sub_field); } // foreach } // if } // foreach } // if // get old value (db only) $old_total = intval(acf_get_value($post_id, $field, true)); if ($old_total > $total) { for ($i = $total; $i < $old_total; $i++) { foreach ($field['sub_fields'] as $sub_field) { // modify name for delete $sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}"; // delete value acf_delete_value($post_id, $sub_field); } // foreach } // for } // if // update $value and return to allow for the normal save function to run $value = $total; // return return $value; }
function updateUserStatus($statusId) { acf_update_value(); }
function psp_update_sub_field($selector, $value, $post_id = false) { // filter post_id // $post_id = acf_get_valid_post_id( $post_id ); // vars $field = false; $name = ''; // within a have_rows loop if (is_string($selector)) { // loop over global data if (!empty($GLOBALS['acf_field'])) { foreach ($GLOBALS['acf_field'] as $row) { // add to name $name .= "{$row['name']}_{$row['i']}_"; // override $post_id $post_id = $row['post_id']; } } // get sub field $field = get_sub_field_object($selector); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => '')); } // append name $name .= $field['name']; // update name $field['name'] = $name; } elseif (is_array($selector)) { // validate if (count($selector) < 3) { return false; } // vars $parent_name = acf_extract_var($selector, 0); // load parent $field = get_field_object($parent_name, $post_id, false, false); // add to name $name .= "{$field['name']}"; // sub fields foreach ($selector as $s) { if (is_numeric($s)) { $row_i = intval($s) - 1; // add to name $name .= "_{$row_i}"; } else { // update parent $field = acf_get_sub_field($s, $field); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $s, 'key' => '', 'type' => '')); } // add to name $name .= "_{$field['name']}"; } // if } // foreach // update name $field['name'] = $name; } // save return acf_update_value($value, $post_id, $field); }
function update_value($value, $post_id, $field) { // bail early if no value if (!acf_is_array($value) || !acf_is_array($field['sub_fields'])) { return null; } // modify $field = $this->prepare_field_for_save($field); // loop foreach (array_keys($field['sub_fields']) as $i) { // vars $sub_field = $field['sub_fields'][$i]; $v = false; // key (backend) if (isset($value[$sub_field['key']])) { $v = $value[$sub_field['key']]; // name (frontend) } elseif (isset($value[$sub_field['_name']])) { $v = $value[$sub_field['_name']]; // empty } else { // input is not set (hidden by conditioanl logic) continue; } // update value acf_update_value($v, $post_id, $sub_field); } // return return ''; }
function update_sub_field($selector, $value, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // vars $field = false; // within a have_rows loop if (is_string($selector)) { // get current row $row = acf_get_row(); // override $post_id $post_id = $row['post_id']; // get sub field $field = get_sub_field_object($selector, false, false); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => '')); } // update name $field['name'] = "{$row['name']}_{$row['i']}_{$field['name']}"; } elseif (is_array($selector)) { // validate if (count($selector) < 3) { return false; } // vars $parent_name = acf_extract_var($selector, 0); // load parent $field = acf_maybe_get_field($parent_name, $post_id); // add to name $name = "{$field['name']}"; // sub fields foreach ($selector as $s) { if (is_numeric($s)) { $row_i = intval($s) - 1; // add to name $name .= "_{$row_i}"; } else { // update parent $field = acf_get_sub_field($s, $field); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $s, 'key' => '', 'type' => '')); } // add to name $name .= "_{$field['name']}"; } // if } // foreach // update name $field['name'] = $name; } // delete if ($value === null) { return acf_delete_value($post_id, $field); } // update return acf_update_value($value, $post_id, $field); }
function save_post($post_id) { // bail early if empty if (empty($_POST['acf'])) { return; } // save $_POST data foreach ($_POST['acf'] as $k => $v) { // get field $field = acf_get_field($k); // continue if no field if (!$field) { continue; } // update acf_update_value($v, $post_id, $field); } }
function update_value($value, $post_id, $field) { if (!empty($value)) { foreach ($value as $field_key => $field_value) { foreach ($field['sub_fields'] as $sub_field) { if ($field_key == $sub_field['key']) { // update field $sub_field_name = $sub_field['name']; $sub_field['name'] = "{$field['name']}_{$sub_field_name}"; acf_update_value($field_value, $post_id, $sub_field); break; } } } } return null; }
function add_row($selector, $value, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // get field $field = acf_maybe_get_field($selector, $post_id); // bail early if no field if (!$field) { return false; } // get row count $i = (int) acf_get_metadata($post_id, $field['name']); // if no rows, save this field via update_field() so that the reference field is created if (!$i) { // acf_update_value will return boolean, simply convert this to int for 1 | 0 (the number of rows!) return (int) acf_update_value(array($value), $post_id, $field); } // increase $i $i++; // update meta $result = acf_update_metadata($post_id, $field['name'], $i); // update sub fields if ($value) { foreach ($value as $k => $v) { update_sub_field(array($field['key'], $i, $k), $v, $post_id); } } // return return $i; }
function acf_save_post($post_id = 0) { // loop through and save if (!empty($_POST['acf'])) { // loop through and save $_POST data foreach ($_POST['acf'] as $key => $value) { // get field $field = acf_get_field($key); // validate field if (!$field) { continue; } // update field acf_update_value($value, $post_id, $field); } // foreach($fields as $key => $value) } // if($fields) // hook for 3rd party customization do_action('acf/save_post', $post_id); return $post_id; }
function delete_sub_row($selector, $i = 1, $post_id = false) { // vars $sub_field = false; $i--; // filter post_id $post_id = acf_get_valid_post_id($post_id); // get sub field if (is_array($selector)) { $sub_field = acf_maybe_get_sub_field($selector, $post_id, false); } else { $sub_field = get_row_sub_field($selector); } // bail early if no sub field if (!$sub_field) { return false; } // get raw value $value = acf_get_value($post_id, $sub_field); // ensure array $value = acf_get_array($value); // append unset($value[$i]); // update return acf_update_value($value, $post_id, $sub_field); }
protected function update_acf_fields() { if (!$this->post_id) { return; } foreach ($this->acf_fields as $data) { if (!isset($data['value'])) { continue; } $field = $data['field']; $value = $data['value']; if (function_exists('acf_update_value')) { acf_update_value($value, $this->post_id, $field); } elseif (function_exists('update_field')) { update_field($field['key'], $value, $this->post_id); } else { do_action('acf/update_value', $value, $this->post_id, $field); } } }
public function update_value($value, $post_id, $field) { global $wp_widget_factory; $widget = isset($field['value']) && isset($field['value']['the_widget']) ? $field['value']['the_widget'] : $field['widget']; // This is a chance for plugins to replace missing widgets $the_widget = !empty($wp_widget_factory->widgets[$widget]) ? $wp_widget_factory->widgets[$widget] : false; if (empty($the_widget)) { return; } if (!empty($value)) { $value['widget_id_base'] = $the_widget->id_base; $value['number'] = isset($value['number']) ? $value['number'] : uniqid(); $value['widget_id'] = $value['widget_id_base'] . '-' . $value['number']; $value['instance'] = array(); $value['the_widget'] = $widget; if (isset($value['widget_fields']) && class_exists($widget) && method_exists($widget, 'update')) { $the_widget = new $widget(); $value['instance'] = $the_widget->update($value['widget_fields'], $value['widget_fields']); } unset($value['widget_fields']); // update sub fields if (!$field['sub_fields']) { return $value; } foreach ($field['sub_fields'] as $sub_field) { $v = false; if (isset($value[$sub_field['key']])) { $v = $value[$sub_field['key']]; } elseif (isset($value[$sub_field['name']])) { $v = $value[$sub_field['name']]; } else { // input is not set (hidden by conditioanl logic) continue; } //Save ACF Widget Fields using a mock widget ID acf_update_value($v, 'widget_' . $value['widget_id'], $sub_field); // modify name for save $sub_field['name'] = "{$field['name']}_{$sub_field['name']}"; // update value acf_update_value($v, $post_id, $sub_field); } } return $value; }
function delete_row($selector, $row = 1, $post_id = false) { // filter post_id $post_id = acf_get_valid_post_id($post_id); // get field $field = acf_maybe_get_field($selector, $post_id); // bail early if no field if (!$field) { return false; } // get value $rows = acf_get_value($post_id, $field); // bail early if no value if (empty($rows)) { return false; } // vars $i = $row - 1; // bail early if row doesn't exist if (empty($rows[$i])) { return false; } // unset unset($rows[$i]); // reindex $rows = array_values($rows); // update acf_update_value($rows, $post_id, $field); // return return true; }