1
|
|
|
<?php |
2
|
|
|
defined( 'ABSPATH' ) || die; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* The Open Street Map field. |
6
|
|
|
*/ |
7
|
|
|
class RWMB_OSM_Field extends RWMB_Field { |
8
|
|
|
public static function admin_enqueue_scripts() { |
9
|
|
|
self::enqueue_map_assets(); |
10
|
|
|
|
11
|
|
|
wp_enqueue_style( 'rwmb-osm', RWMB_CSS_URL . 'osm.css', [ 'leaflet' ], RWMB_VER ); |
12
|
|
|
wp_style_add_data( 'rwmb-osm', 'path', RWMB_CSS_DIR . 'osm.css' ); |
13
|
|
|
wp_enqueue_script( 'rwmb-osm', RWMB_JS_URL . 'osm.js', [ 'jquery', 'jquery-ui-autocomplete', 'leaflet' ], RWMB_VER, true ); |
14
|
|
|
|
15
|
|
|
RWMB_Helpers_Field::localize_script_once( 'rwmb-osm', 'RWMB_Osm', [ |
16
|
|
|
'no_results_string' => __( 'No results found', 'meta-box' ), |
17
|
|
|
] ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get field HTML. |
22
|
|
|
* |
23
|
|
|
* @param mixed $meta Meta value. |
24
|
|
|
* @param array $field Field parameters. |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public static function html( $meta, $field ) { |
29
|
|
|
$address = is_array( $field['address_field'] ) ? implode( ',', $field['address_field'] ) : $field['address_field']; |
30
|
|
|
$html = sprintf( |
31
|
|
|
'<div class="rwmb-osm-field" data-address-field="%s">', |
32
|
|
|
esc_attr( $address ) |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$attributes = self::get_attributes( $field, $meta ); |
36
|
|
|
$attributes['type'] = 'hidden'; |
37
|
|
|
$attributes['value'] = $meta; |
38
|
|
|
|
39
|
|
|
$html .= sprintf( |
40
|
|
|
'<div class="rwmb-osm-canvas" data-default-loc="%s" data-region="%s" data-language="%s"></div> |
41
|
|
|
<input %s>', |
42
|
|
|
esc_attr( $field['std'] ), |
43
|
|
|
esc_attr( $field['region'] ), |
44
|
|
|
esc_attr( $field['language'] ), |
45
|
|
|
self::render_attributes( $attributes ) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$html .= '</div>'; |
49
|
|
|
|
50
|
|
|
return $html; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Normalize parameters for field. |
55
|
|
|
* |
56
|
|
|
* @param array $field Field parameters. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public static function normalize( $field ) { |
61
|
|
|
$field = parent::normalize( $field ); |
62
|
|
|
$field = wp_parse_args( $field, [ |
63
|
|
|
'std' => '', |
64
|
|
|
'address_field' => '', |
65
|
|
|
'language' => '', |
66
|
|
|
'region' => '', |
67
|
|
|
] ); |
68
|
|
|
|
69
|
|
|
return $field; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the field value. |
74
|
|
|
* The difference between this function and 'meta' function is 'meta' function always returns the escaped value |
75
|
|
|
* of the field saved in the database, while this function returns more meaningful value of the field. |
76
|
|
|
* |
77
|
|
|
* @param array $field Field parameters. |
78
|
|
|
* @param array $args Not used for this field. |
79
|
|
|
* @param int|null $post_id Post ID. null for current post. Optional. |
80
|
|
|
* |
81
|
|
|
* @return mixed Array(latitude, longitude, zoom) |
82
|
|
|
*/ |
83
|
|
|
public static function get_value( $field, $args = [], $post_id = null ) { |
84
|
|
|
$value = parent::get_value( $field, $args, $post_id ); |
85
|
|
|
|
86
|
|
|
if ( is_array( $value ) ) { |
87
|
|
|
$location = []; |
88
|
|
|
foreach ( $value as $clone ) { |
89
|
|
|
list( $latitude, $longitude, $zoom ) = explode( ',', $clone . ',,' ); |
90
|
|
|
$location[] = compact( 'latitude', 'longitude', 'zoom' ); |
91
|
|
|
} |
92
|
|
|
return $location; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
list( $latitude, $longitude, $zoom ) = explode( ',', $value . ',,' ); |
96
|
|
|
return compact( 'latitude', 'longitude', 'zoom' ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Format value before render map |
101
|
|
|
* @param mixed $field |
102
|
|
|
* @param mixed $value |
103
|
|
|
* @param mixed $args |
104
|
|
|
* @param mixed $post_id |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public static function format_single_value( $field, $value, $args, $post_id ): string { |
108
|
|
|
return self::render_map( $value, $args ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Render a map in the frontend. |
113
|
|
|
* |
114
|
|
|
* @param string|array $location The "latitude,longitude[,zoom]" location. |
115
|
|
|
* @param array $args Additional arguments for the map. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public static function render_map( $location, $args = [] ) { |
120
|
|
|
// For compatibility with previous version, or within groups. |
121
|
|
|
if ( is_string( $location ) ) { |
122
|
|
|
list( $latitude, $longitude, $zoom ) = explode( ',', $location . ',,' ); |
123
|
|
|
} else { |
124
|
|
|
extract( $location ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ( ! $latitude || ! $longitude ) { |
128
|
|
|
return ''; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$args = wp_parse_args( $args, [ |
132
|
|
|
'latitude' => $latitude, |
133
|
|
|
'longitude' => $longitude, |
134
|
|
|
'width' => '100%', |
135
|
|
|
'height' => '480px', |
136
|
|
|
'marker' => true, // Display marker? |
137
|
|
|
'marker_title' => '', // Marker title, when hover. |
138
|
|
|
'info_window' => '', // Content of info window (when click on marker). HTML allowed. |
139
|
|
|
'js_options' => [], |
140
|
|
|
'zoom' => $zoom, |
141
|
|
|
] ); |
142
|
|
|
|
143
|
|
|
self::enqueue_map_assets(); |
144
|
|
|
wp_enqueue_script( 'rwmb-osm-frontend', RWMB_JS_URL . 'osm-frontend.js', [ 'jquery', 'leaflet' ], RWMB_VER, true ); |
145
|
|
|
wp_enqueue_style( 'rwmb-osm-frontend', RWMB_CSS_URL . 'osm-frontend.css', [], RWMB_VER ); |
146
|
|
|
wp_style_add_data( 'rwmb-osm-frontend', 'path', RWMB_CSS_DIR . 'osm-frontend.css' ); |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* More Open Street Map options |
150
|
|
|
* @link https://leafletjs.com/reference-1.5.0.html#map-option |
151
|
|
|
*/ |
152
|
|
|
$args['js_options'] = wp_parse_args( $args['js_options'], [ |
153
|
|
|
// Default to 'zoom' level set in admin, but can be overwritten. |
154
|
|
|
'zoom' => $args['zoom'], |
155
|
|
|
] ); |
156
|
|
|
|
157
|
|
|
$output = sprintf( |
158
|
|
|
'<div class="rwmb-osm-canvas" data-osm_options="%s" style="width:%s;height:%s"></div>', |
159
|
|
|
esc_attr( wp_json_encode( $args ) ), |
|
|
|
|
160
|
|
|
esc_attr( $args['width'] ), |
161
|
|
|
esc_attr( $args['height'] ) |
162
|
|
|
); |
163
|
|
|
return $output; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private static function enqueue_map_assets() { |
167
|
|
|
wp_enqueue_style( 'leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.css', [], '1.9.4' ); |
168
|
|
|
wp_enqueue_script( 'leaflet', 'https://unpkg.com/[email protected]/dist/leaflet.js', [], '1.9.4', true ); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|