本文整理汇总了PHP中acf_extract_var函数的典型用法代码示例。如果您正苦于以下问题:PHP acf_extract_var函数的具体用法?PHP acf_extract_var怎么用?PHP acf_extract_var使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了acf_extract_var函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: acf_get_valid_options_page
function acf_get_valid_options_page($page = '')
{
// allow for string
if (empty($page)) {
$page = array('page_title' => __('Options', 'acf'), 'menu_title' => __('Options', 'acf'), 'menu_slug' => 'acf-options');
} elseif (is_string($page)) {
$page_title = $page;
$page = array('page_title' => $page_title, 'menu_title' => $page_title);
}
// defaults
$page = wp_parse_args($page, array('page_title' => '', 'menu_title' => '', 'menu_slug' => '', 'capability' => 'edit_posts', 'parent_slug' => '', 'position' => false, 'icon_url' => false, 'redirect' => true, 'post_id' => 'options', 'autoload' => false, 'update_button' => __('Update', 'acf')));
// ACF4 compatibility
$migrate = array('title' => 'page_title', 'menu' => 'menu_title', 'slug' => 'menu_slug', 'parent' => 'parent_slug');
foreach ($migrate as $old => $new) {
if (!empty($page[$old])) {
$page[$new] = acf_extract_var($page, $old);
}
}
// page_title (allows user to define page with just page_title or title)
if (empty($page['menu_title'])) {
$page['menu_title'] = $page['page_title'];
}
// menu_slug
if (empty($page['menu_slug'])) {
$page['menu_slug'] = 'acf-options-' . sanitize_title($page['menu_title']);
}
// return
return $page;
}
示例2: add_field_group
function add_field_group($field_group)
{
// validate
$field_group = acf_get_valid_field_group($field_group);
// don't allow overrides
if (acf_is_local_field_group($field_group['key'])) {
return;
}
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// add field group
$this->groups[$field_group['key']] = $field_group;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['key'];
}
// add field group reference
//$field['field_group'] = $field_group['key'];
// add field
$this->add_field($field);
}
}
示例3: update_field_group
function update_field_group($field_group)
{
// vars
$path = acf_get_setting('save_json');
$file = $field_group['key'] . '.json';
// remove trailing slash
$path = untrailingslashit($path);
// bail early if dir does not exist
if (!is_writable($path)) {
//error_log( 'ACF failed to save field group to .json file. Path does not exist: ' . $path );
return;
}
// load fields
$fields = acf_get_fields($field_group);
// prepare fields
$fields = acf_prepare_fields_for_export($fields);
// add to field group
$field_group['fields'] = $fields;
// extract field group ID
$id = acf_extract_var($field_group, 'ID');
// write file
$f = fopen("{$path}/{$file}", 'w');
fwrite($f, acf_json_encode($field_group));
fclose($f);
}
示例4: relationships_import_json_field_groups
function relationships_import_json_field_groups()
{
// tell ACF NOT to save to JSON
add_filter('acf/settings/save_json', 'lc_import_json_save_no_point', 99);
// Remove previous field groups in DB
$args = array('post_type' => 'acf-field-group', 'post_status' => 'any', 'posts_per_page' => -1);
$query = new WP_Query($args);
foreach ($query->posts as $acf_group) {
wp_delete_post($acf_group->ID, true);
}
// Find local JSON directory
$dir = new DirectoryIterator(dirname(__FILE__) . '/json');
foreach ($dir as $file) {
if (!$file->isDot() && 'json' == $file->getExtension()) {
$json = json_decode(file_get_contents($file->getPathname()), true);
// What follows is basically a copy of import() in ACF admin/settings-export.php
// if importing an auto-json, wrap field group in array
if (isset($json['key'])) {
$json = array($json);
}
// vars
$added = array();
$ignored = array();
$ref = array();
$order = array();
foreach ($json as $field_group) {
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
}
}
}
}
}
示例5: acf_sync_import_json_field_groups
function acf_sync_import_json_field_groups($directory)
{
$dir = new DirectoryIterator($directory);
foreach ($dir as $file) {
if (!$file->isDot() && $file->getExtension() == 'json') {
$json = json_decode(file_get_contents($file->getPathname()), true);
// What follows is basically a copy of import() in ACF admin/settings-export.php
// if importing an auto-json, wrap field group in array
if (isset($json['key'])) {
$json = array($json);
}
// vars
$added = array();
$ignored = array();
$ref = array();
$order = array();
foreach ($json as $field_group) {
// remove fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} elseif (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
}
}
}
}
}
示例6: acf_location_rules_values_page_grandparent
function acf_location_rules_values_page_grandparent($choices)
{
// this code is copied directly from
// render_location_values()
// case "page"
$groups = acf_get_grouped_posts(array('post_type' => 'page'));
if (!empty($groups)) {
foreach (array_keys($groups) as $group_title) {
$posts = acf_extract_var($groups, $group_title);
foreach (array_keys($posts) as $post_id) {
$posts[$post_id] = acf_get_post_title($posts[$post_id]);
}
$choices = $posts;
}
}
// end of copy from ACF
return $choices;
}
示例7: acf_pull
/**
* Import (overwrite) field groups into DB
* @param string $params['name']
* @param string $params['group']
* @param string $params['old_value'] The previous settings data
* @param string $params['new_value'] The new settings data
*/
function acf_pull($params)
{
$field_group = $params['new_value'];
if ($existing_group = acf_get_field_group($field_group['key'])) {
$field_group['ID'] = $existing_group['ID'];
$existing_fields = acf_get_fields($existing_group);
// Remove fields
foreach ($existing_fields as $field) {
wp_delete_post($field['ID'], true);
}
}
// extract fields
$fields = acf_extract_var($field_group, 'fields');
// format fields
$fields = acf_prepare_fields_for_import($fields);
// save field group
$field_group = acf_update_field_group($field_group);
// add to ref
$ref[$field_group['key']] = $field_group['ID'];
// add to order
$order[$field_group['ID']] = 0;
// add fields
foreach ($fields as $index => $field) {
// add parent
if (empty($field['parent'])) {
$field['parent'] = $field_group['ID'];
} else {
if (isset($ref[$field['parent']])) {
$field['parent'] = $ref[$field['parent']];
}
}
// add field menu_order
if (!isset($order[$field['parent']])) {
$order[$field['parent']] = 0;
}
$field['menu_order'] = $order[$field['parent']];
$order[$field['parent']]++;
// save field
$field = acf_update_field($field);
// add to ref
$ref[$field['key']] = $field['ID'];
}
}
示例8: get_json
static function get_json()
{
if (!function_exists('acf_get_field_group')) {
echo 'You need ACF activated to use this screen';
exit;
}
$keys = isset($_GET['generate-template-id']) ? array($_GET['generate-template-id']) : array();
//print_r($keys);
//exit;
//$keys = $_GET['acf_export_keys'];
//$keys = array('group_55e23ad63ecc3');
//$keys = array('group_55d38b033048e');
//$keys = array('group_55d26a506a990');
// validate
if (empty($keys)) {
return false;
}
// vars
$json = array();
// construct JSON
foreach ($keys as $key) {
// load field group
$field_group = acf_get_field_group($key);
// validate field group
if (empty($field_group)) {
continue;
}
// load fields
$field_group['fields'] = acf_get_fields($field_group);
// prepare fields
$field_group['fields'] = acf_prepare_fields_for_export($field_group['fields']);
// extract field group ID
$id = acf_extract_var($field_group, 'ID');
// add to json array
$json[] = $field_group;
}
// return
return $json;
}
示例9: current_screen
function current_screen()
{
// validate screen
if (!acf_is_screen('edit-acf-field-group')) {
return;
}
// customize post_status
global $wp_post_statuses;
// modify publish post status
$wp_post_statuses['publish']->label_count = _n_noop('Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', 'acf');
// reorder trash to end
$wp_post_statuses['trash'] = acf_extract_var($wp_post_statuses, 'trash');
// check stuff
$this->check_duplicate();
$this->check_sync();
// actions
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
add_action('admin_footer', array($this, 'admin_footer'));
// columns
add_filter('manage_edit-acf-field-group_columns', array($this, 'field_group_columns'), 10, 1);
add_action('manage_acf-field-group_posts_custom_column', array($this, 'field_group_columns_html'), 10, 2);
}
示例10: render_field
function render_field($field)
{
// convert value to array
$field['value'] = acf_force_type_array($field['value']);
// add empty value (allows '' to be selected)
if (empty($field['value'])) {
$field['value'][''] = '';
}
// placeholder
if (empty($field['placeholder'])) {
$field['placeholder'] = __("Select", 'acf');
}
// 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']);
// hidden input
if ($field['ui']) {
acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'], 'name' => $field['name'], 'value' => implode(',', $field['value'])));
} elseif ($field['multiple']) {
acf_hidden_input(array('type' => 'hidden', 'name' => $field['name']));
}
// ui
if ($field['ui']) {
$atts['disabled'] = 'disabled';
$atts['class'] .= ' acf-hidden';
}
// 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;
}
}
// vars
$els = array();
$choices = array();
// loop through values and add them as options
if (!empty($field['choices'])) {
foreach ($field['choices'] as $k => $v) {
if (is_array($v)) {
// optgroup
$els[] = array('type' => 'optgroup', 'label' => $k);
if (!empty($v)) {
foreach ($v as $k2 => $v2) {
$els[] = array('type' => 'option', 'value' => $k2, 'label' => $v2, 'selected' => in_array($k2, $field['value']));
$choices[] = $k2;
}
}
$els[] = array('type' => '/optgroup');
} else {
$els[] = array('type' => 'option', 'value' => $k, 'label' => $v, 'selected' => in_array($k, $field['value']));
$choices[] = $k;
}
}
}
// prepende orphans
/*
if( !empty($field['value']) ) {
foreach( $field['value'] as $v ) {
if( empty($v) ) {
continue;
}
if( !in_array($v, $choices) ) {
array_unshift( $els, array( 'type' => 'option', 'value' => $v, 'label' => $v, 'selected' => true ) );
}
}
}
*/
// null
if ($field['allow_null']) {
array_unshift($els, array('type' => 'option', 'value' => '', 'label' => '- ' . $field['placeholder'] . ' -'));
}
// html
echo '<select ' . acf_esc_attr($atts) . '>';
// construct html
if (!empty($els)) {
foreach ($els as $el) {
// extract type
$type = acf_extract_var($el, 'type');
if ($type == 'option') {
// get label
$label = acf_extract_var($el, 'label');
// validate selected
if (acf_extract_var($el, 'selected')) {
$el['selected'] = 'selected';
}
// echo
//.........这里部分代码省略.........
示例11: duplicate_field
function duplicate_field($field)
{
// vars
$sub_fields = array();
if (!empty($field['layouts'])) {
// loop through layouts
foreach ($field['layouts'] as $layout) {
// extract sub fields
$extra = acf_extract_var($layout, 'sub_fields');
// merge
if (!empty($extra)) {
$sub_fields = array_merge($sub_fields, $extra);
}
}
// foreach
}
// if
// save field to get ID
$field = acf_update_field($field);
// duplicate sub fields
acf_duplicate_fields($sub_fields, $field['ID']);
// return
return $field;
}
示例12: render_field
function render_field($field)
{
// Change Field into a select
$field['type'] = 'select';
$field['ui'] = 1;
$field['ajax'] = 1;
$field['choices'] = array();
// load posts
$posts = $this->get_posts($field['value'], $field);
if ($posts) {
foreach (array_keys($posts) as $i) {
// vars
$post = acf_extract_var($posts, $i);
// append to choices
$field['choices'][$post->ID] = $this->get_post_title($post, $field);
}
}
// render
acf_render_field($field);
}
示例13: acf_order_by_search
function acf_order_by_search($array, $search)
{
// vars
$weights = array();
$needle = strtolower($search);
// add key prefix
foreach (array_keys($array) as $k) {
$array['_' . $k] = acf_extract_var($array, $k);
}
// add search weight
foreach ($array as $k => $v) {
// vars
$weight = 0;
$haystack = strtolower($v);
$strpos = strpos($haystack, $needle);
// detect search match
if ($strpos !== false) {
// set eright to length of match
$weight = strlen($search);
// increase weight if match starts at begining of string
if ($strpos == 0) {
$weight++;
}
}
// append to wights
$weights[$k] = $weight;
}
// sort the array with menu_order ascending
array_multisort($weights, SORT_DESC, $array);
// remove key prefix
foreach (array_keys($array) as $k) {
$array[substr($k, 1)] = acf_extract_var($array, $k);
}
// return
return $array;
}
示例14: render_field_select
function render_field_select($field)
{
// Change Field into a select
$field['type'] = 'select';
$field['ui'] = 1;
$field['ajax'] = 1;
$field['choices'] = array();
// value
if (!empty($field['value'])) {
// get terms
$terms = $this->get_terms($field['value'], $field['taxonomy']);
// set choices
if (!empty($terms)) {
foreach (array_keys($terms) as $i) {
// vars
$term = acf_extract_var($terms, $i);
// append to choices
$field['choices'][$term->term_id] = $this->get_term_title($term, $field);
}
}
}
// render select
acf_render_field($field);
}
示例15: format_value
function format_value($value, $post_id, $field)
{
// ACF4 null
if ($value === 'null') {
return false;
}
// bail early if no value
if (empty($value)) {
return $value;
}
// get posts
$value = $this->get_posts($value, $field);
// set choices
foreach (array_keys($value) as $i) {
// vars
$post = acf_extract_var($value, $i);
// convert $post to permalink
if (is_object($post)) {
$post = get_permalink($post);
}
// append back to $value
$value[$i] = $post;
}
// convert back from array if neccessary
if (!$field['multiple']) {
$value = array_shift($value);
}
// return value
return $value;
}