File "acf-meta-functions.php"

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

Download   Open   Edit   Advanced Editor   Back

<?php

function acf_get_meta($post_id = 0)
{
    $null = apply_filters('acf/pre_load_meta', null, $post_id);
    if ($null !== null) {
        return $null === '__return_null' ? null : $null;
    }
    extract(acf_decode_post_id($post_id));
    if ($type === 'option') {
        $allmeta = acf_get_option_meta($id);
    } else {
        $allmeta = get_metadata($type, $id, '');
    }
    $meta = array();
    if ($allmeta) {
        foreach ($allmeta as $key => $value) {
            if (isset($allmeta["_{$key}"])) {
                $meta[$key] = $allmeta[$key][0];
                $meta["_{$key}"] = $allmeta["_{$key}"][0];
            }
        }
    }
    $meta = array_map('maybe_unserialize', $meta);
    return apply_filters('acf/load_meta', $meta, $post_id);
}
function acf_get_option_meta($prefix = '')
{
    global $wpdb;
    $meta = array();
    $search = "{$prefix}_%";
    $_search = "_{$prefix}_%";
    $search = str_replace('_', '\\_', $search);
    $_search = str_replace('_', '\\_', $_search);
    $rows = $wpdb->get_results($wpdb->prepare("SELECT * \n\t\tFROM {$wpdb->options} \n\t\tWHERE option_name LIKE %s \n\t\tOR option_name LIKE %s", $search, $_search), ARRAY_A);
    $len = strlen("{$prefix}_");
    foreach ($rows as $row) {
        $meta[substr($row['option_name'], $len)][] = $row['option_value'];
    }
    return $meta;
}
function acf_get_metadata($post_id = 0, $name = '', $hidden = false)
{
    $null = apply_filters('acf/pre_load_metadata', null, $post_id, $name, $hidden);
    if ($null !== null) {
        return $null === '__return_null' ? null : $null;
    }
    extract(acf_decode_post_id($post_id));
    $prefix = $hidden ? '_' : '';
    if (!$id) {
        return null;
    }
    if ($type === 'option') {
        return get_option("{$prefix}{$id}_{$name}", null);
    } else {
        $meta = get_metadata($type, $id, "{$prefix}{$name}", false);
        return isset($meta[0]) ? $meta[0] : null;
    }
}
function acf_update_metadata($post_id = 0, $name = '', $value = '', $hidden = false)
{
    $pre = apply_filters('acf/pre_update_metadata', null, $post_id, $name, $value, $hidden);
    if ($pre !== null) {
        return $pre;
    }
    extract(acf_decode_post_id($post_id));
    $prefix = $hidden ? '_' : '';
    if (!$id) {
        return false;
    }
    if ($type === 'option') {
        $value = wp_unslash($value);
        $autoload = (bool) acf_get_setting('autoload');
        return update_option("{$prefix}{$id}_{$name}", $value, $autoload);
    } else {
        return update_metadata($type, $id, "{$prefix}{$name}", $value);
    }
}
function acf_delete_metadata($post_id = 0, $name = '', $hidden = false)
{
    $pre = apply_filters('acf/pre_delete_metadata', null, $post_id, $name, $hidden);
    if ($pre !== null) {
        return $pre;
    }
    extract(acf_decode_post_id($post_id));
    $prefix = $hidden ? '_' : '';
    if (!$id) {
        return false;
    }
    if ($type === 'option') {
        return delete_option("{$prefix}{$id}_{$name}");
    } else {
        return delete_metadata($type, $id, "{$prefix}{$name}");
    }
}
function acf_copy_metadata($from_post_id = 0, $to_post_id = 0)
{
    $meta = acf_get_meta($from_post_id);
    if ($meta) {
        $meta = wp_slash($meta);
        foreach ($meta as $name => $value) {
            acf_update_metadata($to_post_id, $name, $value);
        }
    }
}
function acf_copy_postmeta($from_post_id = 0, $to_post_id = 0)
{
    return acf_copy_metadata($from_post_id, $to_post_id);
}
function acf_get_meta_field($key = 0, $post_id = 0)
{
    $field_key = acf_get_reference($key, $post_id);
    if ($field_key) {
        $field = acf_get_field($field_key);
        if ($field) {
            $field['name'] = $key;
            return $field;
        }
    }
    return false;
}
function acf_get_metaref($post_id = 0, $type = 'fields', $name = '')
{
    $meta = acf_get_metadata($post_id, "_acf_{$type}");
    if (!$meta) {
        return $name ? '' : array();
    }
    if ($name) {
        return isset($meta[$name]) ? $meta[$name] : '';
    } else {
        return $meta;
    }
}
function acf_update_metaref($post_id = 0, $type = 'fields', $references = array())
{
    $current = acf_get_metaref($post_id, $type);
    $references = array_merge($current, $references);
    if ($type === 'groups') {
        $references = array_values($references);
    }
    $references = array_unique($references);
    return acf_update_metadata($post_id, "_acf_{$type}", $references);
}