# cbxgooglemap/1.1.11/public/class-cbxgooglemap-public.php

CBX Map for Google Map &amp; OpenStreetMap, version 1.1.11. 351 lines.

- Page: https://pluginprobe.com/plugins/cbxgooglemap/1.1.11/code/public/class-cbxgooglemap-public.php
- Raw: https://pluginprobe.com/plugins/cbxgooglemap/1.1.11/raw/public/class-cbxgooglemap-public.php
- Modified: 2021-09-06T08:29:42+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/cbxgooglemap/1.1.11/code/public/class-cbxgooglemap-public.php#L10-L20`.

```php
<?php

	/**
	 * The public-facing functionality of the plugin.
	 *
	 * @link       https://codeboxr.com
	 * @since      1.0.0
	 *
	 * @package    Cbxgooglemap
	 * @subpackage Cbxgooglemap/public
	 */

	/**
	 * The public-facing functionality of the plugin.
	 *
	 * Defines the plugin name, version, and two examples hooks for how to
	 * enqueue the admin-specific stylesheet and JavaScript.
	 *
	 * @package    Cbxgooglemap
	 * @subpackage Cbxgooglemap/public
	 * @author     Codeboxr <info@codeboxr.com>
	 */
	class CBXGoogleMap_Public {

		/**
		 * The ID of this plugin.
		 *
		 * @since    1.0.0
		 * @access   private
		 * @var      string $plugin_name The ID of this plugin.
		 */
		private $plugin_name;

		/**
		 * The version of this plugin.
		 *
		 * @since    1.0.0
		 * @access   private
		 * @var      string $version The current version of this plugin.
		 */
		private $version;

		/**
		 * Initialize the class and set its properties.
		 *
		 * @param string $plugin_name The name of the plugin.
		 * @param string $version     The version of this plugin.
		 *
		 * @since    1.0.0
		 *
		 */
		public function __construct( $plugin_name, $version ) {

			$this->plugin_name = $plugin_name;
			$this->version     = $version;

			$this->settings_api = new CBXGooglemapSettings();
		}


		/**
		 * Register the stylesheets for the public-facing side of the site.
		 *
		 * @since    1.0.0
		 */
		public function enqueue_styles() {

			$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
			$page = isset( $_GET['page'] ) ? esc_attr( wp_unslash( $_GET['page'] ) ) : '';

			$mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
			if ( $mapsource == 0 ) {
				wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.3.4/dist/leaflet.css', array(), $this->version, 'all' );
				wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array( 'leaflet' ), $this->version, 'all' );
				//wp_enqueue_style( 'leaflet' );
			} else {
				wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array(), $this->version, 'all' );
			}

			//wp_enqueue_style( 'cbxgooglemap-public' );

			$elementor_preview = isset($_REQUEST['elementor-preview'])? intval($_REQUEST['elementor-preview']) : 0;
			if($elementor_preview > 0  || (is_admin() && CBXGooglemapHelper::is_gutenberg_page())  ){
				CBXGooglemapHelper::enqueue_js_css(false, true);
			}
		}//end enqueue_styles

		/**
		 * Register the JavaScript for the public-facing side of the site.
		 *
		 * @since    1.0.0
		 */
		public function enqueue_scripts() {

			$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
			$page = isset( $_GET['page'] ) ? esc_attr( wp_unslash( $_GET['page'] ) ) : '';

			$googlemap_api_key = $this->settings_api->get_option( 'apikey', 'cbxgooglemap_general', '' );
			$mapsource         = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );

			if ( ( $mapsource == 1 && ! empty( $googlemap_api_key ) ) || $mapsource == 0 ) {
				wp_enqueue_script( 'jquery' );

				if ( $mapsource == 1 ) {
					wp_register_script( 'coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . $googlemap_api_key . '&libraries=places', array(), $this->version );
					wp_register_script( 'jqcbxgooglemap', plugin_dir_url( __FILE__ ) . '../assets/js/jqcbxgooglemap.js', array(
						'jquery',
						'coregooglemapapi'
					), $this->version, true );

					wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array(
						'jquery',
						'jqcbxgooglemap'
					), $this->version, true );

					//wp_enqueue_script( 'coregooglemapapi' );
					//wp_enqueue_script( 'jqcbxgooglemap' );
				} else {
					wp_register_script( 'coregooglemapapi', '//unpkg.com/leaflet@1.3.4/dist/leaflet.js', array(), $this->version );
					wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array(
						'jquery',
						'coregooglemapapi'
					), $this->version, true );
					//wp_enqueue_script( 'coregooglemapapi' );
				}

				//wp_enqueue_script( 'cbxgooglemap-public' );

				$elementor_preview = isset($_REQUEST['elementor-preview'])? intval($_REQUEST['elementor-preview']) : 0;
				if($elementor_preview > 0  || (is_admin() && CBXGooglemapHelper::is_gutenberg_page())  ){
					CBXGooglemapHelper::enqueue_js_css(true, false);
				}
			}
		}//end enqueue_scripts

		/**
		 * Init all shortcodes
		 */
		public function init_shortcodes() {
			add_shortcode( 'cbxgooglemap', array( $this, 'cbxgooglemap_shortcode' ) );
		}//end init_shortcodes

		/**
		 * Shortcode callback
		 */
		public function cbxgooglemap_shortcode( $atts ) {

			$settings = $this->settings_api;

			CBXGooglemapHelper::enqueue_js_css();

			$general_settings = get_option( 'cbxgooglemap_general', array() );

			$hide_leaflet   = intval($settings->get_field( 'hide_leaflet', $general_settings, 0 ));

			$api_key   = $settings->get_field( 'apikey', $general_settings, '' );
			$mapsource = intval( $settings->get_field( 'mapsource', $general_settings, 1 ) );

			//if google map and api key is not present
			if ( $mapsource == 1 && $api_key == '' ) {
				return '<p style="text-align: center;">' . esc_html__( 'Google Map Api Key is invalid or empty. Please set in setting.', 'cbxgooglemap' ) . '</p>';
			}

			$zoom_default = $settings->get_field( 'zoom', $general_settings, '8' );
			if ( $zoom_default == 0 ) {
				$zoom_default = 8;
			}


			$width_default = $settings->get_field( 'width', $general_settings, '100%' );
			if ( $width_default == '' || $width_default == 0 ) {
				$width_default = '100%';
			}

			$height_default = intval( $settings->get_field( 'height', $general_settings, '300' ) );
			if ( $height_default == 0 ) {
				$height_default = 300;
			}


			$scrollwheel_default = intval( $settings->get_field( 'scrollwheel', $general_settings, 1 ) );
			$showinfo_default    = intval( $settings->get_field( 'showinfo', $general_settings, 1 ) );
			$infow_open_default  = intval( $settings->get_field( 'infow_open', $general_settings, 1 ) );
			$maptype_default     = $settings->get_field( 'maptype', $general_settings, 'roadmap' );
			$mapicon_default     = $settings->get_field( 'mapicon', $general_settings, '' );


			$atts = shortcode_atts(
				array(
					'id'          => '',
					'maptype'     => $maptype_default,
					'lat'         => '',
					'lng'         => '',
					'width'       => $width_default,
					'height'      => $height_default,
					'zoom'        => $zoom_default,
					'scrollwheel' => $scrollwheel_default,
					'showinfo'    => $showinfo_default,
					'infow_open'  => $infow_open_default, //added in v1.1.2
					'heading'     => '',
					'address'     => '',
					'website'     => '',
					'mapicon'     => $mapicon_default, //added in v1.1.2
				),
				$atts, 'cbxgooglemap' );

			$id = isset( $atts['id'] ) ? intval( $atts['id'] ) : 0;

			if ( $id > 0 ) {
				//show maps from post data
				$combined_field = '_cbxgooglemap_combined'; //field name for non sortable fields
				$meta_prefix    = '_cbxgooglemap'; //field prefix for sortable fields

				$lat = get_post_meta( $id, $meta_prefix . 'lat', true );
				$lat = ( $lat !== false ) ? $lat : '';

				$lng = get_post_meta( $id, $meta_prefix . 'lng', true );
				$lng = ( $lng !== false ) ? $lng : '';

				if ( $lat == '' || $lng == '' ) {
					return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' );
				} //at least we need lat lng


				$meta_combined = get_post_meta( $id, $combined_field, true );
				$heading       = get_the_title( $id );

				$zoom = ( isset( $meta_combined[ $meta_prefix . 'zoom' ] ) && intval( $meta_combined[ $meta_prefix . 'zoom' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'zoom' ] ) : $zoom_default;

				$scrollwheel = ( isset( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) : $scrollwheel_default;
				$showinfo    = ( isset( $meta_combined[ $meta_prefix . 'showinfo' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'showinfo' ] ) : $showinfo_default;
				$infow_open  = ( isset( $meta_combined[ $meta_prefix . 'infow_open' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'infow_open' ] ) : $infow_open_default;

				$width  = ( isset( $meta_combined[ $meta_prefix . 'width' ] ) && $meta_combined[ $meta_prefix . 'width' ] != '' ) ? $meta_combined[ $meta_prefix . 'width' ] : $width_default;
				$height = ( isset( $meta_combined[ $meta_prefix . 'height' ] ) && intval( $meta_combined[ $meta_prefix . 'height' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'height' ] ) : $height_default;

				$address = ( isset( $meta_combined[ $meta_prefix . 'address' ] ) && esc_html( $meta_combined[ $meta_prefix . 'address' ] ) != '' ) ? esc_html( $meta_combined[ $meta_prefix . 'address' ] ) : '';
				$website = ( isset( $meta_combined[ $meta_prefix . 'website' ] ) && esc_url( $meta_combined[ $meta_prefix . 'website' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'website' ] ) : '';

				$mapicon = ( isset( $meta_combined[ $meta_prefix . 'mapicon' ] ) && esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) : '';
				$maptype = ( isset( $meta_combined[ $meta_prefix . 'maptype' ] ) && sanitize_text_field( $meta_combined[ $meta_prefix . 'maptype' ] ) != '' ) ? sanitize_text_field( $meta_combined[ $meta_prefix . 'maptype' ] ) : '';


			} else {
				//show map from shortcode params only
				$lat = isset( $atts['lat'] ) ? $atts['lat'] : '';
				$lng = isset( $atts['lat'] ) ? $atts['lng'] : '';

				if ( $lat == '' || $lng == '' ) {
					return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' );
				} //at least we need lat lng


				$width  = isset( $atts['width'] ) ? $atts['width'] : '';
				$height = isset( $atts['height'] ) ? intval( $atts['height'] ) : '';

				$zoom    = isset( $atts['zoom'] ) ? $atts['zoom'] : $zoom_default;
				$heading = isset( $atts['heading'] ) ? $atts['heading'] : '';
				$address = isset( $atts['address'] ) ? $atts['address'] : '';
				$website = isset( $atts['website'] ) ? $atts['website'] : '';
				$mapicon = isset( $atts['mapicon'] ) ? $atts['mapicon'] : '';
				$maptype = isset( $atts['maptype'] ) ? $atts['maptype'] : '';

				$showinfo    = isset( $atts['showinfo'] ) ? intval( $atts['showinfo'] ) : $showinfo_default;
				$infow_open  = isset( $atts['infow_open'] ) ? intval( $atts['infow_open'] ) : $infow_open_default;
				$scrollwheel = isset( $atts['scrollwheel'] ) ? intval( $atts['scrollwheel'] ) : $scrollwheel_default;


			}

			if ( $heading == '' && $website == '' ) {
				$showinfo = 0;
			}

			if ( is_numeric( $width ) ) {
				$width = $width . 'px';
			}


			$extra_class = ($hide_leaflet)? 'cbxgooglemap_wrapper_hideleaflet' : '';

			$output_html = '<div class="cbxgooglemap_wrapper '.esc_attr($extra_class).'">';
			$output_html .= '<div class="cbxgooglemap_embed" data-render="0" data-mapsource="' . $mapsource . '" style="width: ' . $width . '; height: ' . $height . 'px;" data-scrollwheel="' . $scrollwheel . '"  data-showinfo="' . $showinfo . '" data-infow_open="' . $infow_open . '" data-heading="' . esc_attr( $heading ) . '" data-address="' . esc_attr( $address ) . '" data-website="' . esc_url( $website ) . '"  data-mapicon="' . esc_url( $mapicon ) . '" data-maptype="' . $maptype . '" data-lat="' . esc_attr( $lat ) . '" data-lng="' . esc_attr( $lng ) . '" data-zoom="' . $zoom . '" ></div>';
			$output_html .= '</div>';

			return $output_html;
		}//end cbxgooglemap_shortcode

		/**
		 * Classic widget
		 */
		public function register_widget() {
			if ( ! class_exists( 'CBXGoogleMap_Widget' ) ) {
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'widgets/classic-widgets/class-cbxgooglemap-widget.php';
			}
			register_widget( "CBXGoogleMap_Widget" );
		}//end register_widget

		/**
		 * Init elementor widget
		 *
		 * @throws Exception
		 */
		public function init_elementor_widgets() {
			if ( ! class_exists( 'CBXGooglemap_ElemWidget' ) ) {
				//include the file
				require_once plugin_dir_path( dirname( __FILE__ ) ) . 'widgets/elementor-elements/class-cbxgooglemap-elemwidget.php';
			}

			//register the widget
			\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new CBXGooglemapElemWidget\Widgets\CBXGooglemap_ElemWidget() );
		}//end widgets_registered

		/**
		 * Add new category to elementor
		 *
		 * @param $elements_manager
		 */
		public function add_elementor_widget_categories( $elements_manager ) {
			$elements_manager->add_category(
				'codeboxr',
				array(
					'title' => esc_html__( 'Codeboxr Widgets', 'cbxgooglemap' ),
					'icon'  => 'fa fa-plug',
				)
			);
		}//end add_elementor_widget_categories

		/**
		 * Load Elementor Custom Icon
		 */
		function elementor_icon_loader() {
			wp_register_style( 'cbxgooglemap_elementor_icon', CBXGOOGLEMAP_ROOT_URL . 'widgets/elementor-elements/elementor-icon/icon.css', false, CBXGOOGLEMAP_PLUGIN_VERSION );
			wp_enqueue_style( 'cbxgooglemap_elementor_icon' );

			//CBXGooglemapHelper::enqueue_js_css();
		}//end elementor_icon_loader


		/**
		 * // Before VC Init
		 */
		public function vc_before_init_actions() {
			if ( ! class_exists( 'CBXGoogleMap_WPBWidget' ) ) {
				require_once CBXGOOGLEMAP_ROOT_PATH . 'widgets/vc-element/class-cbxgooglemap-wpbwidget.php';
			}

			new CBXGoogleMap_WPBWidget();
		}// end method vc_before_init_actions
	}//end class CBXGoogleMap_Public

```
