Example #1
1
 function get_choices($options = array())
 {
     // defaults
     $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => ''));
     // vars
     $r = array();
     $args = array('hide_empty' => false);
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         return false;
     }
     // search
     if ($options['s']) {
         $args['search'] = $options['s'];
     }
     // filters
     $args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // get terms
     $terms = get_terms($field['taxonomy'], $args);
     // sort into hierachial order!
     if (is_taxonomy_hierarchical($field['taxonomy'])) {
         // get parent
         $parent = acf_maybe_get($args, 'parent', 0);
         $parent = acf_maybe_get($args, 'child_of', $parent);
         // this will fail if a search has taken place because parents wont exist
         if (empty($args['search'])) {
             $terms = _get_term_children($parent, $terms, $field['taxonomy']);
         }
     }
     /// append to r
     foreach ($terms as $term) {
         // add to json
         $r[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id']));
     }
     // return
     return $r;
 }
 function ajax_layout_title()
 {
     // options
     $options = acf_parse_args($_POST, array('post_id' => 0, 'i' => 0, 'field_key' => '', 'nonce' => '', 'layout' => '', 'acf' => array()));
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         die;
     }
     // vars
     $layout = false;
     foreach ($field['layouts'] as $k => $layout) {
         if ($layout['name'] === $options['layout']) {
             break;
         }
     }
     // bail ealry if no layout
     if (!$layout) {
         die;
     }
     // value
     // this flexible content field may be a sub field so it is important to
     // loop though all $_POST data to find thi's field's row value
     $value = $options['acf'];
     while (is_array($value)) {
         // get first key
         $k = key($value);
         // update value
         $value = array_pop($value[$k]);
         // stop looking if we have found the correct field's value
         if ($k === $options['field_key']) {
             break;
         }
     }
     // title
     $title = $this->get_layout_title($field, $layout, $options['i'], $value);
     // echo
     echo $title;
     die;
 }
Example #3
0
 function ajax_query()
 {
     // options
     $options = acf_parse_args($_POST, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => ''));
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         die;
     }
     // vars
     $r = array();
     $s = false;
     // search
     if ($options['s'] !== '') {
         // search may be integer
         $s = strval($options['s']);
         // strip slashes
         $s = wp_unslash($s);
     }
     // loop through choices
     if (!empty($field['choices'])) {
         foreach ($field['choices'] as $k => $v) {
             // if searching, but doesn't exist
             if ($s !== false && stripos($v, $s) === false) {
                 continue;
             }
             // append
             $r[] = array('id' => $k, 'text' => strval($v));
         }
     }
     // return JSON
     echo json_encode($r);
     die;
 }
Example #4
0
 function ajax_query()
 {
     // options
     $options = acf_parse_args($_GET, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => ''));
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         die;
     }
     // vars
     $r = array();
     // strip slashes
     $options['s'] = wp_unslash($options['s']);
     if (!empty($field['choices'])) {
         foreach ($field['choices'] as $k => $v) {
             // search
             if ($options['s'] && stripos($v, $options['s']) === false) {
                 continue;
             }
             // append
             $r[] = array('id' => $k, 'text' => strval($v));
         }
     }
     // return JSON
     echo json_encode($r);
     die;
 }
Example #5
0
 function save_post($post_id = 0)
 {
     // save $_POST data
     foreach ($_POST['acf'] as $k => $v) {
         // get field
         $field = acf_get_field($k);
         // update field
         if ($field) {
             acf_update_value($v, $post_id, $field);
         }
     }
 }
Example #6
0
 function ajax_query()
 {
     // options
     $options = acf_parse_args($_GET, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => ''));
     // validate
     if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) {
         die;
     }
     // vars
     $r = array();
     $args = array('hide_empty' => false);
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         die;
     }
     // search
     if ($options['s']) {
         $args['search'] = $options['s'];
     }
     // filters
     $args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // get terms
     $terms = get_terms($field['taxonomy'], $args);
     // sort into hierachial order!
     if (is_taxonomy_hierarchical($field['taxonomy'])) {
         // this will fail if a search has taken place because parents wont exist
         if (empty($args['search'])) {
             $terms = _get_term_children(0, $terms, $field['taxonomy']);
         }
     }
     /// append to r
     foreach ($terms as $term) {
         // add to json
         $r[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id']));
     }
     // return JSON
     echo json_encode($r);
     die;
 }
Example #7
0
 function ajax_get_attachment()
 {
     // options
     $options = acf_parse_args($_POST, array('post_id' => 0, 'id' => 0, 'field_key' => '', 'nonce' => ''));
     // validate
     if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) {
         die;
     }
     if (empty($options['id'])) {
         die;
     }
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         die;
     }
     // render
     $this->render_attachment($options['id'], $field);
     die;
 }
Example #8
0
 function get_choices($options = array())
 {
     // defaults
     $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'post_type' => '', 'taxonomy' => '', 'lang' => false, 'field_key' => '', 'paged' => 1));
     // vars
     $r = array();
     $args = array();
     // paged
     $args['posts_per_page'] = 20;
     $args['paged'] = $options['paged'];
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         return false;
     }
     // WPML
     if ($options['lang']) {
         global $sitepress;
         if (!empty($sitepress)) {
             $sitepress->switch_lang($options['lang']);
         }
     }
     // update $args
     if (!empty($options['post_type'])) {
         $args['post_type'] = acf_force_type_array($options['post_type']);
     } elseif (!empty($field['post_type'])) {
         $args['post_type'] = acf_force_type_array($field['post_type']);
     } else {
         $args['post_type'] = acf_get_post_types();
     }
     // update taxonomy
     $taxonomies = array();
     if (!empty($options['taxonomy'])) {
         $term = acf_decode_taxonomy_term($options['taxonomy']);
         // append to $args
         $args['tax_query'] = array(array('taxonomy' => $term['taxonomy'], 'field' => 'slug', 'terms' => $term['term']));
     } elseif (!empty($field['taxonomy'])) {
         $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']);
         // append to $args
         $args['tax_query'] = array();
         // now create the tax queries
         foreach ($taxonomies as $taxonomy => $terms) {
             $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms);
         }
     }
     // search
     if ($options['s']) {
         $args['s'] = $options['s'];
     }
     // filters
     $args = apply_filters('acf/fields/relationship/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/relationship/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/relationship/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // get posts grouped by post type
     $groups = acf_get_grouped_posts($args);
     if (!empty($groups)) {
         foreach (array_keys($groups) as $group_title) {
             // vars
             $posts = acf_extract_var($groups, $group_title);
             $titles = array();
             // data
             $data = array('text' => $group_title, 'children' => array());
             foreach (array_keys($posts) as $post_id) {
                 // override data
                 $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']);
             }
             // order by search
             if (!empty($args['s'])) {
                 $posts = acf_order_by_search($posts, $args['s']);
             }
             // append to $data
             foreach (array_keys($posts) as $post_id) {
                 $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]);
             }
             // append to $r
             $r[] = $data;
         }
         // optgroup or single
         $post_types = acf_force_type_array($args['post_type']);
         // add as optgroup or results
         if (count($post_types) == 1) {
             $r = $r[0]['children'];
         }
     }
     // return
     return $r;
 }
Example #9
0
 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;
 }
Example #10
0
function get_field_objects($post_id = false, $format_value = true, $load_value = true)
{
    // global
    global $wpdb;
    // filter post_id
    $post_id = acf_get_valid_post_id($post_id);
    // vars
    $meta = array();
    $fields = array();
    // get field_names
    if (is_numeric($post_id)) {
        $meta = get_post_meta($post_id);
    } elseif (strpos($post_id, 'user_') !== false) {
        $user_id = (int) str_replace('user_', '', $post_id);
        $meta = get_user_meta($user_id);
    } elseif (strpos($post_id, 'comment_') !== false) {
        $comment_id = (int) str_replace('comment_', '', $post_id);
        $meta = get_comment_meta($comment_id);
    } else {
        $rows = $wpdb->get_results($wpdb->prepare("SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", $post_id . '_%', '_' . $post_id . '_%'), ARRAY_A);
        if (!empty($rows)) {
            foreach ($rows as $row) {
                $meta[$row['option_name']][] = $row['option_value'];
            }
        }
    }
    // bail early if no meta
    if (empty($meta)) {
        return false;
    }
    // populate vars
    foreach ($meta as $k => $v) {
        // Hopefuly improve efficiency: bail early if $k does start with an '_'
        if ($k[0] === '_') {
            continue;
        }
        // does a field key exist for this value?
        if (!array_key_exists("_{$k}", $meta)) {
            continue;
        }
        // get field
        $field_key = $meta["_{$k}"][0];
        $field = acf_get_field($field_key);
        // bail early if not a parent field
        if (!$field || acf_is_sub_field($field)) {
            continue;
        }
        // load value
        if ($load_value) {
            $field['value'] = acf_get_value($post_id, $field);
        }
        // format value
        if ($format_value) {
            // get value for field
            $field['value'] = acf_format_value($field['value'], $post_id, $field);
        }
        // append to $value
        $fields[$field['name']] = $field;
    }
    // no value
    if (empty($fields)) {
        return false;
    }
    // return
    return $fields;
}
Example #11
0
 function get_choices($options = array())
 {
     // defaults
     $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'lang' => false, 'field_key' => '', 'paged' => 1));
     // vars
     $r = array();
     $args = array();
     // paged
     $args['posts_per_page'] = 20;
     $args['paged'] = $options['paged'];
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         return false;
     }
     // update $args
     if (!empty($field['post_type'])) {
         $args['post_type'] = acf_get_array($field['post_type']);
     } else {
         $args['post_type'] = acf_get_post_types();
     }
     // create tax queries
     if (!empty($field['taxonomy'])) {
         // append to $args
         $args['tax_query'] = array();
         // decode terms
         $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']);
         // now create the tax queries
         foreach ($taxonomies as $taxonomy => $terms) {
             $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms);
         }
     }
     // search
     if ($options['s']) {
         $args['s'] = $options['s'];
     }
     // filters
     $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // add archives to $r
     if ($args['paged'] == 1) {
         $archives = array();
         $archives[] = array('id' => home_url(), 'text' => home_url());
         foreach ($args['post_type'] as $post_type) {
             $archive_link = get_post_type_archive_link($post_type);
             if ($archive_link) {
                 $archives[] = array('id' => $archive_link, 'text' => $archive_link);
             }
         }
         // search
         if (!empty($args['s'])) {
             foreach (array_keys($archives) as $i) {
                 if (strpos($archives[$i]['text'], $args['s']) === false) {
                     unset($archives[$i]);
                 }
             }
             $archives = array_values($archives);
         }
         if (!empty($archives)) {
             $r[] = array('text' => __('Archives', 'acf'), 'children' => $archives);
         }
     }
     // get posts grouped by post type
     $groups = acf_get_grouped_posts($args);
     if (!empty($groups)) {
         foreach (array_keys($groups) as $group_title) {
             // vars
             $posts = acf_extract_var($groups, $group_title);
             $titles = array();
             // data
             $data = array('text' => $group_title, 'children' => array());
             foreach (array_keys($posts) as $post_id) {
                 // override data
                 $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id']);
             }
             // order by search
             if (!empty($args['s'])) {
                 $posts = acf_order_by_search($posts, $args['s']);
             }
             // append to $data
             foreach (array_keys($posts) as $post_id) {
                 $data['children'][] = array('id' => $post_id, 'text' => $posts[$post_id]);
             }
             // append to $r
             $r[] = $data;
         }
     }
     // return
     return $r;
 }
Example #12
0
 function ajax_query()
 {
     // validate
     if (!acf_verify_ajax()) {
         die;
     }
     // defaults
     $options = acf_parse_args($_POST, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1));
     // vars
     $results = array();
     $args = array();
     $s = false;
     $is_search = false;
     // paged
     $args['posts_per_page'] = 20;
     $args['paged'] = $options['paged'];
     // search
     if ($options['s'] !== '') {
         // strip slashes (search may be integer)
         $s = wp_unslash(strval($options['s']));
         // update vars
         $args['s'] = $s;
         $is_search = true;
     }
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         die;
     }
     // update $args
     if (!empty($field['post_type'])) {
         $args['post_type'] = acf_get_array($field['post_type']);
     } else {
         $args['post_type'] = acf_get_post_types();
     }
     // create tax queries
     if (!empty($field['taxonomy'])) {
         // append to $args
         $args['tax_query'] = array();
         // decode terms
         $taxonomies = acf_decode_taxonomy_terms($field['taxonomy']);
         // now create the tax queries
         foreach ($taxonomies as $taxonomy => $terms) {
             $args['tax_query'][] = array('taxonomy' => $taxonomy, 'field' => 'slug', 'terms' => $terms);
         }
     }
     // filters
     $args = apply_filters('acf/fields/page_link/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/page_link/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/page_link/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // add archives to $results
     if ($field['allow_archives'] && $args['paged'] == 1) {
         $archives = array();
         $archives[] = array('id' => home_url(), 'text' => home_url());
         foreach ($args['post_type'] as $post_type) {
             // vars
             $archive_link = get_post_type_archive_link($post_type);
             // bail ealry if no link
             if (!$archive_link) {
                 continue;
             }
             // bail early if no search match
             if ($is_search && stripos($archive_link, $s) === false) {
                 continue;
             }
             // append
             $archives[] = array('id' => $archive_link, 'text' => $archive_link);
         }
         // append
         $results[] = array('text' => __('Archives', 'acf'), 'children' => $archives);
     }
     // get posts grouped by post type
     $groups = acf_get_grouped_posts($args);
     // loop
     if (!empty($groups)) {
         foreach (array_keys($groups) as $group_title) {
             // vars
             $posts = acf_extract_var($groups, $group_title);
             // data
             $data = array('text' => $group_title, 'children' => array());
             // convert post objects to post titles
             foreach (array_keys($posts) as $post_id) {
                 $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id'], $is_search);
             }
             // order posts by search
             if ($is_search && empty($args['orderby'])) {
                 $posts = acf_order_by_search($posts, $args['s']);
             }
             // append to $data
             foreach (array_keys($posts) as $post_id) {
                 $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]);
             }
             // append to $results
             $results[] = $data;
         }
     }
     // return
     acf_send_ajax_results(array('results' => $results, 'limit' => $args['posts_per_page']));
 }
Example #13
0
 function update_value($value, $post_id, $field)
 {
     // save_other_choice
     if ($field['save_other_choice']) {
         // value isn't in choices yet
         if (!isset($field['choices'][$value])) {
             // get ID if local
             if (!$field['ID']) {
                 $field = acf_get_field($field['key'], true);
             }
             // bail early if no ID
             if (!$field['ID']) {
                 return $value;
             }
             // update $field
             $field['choices'][$value] = $value;
             // save
             acf_update_field($field);
         }
     }
     // return
     return $value;
 }
Example #14
0
 function remove_field($key)
 {
     // get field
     $field = acf_get_field($key);
     // remove parent reference
     $this->remove_parent_reference($field['parent'], $field['key']);
     // remove field
     unset($this->fields[$key]);
     // remove children
     if (acf_have_local_fields($key)) {
         acf_remove_local_fields($key);
     }
 }
Example #15
0
 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;
 }
Example #16
0
function acf_delete_value($post_id = 0, $key = '')
{
    // vars
    $field = false;
    $return = false;
    // string
    if (is_string($key)) {
        // find selector
        $field = acf_get_field_reference($key, $post_id);
        // get field key
        $field = acf_get_field($field);
        // field
    } elseif (is_array($key)) {
        // set vars
        $field = $key;
        $key = $field['name'];
        // bail early if not valid key
    } else {
        return false;
    }
    // action for 3rd party customization
    do_action("acf/delete_value", $post_id, $key, $field);
    if ($field) {
        do_action("acf/delete_value/type={$field['type']}", $post_id, $key, $field);
        do_action("acf/delete_value/name={$field['_name']}", $post_id, $key, $field);
        do_action("acf/delete_value/key={$field['key']}", $post_id, $key, $field);
    }
    // post
    if (is_numeric($post_id)) {
        $return = delete_metadata('post', $post_id, $key);
        delete_metadata('post', $post_id, '_' . $key);
        // user
    } elseif (strpos($post_id, 'user_') !== false) {
        $user_id = str_replace('user_', '', $post_id);
        $return = delete_metadata('user', $user_id, $key);
        delete_metadata('user', $user_id, '_' . $key);
        // comment
    } elseif (strpos($post_id, 'comment_') !== false) {
        $comment_id = str_replace('comment_', '', $post_id);
        $return = delete_metadata('comment', $comment_id, $key);
        delete_metadata('comment', $comment_id, '_' . $key);
        // option
    } else {
        $return = delete_option($post_id . '_' . $key);
        delete_option('_' . $post_id . '_' . $key);
    }
    // clear cache
    wp_cache_delete("load_value/post_id={$post_id}/name={$key}", 'acf');
    // return
    return $return;
}
 function acf_validate_save_post()
 {
     // bail early if no $_POST
     if (empty($_POST['acf'])) {
         return;
     }
     // loop
     foreach ($_POST['acf'] as $field_key => $value) {
         // get field
         $field = acf_get_field($field_key);
         $input = 'acf[' . $field_key . ']';
         // bail early if not found
         if (!$field) {
             continue;
         }
         // validate
         acf_validate_value($value, $field, $input);
     }
 }
Example #18
0
function acf_untrash_field($selector = 0)
{
    // disable JSON to avoid conflicts between DB and JSON
    acf_disable_local();
    // load the origional field gorup
    $field = acf_get_field($selector);
    // bail early if field did not load correctly
    if (empty($field)) {
        return false;
    }
    // delete field
    wp_untrash_post($field['ID']);
    // action for 3rd party customisation
    do_action('acf/untrash_field', $field);
    // return
    return true;
}
Example #19
0
 function save_post($post_id)
 {
     // bail early if empty
     if (empty($_POST['acf'])) {
         return;
     }
     // save $_POST data
     foreach ($_POST['acf'] as $k => $v) {
         // get field
         $field = acf_get_field($k);
         // continue if no field
         if (!$field) {
             continue;
         }
         // update
         acf_update_value($v, $post_id, $field);
     }
 }
Example #20
0
function acf_get_field_ancestors($field)
{
    // get field
    $ancestors = array();
    // loop
    while ($field && acf_is_field_key($field['parent'])) {
        $ancestors[] = $field['parent'];
        $field = acf_get_field($field['parent']);
    }
    // return
    return $ancestors;
}
Example #21
0
 /**
  * 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);
         }
     }
 }
Example #22
0
 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;
 }
Example #23
0
 function get_choices($options = array())
 {
     // defaults
     $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => ''));
     // vars
     $r = array();
     $args = array();
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         return false;
     }
     // editable roles
     $editable_roles = get_editable_roles();
     if (!empty($field['role'])) {
         foreach ($editable_roles as $role => $role_info) {
             if (!in_array($role, $field['role'])) {
                 unset($editable_roles[$role]);
             }
         }
     }
     // search
     if ($options['s']) {
         // append to $args
         $args['search'] = '*' . $options['s'] . '*';
         // add reference
         $this->field = $field;
         // add filter to modify search colums
         add_filter('user_search_columns', array($this, 'user_search_columns'), 10, 3);
     }
     // filters
     $args = apply_filters("acf/fields/user/query", $args, $field, $options['post_id']);
     $args = apply_filters("acf/fields/user/query/name={$field['_name']}", $args, $field, $options['post_id']);
     $args = apply_filters("acf/fields/user/query/key={$field['key']}", $args, $field, $options['post_id']);
     // get users
     $users = get_users($args);
     if (!empty($users) && !empty($editable_roles)) {
         foreach ($editable_roles as $role => $role_info) {
             // vars
             $this_users = array();
             $this_json = array();
             // loop over users
             foreach (array_keys($users) as $key) {
                 if (in_array($role, $users[$key]->roles)) {
                     // extract user
                     $user = acf_extract_var($users, $key);
                     // append to $this_users
                     $this_users[$user->ID] = $this->get_result($user, $field, $options['post_id']);
                 }
             }
             // bail early if no users for this role
             if (empty($this_users)) {
                 continue;
             }
             // order by search
             if (!empty($args['s'])) {
                 $this_users = acf_order_by_search($this_users, $args['s']);
             }
             // append to json
             foreach (array_keys($this_users) as $user_id) {
                 // add to json
                 $this_json[] = array('id' => $user_id, 'text' => $this_users[$user_id]);
             }
             // add as optgroup or results
             if (count($editable_roles) == 1) {
                 $r = $this_json;
             } else {
                 $r[] = array('text' => translate_user_role($role_info['name']), 'children' => $this_json);
             }
         }
     }
     // return
     return $r;
 }
Example #24
0
 function get_fields($parent_key = '')
 {
     // bail early if no parent
     if (!$this->have_fields($parent_key)) {
         return false;
     }
     // vars
     $fields = array();
     // append
     foreach ($this->parents[$parent_key] as $field_key) {
         $fields[] = acf_get_field($field_key);
     }
     // return
     return $fields;
 }
Example #25
0
        function ajax_add_term()
        {
            // vars
            $args = acf_parse_args($_POST, array('nonce' => '', 'field_key' => '', 'term_name' => '', 'term_parent' => ''));
            // verify nonce
            if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) {
                die;
            }
            // load field
            $field = acf_get_field($args['field_key']);
            if (!$field) {
                die;
            }
            // vars
            $taxonomy_obj = get_taxonomy($field['taxonomy']);
            $taxonomy_label = $taxonomy_obj->labels->singular_name;
            // validate cap
            // note: this situation should never occur due to condition of the add new button
            if (!current_user_can($taxonomy_obj->cap->manage_terms)) {
                echo '<p><strong>' . __("Error", 'acf') . '.</strong> ' . sprintf(__('User unable to add new %s', 'acf'), $taxonomy_label) . '</p>';
                die;
            }
            // save?
            if ($args['term_name']) {
                // exists
                if (term_exists($args['term_name'], $field['taxonomy'])) {
                    wp_send_json_error(array('error' => sprintf(__('%s already exists', 'acf'), $taxonomy_label)));
                }
                // insert
                $extra = array();
                if ($args['term_parent']) {
                    $extra['parent'] = $args['term_parent'];
                }
                $data = wp_insert_term($args['term_name'], $field['taxonomy'], $extra);
                // error?
                if (is_wp_error($data)) {
                    wp_send_json_error(array('error' => $data->get_error_message()));
                }
                // ancestors
                $prefix = '';
                $ancestors = get_ancestors($data['term_id'], $field['taxonomy']);
                if (!empty($ancestors)) {
                    $prefix = str_repeat('- ', count($ancestors));
                }
                // success
                wp_send_json_success(array('message' => sprintf(__('%s added', 'acf'), $taxonomy_label), 'term_id' => $data['term_id'], 'term_name' => $args['term_name'], 'term_label' => $prefix . $args['term_name'], 'term_parent' => $args['term_parent']));
            }
            ?>
<form method="post"><?php 
            acf_render_field_wrap(array('label' => 'Name', 'name' => 'term_name', 'type' => 'text'));
            if (is_taxonomy_hierarchical($field['taxonomy'])) {
                $choices = array();
                $choices2 = $this->get_choices(array('field_key' => $field['key']));
                if ($choices2) {
                    foreach ($choices2 as $v) {
                        $choices[$v['id']] = $v['text'];
                    }
                }
                acf_render_field_wrap(array('label' => 'Parent', 'name' => 'term_parent', 'type' => 'select', 'allow_null' => 1, 'ui' => 0, 'choices' => $choices));
            }
            ?>
<p class="acf-submit"><button class="acf-button blue" type="submit"><?php 
            _e("Add", 'acf');
            ?>
</button><i class="acf-loading"></i><span></span></p></form><?php 
            // die
            die;
        }
 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);
     }
 }
Example #27
0
 function update_field($field)
 {
     // bail ealry if not relevant
     if (!$field['parent'] || !acf_is_field_key($field['parent'])) {
         return $field;
     }
     // vars
     $ref = 0;
     // create reference
     if (empty($this->ref)) {
         $this->ref = array();
     }
     if (isset($this->ref[$field['parent']])) {
         $ref = $this->ref[$field['parent']];
     } else {
         // get parent without caching (important not to cache as parent $field will now contain new sub fields)
         $parent = acf_get_field($field['parent'], true);
         // bail ealry if no parent
         if (!$parent) {
             return $field;
         }
         // get ref
         $ref = $parent['ID'] ? $parent['ID'] : $parent['key'];
         // update ref
         $this->ref[$field['parent']] = $ref;
     }
     // update field's parent
     $field['parent'] = $ref;
     // return
     return $field;
 }
Example #28
0
 function get_ajax_query($options = array())
 {
     // defaults
     $options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 1));
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         return false;
     }
     // vars
     $results = array();
     $args = array();
     $s = false;
     $is_search = false;
     // paged
     $args['posts_per_page'] = 20;
     $args['paged'] = $options['paged'];
     // search
     if ($options['s'] !== '') {
         // strip slashes (search may be integer)
         $s = wp_unslash(strval($options['s']));
         // update vars
         $args['s'] = $s;
         $is_search = true;
     }
     // post_type
     if (!empty($field['post_type'])) {
         $args['post_type'] = acf_get_array($field['post_type']);
     } else {
         $args['post_type'] = acf_get_post_types();
     }
     // taxonomy
     if (!empty($field['taxonomy'])) {
         // vars
         $terms = acf_decode_taxonomy_terms($field['taxonomy']);
         // append to $args
         $args['tax_query'] = array();
         // now create the tax queries
         foreach ($terms as $k => $v) {
             $args['tax_query'][] = array('taxonomy' => $k, 'field' => 'slug', 'terms' => $v);
         }
     }
     // filters
     $args = apply_filters('acf/fields/post_object/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // get posts grouped by post type
     $groups = acf_get_grouped_posts($args);
     // bail early if no posts
     if (empty($groups)) {
         return false;
     }
     // loop
     foreach (array_keys($groups) as $group_title) {
         // vars
         $posts = acf_extract_var($groups, $group_title);
         // data
         $data = array('text' => $group_title, 'children' => array());
         // convert post objects to post titles
         foreach (array_keys($posts) as $post_id) {
             $posts[$post_id] = $this->get_post_title($posts[$post_id], $field, $options['post_id'], $is_search);
         }
         // order posts by search
         if ($is_search && empty($args['orderby'])) {
             $posts = acf_order_by_search($posts, $args['s']);
         }
         // append to $data
         foreach (array_keys($posts) as $post_id) {
             $data['children'][] = $this->get_post_result($post_id, $posts[$post_id]);
         }
         // append to $results
         $results[] = $data;
     }
     // optgroup or single
     if (count($args['post_type']) == 1) {
         $results = $results[0]['children'];
     }
     // vars
     $response = array('results' => $results, 'limit' => $args['posts_per_page']);
     // return
     return $response;
 }
Example #29
0
 function ajax_query()
 {
     // options
     $options = acf_parse_args($_POST, array('post_id' => 0, 's' => '', 'field_key' => '', 'nonce' => ''));
     // validate
     if (!wp_verify_nonce($options['nonce'], 'acf_nonce')) {
         die;
     }
     // vars
     $r = array();
     $args = array();
     // load field
     $field = acf_get_field($options['field_key']);
     if (!$field) {
         die;
     }
     // editable roles
     $editable_roles = get_editable_roles();
     if (!empty($field['role'])) {
         foreach ($editable_roles as $role => $role_info) {
             if (!in_array($role, $field['role'])) {
                 unset($editable_roles[$role]);
             }
         }
     }
     // search
     if ($options['s']) {
         // append to $args
         $args['search'] = '*' . $options['s'] . '*';
         // add filter to modify search colums
         add_filter('user_search_columns', array($this, 'user_search_columns'), 10, 1);
     }
     // filters
     $args = apply_filters('acf/fields/user/query', $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/user/query/name=' . $field['name'], $args, $field, $options['post_id']);
     $args = apply_filters('acf/fields/user/query/key=' . $field['key'], $args, $field, $options['post_id']);
     // get users
     $users = get_users($args);
     if (!empty($users) && !empty($editable_roles)) {
         foreach ($editable_roles as $role => $role_info) {
             // vars
             $this_users = array();
             $this_json = array();
             // loop over users
             foreach (array_keys($users) as $key) {
                 if (in_array($role, $users[$key]->roles)) {
                     // extract user
                     $user = acf_extract_var($users, $key);
                     // append to $this_users
                     $this_users[$user->ID] = ucfirst($user->display_name) . ' (' . $user->user_login . ')';
                 }
             }
             // bail early if no users for this role
             if (empty($this_users)) {
                 continue;
             }
             // order by search
             if (!empty($args['s'])) {
                 $this_users = acf_order_by_search($this_users, $args['s']);
             }
             // append to json
             foreach (array_keys($this_users) as $user_id) {
                 // add to json
                 $this_json[] = array('id' => $user_id, 'text' => $this_users[$user_id]);
             }
             // add as optgroup or results
             if (count($editable_roles) == 1) {
                 $r = $this_json;
             } else {
                 $r[] = array('text' => translate_user_role($role_info['name']), 'children' => $this_json);
             }
         }
     }
     // return JSON
     echo json_encode($r);
     die;
 }
 /**
  * Return flexible content layouts
  * Example: wp-admin/admin-ajax.php?action=flexible-content-copy/layouts&post=512
  */
 public function layouts()
 {
     $post_id = isset($_REQUEST['post']) ? trim($_REQUEST['post']) : '';
     if (empty($post_id)) {
         wp_send_json(array());
     }
     $blocks = get_field('content', (int) $post_id) ?: array();
     $meta = acf_get_field('content');
     if (!$meta) {
         wp_send_json(array());
     }
     $result = array();
     for ($i = 0; $i <= sizeof($blocks) - 1; $i++) {
         $block_name = $blocks[$i]['acf_fc_layout'];
         $block_meta = $this->search($meta['layouts'], 'name', $block_name);
         if (!isset($block_meta[0])) {
             $result[] = array('order' => $i, 'name' => $block_name, 'label' => $block_name);
         } else {
             $block_meta = $block_meta[0];
             $result[] = array('order' => $i, 'name' => $block_name, 'label' => $block_meta['label']);
         }
     }
     wp_send_json(array('layouts' => $result));
 }