<?php

function get_field($selector, $post_id = false, $format_value = true)
{
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_maybe_get_field($selector, $post_id);
    if (!$field) {
        $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => ''));
        $format_value = false;
    }
    $value = acf_get_value($post_id, $field);
    if ($format_value) {
        $value = acf_format_value($value, $post_id, $field);
    }
    return $value;
}
function the_field($selector, $post_id = false, $format_value = true)
{
    $value = get_field($selector, $post_id, $format_value);
    if (is_array($value)) {
        $value = @implode(', ', $value);
    }
    echo $value;
}
function get_field_object($selector, $post_id = false, $format_value = true, $load_value = true)
{
    if (is_array($format_value)) {
        extract($format_value);
    }
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_maybe_get_field($selector, $post_id);
    if (!$field) {
        return false;
    }
    if ($load_value) {
        $field['value'] = acf_get_value($post_id, $field);
    }
    if ($format_value) {
        $field['value'] = acf_format_value($field['value'], $post_id, $field);
    }
    return $field;
}
function acf_maybe_get_field($selector, $post_id = false, $strict = true)
{
    acf_init();
    if (acf_is_field_key($selector)) {
        return acf_get_field($selector);
    }
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_get_meta_field($selector, $post_id);
    if ($field) {
        return $field;
    }
    if (!$strict) {
        return acf_get_field($selector);
    }
    return false;
}
function acf_maybe_get_sub_field($selectors, $post_id = false, $strict = true)
{
    if (!is_array($selectors) || count($selectors) < 3) {
        return false;
    }
    $offset = acf_get_setting('row_index_offset');
    $selector = acf_extract_var($selectors, 0);
    $selectors = array_values($selectors);
    $field = acf_maybe_get_field($selector, $post_id, $strict);
    if (!$field) {
        return false;
    }
    for ($j = 0; $j < count($selectors); $j += 2) {
        $sub_i = $selectors[$j];
        $sub_s = $selectors[$j + 1];
        $field_name = $field['name'];
        $field = acf_get_sub_field($sub_s, $field);
        if (!$field) {
            return false;
        }
        $field['name'] = $field_name . '_' . ($sub_i - $offset) . '_' . $field['name'];
    }
    return $field;
}
function get_fields($post_id = false, $format_value = true)
{
    $fields = get_field_objects($post_id, $format_value);
    $meta = array();
    if (!$fields) {
        return false;
    }
    foreach ($fields as $k => $field) {
        $meta[$k] = $field['value'];
    }
    return $meta;
}
function get_field_objects($post_id = false, $format_value = true, $load_value = true)
{
    acf_init();
    $post_id = acf_get_valid_post_id($post_id);
    $meta = acf_get_meta($post_id);
    if (empty($meta)) {
        return false;
    }
    $fields = array();
    foreach ($meta as $key => $value) {
        if (!isset($meta["_{$key}"])) {
            continue;
        }
        $field = acf_get_field($meta["_{$key}"]);
        if (!$field || $field['name'] !== $key) {
            continue;
        }
        if ($load_value) {
            $field['value'] = acf_get_value($post_id, $field);
        }
        if ($format_value) {
            $field['value'] = acf_format_value($field['value'], $post_id, $field);
        }
        $fields[$key] = $field;
    }
    if (empty($fields)) {
        return false;
    }
    return $fields;
}
function have_rows($selector, $post_id = false)
{
    $_post_id = $post_id;
    $post_id = acf_get_valid_post_id($post_id);
    $key = "selector={$selector}/post_id={$post_id}";
    $active_loop = acf_get_loop('active');
    $prev_loop = acf_get_loop('previous');
    $new_loop = false;
    $sub_field = false;
    if (!$active_loop) {
        $new_loop = 'parent';
    } elseif ($key !== $active_loop['key']) {
        $sub_field_exists = false;
        $sub_field = acf_get_sub_field($selector, $active_loop['field']);
        if ($sub_field) {
            $sub_field_exists = isset($active_loop['value'][$active_loop['i']][$sub_field['key']]);
        }
        if ($post_id != $active_loop['post_id']) {
            if (empty($_post_id) && $sub_field_exists) {
                $new_loop = 'child';
            } elseif ($prev_loop && $prev_loop['post_id'] == $post_id) {
                acf_remove_loop('active');
                $active_loop = $prev_loop;
            } else {
                $new_loop = 'parent';
            }
        } elseif ($selector != $active_loop['selector']) {
            if ($sub_field_exists) {
                $new_loop = 'child';
            } elseif ($prev_loop && $prev_loop['selector'] == $selector && $prev_loop['post_id'] == $post_id) {
                acf_remove_loop('active');
                $active_loop = $prev_loop;
            } else {
                $new_loop = 'parent';
            }
        }
    }
    if ($new_loop) {
        $args = array('key' => $key, 'selector' => $selector, 'post_id' => $post_id, 'name' => null, 'value' => null, 'field' => null, 'i' => -1);
        if ($new_loop === 'parent') {
            $field = get_field_object($selector, $post_id, false);
            if ($field) {
                $args['field'] = $field;
                $args['value'] = $field['value'];
                $args['name'] = $field['name'];
                unset($args['field']['value']);
            }
        } else {
            $args['field'] = $sub_field;
            $args['value'] = $active_loop['value'][$active_loop['i']][$sub_field['key']];
            $args['name'] = "{$active_loop['name']}_{$active_loop['i']}_{$sub_field['name']}";
            $args['post_id'] = $active_loop['post_id'];
        }
        if (!$args['value'] || !is_array($args['value'])) {
            return false;
        }
        if (acf_get_field_type_prop($args['field']['type'], 'have_rows') === 'single') {
            $args['value'] = array($args['value']);
        }
        $active_loop = acf_add_loop($args);
    }
    if ($active_loop && isset($active_loop['value'][$active_loop['i'] + 1])) {
        return true;
    }
    acf_remove_loop('active');
    return false;
}
function the_row($format = false)
{
    $i = acf_get_loop('active', 'i');
    $i++;
    acf_update_loop('active', 'i', $i);
    return get_row($format);
}
function get_row($format = false)
{
    $loop = acf_get_loop('active');
    if (!$loop) {
        return false;
    }
    $value = acf_maybe_get($loop['value'], $loop['i']);
    if (!$value) {
        return false;
    }
    if ($format) {
        $field = $loop['field'];
        if (acf_get_field_type_prop($field['type'], 'have_rows') === 'single') {
            $value = acf_format_value($value, $loop['post_id'], $field);
        } else {
            $value = acf_format_value($loop['value'], $loop['post_id'], $field);
            $value = acf_maybe_get($value, $loop['i']);
        }
    }
    return $value;
}
function get_row_index()
{
    $i = acf_get_loop('active', 'i');
    $offset = acf_get_setting('row_index_offset');
    return $offset + $i;
}
function the_row_index()
{
    echo get_row_index();
}
function get_row_sub_field($selector)
{
    $row = acf_get_loop('active');
    if (!$row) {
        return false;
    }
    $sub_field = acf_get_sub_field($selector, $row['field']);
    if (!$sub_field) {
        return false;
    }
    $sub_field['name'] = "{$row['name']}_{$row['i']}_{$sub_field['name']}";
    return $sub_field;
}
function get_row_sub_value($selector)
{
    $row = acf_get_loop('active');
    if (!$row) {
        return null;
    }
    if (isset($row['value'][$row['i']][$selector])) {
        return $row['value'][$row['i']][$selector];
    }
    return null;
}
function reset_rows()
{
    acf_remove_loop('active');
    return true;
}
function has_sub_field($field_name, $post_id = false)
{
    $r = have_rows($field_name, $post_id);
    if ($r) {
        the_row();
    }
    return $r;
}
function has_sub_fields($field_name, $post_id = false)
{
    return has_sub_field($field_name, $post_id);
}
function get_sub_field($selector = '', $format_value = true)
{
    $sub_field = get_sub_field_object($selector, $format_value);
    if (!$sub_field) {
        return false;
    }
    return $sub_field['value'];
}
function the_sub_field($field_name, $format_value = true)
{
    $value = get_sub_field($field_name, $format_value);
    if (is_array($value)) {
        $value = implode(', ', $value);
    }
    echo $value;
}
function get_sub_field_object($selector, $format_value = true, $load_value = true)
{
    $row = acf_get_loop('active');
    if (!$row) {
        return false;
    }
    $sub_field = get_row_sub_field($selector);
    if (!$sub_field) {
        return false;
    }
    if ($load_value) {
        $sub_field['value'] = get_row_sub_value($sub_field['key']);
    }
    if ($format_value) {
        $sub_field['value'] = acf_format_value($sub_field['value'], $row['post_id'], $sub_field);
    }
    return $sub_field;
}
function get_row_layout()
{
    $row = get_row();
    if (isset($row['acf_fc_layout'])) {
        return $row['acf_fc_layout'];
    }
    return false;
}
function acf_shortcode($atts)
{
    if (wp_doing_ajax() && !current_user_can('edit_posts')) {
        return;
    }
    extract(shortcode_atts(array('field' => '', 'post_id' => false, 'format_value' => true), $atts));
    $value = get_field($field, $post_id, $format_value);
    if (is_array($value)) {
        $value = @implode(', ', $value);
    }
    return $value;
}
add_shortcode('acf', 'acf_shortcode');
function update_field($selector, $value, $post_id = false)
{
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_maybe_get_field($selector, $post_id, false);
    if (!$field) {
        $field = acf_get_valid_field(array('name' => $selector, 'key' => '', 'type' => ''));
    }
    return acf_update_value($value, $post_id, $field);
}
function update_sub_field($selector, $value, $post_id = false)
{
    $sub_field = false;
    if (is_array($selector)) {
        $post_id = acf_get_valid_post_id($post_id);
        $sub_field = acf_maybe_get_sub_field($selector, $post_id, false);
    } else {
        $post_id = acf_get_loop('active', 'post_id');
        $sub_field = get_row_sub_field($selector);
    }
    if (!$sub_field) {
        return false;
    }
    return acf_update_value($value, $post_id, $sub_field);
}
function delete_field($selector, $post_id = false)
{
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_maybe_get_field($selector, $post_id);
    return $field ? acf_delete_value($post_id, $field) : false;
}
function delete_sub_field($selector, $post_id = false)
{
    return update_sub_field($selector, null, $post_id);
}
function add_row($selector, $row = false, $post_id = false)
{
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_maybe_get_field($selector, $post_id, false);
    if (!$field) {
        return false;
    }
    $value = acf_get_value($post_id, $field);
    $value = acf_get_array($value);
    $value[] = $row;
    acf_update_value($value, $post_id, $field);
    return count($value);
}
function add_sub_row($selector, $row = false, $post_id = false)
{
    $sub_field = false;
    if (is_array($selector)) {
        $post_id = acf_get_valid_post_id($post_id);
        $sub_field = acf_maybe_get_sub_field($selector, $post_id, false);
    } else {
        $post_id = acf_get_loop('active', 'post_id');
        $sub_field = get_row_sub_field($selector);
    }
    if (!$sub_field) {
        return false;
    }
    $value = acf_get_value($post_id, $sub_field);
    $value = acf_get_array($value);
    $value[] = $row;
    acf_update_value($value, $post_id, $sub_field);
    return count($value);
}
function update_row($selector, $i = 1, $row = false, $post_id = false)
{
    $offset = acf_get_setting('row_index_offset');
    $i = $i - $offset;
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_maybe_get_field($selector, $post_id, false);
    if (!$field) {
        return false;
    }
    $value = acf_get_value($post_id, $field);
    $value = acf_get_array($value);
    $value[$i] = $row;
    acf_update_value($value, $post_id, $field);
    return true;
}
function update_sub_row($selector, $i = 1, $row = false, $post_id = false)
{
    $sub_field = false;
    $offset = acf_get_setting('row_index_offset');
    $i = $i - $offset;
    if (is_array($selector)) {
        $post_id = acf_get_valid_post_id($post_id);
        $sub_field = acf_maybe_get_sub_field($selector, $post_id, false);
    } else {
        $post_id = acf_get_loop('active', 'post_id');
        $sub_field = get_row_sub_field($selector);
    }
    if (!$sub_field) {
        return false;
    }
    $value = acf_get_value($post_id, $sub_field);
    $value = acf_get_array($value);
    $value[$i] = $row;
    acf_update_value($value, $post_id, $sub_field);
    return true;
}
function delete_row($selector, $i = 1, $post_id = false)
{
    $offset = acf_get_setting('row_index_offset');
    $i = $i - $offset;
    $post_id = acf_get_valid_post_id($post_id);
    $field = acf_maybe_get_field($selector, $post_id);
    if (!$field) {
        return false;
    }
    $value = acf_get_value($post_id, $field);
    $value = acf_get_array($value);
    if (!isset($value[$i])) {
        return false;
    }
    unset($value[$i]);
    acf_update_value($value, $post_id, $field);
    return true;
}
function delete_sub_row($selector, $i = 1, $post_id = false)
{
    $sub_field = false;
    $offset = acf_get_setting('row_index_offset');
    $i = $i - $offset;
    if (is_array($selector)) {
        $post_id = acf_get_valid_post_id($post_id);
        $sub_field = acf_maybe_get_sub_field($selector, $post_id, false);
    } else {
        $post_id = acf_get_loop('active', 'post_id');
        $sub_field = get_row_sub_field($selector);
    }
    if (!$sub_field) {
        return false;
    }
    $value = acf_get_value($post_id, $sub_field);
    $value = acf_get_array($value);
    if (!isset($value[$i])) {
        return false;
    }
    unset($value[$i]);
    acf_update_value($value, $post_id, $sub_field);
    return true;
}
function create_field($field)
{
    acf_render_field($field);
}
function render_field($field)
{
    acf_render_field($field);
}
function reset_the_repeater_field()
{
    return reset_rows();
}
function the_repeater_field($field_name, $post_id = false)
{
    return has_sub_field($field_name, $post_id);
}
function the_flexible_field($field_name, $post_id = false)
{
    return has_sub_field($field_name, $post_id);
}
function acf_filter_post_id($post_id)
{
    return acf_get_valid_post_id($post_id);
}