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