PluginProbe
Maps Plugin using Google Maps for WordPress – WP Google Map / trunk
Maps Plugin using Google Maps for WordPress – WP Google Map vtrunk
1.9.7 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 All 96 releases
gmap-embed / includes / traits / MapCRUD.php

MapCRUD.php in Maps Plugin using Google Maps for WordPress – WP Google Map trunk, at includes/traits/MapCRUD.php

674 lines 33.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WGMSRM\Traits;
4
5 use WP_Query;
6
7 if (!defined('ABSPATH')) {
8 exit;
9 }
10
11 /**
12 * Trait MapCRUD: Map CRUD operation doing here
13 */
14 trait MapCRUD
15 {
16
17 /**
18 * Get all maps for datatable ajax request
19 *
20 * @since 1.7.5
21 */
22 public function wgm_get_all_maps()
23 {
24 check_ajax_referer('wgm_get_all_maps', 'nonce');
25
26 if (!current_user_can($this->capability)) {
27 echo wp_json_encode(
28 array(
29 'responseCode' => 0,
30 'message' => esc_html__('Unauthorized access tried.', 'gmap-embed'),
31 )
32 );
33 wp_die();
34 }
35
36 $args = array(
37 'post_type' => 'wpgmapembed',
38 'posts_per_page' => -1,
39 'post_status' => 'draft',
40 );
41
42 $return_json = array();
43 $maps_list = new WP_Query($args);
44 while ($maps_list->have_posts()) {
45 $maps_list->the_post();
46 $title = esc_html(get_post_meta(get_the_ID(), 'wpgmap_title', true));
47 $type = esc_html(get_post_meta(get_the_ID(), 'wpgmap_map_type', true));
48 $width = esc_html(get_post_meta(get_the_ID(), 'wpgmap_map_width', true));
49 $height = esc_html(get_post_meta(get_the_ID(), 'wpgmap_map_height', true));
50 $shortcode = '<input class="wpgmap-shortcode regular-text" style="width:100%!important;" type="text" value="' . esc_attr('[gmap-embed id=&quot;' . get_the_ID() . '&quot;]') . '"
51 onclick="this.select()"/>';
52 $clone_btn = _wgm_is_premium()
53 ? '<button class="button media-button button-primary button-small wpgmap-clone" data-id="' . esc_attr(get_the_ID()) . '" title="' . esc_attr__('Clone Map', 'gmap-embed') . '" style="margin-right: 5px;"><i class="fas fa-copy"></i></button>'
54 : '<button class="button media-button button-primary button-small wgm_enable_premium" style="margin-right: 5px; opacity: 0.5;" title="' . esc_attr__('Clone Map (Premium)', 'gmap-embed') . '" data-notice="' . esc_attr(sprintf(__('You need to upgrade to the <a target="_blank" href="%s">Premium</a> Version to <b>Clone Maps</b>.', 'gmap-embed'), esc_url('https://wpgooglemap.com/pricing?utm_source=gmap-embed&utm_medium=wordpress-plugin&utm_campaign=upgrade-to-pro&utm_content=map-list-clone-lock'))) . '"><i class="fas fa-copy"></i></button>';
55 $action = '<button class="button media-button button-primary button-small wpgmap-copy-to-clipboard" data-id="' . esc_attr(get_the_ID()) . '" title="' . esc_attr__('Copy Shortcode', 'gmap-embed') . '" style="margin-right: 5px;"><i class="fas fa-code"></i></button>'
56 . '<a href="?page=wpgmapembed&tag=edit&id=' . esc_attr(get_the_ID()) . '&wgm_map_create_nonce=' . wp_create_nonce('wgm_create_map') . '" class="button media-button button-primary button-small wpgmap-edit" data-id="' . esc_attr(get_the_ID()) . '" title="' . esc_attr__('Edit Map', 'gmap-embed') . '" style="margin-right: 5px;"><i class="fas fa-edit"></i>
57 ' . esc_html__('Edit', 'gmap-embed') . '
58 </a>'
59 . $clone_btn . '<span type="button"
60 class="button media-button button-small wgm_wpgmap_delete" data-id="' . esc_attr(get_the_ID()) . '" title="' . esc_attr__('Delete Map', 'gmap-embed') . '" style="background-color: #aa2828;color: white;opacity:0.7;"><i class="fas fa-trash"></i> ' . esc_html__('Delete', 'gmap-embed') . '
61 </span>';
62 $row = array(
63 'id' => get_the_ID(),
64 'title' => $title,
65 'map_type' => $type,
66 'width' => $width,
67 'height' => $height,
68 'shortcode' => $shortcode,
69 'action' => $action,
70 );
71 $return_json[] = $row;
72 }
73
74 echo wp_json_encode(array('data' => $return_json));
75 wp_die();
76 }
77
78 /**
79 * To save New Map Data
80 */
81 public function save_wpgmapembed_data()
82 {
83 check_ajax_referer('wpgmapembed_save_map_data', 'nonce');
84
85 if (!current_user_can($this->capability)) {
86 echo wp_json_encode(
87 array(
88 'responseCode' => 0,
89 'message' => esc_html__('Unauthorized access tried.', 'gmap-embed'),
90 )
91 );
92 wp_die();
93 }
94
95 $error = '';
96 // Getting ajax fields value
97 $meta_data = array(
98 'wpgmap_title' => isset($_POST['map_data']['wpgmap_title']) ? sanitize_text_field(wp_strip_all_tags(wp_unslash($_POST['map_data']['wpgmap_title']))) : '',
99 'wpgmap_heading_class' => isset($_POST['map_data']['wpgmap_heading_class']) ? sanitize_html_class(wp_unslash($_POST['map_data']['wpgmap_heading_class'])) : '',
100 'wpgmap_show_heading' => isset($_POST['map_data']['wpgmap_show_heading']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_show_heading'])) : '',
101 'wpgmap_latlng' => isset($_POST['map_data']['wpgmap_latlng']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_latlng'])) : '',
102 'wpgmap_map_zoom' => isset($_POST['map_data']['wpgmap_map_zoom']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_map_zoom'])) : '',
103 'wpgmap_disable_zoom_scroll' => isset($_POST['map_data']['wpgmap_disable_zoom_scroll']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_disable_zoom_scroll'])) : '',
104 'wpgmap_map_width' => isset($_POST['map_data']['wpgmap_map_width']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_map_width'])) : '',
105 'wpgmap_map_height' => isset($_POST['map_data']['wpgmap_map_height']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_map_height'])) : '',
106 'wpgmap_map_type' => isset($_POST['map_data']['wpgmap_map_type']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_map_type'])) : '',
107 'wpgmap_show_infowindow' => isset($_POST['map_data']['wpgmap_show_infowindow']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_show_infowindow'])) : '',
108 'wpgmap_enable_direction' => isset($_POST['map_data']['wpgmap_enable_direction']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_enable_direction'])) : '',
109 'wpgmap_enable_modern_direction' => isset($_POST['map_data']['wpgmap_enable_modern_direction']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_enable_modern_direction'])) : '',
110 'wpgmap_center_lat_lng' => isset($_POST['map_data']['wpgmap_center_lat_lng']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_center_lat_lng'])) : '',
111 'wgm_theme_json' => isset($_POST['map_data']['wgm_theme_json']) ? sanitize_textarea_field(wp_unslash($_POST['map_data']['wgm_theme_json'])) : '',
112 'wpgmap_marker_listing_style' => isset($_POST['map_data']['wpgmap_marker_listing_style']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_marker_listing_style'])) : 'grid_view',
113 'wpgmap_marker_listing_placement' => isset($_POST['map_data']['wpgmap_marker_listing_placement']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_marker_listing_placement'])) : 'below_map',
114 'wpgmap_marker_listing_width' => isset($_POST['map_data']['wpgmap_marker_listing_width']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_marker_listing_width'])) : '',
115 'wgm_enable_store_locator' => isset($_POST['map_data']['wgm_enable_store_locator']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_enable_store_locator'])) : 0,
116 'wgm_enable_category_filter' => isset($_POST['map_data']['wgm_enable_category_filter']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_enable_category_filter'])) : 0,
117 // Store Locator Extended Settings
118 'wgm_enable_title_search' => isset($_POST['map_data']['wgm_enable_title_search']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_enable_title_search'])) : 0,
119 'wgm_title_search_placeholder' => isset($_POST['map_data']['wgm_title_search_placeholder']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_title_search_placeholder'])) : '',
120 'wgm_address_search_placeholder' => isset($_POST['map_data']['wgm_address_search_placeholder']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_address_search_placeholder'])) : '',
121 'wgm_default_address' => isset($_POST['map_data']['wgm_default_address']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_default_address'])) : '',
122 'wgm_default_radius' => isset($_POST['map_data']['wgm_default_radius']) ? intval(sanitize_text_field(wp_unslash($_POST['map_data']['wgm_default_radius']))) : '',
123 'wgm_not_found_message' => isset($_POST['map_data']['wgm_not_found_message']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_not_found_message'])) : '',
124
125 // Advanced Store Locator Settings
126 'wgm_hide_markers_until_search' => isset($_POST['map_data']['wgm_hide_markers_until_search']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_hide_markers_until_search'])) : 0,
127 'wgm_show_center_icon' => isset($_POST['map_data']['wgm_show_center_icon']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_show_center_icon'])) : 0,
128 'wgm_show_distance' => isset($_POST['map_data']['wgm_show_distance']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_show_distance'])) : 0,
129 'wgm_distance_unit' => isset($_POST['map_data']['wgm_distance_unit']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_distance_unit'])) : 'miles',
130 'wgm_sort_by_distance' => isset($_POST['map_data']['wgm_sort_by_distance']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_sort_by_distance'])) : 0,
131 'wgm_store_locator_placement' => isset($_POST['map_data']['wgm_store_locator_placement']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_store_locator_placement'])) : 'above_map',
132
133 // Circle Settings
134 'wgm_radius_circle_stroke_color' => isset($_POST['map_data']['wgm_radius_circle_stroke_color']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_radius_circle_stroke_color'])) : '#4285F4',
135 'wgm_radius_circle_stroke_opacity' => isset($_POST['map_data']['wgm_radius_circle_stroke_opacity']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_radius_circle_stroke_opacity'])) : '0.8',
136 'wgm_radius_circle_stroke_weight' => isset($_POST['map_data']['wgm_radius_circle_stroke_weight']) ? intval(sanitize_text_field(wp_unslash($_POST['map_data']['wgm_radius_circle_stroke_weight']))) : '2',
137 'wgm_radius_circle_fill_color' => isset($_POST['map_data']['wgm_radius_circle_fill_color']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_radius_circle_fill_color'])) : '#4285F4',
138 'wgm_radius_circle_fill_opacity' => isset($_POST['map_data']['wgm_radius_circle_fill_opacity']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_radius_circle_fill_opacity'])) : '0.2',
139
140 // Marker Listing Extra Settings
141 'marker_orderby_field' => isset($_POST['map_data']['marker_orderby_field']) ? sanitize_text_field(wp_unslash($_POST['map_data']['marker_orderby_field'])) : 'id',
142 'marker_orderby_dir' => isset($_POST['map_data']['marker_orderby_dir']) ? sanitize_text_field(wp_unslash($_POST['map_data']['marker_orderby_dir'])) : 'ASC',
143 'wpgmap_direction_drawer_width' => isset($_POST['map_data']['wpgmap_direction_drawer_width']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_direction_drawer_width'])) : '300',
144 'wgm_enable_show_on_map_icon' => isset($_POST['map_data']['wgm_enable_show_on_map_icon']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_enable_show_on_map_icon'])) : 0,
145 'wgm_enable_get_direction_icon' => isset($_POST['map_data']['wgm_enable_get_direction_icon']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_enable_get_direction_icon'])) : 0,
146 'wgm_enable_direction_link' => isset($_POST['map_data']['wgm_enable_direction_link']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wgm_enable_direction_link'])) : 0,
147 // Map Controls Customization
148 'wpgmap_zoom_control' => isset($_POST['map_data']['wpgmap_zoom_control']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_zoom_control'])) : '',
149 'wpgmap_zoom_control_pos' => isset($_POST['map_data']['wpgmap_zoom_control_pos']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_zoom_control_pos'])) : '',
150 'wpgmap_map_type_control' => isset($_POST['map_data']['wpgmap_map_type_control']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_map_type_control'])) : '',
151 'wpgmap_map_type_control_pos' => isset($_POST['map_data']['wpgmap_map_type_control_pos']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_map_type_control_pos'])) : '',
152 'wpgmap_street_view_control' => isset($_POST['map_data']['wpgmap_street_view_control']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_street_view_control'])) : '',
153 'wpgmap_street_view_control_pos' => isset($_POST['map_data']['wpgmap_street_view_control_pos']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_street_view_control_pos'])) : '',
154 'wpgmap_fullscreen_control' => isset($_POST['map_data']['wpgmap_fullscreen_control']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_fullscreen_control'])) : '',
155 'wpgmap_fullscreen_control_pos' => isset($_POST['map_data']['wpgmap_fullscreen_control_pos']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_fullscreen_control_pos'])) : '',
156 'wpgmap_rotate_control' => isset($_POST['map_data']['wpgmap_rotate_control']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_rotate_control'])) : '',
157 'wpgmap_rotate_control_pos' => isset($_POST['map_data']['wpgmap_rotate_control_pos']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_rotate_control_pos'])) : '',
158 'wpgmap_scale_control' => isset($_POST['map_data']['wpgmap_scale_control']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_scale_control'])) : '',
159 'wpgmap_scale_control_pos' => isset($_POST['map_data']['wpgmap_scale_control_pos']) ? sanitize_text_field(wp_unslash($_POST['map_data']['wpgmap_scale_control_pos'])) : '',
160 );
161 if (isset($meta_data['wpgmap_marker_listing_style'])) {
162 update_option('wpgmap_marker_listing_style', $meta_data['wpgmap_marker_listing_style']);
163 }
164 $decoded_theme = json_decode(sanitize_textarea_field(wp_unslash($meta_data['wgm_theme_json'])));
165 $meta_data['wgm_theme_json'] = (json_last_error() === JSON_ERROR_NONE && !is_null($decoded_theme)) ? wp_json_encode($decoded_theme) : '[]';
166 $action_type = isset($_POST['map_data']['action_type']) ? sanitize_text_field(wp_unslash($_POST['map_data']['action_type'])) : '';
167 if ($meta_data['wpgmap_latlng'] === '') {
168 $error = esc_html__('Please input Latitude and Longitude', 'gmap-embed');
169 }
170 if (strlen($error) > 0) {
171 echo wp_json_encode(
172 array(
173 'responseCode' => 0,
174 'message' => $error,
175 )
176 );
177 wp_die();
178 }
179
180 $post_id = 0;
181 if ($action_type === 'save') {
182 // Saving post array
183 $post_array = array(
184 'post_type' => 'wpgmapembed',
185 );
186 $post_id = wp_insert_post($post_array);
187 } elseif ($action_type === 'update') {
188 $post_id = isset($_POST['map_data']['post_id']) ? intval(sanitize_text_field(wp_unslash($_POST['map_data']['post_id']))) : 0;
189 }
190
191 // Updating post meta
192 foreach ($meta_data as $key => $value) {
193 $this->wgm_update_post_meta($post_id, $key, $value);
194 }
195 $return_array = array(
196 'responseCode' => 1,
197 'post_id' => intval($post_id),
198 );
199 if ($action_type === 'save') {
200 global $wpdb;
201 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for updating custom table
202 $wpdb->query(
203 $wpdb->prepare(
204 "UPDATE {$wpdb->prefix}wgm_markers SET map_id = %d WHERE map_id = %d",
205 intval($post_id),
206 0
207 )
208 );
209 $return_array['message'] = esc_html__('Map created Successfully.', 'gmap-embed');
210 } elseif ($action_type === 'update') {
211 $return_array['message'] = esc_html__('Map updated Successfully.', 'gmap-embed');
212 }
213 echo wp_json_encode($return_array);
214 wp_die();
215 }
216
217 /**
218 * Classic editor: Loading popup content on WP Google Map click
219 */
220 public function load_popup_wpgmapembed_list()
221 {
222 check_ajax_referer('wpgmapembed_popup_load_map_data', 'nonce');
223
224 if (!current_user_can($this->capability)) {
225 echo wp_json_encode(
226 array(
227 'responseCode' => 0,
228 'message' => esc_html__('Unauthorized access tried.', 'gmap-embed'),
229 )
230 );
231 wp_die();
232 }
233 $content = '';
234 $args = array(
235 'post_type' => 'wpgmapembed',
236 'posts_per_page' => -1,
237 'post_status' => 'draft',
238 );
239 $maps_list = new WP_Query($args);
240
241 while ($maps_list->have_posts()) {
242 $maps_list->the_post();
243 $title = get_post_meta(get_the_ID(), 'wpgmap_title', true);
244 $content .= '<div class="wp-gmap-single">
245 <div class="wp-gmap-single-left">
246 <div class="wp-gmap-single-title">
247 ' . esc_html($title) . '
248 </div>
249 <div class="wp-gmap-single-shortcode">
250 <input class="wpgmap-shortcode regular-text" type="text" value="[gmap-embed id=&quot;' . esc_attr(get_the_ID()) . '&quot;]"
251 onclick="this.select()"/>
252 </div>
253 </div>
254 <div class="wp-gmap-single-action">
255 <button type="button"
256 class="button media-button button-primary button-large wpgmap-insert-shortcode">
257 ' . esc_html__('Insert', 'gmap-embed') . '
258 </button>
259 </div>
260 </div>';
261 }
262 $allowed_html = [
263 'a' => [],
264 'br' => [],
265 'em' => [],
266 'strong' => [],
267 'div' => [
268 'class' => []
269 ],
270 'button' => [
271 'type' => [],
272 'class' => []
273 ],
274 'input' => [
275 'class' => [],
276 'value' => [],
277 'name' => [],
278 'onclick' => [],
279 'type' => [],
280 ],
281 ];
282 echo wp_kses(wp_unslash($content), $allowed_html);
283 wp_die();
284 }
285
286 /**
287 * Get map data by mnap id
288 *
289 * @param string $gmap_id
290 *
291 * @return false|string
292 */
293 public function get_wpgmapembed_data($gmap_id = 0)
294 {
295 if ($gmap_id == 0) {
296 $gmap_id = 0;
297 if (
298 isset($_POST['_wgm_nonce']) &&
299 wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wgm_nonce'])), '_wgm_nonce') &&
300 isset($_POST['wpgmap_id'])
301 ) {
302 $gmap_id = intval(sanitize_text_field(wp_unslash($_POST['wpgmap_id'])));
303 }
304 }
305
306 $gmap_data = array(
307 'wpgmap_id' => intval($gmap_id),
308 'wpgmap_title' => esc_html(get_post_meta($gmap_id, 'wpgmap_title', true)),
309 'wpgmap_heading_class' => esc_html(get_post_meta($gmap_id, 'wpgmap_heading_class', true)),
310 'wpgmap_show_heading' => esc_html(get_post_meta($gmap_id, 'wpgmap_show_heading', true)),
311 'wpgmap_latlng' => esc_html(get_post_meta($gmap_id, 'wpgmap_latlng', true)),
312 'wpgmap_map_zoom' => esc_html(get_post_meta($gmap_id, 'wpgmap_map_zoom', true)),
313 'wpgmap_disable_zoom_scroll' => esc_html(get_post_meta($gmap_id, 'wpgmap_disable_zoom_scroll', true)),
314 'wpgmap_map_width' => esc_html(get_post_meta($gmap_id, 'wpgmap_map_width', true)),
315 'wpgmap_map_height' => esc_html(get_post_meta($gmap_id, 'wpgmap_map_height', true)),
316 'wpgmap_map_type' => esc_html(get_post_meta($gmap_id, 'wpgmap_map_type', true)),
317 'wpgmap_show_infowindow' => esc_html(get_post_meta($gmap_id, 'wpgmap_show_infowindow', true)),
318 'wpgmap_enable_direction' => esc_html(get_post_meta($gmap_id, 'wpgmap_enable_direction', true)),
319 'wpgmap_enable_modern_direction' => esc_html(get_post_meta($gmap_id, 'wpgmap_enable_modern_direction', true)),
320 'wpgmap_direction_drawer_width' => esc_html(get_post_meta($gmap_id, 'wpgmap_direction_drawer_width', true)),
321 'wgm_theme_json' => wp_kses_data(get_post_meta($gmap_id, 'wgm_theme_json', true)),
322 'wpgmap_marker_listing_style' => esc_html(get_post_meta($gmap_id, 'wpgmap_marker_listing_style', true)),
323 'wpgmap_marker_listing_placement' => esc_html(get_post_meta($gmap_id, 'wpgmap_marker_listing_placement', true)),
324 'wpgmap_marker_listing_width' => esc_html(get_post_meta($gmap_id, 'wpgmap_marker_listing_width', true)),
325 'wgm_enable_store_locator' => esc_html(get_post_meta($gmap_id, 'wgm_enable_store_locator', true)),
326 'wgm_enable_category_filter' => esc_html(get_post_meta($gmap_id, 'wgm_enable_category_filter', true)),
327 // Store Locator Extended Settings
328 'wgm_enable_title_search' => esc_html(get_post_meta($gmap_id, 'wgm_enable_title_search', true)),
329 'wgm_title_search_placeholder' => esc_html(get_post_meta($gmap_id, 'wgm_title_search_placeholder', true)),
330 'wgm_address_search_placeholder' => esc_html(get_post_meta($gmap_id, 'wgm_address_search_placeholder', true)),
331 'wgm_default_address' => esc_html(get_post_meta($gmap_id, 'wgm_default_address', true)),
332 'wgm_default_radius' => esc_html(get_post_meta($gmap_id, 'wgm_default_radius', true)),
333 'wgm_not_found_message' => esc_html(get_post_meta($gmap_id, 'wgm_not_found_message', true)),
334
335 // Advanced Store Locator retrieval
336 'wgm_hide_markers_until_search' => esc_html(get_post_meta($gmap_id, 'wgm_hide_markers_until_search', true)),
337 'wgm_show_center_icon' => esc_html(get_post_meta($gmap_id, 'wgm_show_center_icon', true)),
338 'wgm_show_distance' => esc_html(get_post_meta($gmap_id, 'wgm_show_distance', true)),
339 'wgm_distance_unit' => esc_html(get_post_meta($gmap_id, 'wgm_distance_unit', true)),
340 'wgm_sort_by_distance' => esc_html(get_post_meta($gmap_id, 'wgm_sort_by_distance', true)),
341 'wgm_store_locator_placement' => esc_html(get_post_meta($gmap_id, 'wgm_store_locator_placement', true)),
342 // Circle Settings
343 'wgm_radius_circle_stroke_color' => esc_html(get_post_meta($gmap_id, 'wgm_radius_circle_stroke_color', true)),
344 'wgm_radius_circle_stroke_opacity' => esc_html(get_post_meta($gmap_id, 'wgm_radius_circle_stroke_opacity', true)),
345 'wgm_radius_circle_stroke_weight' => esc_html(get_post_meta($gmap_id, 'wgm_radius_circle_stroke_weight', true)),
346 'wgm_radius_circle_fill_color' => esc_html(get_post_meta($gmap_id, 'wgm_radius_circle_fill_color', true)),
347 'wgm_radius_circle_fill_opacity' => esc_html(get_post_meta($gmap_id, 'wgm_radius_circle_fill_opacity', true)),
348
349 // Marker Listing Extra Settings
350 'marker_orderby_field' => esc_html(get_post_meta($gmap_id, 'marker_orderby_field', true)),
351 'marker_orderby_dir' => esc_html(get_post_meta($gmap_id, 'marker_orderby_dir', true)),
352 'wpgmap_center_lat_lng' => esc_html(get_center_lat_lng_by_map_id($gmap_id)),
353 'wgm_enable_show_on_map_icon' => esc_html(get_post_meta($gmap_id, 'wgm_enable_show_on_map_icon', true)),
354 'wgm_enable_get_direction_icon' => esc_html(get_post_meta($gmap_id, 'wgm_enable_get_direction_icon', true)),
355 'wgm_enable_direction_link' => esc_html(get_post_meta($gmap_id, 'wgm_enable_direction_link', true)),
356 // Map Controls Customization
357 'wpgmap_zoom_control' => esc_html(get_post_meta($gmap_id, 'wpgmap_zoom_control', true)),
358 'wpgmap_zoom_control_pos' => esc_html(get_post_meta($gmap_id, 'wpgmap_zoom_control_pos', true)),
359 'wpgmap_map_type_control' => esc_html(get_post_meta($gmap_id, 'wpgmap_map_type_control', true)),
360 'wpgmap_map_type_control_pos' => esc_html(get_post_meta($gmap_id, 'wpgmap_map_type_control_pos', true)),
361 'wpgmap_street_view_control' => esc_html(get_post_meta($gmap_id, 'wpgmap_street_view_control', true)),
362 'wpgmap_street_view_control_pos' => esc_html(get_post_meta($gmap_id, 'wpgmap_street_view_control_pos', true)),
363 'wpgmap_fullscreen_control' => esc_html(get_post_meta($gmap_id, 'wpgmap_fullscreen_control', true)),
364 'wpgmap_fullscreen_control_pos' => esc_html(get_post_meta($gmap_id, 'wpgmap_fullscreen_control_pos', true)),
365 'wpgmap_rotate_control' => esc_html(get_post_meta($gmap_id, 'wpgmap_rotate_control', true)),
366 'wpgmap_rotate_control_pos' => esc_html(get_post_meta($gmap_id, 'wpgmap_rotate_control_pos', true)),
367 'wpgmap_scale_control' => esc_html(get_post_meta($gmap_id, 'wpgmap_scale_control', true)),
368 'wpgmap_scale_control_pos' => esc_html(get_post_meta($gmap_id, 'wpgmap_scale_control_pos', true)),
369 );
370 $gmap_data['wgm_theme_json'] = strlen($gmap_data['wgm_theme_json']) == 0 ? '[]' : wp_kses_data($gmap_data['wgm_theme_json']);
371 return wp_json_encode($gmap_data);
372 }
373
374 /**
375 * Remove map including post meta by map id
376 */
377 public function remove_wpgmapembed_data()
378 {
379 check_ajax_referer('wpgmapembed_remove_wpgmap', 'nonce');
380
381 if (!current_user_can($this->capability)) {
382 echo wp_json_encode(
383 [
384 'responseCode' => 0,
385 'message' => esc_html__('Unauthorized access tried.', 'gmap-embed'),
386 ]
387 );
388 wp_die();
389 }
390
391 $meta_data = [
392 'wpgmap_title',
393 'wpgmap_heading_class',
394 'wpgmap_show_heading',
395 'wpgmap_latlng',
396 'wpgmap_map_zoom',
397 'wpgmap_disable_zoom_scroll',
398 'wpgmap_map_width',
399 'wpgmap_map_height',
400 'wpgmap_map_type',
401 'wpgmap_show_infowindow',
402 'wpgmap_enable_direction',
403 'wpgmap_enable_modern_direction',
404 'wpgmap_direction_drawer_width',
405 'wpgmap_marker_listing_width',
406 'wgm_enable_store_locator',
407 'wgm_enable_category_filter',
408 'wgm_enable_title_search',
409 'wgm_enable_show_on_map_icon',
410 'wgm_enable_get_direction_icon',
411 'wgm_enable_direction_link',
412 'wpgmap_zoom_control',
413 'wpgmap_zoom_control_pos',
414 'wpgmap_map_type_control',
415 'wpgmap_map_type_control_pos',
416 'wpgmap_street_view_control',
417 'wpgmap_street_view_control_pos',
418 'wpgmap_fullscreen_control',
419 'wpgmap_fullscreen_control_pos',
420 'wpgmap_rotate_control',
421 'wpgmap_rotate_control_pos',
422 'wpgmap_scale_control',
423 'wpgmap_scale_control_pos',
424 ];
425
426 $post_id = isset($_POST['post_id']) ? \intval(sanitize_text_field(wp_unslash($_POST['post_id']))) : 0;
427 wp_delete_post($post_id);
428 foreach ($meta_data as $field_name) {
429 delete_post_meta($post_id, $field_name);
430 }
431 echo wp_json_encode(
432 [
433 'responseCode' => 1,
434 'message' => esc_html__('Deleted Successfully.', 'gmap-embed'),
435 ]
436 );
437 wp_die();
438 }
439
440
441 /**
442 * Create new map with default map and marker data.
443 *
444 * Sanitizes and escapes all data before saving to the database.
445 * Uses wp_insert_post for map creation and $wpdb->insert for marker creation.
446 * All values are sanitized and escaped according to WordPress coding standards.
447 *
448 * @return int $map_id The ID of the newly created map.
449 */
450 public function initiate_new_map($title = null)
451 {
452 // Set default meta data for new map
453 $meta_data = array(
454 'wpgmap_title' => $title ? $title : 'New Map',
455 'wpgmap_heading_class' => '',
456 'wpgmap_show_heading' => 0,
457 'wpgmap_map_zoom' => 4,
458 'wpgmap_map_width' => '100%',
459 'wpgmap_map_height' => '300px',
460 'wpgmap_map_type' => 'ROADMAP',
461 'wpgmap_show_infowindow' => 0,
462 'wpgmap_enable_direction' => 0,
463 'wpgmap_enable_modern_direction' => 0,
464 'wpgmap_direction_drawer_width' => '300',
465 'wpgmap_marker_listing_width' => '',
466 'wpgmap_center_lat_lng' => '40.779220392557676,-87.3700530411561',
467 'wpgmap_latlng' => '40.779220392557676,-87.3700530411561',
468 'wgm_theme_json' => '[]',
469 'wgm_enable_store_locator' => 0,
470 'wgm_enable_category_filter' => 0,
471 'wgm_enable_title_search' => 0,
472 'wgm_enable_show_on_map_icon' => 1,
473 'wgm_enable_get_direction_icon' => 1,
474 'wgm_enable_direction_link' => 0,
475 );
476
477 // Sanitize and encode theme JSON with validation
478 $decoded_theme = json_decode(sanitize_textarea_field($meta_data['wgm_theme_json']));
479 $meta_data['wgm_theme_json'] = (json_last_error() === JSON_ERROR_NONE && !is_null($decoded_theme)) ? wp_json_encode($decoded_theme) : '[]';
480
481 // Prepare post array
482 $post_array = array(
483 'post_type' => 'wpgmapembed',
484 'post_status' => 'draft',
485 'post_title' => sanitize_text_field($meta_data['wpgmap_title']),
486 );
487
488 // Insert new map post
489 $map_id = wp_insert_post($post_array);
490
491 // Ensure map_id is valid
492 $map_id = intval($map_id);
493
494 // Update post meta with sanitized values
495 foreach ($meta_data as $key => $value) {
496 $this->wgm_update_post_meta($map_id, sanitize_key($key), sanitize_text_field($value));
497 }
498
499 // Prepare demo marker data with sanitization
500 // COMMENTED OUT: Default marker creation disabled as per user request
501 /*
502 $map_marker_data = array(
503 'map_id' => $map_id,
504 'marker_name' => sanitize_text_field('Chicago'),
505 'marker_desc' => wp_kses_post(''),
506 'icon' => esc_url_raw('https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi2.png'),
507 'address' => sanitize_text_field(''),
508 'lat_lng' => sanitize_text_field('40.779220392557676,-87.3700530411561'),
509 'have_marker_link' => 0,
510 'marker_link' => esc_url_raw(''),
511 'marker_link_new_tab' => 0,
512 'show_desc_by_default' => 1,
513 );
514
515 // Merge with marker defaults
516 $defaults = $this->get_marker_default_values();
517 $wp_gmap_marker_data = wp_parse_args($map_marker_data, $defaults);
518
519 // Insert marker into custom table
520 global $wpdb;
521 $wpdb->insert(
522 $wpdb->prefix . 'wgm_markers',
523 array_map('sanitize_text_field', $wp_gmap_marker_data)
524 );
525 */
526
527 return $map_id;
528 }
529
530 /**
531 * Get all maps
532 *
533 * @param array $map_ids
534 * @return array
535 */
536 function get_all_maps($map_ids)
537 {
538 $maps = [];
539 if (empty($map_ids) || !is_array($map_ids)) {
540 return $maps;
541 }
542 foreach ($map_ids as $map_id) {
543 $map_id = intval($map_id);
544 $map = $this->get_map_by_id($map_id);
545 if ($map) {
546 $maps[] = $map;
547 }
548 }
549 return $maps;
550 }
551
552 /**
553 * Get a map by its ID with all associated map data fields from post meta.
554 *
555 * @param int $map_id
556 * @return array|null
557 */
558 public function get_map_by_id($map_id)
559 {
560 $map_id = intval($map_id);
561 $post = get_post($map_id);
562 if (!$post || $post->post_type !== 'wpgmapembed') {
563 return null;
564 }
565
566 $map = [
567 'id' => $post->ID,
568 'wpgmap_title' => $post->post_title,
569 ];
570
571 // Exhaustively get all post meta
572 $all_meta = get_post_custom($post->ID);
573 if ($all_meta && is_array($all_meta)) {
574 foreach ($all_meta as $key => $values) {
575 // We only care about map-specific keys to keep the export clean
576 // Usually start with wpgmap_, wgm_, or specific ordering keys
577 if (
578 strpos($key, 'wpgmap_') === 0 ||
579 strpos($key, 'wgm_') === 0 ||
580 strpos($key, 'marker_orderby_') === 0
581 ) {
582 $map[$key] = maybe_unserialize($values[0]);
583 }
584 }
585 }
586
587 return $map;
588 }
589
590 /**
591 * Clone an existing map (all its settings and markers) into a new draft map.
592 * Pro feature — availability is also enforced server-side here in addition
593 * to the client-side lock, since this is invoked directly over AJAX.
594 *
595 * @since 1.9.7
596 */
597 public function clone_wpgmapembed_data()
598 {
599 if (!_wgm_is_premium()) {
600 wp_send_json_error(array('message' => esc_html__('Cloning maps is a Premium feature. Please upgrade to unlock it.', 'gmap-embed')), 403);
601 }
602
603 $source_map_id = isset($_POST['map_id']) ? intval(sanitize_text_field(wp_unslash($_POST['map_id']))) : 0;
604 if ($source_map_id <= 0) {
605 wp_send_json_error(array('message' => esc_html__('Invalid map ID.', 'gmap-embed')));
606 }
607
608 $source_map = $this->get_map_by_id($source_map_id);
609 if (!$source_map) {
610 wp_send_json_error(array('message' => esc_html__('Source map not found.', 'gmap-embed')));
611 }
612
613 $original_title = isset($source_map['wpgmap_title']) ? $source_map['wpgmap_title'] : '';
614 // translators: %s: original map title.
615 $new_title = sprintf(__('%s (Copy)', 'gmap-embed'), $original_title);
616
617 $new_map_id = wp_insert_post(
618 array(
619 'post_type' => 'wpgmapembed',
620 )
621 );
622
623 if (is_wp_error($new_map_id) || !$new_map_id) {
624 wp_send_json_error(array('message' => esc_html__('Failed to create the cloned map.', 'gmap-embed')));
625 }
626
627 // Copy all map settings (post meta) from the source map.
628 foreach ($source_map as $key => $value) {
629 if ($key === 'id') {
630 continue;
631 }
632 $value = ($key === 'wpgmap_title') ? $new_title : $value;
633 $this->wgm_update_post_meta($new_map_id, $key, $value);
634 }
635
636 // Copy this map's markers to the new map.
637 global $wpdb;
638 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
639 $source_markers = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wgm_markers WHERE map_id = %d", $source_map_id), ARRAY_A);
640 $marker_defaults = $this->get_marker_default_values();
641
642 if (!empty($source_markers) && is_array($source_markers)) {
643 foreach ($source_markers as $marker) {
644 unset($marker['id']);
645 // A NULL text field (e.g. from a legacy/imported row) would otherwise be
646 // carried into the clone and break the marker-edit form (which expects
647 // strings, not null, for these inputs).
648 foreach (array('marker_desc', 'marker_image', 'address', 'marker_link', 'animation') as $text_field) {
649 if (!isset($marker[$text_field]) || is_null($marker[$text_field])) {
650 $marker[$text_field] = '';
651 }
652 }
653 $marker['map_id'] = $new_map_id;
654 $marker['created_at'] = current_time('mysql');
655 $marker['updated_at'] = current_time('mysql');
656 $marker['created_by'] = get_current_user_id();
657 $marker['updated_by'] = get_current_user_id();
658
659 $new_marker_data = wp_parse_args($marker, $marker_defaults);
660 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
661 $wpdb->insert("{$wpdb->prefix}wgm_markers", $new_marker_data);
662 }
663 }
664
665 wp_send_json_success(
666 array(
667 'new_id' => intval($new_map_id),
668 'message' => esc_html__('Map cloned successfully.', 'gmap-embed'),
669 )
670 );
671 }
672
673 }
674