public function getField($fieldId, $format = true) { if (!function_exists('acf_is_field_key')) { throw new \Exception('Advanced Custom Fields must be installed to use ' . __METHOD__); } $value = maybe_unserialize($this->getMeta($fieldId)); $fieldId = $this->getMeta('_' . $fieldId); if (!acf_is_field_key($fieldId)) { return null; } $field = get_field_object($fieldId, $this->ID, false, false); $value = apply_filters("acf/load_value", $value, $this->ID, $field); $value = apply_filters("acf/load_value/type={$field['type']}", $value, $this->ID, $field); $value = apply_filters("acf/load_value/name={$field['name']}", $value, $this->ID, $field); $value = apply_filters("acf/load_value/key={$field['key']}", $value, $this->ID, $field); if ($format) { $value = acf_format_value($value, $this->ID, $field); } return $value; }
function update_field($field) { // bail ealry if not relevant if (!$field['parent'] || !acf_is_field_key($field['parent'])) { return $field; } // vars $ref = 0; // create reference if (empty($this->ref)) { $this->ref = array(); } if (isset($this->ref[$field['parent']])) { $ref = $this->ref[$field['parent']]; } else { // get parent without caching (important not to cache as parent $field will now contain new sub fields) $parent = acf_get_field($field['parent'], true); // bail ealry if no parent if (!$parent) { return $field; } // get ref $ref = $parent['ID'] ? $parent['ID'] : $parent['key']; // update ref $this->ref[$field['parent']] = $ref; } // update field's parent $field['parent'] = $ref; // return return $field; }
function acf_get_field_ancestors($field) { // get field $ancestors = array(); // loop while ($field && acf_is_field_key($field['parent'])) { $ancestors[] = $field['parent']; $field = acf_get_field($field['parent']); } // return return $ancestors; }
function wp_restore_post_revision($post_id, $revision_id) { // global global $wpdb; // get field objects $custom_fields = get_post_custom($revision_id); // populate vars if (!empty($custom_fields)) { foreach ($custom_fields as $k => $v) { // value is always an array $v = $v[0]; // bail early if $value is not is a field_key if (!acf_is_field_key($v)) { continue; } // remove prefix '_' field from reference $field_name = substr($k, 1); // bail early if value could not be found if (!isset($custom_fields[$field_name][0])) { continue; } update_post_meta($post_id, $field_name, $custom_fields[$field_name][0]); } } }
function acf_copy_postmeta($from_post_id, $to_post_id) { // get all postmeta $meta = get_post_meta($from_post_id); // bail early if no meta if (!$meta) { return; } // loop foreach ($meta as $name => $value) { // attempt to find key value $key = acf_maybe_get($meta, '_' . $name); // bail ealry if no key if (!$key) { continue; } // update vars $value = $value[0]; $key = $key[0]; // bail early if $key is a not a field_key if (!acf_is_field_key($key)) { continue; } // get_post_meta will return array before running maybe_unserialize $value = maybe_unserialize($value); // add in slashes // - update_post_meta will unslash the value, so we must first slash it to avoid losing backslashes // - https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping if (is_string($value)) { $value = wp_slash($value); } // update value acf_update_metadata($to_post_id, $name, $value); acf_update_metadata($to_post_id, $name, $key, true); } }
function get_clone_setting_choice($selector = '') { // bail early no selector if (!$selector) { return ''; } // ajax_fields if (isset($_POST['fields'][$selector])) { return $this->get_clone_setting_field_choice($_POST['fields'][$selector]); } // field if (acf_is_field_key($selector)) { return $this->get_clone_setting_field_choice(acf_get_field($selector)); } // group if (acf_is_field_group_key($selector)) { return $this->get_clone_setting_group_choice(acf_get_field_group($selector)); } // return return $selector; }
function is_protected_meta($protected, $meta_key, $meta_type) { // if acf_get_field_reference returns a valid key, this is an acf value, so protect it! if (!$protected) { $reference = acf_get_field_reference($meta_key, $this->post_id); if (acf_is_field_key($reference)) { $protected = true; } } // return return $protected; }
function acf_get_field($selector = null) { // vars $field = false; $k = 'ID'; $v = 0; // $post_id or $key if (is_numeric($selector)) { $v = $selector; } elseif (is_string($selector)) { if (acf_is_field_key($selector)) { $k = 'key'; } else { $k = 'name'; } $v = $selector; } elseif (is_object($selector)) { $v = $selector->ID; } else { return false; } // get cache key $cache_key = "load_field/{$k}={$v}"; // get cache $found = false; $cache = wp_cache_get($cache_key, 'acf', false, $found); if ($found) { return $cache; } // get field group from ID or key if ($k == 'ID') { $field = _acf_get_field_by_id($v); } elseif ($k == 'name') { $field = _acf_get_field_by_name($v); } else { $field = _acf_get_field_by_key($v); } // filter for 3rd party customization $field = apply_filters('acf/load_field', $field); // If a field has been found, apply filters if ($field) { $field = apply_filters("acf/load_field/type={$field['type']}", $field); $field = apply_filters("acf/load_field/name={$field['name']}", $field); $field = apply_filters("acf/load_field/key={$field['key']}", $field); } // set cache wp_cache_set($cache_key, $field, 'acf'); // return return $field; }
function get_field_object($selector, $post_id = false, $format_value = true, $load_value = true) { // complete loading acf()->complete(); // compatibilty if (is_array($format_value)) { extract($format_value); } // vars $field_name = false; // get valid post_id $post_id = acf_get_valid_post_id($post_id); // load field reference if not a field_key if (!acf_is_field_key($selector)) { // save selector as field_name (could be sub field name) $field_name = $selector; // get reference $selector = acf_get_field_reference($selector, $post_id); // bail early if no reference for this field if (!$selector) { return false; } } // get field key $field = acf_get_field($selector); // bail early if no field found if (!$field) { return false; } // Override name - allows the $selector to be a sub field (images_0_image) if ($field_name) { $field['name'] = $field_name; } // load value if ($load_value) { $field['value'] = acf_get_value($post_id, $field); } // format value if ($format_value) { // get value for field $field['value'] = acf_format_value($field['value'], $post_id, $field); } // return return $field; }
function acf_maybe_get_field($selector, $post_id = false, $strict = true) { // complete loading // this function may be used in a theme file before the init action has been run acf()->complete(); // vars $field_name = false; // get valid post_id $post_id = acf_get_valid_post_id($post_id); // load field reference if not a field_key if (!acf_is_field_key($selector)) { // save selector as field_name (could be sub field name) $field_name = $selector; // get reference $field_key = acf_get_field_reference($selector, $post_id); if ($field_key) { $selector = $field_key; } elseif ($strict) { return false; } } // get field key $field = acf_get_field($selector); // bail early if no field if (!$field) { return false; } // Override name - allows the $selector to be a sub field (images_0_image) if ($field_name) { $field['name'] = $field_name; } // return return $field; }
function acf_update_field($field = false, $specific = false) { // $field must be an array if (!is_array($field)) { return false; } // validate $field = acf_get_valid_field($field); // may have been posted. Remove slashes $field = wp_unslash($field); // clean up conditional logic keys if (!empty($field['conditional_logic'])) { // extract groups $groups = acf_extract_var($field, 'conditional_logic'); // clean array $groups = array_filter($groups); $groups = array_values($groups); // clean rules foreach (array_keys($groups) as $i) { $groups[$i] = array_filter($groups[$i]); $groups[$i] = array_values($groups[$i]); } // reset conditional logic $field['conditional_logic'] = $groups; } // find correct parent if (acf_is_field_key($field['parent'])) { // get parent $parent = acf_get_field($field['parent']); // update to ID $field['parent'] = acf_maybe_get($parent, 'ID', 0); } // filter for 3rd party customization $field = apply_filters("acf/update_field", $field); $field = apply_filters("acf/update_field/type={$field['type']}", $field); $field = apply_filters("acf/update_field/name={$field['name']}", $field); $field = apply_filters("acf/update_field/key={$field['key']}", $field); // store origional field for return $data = $field; // extract some args $extract = acf_extract_vars($data, array('ID', 'key', 'label', 'name', 'prefix', 'value', 'menu_order', 'id', 'class', 'parent', '_name', '_input', '_valid')); // serialize for DB $data = maybe_serialize($data); // save $save = array('ID' => $extract['ID'], 'post_status' => 'publish', 'post_type' => 'acf-field', 'post_title' => $extract['label'], 'post_name' => $extract['key'], 'post_excerpt' => $extract['name'], 'post_content' => $data, 'post_parent' => $extract['parent'], 'menu_order' => $extract['menu_order']); // $specific if (!empty($specific)) { // prepend ID array_unshift($specific, 'ID'); // vars $_save = $save; // reset $save = array(); // appen data foreach ($specific as $key) { $save[$key] = $_save[$key]; } } // allow fields to contain the same name add_filter('wp_unique_post_slug', 'acf_update_field_wp_unique_post_slug', 100, 6); // update the field and update the ID if ($field['ID']) { wp_update_post($save); } else { $field['ID'] = wp_insert_post($save); } // clear cache wp_cache_delete("get_field/ID={$field['ID']}", 'acf'); wp_cache_delete("get_field/key={$field['key']}", 'acf'); wp_cache_delete("get_fields/parent={$field['parent']}", 'acf'); // return return $field; }
function get_field_object($selector, $post_id = false, $format_value = true, $load_value = true) { // complete loading acf()->complete(); // compatibilty if (is_array($format_value)) { $format_value = acf_parse_args($format_value, array('format_value' => true, 'load_value' => true)); extract($format_value); } // vars $override_name = false; // filter post_id $post_id = acf_get_valid_post_id($post_id); // load field reference if not a field_key if (!acf_is_field_key($selector)) { $override_name = $selector; $reference = acf_get_field_reference($selector, $post_id); if ($reference) { $selector = $reference; } } // get field key $field = acf_get_field($selector); // bail early if no field found if (!$field) { return false; } // override name? // This allows the $selector to be a sub field (images_0_image) if ($override_name) { $field['name'] = $override_name; } // load value if ($load_value) { $field['value'] = acf_get_value($post_id, $field); } // format value if ($format_value) { // get value for field $field['value'] = acf_format_value($field['value'], $post_id, $field); } // return return $field; }
function update_field($field) { // don't use acf_get_field. Instead, keep a global record of ID from each update_field and use this to get the parent ID => key if ($field['parent']) { if (acf_is_field_key($field['parent'])) { $parent = acf_get_field($field['parent']); $field['parent'] = $parent['ID']; } } return $field; }
function wp_post_revision_fields($fields, $post = null) { // validate page if (acf_is_screen('revision') || acf_is_ajax('get-revision-diffs')) { // allow } else { // bail early (most likely saving a post) return $fields; } // vars $append = array(); $order = array(); $post_id = acf_maybe_get($post, 'ID'); // compatibility with WP < 4.5 (test) if (!$post_id) { global $post; $post_id = $post->ID; } // get all postmeta $meta = get_post_meta($post_id); // bail early if no meta if (!$meta) { return $fields; } // loop foreach ($meta as $name => $value) { // attempt to find key value $key = acf_maybe_get($meta, '_' . $name); // bail ealry if no key if (!$key) { continue; } // update vars $value = $value[0]; $key = $key[0]; // bail early if $key is a not a field_key if (!acf_is_field_key($key)) { continue; } // get field $field = acf_get_field($key); $field_title = $field['label'] . ' (' . $name . ')'; $field_order = $field['menu_order']; $ancestors = acf_get_field_ancestors($field); // ancestors if (!empty($ancestors)) { // vars $count = count($ancestors); $oldest = acf_get_field($ancestors[$count - 1]); // update vars $field_title = str_repeat('- ', $count) . $field_title; $field_order = $oldest['menu_order'] . '.1'; } // append $append[$name] = $field_title; $order[$name] = $field_order; // hook into specific revision field filter and return local value add_filter("_wp_post_revision_field_{$name}", array($this, 'wp_post_revision_field'), 10, 4); } // append if (!empty($append)) { // vars $prefix = '_'; // add prefix $append = acf_add_array_key_prefix($append, $prefix); $order = acf_add_array_key_prefix($order, $prefix); // sort by name (orders sub field values correctly) array_multisort($order, $append); // remove prefix $append = acf_remove_array_key_prefix($append, $prefix); // append $fields = $fields + $append; } // return return $fields; }
function acf_get_field($selector = null, $db_only = false) { // vars $field = false; $type = 'ID'; // is $selector an ID if (is_numeric($selector)) { // do nothing // is $selector a string (name|key) } elseif (is_string($selector)) { $type = 'name'; if (acf_is_field_key($selector)) { $type = 'key'; } // is $selector an object } elseif (is_object($selector)) { $selector = $selector->ID; // selector not valid } else { return false; } // get cache key $cache_key = "get_field/{$type}={$selector}"; // get cache if (!$db_only) { $found = false; $cache = wp_cache_get($cache_key, 'acf', false, $found); if ($found) { return $cache; } } // get field group from ID or key if ($type == 'ID') { $field = _acf_get_field_by_id($selector, $db_only); } elseif ($type == 'name') { $field = _acf_get_field_by_name($selector, $db_only); } else { $field = _acf_get_field_by_key($selector, $db_only); } // bail early if db only value (no need to update cache) if ($db_only) { return $field; } // filter for 3rd party customization if ($field) { $field = apply_filters("acf/load_field", $field); $field = apply_filters("acf/load_field/type={$field['type']}", $field); $field = apply_filters("acf/load_field/name={$field['name']}", $field); $field = apply_filters("acf/load_field/key={$field['key']}", $field); } // set cache wp_cache_set($cache_key, $field, 'acf'); // return return $field; }