<?php function acf_is_array($array) { return is_array($array) && !empty($array); } function acf_has_setting($name = '') { return acf()->has_setting($name); } function acf_raw_setting($name = '') { return acf()->get_setting($name); } function acf_update_setting($name, $value) { $name = acf_validate_setting($name); return acf()->update_setting($name, $value); } function acf_validate_setting($name = '') { return apply_filters('acf/validate_setting', $name); } function acf_get_setting($name, $value = null) { $name = acf_validate_setting($name); if (acf_has_setting($name)) { $value = acf_raw_setting($name); } $value = apply_filters("acf/settings/{$name}", $value); return $value; } function acf_append_setting($name, $value) { $setting = acf_raw_setting($name); if (!is_array($setting)) { $setting = array(); } $setting[] = $value; return acf_update_setting($name, $setting); } function acf_get_data($name) { return acf()->get_data($name); } function acf_set_data($name, $value) { return acf()->set_data($name, $value); } function acf_append_data($name, $data) { $prev_data = acf()->get_data($name); if (is_array($prev_data)) { $data = array_merge($prev_data, $data); } acf()->set_data($name, $data); } function acf_init() { acf()->init(); } function acf_has_done($name) { if (acf_raw_setting("has_done_{$name}")) { return true; } acf_update_setting("has_done_{$name}", true); return false; } function acf_get_external_path($file, $path = '') { return plugin_dir_path($file) . $path; } function acf_get_external_dir($file, $path = '') { return acf_plugin_dir_url($file) . $path; } function acf_plugin_dir_url($file) { $path = plugin_dir_path($file); $path = wp_normalize_path($path); $check_path = wp_normalize_path(realpath(WP_PLUGIN_DIR)); if (strpos($path, $check_path) === 0) { return str_replace($check_path, plugins_url(), $path); } $check_path = wp_normalize_path(realpath(WP_CONTENT_DIR)); if (strpos($path, $check_path) === 0) { return str_replace($check_path, content_url(), $path); } $check_path = wp_normalize_path(realpath(ABSPATH)); if (strpos($path, $check_path) === 0) { return str_replace($check_path, site_url('/'), $path); } return plugin_dir_url($file); } function acf_parse_args($args, $defaults = array()) { $args = wp_parse_args($args, $defaults); $args = acf_parse_types($args); return $args; } function acf_parse_types($array) { return array_map('acf_parse_type', $array); } function acf_parse_type($v) { if (is_string($v)) { $v = trim($v); if (is_numeric($v) && strval(intval($v)) === $v) { $v = intval($v); } } return $v; } function acf_get_view($path = '', $args = array()) { if (substr($path, -4) !== '.php') { $path = acf_get_path("includes/admin/views/{$path}.php"); } if (file_exists($path)) { extract($args); include $path; } } function acf_merge_atts($atts, $extra = array()) { if (empty($extra)) { return $atts; } $extra = array_map('trim', $extra); $extra = array_filter($extra); foreach ($extra as $k => $v) { if ($k == 'class' || $k == 'style') { $atts[$k] .= ' ' . $v; } else { $atts[$k] = $v; } } return $atts; } function acf_nonce_input($nonce = '') { echo '<input type="hidden" name="_acf_nonce" value="' . wp_create_nonce($nonce) . '" />'; } function acf_extract_var(&$array, $key, $default = null) { if (is_array($array) && array_key_exists($key, $array)) { $v = $array[$key]; unset($array[$key]); return $v; } return $default; } function acf_extract_vars(&$array, $keys) { $r = array(); foreach ($keys as $key) { $r[$key] = acf_extract_var($array, $key); } return $r; } function acf_get_sub_array($array, $keys) { $r = array(); foreach ($keys as $key) { $r[$key] = $array[$key]; } return $r; } function acf_get_post_types($args = array()) { $post_types = array(); $exclude = acf_extract_var($args, 'exclude', array()); $exclude[] = 'acf-field'; $exclude[] = 'acf-field-group'; $objects = get_post_types($args, 'objects'); foreach ($objects as $i => $object) { if (in_array($i, $exclude)) { continue; } if ($object->_builtin && !$object->public) { continue; } $post_types[] = $i; } $post_types = apply_filters('acf/get_post_types', $post_types, $args); return $post_types; } function acf_get_pretty_post_types($post_types = array()) { if (empty($post_types)) { $post_types = acf_get_post_types(); } $ref = array(); $r = array(); foreach ($post_types as $post_type) { $label = acf_get_post_type_label($post_type); $r[$post_type] = $label; if (!isset($ref[$label])) { $ref[$label] = 0; } $ref[$label]++; } foreach (array_keys($r) as $i) { $post_type = $r[$i]; if ($ref[$post_type] > 1) { $r[$i] .= ' (' . $i . ')'; } } return $r; } function acf_get_post_type_label($post_type) { $label = $post_type; if (post_type_exists($post_type)) { $obj = get_post_type_object($post_type); $label = $obj->labels->singular_name; } return $label; } function acf_verify_nonce($value) { $nonce = acf_maybe_get_POST('_acf_nonce'); if (!$nonce || !wp_verify_nonce($nonce, $value)) { return false; } $_POST['_acf_nonce'] = false; return true; } function acf_verify_ajax() { $nonce = isset($_REQUEST['nonce']) ? $_REQUEST['nonce'] : ''; if (!$nonce || !wp_verify_nonce($nonce, 'acf_nonce')) { return false; } do_action('acf/verify_ajax'); return true; } function acf_get_image_sizes() { $sizes = array('thumbnail' => __('Thumbnail', 'acf'), 'medium' => __('Medium', 'acf'), 'large' => __('Large', 'acf')); $all_sizes = get_intermediate_image_sizes(); if (!empty($all_sizes)) { foreach ($all_sizes as $size) { if (isset($sizes[$size])) { continue; } $label = str_replace('-', ' ', $size); $label = ucwords($label); $sizes[$size] = $label; } } foreach (array_keys($sizes) as $s) { $data = acf_get_image_size($s); if ($data['width'] && $data['height']) { $sizes[$s] .= ' (' . $data['width'] . ' x ' . $data['height'] . ')'; } } $sizes['full'] = __('Full Size', 'acf'); $sizes = apply_filters('acf/get_image_sizes', $sizes); return $sizes; } function acf_get_image_size($s = '') { global $_wp_additional_image_sizes; $_sizes = $_wp_additional_image_sizes; $data = array('width' => isset($_sizes[$s]['width']) ? $_sizes[$s]['width'] : get_option("{$s}_size_w"), 'height' => isset($_sizes[$s]['height']) ? $_sizes[$s]['height'] : get_option("{$s}_size_h")); return $data; } function acf_version_compare($left = '', $compare = '>', $right = '') { if ($left === 'wp') { global $wp_version; $left = $wp_version; } return version_compare($left, $right, $compare); } function acf_get_full_version($version = '1') { if ($pos = strpos($version, '-')) { $version = substr($version, 0, $pos); } return $version; } function acf_get_terms($args) { $args = wp_parse_args($args, array('taxonomy' => null, 'hide_empty' => false, 'update_term_meta_cache' => false)); if (acf_version_compare('wp', '<', '4.5')) { return get_terms($args['taxonomy'], $args); } return get_terms($args); } function acf_get_taxonomy_terms($taxonomies = array()) { $taxonomies = acf_get_array($taxonomies); $taxonomies = acf_get_pretty_taxonomies($taxonomies); $r = array(); foreach (array_keys($taxonomies) as $taxonomy) { $label = $taxonomies[$taxonomy]; $is_hierarchical = is_taxonomy_hierarchical($taxonomy); $terms = acf_get_terms(array('taxonomy' => $taxonomy, 'hide_empty' => false)); if (empty($terms)) { continue; } if ($is_hierarchical) { $terms = _get_term_children(0, $terms, $taxonomy); } $r[$label] = array(); foreach ($terms as $term) { $k = "{$taxonomy}:{$term->slug}"; $r[$label][$k] = acf_get_term_title($term); } } return $r; } function acf_decode_taxonomy_terms($strings = false) { if (empty($strings)) { return false; } $terms = array(); foreach ($strings as $string) { $data = acf_decode_taxonomy_term($string); $taxonomy = $data['taxonomy']; $term = $data['term']; if (!isset($terms[$taxonomy])) { $terms[$taxonomy] = array(); } $terms[$taxonomy][] = $term; } return $terms; } function acf_decode_taxonomy_term($value) { $data = array('taxonomy' => '', 'term' => ''); if (is_numeric($value)) { $data['term'] = $value; } elseif (is_string($value)) { $value = explode(':', $value); $data['taxonomy'] = isset($value[0]) ? $value[0] : ''; $data['term'] = isset($value[1]) ? $value[1] : ''; } else { return false; } if (is_numeric($data['term'])) { global $wpdb; if (!$data['taxonomy']) { $data['taxonomy'] = $wpdb->get_var($wpdb->prepare("SELECT taxonomy FROM {$wpdb->term_taxonomy} WHERE term_id = %d LIMIT 1", $data['term'])); } $term = get_term_by('slug', $data['term'], $data['taxonomy']); if (!$term) { $term = get_term($data['term'], $data['taxonomy']); } if (!$term) { return false; } $data['taxonomy'] = $term->taxonomy; $data['term'] = $term->slug; } return $data; } function acf_array($val = array()) { return (array) $val; } function acf_unarray($val) { if (is_array($val)) { return reset($val); } return $val; } function acf_get_array($var = false, $delimiter = '') { if (is_array($var)) { return $var; } if (acf_is_empty($var)) { return array(); } if (is_string($var) && $delimiter) { return explode($delimiter, $var); } return (array) $var; } function acf_get_numeric($value = '') { $numbers = array(); $is_array = is_array($value); foreach ((array) $value as $v) { if (is_numeric($v)) { $numbers[] = (int) $v; } } if (empty($numbers)) { return false; } if (!$is_array) { $numbers = $numbers[0]; } return $numbers; } function acf_get_posts($args = array()) { $posts = array(); $args = wp_parse_args($args, array('posts_per_page' => -1, 'post_type' => '', 'post_status' => 'any', 'update_post_meta_cache' => false, 'update_post_term_cache' => false)); if (!$args['post_type']) { $args['post_type'] = acf_get_post_types(); } if ($args['post__in']) { $args['post__in'] = array_map('intval', acf_array($args['post__in'])); } $posts = get_posts($args); $posts = array_filter($posts); if ($posts && $args['post__in']) { $order = array(); foreach ($posts as $i => $post) { $order[$i] = array_search($post->ID, $args['post__in']); } array_multisort($order, $posts); } return $posts; } function _acf_query_remove_post_type($sql) { global $wpdb; if (strpos($sql, "{$wpdb->posts}.ID IN") === false) { return $sql; } $glue = 'AND'; $bits = explode($glue, $sql); foreach ($bits as $i => $bit) { if (strpos($bit, "{$wpdb->posts}.post_type") !== false) { unset($bits[$i]); } } $sql = implode($glue, $bits); return $sql; } function acf_get_grouped_posts($args) { $data = array(); $args = wp_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)); $post_types = acf_get_array($args['post_type']); $post_types_labels = acf_get_pretty_post_types($post_types); $is_single_post_type = count($post_types) == 1; if ($is_single_post_type) { $args['post_type'] = reset($post_types); } if (!$is_single_post_type) { add_filter('posts_orderby', '_acf_orderby_post_type', 10, 2); } $posts = get_posts($args); if (!$is_single_post_type) { remove_filter('posts_orderby', '_acf_orderby_post_type', 10, 2); } foreach ($post_types as $post_type) { $this_posts = array(); $this_group = array(); foreach ($posts as $post) { if ($post->post_type == $post_type) { $this_posts[] = $post; } } if (empty($this_posts)) { continue; } if (is_post_type_hierarchical($post_type) && empty($args['s'])) { $post_id = $this_posts[0]->ID; $parent_id = acf_maybe_get($args, 'post_parent', 0); $offset = 0; $length = count($this_posts); $all_posts = get_posts(array_merge($args, array('posts_per_page' => -1, 'paged' => 0, 'post_type' => $post_type))); foreach ($all_posts as $i => $post) { if ($post->ID == $post_id) { $offset = $i; break; } } $ordered_posts = get_page_children($parent_id, $all_posts); if (count($ordered_posts) == count($all_posts)) { $this_posts = array_slice($ordered_posts, $offset, $length); } } foreach ($this_posts as $post) { $this_group[$post->ID] = $post; } $label = $post_types_labels[$post_type]; $data[$label] = $this_group; } return $data; } function _acf_orderby_post_type($ordeby, $wp_query) { global $wpdb; $post_types = $wp_query->get('post_type'); if (is_array($post_types)) { $post_types = implode("','", $post_types); $ordeby = "FIELD({$wpdb->posts}.post_type,'{$post_types}')," . $ordeby; } return $ordeby; } function acf_get_post_title($post = 0, $is_search = false) { $post = get_post($post); $title = ''; $prepend = ''; $append = ''; if (!$post) { return ''; } $title = get_the_title($post->ID); if ($title === '') { $title = __('(no title)', 'acf'); } if (get_post_status($post->ID) != 'publish') { $append .= ' (' . get_post_status($post->ID) . ')'; } if ($post->post_type !== 'attachment') { $ancestors = get_ancestors($post->ID, $post->post_type); $prepend .= str_repeat('- ', count($ancestors)); } $title = $prepend . $title . $append; return $title; } function acf_order_by_search($array, $search) { $weights = array(); $needle = strtolower($search); foreach (array_keys($array) as $k) { $array['_' . $k] = acf_extract_var($array, $k); } foreach ($array as $k => $v) { $weight = 0; $haystack = strtolower($v); $strpos = strpos($haystack, $needle); if ($strpos !== false) { $weight = strlen($search); if ($strpos == 0) { $weight++; } } $weights[$k] = $weight; } array_multisort($weights, SORT_DESC, $array); foreach (array_keys($array) as $k) { $array[substr($k, 1)] = acf_extract_var($array, $k); } return $array; } function acf_get_pretty_user_roles($allowed = false) { $editable_roles = get_editable_roles(); $allowed = acf_get_array($allowed); $roles = array(); foreach ($editable_roles as $role_name => $role_details) { if (!empty($allowed) && !in_array($role_name, $allowed)) { continue; } $roles[$role_name] = translate_user_role($role_details['name']); } return $roles; } function acf_get_grouped_users($args = array()) { $r = array(); $args = wp_parse_args($args, array('users_per_page' => -1, 'paged' => 0, 'role' => '', 'orderby' => 'login', 'order' => 'ASC')); $i = 0; $min = 0; $max = 0; $users_per_page = acf_extract_var($args, 'users_per_page'); $paged = acf_extract_var($args, 'paged'); if ($users_per_page > 0) { $paged = max(0, $paged); $min = ($paged - 1) * $users_per_page + 1; $max = $paged * $users_per_page; } $user_roles = acf_get_pretty_user_roles($args['role']); if (is_array($args['role'])) { global $wp_version, $wpdb; $roles = acf_extract_var($args, 'role'); if (version_compare($wp_version, '4.4', '>=')) { $args['role__in'] = $roles; } else { $blog_id = get_current_blog_id(); $meta_query = array('relation' => 'OR'); foreach ($roles as $role) { $meta_query[] = array('key' => $wpdb->get_blog_prefix($blog_id) . 'capabilities', 'value' => '"' . $role . '"', 'compare' => 'LIKE'); } $args['meta_query'] = $meta_query; } } $users = get_users($args); foreach ($user_roles as $user_role_name => $user_role_label) { $this_users = array(); $this_group = array(); foreach (array_keys($users) as $key) { if (!in_array($user_role_name, $users[$key]->roles)) { continue; } $user = acf_extract_var($users, $key); $i++; if ($min && $i < $min) { continue; } if ($max && $i > $max) { break; } $this_users[$user->ID] = $user; } if (empty($this_users)) { continue; } $r[$user_role_label] = $this_users; } return $r; } function acf_json_encode($json) { return json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); } function acf_str_exists($needle, $haystack) { if (is_string($haystack) && strpos($haystack, $needle) !== false) { return true; } return false; } function acf_debug() { $args = func_get_args(); $s = array_shift($args); $o = ''; $nl = "\r\n"; $o .= '<script type="text/javascript">' . $nl; $o .= 'console.log("' . $s . '"'; if (!empty($args)) { foreach ($args as $arg) { if (is_object($arg) || is_array($arg)) { $arg = json_encode($arg); } elseif (is_bool($arg)) { $arg = $arg ? 'true' : 'false'; } elseif (is_string($arg)) { $arg = '"' . $arg . '"'; } $o .= ', ' . $arg; } } $o .= ');' . $nl; $o .= '</script>' . $nl; echo $o; } function acf_debug_start() { acf_update_setting('debug_start', memory_get_usage()); } function acf_debug_end() { $start = acf_get_setting('debug_start'); $end = memory_get_usage(); return $end - $start; } function acf_encode_choices($array = array(), $show_keys = true) { if (!is_array($array)) { return $array; } if (empty($array)) { return ''; } $string = ''; if ($show_keys) { foreach ($array as $k => $v) { if (strval($k) == strval($v)) { continue; } $array[$k] = $k . ' : ' . $v; } } $string = implode("\n", $array); return $string; } function acf_decode_choices($string = '', $array_keys = false) { if (is_array($string)) { return $string; } elseif (is_numeric($string)) { } elseif (!is_string($string)) { return array(); } elseif ($string === '') { return array(); } $array = array(); $lines = explode("\n", $string); foreach ($lines as $line) { $k = trim($line); $v = trim($line); if (acf_str_exists(' : ', $line)) { $line = explode(' : ', $line); $k = trim($line[0]); $v = trim($line[1]); } $array[$k] = $v; } if ($array_keys) { return array_keys($array); } return $array; } function acf_str_replace($string = '', $search_replace = array()) { $ignore = array(); unset($search_replace['']); foreach ($search_replace as $search => $replace) { if (in_array($search, $ignore)) { continue; } if (strpos($string, $search) === false) { continue; } $string = str_replace($search, $replace, $string); $ignore[] = $replace; } return $string; } acf_update_setting('php_to_js_date_formats', array('Y' => 'yy', 'y' => 'y', 'm' => 'mm', 'n' => 'm', 'F' => 'MM', 'M' => 'M', 'l' => 'DD', 'D' => 'D', 'd' => 'dd', 'j' => 'd', 'S' => '')); acf_update_setting('php_to_js_time_formats', array('a' => 'tt', 'A' => 'TT', 'h' => 'hh', 'g' => 'h', 'H' => 'HH', 'G' => 'H', 'i' => 'mm', 's' => 'ss')); function acf_split_date_time($date_time = '') { $php_date = acf_get_setting('php_to_js_date_formats'); $php_time = acf_get_setting('php_to_js_time_formats'); $chars = str_split($date_time); $type = 'date'; $data = array('date' => '', 'time' => ''); foreach ($chars as $i => $c) { if (isset($php_date[$c])) { $type = 'date'; } elseif (isset($php_time[$c])) { $type = 'time'; } $data[$type] .= $c; } $data['date'] = trim($data['date']); $data['time'] = trim($data['time']); return $data; } function acf_convert_date_to_php($date = '') { $php_to_js = acf_get_setting('php_to_js_date_formats'); $js_to_php = array_flip($php_to_js); return acf_str_replace($date, $js_to_php); } function acf_convert_date_to_js($date = '') { $php_to_js = acf_get_setting('php_to_js_date_formats'); return acf_str_replace($date, $php_to_js); } function acf_convert_time_to_php($time = '') { $php_to_js = acf_get_setting('php_to_js_time_formats'); $js_to_php = array_flip($php_to_js); return acf_str_replace($time, $js_to_php); } function acf_convert_time_to_js($time = '') { $php_to_js = acf_get_setting('php_to_js_time_formats'); return acf_str_replace($time, $php_to_js); } function acf_update_user_setting($name, $value) { $user_id = get_current_user_id(); $settings = get_user_meta($user_id, 'acf_user_settings', true); $settings = acf_get_array($settings); if (acf_is_empty($value)) { unset($settings[$name]); } else { $settings[$name] = $value; } return update_metadata('user', $user_id, 'acf_user_settings', $settings); } function acf_get_user_setting($name = '', $default = false) { $user_id = get_current_user_id(); $settings = get_user_meta($user_id, 'acf_user_settings', true); $settings = acf_get_array($settings); if (!isset($settings[$name])) { return $default; } return $settings[$name]; } function acf_in_array($value = '', $array = false) { if (!is_array($array)) { return false; } return in_array($value, $array); } function acf_get_valid_post_id($post_id = 0) { $preload = apply_filters('acf/pre_load_post_id', null, $post_id); if ($preload !== null) { return $preload; } $_post_id = $post_id; if (!$post_id) { $post_id = (int) get_the_ID(); if (!$post_id) { $post_id = get_queried_object(); } } if (is_object($post_id)) { if (isset($post_id->post_type, $post_id->ID)) { $post_id = $post_id->ID; } elseif (isset($post_id->roles, $post_id->ID)) { $post_id = 'user_' . $post_id->ID; } elseif (isset($post_id->taxonomy, $post_id->term_id)) { $post_id = 'term_' . $post_id->term_id; } elseif (isset($post_id->comment_ID)) { $post_id = 'comment_' . $post_id->comment_ID; } else { $post_id = 0; } } if ($post_id === 'option') { $post_id = 'options'; } if ($post_id == 'options') { $dl = acf_get_setting('default_language'); $cl = acf_get_setting('current_language'); if ($cl && $cl !== $dl) { $post_id .= '_' . $cl; } } $post_id = apply_filters('acf/validate_post_id', $post_id, $_post_id); return $post_id; } function acf_get_post_id_info($post_id = 0) { $info = array('type' => 'post', 'id' => 0); if (!$post_id) { return $info; } if (is_numeric($post_id)) { $info['id'] = (int) $post_id; } elseif (is_string($post_id)) { $glue = '_'; $type = explode($glue, $post_id); $id = array_pop($type); $type = implode($glue, $type); $meta = array('post', 'user', 'comment', 'term'); if (!in_array($type, $meta) && acf_isset_termmeta($type)) { $type = 'term'; } if (is_numeric($id) && in_array($type, $meta)) { $info['type'] = $type; $info['id'] = (int) $id; } else { $info['type'] = 'option'; $info['id'] = $post_id; } } $info = apply_filters('acf/get_post_id_info', $info, $post_id); return $info; } function acf_isset_termmeta($taxonomy = '') { if (get_option('db_version') < 34370) { return false; } if ($taxonomy && !taxonomy_exists($taxonomy)) { return false; } return true; } function acf_upload_files($ancestors = array()) { $file = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => '', 'size' => ''); foreach (array_keys($file) as $k) { $file[$k] = $_FILES['acf'][$k]; } if (!empty($ancestors)) { foreach ($ancestors as $a) { foreach (array_keys($file) as $k) { $file[$k] = $file[$k][$a]; } } } if (is_array($file['name'])) { foreach (array_keys($file['name']) as $k) { $_ancestors = array_merge($ancestors, array($k)); acf_upload_files($_ancestors); } return; } if ($file['error']) { return; } $_POST['_acfuploader'] = end($ancestors); $attachment_id = acf_upload_file($file); array_unshift($ancestors, 'acf'); acf_update_nested_array($_POST, $ancestors, $attachment_id); } function acf_upload_file($uploaded_file) { require_once ABSPATH . '/wp-admin/includes/media.php'; require_once ABSPATH . '/wp-admin/includes/file.php'; require_once ABSPATH . '/wp-admin/includes/image.php'; $upload_overrides = array('test_form' => false); $file = wp_handle_upload($uploaded_file, $upload_overrides); if (isset($file['error'])) { return $file['error']; } $url = $file['url']; $type = $file['type']; $file = $file['file']; $filename = basename($file); $object = array('post_title' => $filename, 'post_mime_type' => $type, 'guid' => $url); $id = wp_insert_attachment($object, $file); wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file)); do_action('wp_create_file_in_uploads', $file, $id); return $id; } function acf_update_nested_array(&$array, $ancestors, $value) { if (empty($ancestors)) { $array = $value; return true; } $k = array_shift($ancestors); if (isset($array[$k])) { return acf_update_nested_array($array[$k], $ancestors, $value); } return false; } function acf_is_screen($id = '') { if (!function_exists('get_current_screen')) { return false; } $current_screen = get_current_screen(); if (!$current_screen) { return false; } elseif (is_array($id)) { return in_array($current_screen->id, $id); } else { return $id === $current_screen->id; } } function acf_maybe_get($array = array(), $key = 0, $default = null) { return isset($array[$key]) ? $array[$key] : $default; } function acf_maybe_get_POST($key = '', $default = null) { return isset($_POST[$key]) ? $_POST[$key] : $default; } function acf_maybe_get_GET($key = '', $default = null) { return isset($_GET[$key]) ? $_GET[$key] : $default; } function acf_get_attachment($attachment) { $response = apply_filters('acf/pre_load_attachment', null, $attachment); if ($response !== null) { return $response; } $attachment = get_post($attachment); if (!$attachment) { return false; } if ($attachment->post_type !== 'attachment') { return false; } $meta = wp_get_attachment_metadata($attachment->ID); $attached_file = get_attached_file($attachment->ID); if (strpos($attachment->post_mime_type, '/') !== false) { list($type, $subtype) = explode('/', $attachment->post_mime_type); } else { list($type, $subtype) = array($attachment->post_mime_type, ''); } $response = array('ID' => $attachment->ID, 'id' => $attachment->ID, 'title' => $attachment->post_title, 'filename' => wp_basename($attached_file), 'filesize' => 0, 'url' => wp_get_attachment_url($attachment->ID), 'link' => get_attachment_link($attachment->ID), 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true), 'author' => $attachment->post_author, 'description' => $attachment->post_content, 'caption' => $attachment->post_excerpt, 'name' => $attachment->post_name, 'status' => $attachment->post_status, 'uploaded_to' => $attachment->post_parent, 'date' => $attachment->post_date_gmt, 'modified' => $attachment->post_modified_gmt, 'menu_order' => $attachment->menu_order, 'mime_type' => $attachment->post_mime_type, 'type' => $type, 'subtype' => $subtype, 'icon' => wp_mime_type_icon($attachment->ID)); if (isset($meta['filesize'])) { $response['filesize'] = $meta['filesize']; } elseif (file_exists($attached_file)) { $response['filesize'] = filesize($attached_file); } $sizes_id = 0; switch ($type) { case 'image': $sizes_id = $attachment->ID; $src = wp_get_attachment_image_src($attachment->ID, 'full'); if ($src) { $response['url'] = $src[0]; $response['width'] = $src[1]; $response['height'] = $src[2]; } break; case 'video': $response['width'] = acf_maybe_get($meta, 'width', 0); $response['height'] = acf_maybe_get($meta, 'height', 0); if ($featured_id = get_post_thumbnail_id($attachment->ID)) { $sizes_id = $featured_id; } break; case 'audio': if ($featured_id = get_post_thumbnail_id($attachment->ID)) { $sizes_id = $featured_id; } break; } if ($sizes_id) { $sizes = get_intermediate_image_sizes(); $sizes_data = array(); foreach ($sizes as $size) { $src = wp_get_attachment_image_src($sizes_id, $size); if ($src) { $sizes_data[$size] = $src[0]; $sizes_data[$size . '-width'] = $src[1]; $sizes_data[$size . '-height'] = $src[2]; } } $response['sizes'] = $sizes_data; } return apply_filters('acf/load_attachment', $response, $attachment, $meta); } function acf_get_truncated($text, $length = 64) { $text = trim($text); $the_length = strlen($text); $return = substr($text, 0, $length - 3); if ($the_length > $length - 3) { $return .= '...'; } return $return; } function acf_current_user_can_admin() { if (acf_get_setting('show_admin') && current_user_can(acf_get_setting('capability'))) { return true; } return false; } function acf_get_filesize($size = 1) { $unit = 'MB'; $units = array('TB' => 4, 'GB' => 3, 'MB' => 2, 'KB' => 1); if (is_string($size)) { $custom = strtoupper(substr($size, -2)); foreach ($units as $k => $v) { if ($custom === $k) { $unit = $k; $size = substr($size, 0, -2); } } } $bytes = floatval($size) * pow(1024, $units[$unit]); return $bytes; } function acf_format_filesize($size = 1) { $bytes = acf_get_filesize($size); $units = array('TB' => 4, 'GB' => 3, 'MB' => 2, 'KB' => 1); foreach ($units as $k => $v) { $result = $bytes / pow(1024, $v); if ($result >= 1) { return $result . ' ' . $k; } } return $bytes . ' B'; } function acf_get_valid_terms($terms = false, $taxonomy = 'category') { $terms = acf_get_array($terms); $terms = array_map('intval', $terms); if (!function_exists('wp_get_split_term') || empty($terms)) { return $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; } } return $terms; } function acf_validate_attachment($attachment, $field, $context = 'prepare') { $errors = array(); $file = array('type' => '', 'width' => 0, 'height' => 0, 'size' => 0); if ($context == 'upload') { $file['type'] = pathinfo($attachment['name'], PATHINFO_EXTENSION); $file['size'] = filesize($attachment['tmp_name']); if (strpos($attachment['type'], 'image') !== false) { $size = getimagesize($attachment['tmp_name']); $file['width'] = acf_maybe_get($size, 0); $file['height'] = acf_maybe_get($size, 1); } } elseif ($context == 'prepare') { $use_path = isset($attachment['filename']) ? $attachment['filename'] : $attachment['url']; $file['type'] = pathinfo($use_path, PATHINFO_EXTENSION); $file['size'] = acf_maybe_get($attachment, 'filesizeInBytes', 0); $file['width'] = acf_maybe_get($attachment, 'width', 0); $file['height'] = acf_maybe_get($attachment, 'height', 0); } else { $file = array_merge($file, $attachment); $use_path = isset($attachment['filename']) ? $attachment['filename'] : $attachment['url']; $file['type'] = pathinfo($use_path, PATHINFO_EXTENSION); } if ($file['width'] || $file['height']) { $min_width = (int) acf_maybe_get($field, 'min_width', 0); $max_width = (int) acf_maybe_get($field, 'max_width', 0); if ($file['width']) { if ($min_width && $file['width'] < $min_width) { $errors['min_width'] = sprintf(__('Image width must be at least %dpx.', 'acf'), $min_width); } elseif ($max_width && $file['width'] > $max_width) { $errors['max_width'] = sprintf(__('Image width must not exceed %dpx.', 'acf'), $max_width); } } $min_height = (int) acf_maybe_get($field, 'min_height', 0); $max_height = (int) acf_maybe_get($field, 'max_height', 0); if ($file['height']) { if ($min_height && $file['height'] < $min_height) { $errors['min_height'] = sprintf(__('Image height must be at least %dpx.', 'acf'), $min_height); } elseif ($max_height && $file['height'] > $max_height) { $errors['max_height'] = sprintf(__('Image height must not exceed %dpx.', 'acf'), $max_height); } } } if ($file['size']) { $min_size = acf_maybe_get($field, 'min_size', 0); $max_size = acf_maybe_get($field, 'max_size', 0); if ($min_size && $file['size'] < acf_get_filesize($min_size)) { $errors['min_size'] = sprintf(__('File size must be at least %s.', 'acf'), acf_format_filesize($min_size)); } elseif ($max_size && $file['size'] > acf_get_filesize($max_size)) { $errors['max_size'] = sprintf(__('File size must not exceed %s.', 'acf'), acf_format_filesize($max_size)); } } if ($file['type']) { $mime_types = acf_maybe_get($field, 'mime_types', ''); $file['type'] = strtolower($file['type']); $mime_types = strtolower($mime_types); $mime_types = str_replace(array(' ', '.'), '', $mime_types); $mime_types = explode(',', $mime_types); $mime_types = array_filter($mime_types); if (!empty($mime_types) && !in_array($file['type'], $mime_types)) { if (count($mime_types) > 1) { $last1 = array_pop($mime_types); $last2 = array_pop($mime_types); $mime_types[] = $last2 . ' ' . __('or', 'acf') . ' ' . $last1; } $errors['mime_types'] = sprintf(__('File type must be %s.', 'acf'), implode(', ', $mime_types)); } } $errors = apply_filters("acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field, $context); $errors = apply_filters("acf/validate_attachment/name={$field['_name']}", $errors, $file, $attachment, $field, $context); $errors = apply_filters("acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field, $context); $errors = apply_filters('acf/validate_attachment', $errors, $file, $attachment, $field, $context); return $errors; } add_filter('acf/settings/uploader', '_acf_settings_uploader'); function _acf_settings_uploader($uploader) { if (!current_user_can('upload_files')) { $uploader = 'basic'; } return $uploader; } function acf_translate($string) { $l10n = acf_get_setting('l10n'); $textdomain = acf_get_setting('l10n_textdomain'); if (!$l10n) { return $string; } if (!$textdomain) { return $string; } if (is_array($string)) { return array_map('acf_translate', $string); } if (!is_string($string)) { return $string; } if ($string === '') { return $string; } if (acf_get_setting('l10n_var_export')) { if (substr($string, 0, 7) === '!!__(!!') { return $string; } return "!!__(!!'" . $string . "!!', !!'" . $textdomain . "!!')!!"; } return __($string, $textdomain); } function acf_maybe_add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { if (did_action($tag) && !doing_action($tag)) { call_user_func($function_to_add); } else { add_action($tag, $function_to_add, $priority, $accepted_args); } } function acf_is_row_collapsed($field_key = '', $row_index = 0) { $collapsed = acf_get_user_setting('collapsed_' . $field_key, ''); if ($collapsed === '') { $collapsed = acf_extract_var($_COOKIE, "acf_collapsed_{$field_key}", ''); $collapsed = str_replace('|', ',', $collapsed); acf_update_user_setting('collapsed_' . $field_key, $collapsed); } $collapsed = explode(',', $collapsed); $collapsed = array_filter($collapsed, 'is_numeric'); return in_array($row_index, $collapsed); } function acf_get_attachment_image($attachment_id = 0, $size = 'thumbnail') { $url = wp_get_attachment_image_src($attachment_id, 'thumbnail'); $alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); if (!$url) { return ''; } $value = '<img src="' . $url . '" alt="' . $alt . '" />'; } function acf_get_post_thumbnail($post = null, $size = 'thumbnail') { $data = array('url' => '', 'type' => '', 'html' => ''); $post = get_post($post); if (!$post) { return $data; } $thumb_id = $post->ID; $mime_type = acf_maybe_get(explode('/', $post->post_mime_type), 0); if ($post->post_type === 'attachment') { if ($mime_type === 'audio' || $mime_type === 'video') { $thumb_id = get_post_thumbnail_id($post->ID); } } else { $thumb_id = get_post_thumbnail_id($post->ID); } $data['url'] = wp_get_attachment_image_src($thumb_id, $size); $data['url'] = acf_maybe_get($data['url'], 0); if (!$data['url'] && $post->post_type === 'attachment') { $data['url'] = wp_mime_type_icon($post->ID); $data['type'] = 'icon'; } $data['html'] = '<img src="' . $data['url'] . '" alt="" />'; return $data; } function acf_get_browser() { if (isset($_SERVER['HTTP_USER_AGENT'])) { $agent = $_SERVER['HTTP_USER_AGENT']; $browsers = array('Firefox' => 'firefox', 'Trident' => 'msie', 'MSIE' => 'msie', 'Edge' => 'edge', 'Chrome' => 'chrome', 'Safari' => 'safari'); foreach ($browsers as $k => $v) { if (strpos($agent, $k) !== false) { return $v; } } } return ''; } function acf_is_ajax($action = '') { $is_ajax = false; if (defined('DOING_AJAX') && DOING_AJAX) { $is_ajax = true; } if ($action && acf_maybe_get($_POST, 'action') !== $action) { $is_ajax = false; } return $is_ajax; } function acf_format_date($value, $format) { if (!$value) { return $value; } $unixtimestamp = 0; if (is_numeric($value) && strlen($value) !== 8) { $unixtimestamp = $value; } else { $unixtimestamp = strtotime($value); } return date_i18n($format, $unixtimestamp); } function acf_clear_log() { unlink(WP_CONTENT_DIR . '/debug.log'); } function acf_log() { $args = func_get_args(); foreach ($args as $i => $arg) { if (is_array($arg) || is_object($arg)) { $arg = print_r($arg, true); } elseif (is_bool($arg)) { $arg = 'bool(' . ($arg ? 'true' : 'false') . ')'; } $args[$i] = $arg; } error_log(implode(' ', $args)); } function acf_dev_log() { if (defined('ACF_DEV') && ACF_DEV) { call_user_func_array('acf_log', func_get_args()); } } function acf_doing($event = '', $context = '') { acf_update_setting('doing', $event); acf_update_setting('doing_context', $context); } function acf_is_doing($event = '', $context = '') { $doing = false; if (acf_get_setting('doing') === $event) { $doing = true; } if ($context && acf_get_setting('doing_context') !== $context) { $doing = false; } return $doing; } function acf_is_plugin_active() { $basename = acf_get_setting('basename'); if (!function_exists('is_plugin_active')) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; } return is_plugin_active($basename); } function acf_send_ajax_results($response) { $response = wp_parse_args($response, array('results' => array(), 'more' => false, 'limit' => 0)); if ($response['limit'] && $response['results']) { $total = 0; foreach ($response['results'] as $result) { $total++; if (!empty($result['children'])) { $total += count($result['children']); } } if ($total >= $response['limit']) { $response['more'] = true; } } wp_send_json($response); } function acf_is_sequential_array($array) { if (!is_array($array)) { return false; } foreach ($array as $key => $value) { if (is_string($key)) { return false; } } return true; } function acf_is_associative_array($array) { if (!is_array($array)) { return false; } foreach ($array as $key => $value) { if (is_string($key)) { return true; } } return false; } function acf_add_array_key_prefix($array, $prefix) { $array2 = array(); foreach ($array as $k => $v) { $k2 = $prefix . $k; $array2[$k2] = $v; } return $array2; } function acf_remove_array_key_prefix($array, $prefix) { $array2 = array(); $l = strlen($prefix); foreach ($array as $k => $v) { $k2 = substr($k, 0, $l) === $prefix ? substr($k, $l) : $k; $array2[$k2] = $v; } return $array2; } function acf_strip_protocol($url) { return str_replace(array('http://', 'https://'), '', $url); } function acf_connect_attachment_to_post($attachment_id = 0, $post_id = 0) { if (!$attachment_id || !is_numeric($attachment_id)) { return false; } if (!$post_id || !is_numeric($post_id)) { return false; } if (!apply_filters('acf/connect_attachment_to_post', true, $attachment_id, $post_id)) { return false; } $post = get_post($attachment_id); if ($post && $post->post_type == 'attachment' && $post->post_parent == 0) { wp_update_post(array('ID' => $post->ID, 'post_parent' => $post_id)); return true; } return true; } function acf_encrypt($data = '') { if (!function_exists('openssl_encrypt')) { return base64_encode($data); } $key = wp_hash('acf_encrypt'); $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); $encrypted_data = openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv); return base64_encode($encrypted_data . '::' . $iv); } function acf_decrypt($data = '') { if (!function_exists('openssl_decrypt')) { return base64_decode($data); } $key = wp_hash('acf_encrypt'); list($encrypted_data, $iv) = explode('::', base64_decode($data), 2); return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv); } function acf_parse_markdown($text = '') { $text = trim($text); $rules = array('/=== (.+?) ===/' => '<h2>$1</h2>', '/== (.+?) ==/' => '<h3>$1</h3>', '/= (.+?) =/' => '<h4>$1</h4>', '/\\[([^\\[]+)\\]\\(([^\\)]+)\\)/' => '<a href="$2">$1</a>', '/(\\*\\*)(.*?)\\1/' => '<strong>$2</strong>', '/(\\*)(.*?)\\1/' => '<em>$2</em>', '/`(.*?)`/' => '<code>$1</code>', '/\\n\\*(.*)/' => "\n<ul>\n\t<li>\$1</li>\n</ul>", '/\\n[0-9]+\\.(.*)/' => "\n<ol>\n\t<li>\$1</li>\n</ol>", '/<\\/ul>\\s?<ul>/' => '', '/<\\/ol>\\s?<ol>/' => ''); foreach ($rules as $k => $v) { $text = preg_replace($k, $v, $text); } $text = wpautop($text); return $text; } function acf_get_sites() { $results = array(); $sites = get_sites(array('number' => 0)); if ($sites) { foreach ($sites as $site) { $results[] = get_site($site)->to_array(); } } return $results; } function acf_convert_rules_to_groups($rules, $anyorall = 'any') { $groups = array(); $index = 0; foreach ($rules as $rule) { $group = acf_extract_var($rule, 'group_no'); $order = acf_extract_var($rule, 'order_no'); if ($group === null) { $group = $index; if ($anyorall == 'any') { $index++; } } if ($order === null) { $order = isset($groups[$group]) ? count($groups[$group]) : 0; } $groups[$group][$order] = $rule; ksort($groups[$group]); } ksort($groups); return $groups; } function acf_register_ajax($name = '', $callback = false, $public = false) { $action = "acf/ajax/{$name}"; add_action("wp_ajax_{$action}", $callback); if ($public) { add_action("wp_ajax_nopriv_{$action}", $callback); } } function acf_str_camel_case($string = '') { return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $string)))); } function acf_array_camel_case($array = array()) { $array2 = array(); foreach ($array as $k => $v) { $array2[acf_str_camel_case($k)] = $v; } return $array2; } function acf_is_block_editor() { if (function_exists('get_current_screen')) { $screen = get_current_screen(); if ($screen && method_exists($screen, 'is_block_editor')) { return $screen->is_block_editor(); } } return false; }