| 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 = intval( $settings->get_field( 'hide_leaflet', 'cbxgooglemap_general', 0 ) ); |
| 196 |
$api_key = esc_attr( $settings->get_field( 'apikey', 'cbxgooglemap_general', '' ) ); |
| 197 |
$map_source = intval( $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 |
|
| 204 |
$zoom_default = intval( $settings->get_field( 'zoom', 'cbxgooglemap_general', '8' ) ); |
| 205 |
if ( $zoom_default == 0 ) { |
| 206 |
$zoom_default = 8; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
$width_default = $settings->get_field( 'width', 'cbxgooglemap_general', '100%' ); |
| 211 |
if ( $width_default == '' || $width_default == 0 ) { |
| 212 |
$width_default = '100%'; |
| 213 |
} |
| 214 |
|
| 215 |
$height_default = intval( $settings->get_field( 'height', 'cbxgooglemap_general', '300' ) ); |
| 216 |
if ( $height_default == 0 ) { |
| 217 |
$height_default = 300; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
$scrollwheel_default = intval( $settings->get_field( 'scrollwheel', 'cbxgooglemap_general', 1 ) ); |
| 222 |
$showinfo_default = intval( $settings->get_field( 'showinfo', 'cbxgooglemap_general', 1 ) ); |
| 223 |
$infow_open_default = intval( $settings->get_field( 'infow_open', 'cbxgooglemap_general', 1 ) ); |
| 224 |
|
| 225 |
$maptype_default = $settings->get_field( 'maptype', 'cbxgooglemap_general', 'roadmap' ); |
| 226 |
$mapicon_default = $settings->get_field( 'mapicon', 'cbxgooglemap_general', '' ); |
| 227 |
|
| 228 |
if ( $mapicon_default != '' ) { |
| 229 |
$mapicon_default = esc_url( $mapicon_default ); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
$atts = shortcode_atts( |
| 234 |
[ |
| 235 |
'id' => '', |
| 236 |
'maptype' => esc_attr( $maptype_default ), |
| 237 |
'lat' => '', |
| 238 |
'lng' => '', |
| 239 |
'width' => $width_default, |
| 240 |
'height' => $height_default, |
| 241 |
'zoom' => $zoom_default, |
| 242 |
'scrollwheel' => $scrollwheel_default, |
| 243 |
'showinfo' => $showinfo_default, |
| 244 |
'infow_open' => $infow_open_default, //added in v1.1.2 |
| 245 |
'heading' => '', |
| 246 |
'address' => '', |
| 247 |
'website' => '', |
| 248 |
'mapicon' => $mapicon_default, //added in v1.1.2 |
| 249 |
], |
| 250 |
$atts, 'cbxgooglemap' ); |
| 251 |
|
| 252 |
$id = isset( $atts['id'] ) ? intval( $atts['id'] ) : 0; |
| 253 |
|
| 254 |
$data_custom_attrs = [ |
| 255 |
'data-post-id' => $id |
| 256 |
]; |
| 257 |
|
| 258 |
|
| 259 |
$output_html = ''; |
| 260 |
|
| 261 |
if ( $id > 0 ) { |
| 262 |
//show maps from post data |
| 263 |
$combined_field = '_cbxgooglemap_combined'; //field name for non sortable fields |
| 264 |
$meta_prefix = '_cbxgooglemap'; //field prefix for sortable fields |
| 265 |
|
| 266 |
$lat = get_post_meta( $id, $meta_prefix . 'lat', true ); |
| 267 |
|
| 268 |
|
| 269 |
$lat = ( $lat !== false ) ? $lat : ''; |
| 270 |
|
| 271 |
$lng = get_post_meta( $id, $meta_prefix . 'lng', true ); |
| 272 |
$lng = ( $lng !== false ) ? $lng : ''; |
| 273 |
|
| 274 |
if ( $lat == '' || $lng == '' ) { |
| 275 |
return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' ); |
| 276 |
} //at least we need lat lng |
| 277 |
|
| 278 |
|
| 279 |
$meta_combined = get_post_meta( $id, $combined_field, true ); |
| 280 |
|
| 281 |
$heading = isset( $meta_combined['_cbxgooglemaptitle'] ) ? esc_html( $meta_combined['_cbxgooglemaptitle'] ) : ''; |
| 282 |
|
| 283 |
$zoom = ( isset( $meta_combined[ $meta_prefix . 'zoom' ] ) && intval( $meta_combined[ $meta_prefix . 'zoom' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'zoom' ] ) : $zoom_default; |
| 284 |
|
| 285 |
$scrollwheel = ( isset( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) : intval( $scrollwheel_default ); |
| 286 |
$showinfo = ( isset( $meta_combined[ $meta_prefix . 'showinfo' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'showinfo' ] ) : intval( $showinfo_default ); |
| 287 |
$infow_open = ( isset( $meta_combined[ $meta_prefix . 'infow_open' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'infow_open' ] ) : intval( $infow_open_default ); |
| 288 |
|
| 289 |
$width = ( isset( $meta_combined[ $meta_prefix . 'width' ] ) && $meta_combined[ $meta_prefix . 'width' ] != '' ) ? $meta_combined[ $meta_prefix . 'width' ] : $width_default; |
| 290 |
$height = ( isset( $meta_combined[ $meta_prefix . 'height' ] ) && intval( $meta_combined[ $meta_prefix . 'height' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'height' ] ) : $height_default; |
| 291 |
|
| 292 |
$address = ( isset( $meta_combined[ $meta_prefix . 'address' ] ) && esc_attr( $meta_combined[ $meta_prefix . 'address' ] ) != '' ) ? esc_attr( $meta_combined[ $meta_prefix . 'address' ] ) : ''; |
| 293 |
$website = ( isset( $meta_combined[ $meta_prefix . 'website' ] ) && esc_url( $meta_combined[ $meta_prefix . 'website' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'website' ] ) : ''; |
| 294 |
|
| 295 |
$mapicon = ( isset( $meta_combined[ $meta_prefix . 'mapicon' ] ) && esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) : $mapicon_default; |
| 296 |
$maptype = ( isset( $meta_combined[ $meta_prefix . 'maptype' ] ) && esc_attr( $meta_combined[ $meta_prefix . 'maptype' ] ) != '' ) ? esc_attr( $meta_combined[ $meta_prefix . 'maptype' ] ) : ''; |
| 297 |
|
| 298 |
} |
| 299 |
else { |
| 300 |
$atts = cbxgooglemap_decode_entities_array($atts); |
| 301 |
|
| 302 |
//show map from shortcode params only |
| 303 |
$lat = isset( $atts['lat'] ) ? floatval( $atts['lat'] ) : ''; |
| 304 |
$lng = isset( $atts['lat'] ) ? floatval( $atts['lng'] ) : ''; |
| 305 |
|
| 306 |
if($lat === 0) $lat = ''; |
| 307 |
if($lng === 0) $lng = ''; |
| 308 |
|
| 309 |
if ( $lat == '' || $lng == '' ) { |
| 310 |
return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' ); |
| 311 |
} //at least we need lat lng |
| 312 |
|
| 313 |
|
| 314 |
$width = isset( $atts['width'] ) ? $atts['width'] : ''; |
| 315 |
$height = isset( $atts['height'] ) ? intval( $atts['height'] ) : ''; |
| 316 |
|
| 317 |
$zoom = isset( $atts['zoom'] ) ? $atts['zoom'] : $zoom_default; |
| 318 |
$heading = isset( $atts['heading'] ) ? sanitize_text_field( wp_unslash( $atts['heading'] ) ) : ''; |
| 319 |
$address = isset( $atts['address'] ) ? sanitize_text_field( wp_unslash( $atts['address'] ) ) : ''; |
| 320 |
$website = isset( $atts['website'] ) ? sanitize_text_field( $atts['website'] ) : ''; |
| 321 |
$mapicon = isset( $atts['mapicon'] ) ? sanitize_text_field( $atts['mapicon'] ) : ''; |
| 322 |
$maptype = isset( $atts['maptype'] ) ? sanitize_text_field( $atts['maptype'] ) : ''; |
| 323 |
|
| 324 |
$showinfo = isset( $atts['showinfo'] ) ? absint( $atts['showinfo'] ) : $showinfo_default; |
| 325 |
$infow_open = isset( $atts['infow_open'] ) ? absint( $atts['infow_open'] ) : $infow_open_default; |
| 326 |
$scrollwheel = isset( $atts['scrollwheel'] ) ? absint( $atts['scrollwheel'] ) : $scrollwheel_default; |
| 327 |
} |
| 328 |
|
| 329 |
if ( $heading == '' && $website == '' ) { |
| 330 |
$showinfo = 0; |
| 331 |
} |
| 332 |
|
| 333 |
if ( is_numeric( $width ) ) { |
| 334 |
$width = $width . 'px'; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
$extra_class = ( $hide_leaflet ) ? 'cbxgooglemap_wrapper_hideleaflet' : ''; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
$data_custom_attrs = apply_filters( 'cbxgooglemap_data_custom_attrs', $data_custom_attrs, $id, $atts ); |
| 343 |
|
| 344 |
$data_custom_attrs_html = ' '; |
| 345 |
foreach ( $data_custom_attrs as $custom_attr_key => $custom_attr_value ) { |
| 346 |
$data_custom_attrs_html .= ' ' . $custom_attr_key . '="' . esc_attr( html_entity_decode($custom_attr_value) ) . '" '; |
| 347 |
} |
| 348 |
|
| 349 |
$output_html .= '<div class="cbxgooglemap_wrapper ' . esc_attr( $extra_class ) . '">'; |
| 350 |
$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>'; |
| 351 |
$output_html .= '</div>'; |
| 352 |
|
| 353 |
return apply_filters('cbxgooglemap_shortcode_html', $output_html, $id, $atts); |
| 354 |
}//end cbxgooglemap_shortcode |
| 355 |
|
| 356 |
/** |
| 357 |
* Register classic widget |
| 358 |
*/ |
| 359 |
public function register_widget() { |
| 360 |
register_widget( CBXGoogleMapWidget::class ); |
| 361 |
}//end register_widget |
| 362 |
|
| 363 |
/** |
| 364 |
* Register elementor widget |
| 365 |
* |
| 366 |
* @return void |
| 367 |
*/ |
| 368 |
public function init_elementor_widgets() { |
| 369 |
\Elementor\Plugin::instance()->widgets_manager->register( new CBXGooglemapElemWidget() ); |
| 370 |
}//end widgets_registered |
| 371 |
|
| 372 |
/** |
| 373 |
* Add new category to elementor |
| 374 |
* |
| 375 |
* @param $elements_manager |
| 376 |
*/ |
| 377 |
public function add_elementor_widget_categories( $elements_manager ) { |
| 378 |
$elements_manager->add_category( |
| 379 |
'codeboxr', |
| 380 |
[ |
| 381 |
'title' => esc_html__( 'Codeboxr Widgets', 'cbxgooglemap' ), |
| 382 |
'icon' => 'fa fa-plug', |
| 383 |
] |
| 384 |
); |
| 385 |
}//end add_elementor_widget_categories |
| 386 |
|
| 387 |
/** |
| 388 |
* Load Elementor Custom Icon |
| 389 |
*/ |
| 390 |
function elementor_icon_loader() { |
| 391 |
wp_register_style( 'cbxgooglemap-elementor-icon', CBXGOOGLEMAP_ROOT_URL . 'assets/css/cbxgooglemap-elementor.css', false, CBXGOOGLEMAP_PLUGIN_VERSION ); |
| 392 |
wp_enqueue_style( 'cbxgooglemap-elementor-icon' ); |
| 393 |
}//end elementor_icon_loader |
| 394 |
}//end class CBXGoogleMapPublic |