本文整理汇总了PHP中acf_update_field函数的典型用法代码示例。如果您正苦于以下问题:PHP acf_update_field函数的具体用法?PHP acf_update_field怎么用?PHP acf_update_field使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了acf_update_field函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: acf_sync_import_json_field_groups
function acf_sync_import_json_field_groups($directory)
{
$dir = new DirectoryIterator($directory);
foreach ($dir as $file) {
if (!$file->isDot() && $file->getExtension() == 'json') {
$json = json_decode(file_get_contents($file->getPathname()), true);
// What follows is basically a copy of import() in ACF admin/settings-export.php
// if importing an auto-json, wrap field group in array
if (isset($json['key'])) {
$json = array($json);
}
// vars
$added = array();
$ignored = array();
$ref = array();
$order = array();
foreach ($json as $field_group) {
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
}
}
}
}
}
示例2: duplicate_field
function duplicate_field($field)
{
// get sub fields
$sub_fields = acf_extract_var($field, 'sub_fields');
// save field to get ID
$field = acf_update_field($field);
// duplicate sub fields
acf_duplicate_fields($sub_fields, $field['ID']);
// return
return $field;
}
示例3: acf_import_field_group
function acf_import_field_group($field_group)
{
// vars
$ref = array();
$order = array();
// extract fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// remove old fields
if ($field_group['ID']) {
$db_fields = acf_get_fields_by_id($field_group['ID']);
$db_fields = acf_prepare_fields_for_import($db_fields);
// get field keys
$keys = array();
foreach ($fields as $field) {
$keys[] = $field['key'];
}
// loop over db fields
foreach ($db_fields as $field) {
// add to ref
$ref[$field['key']] = $field['ID'];
if (!in_array($field['key'], $keys)) {
acf_delete_field($field['ID']);
}
}
}
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add ID
if (!$field['ID'] && isset($ref[$field['key']])) {
$field['ID'] = $ref[$field['key']];
}
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
// add to ref
$ref[$field['key']] = $field['ID'];
}
// return new field group
return $field_group;
}
示例4: update_value
function update_value($value, $post_id, $field)
{
// bail early if is empty
if (empty($value)) {
return $value;
}
// select -> update_value()
$value = acf_get_field_type('select')->update_value($value, $post_id, $field);
// save_other_choice
if ($field['save_custom']) {
// get raw $field (may have been changed via repeater field)
// if field is local, it won't have an ID
$selector = $field['ID'] ? $field['ID'] : $field['key'];
$field = acf_get_field($selector, true);
// bail early if no ID (JSON only)
if (!$field['ID']) {
return $value;
}
// loop
foreach ($value as $v) {
// ignore if already eixsts
if (isset($field['choices'][$v])) {
continue;
}
// append
$field['choices'][$v] = $v;
}
// save
acf_update_field($field);
}
// return
return $value;
}
示例5: _migrate_field_500
function _migrate_field_500($field)
{
// orig
$orig = $field;
// order_no is now menu_order
$field['menu_order'] = acf_extract_var($field, 'order_no');
// correct very old field keys
if (substr($field['key'], 0, 6) !== 'field_') {
$field['key'] = 'field_' . str_replace('field', '', $field['key']);
}
// get valid field
$field = acf_get_valid_field($field);
// save field
$field = acf_update_field($field);
// sub fields
if ($field['type'] == 'repeater') {
// get sub fields
$sub_fields = acf_extract_var($orig, 'sub_fields');
// save sub fields
if (!empty($sub_fields)) {
$keys = array_keys($sub_fields);
foreach ($keys as $key) {
$sub_field = acf_extract_var($sub_fields, $key);
$sub_field['parent'] = $field['ID'];
_migrate_field_500($sub_field);
}
}
} elseif ($field['type'] == 'flexible_content') {
// get layouts
$layouts = acf_extract_var($orig, 'layouts');
// update layouts
$field['layouts'] = array();
// save sub fields
if (!empty($layouts)) {
foreach ($layouts as $layout) {
// vars
$layout_key = uniqid();
// append layotu key
$layout['key'] = $layout_key;
// extract sub fields
$sub_fields = acf_extract_var($layout, 'sub_fields');
// save sub fields
if (!empty($sub_fields)) {
$keys = array_keys($sub_fields);
foreach ($keys as $key) {
$sub_field = acf_extract_var($sub_fields, $key);
$sub_field['parent'] = $field['ID'];
$sub_field['parent_layout'] = $layout_key;
_migrate_field_500($sub_field);
}
// foreach
}
// if
// append layout
$field['layouts'][] = $layout;
}
// foreach
}
// if
// save field again with less sub field data
$field = acf_update_field($field);
}
// return
return $field;
}
示例6: acf_commit_import
/**
* Revert action for ACF Commits
*
* Delete all existing field groups and fields, and import a backup
*/
public function acf_commit_import()
{
$post = get_post($_POST['post_id']);
// decode json
$json = json_decode($post->post_content, true);
// validate json
if (empty($json)) {
return;
}
// get all previous acf posts
$posts = get_posts(array('post_type' => array('acf-field-group', 'acf-field', 'acf')));
// delete al previous acf posts
if (!empty($posts)) {
foreach ($posts as $post) {
wp_delete_post($post);
}
}
// if importing an auto-json, wrap field group in array
if (isset($json['key'])) {
$json = array($json);
}
// vars
$ignored = array();
$ref = array();
$order = array();
foreach ($json as $field_group) {
// check if field group exists
if (acf_get_field_group($field_group['key'])) {
// append to ignored
$ignored[] = $field_group['title'];
continue;
}
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
// add to ref
$ref[$field['key']] = $field['ID'];
}
}
$this->create_commit('[Reverted] ' . get_field('commit_message', $post->ID));
die;
}
示例7: import
function import()
{
// validate
if (empty($_FILES['acf_import_file'])) {
acf_add_admin_notice(__("No file selected", 'acf'), 'error');
return;
}
// vars
$file = $_FILES['acf_import_file'];
// validate error
if ($file['error']) {
acf_add_admin_notice(__('Error uploading file. Please try again', 'acf'), 'error');
return;
}
// validate type
if (pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json') {
acf_add_admin_notice(__('Incorrect file type', 'acf'), 'error');
return;
}
// read file
$json = file_get_contents($file['tmp_name']);
// decode json
$json = json_decode($json, true);
// validate json
if (empty($json)) {
acf_add_admin_notice(__('Import file empty', 'acf'), 'error');
return;
}
// if importing an auto-json, wrap field group in array
if (isset($json['key'])) {
$json = array($json);
}
// vars
$added = array();
$ignored = array();
$ref = array();
$order = array();
foreach ($json as $field_group) {
// check if field group exists
if (acf_get_field_group($field_group['key'], true)) {
// append to ignored
$ignored[] = $field_group['title'];
continue;
}
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
// add to ref
$ref[$field['key']] = $field['ID'];
}
// append to added
$added[] = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>';
}
// messages
if (!empty($added)) {
$message = __('<b>Success</b>. Import tool added %s field groups: %s', 'acf');
$message = sprintf($message, count($added), implode(', ', $added));
acf_add_admin_notice($message);
}
if (!empty($ignored)) {
$message = __('<b>Warning</b>. Import tool detected %s field groups already exist and have been ignored: %s', 'acf');
$message = sprintf($message, count($ignored), implode(', ', $ignored));
acf_add_admin_notice($message, 'error');
}
}
示例8: duplicate_field
function duplicate_field($field)
{
// vars
$sub_fields = array();
if (!empty($field['layouts'])) {
// loop through layouts
foreach ($field['layouts'] as $layout) {
// extract sub fields
$extra = acf_extract_var($layout, 'sub_fields');
// merge
if (!empty($extra)) {
$sub_fields = array_merge($sub_fields, $extra);
}
}
// foreach
}
// if
// save field to get ID
$field = acf_update_field($field);
// duplicate sub fields
acf_duplicate_fields($sub_fields, $field['ID']);
// return
return $field;
}
示例9: update_value
function update_value($value, $post_id, $field)
{
// bail early if no value (allow 0 to be saved)
if (!$value && !is_numeric($value)) {
return $value;
}
// save_other_choice
if ($field['save_other_choice']) {
// value isn't in choices yet
if (!isset($field['choices'][$value])) {
// get raw $field (may have been changed via repeater field)
// if field is local, it won't have an ID
$selector = $field['ID'] ? $field['ID'] : $field['key'];
$field = acf_get_field($selector, true);
// bail early if no ID (JSON only)
if (!$field['ID']) {
return $value;
}
// update $field
$field['choices'][$value] = $value;
// save
acf_update_field($field);
}
}
// return
return $value;
}
示例10: import_json_field_group
private function import_json_field_group($json)
{
// What follows is basically a copy of import() in ACF admin/settings-export.php
// if importing an auto-json, wrap field group in array
if (isset($json['key'])) {
$json = array($json);
}
// vars
$added = array();
$ignored = array();
$ref = array();
$order = array();
foreach ($json as $field_group) {
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
// add to ref
$ref[$field['key']] = $field['ID'];
}
}
}
示例11: acf_pull
/**
* Import (overwrite) field groups into DB
* @param string $params['name']
* @param string $params['group']
* @param string $params['old_value'] The previous settings data
* @param string $params['new_value'] The new settings data
*/
function acf_pull($params)
{
$field_group = $params['new_value'];
if ($existing_group = acf_get_field_group($field_group['key'])) {
$field_group['ID'] = $existing_group['ID'];
$existing_fields = acf_get_fields($existing_group);
// Remove fields
foreach ($existing_fields as $field) {
wp_delete_post($field['ID'], true);
}
}
// extract fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $index => $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} else {
if (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
// add to ref
$ref[$field['key']] = $field['ID'];
}
}
示例12: acf_php_recovery_page
function acf_php_recovery_page()
{
global $wpdb;
$acf_local = acf_local();
// process the form
if (isset($_POST['acf_php_recovery_action']) && $_POST['acf_php_recovery_action'] == 'import' && isset($_POST['fieldsets']) && check_admin_referer('acf_php_recovery')) {
$import_fieldsets = $_POST['fieldsets'];
$imported = array();
// Keep track of the imported
$key_to_post_id = array();
// Group or field key to post id
// Now we can import the groups
foreach ($acf_local->groups as $key => $group) {
$group['title'] = $group['title'] . ' (Recovered)';
// Only import those that were selected
if (in_array($key, $import_fieldsets)) {
$saved_group = acf_update_field_group($group);
$key_to_post_id[$key] = $saved_group['ID'];
// For displaying the success message
$imported[] = array('title' => $group['title'], 'id' => $saved_group['ID']);
}
}
// This requires multipile runs to handle sub-fields that have their parent set to the parent field instead of the group
$field_parents = $import_fieldsets;
// The groups and fields
$imported_fields = array();
// Keep track of the already imported
do {
$num_import = 0;
foreach ($acf_local->fields as $key => $field) {
if (!in_array($key, $imported_fields) && in_array($field['parent'], $field_parents)) {
$num_import = $num_import + 1;
$field_parents[] = $key;
$imported_fields[] = $key;
$field['parent'] = $key_to_post_id[$field['parent']];
// Convert the key into the post_parent
$saved_field = acf_update_field($field);
$key_to_post_id[$key] = $saved_field['ID'];
}
}
} while ($num_import > 0);
}
// output
?>
<div class="wrap">
<h2>ACF PHP Recovery Tool</h2>
<?php
// Check the version of ACF
$acf_version = explode('.', acf_get_setting('version'));
if ($acf_version[0] != '5') {
?>
<div id="message" class="error below-h2">
<p><?php
printf(__('This tool was built for ACF version 5 and you have version %s.'), $acf_version[0]);
?>
</p>
</div>
<?php
}
?>
<?php
if (!empty($imported)) {
?>
<div id="message" class="updated below-h2"><p><?php
_e('Fieldsets recovered');
?>
.</p>
<ul>
<?php
foreach ($imported as $import) {
?>
<li><?php
edit_post_link($import['title'], '', '', $import['id']);
?>
</li>
<?php
}
?>
<li><strong><?php
_e('Remove the PHP defined fields! The duplicate field IDs interfer with the editing of the fields.');
?>
</strong></li>
</ul>
</div>
<?php
}
?>
<p><strong>This is a recovery tool. Do not use this as part of your workflow for importing and exporting ACF fieldsets.</strong></p>
<form method="POST">
<table class="widefat">
<thead>
<th>Import</th>
<th>Name</th>
<th>Possible Existing Matches</th>
</thead>
<tbody>
<?php
//.........这里部分代码省略.........
示例13: import
function import($args, $assoc_args)
{
if (is_multisite()) {
$choice = $this->select_blog();
switch_to_blog($choice);
if (!isset($args[0])) {
$choices = array();
$choices['all'] = 'all';
foreach ($this->paths as $path) {
if (!file_exists($path)) {
continue;
}
if ($dir = opendir($path)) {
while (false !== ($folder = readdir($dir))) {
if ($folder != '.' && $folder != '..') {
$key = trailingslashit($path . $folder);
$choices[$key] = $folder;
}
}
}
}
while (true) {
$choice = \cli\menu($choices, null, __('Choose a fieldgroup to import', 'acf-wpcli'));
\cli\line();
break;
}
}
$patterns = array();
if ($choice == 'all') {
foreach ($this->paths as $key => $value) {
$patterns[$key] = trailingslashit($value) . '*/data.json';
}
} else {
$patterns[] = $choice . 'data.json';
}
foreach ($patterns as $pattern) {
$i = 0;
//echo $pattern."\n";
foreach (glob($pattern) as $file) {
//Start acf 5 import
// read file
$json = file_get_contents($file);
// decode json
$json = json_decode($json, true);
// if importing an auto-json, wrap field group in array
if (isset($json['key'])) {
$json = array($json);
}
// vars
$ref = array();
$order = array();
foreach ($json as $field_group) {
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
// add to ref
$ref[$field['key']] = $field['ID'];
}
WP_CLI::success('imported the data.json for field_group ' . $field_group['title'] . '" into the dabatase!');
}
}
$i++;
if ($i === 1) {
break;
}
}
} else {
if (!isset($args[0])) {
$choices = array();
$choices['all'] = 'all';
foreach ($this->paths as $path) {
if (!file_exists($path)) {
continue;
}
if ($dir = opendir($path)) {
while (false !== ($folder = readdir($dir))) {
if ($folder != '.' && $folder != '..') {
//.........这里部分代码省略.........
示例14: synchronize
/**
* Synchronize Acf from json files
*
* @throws \Exception
*/
public static function synchronize()
{
global $wpdb;
$path = self::get_configuration_path();
$sql = "DELETE FROM {$wpdb->posts} WHERE post_type='acf-field' OR post_type='acf-field-group';";
$wpdb->query($sql);
// Just because we love implicit eval
$files = glob("{$path}/*.json");
if (is_array($files)) {
foreach ($files as $file) {
$json = file_get_contents($file);
$json = json_decode($json, true);
// copied from acf settings-export.php / import function
$fields = acf_extract_var($json, 'fields');
$fields = acf_prepare_fields_for_import($fields);
$field_group = acf_update_field_group($json);
$ref[$field_group['key']] = $field_group['ID'];
$order[$field_group['ID']] = 0;
foreach ($fields as $field) {
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
$field = acf_update_field($field);
$ref[$field['key']] = $field['ID'];
}
// /copied
}
}
ChecksumHandler::stamp();
}
示例15: ajax_move_field
function ajax_move_field()
{
// disable filters to ensure ACF loads raw data from DB
acf_disable_filters();
$args = acf_parse_args($_POST, array('nonce' => '', 'post_id' => 0, 'field_id' => 0, 'field_group_id' => 0));
// verify nonce
if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) {
die;
}
// confirm?
if ($args['field_id'] && $args['field_group_id']) {
// vars
$field = acf_get_field($args['field_id']);
$field_group = acf_get_field_group($args['field_group_id']);
// update parent
$field['parent'] = $field_group['ID'];
// remove conditional logic
$field['conditional_logic'] = 0;
// update field
acf_update_field($field);
$v1 = $field['label'];
$v2 = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>';
echo '<p><strong>' . __('Move Complete.', 'acf') . '</strong></p>';
echo '<p>' . sprintf(__('The %s field can now be found in the %s field group', 'acf'), $v1, $v2) . '</p>';
echo '<a href="#" class="button button-primary acf-close-popup">' . __("Close Window", 'acf') . '</a>';
die;
}
// get all field groups
$field_groups = acf_get_field_groups();
$choices = array();
// check
if (!empty($field_groups)) {
// loop
foreach ($field_groups as $field_group) {
// bail early if no ID
if (!$field_group['ID']) {
continue;
}
// bail ealry if is current
if ($field_group['ID'] == $args['post_id']) {
continue;
}
// append
$choices[$field_group['ID']] = $field_group['title'];
}
}
// render options
$field = acf_get_valid_field(array('type' => 'select', 'name' => 'acf_field_group', 'choices' => $choices));
echo '<p>' . __('Please select the destination for this field', 'acf') . '</p>';
echo '<form id="acf-move-field-form">';
// render
acf_render_field_wrap($field);
echo '<button type="submit" class="button button-primary">' . __("Move Field", 'acf') . '</button>';
echo '</form>';
// die
die;
}