本文整理汇总了PHP中acf_maybe_get函数的典型用法代码示例。如果您正苦于以下问题:PHP acf_maybe_get函数的具体用法?PHP acf_maybe_get怎么用?PHP acf_maybe_get使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了acf_maybe_get函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: acf_validate_attachment
function acf_validate_attachment($attachment, $field, $context = 'prepare')
{
// vars
$errors = array();
$file = array('type' => '', 'width' => 0, 'height' => 0, 'size' => 0);
// upload
if ($context == 'upload') {
// vars
$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);
}
// prepare
} elseif ($context == 'prepare') {
$file['type'] = pathinfo($attachment['url'], 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);
// custom
} else {
$file = wp_parse_args($file, $attachment);
}
// image
if ($file['width'] || $file['height']) {
// width
$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) {
// min width
$errors['min_width'] = sprintf(__('Image width must be at least %dpx.', 'acf'), $min_width);
} elseif ($max_width && $file['width'] > $max_width) {
// min width
$errors['max_width'] = sprintf(__('Image width must not exceed %dpx.', 'acf'), $max_width);
}
}
// height
$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) {
// min height
$errors['min_height'] = sprintf(__('Image height must be at least %dpx.', 'acf'), $min_height);
} elseif ($max_height && $file['height'] > $max_height) {
// min height
$errors['max_height'] = sprintf(__('Image height must not exceed %dpx.', 'acf'), $max_height);
}
}
}
// file size
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)) {
// min width
$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)) {
// min width
$errors['max_size'] = sprintf(__('File size must must not exceed %s.', 'acf'), acf_format_filesize($max_size));
}
}
// file type
if ($file['type']) {
$mime_types = acf_maybe_get($field, 'mime_types', '');
// lower case
$file['type'] = strtolower($file['type']);
$mime_types = strtolower($mime_types);
// explode
$mime_types = str_replace(array(' ', '.'), '', $mime_types);
$mime_types = explode(',', $mime_types);
// split pieces
$mime_types = array_filter($mime_types);
// remove empty pieces
if (!empty($mime_types) && !in_array($file['type'], $mime_types)) {
// glue together last 2 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));
}
}
// filter for 3rd party customization
$errors = apply_filters("acf/validate_attachment", $errors, $file, $attachment, $field);
$errors = apply_filters("acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field);
$errors = apply_filters("acf/validate_attachment/name={$field['name']}", $errors, $file, $attachment, $field);
$errors = apply_filters("acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field);
// return
return $errors;
}
示例2: is_translatable
function is_translatable()
{
// global
global $sitepress, $sitepress_settings;
// vars
$post_types = acf_maybe_get($sitepress_settings, 'custom_posts_sync_option', array());
// return true if acf-field-group is translatable
if (!empty($post_types['acf-field-group'])) {
return true;
}
// return true if acf is translatable, and acf-field-group does not yet exist
if (!empty($post_types['acf']) && !isset($post_types['acf-field-group'])) {
return true;
}
// return
return false;
}
示例3: get_choices
function get_choices($options = array())
{
// defaults
$options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => ''));
// vars
$r = array();
$args = array('hide_empty' => false);
// load field
$field = acf_get_field($options['field_key']);
if (!$field) {
return false;
}
// search
if ($options['s']) {
$args['search'] = $options['s'];
}
// filters
$args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']);
$args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']);
$args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']);
// get terms
$terms = get_terms($field['taxonomy'], $args);
// sort into hierachial order!
if (is_taxonomy_hierarchical($field['taxonomy'])) {
// get parent
$parent = acf_maybe_get($args, 'parent', 0);
$parent = acf_maybe_get($args, 'child_of', $parent);
// this will fail if a search has taken place because parents wont exist
if (empty($args['search'])) {
$terms = _get_term_children($parent, $terms, $field['taxonomy']);
}
}
/// append to r
foreach ($terms as $term) {
// add to json
$r[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id']));
}
// return
return $r;
}
示例4: add_field
function add_field($field)
{
// vars
// - allow for the very unexpected case where no key or parent exist
$key = acf_maybe_get($field, 'key', '');
$parent = acf_maybe_get($field, 'parent', '');
// add parent reference
$this->add_parent_reference($parent, $key);
// add in menu order
$field['menu_order'] = count($this->parents[$parent]) - 1;
// add field
$this->fields[$key] = $field;
// clear cache
wp_cache_delete("get_field/key={$key}", 'acf');
wp_cache_delete("get_fields/parent={$parent}", 'acf');
}
示例5: wp_post_revision_fields
function wp_post_revision_fields($fields, $post = null)
{
// validate page
if (acf_is_screen('revision') || acf_is_ajax('get-revision-diffs')) {
// allow
} else {
// bail early (most likely saving a post)
return $fields;
}
// vars
$append = array();
$order = array();
$post_id = acf_maybe_get($post, 'ID');
// compatibility with WP < 4.5 (test)
if (!$post_id) {
global $post;
$post_id = $post->ID;
}
// get all postmeta
$meta = get_post_meta($post_id);
// bail early if no meta
if (!$meta) {
return $fields;
}
// loop
foreach ($meta as $name => $value) {
// attempt to find key value
$key = acf_maybe_get($meta, '_' . $name);
// bail ealry if no key
if (!$key) {
continue;
}
// update vars
$value = $value[0];
$key = $key[0];
// bail early if $key is a not a field_key
if (!acf_is_field_key($key)) {
continue;
}
// get field
$field = acf_get_field($key);
$field_title = $field['label'] . ' (' . $name . ')';
$field_order = $field['menu_order'];
$ancestors = acf_get_field_ancestors($field);
// ancestors
if (!empty($ancestors)) {
// vars
$count = count($ancestors);
$oldest = acf_get_field($ancestors[$count - 1]);
// update vars
$field_title = str_repeat('- ', $count) . $field_title;
$field_order = $oldest['menu_order'] . '.1';
}
// append
$append[$name] = $field_title;
$order[$name] = $field_order;
// hook into specific revision field filter and return local value
add_filter("_wp_post_revision_field_{$name}", array($this, 'wp_post_revision_field'), 10, 4);
}
// append
if (!empty($append)) {
// vars
$prefix = '_';
// add prefix
$append = acf_add_array_key_prefix($append, $prefix);
$order = acf_add_array_key_prefix($order, $prefix);
// sort by name (orders sub field values correctly)
array_multisort($order, $append);
// remove prefix
$append = acf_remove_array_key_prefix($append, $prefix);
// append
$fields = $fields + $append;
}
// return
return $fields;
}
示例6: acf_copy_postmeta
function acf_copy_postmeta($from_post_id, $to_post_id)
{
// get all postmeta
$meta = get_post_meta($from_post_id);
// bail early if no meta
if (!$meta) {
return;
}
// loop
foreach ($meta as $name => $value) {
// attempt to find key value
$key = acf_maybe_get($meta, '_' . $name);
// bail ealry if no key
if (!$key) {
continue;
}
// update vars
$value = $value[0];
$key = $key[0];
// bail early if $key is a not a field_key
if (!acf_is_field_key($key)) {
continue;
}
// get_post_meta will return array before running maybe_unserialize
$value = maybe_unserialize($value);
// add in slashes
// - update_post_meta will unslash the value, so we must first slash it to avoid losing backslashes
// - https://codex.wordpress.org/Function_Reference/update_post_meta#Character_Escaping
if (is_string($value)) {
$value = wp_slash($value);
}
// update value
acf_update_metadata($to_post_id, $name, $value);
acf_update_metadata($to_post_id, $name, $key, true);
}
}
示例7: render_field
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>';
}
示例8: render_field
function render_field($field)
{
// enqueue
acf_enqueue_uploader();
// vars
$atts = array('id' => $field['id'], 'class' => "acf-gallery {$field['class']}", 'data-library' => $field['library'], 'data-min' => $field['min'], 'data-max' => $field['max'], 'data-mime_types' => $field['mime_types'], 'data-insert' => $field['insert'], 'data-columns' => 4);
// set gallery height
$height = acf_get_user_setting('gallery_height', 400);
$height = max($height, 200);
// minimum height is 200
$atts['style'] = "height:{$height}px";
// get posts
$value = $this->get_attachments($field['value']);
?>
<div <?php
acf_esc_attr_e($atts);
?>
>
<div class="acf-hidden">
<?php
acf_hidden_input(array('name' => $field['name'], 'value' => ''));
?>
</div>
<div class="acf-gallery-main">
<div class="acf-gallery-attachments">
<?php
if ($value) {
?>
<?php
foreach ($value as $i => $v) {
// bail early if no value
if (!$v) {
continue;
}
// vars
$a = array('ID' => $v->ID, 'title' => $v->post_title, 'filename' => wp_basename($v->guid), 'type' => acf_maybe_get(explode('/', $v->post_mime_type), 0), 'class' => 'acf-gallery-attachment acf-soh');
// thumbnail
$thumbnail = acf_get_post_thumbnail($a['ID'], 'medium');
// remove filename if is image
if ($a['type'] == 'image') {
$a['filename'] = '';
}
// class
$a['class'] .= ' -' . $a['type'];
if ($thumbnail['type'] == 'icon') {
$a['class'] .= ' -icon';
}
?>
<div class="<?php
echo $a['class'];
?>
" data-id="<?php
echo $a['ID'];
?>
">
<?php
acf_hidden_input(array('name' => $field['name'] . '[]', 'value' => $a['ID']));
?>
<div class="margin">
<div class="thumbnail">
<img src="<?php
echo $thumbnail['url'];
?>
" alt="" title="<?php
echo $a['title'];
?>
"/>
</div>
<?php
if ($a['filename']) {
?>
<div class="filename"><?php
echo acf_get_truncated($a['filename'], 30);
?>
</div>
<?php
}
?>
</div>
<div class="actions acf-soh-target">
<a class="acf-icon -cancel dark acf-gallery-remove" href="#" data-id="<?php
echo $a['ID'];
?>
" title="<?php
_e('Remove', 'acf');
?>
"></a>
</div>
</div>
<?php
}
?>
<?php
}
//.........这里部分代码省略.........
示例9: ajax_update_widget
function ajax_update_widget()
{
// remove default save filter
remove_filter('widget_update_callback', array($this, 'widget_update_callback'), 10, 4);
// bail early if no nonce
if (!acf_verify_nonce('widget')) {
return;
}
// vars
$id = acf_maybe_get($_POST, 'widget-id');
// save data
if ($id && acf_validate_save_post()) {
acf_save_post("widget_{$id}");
}
}
示例10: acf_update_field
function acf_update_field($field = false, $specific = false)
{
// $field must be an array
if (!is_array($field)) {
return false;
}
// validate
$field = acf_get_valid_field($field);
// may have been posted. Remove slashes
$field = wp_unslash($field);
// clean up conditional logic keys
if (!empty($field['conditional_logic'])) {
// extract groups
$groups = acf_extract_var($field, 'conditional_logic');
// clean array
$groups = array_filter($groups);
$groups = array_values($groups);
// clean rules
foreach (array_keys($groups) as $i) {
$groups[$i] = array_filter($groups[$i]);
$groups[$i] = array_values($groups[$i]);
}
// reset conditional logic
$field['conditional_logic'] = $groups;
}
// find correct parent
if (acf_is_field_key($field['parent'])) {
// get parent
$parent = acf_get_field($field['parent']);
// update to ID
$field['parent'] = acf_maybe_get($parent, 'ID', 0);
}
// filter for 3rd party customization
$field = apply_filters("acf/update_field", $field);
$field = apply_filters("acf/update_field/type={$field['type']}", $field);
$field = apply_filters("acf/update_field/name={$field['name']}", $field);
$field = apply_filters("acf/update_field/key={$field['key']}", $field);
// store origional field for return
$data = $field;
// extract some args
$extract = acf_extract_vars($data, array('ID', 'key', 'label', 'name', 'prefix', 'value', 'menu_order', 'id', 'class', 'parent', '_name', '_input', '_valid'));
// serialize for DB
$data = maybe_serialize($data);
// save
$save = array('ID' => $extract['ID'], 'post_status' => 'publish', 'post_type' => 'acf-field', 'post_title' => $extract['label'], 'post_name' => $extract['key'], 'post_excerpt' => $extract['name'], 'post_content' => $data, 'post_parent' => $extract['parent'], 'menu_order' => $extract['menu_order']);
// $specific
if (!empty($specific)) {
// prepend ID
array_unshift($specific, 'ID');
// vars
$_save = $save;
// reset
$save = array();
// appen data
foreach ($specific as $key) {
$save[$key] = $_save[$key];
}
}
// allow fields to contain the same name
add_filter('wp_unique_post_slug', 'acf_update_field_wp_unique_post_slug', 100, 6);
// update the field and update the ID
if ($field['ID']) {
wp_update_post($save);
} else {
$field['ID'] = wp_insert_post($save);
}
// clear cache
wp_cache_delete("get_field/ID={$field['ID']}", 'acf');
wp_cache_delete("get_field/key={$field['key']}", 'acf');
wp_cache_delete("get_fields/parent={$field['parent']}", 'acf');
// return
return $field;
}
示例11: get_ajax_query
function get_ajax_query($options = array())
{
// defaults
$options = acf_parse_args($options, array('post_id' => 0, 's' => '', 'field_key' => '', 'paged' => 0));
// load field
$field = acf_get_field($options['field_key']);
if (!$field) {
return false;
}
// bail early if taxonomy does not exist
if (!taxonomy_exists($field['taxonomy'])) {
return false;
}
// vars
$results = array();
$is_hierarchical = is_taxonomy_hierarchical($field['taxonomy']);
$is_pagination = $options['paged'] > 0;
$is_search = false;
$limit = 20;
$offset = 20 * ($options['paged'] - 1);
// args
$args = array('taxonomy' => $field['taxonomy'], 'hide_empty' => false);
// pagination
// - don't bother for hierarchial terms, we will need to load all terms anyway
if ($is_pagination && !$is_hierarchical) {
$args['number'] = $limit;
$args['offset'] = $offset;
}
// search
if ($options['s'] !== '') {
// strip slashes (search may be integer)
$s = wp_unslash(strval($options['s']));
// update vars
$args['search'] = $s;
$is_search = true;
}
// filters
$args = apply_filters('acf/fields/taxonomy/query', $args, $field, $options['post_id']);
$args = apply_filters('acf/fields/taxonomy/query/name=' . $field['name'], $args, $field, $options['post_id']);
$args = apply_filters('acf/fields/taxonomy/query/key=' . $field['key'], $args, $field, $options['post_id']);
// get terms
$terms = acf_get_terms($args);
// sort into hierachial order!
if ($is_hierarchical) {
// update vars
$limit = acf_maybe_get($args, 'number', $limit);
$offset = acf_maybe_get($args, 'offset', $offset);
// get parent
$parent = acf_maybe_get($args, 'parent', 0);
$parent = acf_maybe_get($args, 'child_of', $parent);
// this will fail if a search has taken place because parents wont exist
if (!$is_search) {
$terms = _get_term_children($parent, $terms, $field['taxonomy']);
}
// fake pagination
if ($is_pagination) {
$terms = array_slice($terms, $offset, $limit);
}
}
/// append to r
foreach ($terms as $term) {
// add to json
$results[] = array('id' => $term->term_id, 'text' => $this->get_term_title($term, $field, $options['post_id']));
}
// vars
$response = array('results' => $results, 'limit' => $limit);
// return
return $response;
}
示例12: modify_plugin_update
function modify_plugin_update($transient)
{
// bail early if no response (dashboard showed an error)
if (empty($transient->response)) {
return $transient;
}
// vars
$basename = acf_get_setting('basename');
$show_updates = acf_get_setting('show_updates');
// ensure is_plugin_active() exists (not on frontend)
if (!function_exists('is_plugin_active')) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// bail early if not a plugin (included in theme)
if (!is_plugin_active($basename)) {
$show_updates = false;
}
// bail early if no show_updates
if (!$show_updates) {
// remove from transient
unset($transient->response[$basename]);
// return
return $transient;
}
// get update
$update = acf_maybe_get($transient->response, $basename);
// filter
$update = apply_filters('acf/updates/plugin_update', $update, $transient);
// update
if ($update) {
$transient->response[$basename] = $update;
} else {
unset($transient->response[$basename]);
}
// return
return $transient;
}
示例13: add_field
function add_field($field)
{
// vars
$key = acf_maybe_get($field, 'key', '');
$parent = acf_maybe_get($field, 'parent', '');
// add parent reference
$this->add_parent_reference($parent, $key);
// add in menu order
$field['menu_order'] = count($this->parents[$parent]) - 1;
// add field
$this->fields[$key] = $field;
}
示例14: modify_plugin_update
function modify_plugin_update($transient)
{
// bail early if no response (dashboard showed an error)
if (!isset($transient->response)) {
return $transient;
}
// vars
$basename = acf_get_setting('basename');
$show_updates = acf_get_setting('show_updates');
// bail early if not a plugin (included in theme)
if (!acf_is_plugin_active()) {
$show_updates = false;
}
// bail early if no show_updates
if (!$show_updates) {
// remove from transient
unset($transient->response[$basename]);
// return
return $transient;
}
// get update
$update = acf_maybe_get($transient->response, $basename);
// filter
$update = apply_filters('acf/updates/plugin_update', $update, $transient);
// update
if ($update) {
$transient->response[$basename] = $update;
} else {
unset($transient->response[$basename]);
}
// return
return $transient;
}
示例15: get_customizer_widgets
function get_customizer_widgets($customizer)
{
// vars
$widgets = array();
$settings = $customizer->settings();
// bail ealry if no settings
if (empty($settings)) {
return false;
}
// loop over settings
foreach (array_keys($settings) as $i) {
// vars
$setting = $settings[$i];
// bail ealry if not widget
if (substr($setting->id, 0, 7) !== 'widget_') {
continue;
}
// get value
$value = $setting->post_value();
// set data
$setting->acf = acf_maybe_get($value, 'acf');
// append
$widgets[] = $setting;
}
// bail ealry if no preview values
if (empty($widgets)) {
return false;
}
// return
return $widgets;
}