* @version 1.0.1 */ class ACF_Address_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; /** * Class Constructor - Instantiates a new Address Field * @param Acf $parent Parent Acf class */ public function __construct( $parent ) { parent::__construct( $parent ); //Get the textdomain from the Helper class $this->l10n_domain = ACF_Address_Field_Helper::L10N_DOMAIN; $this->base_dir = rtrim( dirname( realpath( __FILE__ ) ), DIRECTORY_SEPARATOR ); //Build the base relative uri by searching backwards until we encounter the wordpress ABSPATH $root = array_pop( explode( DIRECTORY_SEPARATOR, rtrim( 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_site_url( null, $this->base_uri_rel ); $this->name = 'address-field'; $this->title = __( 'Address', $this->l10n_domain ); add_action( 'admin_print_scripts', array( &$this, 'admin_print_scripts' ), 12, 0 ); add_action( 'admin_print_styles', array( &$this, 'admin_print_styles' ), 12, 0 ); } /** * Registers and enqueues necessary CSS * * This method is called by ACF when rendering a post add or edit screen. * We also call this method on the Acf Field Options screen as well in order * to style out Field options * * @see acf_Field::admin_print_styles() */ public function admin_print_styles() { global $pagenow; wp_register_style( 'acf-address-field', $this->base_uri_abs . '/address-field.css' ); if( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { wp_enqueue_style( 'acf-address-field' ); } } /** * Registers and enqueues necessary JavaScript * * This method is called by ACF when rendering a post add or edit screen. * We also call this method on the Acf Field Options screen as well in order * to add the necessary JavaScript for address layout. * * @see acf_Field::admin_print_scripts() */ public function admin_print_scripts() { global $pagenow; wp_register_script( 'acf-address-field', $this->base_uri_abs . '/address-field.js', array( 'jquery-ui-sortable' ) ); if( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) { wp_enqueue_script( 'acf-address-field' ); } } /** * Populates the fields array with defaults for this field type * * @param array $field * @return array */ private function set_field_defaults( &$field ) { $component_defaults = array( 'address1' => array( 'label' => __( 'Address 1', $this->l10n_domain ), 'default_value' => '', 'enabled' => 1, 'class' => 'address1', 'separator' => '', ), 'address2' => array( 'label' => __( 'Address 2', $this->l10n_domain ), 'default_value' => '', 'enabled' => 1, 'class' => 'address2', 'separator' => '', ), 'address3' => array( 'label' => __( 'Address 3', $this->l10n_domain ), 'default_value' => '', 'enabled' => 1, 'class' => 'address3', 'separator' => '', ), 'city' => array( 'label' => __( 'City', $this->l10n_domain ), 'default_value' => '', 'enabled' => 1, 'class' => 'city', 'separator' => ',', ), 'state' => array( 'label' => __( 'State', $this->l10n_domain ), 'default_value' => '', 'enabled' => 1, 'class' => 'state', 'separator' => '', ), 'postal_code' => array( 'label' => __( 'Postal Code', $this->l10n_domain ), 'default_value' => '', 'enabled' => 1, 'class' => 'postal_code', 'separator' => '', ), 'country' => array( 'label' => __( 'Country', $this->l10n_domain ), 'default_value' => '', 'enabled' => 1, 'class' => 'country', 'separator' => '', ), ); $layout_defaults = array( 0 => array( 0 => 'address1' ), 1 => array( 0 => 'address2' ), 2 => array( 0 => 'address3' ), 3 => array( 0 => 'city', 1 => 'state', 2 => 'postal_code', 3 => 'country' ), ); $field[ 'address_components' ] = ( array_key_exists( 'address_components' , $field ) && is_array( $field[ 'address_components' ] ) ) ? wp_parse_args( (array) $field[ 'address_components' ], $component_defaults ) : $component_defaults; $field[ 'address_layout' ] = ( array_key_exists( 'address_layout', $field ) && is_array( $field[ 'address_layout' ] ) ) ? (array) $field[ 'address_layout' ] : $layout_defaults; return $field; } /** * Creates the address field for inside post metaboxes * * @see acf_Field::create_field() */ public function create_field( $field ) { $this->set_field_defaults( $field ); $components = $field[ 'address_components' ]; $layout = $field[ 'address_layout' ]; $values = (array) $field[ 'value' ]; ?>
set_field_defaults( $field ); $components = $field[ 'address_components' ]; $layout = $field[ 'address_layout' ]; $missing = array_keys( $components ); ?>

l10n_domain ); ?>: l10n_domain ); ?>
l10n_domain ); ?>: l10n_domain ); ?>
l10n_domain ); ?>: l10n_domain ); ?>
l10n_domain ); ?>: l10n_domain ); ?>
l10n_domain ); ?>: l10n_domain ); ?>

$settings ) : ?>
l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?>
l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?> l10n_domain ); ?>
parent->create_field( array( 'type' => 'true_false', 'name' => "fields[{$key}][address_components][{$name}][enabled]", 'value' => $settings[ 'enabled' ], 'class' => 'address_enabled', ) ); ?> parent->create_field( array( 'type' => 'text', 'name' => "fields[{$key}][address_components][{$name}][label]", 'value' => $settings[ 'label' ], 'class' => 'address_label', ) ); ?> parent->create_field( array( 'type' => 'text', 'name' => "fields[{$key}][address_components][{$name}][default_value]", 'value' => $settings[ 'default_value' ], 'class' => 'address_default_value', ) ); ?> parent->create_field( array( 'type' => 'text', 'name' => "fields[{$key}][address_components][{$name}][class]", 'value' => $settings[ 'class' ], 'class' => 'address_class', ) ); ?> parent->create_field( array( 'type' => 'text', 'name' => "fields[{$key}][address_components][{$name}][separator]", 'value' => $settings[ 'separator' ], 'class' => 'address_separator', ) ); ?>

l10n_domain ); ?>

set_field_defaults( $field ); $components = $field[ 'address_components' ]; $defaults = array(); foreach( $components as $name => $settings ) $defaults[ $name ] = $settings[ 'default_value' ]; $value = (array) parent::get_value( $post_id, $field ); $value = wp_parse_args($value, $defaults); return $value; } /** * Returns the value of the field for the advanced custom fields API * * @see acf_Field::get_value_for_api() * @param int $post_id * @param array $field */ public function get_value_for_api( $post_id, $field ) { $this->set_field_defaults( $field ); $components = $field[ 'address_components' ]; $layout = $field[ 'address_layout' ]; $values = $this->get_value( $post_id, $field ); $output = ''; foreach( $layout as $layout_row ) { if( empty( $layout_row ) ) continue; $output .= '
'; foreach( $layout_row as $name ) { if( empty( $name ) || !$components[ $name ][ 'enabled' ] ) continue; $output .= sprintf( '%1$s%3$s ', $values[ $name ], $components[ $name ][ 'class' ] ? 'class="' . esc_attr( $components[ $name ][ 'class' ] ) . '"' : '', $components[ $name ][ 'separator' ] ? esc_html( $components[ $name ][ 'separator' ] ) : '' ); } $output .= '
'; } return $output; } } endif; //class_exists 'ACF_Address_Field' if( !class_exists( 'ACF_Address_Field_Helper' ) ) : /** * Advanced Custom Fields - Address Field Helper * * This class is a Helper for the ACF_Address_Field class. * * It provides: * Localization support and registering the textdomain with WordPress. * Registering the address field with Advanced Custom Fields. There is no need in your plugin or theme * to manually call the register_field() method, just include this file. * include_once( rtrim( dirname( __FILE__ ), '/' ) . '/acf-address-field/address-field.php' ); * * @author Brian Zoetewey * @todo Provide shortcode support for address fields */ class ACF_Address_Field_Helper { /** * WordPress Localization Text Domain * * Used in wordpress localization and translation methods. * @var string */ const L10N_DOMAIN = 'acf-address-field'; /** * Singleton instance * @var ACF_Address_Field_Helper */ private static $instance; /** * Returns the ACF_Address_Field_Helper singleton * * $obj = ACF_Address_Field_Helper::singleton(); * @return ACF_Address_Field_Helper */ public static function singleton() { if( !isset( self::$instance ) ) { $class = __CLASS__; self::$instance = new $class(); } return self::$instance; } /** * Prevent cloning of the ACF_Address_Field_Helper object * @internal */ private function __clone() { } /** * 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__ ) ), '/' ) . '/languages'; add_action( 'init', array( &$this, 'register_address_field' ), 5, 0 ); add_action( 'init', array( &$this, 'load_textdomain' ), 2, 0 ); } /** * Registers the Address Field with Advanced Custom Fields */ public function register_address_field() { if( function_exists( 'register_field' ) ) { register_field( 'ACF_Address_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_Address_Field_Helper' //Instantiate the Addon Helper class ACF_Address_Field_Helper::singleton();