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; }
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; }
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; }
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'); }
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; }
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); } }
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>'; }
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 } ?> </div> <div class="acf-gallery-toolbar"> <ul class="acf-hl"> <li> <a href="#" class="acf-button button button-primary acf-gallery-add"><?php _e('Add to gallery', 'acf'); ?> </a> </li> <li class="acf-fr"> <select class="acf-gallery-sort"> <option value=""><?php _e('Bulk actions', 'acf'); ?> </option> <option value="date"><?php _e('Sort by date uploaded', 'acf'); ?> </option> <option value="modified"><?php _e('Sort by date modified', 'acf'); ?> </option> <option value="title"><?php _e('Sort by title', 'acf'); ?> </option> <option value="reverse"><?php _e('Reverse current order', 'acf'); ?> </option> </select> </li> </ul> </div> </div> <div class="acf-gallery-side"> <div class="acf-gallery-side-inner"> <div class="acf-gallery-side-data"></div> <div class="acf-gallery-toolbar"> <ul class="acf-hl"> <li> <a href="#" class="acf-button button acf-gallery-close"><?php _e('Close', 'acf'); ?> </a> </li> <li class="acf-fr"> <a class="acf-button button button-primary acf-gallery-update"><?php _e('Update', 'acf'); ?> </a> </li> </ul> </div> </div> </div> </div> <?php }
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}"); } }
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; }
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; }
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; }
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; }
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; }
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; }
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(); // get id from widget_text[2] to widget_text-2 $setting->acf_id = str_replace(array('[', ']'), array('-', ''), $setting->id); // get acf value $setting->acf_value = acf_maybe_get($value, 'acf'); // append $widgets[] = $setting; } // bail ealry if no preview values if (empty($widgets)) { return false; } // return return $widgets; }
function acf_form_head() { // verify nonce if (acf_verify_nonce('acf_form')) { // validate data if (acf_validate_save_post(true)) { // form $GLOBALS['acf_form'] = acf_extract_var($_POST, '_acf_form'); $GLOBALS['acf_form'] = @json_decode(base64_decode($GLOBALS['acf_form']), true); // validate if (empty($GLOBALS['acf_form'])) { return; } // vars $post_id = acf_maybe_get($GLOBALS['acf_form'], 'post_id', 0); // allow for custom save $post_id = apply_filters('acf/pre_save_post', $post_id, $GLOBALS['acf_form']); // save acf_save_post($post_id); // vars $return = acf_maybe_get($GLOBALS['acf_form'], 'return', ''); // redirect if ($return) { // update %placeholders% $return = str_replace('%post_url%', get_permalink($post_id), $return); // redirect wp_redirect($return); exit; } } // if } // if // load acf scripts acf_enqueue_scripts(); }
function acf_is_ajax($action = '') { // vars $is_ajax = false; // check if is doing ajax if (defined('DOING_AJAX') && DOING_AJAX) { $is_ajax = true; } // check $action if ($action && acf_maybe_get($_POST, 'action') !== $action) { $is_ajax = false; } // return return $is_ajax; }
function render_field($field) { // convert value to array $field['value'] = acf_get_array($field['value'], false); // 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']); // 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; } } } // hidden input if ($field['ui']) { // restirct value $v = array_intersect($field['value'], $choices); if ($field['multiple']) { $v = implode('||', $v); } else { $v = acf_maybe_get($v, 0, ''); } acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'] . '-input', 'name' => $field['name'], 'value' => $v)); } elseif ($field['multiple']) { acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'] . '-input', 'name' => $field['name'])); } // 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 echo '<option ' . acf_esc_attr($el) . '>' . $label . '</option>'; } else { // echo echo '<' . $type . ' ' . acf_esc_attr($el) . '>'; } } } echo '</select>'; }
function load() { // $_POST if (acf_verify_nonce('activate_pro_licence')) { $this->activate_pro_licence(); } elseif (acf_verify_nonce('deactivate_pro_licence')) { $this->deactivate_pro_licence(); } // view $this->view = array('license' => '', 'active' => 0, 'current_version' => acf_get_setting('version'), 'remote_version' => '', 'update_available' => false, 'changelog' => '', 'upgrade_notice' => ''); // license if (acf_pro_is_license_active()) { $this->view['license'] = acf_pro_get_license_key(); $this->view['active'] = 1; } // vars $info = acf_get_remote_plugin_info(); // validate if (empty($info)) { return $this->show_remote_response_error(); } // add info to view $this->view['remote_version'] = $info['version']; // add changelog if the remote version is '>' than the current version if (acf_pro_is_update_available()) { $this->view['update_available'] = true; $this->view['changelog'] = acf_maybe_get($info, 'changelog'); $this->view['upgrade_notice'] = acf_maybe_get($info, 'upgrade_notice'); } // update transient acf_refresh_plugin_updates_transient(); }
function list_table_views($views) { // vars $class = ''; $total = count($this->sync); // active if (acf_maybe_get($_GET, 'post_status') === 'sync') { // actions add_action('admin_footer', array($this, 'sync_admin_footer'), 5); // set active class $class = ' class="current"'; // global global $wp_list_table; // update pagination $wp_list_table->set_pagination_args(array('total_items' => $total, 'total_pages' => 1, 'per_page' => $total)); } // add view $views['json'] = '<a' . $class . ' href="' . admin_url($this->url . '&post_status=sync') . '">' . __('Sync available', 'acf') . ' <span class="count">(' . $total . ')</span></a>'; // return return $views; }
function get_choices($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; } // vars $r = array(); $args = array(); $is_hierarchical = is_taxonomy_hierarchical($field['taxonomy']); $is_pagination = $options['paged'] > 0; $limit = 20; $offset = 20 * ($options['paged'] - 1); // hide empty $args['hide_empty'] = false; // pagination // - don't bother for hierarchial terms, we will need to load all terms anyway if (!$is_hierarchical && $is_pagination) { $args['offset'] = $offset; $args['number'] = $limit; } // 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_hierarchical) { // 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']); } // fake pagination if ($is_pagination) { $terms = array_slice($terms, $offset, $limit); } } /// 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; }
function verify_ajax() { // globals global $sitepress; // vars $lang = acf_maybe_get($_POST, 'lang'); // bail early if no lang if (!$lang) { return; } // switch lang // this will allow get_posts to work as expected (load posts from the correct language) $sitepress->switch_lang($_REQUEST['lang']); // remove post_id // this will prevent WPML from setting the current language based on the current post being edited // in theory, WPML is correct, however, when adding a new post, the post's lang is not found and will default to 'en' unset($_REQUEST['post_id']); }
function acf_get_attachment($post) { // get post if (!($post = get_post($post))) { return false; } // vars $thumb_id = 0; $id = $post->ID; $a = array('ID' => $id, 'id' => $id, 'title' => $post->post_title, 'filename' => wp_basename($post->guid), 'url' => wp_get_attachment_url($id), 'alt' => get_post_meta($id, '_wp_attachment_image_alt', true), 'author' => $post->post_author, 'description' => $post->post_content, 'caption' => $post->post_excerpt, 'name' => $post->post_name, 'date' => $post->post_date_gmt, 'modified' => $post->post_modified_gmt, 'mime_type' => $post->post_mime_type, 'type' => acf_maybe_get(explode('/', $post->post_mime_type), 0, ''), 'icon' => wp_mime_type_icon($id)); // video may use featured image if ($a['type'] === 'image') { $thumb_id = $id; $src = wp_get_attachment_image_src($id, 'full'); $a['url'] = $src[0]; $a['width'] = $src[1]; $a['height'] = $src[2]; } elseif ($a['type'] === 'audio' || $a['type'] === 'video') { // video dimentions if ($a['type'] == 'video') { $meta = wp_get_attachment_metadata($id); $a['width'] = acf_maybe_get($meta, 'width', 0); $a['height'] = acf_maybe_get($meta, 'height', 0); } // feature image if ($featured_id = get_post_thumbnail_id($id)) { $thumb_id = $featured_id; } } // sizes if ($thumb_id) { // find all image sizes if ($sizes = get_intermediate_image_sizes()) { $a['sizes'] = array(); foreach ($sizes as $size) { // url $src = wp_get_attachment_image_src($thumb_id, $size); // add src $a['sizes'][$size] = $src[0]; $a['sizes'][$size . '-width'] = $src[1]; $a['sizes'][$size . '-height'] = $src[2]; } } } // return return $a; }
function render_field($field) { // enqueue acf_enqueue_uploader(); // vars $posts = array(); $atts = array('id' => $field['id'], 'class' => "acf-gallery {$field['class']}", 'data-preview_size' => $field['preview_size'], 'data-library' => $field['library'], 'data-min' => $field['min'], 'data-max' => $field['max'], 'data-mime_types' => $field['mime_types']); // set gallery height $height = acf_get_user_setting('gallery_height', 400); $height = max($height, 200); // minimum height is 200 $atts['style'] = "height:{$height}px"; // load posts if (!empty($field['value'])) { $posts = acf_get_posts(array('post_type' => 'attachment', 'post__in' => $field['value'])); } ?> <div <?php acf_esc_attr_e($atts); ?> > <div class="acf-hidden"> <input type="hidden" <?php acf_esc_attr_e(array('name' => $field['name'], 'value' => '', 'data-name' => 'ids')); ?> /> </div> <div class="acf-gallery-main"> <div class="acf-gallery-attachments"> <?php if (!empty($posts)) { ?> <?php foreach ($posts as $post) { // vars $type = acf_maybe_get(explode('/', $post->post_mime_type), 0); $thumb_id = $post->ID; $thumb_url = ''; $thumb_class = 'acf-gallery-attachment acf-soh'; $filename = wp_basename($post->guid); // thumb if ($type === 'image' || $type === 'audio' || $type === 'video') { // change $thumb_id if ($type === 'audio' || $type === 'video') { $thumb_id = get_post_thumbnail_id($post->ID); } // get attachment if ($thumb_id) { $thumb_url = wp_get_attachment_image_src($thumb_id, $field['preview_size']); $thumb_url = acf_maybe_get($thumb_url, 0); } } // fallback if (!$thumb_url) { $thumb_url = wp_mime_type_icon($post->ID); $thumb_class .= ' is-mime-icon'; } ?> <div class="<?php echo $thumb_class; ?> " data-id="<?php echo $post->ID; ?> "> <input type="hidden" name="<?php echo $field['name']; ?> []" value="<?php echo $post->ID; ?> " /> <div class="margin" title="<?php echo $filename; ?> "> <div class="thumbnail"> <img src="<?php echo $thumb_url; ?> "/> </div> <?php if ($type !== 'image') { ?> <div class="filename"><?php echo acf_get_truncated($filename, 18); ?> </div> <?php } ?> </div> <div class="actions acf-soh-target"> <a class="acf-icon dark remove-attachment" data-id="<?php echo $post->ID; ?> " href="#"> <i class="acf-sprite-delete"></i> </a> </div> </div> <?php } ?> <?php } ?> </div> <div class="acf-gallery-toolbar"> <ul class="acf-hl"> <li> <a href="#" class="acf-button blue add-attachment"><?php _e('Add to gallery', 'acf'); ?> </a> </li> <li class="acf-fr"> <select class="bulk-actions"> <option value=""><?php _e('Bulk actions', 'acf'); ?> </option> <option value="date"><?php _e('Sort by date uploaded', 'acf'); ?> </option> <option value="modified"><?php _e('Sort by date modified', 'acf'); ?> </option> <option value="title"><?php _e('Sort by title', 'acf'); ?> </option> <option value="reverse"><?php _e('Reverse current order', 'acf'); ?> </option> </select> </li> </ul> </div> </div> <div class="acf-gallery-side"> <div class="acf-gallery-side-inner"> <div class="acf-gallery-side-data"></div> <div class="acf-gallery-toolbar"> <ul class="acf-hl"> <li> <a href="#" class="acf-button close-sidebar"><?php _e('Close', 'acf'); ?> </a> </li> <li class="acf-fr"> <a class="acf-button blue update-attachment"><?php _e('Update', 'acf'); ?> </a> </li> </ul> </div> </div> </div> </div> <?php }
function acf_get_post_thumbnail($post = null, $size = 'thumbnail') { // vars $data = array('url' => '', 'type' => '', 'html' => ''); // post $post = get_post($post); // bail early if no post if (!$post) { return $data; } // vars $thumb_id = $post->ID; $mime_type = acf_maybe_get(explode('/', $post->post_mime_type), 0); // attachment if ($post->post_type === 'attachment') { // change $thumb_id if ($mime_type === 'audio' || $mime_type === 'video') { $thumb_id = get_post_thumbnail_id($post->ID); } // post } else { $thumb_id = get_post_thumbnail_id($post->ID); } // try url $data['url'] = wp_get_attachment_image_src($thumb_id, $size); $data['url'] = acf_maybe_get($data['url'], 0); // default icon if (!$data['url'] && $post->post_type === 'attachment') { $data['url'] = wp_mime_type_icon($post->ID); $data['type'] = 'icon'; } // html $data['html'] = '<img src="' . $data['url'] . '" alt="" />'; // return return $data; }
function save_widget() { // 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}"); } }
function import() { // validate if (empty($_FILES['acf_import_file'])) { acf_add_admin_notice(__("No file selected", 'acf'), 'error'); return; } // vars $file = $_FILES['acf_import_file']; // validate error if ($file['error']) { acf_add_admin_notice(__('Error uploading file. Please try again', 'acf'), 'error'); return; } // validate type if (pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json') { acf_add_admin_notice(__('Incorrect file type', 'acf'), 'error'); return; } // read file $json = file_get_contents($file['tmp_name']); // decode json $json = json_decode($json, true); // validate json if (empty($json)) { acf_add_admin_notice(__('Import file empty', 'acf'), 'error'); return; } // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $ids = array(); $keys = array(); $imported = array(); // populate keys foreach ($json as $field_group) { // append key $keys[] = $field_group['key']; } // look for existing ids foreach ($keys as $key) { // attempt find ID $field_group = _acf_get_field_group_by_key($key); // bail early if no field group if (!$field_group) { continue; } // append $ids[$key] = $field_group['ID']; } // enable local acf_enable_local(); // reset local (JSON class has already included .json field groups which may conflict) acf_reset_local(); // add local field groups foreach ($json as $field_group) { // add field group acf_add_local_field_group($field_group); } // loop over keys foreach ($keys as $key) { // vars $field_group = acf_get_local_field_group($key); // attempt get id $id = acf_maybe_get($ids, $key); if ($id) { $field_group['ID'] = $id; } // append fields if (acf_have_local_fields($key)) { $field_group['fields'] = acf_get_local_fields($key); } // import $field_group = acf_import_field_group($field_group); // append message $imported[] = array('ID' => $field_group['ID'], 'title' => $field_group['title'], 'updated' => $id ? 1 : 0); } // messages if (!empty($imported)) { // vars $links = array(); $count = count($imported); $message = sprintf(_n('Imported 1 field group', 'Imported %s field groups', $count, 'acf'), $count) . '.'; // populate links foreach ($imported as $import) { $links[] = '<a href="' . admin_url("post.php?post={$import['ID']}&action=edit") . '" target="_blank">' . $import['title'] . '</a>'; } // append links $message .= ' ' . implode(', ', $links); // add notice acf_add_admin_notice($message); } }
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 // - delete cache was origional added to ensure changes to JSON / PHP would appear in WP when using memcache // - the downside is that wp_cache_delet is taxing on the system so has been commented out //wp_cache_delete( "get_field/key={$key}", 'acf' ); //wp_cache_delete( "get_fields/parent={$parent}", 'acf' ); }
function allow_save_post($post) { // vars $allow = true; $reject = array('auto-draft', 'revision', 'acf-field', 'acf-field-group'); $wp_preview = acf_maybe_get($_POST, 'wp-preview'); // check post type if (in_array($post->post_type, $reject)) { $allow = false; } // allow preview if ($post->post_type == 'revision' && $wp_preview === 'dopreview') { $allow = true; } // return return $allow; }