forked from constlab/acf-yandex-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acf-yandex-map-v4.php
303 lines (259 loc) · 9.54 KB
/
acf-yandex-map-v4.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'acf_field_yandex_map' ) ):
class acf_field_yandex_map extends acf_field {
public $settings,
$defaults;
/*
* __construct
*
* Set name / label needed for actions / filters
*
* @since 3.6
* @date 23/01/13
*/
public function __construct( $settings ) {
$this->name = 'yandex-map';
$this->label = __( 'Yandex Map', YA_MAP_LANG_DOMAIN );
$this->category = __( 'jQuery', 'acf' ); // Basic, Content, Choice, etc
$this->defaults = array(
'height' => '400',
'center_lat' => '55.7522200',
'center_lng' => '37.6155600',
'zoom' => '14',
'map_type' => 'map'
);
// do not delete!
parent::__construct();
// settings
$this->settings = $settings;
}
/*
* create_options()
*
* Create extra options for your field. This is rendered when editing a field.
* The value of $field['name'] can be used (like below) to save extra data to the $field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field - an array holding all the field's data
*/
public function create_options( $field ) {
// defaults?
/*
$field = array_merge($this->defaults, $field);
*/
// key is needed in the field names to correctly save the data
$key = $field['name'];
// Create Field Options HTML
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __( 'Height', YA_MAP_LANG_DOMAIN ) ?></label>
<p class="description"><?php echo __( 'Set map height', YA_MAP_LANG_DOMAIN ) ?></p>
</td>
<td>
<?php
do_action( 'acf/create_field', array(
'type' => 'number',
'name' => 'fields[' . $key . '][height]',
'value' => $field['height'],
'layout' => 'horizontal',
'append' => 'px'
) );
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __( 'Map type', YA_MAP_LANG_DOMAIN ) ?></label>
</td>
<td>
<?php
do_action( 'acf/create_field', array(
'type' => 'select',
'name' => 'fields[' . $key . '][map_type]',
'value' => $field['map_type'],
'layout' => 'horizontal',
'choices' => array(
'map' => __( 'Map', YA_MAP_LANG_DOMAIN ),
'satellite' => __( 'Satellite', YA_MAP_LANG_DOMAIN ),
'hybrid' => __( 'Hybrid', YA_MAP_LANG_DOMAIN ),
)
) );
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __( 'Zoom', YA_MAP_LANG_DOMAIN ) ?></label>
<p class="description"><?php echo __( 'Set map zoom', YA_MAP_LANG_DOMAIN ) ?></p>
</td>
<td>
<?php
do_action( 'acf/create_field', array(
'type' => 'number',
'name' => 'fields[' . $key . '][zoom]',
'value' => $field['zoom'],
'layout' => 'horizontal',
'min' => '10',
'max' => '18'
) );
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __( 'Center', YA_MAP_LANG_DOMAIN ) ?></label>
<p class="description"><?php echo __( 'Center the initial map', YA_MAP_LANG_DOMAIN ) ?></p>
</td>
<td>
<?php
do_action( 'acf/create_field', array(
'type' => 'text',
'name' => 'fields[' . $key . '][center_lat]',
'value' => $field['center_lat'],
'layout' => 'horizontal',
'prepend' => 'lat',
'placeholder' => $this->defaults['center_lat']
) );
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php echo __( 'Center', YA_MAP_LANG_DOMAIN ) ?></label>
<p class="description"><?php echo __( 'Center the initial map', YA_MAP_LANG_DOMAIN ) ?></p>
</td>
<td>
<?php
do_action( 'acf/create_field', array(
'type' => 'text',
'name' => 'fields[' . $key . '][center_lng]',
'value' => $field['center_lng'],
'layout' => 'horizontal',
'prepend' => 'lng',
'placeholder' => $this->defaults['center_lng']
) );
?>
</td>
</tr>
<?php
}
/*
* create_field()
*
* Create the HTML interface for your field
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
public function create_field( $field ) {
wp_enqueue_script( 'acf-yandex' );
$saved = json_decode( $field['value'], true );
$data = array();
$data['center_lat'] = ( $saved['center_lat'] ) ?: $field['center_lat'];
$data['center_lng'] = ( $saved['center_lng'] ) ?: $field['center_lng'];
$data['zoom'] = ( $saved['zoom'] ) ?: $field['zoom'];
$data['type'] = $this->get_map_type( $saved['type'], $field );
$data['marks'] = ( $saved['marks'] ) ?: array();
?>
<div>
<input type="hidden" name="<?php echo esc_attr( $field['name'] ) ?>"
value="<?php echo esc_attr( wp_json_encode( $data ) ) ?>"
class="map-input"/>
<table class="form-table">
<tr>
<th style="width: 20%"><?php echo __( 'Marker type', YA_MAP_LANG_DOMAIN ) ?></th>
<td style="width: 30%">
<select class="marker-type">
<option value="point" selected><?php echo __( 'Point', YA_MAP_LANG_DOMAIN ) ?></option>
<option value="circle"><?php echo __( 'Circle', YA_MAP_LANG_DOMAIN ) ?></option>
</select>
</td>
<th><span class="circle hidden"><?php echo __( 'Circle radius', YA_MAP_LANG_DOMAIN ) ?></span>
</th>
<td>
<?php
$m_str = __( 'm.', YA_MAP_LANG_DOMAIN );
$km_str = __( 'km.', YA_MAP_LANG_DOMAIN );
?>
<select class="circle-size circle hidden">
<option value="250" selected>250<?php echo $m_str ?></option>
<option value="500">500<?php echo $m_str ?></option>
<option value="1000">1<?php echo $km_str ?></option>
<option value="4000">4<?php echo $km_str ?></option>
<option value="10000">10<?php echo $km_str ?></option>
</select>
</td>
</tr>
</table>
<div class="map"
style="width: auto;height:<?php echo ( esc_attr( $field['height'] ) ) ?: $this->defaults['height'] ?>px"></div>
</div>
<?php
}
/*
* 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.
*
* $info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
* @type action
* @since 3.6
* @date 23/01/13
*/
public function input_admin_enqueue_scripts() {
// Note: This function can be removed if not used
// vars
$url = $this->settings['url'];
$version = $this->settings['version'];
$dir = plugin_dir_url( __FILE__ );
wp_register_script( 'yandex-map-api', '//api-maps.yandex.ru/2.1/?lang=' . get_bloginfo( 'language' ), array( 'jquery' ), null );
wp_register_script( 'acf-yandex', "{$dir}js/acf-yandex-map.min.js", array( 'yandex-map-api' ), null, true );
wp_localize_script( 'acf-yandex', 'acf_yandex_locale', array(
'map_init_fail' => __( 'Error init Yandex map! Field not found.', YA_MAP_LANG_DOMAIN ),
'mark_hint' => __( 'Drag mark. Right click for remove', YA_MAP_LANG_DOMAIN ),
'btn_clear_all' => __( 'Clear all', YA_MAP_LANG_DOMAIN ),
'btn_clear_all_hint' => __( 'Remove all marks', YA_MAP_LANG_DOMAIN ),
'btn_import' => __( 'Import map from json', YA_MAP_LANG_DOMAIN ),
'import_go' => __( 'Import', YA_MAP_LANG_DOMAIN ),
'import_desc' => __( 'Paste exported json', YA_MAP_LANG_DOMAIN ),
'btn_export' => __( 'Export map to json string', YA_MAP_LANG_DOMAIN ),
'export_desc' => __( 'Copy it', YA_MAP_LANG_DOMAIN ),
'mark_save' => __( 'Save', YA_MAP_LANG_DOMAIN ),
'mark_remove' => __( 'Remove', YA_MAP_LANG_DOMAIN ),
) );
}
/**
* Validate and return map type
*
* @param string $value
* @param array $field
*
* @return string
*/
private function get_map_type( $value, $field ) {
$allowed = array(
'map',
'satellite',
'hybrid'
);
$result = in_array( trim( $value ), $allowed, true ) ? $value : $field['map_type'];
if ( ! $result ) {
$result = $this->defaults['map_type'];
}
return $result;
}
}
new acf_field_yandex_map( [] );
endif;