* @contributor Brian Zoetewey * @version 1.0 * */ class ACF_Location_Field extends acf_Field { /* * Base directory * @var string * */ private $base_dir; /* * Relative Uri from the WordPress ABSPATH constant * @var string * */ private $base_uri_rel; /* * Absolute Uri * * This is used to create urls to CSS and JavaScript files. * @var string * */ private $base_uri_abs; /* * WordPress Localization Text Domain * * The textdomain for the field is controlled by the helper class. * @var string * */ private $l10n_domain; /*-------------------------------------------------------------------------------------- * * Constructor * - This function is called when the field class is initalized on each page. * - Here you can add filters / actions and setup any other functionality for your field * * @author Elliot Condon * @since 2.2.0 * *-------------------------------------------------------------------------------------*/ public function __construct($parent) { //Call parent constructor parent::__construct($parent); //Get the textdomain from the Helper class $this->l10n_domain = ACF_Location_Field_Helper::L10N_DOMAIN; //Base directory of this field $this->base_dir = rtrim( dirname( realpath( __FILE__ ) ), DIRECTORY_SEPARATOR ); //Build the base relative uri by searching backwards until we encounter the wordpress ABSPATH //This may not work if the $base_dir contains a symlink outside of the WordPress ABSPATH $root = array_pop( explode( DIRECTORY_SEPARATOR, rtrim( realpath( ABSPATH ), DIRECTORY_SEPARATOR ) ) ); $path_parts = explode( DIRECTORY_SEPARATOR, $this->base_dir ); $parts = array(); while( $part = array_pop( $path_parts ) ) { if( $part == $root ) break; array_unshift( $parts, $part ); } $this->base_uri_rel = '/' . implode( '/', $parts ); $this->base_uri_abs = get_template_directory_uri()."/googlemap"; // set name / title $this->name = 'location-field'; // variable name (no spaces / special characters / etc) $this->title = __( 'Location', $this->l10n_domain ); // field label (Displayed in edit screens) add_action( 'admin_print_scripts', array( &$this, 'admin_print_scripts' ), 12, 0 ); add_action( 'admin_print_styles', array( &$this, 'admin_print_styles' ), 12, 0 ); } /*-------------------------------------------------------------------------------------- * * admin_head * - this function is called in the admin_head of the edit screen where your field * is created. Use this function to create css and javascript to assist your * create_field() function. * * @author Elliot Condon * @since 2.2.0 * *-------------------------------------------------------------------------------------*/ public function admin_head() { ?> base_uri_abs . '/style.css' ); if( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { wp_enqueue_style( 'acf-location-field' ); } } public function admin_print_scripts() { global $pagenow; //wp_register_script( 'acf-location-field', $this->base_uri_abs . '/js/script.max.js', array( 'jquery' ) ); if( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { wp_enqueue_script( 'acf-location-field' ); } } /*-------------------------------------------------------------------------------------- * * set_field_defaults * - populates the fields array with defaults for this field type * * @param array $field * @return array * *-------------------------------------------------------------------------------------*/ private function set_field_defaults(&$field) { $field['center'] = isset($field['center']) ? $field['center'] : '48.856614,2.3522219000000177'; $field['zoom'] = isset($field['zoom']) ? $field['zoom'] : '2'; $field['val'] = isset($field['val']) ? $field['val'] : 'address'; } /*-------------------------------------------------------------------------------------- * * create_options * - this function is called from core/field_meta_box.php to create extra options * for your field * * @params * - $key (int) - the $_POST obejct key required to save the options to the field * - $field (array) - the field object * * @author Elliot Condon * @since 2.2.0 * *-------------------------------------------------------------------------------------*/ public function create_options($key, $field) { $this->set_field_defaults($field); ?>

parent->create_field(array( 'type' => 'radio', 'name' => 'fields['.$key.'][val]', 'value' => $field['val'], 'layout' => 'horizontal', 'choices' => array( 'address' => __('Yes', 'acf-location-field'), 'coordinates' => __('No', 'acf-location-field') ) )); ?>

parent->create_field(array( 'type' => 'text', 'name' => 'fields['.$key.'][center]', 'value' => $field['center'] )); ?>

parent->create_field(array( 'type' => 'text', 'name' => 'fields['.$key.'][zoom]', 'value' => $field['zoom'] )); ?> set_field_defaults($field); // Build an unique id based on ACF's one. $pattern = array('/\[/', '/\]/'); $replace = array('_', ''); $uid = preg_replace($pattern, $replace, $field['name']); // Retrieve options value $zoom = $field['zoom']; $center = explode(',', $field['center']); ?>
 
 
get_value($post_id, $field); // format value $value = explode('|', $value); if ($field['val'] == 'address') { $value = array( 'coordinates' => isset($value[1]) ? $value[1] : $value[0] , 'address' => $value[0] ); } else { $value = $value[0]; } // return value return $value; } } endif; //class_exists 'ACF_Location_Field' if( !class_exists( 'ACF_Location_Field_Helper' ) ) : /* * Advanced Custom Fields - Location Field Helper * * @author Brian Zoetewey * */ class ACF_Location_Field_Helper { /* * Singleton instance * @var ACF_Location_Field_Helper * */ private static $instance; /* * Returns the ACF_Location_Field_Helper singleton * * $obj = ACF_Location_Field_Helper::singleton(); * @return ACF_Location_Field_Helper * */ public static function singleton() { if( !isset( self::$instance ) ) { $class = __CLASS__; self::$instance = new $class(); } return self::$instance; } /* * Prevent cloning of the ACF_Location_Field_Helper object * @internal * */ private function __clone() { } /* * WordPress Localization Text Domain * * Used in wordpress localization and translation methods. * @var string * */ const L10N_DOMAIN = 'acf-location-field'; /* * Language directory path * * Used to build the path for WordPress localization files. * @var string * */ private $lang_dir; /* * Constructor * */ private function __construct() { $this->lang_dir = rtrim( dirname( realpath( __FILE__ ) ), '/' ) . '/lang'; add_action( 'init', array( &$this, 'register_field' ), 5, 0 ); add_action( 'init', array( &$this, 'load_textdomain' ), 2, 0 ); } /* * Registers the Field with Advanced Custom Fields * */ public function register_field() { if( function_exists( 'register_field' ) ) { register_field( 'ACF_Location_Field', __FILE__ ); } } /* * Loads the textdomain for the current locale if it exists * */ public function load_textdomain() { $locale = get_locale(); $mofile = $this->lang_dir . '/' . self::L10N_DOMAIN . '-' . $locale . '.mo'; load_textdomain( self::L10N_DOMAIN, $mofile ); } } endif; //class_exists 'ACF_Location_Field_Helper' //Instantiate the Addon Helper class ACF_Location_Field_Helper::singleton(); ?>