/** * Get the visible ACF fields. * @return array */ public function get_visible_acf_fields($widget_id = null) { global $wp_registered_widgets; $visible_fields = array(); // build field group filters required for current screen $filter = $this->get_acf_field_group_filters(); if (count($filter) === 0) { return $visible_fields; } // widgets need some special handling since they // require multiple acf_get_field_group_visibility() // calls in order to return all the visible fields if (acf_is_screen('widgets') || acf_is_screen('customize')) { if ($widget_id) { $filter['widget'] = _get_widget_id_base($widget_id); } else { // process each widget form individually for any visible fields foreach ($wp_registered_widgets as $widget) { $visible_fields += $this->get_visible_acf_fields($widget['id']); } return $visible_fields; } } $supported_field_types = array('email', 'text', 'textarea', 'repeater', 'flexible_content', 'qtranslate_file', 'qtranslate_image', 'qtranslate_text', 'qtranslate_textarea', 'qtranslate_wysiwyg'); foreach (acf_get_field_groups($filter) as $field_group) { $fields = acf_get_fields($field_group); foreach ($fields as $field) { if (in_array($field['type'], $supported_field_types)) { $visible_fields[] = array('id' => 'acf-' . $field['key']); } } } return $visible_fields; }
/** * @param $atts * @param null $content * * @return mixed|void */ protected function content($atts, $content = null) { $field_key = $label = ''; /** * @var string $el_class * @var string $show_label * @var string $align * @var string $field_group */ extract(shortcode_atts(array('el_class' => '', 'field_group' => '', 'show_label' => '', 'align' => ''), $atts)); if (0 === strlen($field_group)) { $groups = function_exists('acf_get_field_groups') ? acf_get_field_groups() : apply_filters('acf/get_field_groups', array()); if (is_array($groups) && isset($groups[0])) { $key = isset($groups[0]['id']) ? 'id' : (isset($groups[0]['ID']) ? 'ID' : 'id'); $field_group = $groups[0][$key]; } } if (!empty($field_group)) { $field_key = !empty($atts['field_from_' . $field_group]) ? $atts['field_from_' . $field_group] : 'field_from_group_' . $field_group; } if ('yes' === $show_label && $field_key) { $field_key .= '_labeled'; } $css_class = 'vc_gitem-acf' . (strlen($el_class) ? ' ' . $el_class : '') . (strlen($align) ? ' vc_gitem-align-' . $align : '') . (strlen($field_key) ? ' ' . $field_key : ''); return '<div ' . $field_key . ' class="' . esc_attr($css_class) . '">' . '{{ acf' . (!empty($field_key) ? ':' . $field_key : '') . ' }}' . '</div>'; }
function edit_widget($widget, $return, $instance) { // vars $post_id = 0; // get id if ($widget->number !== '__i__') { $post_id = "widget_{$widget->id}"; } // get field groups $field_groups = acf_get_field_groups(array('widget' => $widget->id_base)); // render if (!empty($field_groups)) { // render post data acf_form_data(array('post_id' => $post_id, 'nonce' => 'widget')); foreach ($field_groups as $field_group) { $fields = acf_get_fields($field_group); acf_render_fields($post_id, $fields, 'div', 'field'); } if ($widget->updated) { ?> <script type="text/javascript"> (function($) { acf.do_action('append', $('[id^="widget"][id$="<?php echo $widget->id; ?> "]') ); })(jQuery); </script> <?php } } }
/** * @return array multidimensional list of custom fields definitions */ public function getCustomFieldDefinitions() { $fieldGroups = acf_get_field_groups(); $customFields = array(); foreach ($fieldGroups as $fieldGroup) { $fields = acf_get_fields($fieldGroup); $customFields[] = array('id' => $fieldGroup['key'], 'label' => $fieldGroup['title'], 'type' => 'group', 'field_definitions' => $this->getFieldDefinitions($fields)); } return $customFields; }
private function get_acf_field_groups() { $acf_field_groups = acf_get_field_groups(); wp_cache_delete('field_groups', 'acf'); if (count($acf_field_groups)) { foreach ($acf_field_groups as $group) { $this->acf_field_groups[$group['key']] = $group['key']; } // end foreach field group } // end if field groups }
private function get_acf_field_groups() { $groups = array(); $acf_groups = acf_get_field_groups(); if (!count($acf_groups)) { return; } foreach ($acf_groups as $group) { $groups[$group['key']] = $group['key']; } return $groups; }
function widget($args, $instance) { $instance = (object) $instance; foreach (acf_get_field_groups(array('widget' => $this->identifier)) as $field_group) { foreach (acf_get_fields($field_group) as $field) { $name = $field['name']; $instance->{$name} = get_field($name, "widget_{$this->id}"); } } if (!empty($this->has_title)) { $instance->title = apply_filters('widget_title', empty($instance->title) ? '' : $instance->title, $instance, $this->id_base); } $this->render_widget($instance, $args); }
private function get_acf_field_groups() { $groups = array(); $acf_groups = acf_get_field_groups(); if (!count($acf_groups)) { return; } foreach ($acf_groups as $group) { $groups[$group['key']] = $group['key']; } // need to delete the ACF field group cache wp_cache_delete('get_field_groups', 'acf'); return $groups; }
public function display_settings() { $groups = acf_get_field_groups(false); $options = array(); foreach ($groups as $group_id => $group) { $fields = acf_get_fields($group); foreach ($fields as $field) { if (in_array($field['type'], array('tab'))) { continue; } if ('image_crop' == $field['type']) { $options[$field['key']] = $field['label']; } } } if ($options) { $this->display_field_select('cropper_field', 'Field', $options, ''); } }
public function load_field($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 $field; } // get any ACF field groups attached to the widget. $field_groups = acf_get_field_groups(array('widget' => $the_widget->id_base)); $field['sub_fields'] = array(); if (!empty($field_groups)) { foreach ($field_groups as $group) { $these_subfields = acf_get_fields($group); foreach ($these_subfields as $the_subfield) { $field['sub_fields'][] = $the_subfield; } } } return $field; }
/** * @param $atts * @param null $content * * @return mixed|void */ protected function content($atts, $content = null) { $atts = $atts + vc_map_get_attributes($this->getShortcode(), $atts); $field_group = $atts['field_group']; $field_key = ''; if (0 === strlen($atts['field_group'])) { $groups = function_exists('acf_get_field_groups') ? acf_get_field_groups() : apply_filters('acf/get_field_groups', array()); if (is_array($groups) && isset($groups[0])) { $key = isset($groups[0]['id']) ? 'id' : (isset($groups[0]['ID']) ? 'ID' : 'id'); $field_group = $groups[0][$key]; } } if ($field_group) { $field_key = !empty($atts['field_from_' . $field_group]) ? $atts['field_from_' . $field_group] : 'field_from_group_' . $field_group; } $css_class = array(); $css_class[] = 'vc_acf'; if ($atts['el_class']) { $css_class[] = $atts['el_class']; } if ($atts['align']) { $css_class[] = 'vc_txt_align_' . $atts['align']; } $value = ''; if ($field_key) { $css_class[] = $field_key; $value = do_shortcode('[acf field="' . $field_key . '" post_id="' . get_the_ID() . '"]'); if ($atts['show_label']) { $field = get_field_object($field_key); $label = is_array($field) && isset($field['label']) ? '<span class="vc_acf-label">' . $field['label'] . ':</span> ' : ''; $value = $label . $value; } } $css_string = implode(' ', $css_class); $output = '<div class="' . esc_attr($css_string) . '">' . $value . '</div>'; return $output; }
/** * Get the fields for the given taxonomies * * @return array|null * @author Nicolas Juen */ private function get_fields() { if (!is_null($this->fields)) { return $this->fields; } // Empty the fields on the taxonomies if (empty($this->taxonomies)) { $this->fields = null; } foreach ($this->taxonomies as $taxonomy_name => $taxonomy) { $groups = acf_get_field_groups(array('taxonomy' => $taxonomy_name)); if (empty($groups)) { continue; } $fields = array(); foreach ($groups as $group) { $fields += acf_get_fields($group); } foreach ($fields as $field) { $this->fields[$taxonomy_name][$field['name']] = $field['key']; } } return $this->fields; }
function get_field_groups() { // options $options = acf_parse_args($_POST, array('nonce' => '', 'post_id' => 0, 'ajax' => 1)); // vars $r = array(); $nonce = acf_extract_var($options, 'nonce'); // verify nonce if (!wp_verify_nonce($nonce, 'acf_nonce')) { die; } // get field groups $field_groups = acf_get_field_groups($options); // loop through field groups and build $r if (!empty($field_groups)) { foreach ($field_groups as $field_group) { // vars $class = 'acf-postbox ' . $field_group['style']; // load fields $fields = acf_get_fields($field_group); // get field HTML ob_start(); // render if ($field_group['label_placement'] == 'left') { ?> <table class="acf-table"> <tbody> <?php acf_render_fields($options['post_id'], $fields, 'tr', $field_group['instruction_placement']); ?> </tbody> </table> <?php } else { acf_render_fields($options['post_id'], $fields, 'div', $field_group['instruction_placement']); } $html = ob_get_clean(); // get style $style = acf_get_field_group_style($field_group); // append to $r $r[] = array('key' => $field_group['key'], 'title' => $field_group['title'], 'html' => $html, 'style' => $style, 'class' => $class); } } // return wp_send_json_success($r); }
function get_acf_field_groups() { $found = false; $cache = wp_cache_get('acf_reusable/acf_field_groups', 'acf_resuable', false, $found); if ($found) { $this->field_groups = $cache; return; } else { // look in acf-json $json_path = plugin_dir_path(__FILE__) . 'acf-json'; if (!is_dir($json_path)) { @mkdir($json_path); } if (is_multisite()) { $json_path .= '/' . get_current_blog_id(); if (!is_dir($json_path)) { @mkdir($json_path); } } $object_path = $json_path . '/acf_field_groups.json'; if (is_dir($json_path) && file_exists($object_path)) { $json = @file_get_contents($object_path); if ($json !== false) { $object = json_decode($json, true); if ($object !== NULL) { $this->field_groups = $object; } } return; } // end if is_dir etc } // end if else $field_groups = acf_get_field_groups(); $count = count($field_groups); for ($i = 0; $i < $count; $i++) { $fields = acf_get_fields($field_groups[$i]['key']); $field_groups[$i]['fields'] = $fields; $this->field_groups[$field_groups[$i]['key']] = $field_groups[$i]; } wp_cache_set('acf_reusable/acf_field_groups', $this->field_groups, 'acf_resuable'); // store json file to avoid using acf_get_field_groups unless necessary $json_path = plugin_dir_path(__FILE__) . 'acf-json'; if (!is_dir($json_path)) { @mkdir($json_path); } if (is_multisite()) { $json_path .= '/' . get_current_blog_id(); if (!is_dir($json_path)) { @mkdir($json_path); } } if (($handle = @fopen($json_path . '/acf_field_groups.json', 'w')) !== false) { $json = json_encode($this->field_groups); fwrite($handle, $json, strlen($json)); fclose($handle); } $this->clear_acf_cache(); }
function check_sync() { // message if ($ids = acf_maybe_get($_GET, 'acfsynccomplete')) { // explode $ids = explode(',', $ids); $total = count($ids); if ($total == 1) { acf_add_admin_notice(sprintf(__('Field group synchronised. %s', 'acf'), '<a href="' . get_edit_post_link($ids[0]) . '">' . get_the_title($ids[0]) . '</a>')); } else { acf_add_admin_notice(sprintf(_n('%s field group synchronised.', '%s field groups synchronised.', $total, 'acf'), $total)); } } // vars $groups = acf_get_field_groups(); // bail early if no field groups if (empty($groups)) { return; } // find JSON field groups which have not yet been imported foreach ($groups as $group) { // vars $local = acf_maybe_get($group, 'local', false); $modified = acf_maybe_get($group, 'modified', 0); $private = acf_maybe_get($group, 'private', false); // ignore DB / PHP / private field groups if ($local !== 'json' || $private) { // do nothing } elseif (!$group['ID']) { $this->sync[$group['key']] = $group; } elseif ($modified && $modified > get_post_modified_time('U', true, $group['ID'], true)) { $this->sync[$group['key']] = $group; } } // bail if no sync needed if (empty($this->sync)) { return; } // import field group if ($key = acf_maybe_get($_GET, 'acfsync')) { // disable JSON // - this prevents a new JSON file being created and causing a 'change' to theme files - solves git anoyance acf_update_setting('json', false); // validate check_admin_referer('bulk-posts'); // append fields if (acf_have_local_fields($key)) { $this->sync[$key]['fields'] = acf_get_local_fields($key); } // import $field_group = acf_import_field_group($this->sync[$key]); // redirect wp_redirect(admin_url($this->url . '&acfsynccomplete=' . $field_group['ID'])); exit; } elseif (acf_maybe_get($_GET, 'action2') === 'acfsync') { // validate check_admin_referer('bulk-posts'); // get ids $keys = acf_maybe_get($_GET, 'post'); if (!empty($keys)) { // disable JSON // - this prevents a new JSON file being created and causing a 'change' to theme files - solves git anoyance acf_update_setting('json', false); // vars $new_ids = array(); foreach ($keys as $key) { // append fields if (acf_have_local_fields($key)) { $this->sync[$key]['fields'] = acf_get_local_fields($key); } // import $field_group = acf_import_field_group($this->sync[$key]); // append $new_ids[] = $field_group['ID']; } // redirect wp_redirect(admin_url($this->url . '&acfsynccomplete=' . implode(',', $new_ids))); exit; } } // filters add_filter('views_edit-acf-field-group', array($this, 'list_table_views')); }
<?php // vars $field = array('label' => __('Select Field Groups', 'acf'), 'type' => 'checkbox', 'name' => 'acf_export_keys', 'prefix' => false, 'value' => false, 'toggle' => true, 'choices' => array()); $field_groups = acf_get_field_groups(); // populate choices if ($field_groups) { foreach ($field_groups as $field_group) { $field['choices'][$field_group['key']] = $field_group['title']; } } ?> <div class="wrap acf-settings-wrap"> <h1><?php _e('Tools', 'acf'); ?> </h1> <div class="acf-box" id="acf-export-field-groups"> <div class="title"> <h3><?php _e('Export Field Groups', 'acf'); ?> </h3> </div> <div class="inner"> <p><?php _e('Select the field groups you would like to export and then select your export method. Use the download button to export to a .json file which you can then import to another ACF installation. Use the generate button to export to PHP code which you can place in your theme.', 'acf'); ?> </p>
function ajax_query() { // validate if (!acf_verify_ajax()) { die; } // disable field to allow clone fields to appear selectable acf_disable_filter('clone'); // options $options = acf_parse_args($_POST, array('post_id' => 0, 'paged' => 0, 's' => '', 'title' => '', 'fields' => array())); // vars $results = array(); $s = false; $i = -1; $limit = 20; $range_start = $limit * ($options['paged'] - 1); // 0, 20, 40 $range_end = $range_start + ($limit - 1); // 19, 39, 59 // search if ($options['s'] !== '') { // strip slashes (search may be integer) $s = wp_unslash(strval($options['s'])); } // load groups $field_groups = acf_get_field_groups(); $field_group = false; // bail early if no field groups if (empty($field_groups)) { die; } // move current field group to start foreach (array_keys($field_groups) as $j) { // check ID if ($field_groups[$j]['ID'] !== $options['post_id']) { continue; } // extract field group and move to start $field_group = acf_extract_var($field_groups, $j); // field group found, stop looking break; } // if field group was not found, this is a new field group (not yet saved) if (!$field_group) { $field_group = array('ID' => $options['post_id'], 'title' => $options['title'], 'key' => ''); } // move current field group to start of list array_unshift($field_groups, $field_group); // loop foreach ($field_groups as $field_group) { // vars $fields = false; $data = array('text' => $field_group['title'], 'children' => array()); // get fields if ($field_group['ID'] == $options['post_id']) { $fields = $options['fields']; } else { $fields = acf_get_fields($field_group); $fields = acf_prepare_fields_for_import($fields); } // bail early if no fields if (!$fields) { continue; } // populate children $children = array(); $children[] = $field_group['key']; foreach ($fields as $field) { $children[] = $field['key']; } // loop foreach ($children as $child) { // bail ealry if no key (fake field group or corrupt field) if (!$child) { continue; } // vars $text = false; // bail early if is search, and $text does not contain $s if ($s !== false) { // get early $text = $this->get_clone_setting_choice($child); // search if (stripos($text, $s) === false) { continue; } } // $i $i++; // bail early if $i is out of bounds if ($i < $range_start || $i > $range_end) { continue; } // load text if ($text === false) { $text = $this->get_clone_setting_choice($child); } // append $data['children'][] = array('id' => $child, 'text' => $text); } // bail early if no children // - this group contained fields, but none shown on this page if (empty($data['children'])) { continue; } // append $results[] = $data; // end loop if $i is out of bounds // - no need to look further if ($i > $range_end) { break; } } // return acf_send_ajax_results(array('results' => $results, 'limit' => $limit)); }
function tabify_add_meta_boxes($post_type) { // get field groups $field_groups = acf_get_field_groups(); if (!empty($field_groups)) { foreach ($field_groups as $field_group) { // vars $id = "acf-{$field_group['key']}"; $title = 'ACF: ' . $field_group['title']; // add meta box add_meta_box($id, $title, '__return_true', $post_type); } } }
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 edit_term($term, $taxonomy) { // vars $post_id = "{$taxonomy}_{$term->term_id}"; $args = array('taxonomy' => $taxonomy); // update vars $this->form = '#edittag'; // get field groups $field_groups = acf_get_field_groups($args); // render if (!empty($field_groups)) { acf_form_data(array('post_id' => $post_id, 'nonce' => 'taxonomy')); foreach ($field_groups as $field_group) { $fields = acf_get_fields($field_group); ?> <?php if ($field_group['style'] == 'default') { ?> <h3><?php echo $field_group['title']; ?> </h3> <?php } ?> <table class="form-table"> <tbody> <?php acf_render_fields($post_id, $fields, 'tr', 'field'); ?> </tbody> </table> <?php } } }
function edit_attachment($form_fields, $post) { // vars $el = 'tr'; $post_id = $post->ID; $args = array('attachment' => 'All'); // $el if ($this->validate_page()) { //$el = 'div'; } // get field groups $field_groups = acf_get_field_groups($args); // render if (!empty($field_groups)) { // get acf_form_data ob_start(); acf_form_data(array('post_id' => $post_id, 'nonce' => 'attachment')); if ($this->validate_page()) { echo '<style type="text/css"> .compat-attachment-fields, .compat-attachment-fields > tbody, .compat-attachment-fields > tbody > tr, .compat-attachment-fields > tbody > tr > th, .compat-attachment-fields > tbody > tr > td { display: block; } tr.acf-field { display: block; margin: 0 0 13px; } tr.acf-field td.acf-label { display: block; margin: 0; } tr.acf-field td.acf-input { display: block; margin: 0; } </style>'; } // $el //if( $el == 'tr' ) { echo '</td></tr>'; //} foreach ($field_groups as $field_group) { $fields = acf_get_fields($field_group); acf_render_fields($post_id, $fields, $el, 'field'); } // $el //if( $el == 'tr' ) { echo '<tr class="compat-field-acf-blank"><td>'; //} $html = ob_get_contents(); ob_end_clean(); $form_fields['acf-form-data'] = array('label' => '', 'input' => 'html', 'html' => $html); } // return return $form_fields; }
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 } }
public function post_field_groups($post_id) { $found = false; $cache = wp_cache_get('post_field_groups-' . $post_id, 'acfpost2post', false, $found); if ($found) { return $cache; } $args = array('post_id' => $post_id); $field_groups = acf_get_field_groups($args); //echo '<pre>'; print_r($field_groups); die; $count = count($field_groups); for ($i = 0; $i < $count; $i++) { $field_groups[$i]['fields'] = acf_get_fields($field_groups[$i]['key']); } //echo '<pre>'; print_r($field_groups); die; wp_cache_set('post_field_groups-' . $post_id, $field_groups, 'acfpost2post'); return $field_groups; }
function admin_head() { // get field groups $field_groups = acf_get_field_groups(array('options_page' => $this->view['slug'])); // notices if (!empty($_GET['message']) && $_GET['message'] == '1') { acf_add_admin_notice(__("Options Updated", 'acf')); } if (empty($field_groups)) { $this->view['have_fields'] = 0; acf_add_admin_notice(__("No Custom Field Groups found for this options page", 'acf') . '. <a href="' . admin_url() . 'post-new.php?post_type=acf-field-group">' . __("Create a Custom Field Group", 'acf') . '</a>', 'error'); } else { foreach ($field_groups as $i => $field_group) { // vars $id = "acf-{$field_group['key']}"; $title = $field_group['title']; $context = $field_group['position']; $priority = 'high'; $args = array('field_group' => $field_group); // tweaks to vars if ($context == 'acf_after_title') { $context = 'normal'; } elseif ($context == 'side') { $priority = 'core'; } // filter for 3rd party customization $priority = apply_filters('acf/input/meta_box_priority', $priority, $field_group); // add meta box add_meta_box($id, $title, array($this, 'render_meta_box'), 'acf_options_page', $context, $priority, $args); } // foreach } // if }
function render_field($field) { global $post, $self; if (is_object($post)) { $current_id = $post->ID; } elseif ($self === "profile.php") { $current_id = "user_" . $_GET["user_id"]; } elseif ($self === "comment.php") { $current_id = "comment_" . $_GET["c"]; } else { $current_id = "options"; } $name_prefix = ''; // Geniem addition: get field group to get the visibility settings $field_groups = acf_get_field_groups(); foreach ($field_groups as $group) { if ($group["key"] == $field["group_key"]) { $contents = $group; break; } } $contents["active"] = true; // Geniem addition: set a variable that we are including the field group // this variable will be checked in the visibility condition check $this->included = "true"; // Geniem addition: check the visibility rules and return false if not visible if (!acf_get_field_group_visibility($contents)) { return false; } if (isset($field['parent'])) { preg_match_all('/\\[(field_\\w+)\\](\\[(\\d+)\\])?/', $field['prefix'], $parent_fields); if (isset($parent_fields[0])) { foreach ($parent_fields[0] as $parent_field_index => $parent_field) { $field_name = $parent_fields[1][$parent_field_index]; $index = $parent_fields[3][$parent_field_index]; $parent_field_object = acf_get_field($field_name); $parent_prefix = $parent_field_object['name']; if ($index !== '') { $parent_prefix .= '_' . $index; } $name_prefix .= $parent_prefix . '_'; } } $name_prefix .= $field['_name'] . '_'; } foreach ($field['sub_fields'] as $sub_field) { $sub_name_prefix = $name_prefix; $sub_field_name = $sub_field['name']; // update prefix to allow for nested values $sub_field['prefix'] = $field["name"]; $sub_field['name'] = "{$name_prefix}{$sub_field_name}"; // load value if ($sub_field['value'] === null) { $sub_field['value'] = acf_get_value($current_id, $sub_field); } // render input acf_render_field_wrap($sub_field); } }
private function acf_get_field_groups() { $field_groups = acf_get_field_groups(); // ************************************************************************ // call wp_cache_delete on the field_groups cache added by ACF // fields groups added after a call to the function acf_get_field_groups // will not appear unless that cache is cleared // hoping that ACF will be corrected to handle this at some point // will remove this when it is wp_cache_delete('get_field_groups', 'acf'); // ************************************************************************ $count = count($field_groups); for ($i = 0; $i < $count; $i++) { // skip field group of it is a field group in this plugin if ($field_groups[$i]['key'] != 'group_acf_opt_grp_dup' && $field_groups[$i]['key'] != 'acf_options-page-details') { $fields = acf_get_fields($field_groups[$i]['key']); $field_groups[$i]['fields'] = $fields; $this->field_groups[$field_groups[$i]['key']] = $field_groups[$i]; } } }
function render($user_id, $user_form, $el = 'tr') { // vars $post_id = "user_{$user_id}"; $show_title = true; // show title if ($user_form === 'register') { $show_title = false; } // args $args = array('user_id' => 'new', 'user_form' => $user_form); if ($user_id) { $args['user_id'] = $user_id; } // get field groups $field_groups = acf_get_field_groups($args); // bail early if no field groups if (empty($field_groups)) { return; } // form data acf_form_data(array('post_id' => $post_id, 'nonce' => 'user')); // loop foreach ($field_groups as $field_group) { // vars $fields = acf_get_fields($field_group); // title if ($show_title && $field_group['style'] === 'default') { echo '<h2>' . $field_group['title'] . '</h2>'; } // table start if ($el == 'tr') { echo '<table class="form-table"><tbody>'; } // render fields acf_render_fields($post_id, $fields, $el, $field_group['instruction_placement']); // table end if ($el == 'tr') { echo '</tbody></table>'; } } }
<?php $groups = function_exists('acf_get_field_groups') ? acf_get_field_groups() : apply_filters('acf/get_field_groups', array()); $groups_param_values = $fields_params = array(); foreach ($groups as $group) { $id = isset($group['id']) ? 'id' : (isset($group['ID']) ? 'ID' : 'id'); $groups_param_values[$group['title']] = $group[$id]; $fields = function_exists('acf_get_fields') ? acf_get_fields($group[$id]) : apply_filters('acf/field_group/get_fields', array(), $group[$id]); $fields_param_value = array(); foreach ($fields as $field) { $fields_param_value[$field['label']] = (string) $field['key']; } $fields_params[] = array('type' => 'dropdown', 'heading' => __('Field name', 'js_composer'), 'param_name' => 'field_from_' . $group[$id], 'value' => $fields_param_value, 'description' => __('Select field from group.', 'js_composer'), 'dependency' => array('element' => 'field_group', 'value' => array((string) $group[$id]))); } return array('vc_gitem_acf' => array('name' => __('Advanced Custom Field', 'js_composer'), 'base' => 'vc_gitem_acf', 'icon' => 'vc_icon-acf', 'category' => __('Content', 'js_composer'), 'description' => __('Advanced Custom Field', 'js_composer'), 'php_class_name' => 'Vc_Gitem_Acf_Shortcode', 'params' => array_merge(array(array('type' => 'dropdown', 'heading' => __('Field group', 'js_composer'), 'param_name' => 'field_group', 'value' => $groups_param_values, 'description' => __('Select field group.', 'js_composer'))), $fields_params, array(array('type' => 'checkbox', 'heading' => __('Show label', 'js_composer'), 'param_name' => 'show_label', 'value' => array(__('Yes, please', 'js_composer') => 'yes'), 'description' => __('Enter label to display before key value.', 'js_composer')), array('type' => 'dropdown', 'heading' => __('Align', 'js_composer'), 'param_name' => 'align', 'value' => array(__('left', 'js_composer') => 'left', __('right', 'js_composer') => 'right', __('center', 'js_composer') => 'center', __('justify', 'js_composer') => 'justify'), 'description' => __('Select alignment.', 'js_composer')), array('type' => 'textfield', 'heading' => __('Extra class name', 'js_composer'), 'param_name' => 'el_class', 'description' => __('If you wish to style particular content element differently, then use this field to add a class name and then refer to it in your css file.', 'js_composer'))))));
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_comment() { // vars $post_id = "comment_0"; // get field groups $field_groups = acf_get_field_groups(array('comment' => 'new')); if (!empty($field_groups)) { // render post data acf_form_data(array('post_id' => $post_id, 'nonce' => 'comment')); foreach ($field_groups as $field_group) { $fields = acf_get_fields($field_group); ?> <table class="form-table"> <tbody> <?php acf_render_fields($post_id, $fields, 'tr', 'field'); ?> </tbody> </table> <?php } } }