/** * 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']; } }
static function get_json() { if (!function_exists('acf_get_field_group')) { echo 'You need ACF activated to use this screen'; exit; } $keys = isset($_GET['generate-template-id']) ? array($_GET['generate-template-id']) : array(); //print_r($keys); //exit; //$keys = $_GET['acf_export_keys']; //$keys = array('group_55e23ad63ecc3'); //$keys = array('group_55d38b033048e'); //$keys = array('group_55d26a506a990'); // validate if (empty($keys)) { return false; } // vars $json = array(); // construct JSON foreach ($keys as $key) { // load field group $field_group = acf_get_field_group($key); // validate field group if (empty($field_group)) { continue; } // load fields $field_group['fields'] = acf_get_fields($field_group); // prepare fields $field_group['fields'] = acf_prepare_fields_for_export($field_group['fields']); // extract field group ID $id = acf_extract_var($field_group, 'ID'); // add to json array $json[] = $field_group; } // return return $json; }
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 acf_form($args = array()) { // vars $url = acf_get_current_url(); // defaults $args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg('updated', 'true', $url), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp')); $args['form_attributes'] = wp_parse_args($args['form_attributes'], array('id' => 'post', 'class' => '', 'action' => '', 'method' => 'post')); // filter post_id $args['post_id'] = acf_get_valid_post_id($args['post_id']); // load values from this post $post_id = $args['post_id']; // new post? if ($post_id == 'new_post') { // dont load values $post_id = false; // new post defaults $args['new_post'] = acf_parse_args($args['new_post'], array('post_type' => 'post', 'post_status' => 'draft')); } // attributes $args['form_attributes']['class'] .= ' acf-form'; // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { $fields[] = acf_get_valid_field(array('name' => '_post_title', 'label' => 'Title', 'type' => 'text', 'value' => $post_id ? get_post_field('post_title', $post_id) : '', 'required' => true)); } // post_content if ($args['post_content']) { $fields[] = acf_get_valid_field(array('name' => '_post_content', 'label' => 'Content', 'type' => 'wysiwyg', 'value' => $post_id ? get_post_field('post_content', $post_id) : '')); } // specific fields if ($args['fields']) { foreach ($args['fields'] as $selector) { // append field ($strict = false to allow for better compatibility with field names) $fields[] = acf_maybe_get_field($selector, $post_id, false); } } elseif ($args['field_groups']) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups(array('post_type' => $args['new_post']['post_type'])); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $field_group_fields = acf_get_fields($field_group); if (!empty($field_group_fields)) { foreach (array_keys($field_group_fields) as $i) { $fields[] = acf_extract_var($field_group_fields, $i); } } } } // updated message if (!empty($_GET['updated']) && $args['updated_message']) { echo '<div id="message" class="updated"><p>' . $args['updated_message'] . '</p></div>'; } // uploader (always set incase of multiple forms on the page) acf_update_setting('uploader', $args['uploader']); // display form if ($args['form']) { ?> <form <?php acf_esc_attr_e($args['form_attributes']); ?> > <?php } // render post data acf_form_data(array('post_id' => $args['post_id'], 'nonce' => 'acf_form')); ?> <div class="acf-hidden"> <?php acf_hidden_input(array('name' => '_acf_form', 'value' => base64_encode(json_encode($args)))); ?> </div> <div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?> "> <?php // html before fields echo $args['html_before_fields']; // render acf_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php if ($args['form']) { ?> <!-- Submit --> <div class="acf-form-submit"> <input type="submit" class="button button-primary button-large" value="<?php echo $args['submit_value']; ?> " /> <span class="acf-spinner"></span> </div> <!-- / Submit --> </form> <?php } }
function acf_untrash_field_group($selector = 0) { // disable JSON to avoid conflicts between DB and JSON acf_disable_local(); // load the origional field gorup $field_group = acf_get_field_group($selector); // bail early if field group did not load correctly if (empty($field_group)) { return false; } // get fields $fields = acf_get_fields($field_group); if (!empty($fields)) { foreach ($fields as $field) { acf_untrash_field($field['ID']); } } // delete wp_untrash_post($field_group['ID']); // action for 3rd party customization do_action('acf/untrash_field_group', $field_group); // return return true; }
/** * Get an ACF group slug * * @param $group_id * @return string|bool */ private static function get_group_slug($group_id) { global $acf_local; if (isset($acf_local->groups[$group_id])) { // First try the local groups (added by PHP). $title = $acf_local->groups[$group_id]['title']; } else { $args = ['post_type' => 'acf-field-group']; if (is_numeric($group_id)) { $args['id'] = $group_id; } else { $args['name'] = $group_id; } // Then try the db if not found in local. // @codingStandardsIgnoreStart // Ignore use WP_Query rule because we don't know if this will be a sub-query and hence create complications. $groups = get_posts($args); // @codingStandardsIgnoreEnd $title = empty($groups) ? false : $groups[0]->post_title; // Patch for the new version of ACF Fields plugins >= 5.4.*. if (!$title) { $groups = acf_get_field_group($group_id); $title = empty($groups) ? false : $groups['title']; } } if ($title) { return strtolower(str_replace(' ', '_', $title)); } return false; }
/** * An altered version of acf/admin/settings-tool.php::import() * * ACF's import function takes care of most things, but this * function makes up for what it lacks: * * - Removes fields that are in the DB, but not in the import file * - Removes field groups that are in the DB, but not in the import file * - Updates fields that have changed * - Displays command-line messages * * @return null */ protected function importFields() { // 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(); $deletedgroups = array(); $deletedfields = array(); $ref = array(); $order = array(); $allgroups = $this->getFieldGroups(); $allfields = $this->getFields(); foreach ($json as $field_group) { $update = false; // check if field group exists if ($post = acf_get_field_group($field_group['key'], true)) { // \WP_CLI::log($field_group['title'] . " group already exists. Updating."); // add ID to trigger update instead of insert $field_group["ID"] = $post["ID"]; $update = true; // } else { // \WP_CLI::log($field_group['title'] . " group is new. Adding."); } // 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); // remove group from $allgroups array if (isset($allgroups[$field_group['ID']])) { unset($allgroups[$field_group['ID']]); } // 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']]++; // add ID if the field already exists if ($post = acf_get_field($field['key'], true)) { // add ID to trigger update instead of insert $field["ID"] = $post["ID"]; // \WP_CLI::log($field_group['title'] . "->" . $field['label'] . " field already exists. Updating."); // } else { // \WP_CLI::log($field_group['title'] . "->" . $field['label'] . " field is new. Adding."); } // save field $field = acf_update_field($field); // remove field from allfields array if (isset($allfields[$field['ID']])) { unset($allfields[$field['ID']]); } // add to ref $ref[$field['key']] = $field['ID']; } if ($update) { \WP_CLI::success($field_group['title'] . " field group updated."); } else { \WP_CLI::success($field_group['title'] . " field group added."); } } if (!empty($allgroups)) { foreach ($allgroups as $post) { \WP_CLI::success($post->post_title . " field group deleted."); wp_delete_post($post->ID); } } if (!empty($allfields)) { foreach ($allfields as $post) { \WP_CLI::success($post->post_title . " field deleted."); wp_delete_post($post->ID); } } }
function generate() { // validate if (empty($_POST['acf_export_keys'])) { acf_add_admin_notice(__("No field groups selected", 'acf'), 'error'); return; } // vars $id_ref = array(); $json = array(); // construct JSON foreach ($_POST['acf_export_keys'] as $key) { // load field group $field_group = acf_get_field_group($key); // validate field group if (empty($field_group)) { continue; } // load fields $fields = acf_get_fields($field_group); // prepare fields $fields = acf_prepare_fields_for_export($fields); // add to field group $field_group['fields'] = $fields; // extract field group ID $id = acf_extract_var($field_group, 'ID'); // add to json array $json[] = $field_group; } // end foreach // update view $this->view = 'settings-export-generate'; $this->data['field_groups'] = $json; }
function get_json() { // validate if (empty($_POST['acf_export_keys'])) { return false; } // vars $json = array(); // construct JSON foreach ($_POST['acf_export_keys'] as $key) { // load field group $field_group = acf_get_field_group($key); // validate field group if (empty($field_group)) { continue; } // load fields $field_group['fields'] = acf_get_fields($field_group); // prepare fields $field_group['fields'] = acf_prepare_fields_for_export($field_group['fields']); // extract field group ID $id = acf_extract_var($field_group, 'ID'); // add to json array $json[] = $field_group; } // return return $json; }
<?php // global global $post; // vars $field_group = acf_get_field_group($post); // UI needs at lease 1 location rule if (empty($field_group['location'])) { $field_group['location'] = array(array(array('param' => 'post_type', 'operator' => '==', 'value' => 'post'))); } ?> <table class="acf-table"> <tbody> <tr class="acf-field"> <td class="acf-label"> <label><?php _e("Rules", 'acf'); ?> </label> <p><?php _e("Create a set of rules to determine which edit screens will use these advanced custom fields", 'acf'); ?> </p> </td> <td class="acf-input"> <div class="location-groups"> <?php foreach ($field_group['location'] as $group_id => $group) { // $group_id must be completely different to $rule_id to avoid JS issues $group_id = "group_{$group_id}";
public function render() { acf_enqueue_scripts(); do_action('acf/view', $this->args, $this); $post_id = $this->post_id; $args = $this->args; // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { $fields[] = acf_get_valid_field(array('name' => '_post_title', 'label' => 'Title', 'type' => 'text', 'value' => $post_id ? get_post_field('post_title', $post_id) : '', 'required' => true)); } // post_content if ($args['post_content']) { $fields[] = acf_get_valid_field(array('name' => '_post_content', 'label' => 'Content', 'type' => 'wysiwyg', 'value' => $post_id ? get_post_field('post_content', $post_id) : '')); } // specific fields if ($args['fields']) { foreach ($args['fields'] as $selector) { // append field ($strict = false to allow for better compatibility with field names) $fields[] = acf_maybe_get_field($selector, $post_id, false); } } elseif ($args['field_groups']) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups(array('post_type' => $args['new_post']['post_type'])); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $field_group_fields = acf_get_fields($field_group); if (!empty($field_group_fields)) { foreach (array_keys($field_group_fields) as $i) { $fields[] = acf_extract_var($field_group_fields, $i); } } } } ?> <div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?> "> <?php // html before fields echo $args['html_before_fields']; // render acf_views_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php }
function export($args, $assoc_args) { // if empty it will show export all fields $export_field = ''; if (is_multisite()) { $choice = $this->select_blog(); switch_to_blog($choice); } //if export all is used skip the question popup if (empty($args) || $args[0] != 'all') { $export_field = $this->select_acf_field(); } if (empty($export_field)) { WP_CLI::success("Exporting all fieldgroups \n"); } else { WP_CLI::success("Exporting fieldgroup \n"); } $field_groups = get_posts(array('numberposts' => -1, 'post_type' => 'acf-field-group', 'sort_column' => 'menu_order', 'order' => 'ASC', 'include' => $export_field)); if ($field_groups) { if (isset($assoc_args['export-path']) && isset($this->paths[$assoc_args['export-path']])) { $export_path = $this->paths[$assoc_args['export-path']]; } else { $export_path = $this->select_export_path(); } $acf_fld_grp = new acf_field(); if (!is_dir($export_path) && !mkdir($export_path, 0755, false)) { WP_CLI::line('fieldgroup directory exists or cant be created!'); } foreach ($field_groups as $group) { $title = get_the_title($group->ID); $sanitized_title = sanitize_title($title); $subpath = $export_path . $sanitized_title; $uniquid_path = $subpath . '/uniqid'; $field_group_array = array(); $field_group = acf_get_field_group($group->ID); // validate field group if (empty($field_group)) { continue; } // load fields $fields = acf_get_fields($field_group); // prepare fields $fields = acf_prepare_fields_for_export($fields); // add to field group $field_group['fields'] = $fields; // extract field group ID $id = acf_extract_var($field_group, 'ID'); $json = acf_json_encode($field_group); // retrieve the uniquid from the file if it exists else we make a new one $uniqid = file_exists($uniquid_path) ? file_get_contents($uniquid_path) : uniqid(); // each field_group gets it's own folder by field_group name if (!is_dir($subpath) && !mkdir($subpath, 0755, false)) { WP_CLI::line('fieldgroup subdirectory exists or cant be created!'); } else { // let's write the array to a data.php file so it can be used later on $fp = fopen($subpath . '/' . "data.php", "w"); $output = "<?php \n\$group = " . var_export($field_group, true) . ';'; fwrite($fp, $output); fclose($fp); $fp = fopen($subpath . '/' . "data.json", "w"); $output = $json; fwrite($fp, $output); fclose($fp); // write the uniquid file if it doesn't exist if (!file_exists($uniquid_path)) { $fp = fopen($subpath . '/' . "uniqid", "w"); $output = $uniqid; fwrite($fp, $output); fclose($fp); } WP_CLI::success("Fieldgroup " . $title . " exported "); } } } else { //error seems to be returning and break out of my loop //WP_CLI::error( 'No field groups were found in the database' ); echo 'No field groups were found in the database'; echo ' '; } if (is_multisite()) { restore_current_blog(); } }
function acf_form($args = array()) { // vars $url = home_url($_SERVER['REQUEST_URI']); // defaults $args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg('updated', 'true', $url), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div')); $args['form_attributes'] = wp_parse_args($args['form_attributes'], array('id' => 'post', 'class' => '', 'action' => '', 'method' => 'post')); // filter post_id $args['post_id'] = acf_get_valid_post_id($args['post_id']); // load values from this post $post_id = $args['post_id']; // new post? if ($post_id == 'new_post') { // dont load values $post_id = false; // new post defaults $args['new_post'] = acf_parse_args($args['new_post'], array('post_type' => 'post', 'post_status' => 'draft')); } // attributes $args['form_attributes']['class'] .= ' acf-form'; // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { $fields[] = acf_get_valid_field(array('name' => '_post_title', 'label' => 'Title', 'type' => 'text', 'value' => $post_id ? get_post_field('post_title', $post_id) : '', 'required' => true)); } // post_content if ($args['post_content']) { $fields[] = acf_get_valid_field(array('name' => '_post_content', 'label' => 'Content', 'type' => 'wysiwyg', 'value' => $post_id ? get_post_field('post_content', $post_id) : '')); } // specific fields if (!empty($args['fields'])) { foreach ($args['fields'] as $selector) { $fields[] = acf_get_field($selector); } } elseif (!empty($args['field_groups'])) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups(array('post_type' => $args['new_post']['post_type'])); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $fields = array_merge($fields, acf_get_fields($field_group)); } } // updated message if (!empty($_GET['updated']) && $args['updated_message']) { echo '<div id="message" class="updated"><p>' . $args['updated_message'] . '</p></div>'; } // display form if ($args['form']) { ?> <form <?php acf_esc_attr_e($args['form_attributes']); ?> > <?php } // render post data acf_form_data(array('post_id' => $args['post_id'], 'nonce' => 'acf_form')); ?> <div class="acf-hidden"> <?php acf_hidden_input(array('name' => '_acf_form', 'value' => base64_encode(json_encode($args)))); ?> </div> <div class="acf-form-fields"> <?php // html before fields echo $args['html_before_fields']; // start table if ($args['label_placement'] == 'left') { $args['field_el'] = 'tr'; ?> <table class="acf-table"><tbody><?php } acf_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // end table if ($args['label_placement'] == 'left') { ?> </tbody></table><?php } // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php if ($args['form']) { ?> <!-- Submit --> <div class="acf-form-submit"> <input type="submit" class="button button-primary button-large" value="<?php echo $args['submit_value']; ?> " /> </div> <!-- / Submit --> </form> <?php } }
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; }
function field_group_columns_html($column, $post_id) { // vars $field_group = acf_get_field_group($post_id); // render $this->render_column($column, $field_group); }
function acf_form($args = array()) { // vars $url = acf_get_current_url(); // defaults $args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg('updated', 'true', $url), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div')); $args['form_attributes'] = wp_parse_args($args['form_attributes'], array('id' => 'post', 'class' => '', 'action' => '', 'method' => 'post')); // filter post_id $args['post_id'] = acf_get_valid_post_id($args['post_id']); // load values from this post $post_id = $args['post_id']; // new post? if ($post_id == 'new_post') { // dont load values $post_id = false; // new post defaults $args['new_post'] = acf_parse_args($args['new_post'], array('post_type' => 'post', 'post_status' => 'draft')); } // attributes $args['form_attributes']['class'] .= ' acf-form'; // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { $fields[] = acf_get_valid_field(array('name' => '_post_title', 'label' => 'Title', 'type' => 'text', 'value' => $post_id ? get_post_field('post_title', $post_id) : '', 'required' => true)); } // post_content if ($args['post_content']) { $fields[] = acf_get_valid_field(array('name' => '_post_content', 'label' => 'Content', 'type' => 'wysiwyg', 'value' => $post_id ? get_post_field('post_content', $post_id) : '')); } // specific fields if (!empty($args['fields'])) { foreach ($args['fields'] as $selector) { $fields[] = get_field_object($selector, $post_id, false, false); } } elseif (!empty($args['field_groups'])) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups(array('post_type' => $args['new_post']['post_type'])); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $field_group_fields = acf_get_fields($field_group); if (!empty($field_group_fields)) { foreach (array_keys($field_group_fields) as $i) { $fields[] = acf_extract_var($field_group_fields, $i); } } } } // updated message if (!empty($_GET['updated']) && $args['updated_message']) { echo '<div id="message" class="updated"><p>' . $args['updated_message'] . '</p></div>'; } // display form if ($args['form']) { ?> <form <?php acf_esc_attr_e($args['form_attributes']); ?> > <?php } // render post data acf_form_data(array('post_id' => $args['post_id'], 'nonce' => 'acf_form')); ?> <div class="acf-hidden"> <?php acf_hidden_input(array('name' => '_acf_form', 'value' => base64_encode(json_encode($args)))); ?> </div> <div class="acf-fields acf-form-fields"> <?php // html before fields echo $args['html_before_fields']; // start table if ($args['label_placement'] == 'left') { $args['field_el'] = 'tr'; ?> <table class="acf-table"><tbody><?php } acf_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // end table if ($args['label_placement'] == 'left') { ?> </tbody></table><?php } // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php if ($args['form']) { ?> <!-- Submit --> <div class="acf-form-submit"> <input type="submit" class="button button-primary button-large" value="<?php echo $args['submit_value']; ?> " /> <span class="acf-loading" style="display: none;"></span> </div> <!-- / Submit --> </form> <script type="text/javascript"> (function($) { // vars var $spinner = $('#<?php echo $args['form_attributes']['id']; ?> .acf-form-submit .acf-loading'); // show spinner on submit $(document).on('submit', '#<?php echo $args['form_attributes']['id']; ?> ', function(){ // show spinner $spinner.css('display', 'inline-block'); }); // hide spinner after validation acf.add_filter('validation_complete', function( json, $form ){ // hide spinner $spinner.css('display', 'none'); // return return json; }); })(jQuery); </script> <?php } }
function acf_get_fields($parent = false) { // allow $parent to be a field group ID if (!is_array($parent)) { $parent = acf_get_field_group($parent); } // bail early if no parent if (!$parent) { return false; } // vars $fields = array(); // try JSON before DB to save query time if (acf_have_local_fields($parent['key'])) { $fields = acf_get_local_fields($parent['key']); } else { $fields = acf_get_fields_by_id($parent['ID']); } // filter $fields = apply_filters('acf/get_fields', $fields, $parent); // return return $fields; }
function acf_untrash_field_group($selector = 0) { // disable filters to ensure ACF loads raw data from DB acf_disable_filters(); // load the origional field gorup $field_group = acf_get_field_group($selector); // bail early if field group did not load correctly if (empty($field_group)) { return false; } // get fields $fields = acf_get_fields($field_group); if (!empty($fields)) { foreach ($fields as $field) { acf_untrash_field($field['ID']); } } // delete wp_untrash_post($field_group['ID']); // action for 3rd party customization do_action('acf/untrash_field_group', $field_group); // return return true; }
function ajax_move_field() { // disable JSON to avoid conflicts between DB and JSON acf_disable_local(); $args = acf_parse_args($_POST, array('nonce' => '', '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 sprintf(__('The %s field can now be found in the %s field group', 'acf'), $v1, $v2) . '</p>'; echo '<a href="#" class="acf-button blue acf-close-popup">' . __("Close Window", 'acf') . '</a>'; die; } // get all field groups $field_groups = acf_get_field_groups(); $choices = array(); if (!empty($field_groups)) { foreach ($field_groups as $field_group) { if ($field_group['ID']) { $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="acf-button blue">' . __("Move Field", 'acf') . '</button>'; echo '</form>'; // die die; }
/** * 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; }