File "acf-field-group-functions.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/advanced-custom-fields/includes/acf-field-group-functions.php
File size: 14 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

acf_register_store('field-groups')->prop('multisite', true);
function acf_get_field_group($id = 0)
{
    if (is_object($id)) {
        $id = $id->ID;
    }
    $store = acf_get_store('field-groups');
    if ($store->has($id)) {
        return $store->get($id);
    }
    if (acf_is_local_field_group($id)) {
        $field_group = acf_get_local_field_group($id);
    } else {
        $field_group = acf_get_raw_field_group($id);
    }
    if (!$field_group) {
        return false;
    }
    $field_group = acf_validate_field_group($field_group);
    $field_group = apply_filters('acf/load_field_group', $field_group);
    $store->set($field_group['key'], $field_group);
    $store->alias($field_group['key'], $field_group['ID']);
    return $field_group;
}
function acf_get_raw_field_group($id = 0)
{
    $post = acf_get_field_group_post($id);
    if (!$post) {
        return false;
    }
    if ($post->post_type !== 'acf-field-group') {
        return false;
    }
    $field_group = (array) maybe_unserialize($post->post_content);
    $field_group['ID'] = $post->ID;
    $field_group['title'] = $post->post_title;
    $field_group['key'] = $post->post_name;
    $field_group['menu_order'] = $post->menu_order;
    $field_group['active'] = in_array($post->post_status, array('publish', 'auto-draft'));
    return $field_group;
}
function acf_get_field_group_post($id = 0)
{
    if (is_numeric($id)) {
        return get_post($id);
    } elseif (is_string($id)) {
        $cache_key = acf_cache_key("acf_get_field_group_post:key:{$id}");
        $post_id = wp_cache_get($cache_key, 'acf');
        if ($post_id === false) {
            $posts = get_posts(array('posts_per_page' => 1, 'post_type' => 'acf-field-group', 'post_status' => array('publish', 'acf-disabled', 'trash'), 'orderby' => 'menu_order title', 'order' => 'ASC', 'suppress_filters' => false, 'cache_results' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'acf_group_key' => $id));
            $post_id = $posts ? $posts[0]->ID : 0;
            wp_cache_set($cache_key, $post_id, 'acf');
        }
        if ($post_id) {
            return get_post($post_id);
        }
    }
    return false;
}
function acf_is_field_group_key($id = '')
{
    if (is_string($id) && substr($id, 0, 6) === 'group_') {
        return true;
    }
    return apply_filters('acf/is_field_group_key', false, $id);
}
function acf_validate_field_group($field_group = array())
{
    if (is_array($field_group) && !empty($field_group['_valid'])) {
        return $field_group;
    }
    $field_group = wp_parse_args($field_group, array('ID' => 0, 'key' => '', 'title' => '', 'fields' => array(), 'location' => array(), 'menu_order' => 0, 'position' => 'normal', 'style' => 'default', 'label_placement' => 'top', 'instruction_placement' => 'label', 'hide_on_screen' => array(), 'active' => true, 'description' => ''));
    $field_group['ID'] = (int) $field_group['ID'];
    $field_group['menu_order'] = (int) $field_group['menu_order'];
    $field_group['active'] = (bool) $field_group['active'];
    $field_group['_valid'] = true;
    $field_group = apply_filters('acf/validate_field_group', $field_group);
    return $field_group;
}
function acf_get_valid_field_group($field_group = false)
{
    return acf_validate_field_group($field_group);
}
function acf_translate_field_group($field_group = array())
{
    $l10n = acf_get_setting('l10n');
    $l10n_textdomain = acf_get_setting('l10n_textdomain');
    if ($l10n && $l10n_textdomain) {
        $field_group['title'] = acf_translate($field_group['title']);
        $field_group = apply_filters('acf/translate_field_group', $field_group);
    }
    return $field_group;
}
add_action('acf/validate_field_group', 'acf_translate_field_group');
function acf_get_field_groups($filter = array())
{
    $field_groups = array();
    $raw_field_groups = acf_get_raw_field_groups();
    if ($raw_field_groups) {
        foreach ($raw_field_groups as $raw_field_group) {
            $field_groups[] = acf_get_field_group($raw_field_group['ID']);
        }
    }
    $field_groups = apply_filters('acf/load_field_groups', $field_groups);
    if ($filter) {
        return acf_filter_field_groups($field_groups, $filter);
    }
    return $field_groups;
}
function acf_get_raw_field_groups()
{
    $cache_key = acf_cache_key('acf_get_field_group_posts');
    $post_ids = wp_cache_get($cache_key, 'acf');
    if ($post_ids === false) {
        $posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field-group', 'orderby' => 'menu_order title', 'order' => 'ASC', 'suppress_filters' => false, 'cache_results' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'post_status' => array('publish', 'acf-disabled')));
        $post_ids = array();
        foreach ($posts as $post) {
            $post_ids[] = $post->ID;
        }
        wp_cache_set($cache_key, $post_ids, 'acf');
    }
    $field_groups = array();
    foreach ($post_ids as $post_id) {
        $field_groups[] = acf_get_raw_field_group($post_id);
    }
    return $field_groups;
}
function acf_filter_field_groups($field_groups, $args = array())
{
    $filtered = array();
    if ($field_groups) {
        foreach ($field_groups as $field_group) {
            if (acf_get_field_group_visibility($field_group, $args)) {
                $filtered[] = $field_group;
            }
        }
    }
    return $filtered;
}
function acf_get_field_group_visibility($field_group, $args = array())
{
    if (!$field_group['active']) {
        return false;
    }
    if ($field_group['location']) {
        $screen = acf_get_location_screen($args);
        foreach ($field_group['location'] as $group) {
            if (empty($group)) {
                continue;
            }
            $match_group = true;
            foreach ($group as $rule) {
                if (!acf_match_location_rule($rule, $screen, $field_group)) {
                    $match_group = false;
                    break;
                }
            }
            if ($match_group) {
                return true;
            }
        }
    }
    return false;
}
function acf_update_field_group($field_group)
{
    $field_group = acf_get_valid_field_group($field_group);
    $field_group = wp_unslash($field_group);
    $field_group = acf_parse_types($field_group);
    if ($field_group['location']) {
        $field_group['location'] = array_filter($field_group['location']);
        $field_group['location'] = array_values($field_group['location']);
        $field_group['location'] = array_map('array_filter', $field_group['location']);
        $field_group['location'] = array_map('array_values', $field_group['location']);
    }
    $_field_group = $field_group;
    acf_extract_vars($_field_group, array('ID', 'key', 'title', 'menu_order', 'fields', 'active', '_valid'));
    $save = array('ID' => $field_group['ID'], 'post_status' => $field_group['active'] ? 'publish' : 'acf-disabled', 'post_type' => 'acf-field-group', 'post_title' => $field_group['title'], 'post_name' => $field_group['key'], 'post_excerpt' => sanitize_title($field_group['title']), 'post_content' => maybe_serialize($_field_group), 'menu_order' => $field_group['menu_order'], 'comment_status' => 'closed', 'ping_status' => 'closed');
    remove_filter('content_save_pre', 'wp_targeted_link_rel');
    $save = wp_slash($save);
    if ($field_group['ID']) {
        wp_update_post($save);
    } else {
        $field_group['ID'] = wp_insert_post($save);
    }
    acf_flush_field_group_cache($field_group);
    do_action('acf/update_field_group', $field_group);
    return $field_group;
}
function _acf_apply_unique_field_group_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug)
{
    if ($post_type === 'acf-field-group') {
        return $original_slug;
    }
    return $slug;
}
add_filter('wp_unique_post_slug', '_acf_apply_unique_field_group_slug', 999, 6);
function acf_flush_field_group_cache($field_group)
{
    acf_get_store('field-groups')->remove($field_group['key']);
    wp_cache_delete(acf_cache_key("acf_get_field_group_post:key:{$field_group['key']}"), 'acf');
    wp_cache_delete(acf_cache_key('acf_get_field_group_posts'), 'acf');
}
function acf_delete_field_group($id = 0)
{
    acf_disable_filters();
    $field_group = acf_get_field_group($id);
    if (!$field_group || !$field_group['ID']) {
        return false;
    }
    $fields = acf_get_fields($field_group);
    if ($fields) {
        foreach ($fields as $field) {
            acf_delete_field($field['ID']);
        }
    }
    wp_delete_post($field_group['ID'], true);
    acf_flush_field_group_cache($field_group);
    do_action('acf/delete_field_group', $field_group);
    return true;
}
function acf_trash_field_group($id = 0)
{
    acf_disable_filters();
    $field_group = acf_get_field_group($id);
    if (!$field_group || !$field_group['ID']) {
        return false;
    }
    $fields = acf_get_fields($field_group);
    if ($fields) {
        foreach ($fields as $field) {
            acf_trash_field($field['ID']);
        }
    }
    wp_trash_post($field_group['ID'], true);
    acf_flush_field_group_cache($field_group);
    do_action('acf/trash_field_group', $field_group);
    return true;
}
function acf_untrash_field_group($id = 0)
{
    acf_disable_filters();
    $field_group = acf_get_field_group($id);
    if (!$field_group || !$field_group['ID']) {
        return false;
    }
    $fields = acf_get_fields($field_group);
    if ($fields) {
        foreach ($fields as $field) {
            acf_untrash_field($field['ID']);
        }
    }
    wp_untrash_post($field_group['ID'], true);
    acf_flush_field_group_cache($field_group);
    do_action('acf/untrash_field_group', $field_group);
    return true;
}
function _acf_untrash_field_group_post_status($new_status, $post_id, $previous_status)
{
    return get_post_type($post_id) === 'acf-field-group' ? $previous_status : $new_status;
}
add_action('wp_untrash_post_status', '_acf_untrash_field_group_post_status', 10, 3);
function acf_is_field_group($field_group = false)
{
    return is_array($field_group) && isset($field_group['key']) && isset($field_group['title']);
}
function acf_duplicate_field_group($id = 0, $new_post_id = 0)
{
    acf_disable_filters();
    $field_group = acf_get_field_group($id);
    if (!$field_group || !$field_group['ID']) {
        return false;
    }
    $fields = acf_get_fields($field_group);
    $field_group['ID'] = $new_post_id;
    $field_group['key'] = uniqid('group_');
    if (!$new_post_id) {
        $field_group['title'] .= ' (' . __('copy', 'acf') . ')';
    }
    if (!$field_group['ID']) {
        $field_group['ID'] = wp_insert_post(array('post_title' => $field_group['key']));
    }
    $duplicates = acf_duplicate_fields($fields, $field_group['ID']);
    $field_group = acf_update_field_group($field_group);
    do_action('acf/duplicate_field_group', $field_group);
    return $field_group;
}
function acf_get_field_group_style($field_group)
{
    $style = '';
    $elements = array('permalink' => '#edit-slug-box', 'the_content' => '#postdivrich', 'excerpt' => '#postexcerpt', 'custom_fields' => '#postcustom', 'discussion' => '#commentstatusdiv', 'comments' => '#commentsdiv', 'slug' => '#slugdiv', 'author' => '#authordiv', 'format' => '#formatdiv', 'page_attributes' => '#pageparentdiv', 'featured_image' => '#postimagediv', 'revisions' => '#revisionsdiv', 'categories' => '#categorydiv', 'tags' => '#tagsdiv-post_tag', 'send-trackbacks' => '#trackbacksdiv');
    if (is_array($field_group['hide_on_screen'])) {
        $hide = array();
        foreach ($field_group['hide_on_screen'] as $k) {
            if (isset($elements[$k])) {
                $id = $elements[$k];
                $hide[] = $id;
                $hide[] = '#screen-meta label[for=' . substr($id, 1) . '-hide]';
            }
        }
        $style = implode(', ', $hide) . ' {display: none;}';
    }
    return apply_filters('acf/get_field_group_style', $style, $field_group);
}
function acf_get_field_group_edit_link($post_id)
{
    if ($post_id && acf_current_user_can_admin()) {
        return admin_url('post.php?post=' . $post_id . '&action=edit');
    }
    return '';
}
function acf_prepare_field_group_for_export($field_group = array())
{
    acf_extract_vars($field_group, array('ID', 'local', '_valid'));
    $field_group['fields'] = acf_prepare_fields_for_export($field_group['fields']);
    return apply_filters('acf/prepare_field_group_for_export', $field_group);
}
function acf_prepare_field_group_for_import($field_group)
{
    if ($field_group['fields']) {
        foreach ($field_group['fields'] as $i => &$field) {
            $field['parent'] = $field_group['key'];
            $field['menu_order'] = $i;
        }
    }
    return apply_filters('acf/prepare_field_group_for_import', $field_group);
}
function acf_import_field_group($field_group)
{
    $filters = acf_disable_filters();
    $field_group = acf_get_valid_field_group($field_group);
    $field_group = acf_prepare_field_group_for_import($field_group);
    $fields = acf_prepare_fields_for_import($field_group['fields']);
    $ids = array();
    if ($field_group['ID']) {
        $db_fields = acf_prepare_fields_for_import(acf_get_fields($field_group));
        $keys = wp_list_pluck($fields, 'key');
        foreach ($db_fields as $field) {
            $ids[$field['key']] = $field['ID'];
            if (!in_array($field['key'], $keys)) {
                acf_delete_field($field['ID']);
            }
        }
    }
    if (!$field_group['ID']) {
        $field_group['ID'] = wp_insert_post(array('post_title' => $field_group['key']));
    }
    $ids[$field_group['key']] = $field_group['ID'];
    if ($fields) {
        foreach ($fields as $field) {
            if (isset($ids[$field['key']])) {
                $field['ID'] = $ids[$field['key']];
            }
            if (isset($ids[$field['parent']])) {
                $field['parent'] = $ids[$field['parent']];
            }
            $field = acf_update_field($field);
            $ids[$field['key']] = $field['ID'];
        }
    }
    $field_group = acf_update_field_group($field_group);
    acf_enable_filters($filters);
    do_action('acf/import_field_group', $field_group);
    return $field_group;
}