| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. |
| 2 |
/** |
| 3 |
* |
| 4 |
* Field: map |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* @version 1.0.0 |
| 8 |
* |
| 9 |
*/ |
| 10 |
if ( ! class_exists( 'ADMINIFY_Field_map' ) ) { |
| 11 |
class ADMINIFY_Field_map extends ADMINIFY_Fields { |
| 12 |
|
| 13 |
public $version = '1.7.1'; |
| 14 |
public $cdn_url = 'https://cdn.jsdelivr.net/npm/leaflet@'; |
| 15 |
|
| 16 |
public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) { |
| 17 |
parent::__construct( $field, $value, $unique, $where, $parent ); |
| 18 |
} |
| 19 |
|
| 20 |
public function render() { |
| 21 |
|
| 22 |
$args = wp_parse_args( $this->field, array( |
| 23 |
'placeholder' => esc_html__( 'Search...', 'adminify' ), |
| 24 |
'latitude_text' => esc_html__( 'Latitude', 'adminify' ), |
| 25 |
'longitude_text' => esc_html__( 'Longitude', 'adminify' ), |
| 26 |
'address_field' => '', |
| 27 |
'height' => '', |
| 28 |
) ); |
| 29 |
|
| 30 |
$value = wp_parse_args( $this->value, array( |
| 31 |
'address' => '', |
| 32 |
'latitude' => '20', |
| 33 |
'longitude' => '0', |
| 34 |
'zoom' => '2', |
| 35 |
) ); |
| 36 |
|
| 37 |
$default_settings = array( |
| 38 |
'center' => array( $value['latitude'], $value['longitude'] ), |
| 39 |
'zoom' => $value['zoom'], |
| 40 |
'scrollWheelZoom' => false, |
| 41 |
); |
| 42 |
|
| 43 |
$settings = ( ! empty( $this->field['settings'] ) ) ? $this->field['settings'] : array(); |
| 44 |
$settings = wp_parse_args( $settings, $default_settings ); |
| 45 |
|
| 46 |
$style_attr = ( ! empty( $args['height'] ) ) ? ' style="min-height:'. esc_attr( $args['height'] ) .';"' : ''; |
| 47 |
$placeholder = ( ! empty( $args['placeholder'] ) ) ? array( 'placeholder' => $args['placeholder'] ) : ''; |
| 48 |
|
| 49 |
echo $this->field_before(); |
| 50 |
|
| 51 |
if ( empty( $args['address_field'] ) ) { |
| 52 |
echo '<div class="adminify--map-search">'; |
| 53 |
echo '<input type="text" name="'. esc_attr( $this->field_name( '[address]' ) ) .'" value="'. esc_attr( $value['address'] ) .'"'. $this->field_attributes( $placeholder ) .' />'; |
| 54 |
echo '</div>'; |
| 55 |
} else { |
| 56 |
echo '<div class="adminify--address-field" data-address-field="'. esc_attr( $args['address_field'] ) .'"></div>'; |
| 57 |
} |
| 58 |
|
| 59 |
echo '<div class="adminify--map-osm-wrap"><div class="adminify--map-osm" data-map="'. esc_attr( json_encode( $settings ) ) .'"'. $style_attr .'></div></div>'; |
| 60 |
|
| 61 |
echo '<div class="adminify--map-inputs">'; |
| 62 |
|
| 63 |
echo '<div class="adminify--map-input">'; |
| 64 |
echo '<label>'. esc_attr( $args['latitude_text'] ) .'</label>'; |
| 65 |
echo '<input type="text" name="'. esc_attr( $this->field_name( '[latitude]' ) ) .'" value="'. esc_attr( $value['latitude'] ) .'" class="adminify--latitude" />'; |
| 66 |
echo '</div>'; |
| 67 |
|
| 68 |
echo '<div class="adminify--map-input">'; |
| 69 |
echo '<label>'. esc_attr( $args['longitude_text'] ) .'</label>'; |
| 70 |
echo '<input type="text" name="'. esc_attr( $this->field_name( '[longitude]' ) ) .'" value="'. esc_attr( $value['longitude'] ) .'" class="adminify--longitude" />'; |
| 71 |
echo '</div>'; |
| 72 |
|
| 73 |
echo '</div>'; |
| 74 |
|
| 75 |
echo '<input type="hidden" name="'. esc_attr( $this->field_name( '[zoom]' ) ) .'" value="'. esc_attr( $value['zoom'] ) .'" class="adminify--zoom" />'; |
| 76 |
|
| 77 |
echo $this->field_after(); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
public function enqueue() { |
| 82 |
|
| 83 |
if ( ! wp_script_is( 'adminify-leaflet' ) ) { |
| 84 |
wp_enqueue_script( 'adminify-leaflet', esc_url( $this->cdn_url . $this->version .'/dist/leaflet.js' ), array( 'adminify' ), $this->version, true ); |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! wp_style_is( 'adminify-leaflet' ) ) { |
| 88 |
wp_enqueue_style( 'adminify-leaflet', esc_url( $this->cdn_url . $this->version .'/dist/leaflet.css' ), array(), $this->version ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! wp_script_is( 'jquery-ui-autocomplete' ) ) { |
| 92 |
wp_enqueue_script( 'jquery-ui-autocomplete' ); |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
} |
| 99 |
|