path = plugin_dir_path(__FILE__); $this->dir = plugins_url('',__FILE__); $this->version = '3.3.2'; $this->upgrade_version = '3.2.5'; // this is the latest version which requires an upgrade $this->cache = array(); // basic array cache to hold data throughout the page load // set text domain load_plugin_textdomain('acf', false, basename(dirname(__FILE__)).'/lang' ); // controllers $this->setup_controllers(); // actions add_action('init', array($this, 'init')); add_filter('post_updated_messages', array($this, 'post_updated_messages')); add_filter('manage_edit-acf_columns', array($this, 'acf_columns_filter')); add_action('admin_menu', array($this,'admin_menu')); add_action('admin_head', array($this,'admin_head')); add_action('wp_ajax_get_input_metabox_ids', array($this, 'get_input_metabox_ids')); return true; } /* * get_cache * * @description: Simple ACF (once per page) cache * @since 3.1.9 * @created: 23/06/12 */ function get_cache($key = false) { // key is required if( !$key ) return false; // does cache at key exist? if( !isset($this->cache[$key]) ) return false; // return cahced item return $this->cache[$key]; } /* * set_cache * * @description: Simple ACF (once per page) cache * @since 3.1.9 * @created: 23/06/12 */ function set_cache($key = false, $value = null) { // key is required if( !$key ) return false; // update the cache array $this->cache[$key] = $value; // return true. Probably not needed return true; } /* * setup_fields * * @description: Create an array of field objects, including custom registered field types * @since 1.0.0 * @created: 23/06/12 */ function setup_fields() { // vars $return = array(); // include parent field include_once('core/fields/acf_field.php'); // include child fields include_once('core/fields/acf_field.php'); include_once('core/fields/text.php'); include_once('core/fields/textarea.php'); include_once('core/fields/wysiwyg.php'); include_once('core/fields/image.php'); include_once('core/fields/file.php'); include_once('core/fields/select.php'); include_once('core/fields/checkbox.php'); include_once('core/fields/radio.php'); include_once('core/fields/true_false.php'); include_once('core/fields/page_link.php'); include_once('core/fields/post_object.php'); include_once('core/fields/relationship.php'); include_once('core/fields/date_picker/date_picker.php'); include_once('core/fields/color_picker.php'); // add child fields $return['text'] = new acf_Text($this); $return['textarea'] = new acf_Textarea($this); $return['wysiwyg'] = new acf_Wysiwyg($this); $return['image'] = new acf_Image($this); $return['file'] = new acf_File($this); $return['select'] = new acf_Select($this); $return['checkbox'] = new acf_Checkbox($this); $return['radio'] = new acf_Radio($this); $return['true_false'] = new acf_True_false($this); $return['page_link'] = new acf_Page_link($this); $return['post_object'] = new acf_Post_object($this); $return['relationship'] = new acf_Relationship($this); $return['date_picker'] = new acf_Date_picker($this); $return['color_picker'] = new acf_Color_picker($this); // add repeater if($this->is_field_unlocked('repeater')) { include_once('core/fields/repeater.php'); $return['repeater'] = new acf_Repeater($this); } // add flexible content if($this->is_field_unlocked('flexible_content')) { include_once('core/fields/flexible_content.php'); $return['flexible_content'] = new acf_Flexible_content($this); } // add gallery if($this->is_field_unlocked('gallery')) { include_once('core/fields/gallery.php'); $return['gallery'] = new acf_Gallery($this); } // hook to load in third party fields $custom = apply_filters('acf_register_field',array()); if(!empty($custom)) { foreach($custom as $v) { //var_dump($v['url']); include($v['url']); $name = $v['class']; $custom_field = new $name($this); $return[$custom_field->name] = $custom_field; } } // set all the fields $this->fields = $return; } /* * setup_fields * * @description: * @since 3.2.6 * @created: 23/06/12 */ function setup_controllers() { // Settings include_once('core/controllers/settings.php'); $this->settings = new acf_settings($this); // upgrade include_once('core/controllers/upgrade.php'); $this->upgrade = new acf_upgrade($this); // field_groups include_once('core/controllers/field_groups.php'); $this->field_groups = new acf_field_groups($this); // field_group include_once('core/controllers/field_group.php'); $this->field_group = new acf_field_group($this); // input include_once('core/controllers/input.php'); $this->input = new acf_input($this); // options page include_once('core/controllers/options_page.php'); $this->options_page = new acf_options_page($this); // everthing fields include_once('core/controllers/everything_fields.php'); $this->everything_fields = new acf_everything_fields($this); } /* * admin_menu * * @description: * @since 1.0.0 * @created: 23/06/12 */ function admin_menu() { // add acf page to options menu add_utility_page(__("Custom Fields",'acf'), __("Custom Fields",'acf'), 'manage_options', 'edit.php?post_type=acf'); } /* * Init * * @description: * @since 1.0.0 * @created: 23/06/12 */ function init() { // setup fields $this->setup_fields(); // Create ACF post type $labels = array( 'name' => __( 'Field Groups', 'acf' ), 'singular_name' => __( 'Advanced Custom Fields', 'acf' ), 'add_new' => __( 'Add New' , 'acf' ), 'add_new_item' => __( 'Add New Field Group' , 'acf' ), 'edit_item' => __( 'Edit Field Group' , 'acf' ), 'new_item' => __( 'New Field Group' , 'acf' ), 'view_item' => __('View Field Group', 'acf'), 'search_items' => __('Search Field Groups', 'acf'), 'not_found' => __('No Field Groups found', 'acf'), 'not_found_in_trash' => __('No Field Groups found in Trash', 'acf'), ); register_post_type('acf', array( 'labels' => $labels, 'public' => false, 'show_ui' => true, '_builtin' => false, 'capability_type' => 'page', 'hierarchical' => true, 'rewrite' => false, 'query_var' => "acf", 'supports' => array( 'title', ), 'show_in_menu' => false, )); } /* * post_updated_messages * * @description: messages for saving a field group * @since 1.0.0 * @created: 23/06/12 */ function post_updated_messages( $messages ) { global $post, $post_ID; $messages['acf'] = array( 0 => '', // Unused. Messages start at index 1. 1 => __('Field group updated.', 'acf'), 2 => __('Custom field updated.', 'acf'), 3 => __('Custom field deleted.', 'acf'), 4 => __('Field group updated.', 'acf'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('Field group restored to revision from %s', 'acf'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => __('Field group published.', 'acf'), 7 => __('Field group saved.', 'acf'), 8 => __('Field group submitted.', 'acf'), 9 => __('Field group scheduled for.', 'acf'), 10 => __('Field group draft updated.', 'acf'), ); return $messages; } /* * acf_columns_filter * * @description: Custom Columns for ACF * @since 1.0.0 * @created: 23/06/12 */ function acf_columns_filter($columns) { $columns = array( 'cb' => '', 'title' => __("Title"), ); return $columns; } /*-------------------------------------------------------------------------------------- * * admin_head * * @author Elliot Condon * @since 1.0.0 * *-------------------------------------------------------------------------------------*/ function admin_head() { // vars global $post, $pagenow; // hide upgrade page from nav echo ''; } /*-------------------------------------------------------------------------------------- * * get_field_groups * * This function returns an array of post objects found in the get_pages and the * register_field_group calls. * * @author Elliot Condon * @since 3.0.6 * *-------------------------------------------------------------------------------------*/ function get_field_groups() { // return cache $cache = $this->get_cache('acf_field_groups'); if($cache != false) { return $cache; } // vars $acfs = array(); // get acf's $result = get_pages(array( 'numberposts' => -1, 'post_type' => 'acf', 'sort_column' => 'menu_order', 'order' => 'ASC', )); // populate acfs if($result) { foreach($result as $acf) { $acfs[] = array( 'id' => $acf->ID, 'title' => get_the_title($acf->ID), 'fields' => $this->get_acf_fields($acf->ID), 'location' => $this->get_acf_location($acf->ID), 'options' => $this->get_acf_options($acf->ID), 'menu_order' => $acf->menu_order, ); } } // hook to load in registered field groups $acfs = apply_filters('acf_register_field_group', $acfs); // update cache $this->set_cache('acf_field_groups', $acfs); // return if(empty($acfs)) { return false; } return $acfs; } /*-------------------------------------------------------------------------------------- * * get_acf_fields * - returns an array of fields for a acf object * * @author Elliot Condon * @since 1.0.0 * *-------------------------------------------------------------------------------------*/ function get_acf_fields($post_id) { // vars $return = array(); $keys = get_post_custom_keys($post_id); if($keys) { foreach($keys as $key) { if(strpos($key, 'field_') !== false) { $field = $this->get_acf_field($key, $post_id); $return[$field['order_no']] = $field; } } ksort($return); } // return fields return $return; } /*-------------------------------------------------------------------------------------- * * get_acf_field * - returns a field * * @author Elliot Condon * @since 1.0.0 * *-------------------------------------------------------------------------------------*/ function get_acf_field($field_name, $post_id = false) { // vars $post_id = $post_id ? $post_id : $this->get_post_meta_post_id($field_name); $field = false; // if this acf ($post_id) is trashed don't use it's fields if(get_post_status($post_id) != "trash") { $field = get_post_meta($post_id, $field_name, true); } // field could be registered via php, and not in db at all! if(!$field) { // hook to load in registered field groups $acfs = apply_filters('acf_register_field_group', array()); if($acfs) { // loop through acfs foreach($acfs as $acf) { // loop through fields if($acf['fields']) { foreach($acf['fields'] as $field) { if($field['key'] == $field_name) { return $field; } } } // if($acf['fields']) } // foreach($acfs as $acf) } // if($acfs) } // if(!$field) return $field; } /*-------------------------------------------------------------------------------------- * * get_post_meta_post_id * - returns the post_id for a meta_key * * @author Elliot Condon * @since 1.0.0 * *-------------------------------------------------------------------------------------*/ function get_post_meta_post_id($field_name) { global $wpdb; $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $field_name) ); if($post_id) return (int)$post_id; return false; } /*-------------------------------------------------------------------------------------- * * create_field * * @author Elliot Condon * @since 1.0.0 * *-------------------------------------------------------------------------------------*/ function create_field($field) { if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']])) { _e('Error: Field Type does not exist!','acf'); return false; } // defaults if(!isset($field['class'])) $field['class'] = $field['type']; $this->fields[$field['type']]->create_field($field); } /*-------------------------------------------------------------------------------------- * * get_acf_location * * @author Elliot Condon * @since 1.0.0 * *-------------------------------------------------------------------------------------*/ function get_acf_location($post_id) { // vars $return = array( 'rules' => array(), 'allorany' => get_post_meta($post_id, 'allorany', true) ? get_post_meta($post_id, 'allorany', true) : 'all', ); // get all fields $rules = get_post_meta($post_id, 'rule', false); if($rules) { foreach($rules as $rule) { $return['rules'][$rule['order_no']] = $rule; } } ksort($return['rules']); // return fields return $return; } /*-------------------------------------------------------------------------------------- * * get_acf_options * * @author Elliot Condon * @since 1.0.0 * *-------------------------------------------------------------------------------------*/ function get_acf_options($post_id) { // defaults $options = array( 'position' => get_post_meta($post_id, 'position', true) ? get_post_meta($post_id, 'position', true) : 'normal', 'layout' => get_post_meta($post_id, 'layout', true) ? get_post_meta($post_id, 'layout', true) : 'default', 'hide_on_screen' => get_post_meta($post_id, 'hide_on_screen', true) ? get_post_meta($post_id, 'hide_on_screen', true) : array(), ); // return return $options; } /*-------------------------------------------------------------------------------------- * * get_value * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function get_value($post_id, $field) { if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']])) { return false; } return $this->fields[$field['type']]->get_value($post_id, $field); } /*-------------------------------------------------------------------------------------- * * get_value_for_api * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function get_value_for_api($post_id, $field) { if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']])) { return ''; } return $this->fields[$field['type']]->get_value_for_api($post_id, $field); } /*-------------------------------------------------------------------------------------- * * update_value * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function update_value($post_id, $field, $value) { $this->fields[$field['type']]->update_value($post_id, $field, $value); } /*-------------------------------------------------------------------------------------- * * update_field * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function update_field($post_id, $field) { // format the field (select, repeater, etc) $field = $this->pre_save_field($field); // save it! update_post_meta($post_id, $field['key'], $field); } /*-------------------------------------------------------------------------------------- * * pre_save_field * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function pre_save_field($field) { // format the field (select, repeater, etc) return $this->fields[$field['type']]->pre_save_field($field); } /*-------------------------------------------------------------------------------------- * * format_value_for_api * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function format_value_for_api($value, $field) { return $this->fields[$field['type']]->format_value_for_api($value, $field); } /*-------------------------------------------------------------------------------------- * * create_format_data * * @author Elliot Condon * @since 3.0.0 * *-------------------------------------------------------------------------------------*/ function create_format_data($field) { return $this->fields[$field['type']]->create_format_data($field); } /* * render_fields_for_input * * @description: * @since 3.1.6 * @created: 23/06/12 */ function render_fields_for_input($fields, $post_id) { // create fields if($fields) { foreach($fields as $field) { // if they didn't select a type, skip this field if($field['type'] == 'null') continue; // set value $field['value'] = $this->get_value($post_id, $field); // required if(!isset($field['required'])) { $field['required'] = "0"; } $required_class = ""; $required_label = ""; if($field['required'] == "1") { $required_class = ' required'; $required_label = ' *'; } echo '
'; echo ''; echo $field['instructions']; echo '
'; $field['name'] = 'fields[' . $field['key'] . ']'; $this->create_field($field); echo '