PluginProbe
CBX Map for Google Map & OpenStreetMap / 2.0.4
CBX Map for Google Map & OpenStreetMap v2.0.4
trunk 1.1.10 1.1.11 1.1.12 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5
cbxgooglemap / includes / CBXGoogleMapPublic.php

CBXGoogleMapPublic.php in CBX Map for Google Map & OpenStreetMap 2.0.4, at includes/CBXGoogleMapPublic.php

404 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The public-facing functionality of the plugin.
4 *
5 * @link https://codeboxr.com
6 * @since 1.0.0
7 *
8 * @package Cbxgooglemap
9 * @subpackage Cbxgooglemap/public
10 */
11
12 /**
13 * The public-facing functionality of the plugin.
14 *
15 * Defines the plugin name, version, and two examples hooks for how to
16 * enqueue the admin-specific stylesheet and JavaScript.
17 *
18 * @package Cbxgooglemap
19 * @subpackage Cbxgooglemap/public
20 * @author Codeboxr <info@codeboxr.com>
21 */
22
23 namespace Cbx\Googlemap;
24
25 if ( ! defined( 'ABSPATH' ) ) {
26 exit;
27 }
28
29 use Cbx\Googlemap\Helpers\CBXGooglemapHelper;
30 use Cbx\Googlemap\Widgets\Classic\CBXGoogleMapWidget;
31 use Cbx\Googlemap\Widgets\Elementor\CBXGooglemapElemWidget;
32
33 class CBXGoogleMapPublic {
34
35 /**
36 * The ID of this plugin.
37 *
38 * @since 1.0.0
39 * @access private
40 * @var string $plugin_name The ID of this plugin.
41 */
42 private $plugin_name;
43
44 /**
45 * The version of this plugin.
46 *
47 * @since 1.0.0
48 * @access private
49 * @var string $version The current version of this plugin.
50 */
51 private $version;
52
53 /**
54 * Initialize the class and set its properties.
55 *
56 * @since 1.0.0
57 *
58 */
59
60 /**
61 * The ID of this plugin.
62 *
63 * @since 1.1.12
64 * @access private
65 * @var string $settings The ID of this plugin.
66 */
67 private $settings;
68
69 public function __construct() {
70 $this->plugin_name = CBXGOOGLEMAP_PLUGIN_NAME;
71 $this->version = CBXGOOGLEMAP_PLUGIN_VERSION;
72
73 $this->settings = new CBXGooglemapSettings();
74 }//end constructor
75
76
77 /**
78 * Register the stylesheets for the public-facing side of the site.
79 *
80 * @since 1.0.0
81 */
82 public function enqueue_styles() {
83 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
84 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
85 $version = $this->version;
86
87 $vendor_url = CBXGOOGLEMAP_ROOT_URL . 'assets/vendors/';
88 $css_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
89 $js_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
90
91 $map_source = intval( $this->settings->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
92
93 if ( $map_source == 0 ) {
94 wp_register_style( 'leaflet', $vendor_url . 'leaflet/leaflet.css', [], $version, 'all' );
95 wp_register_style( 'cbxgooglemap-public', $css_url . 'cbxgooglemap-public.css', [ 'leaflet' ], $version, 'all' );
96 } else {
97 wp_register_style( 'cbxgooglemap-public', $css_url . 'cbxgooglemap-public.css', [], $version, 'all' );
98 }
99
100 //wp_enqueue_style( 'cbxgooglemap-public' );
101
102 $elementor_preview = isset( $_REQUEST['elementor-preview'] ) ? intval( $_REQUEST['elementor-preview'] ) : 0;// phpcs:ignore WordPress.Security.NonceVerification.Recommended
103 if ( $elementor_preview > 0 || ( is_admin() && CBXGooglemapHelper::is_gutenberg_page() ) ) {
104 CBXGooglemapHelper::enqueue_js_css( false, true );
105 }
106 }//end enqueue_styles
107
108 /**
109 * Register the JavaScript for the public-facing side of the site.
110 *
111 * @since 1.0.0
112 */
113 public function enqueue_scripts() {
114 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
115 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
116
117 $version = $this->version;
118 $in_footer = [
119 'in_footer' => true,
120 ];
121
122 $in_footer_async = [
123 'in_footer' => true,
124 'strategy' => 'async'
125 ];
126
127
128 $vendor_url = CBXGOOGLEMAP_ROOT_URL . 'assets/vendors/';
129 $css_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
130 $js_url = CBXGOOGLEMAP_ROOT_URL . 'assets/js/';
131
132 $api_key = esc_attr( $this->settings->get_option( 'apikey', 'cbxgooglemap_general', '' ) );
133 $map_source = intval( $this->settings->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
134 $default_mapicon = esc_url( $this->settings->get_option( 'mapicon', 'cbxgooglemap_general', '' ) );
135
136 wp_register_script( 'cbxgooglemap-events', $js_url . 'cbxgooglemap-events.js', [], $version, $in_footer );
137
138 if ( ( $map_source == 1 && ! empty( $api_key ) ) || $map_source == 0 ) {
139 wp_enqueue_script( 'jquery' );
140
141 if ( $map_source == 1 ) {
142 wp_register_script( 'coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . esc_attr( $api_key ) . '&libraries=places&callback=Function.prototype', [], $version, $in_footer_async );
143 wp_register_script( 'cbxgooglemap-public', $js_url . 'cbxgooglemap-public.js', [
144 'jquery',
145 'cbxgooglemap-events'
146 ], $version, $in_footer );
147
148 wp_enqueue_script( 'coregooglemapapi' );
149 } else {
150 wp_register_script( 'coregooglemapapi', $vendor_url . 'leaflet/leaflet.js', [], $version, $in_footer_async );
151 wp_register_script( 'cbxgooglemap-public', $js_url . 'cbxgooglemap-public.js', [
152 'jquery',
153 'coregooglemapapi',
154 'cbxgooglemap-events'
155 ], $version, $in_footer );
156 }
157
158 $cbxgooglemap_public_js_vars = [
159 'icon_url_default' => $default_mapicon,
160 'api_key' => $api_key,
161 'map_source' => $map_source,
162 'extra_markers' => []
163 ];
164
165 wp_localize_script( 'cbxgooglemap-public', 'cbxgooglemap_public', apply_filters( 'cbxgooglemap_public_js_vars', $cbxgooglemap_public_js_vars ) );
166
167
168 $elementor_preview = isset( $_REQUEST['elementor-preview'] ) ? intval( $_REQUEST['elementor-preview'] ) : 0;// phpcs:ignore WordPress.Security.NonceVerification.Recommended
169 if ( $elementor_preview > 0 || ( is_admin() && CBXGooglemapHelper::is_gutenberg_page() ) ) {
170 CBXGooglemapHelper::enqueue_js_css( true, false );
171 }
172 }
173 }//end enqueue_scripts
174
175 /**
176 * Init all shortcodes
177 */
178 public function init_shortcodes() {
179 add_shortcode( 'cbxgooglemap', [ $this, 'cbxgooglemap_shortcode' ] );
180 }//end init_shortcodes
181
182 /**
183 * Shortcode callback for shortcode '[cbxgooglemap]'
184 */
185 public function cbxgooglemap_shortcode( $atts ) {
186
187 $atts = array_change_key_case( (array) $atts, CASE_LOWER );
188
189 $settings = $this->settings;
190
191 CBXGooglemapHelper::enqueue_js_css();
192
193 $general_settings = get_option( 'cbxgooglemap_general', [] );
194
195 $hide_leaflet = absint( $settings->get_field( 'hide_leaflet', 'cbxgooglemap_general', 0 ) );
196 $api_key = esc_attr( $settings->get_field( 'apikey', 'cbxgooglemap_general', '' ) );
197 $map_source = absint( $settings->get_field( 'mapsource', 'cbxgooglemap_general', 1 ) );
198
199 //if google map and api key is not present
200 if ( $map_source == 1 && $api_key == '' ) {
201 //return '<p style="text-align: center;">' . esc_html__( 'Google Map Api Key is invalid or empty. Please set in plugin setting.', 'cbxgooglemap' ) . '</p>';
202
203 return '<div class="notification notification-warning" style="margin-top: 10px;">
204 <div class="notification-icon">
205 <svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" viewBox="0 0 256 256"><path fill="#f59e0b" d="M235.07 189.09L147.61 37.22a22.75 22.75 0 0 0-39.22 0L20.93 189.09a21.53 21.53 0 0 0 0 21.72A22.35 22.35 0 0 0 40.55 222h174.9a22.35 22.35 0 0 0 19.6-11.19a21.53 21.53 0 0 0 .02-21.72Zm-10.41 15.71a10.46 10.46 0 0 1-9.21 5.2H40.55a10.46 10.46 0 0 1-9.21-5.2a9.51 9.51 0 0 1 0-9.72l87.45-151.87a10.75 10.75 0 0 1 18.42 0l87.46 151.87a9.51 9.51 0 0 1-.01 9.72ZM122 144v-40a6 6 0 0 1 12 0v40a6 6 0 0 1-12 0Zm16 36a10 10 0 1 1-10-10a10 10 0 0 1 10 10Z"/></svg>
206 </div>
207 <div class="notification-content">
208 <div class="notification-title">'.esc_html__('Googlemap Api Key Missing', 'cbxgooglemap').'</div>
209 <div class="notification-message"><p>' . esc_html__( 'Google map api key missing, Please choose alternative openstreet map or set google map api key in plugin setting.', 'cbxgooglemap' ) . '</p></div>
210 </div>
211 </div>';
212 }
213
214 $zoom_default = absint( $settings->get_field( 'zoom', 'cbxgooglemap_general', '8' ) );
215 if ( $zoom_default == 0 ) {
216 $zoom_default = 8;
217 }
218
219
220 $width_default = $settings->get_field( 'width', 'cbxgooglemap_general', '100%' );
221 if ( $width_default == '' || $width_default == 0 ) {
222 $width_default = '100%';
223 }
224
225 $height_default = absint( $settings->get_field( 'height', 'cbxgooglemap_general', '300' ) );
226 if ( $height_default == 0 ) {
227 $height_default = 300;
228 }
229
230
231 $scrollwheel_default = absint( $settings->get_field( 'scrollwheel', 'cbxgooglemap_general', 1 ) );
232 $showinfo_default = absint( $settings->get_field( 'showinfo', 'cbxgooglemap_general', 1 ) );
233 $infow_open_default = absint( $settings->get_field( 'infow_open', 'cbxgooglemap_general', 1 ) );
234
235 $maptype_default = $settings->get_field( 'maptype', 'cbxgooglemap_general', 'roadmap' );
236 $mapicon_default = $settings->get_field( 'mapicon', 'cbxgooglemap_general', '' );
237
238 if ( $mapicon_default != '' ) {
239 $mapicon_default = esc_url( $mapicon_default );
240 }
241
242
243 $atts = shortcode_atts(
244 [
245 'id' => '',
246 'maptype' => esc_attr( $maptype_default ),
247 'lat' => '',
248 'lng' => '',
249 'width' => $width_default,
250 'height' => $height_default,
251 'zoom' => $zoom_default,
252 'scrollwheel' => $scrollwheel_default,
253 'showinfo' => $showinfo_default,
254 'infow_open' => $infow_open_default, //added in v1.1.2
255 'heading' => '',
256 'address' => '',
257 'website' => '',
258 'mapicon' => $mapicon_default, //added in v1.1.2
259 ],
260 $atts, 'cbxgooglemap' );
261
262 $id = isset( $atts['id'] ) ? intval( $atts['id'] ) : 0;
263
264 $data_custom_attrs = [
265 'data-post-id' => $id
266 ];
267
268
269 $output_html = '';
270
271 if ( $id > 0 ) {
272 //show maps from post data
273 $combined_field = '_cbxgooglemap_combined'; //field name for non sortable fields
274 $meta_prefix = '_cbxgooglemap'; //field prefix for sortable fields
275
276 $lat = get_post_meta( $id, $meta_prefix . 'lat', true );
277
278
279 $lat = ( $lat !== false ) ? $lat : '';
280
281 $lng = get_post_meta( $id, $meta_prefix . 'lng', true );
282 $lng = ( $lng !== false ) ? $lng : '';
283
284 if ( $lat == '' || $lng == '' ) {
285 return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' );
286 } //at least we need lat lng
287
288
289 $meta_combined = get_post_meta( $id, $combined_field, true );
290
291 $heading = isset( $meta_combined['_cbxgooglemaptitle'] ) ? esc_html( $meta_combined['_cbxgooglemaptitle'] ) : '';
292
293 $zoom = ( isset( $meta_combined[ $meta_prefix . 'zoom' ] ) && intval( $meta_combined[ $meta_prefix . 'zoom' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'zoom' ] ) : $zoom_default;
294
295 $scrollwheel = ( isset( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) ) ? absint( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) : intval( $scrollwheel_default );
296 $showinfo = ( isset( $meta_combined[ $meta_prefix . 'showinfo' ] ) ) ? absint( $meta_combined[ $meta_prefix . 'showinfo' ] ) : intval( $showinfo_default );
297 $infow_open = ( isset( $meta_combined[ $meta_prefix . 'infow_open' ] ) ) ? absint( $meta_combined[ $meta_prefix . 'infow_open' ] ) : intval( $infow_open_default );
298
299 $width = ( isset( $meta_combined[ $meta_prefix . 'width' ] ) && $meta_combined[ $meta_prefix . 'width' ] != '' ) ? $meta_combined[ $meta_prefix . 'width' ] : $width_default;
300 $height = ( isset( $meta_combined[ $meta_prefix . 'height' ] ) && absint( $meta_combined[ $meta_prefix . 'height' ] ) > 0 ) ? absint( $meta_combined[ $meta_prefix . 'height' ] ) : $height_default;
301
302 $address = ( isset( $meta_combined[ $meta_prefix . 'address' ] ) && esc_attr( $meta_combined[ $meta_prefix . 'address' ] ) != '' ) ? esc_attr( $meta_combined[ $meta_prefix . 'address' ] ) : '';
303 $website = ( isset( $meta_combined[ $meta_prefix . 'website' ] ) && esc_url( $meta_combined[ $meta_prefix . 'website' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'website' ] ) : '';
304
305 $mapicon = ( isset( $meta_combined[ $meta_prefix . 'mapicon' ] ) && esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) : $mapicon_default;
306 $maptype = ( isset( $meta_combined[ $meta_prefix . 'maptype' ] ) && esc_attr( $meta_combined[ $meta_prefix . 'maptype' ] ) != '' ) ? esc_attr( $meta_combined[ $meta_prefix . 'maptype' ] ) : '';
307
308 }
309 else {
310 $atts = cbxgooglemap_decode_entities_array($atts);
311
312 //show map from shortcode params only
313 $lat = isset( $atts['lat'] ) ? (float) $atts['lat'] : '';
314 $lng = isset( $atts['lat'] ) ? (float) $atts['lng'] : '';
315
316 if($lat === 0) $lat = '';
317 if($lng === 0) $lng = '';
318
319 if ( $lat == '' || $lng == '' ) {
320 return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' );
321 } //at least we need lat lng
322
323
324 $width = isset( $atts['width'] ) ? $atts['width'] : '';
325 $height = isset( $atts['height'] ) ? (int) $atts['height'] : '';
326
327 $zoom = isset( $atts['zoom'] ) ? $atts['zoom'] : $zoom_default;
328 $heading = isset( $atts['heading'] ) ? sanitize_text_field( wp_unslash( $atts['heading'] ) ) : '';
329 $address = isset( $atts['address'] ) ? sanitize_text_field( wp_unslash( $atts['address'] ) ) : '';
330 $website = isset( $atts['website'] ) ? sanitize_text_field( $atts['website'] ) : '';
331 $mapicon = isset( $atts['mapicon'] ) ? sanitize_text_field( $atts['mapicon'] ) : '';
332 $maptype = isset( $atts['maptype'] ) ? sanitize_text_field( $atts['maptype'] ) : '';
333
334 $showinfo = isset( $atts['showinfo'] ) ? absint( $atts['showinfo'] ) : $showinfo_default;
335 $infow_open = isset( $atts['infow_open'] ) ? absint( $atts['infow_open'] ) : $infow_open_default;
336 $scrollwheel = isset( $atts['scrollwheel'] ) ? absint( $atts['scrollwheel'] ) : $scrollwheel_default;
337 }
338
339 if ( $heading == '' && $website == '' ) {
340 $showinfo = 0;
341 }
342
343 if ( is_numeric( $width ) ) {
344 $width = $width . 'px';
345 }
346
347
348 $extra_class = ( $hide_leaflet ) ? 'cbxgooglemap_wrapper_hideleaflet' : '';
349
350
351
352 $data_custom_attrs = apply_filters( 'cbxgooglemap_data_custom_attrs', $data_custom_attrs, $id, $atts );
353
354 $data_custom_attrs_html = ' ';
355 foreach ( $data_custom_attrs as $custom_attr_key => $custom_attr_value ) {
356 $data_custom_attrs_html .= ' ' . $custom_attr_key . '="' . esc_attr( html_entity_decode($custom_attr_value) ) . '" ';
357 }
358
359 $output_html .= '<div class="cbxgooglemap_wrapper ' . esc_attr( $extra_class ) . '">';
360 $output_html .= '<div class="cbxgooglemap_embed" ' . $data_custom_attrs_html . ' data-render="0" data-mapsource="' . absint( $map_source ) . '" style="width: ' . esc_attr($width) . '; height: ' . esc_attr($height) . 'px;" data-scrollwheel="' . absint( $scrollwheel ) . '" data-showinfo="' . absint( $showinfo ) . '" data-infow_open="' . absint( $infow_open ) . '" data-heading="' . esc_attr( $heading ) . '" data-address="' . esc_attr( $address ) . '" data-website="' . esc_url( $website ) . '" data-mapicon="' . esc_url( $mapicon ) . '" data-maptype="' . esc_attr( $maptype ) . '" data-lat="' . esc_attr( $lat ) . '" data-lng="' . esc_attr( $lng ) . '" data-zoom="' . esc_attr( $zoom ) . '" ></div>';
361 $output_html .= '</div>';
362
363 return apply_filters('cbxgooglemap_shortcode_html', $output_html, $id, $atts);
364 }//end cbxgooglemap_shortcode
365
366 /**
367 * Register classic widget
368 */
369 public function register_widget() {
370 register_widget( CBXGoogleMapWidget::class );
371 }//end register_widget
372
373 /**
374 * Register elementor widget
375 *
376 * @return void
377 */
378 public function init_elementor_widgets() {
379 \Elementor\Plugin::instance()->widgets_manager->register( new CBXGooglemapElemWidget() );
380 }//end widgets_registered
381
382 /**
383 * Add new category to elementor
384 *
385 * @param $elements_manager
386 */
387 public function add_elementor_widget_categories( $elements_manager ) {
388 $elements_manager->add_category(
389 'codeboxr',
390 [
391 'title' => esc_html__( 'Codeboxr Widgets', 'cbxgooglemap' ),
392 'icon' => 'fa fa-plug',
393 ]
394 );
395 }//end add_elementor_widget_categories
396
397 /**
398 * Load Elementor Custom Icon
399 */
400 function elementor_icon_loader() {
401 wp_register_style( 'cbxgooglemap-elementor-icon', CBXGOOGLEMAP_ROOT_URL . 'assets/css/cbxgooglemap-elementor.css', false, CBXGOOGLEMAP_PLUGIN_VERSION );
402 wp_enqueue_style( 'cbxgooglemap-elementor-icon' );
403 }//end elementor_icon_loader
404 }//end class CBXGoogleMapPublic