<?php // vars // Note: $args is always passed to this view from above $fields = array(); $layout = false; $parent = 0; // use fields if passed in extract($args); // add clone $fields[] = acf_get_valid_field(array('ID' => 'acfcloneindex', 'key' => 'acfcloneindex', 'label' => __('New Field', 'acf'), 'name' => 'new_field', 'type' => 'text', 'parent' => $parent)); ?> <div class="acf-field-list-wrap"> <ul class="acf-hl acf-thead"> <li class="li-field-order"><?php _e('Order', 'acf'); ?> </li> <li class="li-field-label"><?php _e('Label', 'acf'); ?> </li> <li class="li-field-name"><?php _e('Name', 'acf'); ?> </li> <li class="li-field-type"><?php _e('Type', 'acf'); ?> </li>
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; }
function add_field($field) { // validate $field = acf_get_valid_field($field); // add parent reference $this->add_parent_reference($field['parent'], $field['key']); // add in menu order $field['menu_order'] = count($this->parents[$field['parent']]) - 1; // add field $this->fields[$field['key']] = $field; // clear cache wp_cache_delete("get_field/key={$field['key']}", 'acf'); wp_cache_delete("get_fields/parent={$field['parent']}", 'acf'); }
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); // parse types (converts string '0' to int 0) $field = acf_parse_types($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; } // parent may be a field key // - lookup parent ID if (acf_is_field_key($field['parent'])) { $field['parent'] = acf_get_field_id($field['parent']); } // 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 acf_delete_cache("get_field/key={$field['key']}"); // return return $field; }
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 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 acf_render_field_setting($field, $setting, $global = false) { // vars $atts = array(); // validate $setting = acf_get_valid_field($setting); // if this setting is not global, add a data attribute if (!$global) { $setting['wrapper']['data-setting'] = $field['type']; } // copy across prefix $setting['prefix'] = $field['prefix']; // copy across the $setting value if (isset($field[$setting['name']])) { $setting['value'] = $field[$setting['name']]; } // render acf_render_field_wrap($setting, 'tr', 'label'); }
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; }
function add_field($field) { // validate $field = acf_get_valid_field($field); // don't allow overrides // edit: some manually created fields (via .php) used duplicate keys (copy of origional field). /* if( acf_is_local_field($field['key']) ) { return; } */ // vars $parent = $field['parent']; // append $parents $this->parents[$parent][] = $field['key']; // add in menu order $field['menu_order'] = count($this->parents[$parent]) - 1; // find ancestors //$field['ancestors'] = array(); while (acf_is_local_field($parent)) { //$field['ancestors'][] = $parent; $parent = acf_get_local_field($parent); $parent = $parent['parent']; } //$field['ancestors'][] = $field['field_group']; // add field $this->fields[$field['key']] = $field; }
} } ?> </div> <ul class="acf-hl acf-tfoot"> <li class="acf-fr"> <a href="#" class="button button-primary button-large add-field"><?php _e('+ Add Field', 'acf'); ?> </a> </li> </ul> <?php if (!$parent) { // get clone $clone = acf_get_valid_field(array('ID' => 'acfcloneindex', 'key' => 'acfcloneindex', 'label' => __('New Field', 'acf'), 'name' => 'new_field', 'type' => 'text')); ?> <script type="text/html" id="tmpl-acf-field"> <?php acf_get_view('field-group-field', array('field' => $clone)); ?> </script> <?php } ?> </div>
function acf_render_field_wrap($field, $el = 'div', $instruction = 'label', $atts = array()) { // get valid field $field = acf_get_valid_field($field); // prepare field for input $field = acf_prepare_field($field); // el $elements = apply_filters('acf/render_field_wrap/elements', array('div' => 'div', 'tr' => 'td', 'ul' => 'li', 'ol' => 'li', 'dl' => 'dt', 'td' => 'div')); // validate $el if (!array_key_exists($el, $elements)) { $el = 'div'; } // atts $atts = acf_parse_args($atts, array('class' => '', 'data-name' => $field['name'], 'data-type' => $field['type'])); // add to atts $atts['class'] .= " acf-field field_type-{$field['type']}"; // add key if ($field['key']) { $atts['class'] .= " field_key-{$field['key']}"; $atts['data-key'] = $field['key']; } // add required if ($field['required']) { $atts['data-required'] = 1; } // vars $show_label = true; if ($el == 'td') { $show_label = false; } ?> <<?php echo $el; ?> <?php echo acf_esc_attr($atts); ?> > <?php if ($show_label) { ?> <<?php echo $elements[$el]; ?> class="acf-label"> <label for="<?php echo $field['id']; ?> "><?php echo acf_get_field_label($field); ?> </label> <?php if ($instruction == 'label' && $field['instructions']) { ?> <p class="description"><?php echo $field['instructions']; ?> </p> <?php } ?> </<?php echo $elements[$el]; ?> > <?php } ?> <<?php echo $elements[$el]; ?> class="acf-input"> <?php acf_render_field($field); ?> <?php if ($instruction == 'field' && $field['instructions']) { ?> <p class="description"><?php echo $field['instructions']; ?> </p> <?php } ?> <?php if (!empty($field['conditional_logic'])) { ?> <script type="text/javascript"> (function($) { if( typeof acf !== 'undefined' ) { acf.conditional_logic.add( '<?php echo $field['key']; ?> ', <?php echo json_encode($field['conditional_logic']); ?> ); } })(jQuery); </script> <?php } ?> </<?php echo $elements[$el]; ?> > </<?php echo $el; ?> ><?php }
function update_field($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, false); // create dummy field if (!$field) { $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => '')); } // save return acf_update_value($value, $post_id, $field); }
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 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 acf_views_render_field($field = false) { // get valid field $field = acf_get_valid_field($field); // prepare field for input $field = acf_prepare_field($field); // update $field['name'] $field['name'] = $field['_input']; // create field specific html do_action("acf/views_render_field", $field); do_action("acf/views_render_field/type={$field['type']}", $field); }