public function __construct($args = array()) { //Some of these args will be changed in the future, not all are required at this point. $this->args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'return' => add_query_arg('updated', 'true', acf_get_current_url()), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp')); // filter post_id $this->args['post_id'] = acf_get_valid_post_id($this->args['post_id']); // load values from this post $this->post_id = $this->args['post_id']; if ($this->post_id == 'new_post') { throw new Exception(__('Post ID must be set when using the acf_view function', 'acf-views')); } //This will eventually be moved from an action into a better object oriented rendering method. add_action("acf/views_render_field", array($this, 'on_render_field'), 0, 1); }
function acf_maybe_get_field($selector, $post_id = false, $strict = true) { // complete init // this function may be used in a theme file before the init action has been run acf()->init(); // vars $field_name = false; // get valid post_id $post_id = acf_get_valid_post_id($post_id); // load field reference if not a field_key if (!acf_is_field_key($selector)) { // save selector as field_name (could be sub field name) $field_name = $selector; // get reference $field_key = acf_get_field_reference($selector, $post_id); if ($field_key) { $selector = $field_key; } elseif ($strict) { return false; } } // get field key $field = acf_get_field($selector); // bail early if no field if (!$field) { return false; } // Override name - allows the $selector to be a sub field (images_0_image) if ($field_name) { $field['name'] = $field_name; } // return return $field; }
/** * Get field group filters based on active screen. */ public function get_acf_field_group_filters() { global $post, $pagenow, $typenow, $plugin_page; $filter = array(); if ($pagenow === 'post.php' || $pagenow === 'post-new.php') { if ($typenow !== 'acf') { $filter['post_id'] = $post->ID; $filter['post_type'] = $typenow; } } elseif ($pagenow === 'admin.php' && isset($plugin_page)) { if (acf_get_options_page($plugin_page)) { $filter['post_id'] = acf_get_valid_post_id('options'); } } elseif ($pagenow === 'edit-tags.php' && isset($_GET['taxonomy'])) { $filter['taxonomy'] = filter_var($_GET['taxonomy'], FILTER_SANITIZE_STRING); } elseif ($pagenow === 'profile.php') { $filter['user_id'] = get_current_user_id(); $filter['user_form'] = 'edit'; } elseif ($pagenow === 'user-edit.php' && isset($_GET['user_id'])) { $filter['user_id'] = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT); $filter['user_form'] = 'edit'; } elseif ($pagenow === 'user-new.php') { $filter['user_id'] = 'new'; $filter['user_form'] = 'edit'; } elseif ($pagenow === 'media.php' || $pagenow === 'upload.php') { $filter['attachment'] = 'All'; } elseif (acf_is_screen('widgets') || acf_is_screen('customize')) { $filter['widget'] = 'all'; } return $filter; }
function acf_filter_post_id($post_id) { return acf_get_valid_post_id($post_id); }
function render_meta_box($post, $args) { // extract args extract($args); // all variables from the add_meta_box function extract($args); // all variables from the args argument // vars $o = array('id' => $id, 'key' => $field_group['key'], 'style' => $field_group['style'], 'edit_url' => '', 'edit_title' => __('Edit field group', 'acf'), 'visibility' => true); // vars $post_id = acf_get_valid_post_id('options'); // load fields $fields = acf_get_fields($field_group); // render if ($field_group['label_placement'] == 'left') { ?> <table class="acf-table"> <tbody> <?php acf_render_fields($post_id, $fields, 'tr', $field_group['instruction_placement']); ?> </tbody> </table> <?php } else { acf_render_fields($post_id, $fields, 'div', $field_group['instruction_placement']); } // edit_url if ($field_group['ID'] && acf_current_user_can_admin()) { $o['edit_url'] = admin_url('post.php?post=' . $field_group['ID'] . '&action=edit'); } ?> <script type="text/javascript"> if( typeof acf !== 'undefined' ) { acf.postbox.render(<?php echo json_encode($o); ?> ); } </script> <?php }
function get_field_choice_label($selector, $value, $post_id = false) { $post_id = acf_get_valid_post_id($post_id); $field = acf_maybe_get_field($selector, $post_id); if (!$field || !isset($field['choices']) || !isset($field['choices'][$value])) { return NULL; } return $field['choices'][$value]; }
function render_form($args = array()) { // vars $url = acf_get_current_url(); // defaults $args = wp_parse_args($args, array('id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg('updated', 'true', $url), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp', 'honeypot' => true)); $args['form_attributes'] = wp_parse_args($args['form_attributes'], array('id' => $args['id'], 'class' => 'acf-form', 'action' => '', 'method' => 'post')); // filter post_id $args['post_id'] = acf_get_valid_post_id($args['post_id']); // load values from this post $post_id = $args['post_id']; // new post? if ($post_id == 'new_post') { // dont load values $post_id = false; // new post defaults $args['new_post'] = wp_parse_args($args['new_post'], array('post_type' => 'post', 'post_status' => 'draft')); } // register local fields foreach ($this->fields as $k => $field) { acf_add_local_field($field); } // vars $field_groups = array(); $fields = array(); // post_title if ($args['post_title']) { // load local field $_post_title = acf_get_field('_post_title'); $_post_title['value'] = $post_id ? get_post_field('post_title', $post_id) : ''; // append $fields[] = $_post_title; } // post_content if ($args['post_content']) { // load local field $_post_content = acf_get_field('_post_content'); $_post_content['value'] = $post_id ? get_post_field('post_content', $post_id) : ''; // append $fields[] = $_post_content; } // specific fields if ($args['fields']) { foreach ($args['fields'] as $selector) { // append field ($strict = false to allow for better compatibility with field names) $fields[] = acf_maybe_get_field($selector, $post_id, false); } } elseif ($args['field_groups']) { foreach ($args['field_groups'] as $selector) { $field_groups[] = acf_get_field_group($selector); } } elseif ($args['post_id'] == 'new_post') { $field_groups = acf_get_field_groups($args['new_post']); } else { $field_groups = acf_get_field_groups(array('post_id' => $args['post_id'])); } //load fields based on field groups if (!empty($field_groups)) { foreach ($field_groups as $field_group) { $field_group_fields = acf_get_fields($field_group); if (!empty($field_group_fields)) { foreach (array_keys($field_group_fields) as $i) { $fields[] = acf_extract_var($field_group_fields, $i); } } } } // honeypot if ($args['honeypot']) { $fields[] = acf_get_field('_validate_email'); } // updated message if (!empty($_GET['updated']) && $args['updated_message']) { echo '<div id="message" class="updated"><p>' . $args['updated_message'] . '</p></div>'; } // uploader (always set incase of multiple forms on the page) acf_update_setting('uploader', $args['uploader']); // display form if ($args['form']) { ?> <form <?php acf_esc_attr_e($args['form_attributes']); ?> > <?php } // render post data acf_form_data(array('post_id' => $args['post_id'], 'nonce' => 'acf_form')); ?> <div class="acf-hidden"> <?php acf_hidden_input(array('name' => '_acf_form', 'value' => base64_encode(json_encode($args)))); ?> </div> <div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?> "> <?php // html before fields echo $args['html_before_fields']; // render acf_render_fields($post_id, $fields, $args['field_el'], $args['instruction_placement']); // html after fields echo $args['html_after_fields']; ?> </div><!-- acf-form-fields --> <?php if ($args['form']) { ?> <div class="acf-form-submit"> <input type="submit" class="acf-button button button-primary button-large" value="<?php echo $args['submit_value']; ?> " /> <span class="acf-spinner"></span> </div> </form> <?php } }
function render_meta_box($post, $args) { // extract args extract($args); // all variables from the add_meta_box function extract($args); // all variables from the args argument // vars $post_id = acf_get_valid_post_id('options'); $class = 'acf-postbox ' . $field_group['style']; $toggle_class = 'acf-postbox-toggle'; // load fields $fields = acf_get_fields($field_group); // render if ($field_group['label_placement'] == 'left') { ?> <table class="acf-table"> <tbody> <?php acf_render_fields($post_id, $fields, 'tr', $field_group['instruction_placement']); ?> </tbody> </table> <?php } else { acf_render_fields($post_id, $fields, 'div', $field_group['instruction_placement']); } // inline script ?> <div class="acf-hidden"> <script type="text/javascript"> (function($) { $('#<?php echo $id; ?> ').addClass('<?php echo $class; ?> ').removeClass('hide-if-js'); $('#adv-settings label[for="<?php echo $id; ?> -hide"]').addClass('<?php echo $toggle_class; ?> '); })(jQuery); </script> </div> <?php }
function postbox_acf($post, $args) { // extract args extract($args); // all variables from the add_meta_box function extract($args); // all variables from the args argument // vars $o = array('id' => $id, 'key' => $field_group['key'], 'style' => $field_group['style'], 'label' => $field_group['label_placement'], 'edit_url' => '', 'edit_title' => __('Edit field group', 'acf'), 'visibility' => true); // get post_id (allow lang modification) $post_id = acf_get_valid_post_id($this->page['post_id']); // edit_url if ($field_group['ID'] && acf_current_user_can_admin()) { $o['edit_url'] = admin_url('post.php?post=' . $field_group['ID'] . '&action=edit'); } // load fields $fields = acf_get_fields($field_group); // render acf_render_fields($post_id, $fields, 'div', $field_group['instruction_placement']); ?> <script type="text/javascript"> if( typeof acf !== 'undefined' ) { acf.postbox.render(<?php echo json_encode($o); ?> ); } </script> <?php }
function live_edit($fields = false, $post_id = false) { // validate fields if (!$fields) { return false; } // filter post_id $post_id = acf_get_valid_post_id($post_id); // turn array into string if (is_array($fields)) { $fields = implode(',', $fields); } // remove any white spaces from $fields $fields = str_replace(' ', '', $fields); // build atts acf_esc_attr_e(array('data-live-edit-id' => $post_id . '-' . str_replace(',', '-', $fields), 'data-live-edit-fields' => $fields, 'data-live-edit-post_id' => $post_id)); }