function clean_post_value($value) { // validate if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // array foreach ($value as $k => $v) { // object? if (is_object($v) && isset($v->ID)) { $value[$k] = $v->ID; } } // save value as strings, so we can clearly search for them in SQL LIKE statements $value = array_map('strval', $value); // return return $value; }
function render_field($field) { // force value to array $field['value'] = acf_force_type_array($field['value']); // convert values to int $field['value'] = array_map('intval', $field['value']); // get taxonomy $taxonomy = get_taxonomy($field['taxonomy']); // Change Field into a select $field['type'] = 'select'; $field['ui'] = 1; $field['ajax'] = 1; $field['choices'] = array(); // value if (!empty($field['value'])) { // get terms } // render select acf_render_field($field); var_dump($field); }
function render_field($field) { // decode value (convert to array) $field['value'] = acf_force_type_array($field['value']); // hiden input acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); // vars $i = 0; // class $field['class'] .= ' acf-checkbox-list'; $field['class'] .= $field['layout'] == 'horizontal' ? ' acf-hl' : ' acf-bl'; // e $e = '<ul ' . acf_esc_attr(array('class' => $field['class'])) . '>'; // checkbox saves an array $field['name'] .= '[]'; // foreach choices if (!empty($field['choices'])) { foreach ($field['choices'] as $value => $label) { // increase counter $i++; // vars $atts = array('type' => 'checkbox', 'id' => $field['id'], 'name' => $field['name'], 'value' => $value); if (in_array($value, $field['value'])) { $atts['checked'] = 'checked'; } if (isset($field['disabled']) && in_array($value, $field['disabled'])) { $atts['disabled'] = 'true'; } // each input ID is generated with the $key, however, the first input must not use $key so that it matches the field's label for attribute if ($i > 1) { $atts['id'] .= '-' . $value; } $e .= '<li><label><input ' . acf_esc_attr($atts) . '/>' . $label . '</label></li>'; } } $e .= '</ul>'; // return echo $e; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert values to int $value = array_map('intval', $value); // load users foreach (array_keys($value) as $i) { // vars $user_id = $value[$i]; $user_data = get_userdata($user_id); //cope with deleted users by @adampope if (!is_object($user_data)) { unset($value[$i]); continue; } // append to array $value[$i] = array(); $value[$i]['ID'] = $user_id; $value[$i]['user_firstname'] = $user_data->user_firstname; $value[$i]['user_lastname'] = $user_data->user_lastname; $value[$i]['nickname'] = $user_data->nickname; $value[$i]['user_nicename'] = $user_data->user_nicename; $value[$i]['display_name'] = $user_data->display_name; $value[$i]['user_email'] = $user_data->user_email; $value[$i]['user_url'] = $user_data->user_url; $value[$i]['user_registered'] = $user_data->user_registered; $value[$i]['user_description'] = $user_data->user_description; $value[$i]['user_avatar'] = get_avatar($user_id); } // convert back from array if neccessary if (!$field['multiple']) { $value = array_shift($value); } // return value return $value; }
function render_field($field) { // force value to array $field['value'] = acf_force_type_array($field['value']); // convert values to int $field['value'] = array_map('intval', $field['value']); // vars $div = array('class' => 'acf-taxonomy-field acf-soh', 'data-load_save' => $field['load_save_terms'], 'data-type' => $field['field_type'], 'data-taxonomy' => $field['taxonomy']); // get taxonomy $taxonomy = get_taxonomy($field['taxonomy']); ?> <div <?php acf_esc_attr_e($div); ?> > <?php if ($field['add_term'] && current_user_can($taxonomy->cap->manage_terms)) { ?> <a href="#" class="acf-js-tooltip acf-icon small acf-soh-target" data-name="add" title="<?php echo sprintf(__('Add new %s ', 'acf'), $taxonomy->labels->singular_name); ?> "> <i class="acf-sprite-add"></i> </a> <?php } if ($field['field_type'] == 'select') { $field['multiple'] = 0; $this->render_field_select($field); } elseif ($field['field_type'] == 'multi_select') { $field['multiple'] = 1; $this->render_field_select($field); } elseif ($field['field_type'] == 'radio') { $this->render_field_checkbox($field); } elseif ($field['field_type'] == 'checkbox') { $this->render_field_checkbox($field); } ?> </div><?php }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert values to int $value = array_map('intval', $value); // load posts if needed if ($field['return_format'] == 'object') { // get posts $value = $this->get_posts($value); } // convert back from array if neccessary if (!$field['multiple']) { $value = array_shift($value); } // return value return $value; }
function get_posts($value) { // force value to array $value = acf_force_type_array($value); // get selected post ID's $post__in = array(); foreach (array_keys($value) as $k) { if (is_numeric($value[$k])) { // convert to int $value[$k] = intval($value[$k]); // append to $post__in $post__in[] = $value[$k]; } } // bail early if no posts if (empty($post__in)) { return $value; } // get posts $posts = acf_get_posts(array('post__in' => $post__in)); // override value with post $return = array(); // append to $return foreach ($value as $k => $v) { if (is_numeric($v)) { // find matching $post foreach ($posts as $post) { if ($post->ID == $v) { $return[] = $post; break; } } } else { $return[] = $v; } } // return return $return; }
function get_valid_relationship_field($field) { // force array $field['post_type'] = acf_force_type_array($field['post_type']); $field['taxonomy'] = acf_force_type_array($field['taxonomy']); // remove 'all' from post_type if (acf_in_array('all', $field['post_type'])) { $field['post_type'] = array(); } // remove 'all' from taxonomy if (acf_in_array('all', $field['taxonomy'])) { $field['taxonomy'] = array(); } // save_format is now return_format if (!empty($field['result_elements'])) { $field['elements'] = acf_extract_var($field, 'result_elements'); } // return return $field; }
function acf_get_posts($args) { // vars $r = array(); // defaults $args = acf_parse_args($args, array('posts_per_page' => -1, 'post_type' => 'post', 'orderby' => 'menu_order title', 'order' => 'ASC', 'post_status' => 'any', 'suppress_filters' => false, 'update_post_meta_cache' => false)); // find array of post_type $post_types = acf_force_type_array($args['post_type']); // attachment doesn't work if it is the only item in an array if (count($post_types) == 1) { $args['post_type'] = current($post_types); } // get posts $posts = get_posts($args); // loop foreach ($post_types as $post_type) { // vars $this_posts = array(); $this_group = array(); // populate $this_posts foreach (array_keys($posts) as $key) { if ($posts[$key]->post_type == $post_type) { $this_posts[] = acf_extract_var($posts, $key); } } // bail early if no posts for this post type if (empty($this_posts)) { continue; } // sort into hierachial order! if (is_post_type_hierarchical($post_type)) { // this will fail if a search has taken place because parents wont exist if (empty($args['s'])) { $this_posts = get_page_children(0, $this_posts); } } // populate $this_posts foreach (array_keys($this_posts) as $key) { // extract post $post = acf_extract_var($this_posts, $key); // add to group $this_group[$post->ID] = $post; } // group by post type $post_type_object = get_post_type_object($post_type); $post_type_name = $post_type_object->labels->name; $r[$post_type_name] = $this_group; } // return return $r; }
function acf_get_posts($args) { // vars $r = array(); // defaults $args = acf_parse_args($args, array('posts_per_page' => -1, 'paged' => 0, 'post_type' => 'post', 'orderby' => 'menu_order title', 'order' => 'ASC', 'post_status' => 'any', 'suppress_filters' => false, 'update_post_meta_cache' => false)); // find array of post_type $post_types = acf_force_type_array($args['post_type']); $post_types_labels = acf_get_pretty_post_types($post_types); // attachment doesn't work if it is the only item in an array if (count($post_types) == 1) { $args['post_type'] = current($post_types); } // add filter to orderby post type add_filter('posts_orderby', '_acf_orderby_post_type', 10, 2); // get posts $posts = get_posts($args); // loop foreach ($post_types as $post_type) { // vars $this_posts = array(); $this_group = array(); // populate $this_posts foreach (array_keys($posts) as $key) { if ($posts[$key]->post_type == $post_type) { $this_posts[] = acf_extract_var($posts, $key); } } // bail early if no posts for this post type if (empty($this_posts)) { continue; } // sort into hierachial order! // this will fail if a search has taken place because parents wont exist if (is_post_type_hierarchical($post_type) && empty($args['s'])) { // vars $match_id = $this_posts[0]->ID; $offset = 0; $length = count($this_posts); // reset $this_posts $this_posts = array(); // get all posts $all_args = array_merge($args, array('posts_per_page' => -1, 'paged' => 0, 'post_type' => $post_type)); $all_posts = get_posts($all_args); // loop over posts and find $i foreach ($all_posts as $offset => $p) { if ($p->ID == $match_id) { break; } } // order posts $all_posts = get_page_children(0, $all_posts); for ($i = $offset; $i < $offset + $length; $i++) { $this_posts[] = acf_extract_var($all_posts, $i); } } // populate $this_posts foreach (array_keys($this_posts) as $key) { // extract post $post = acf_extract_var($this_posts, $key); // add to group $this_group[$post->ID] = $post; } // group by post type $post_type_name = $post_types_labels[$post_type]; $r[$post_type_name] = $this_group; } // return return $r; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert values to int $value = array_map('intval', $value); // load posts in 1 query to save multiple DB calls from following code $posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'attachment', 'post_status' => 'any', 'post__in' => $value, 'orderby' => 'post__in')); foreach ($value as $k => $v) { // get post $post = get_post($v); // create $attachment $a = array('ID' => $post->ID, 'id' => $post->ID, 'alt' => get_post_meta($post->ID, '_wp_attachment_image_alt', true), 'title' => $post->post_title, 'caption' => $post->post_excerpt, 'description' => $post->post_content, 'mime_type' => $post->post_mime_type, 'type' => 'file', 'url' => ''); // image if (strpos($a['mime_type'], 'image') !== false) { // type $a['type'] = 'image'; // url $src = wp_get_attachment_image_src($a['ID'], 'full'); $a['url'] = $src[0]; $a['width'] = $src[1]; $a['height'] = $src[2]; // find all image sizes $sizes = get_intermediate_image_sizes(); // sizes if (!empty($sizes)) { $a['sizes'] = array(); foreach ($sizes as $size) { // url $src = wp_get_attachment_image_src($a['ID'], $size); // add src $a['sizes'][$size] = $src[0]; $a['sizes'][$size . '-width'] = $src[1]; $a['sizes'][$size . '-height'] = $src[2]; } // foreach } // if } else { // is file $src = wp_get_attachment_url($a['ID']); $a['url'] = $src; } $value[$k] = $a; } // foreach // return return $value; }
function format_value_for_api($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert values to int $value = array_map('intval', $value); // load posts if needed if ($field['return_format'] == 'object') { // get posts $value = $this->get_terms($value, $field["taxonomy"]); } // convert back from array if neccessary if ($field['field_type'] == 'select' || $field['field_type'] == 'radio') { $value = array_shift($value); } // return return $value; }
function get_posts($value) { // force value to array $value = acf_force_type_array($value); // get selected post ID's $post_ids = array(); foreach ($value as $v) { if (is_numeric($v)) { $post_ids[] = intval($v); } } // load posts in 1 query to save multiple DB calls from following code if (count($post_ids) > 1) { get_posts(array('posts_per_page' => -1, 'post_type' => acf_get_post_types(), 'post_status' => 'any', 'post__in' => $post_ids)); } // vars $posts = array(); // update value to include $post foreach ($value as $v) { if (is_numeric($v)) { if ($post = get_post($v)) { $posts[] = $post; } } else { $posts[] = $v; } } // return return $posts; }
function acf_get_valid_terms($terms = false, $taxonomy = 'category') { // bail early if function does not yet exist or if (!function_exists('wp_get_split_term') || empty($terms)) { return $terms; } // vars $is_array = is_array($terms); // force into array $terms = acf_force_type_array($terms); // force ints $terms = array_map('intval', $terms); // attempt to find new terms foreach ($terms as $i => $term_id) { $new_term_id = wp_get_split_term($term_id, $taxonomy); if ($new_term_id) { $terms[$i] = $new_term_id; } } // revert array if needed if (!$is_array) { $terms = $terms[0]; } // return return $terms; }
function load_value($value, $post_id, $field) { // bail early if no value if (empty($value) || empty($field['layouts'])) { return $value; } // value must be an array $value = acf_force_type_array($value); // vars $rows = array(); // populate $layouts $layouts = array(); foreach (array_keys($field['layouts']) as $i) { // get layout $layout = $field['layouts'][$i]; // append to $layouts $layouts[$layout['name']] = $layout['sub_fields']; } // loop through rows foreach ($value as $i => $l) { // append to $values $rows[$i] = array(); $rows[$i]['acf_fc_layout'] = $l; // bail early if layout deosnt contain sub fields if (empty($layouts[$l])) { continue; } // get layout $layout = $layouts[$l]; // loop through sub fields foreach (array_keys($layout) as $j) { // get sub field $sub_field = $layout[$j]; // update full name $sub_field['name'] = "{$field['name']}_{$i}_{$sub_field['name']}"; // get value $sub_value = acf_get_value($post_id, $sub_field); // add value $rows[$i][$sub_field['key']] = $sub_value; } // foreach } // foreach // return return $rows; }
function render_field($field) { // convert value to array $field['value'] = acf_force_type_array($field['value']); // add empty value (allows '' to be selected) if (empty($field['value'])) { $field['value'][''] = ''; } // placeholder if (empty($field['placeholder'])) { $field['placeholder'] = __("Select", 'acf'); } // vars $atts = array('id' => $field['id'], 'class' => $field['class'], 'name' => $field['name'], 'data-ui' => $field['ui'], 'data-ajax' => $field['ajax'], 'data-multiple' => $field['multiple'], 'data-placeholder' => $field['placeholder'], 'data-allow_null' => $field['allow_null']); // hidden input if ($field['ui']) { acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'], 'name' => $field['name'], 'value' => implode(',', $field['value']))); } elseif ($field['multiple']) { acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); } // ui if ($field['ui']) { $atts['disabled'] = 'disabled'; $atts['class'] .= ' acf-hidden'; } // multiple if ($field['multiple']) { $atts['multiple'] = 'multiple'; $atts['size'] = 5; $atts['name'] .= '[]'; } // special atts foreach (array('readonly', 'disabled') as $k) { if (!empty($field[$k])) { $atts[$k] = $k; } } // vars $els = array(); $choices = array(); // loop through values and add them as options if (!empty($field['choices'])) { foreach ($field['choices'] as $k => $v) { if (is_array($v)) { // optgroup $els[] = array('type' => 'optgroup', 'label' => $k); if (!empty($v)) { foreach ($v as $k2 => $v2) { $els[] = array('type' => 'option', 'value' => $k2, 'label' => $v2, 'selected' => in_array($k2, $field['value'])); $choices[] = $k2; } } $els[] = array('type' => '/optgroup'); } else { $els[] = array('type' => 'option', 'value' => $k, 'label' => $v, 'selected' => in_array($k, $field['value'])); $choices[] = $k; } } } // prepende orphans /* if( !empty($field['value']) ) { foreach( $field['value'] as $v ) { if( empty($v) ) { continue; } if( !in_array($v, $choices) ) { array_unshift( $els, array( 'type' => 'option', 'value' => $v, 'label' => $v, 'selected' => true ) ); } } } */ // null if ($field['allow_null']) { array_unshift($els, array('type' => 'option', 'value' => '', 'label' => '- ' . $field['placeholder'] . ' -')); } // html echo '<select ' . acf_esc_attr($atts) . '>'; // construct html if (!empty($els)) { foreach ($els as $el) { // extract type $type = acf_extract_var($el, 'type'); if ($type == 'option') { // get label $label = acf_extract_var($el, 'label'); // validate selected if (acf_extract_var($el, 'selected')) { $el['selected'] = 'selected'; } // echo echo '<option ' . acf_esc_attr($el) . '>' . $label . '</option>'; } else { // echo echo '<' . $type . ' ' . acf_esc_attr($el) . '>'; } } } echo '</select>'; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_force_type_array($value); // convert values to int $value = array_map('intval', $value); // load posts in 1 query to save multiple DB calls from following code $posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'attachment', 'post_status' => 'any', 'post__in' => $value, 'orderby' => 'post__in')); // reset value $value = array(); // populate value foreach ($posts as $post) { $value[] = acf_get_attachment($post); } // return return $value; }
function render_field($field) { // force value to array $field['value'] = acf_force_type_array($field['value']); // convert values to int $field['value'] = array_map('intval', $field['value']); ?> <div class="acf-taxonomy-field" data-load_save="<?php echo $field['load_save_terms']; ?> "> <?php if ($field['field_type'] == 'select') { $field['multiple'] = 0; $this->render_field_select($field); } elseif ($field['field_type'] == 'multi_select') { $field['multiple'] = 1; $this->render_field_select($field); } elseif ($field['field_type'] == 'radio') { $this->render_field_checkbox($field); } elseif ($field['field_type'] == 'checkbox') { $this->render_field_checkbox($field); } ?> </div><?php }
function render_field($field) { // echo "<pre>"; // print_r($field); // echo "</pre>"; $taxonomies = array(); $taxonomies = acf_force_type_array($taxonomies); $taxonomies = acf_get_pretty_taxonomies($taxonomies); $all_taxonomies = acf_get_taxonomy_terms(); $selected_taxonomies = array(); $terms = array(); $slug_name = $field['choices']; foreach ($field['choices'] as $k1 => $v1) { $terms = array_merge($terms, get_terms($v1, array('hide_empty' => false))); foreach ($taxonomies as $k2 => $v2) { if ($v1 == $k2) { $field['choices'][$k1] = $v2; } } } foreach ($field['choices'] as $k1 => $v1) { foreach ($all_taxonomies as $k2 => $v2) { if ($v1 == $k2) { $selected_taxonomies[$v1] = $all_taxonomies[$k2]; } } } $field['choices'] = $selected_taxonomies; // convert value to array // $field['value'] = acf_force_type_array($field['value']); // add empty value (allows '' to be selected) if (empty($field['value'])) { $field['value'][''] = ''; $field['value']['cat'] = ''; } // placeholder if (empty($field['placeholder'])) { $field['placeholder'] = __("Select", 'acf'); } // vars $atts = array('id' => $field['id'], 'class' => $field['class'] . ' js-multi-taxonomy-select2', 'name' => $field['name'], 'data-ui' => $field['ui'], 'data-ajax' => $field['ajax'], 'data-multiple' => $field['multiple'], 'data-placeholder' => $field['placeholder'], 'data-allow_null' => $field['allow_null']); // hidden input if ($field['ui']) { acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'], 'name' => $field['name'], 'value' => implode(',', $field['value']))); } elseif ($field['multiple']) { acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); } // ui if ($field['ui']) { $atts['disabled'] = 'disabled'; $atts['class'] .= ' acf-hidden'; } // multiple if ($field['multiple']) { $atts['multiple'] = 'multiple'; $atts['size'] = 5; $atts['name'] .= '[]'; } // special atts foreach (array('readonly', 'disabled') as $k) { if (!empty($field[$k])) { $atts[$k] = $k; } } // vars $els = array(); $choices = array(); if ($field['data_type']) { // loop through values and add them as options if (!empty($field['choices'])) { foreach ($field['choices'] as $k => $v) { if (is_array($v)) { // optgroup $els[] = array('type' => 'optgroup', 'label' => $k); if (!empty($v)) { foreach ($v as $k2 => $v2) { if ($field['type_value']) { foreach ($terms as $key => $val) { if ($val->name == $v2) { $els[] = array('type' => 'option', 'value' => $val->term_id, 'label' => $v2, 'selected' => $slct = $val->term_id == $field['value'] ? "selected" : ""); } } } else { $els[] = array('type' => 'option', 'value' => $k2, 'label' => $v2, 'selected' => $slct = $k2 == $field['value'] ? "selected" : ""); } $choices[] = $k2; } } $els[] = array('type' => '/optgroup'); } else { $els[] = array('type' => 'option', 'value' => $k, 'label' => $v, 'selected' => $slct = $k == $field['value'] ? "selected" : ""); $choices[] = $k; } } } // null if ($field['allow_null']) { array_unshift($els, array('type' => 'option', 'value' => '', 'label' => '- ' . $field['placeholder'] . ' -')); } // if ($field['data_type']) { // html echo '<select ' . acf_esc_attr($atts) . '>'; // construct html if (!empty($els)) { foreach ($els as $el) { // extract type $type = acf_extract_var($el, 'type'); if ($type == 'option') { // get label $label = acf_extract_var($el, 'label'); // validate selected if (acf_extract_var($el, 'selected')) { $el['selected'] = 'selected'; } echo acf_esc_attr($el); echo '<option ' . acf_esc_attr($el) . '>' . $label . '</option>'; } else { echo '<' . $type . ' ' . acf_esc_attr($el) . '>'; } } } } else { $els = '['; $i = 0; foreach ($field['choices'] as $k => $v) { if (is_array($v)) { $els .= '['; foreach ($v as $k2 => $v2) { foreach ($terms as $key => $val) { if ($val->name == $v2) { $els .= '["' . $v2 . '","' . $val->term_id . '",' . '"' . $slug_name[$i] . '"],'; } } } $els .= '],'; } $i++; } $els .= ']'; echo '<div class="h">'; echo '<div class="Taxonomies">'; echo '<label class="" for="' . $field['key'] . '">Taxonomies</label> '; echo '<select class="js-multi-taxonomy-select2 taxonomiesF" name="' . $field['name'] . '" id="' . $field['key'] . '-taxonomies">'; $i = 0; foreach ($field['choices'] as $k => $v) { echo '<option value="' . $i++ . '">' . $k . '</option>'; } echo '</select>'; echo '</div>'; echo '<div class="Terms">'; echo '<label class="" >Terms</label> '; echo '<input name="' . $field['name'] . '[cat]" id="' . $field['key'] . '-terms" class="js-multi-taxonomy-select2 termsF" value="" />'; echo '</div>'; echo '</div>'; // } echo ' <script> (function($){ var arr = ' . $els . '; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ $(".TaxonomiesF").each(function(){ $(this).ready(function(){ $(this).closest(":has(.h .Terms .termsF)").find(".termsF").attr("name","' . $field['name'] . '[" + arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()][0][2]+ "]") $(this).closest(":has(.h .Terms .termsF)").find(".termsF").select2({ multiple: true, query: function (query) { var data = {results: []}, i; for (i in arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()] ) { data.results.push({id: arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()][i][1] , text: arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()][i][0]}); } query.callback(data); }, initSelection : function (element, callback) { var elementText = $(element).val(); var data = {id: elementText, text: elementText}; callback(data); } }); }); }); //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ })(jQuery); </script> '; } }