. * * Field type implementation for the legacy ACF 4 field API * (`acf/register_fields`). ACF 5 and later use fields-v5.php. */ class acfmc_field_multiple_coordinates_v4 extends acf_field { public $defaults; // default field options /* * __construct */ public function __construct() { $this->name = 'multiple-coordinates-field'; $this->label = __('Coordinates map', 'advanced-custom-fields-multiple-coordinates'); $this->category = __('Content', 'advanced-custom-fields-multiple-coordinates'); // default values sets the map to center around Fliseryd/Mönserås Småland $this->defaults = array( 'center' => array( 'lat' => 57.13485135696844, 'lng' => 16.292744633789084), 'zoom' => 11 ); // call parent acf_field's constructor parent::__construct(); // settings $this->settings = array( 'path' => ACFMC_PATH, 'dir' => ACFMC_URL, 'version' => ACFMC_VERSION ); } /* * input_admin_enqueue_scripts * * This action is called in the admin_enqueue_scripts action on the edit screen where your field is created. * Use this action to add css + javascript to assist your create_field() action. */ public function input_admin_enqueue_scripts() { // Google Maps API key — sajt-ägaren konfigurerar via ACF:s globala // setting (`acf_get_setting('google_api_key')`, samma nyckel som ACF:s // egna Google Map-field använder), eller via filter: // // add_filter('acfmc_gmaps_key', function () { return 'AIza…'; }); // // Utan nyckel laddar Google Maps i begränsat läge (watermark + modal). $gmaps_url = 'https://maps.googleapis.com/maps/api/js'; $gmaps_key = function_exists('acf_get_setting') ? (string) acf_get_setting('google_api_key') : ''; $gmaps_key = (string) apply_filters('acfmc_gmaps_key', $gmaps_key); if ($gmaps_key !== '') { $gmaps_url = add_query_arg('key', rawurlencode($gmaps_key), $gmaps_url); } // register scripts and css wp_register_style('acf-multiple-coordinates-field', $this->settings['dir'].'css/fields.css', array('wp-color-picker', 'dashicons'), ACFMC_VERSION); wp_register_script('googlemaps-api', $gmaps_url, array(), null, false); wp_register_script('acf-multiple-coordinates-map', $this->settings['dir'].'js/map.js', array('jquery', 'jquery-ui-sortable', 'wp-color-picker', 'googlemaps-api'), ACFMC_VERSION, false); // strings for the map UI wp_localize_script('acf-multiple-coordinates-map', 'acfmcL10n', acfmc_l10n_strings()); // load scripts and styles wp_enqueue_style('acf-multiple-coordinates-field'); wp_enqueue_script('googlemaps-api'); wp_enqueue_script('acf-multiple-coordinates-map'); } /* * create_field * * Create the HTML interface for the field. * * @param $field - an array containing the fields data */ public function create_field($field) { // load default data into the field $field = array_merge($this->defaults, $field); $lat = $field['center']['lat']; $lng = $field['center']['lng']; $zoom = $field['zoom']; // generate a unique id and put it as suffix to the fields name // the field name includes [ and ] so we just replace them with // more proper values for use in an html attribute $ptrns = array('/\[/', '/\]/'); $rplcs = array('_', ''); $uid = preg_replace($ptrns, $rplcs, $field['name']); // if field value is not a valid json string, create a new one with // the default values if (!json_decode($field['value'])) { $values = json_encode( array( 'zoom' => 11, 'coords' => array( array( 'lat' => $lat, 'lng' => $lng)))); } else { $values = $field['value']; } // Shape, style, colour and the marker switch are stored in the same // value as the points, and every one of them is optional — a value // without them draws no shape at all. $decoded = json_decode($values, true); acfmc_render_field_markup(array( 'uid' => $uid, 'name' => $field['name'], 'values' => $values, 'shape' => (is_array($decoded) && isset($decoded['shape'])) ? $decoded['shape'] : 'none', 'style' => (is_array($decoded) && isset($decoded['style'])) ? $decoded['style'] : 'sharp', 'color' => (is_array($decoded) && isset($decoded['color'])) ? $decoded['color'] : ACFMC_DEFAULT_COLOR, 'markers' => !(is_array($decoded) && isset($decoded['markers']) && !$decoded['markers']), )); } /* * format_value_for_api * * The data in the API is json encoded so we json decode * it as an array before returning it so its easily usable * * we also remove the zoom attribute since its only of importance * in the backend * * @param $value - the value loaded from the database * @param $post_id - the id of the loaded post * @param $field - the field array holding all the field options */ public function format_value_for_api($value, $post_id, $field) { $value_arr = json_decode($value, true); //unset($value_arr['zoom']); return $value_arr; } } new acfmc_field_multiple_coordinates_v4();