function render_field($field) { // decode value (convert to array) $field['value'] = acf_get_array($field['value'], false); // hiden input acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); // vars $i = 0; $li = ''; $all_checked = true; // 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); // is choice selected? if (in_array($value, $field['value'])) { $atts['checked'] = 'checked'; } else { $all_checked = false; } if (isset($field['disabled']) && acf_in_array($value, $field['disabled'])) { $atts['disabled'] = 'disabled'; } // 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; } // append HTML $li .= '<li><label><input ' . acf_esc_attr($atts) . '/>' . $label . '</label></li>'; } // toggle all if ($field['toggle']) { // vars $label = __("Toggle All", 'acf'); $atts = array('type' => 'checkbox', 'class' => 'acf-checkbox-toggle'); // custom label if (is_string($field['toggle'])) { $label = $field['toggle']; } // checked if ($all_checked) { $atts['checked'] = 'checked'; } // append HTML $li = '<li><label><input ' . acf_esc_attr($atts) . '/>' . $label . '</label></li>' . $li; } } // class $field['class'] .= ' acf-checkbox-list'; $field['class'] .= $field['layout'] == 'horizontal' ? ' acf-hl' : ' acf-bl'; // return echo '<ul ' . acf_esc_attr(array('class' => $field['class'])) . '>' . $li . '</ul>'; }
public function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_get_array($value); // convert values to int $value = array_map('intval', $value); // load posts if needed if ($field['return_format'] == 'object') { $value = ACF_API_Fields()->api->get_posts($field, array('include' => $value)); } // convert back from array if neccessary if (!$field['multiple']) { $value = array_shift($value); } // return value return $value; }
function update_value($value, $post_id, $field) { // validate if (empty($value)) { return $value; } // force value to array $value = acf_get_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 update_value($value, $post_id, $field) { if ($post_id === $GLOBALS['post_id']) { $new_value = $this->clean_post_value($value); $old_value = $this->clean_post_value(get_field($field['key'])); $new_values = array(); $missing_values = array(); if (!empty($new_value) && !empty($old_value)) { $new_values = array_diff($new_value, $old_value); $missing_values = array_diff($old_value, $new_value); } else { if (!empty($new_value)) { $new_values = array_map('strval', $new_value); } if (!empty($old_value)) { $missing_values = array_map('strval', $old_value); } } foreach ($new_values as $value_add) { $existing_value = get_field($field['key'], $value_add); if (!empty($existing_value)) { $existing_value[] = $post_id; } else { $existing_value = array($post_id); } update_field($field['key'], $existing_value, $value_add); } foreach ($missing_values as $value_remove) { $existing_value = get_field($field['key'], $value_remove); if (!empty($existing_value)) { if ($existing_value[0] instanceof WP_Post) { $old_existing_value = $existing_value; $existing_value = array(); foreach ($old_existing_value as $post) { if ($post->ID !== $post_id) { $existing_value[] = $post; } } } else { $existing_value = array_diff($existing_val, array($post_id)); } update_field($field['key'], $existing_value, $value_remove); } } } // validate if (empty($value)) { return $value; } // force value to array $value = acf_get_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) { $taxonomies = array(); $taxonomies = acf_get_array($taxonomies); $taxonomies = acf_get_pretty_taxonomies($taxonomies); $taxonomy_terms = acf_get_taxonomy_terms(); $selected_taxonomies = array(); $terms = array(); $slug_name = !empty($field['choices']) ? $field['choices'] : array_keys(acf_get_pretty_taxonomies()); if ($field['tax_type'] == 'Term') { // select terms foreach ($slug_name 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 ($taxonomy_terms as $k2 => $v2) { if ($v1 == $k2) { $selected_taxonomies[$v1] = $taxonomy_terms[$k2]; } } } } else { //select taxonomies $taxonomies = array(); foreach ($slug_name as $tax_name) { // only use allowed taxonomies $taxonomies[$tax_name] = get_taxonomy($tax_name); } foreach ($taxonomies as $taxonomy) { $selected_taxonomies[$taxonomy->name] = $taxonomy->label; } } $field['choices'] = $selected_taxonomies; // 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-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'; } // 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) { // allowed taxonomies if (is_array($v)) { // optgroup $els[] = array('type' => 'optgroup', 'label' => $k); if (!empty($v)) { foreach ($v as $k2 => $v2) { $strip_v2_hyphen = preg_replace('#-\\s?#', '', $v2); // Child categories have hyphens before the name, we need to remove them in order to match them if ($field['type_value']) { // value = term ID foreach ($terms as $key => $val) { if ($val->name == $strip_v2_hyphen) { $els[] = array('type' => 'option', 'value' => $val->term_id, 'label' => $v2, 'selected' => $slct = $val->term_id == $field['value'] ? "selected" : ""); } } } else { // value = term slug preg_match('#(?::)(.*)#', $k2, $value); // originally returns 'taxonomy:term-slug' this removes 'taxonomy:' $els[] = array('type' => 'option', 'value' => $value[1], 'label' => $v2, 'selected' => $slct = $value[1] == $field['value'] ? "selected" : ""); } $choices[] = $k2; } } $els[] = array('type' => '/optgroup'); } else { // value = Taxonomy Slug $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'] . ' -')); } // 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) . '>'; } } } echo '</select>'; }
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_get_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 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_get_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 get_clone_setting_choices($value) { // vars $choices = array(); // bail early if no $value if (empty($value)) { return $choices; } // force value to array $value = acf_get_array($value); // loop foreach ($value as $v) { $choices[$v] = $this->get_clone_setting_choice($v); } // return return $choices; }
function render_field($field) { // convert value to array $field['value'] = acf_get_array($field['value'], false); // 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']); // 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; } } } // hidden input if ($field['ui']) { // restirct value $v = array_intersect($field['value'], $choices); if ($field['multiple']) { $v = implode('||', $v); } else { $v = acf_maybe_get($v, 0, ''); } acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'] . '-input', 'name' => $field['name'], 'value' => $v)); } elseif ($field['multiple']) { acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'] . '-input', 'name' => $field['name'])); } // 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>'; }
public function render_field($field) { // convert value to array $field['value'] = acf_get_array($field['value'], false); // 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-multiple' => $field['multiple'], 'data-placeholder' => $field['placeholder'], 'data-allow_null' => $field['allow_null']); // 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(); $posttypes = $this->post_type_options(); foreach ($posttypes as $pt) { $els[] = array('type' => 'option', 'value' => $pt['value'], 'label' => $pt['label'], 'selected' => in_array($pt['value'], $field['value'])); } // 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 display_quickedit_field($column, $post_type, $field) { ?> <fieldset class="inline-edit-col-left inline-edit-<?php echo $post_type; ?> "><?php ?> <div class="inline-edit-col column-<?php echo $column; ?> "><?php ?> <label class="inline-edit-group"><?php ?> <span class="title"><?php echo $field['label']; ?> </span><?php ?> <span class="input-text-wrap"><?php $input_atts = array('data-acf-field-key' => $field['key'], 'name' => $this->post_field_prefix . $column); switch ($field['type']) { case 'checkbox': $input_atts += array('class' => 'acf-quick-edit', 'id' => $this->post_field_prefix . $column); $field['value'] = \acf_get_array($field['value'], false); $input_atts['name'] .= '[]'; foreach ($field['choices'] as $value => $label) { $atts = array('type' => 'checkbox', 'value' => $value) + $input_atts; if (in_array($value, $field['value'])) { $atts['checked'] = 'checked'; } else { $all_checked = false; } $atts['id'] .= '-' . $value; echo '<label><input ' . \acf_esc_attr($atts) . '/>' . $label . '</label>'; } break; case 'select': $input_atts += array('class' => 'acf-quick-edit widefat', 'id' => $this->post_field_prefix . $column); if ($field['multiple']) { $input_atts['multiple'] = 'multiple'; } ?> <select <?php echo \acf_esc_attr($input_atts); ?> ><?php if ($field['allow_null']) { echo '<option value="">' . '- ' . \__('Select', 'acf-quick-edit-fields') . ' -'; } foreach ($field['choices'] as $name => $label) { echo '<option value="' . $name . '">' . $label; } ?> </select><?php break; case 'radio': ?> <ul class="acf-radio-list<?php echo $field['other_choice'] ? ' other' : ''; ?> " data-acf-field-key="<?php echo $field['key']; ?> "><?php foreach ($field['choices'] as $name => $value) { ?> <li><label for="<?php echo $this->post_field_prefix . $column . '-' . $name; ?> "><?php ?> <input id="<?php echo $this->post_field_prefix . $column . '-' . $name; ?> " type="radio" value="<?php echo $name; ?> " class="acf-quick-edit" data-acf-field-key="<?php echo $field['key']; ?> " name="<?php echo $this->post_field_prefix . $column; ?> " /><?php echo $value; ?> </label></li><?php } if ($field['other_choice']) { ?> <li><label for="<?php echo $this->post_field_prefix . $column . '-other'; ?> "><?php ?> <input id="<?php echo $this->post_field_prefix . $column . '-other'; ?> " type="radio" value="other" class="acf-quick-edit" data-acf-field-key="<?php echo $field['key']; ?> " name="<?php echo $this->post_field_prefix . $column; ?> " /><?php ?> <input type="text" class="acf-quick-edit" data-acf-field-key="<?php echo $field['key']; ?> " name="<?php echo $this->post_field_prefix . $column; ?> " style="width:initial" /><?php ?> </li><?php ?> </label><?php } ?> </ul><?php break; case 'true_false': ?> <ul class="acf-radio-list" data-acf-field-key="<?php echo $field['key']; ?> "><?php ?> <li><label for="<?php echo $this->post_field_prefix . $column; ?> -yes"><?php ?> <input id="<?php echo $this->post_field_prefix . $column; ?> -yes" type="radio" value="1" class="acf-quick-edit" data-acf-field-key="<?php echo $field['key']; ?> " name="<?php echo $this->post_field_prefix . $column; ?> " /><?php _e('Yes', 'acf-quick-edit-fields'); ?> </label></li><?php ?> <li><label for="<?php echo $this->post_field_prefix . $column; ?> -no"><?php ?> <input id="<?php echo $this->post_field_prefix . $column; ?> -no" type="radio" value="0" class="acf-quick-edit" data-acf-field-key="<?php echo $field['key']; ?> " name="<?php echo $this->post_field_prefix . $column; ?> " /><?php \_e('No', 'acf-quick-edit-fields'); ?> </label></li><?php ?> </ul><?php break; case 'number': $input_atts += array('class' => 'acf-quick-edit', 'type' => 'number', 'min' => $field['min'], 'max' => $field['max'], 'step' => $field['step']); echo '<input ' . \acf_esc_attr($input_atts) . ' />'; break; case 'date_picker': $input_atts += array('class' => 'acf-quick-edit acf-quick-edit-' . $field['type'], 'type' => 'text', 'data-display_format' => \acf_convert_date_to_js($field['display_format']), 'data-first_day' => $field['first_day']); echo '<input ' . \acf_esc_attr($input_atts) . ' />'; break; case 'textarea': $input_atts += array('class' => 'acf-quick-edit acf-quick-edit-' . $field['type'], 'type' => 'text'); echo '<textarea ' . \acf_esc_attr($input_atts) . '>' . esc_textarea($field['value']) . '</textarea>'; break; default: $input_atts += array('class' => 'acf-quick-edit acf-quick-edit-' . $field['type'], 'type' => 'text'); echo '<input ' . \acf_esc_attr($input_atts) . ' />'; break; } ?> </span><?php ?> </label><?php ?> </div><?php ?> </fieldset><?php }
if ($width['post_type']) { if (!empty($field['post_type'])) { $post_types = $field['post_type']; } else { $post_types = acf_get_post_types(); } $post_types = acf_get_pretty_post_types($post_types); } // taxonomy filter $taxonomies = array(); $term_groups = array(); if ($width['taxonomy']) { // taxonomies if (!empty($field['taxonomy'])) { // get the field's terms $term_groups = acf_get_array($field['taxonomy']); $term_groups = acf_decode_taxonomy_terms($term_groups); // update taxonomies $taxonomies = array_keys($term_groups); } elseif (!empty($field['post_type'])) { // loop over post types and find connected taxonomies foreach ($field['post_type'] as $post_type) { $post_taxonomies = get_object_taxonomies($post_type); // bail early if no taxonomies if (empty($post_taxonomies)) { continue; } foreach ($post_taxonomies as $post_taxonomy) { if (!in_array($post_taxonomy, $taxonomies)) { $taxonomies[] = $post_taxonomy; }
function render_field($field) { // echo "<pre>"; // print_r($field); // echo "</pre>"; $taxonomies = array(); $taxonomies = acf_get_array($taxonomies); $taxonomies = acf_get_pretty_taxonomies($taxonomies); $all_taxonomies = acf_get_taxonomy_terms(); $selected_taxonomies = array(); $terms = array(); $slug_name = !empty($field['choices']) ? $field['choices'] : array_keys(acf_get_pretty_taxonomies()); foreach ($slug_name 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'] . ' -')); } // 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) . '>'; } } } echo '</select>'; } 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> '; } }
function render_field($field) { // vars $sub_fields = $field['sub_fields']; $value = acf_get_array($field['value']); $show_order = true; $show_add = true; $show_remove = true; // bail early if no sub fields if (empty($sub_fields)) { return; } // div $div = array('class' => 'acf-repeater', 'data-min' => $field['min'], 'data-max' => $field['max']); // empty if (empty($value)) { $div['class'] .= ' -empty'; } // If there are less values than min, populate the extra values if ($field['min']) { $value = array_pad($value, $field['min'], array()); } // If there are more values than man, remove some values if ($field['max']) { $value = array_slice($value, 0, $field['max']); // if max 1 row, don't show order if ($field['max'] == 1) { $show_order = false; } // if max == min, don't show add or remove buttons if ($field['max'] <= $field['min']) { $show_remove = false; $show_add = false; } } // setup values for row clone $value['acfcloneindex'] = array(); // button label if ($field['button_label'] === '') { $field['button_label'] = __('Add Row', 'acf'); } // field wrap $el = 'td'; $before_fields = ''; $after_fields = ''; if ($field['layout'] == 'row') { $el = 'div'; $before_fields = '<td class="acf-fields -left">'; $after_fields = '</td>'; } elseif ($field['layout'] == 'block') { $el = 'div'; $before_fields = '<td class="acf-fields">'; $after_fields = '</td>'; } // layout $div['class'] .= ' -' . $field['layout']; // hidden input acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); // collapsed if ($field['collapsed']) { // add target class foreach ($sub_fields as $i => $sub_field) { // bail early if no match if ($sub_field['key'] !== $field['collapsed']) { continue; } // class $sub_field['wrapper']['class'] .= ' -collapsed-target'; // update $sub_fields[$i] = $sub_field; } } ?> <div <?php acf_esc_attr_e($div); ?> > <table class="acf-table"> <?php if ($field['layout'] == 'table') { ?> <thead> <tr> <?php if ($show_order) { ?> <th class="acf-row-handle"></th> <?php } ?> <?php foreach ($sub_fields as $sub_field) { // prepare field (allow sub fields to be removed) $sub_field = acf_prepare_field($sub_field); // bail ealry if no field if (!$sub_field) { continue; } // vars $atts = array(); $atts['class'] = 'acf-th'; $atts['data-name'] = $sub_field['_name']; $atts['data-type'] = $sub_field['type']; $atts['data-key'] = $sub_field['key']; // Add custom width if ($sub_field['wrapper']['width']) { $atts['data-width'] = $sub_field['wrapper']['width']; $atts['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;'; } ?> <th <?php echo acf_esc_attr($atts); ?> > <?php echo acf_get_field_label($sub_field); ?> <?php if ($sub_field['instructions']) { ?> <p class="description"><?php echo $sub_field['instructions']; ?> </p> <?php } ?> </th> <?php } ?> <?php if ($show_remove) { ?> <th class="acf-row-handle"></th> <?php } ?> </tr> </thead> <?php } ?> <tbody> <?php foreach ($value as $i => $row) { $row_class = 'acf-row'; if ($i === 'acfcloneindex') { $row_class .= ' acf-clone'; } elseif (acf_is_row_collapsed($field['key'], $i)) { $row_class .= ' -collapsed'; } ?> <tr class="<?php echo $row_class; ?> " data-id="<?php echo $i; ?> "> <?php if ($show_order) { ?> <td class="acf-row-handle order" title="<?php _e('Drag to reorder', 'acf'); ?> "> <?php if ($field['collapsed']) { ?> <a class="acf-icon -collapse small" href="#" data-event="collapse-row" title="<?php _e('Click to toggle', 'acf'); ?> "></a> <?php } ?> <span><?php echo intval($i) + 1; ?> </span> </td> <?php } ?> <?php echo $before_fields; ?> <?php foreach ($sub_fields as $sub_field) { // prevent repeater field from creating multiple conditional logic items for each row if ($i !== 'acfcloneindex') { $sub_field['conditional_logic'] = 0; } // add value if (isset($row[$sub_field['key']])) { // this is a normal value $sub_field['value'] = $row[$sub_field['key']]; } elseif (isset($sub_field['default_value'])) { // no value, but this sub field has a default value $sub_field['value'] = $sub_field['default_value']; } // update prefix to allow for nested values $sub_field['prefix'] = $field['name'] . '[' . $i . ']'; // render input acf_render_field_wrap($sub_field, $el); ?> <?php } ?> <?php echo $after_fields; ?> <?php if ($show_remove) { ?> <td class="acf-row-handle remove"> <a class="acf-icon -plus small" href="#" data-event="add-row" title="<?php _e('Add row', 'acf'); ?> "></a> <a class="acf-icon -minus small" href="#" data-event="remove-row" title="<?php _e('Remove row', 'acf'); ?> "></a> </td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php if ($show_add) { ?> <ul class="acf-actions acf-hl"> <li> <a class="acf-button button button-primary" data-event="add-row"><?php echo $field['button_label']; ?> </a> </li> </ul> <?php } ?> </div> <?php }
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; }
function get_posts($value, $field) { // force value to array $value = acf_get_array($value); // get selected post ID's $post__in = array(); foreach ($value as $k => $v) { if (is_numeric($v)) { // append to $post__in $post__in[] = (int) $v; } } // bail early if no posts if (empty($post__in)) { return $value; } // get posts $posts = acf_get_posts(array('post__in' => $post__in, 'post_type' => $field['post_type'])); // override value with post $return = array(); // append to $return foreach ($value as $k => $v) { if (is_numeric($v)) { // extract first post $post = array_shift($posts); // append if ($post) { $return[] = $post; } } else { $return[] = $v; } } // return return $return; }
function delete_sub_row($selector, $i = 1, $post_id = false) { // vars $sub_field = false; $i--; // filter post_id $post_id = acf_get_valid_post_id($post_id); // get sub field if (is_array($selector)) { $sub_field = acf_maybe_get_sub_field($selector, $post_id, false); } else { $sub_field = get_row_sub_field($selector); } // bail early if no sub field if (!$sub_field) { return false; } // get raw value $value = acf_get_value($post_id, $sub_field); // ensure array $value = acf_get_array($value); // append unset($value[$i]); // update return acf_update_value($value, $post_id, $sub_field); }
function get_attachments($value) { // bail early if no value if (empty($value)) { return false; } // force value to array $post__in = acf_get_array($value); // get posts $posts = acf_get_posts(array('post_type' => 'attachment', 'post__in' => $post__in)); // return return $posts; }
function load_value($value, $post_id, $field) { // bail early if no value if (empty($field['layouts'])) { return $value; } // value must be an array $value = acf_get_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 $value['rows'] = $rows; $customized_value = get_post_meta($post_id, 'acf_widget_area_is_customized', true); if (!empty($customized_value) && isset($customized_value[$field['registered_widget_area']])) { $value['internal_values']['customized'] = isset($customized_value[$field['registered_widget_area']][$field['key']]) ? $customized_value[$field['registered_widget_area']][$field['key']] : 'no'; } else { $value['internal_values']['customized'] = 'no'; } return $value; }
function render_field($field) { // convert $field['value'] = acf_get_array($field['value'], false); $field['choices'] = acf_get_array($field['choices']); // placeholder if (empty($field['placeholder'])) { $field['placeholder'] = __("Select", 'acf'); } // add empty value (allows '' to be selected) if (!count($field['value'])) { $field['value'][''] = ''; } // null if ($field['allow_null'] && !$field['multiple']) { $prepend = array('' => '- ' . $field['placeholder'] . ' -'); $field['choices'] = $prepend + $field['choices']; } // 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']); // 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; } } // hidden input if ($field['ui']) { $v = $field['value']; if ($field['multiple']) { $v = implode('||', $v); } else { $v = acf_maybe_get($v, 0, ''); } acf_hidden_input(array('id' => $field['id'] . '-input', 'name' => $field['name'], 'value' => $v)); } elseif ($field['multiple']) { acf_hidden_input(array('id' => $field['id'] . '-input', 'name' => $field['name'])); } // open echo '<select ' . acf_esc_attr($atts) . '>'; // walk $this->walk($field['choices'], $field['value']); // close 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_get_array($value); // convert values to int $value = array_map('intval', $value); // load posts if needed if ($field['return_format'] == 'object') { // get posts $value = acf_get_posts(array('post__in' => $value, 'post_type' => $field['post_type'])); } // convert back from array if neccessary if (!$field['multiple']) { $value = array_shift($value); } // return value return $value; }
function format_value($value, $post_id, $field) { // bail early if no value if (empty($value)) { return $value; } // force value to array $value = acf_get_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_get_array($field['value']); // convert values to int $field['value'] = array_map('intval', $field['value']); // vars $div = array('class' => 'acf-taxonomy-field acf-soh', 'data-save' => $field['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-icon -plus acf-js-tooltip small acf-soh-target" data-name="add" title="<?php echo sprintf(__('Add new %s ', 'acf'), $taxonomy->labels->singular_name); ?> "></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 get_valid_relationship_field($field) { // force array $field['post_type'] = acf_get_array($field['post_type']); $field['taxonomy'] = acf_get_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 get_posts($value, $field) { // force value to array $value = acf_get_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, 'post_type' => $field['post_type'])); // 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; }