| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The public-facing functionality of the plugin. |
| 5 |
* |
| 6 |
* @link https://codeboxr.com |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package Cbxgooglemap |
| 10 |
* @subpackage Cbxgooglemap/public |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* The public-facing functionality of the plugin. |
| 15 |
* |
| 16 |
* Defines the plugin name, version, and two examples hooks for how to |
| 17 |
* enqueue the admin-specific stylesheet and JavaScript. |
| 18 |
* |
| 19 |
* @package Cbxgooglemap |
| 20 |
* @subpackage Cbxgooglemap/public |
| 21 |
* @author Codeboxr <info@codeboxr.com> |
| 22 |
*/ |
| 23 |
class CBXGoogleMap_Public { |
| 24 |
|
| 25 |
/** |
| 26 |
* The ID of this plugin. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @access private |
| 30 |
* @var string $plugin_name The ID of this plugin. |
| 31 |
*/ |
| 32 |
private $plugin_name; |
| 33 |
|
| 34 |
/** |
| 35 |
* The version of this plugin. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @access private |
| 39 |
* @var string $version The current version of this plugin. |
| 40 |
*/ |
| 41 |
private $version; |
| 42 |
|
| 43 |
/** |
| 44 |
* Initialize the class and set its properties. |
| 45 |
* |
| 46 |
* @param string $plugin_name The name of the plugin. |
| 47 |
* @param string $version The version of this plugin. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* |
| 51 |
*/ |
| 52 |
public function __construct( $plugin_name, $version ) { |
| 53 |
|
| 54 |
$this->plugin_name = $plugin_name; |
| 55 |
$this->version = $version; |
| 56 |
|
| 57 |
$this->settings_api = new CBXGooglemapSettings(); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Register the stylesheets for the public-facing side of the site. |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
*/ |
| 66 |
public function enqueue_styles() { |
| 67 |
|
| 68 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 69 |
$page = isset( $_GET['page'] ) ? esc_attr( wp_unslash( $_GET['page'] ) ) : ''; |
| 70 |
|
| 71 |
$mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) ); |
| 72 |
if ( $mapsource == 0 ) { |
| 73 |
wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.3.4/dist/leaflet.css', array(), $this->version, 'all' ); |
| 74 |
wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array( 'leaflet' ), $this->version, 'all' ); |
| 75 |
//wp_enqueue_style( 'leaflet' ); |
| 76 |
} else { |
| 77 |
wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array(), $this->version, 'all' ); |
| 78 |
} |
| 79 |
|
| 80 |
//wp_enqueue_style( 'cbxgooglemap-public' ); |
| 81 |
|
| 82 |
$elementor_preview = isset($_REQUEST['elementor-preview'])? intval($_REQUEST['elementor-preview']) : 0; |
| 83 |
if($elementor_preview > 0 || (is_admin() && CBXGooglemapHelper::is_gutenberg_page()) ){ |
| 84 |
CBXGooglemapHelper::enqueue_js_css(false, true); |
| 85 |
} |
| 86 |
}//end enqueue_styles |
| 87 |
|
| 88 |
/** |
| 89 |
* Register the JavaScript for the public-facing side of the site. |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
*/ |
| 93 |
public function enqueue_scripts() { |
| 94 |
|
| 95 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 96 |
$page = isset( $_GET['page'] ) ? esc_attr( wp_unslash( $_GET['page'] ) ) : ''; |
| 97 |
|
| 98 |
$googlemap_api_key = $this->settings_api->get_option( 'apikey', 'cbxgooglemap_general', '' ); |
| 99 |
$mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) ); |
| 100 |
|
| 101 |
if ( ( $mapsource == 1 && ! empty( $googlemap_api_key ) ) || $mapsource == 0 ) { |
| 102 |
wp_enqueue_script( 'jquery' ); |
| 103 |
|
| 104 |
if ( $mapsource == 1 ) { |
| 105 |
wp_register_script( 'coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . $googlemap_api_key . '&libraries=places', array(), $this->version ); |
| 106 |
wp_register_script( 'jqcbxgooglemap', plugin_dir_url( __FILE__ ) . '../assets/js/jqcbxgooglemap.js', array( |
| 107 |
'jquery', |
| 108 |
'coregooglemapapi' |
| 109 |
), $this->version, true ); |
| 110 |
|
| 111 |
wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array( |
| 112 |
'jquery', |
| 113 |
'jqcbxgooglemap' |
| 114 |
), $this->version, true ); |
| 115 |
|
| 116 |
//wp_enqueue_script( 'coregooglemapapi' ); |
| 117 |
//wp_enqueue_script( 'jqcbxgooglemap' ); |
| 118 |
} else { |
| 119 |
wp_register_script( 'coregooglemapapi', '//unpkg.com/leaflet@1.3.4/dist/leaflet.js', array(), $this->version ); |
| 120 |
wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array( |
| 121 |
'jquery', |
| 122 |
'coregooglemapapi' |
| 123 |
), $this->version, true ); |
| 124 |
//wp_enqueue_script( 'coregooglemapapi' ); |
| 125 |
} |
| 126 |
|
| 127 |
//wp_enqueue_script( 'cbxgooglemap-public' ); |
| 128 |
|
| 129 |
$elementor_preview = isset($_REQUEST['elementor-preview'])? intval($_REQUEST['elementor-preview']) : 0; |
| 130 |
if($elementor_preview > 0 || (is_admin() && CBXGooglemapHelper::is_gutenberg_page()) ){ |
| 131 |
CBXGooglemapHelper::enqueue_js_css(true, false); |
| 132 |
} |
| 133 |
} |
| 134 |
}//end enqueue_scripts |
| 135 |
|
| 136 |
/** |
| 137 |
* Init all shortcodes |
| 138 |
*/ |
| 139 |
public function init_shortcodes() { |
| 140 |
add_shortcode( 'cbxgooglemap', array( $this, 'cbxgooglemap_shortcode' ) ); |
| 141 |
}//end init_shortcodes |
| 142 |
|
| 143 |
/** |
| 144 |
* Shortcode callback |
| 145 |
*/ |
| 146 |
public function cbxgooglemap_shortcode( $atts ) { |
| 147 |
|
| 148 |
$settings = $this->settings_api; |
| 149 |
|
| 150 |
CBXGooglemapHelper::enqueue_js_css(); |
| 151 |
|
| 152 |
$general_settings = get_option( 'cbxgooglemap_general', array() ); |
| 153 |
|
| 154 |
$hide_leaflet = intval($settings->get_field( 'hide_leaflet', $general_settings, 0 )); |
| 155 |
|
| 156 |
$api_key = $settings->get_field( 'apikey', $general_settings, '' ); |
| 157 |
$mapsource = intval( $settings->get_field( 'mapsource', $general_settings, 1 ) ); |
| 158 |
|
| 159 |
//if google map and api key is not present |
| 160 |
if ( $mapsource == 1 && $api_key == '' ) { |
| 161 |
return '<p style="text-align: center;">' . esc_html__( 'Google Map Api Key is invalid or empty. Please set in setting.', 'cbxgooglemap' ) . '</p>'; |
| 162 |
} |
| 163 |
|
| 164 |
$zoom_default = $settings->get_field( 'zoom', $general_settings, '8' ); |
| 165 |
if ( $zoom_default == 0 ) { |
| 166 |
$zoom_default = 8; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
$width_default = $settings->get_field( 'width', $general_settings, '100%' ); |
| 171 |
if ( $width_default == '' || $width_default == 0 ) { |
| 172 |
$width_default = '100%'; |
| 173 |
} |
| 174 |
|
| 175 |
$height_default = intval( $settings->get_field( 'height', $general_settings, '300' ) ); |
| 176 |
if ( $height_default == 0 ) { |
| 177 |
$height_default = 300; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
$scrollwheel_default = intval( $settings->get_field( 'scrollwheel', $general_settings, 1 ) ); |
| 182 |
$showinfo_default = intval( $settings->get_field( 'showinfo', $general_settings, 1 ) ); |
| 183 |
$infow_open_default = intval( $settings->get_field( 'infow_open', $general_settings, 1 ) ); |
| 184 |
$maptype_default = $settings->get_field( 'maptype', $general_settings, 'roadmap' ); |
| 185 |
$mapicon_default = $settings->get_field( 'mapicon', $general_settings, '' ); |
| 186 |
|
| 187 |
|
| 188 |
$atts = shortcode_atts( |
| 189 |
array( |
| 190 |
'id' => '', |
| 191 |
'maptype' => $maptype_default, |
| 192 |
'lat' => '', |
| 193 |
'lng' => '', |
| 194 |
'width' => $width_default, |
| 195 |
'height' => $height_default, |
| 196 |
'zoom' => $zoom_default, |
| 197 |
'scrollwheel' => $scrollwheel_default, |
| 198 |
'showinfo' => $showinfo_default, |
| 199 |
'infow_open' => $infow_open_default, //added in v1.1.2 |
| 200 |
'heading' => '', |
| 201 |
'address' => '', |
| 202 |
'website' => '', |
| 203 |
'mapicon' => $mapicon_default, //added in v1.1.2 |
| 204 |
), |
| 205 |
$atts, 'cbxgooglemap' ); |
| 206 |
|
| 207 |
$id = isset( $atts['id'] ) ? intval( $atts['id'] ) : 0; |
| 208 |
|
| 209 |
if ( $id > 0 ) { |
| 210 |
//show maps from post data |
| 211 |
$combined_field = '_cbxgooglemap_combined'; //field name for non sortable fields |
| 212 |
$meta_prefix = '_cbxgooglemap'; //field prefix for sortable fields |
| 213 |
|
| 214 |
$lat = get_post_meta( $id, $meta_prefix . 'lat', true ); |
| 215 |
$lat = ( $lat !== false ) ? $lat : ''; |
| 216 |
|
| 217 |
$lng = get_post_meta( $id, $meta_prefix . 'lng', true ); |
| 218 |
$lng = ( $lng !== false ) ? $lng : ''; |
| 219 |
|
| 220 |
if ( $lat == '' || $lng == '' ) { |
| 221 |
return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' ); |
| 222 |
} //at least we need lat lng |
| 223 |
|
| 224 |
|
| 225 |
$meta_combined = get_post_meta( $id, $combined_field, true ); |
| 226 |
$heading = get_the_title( $id ); |
| 227 |
|
| 228 |
$zoom = ( isset( $meta_combined[ $meta_prefix . 'zoom' ] ) && intval( $meta_combined[ $meta_prefix . 'zoom' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'zoom' ] ) : $zoom_default; |
| 229 |
|
| 230 |
$scrollwheel = ( isset( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'scrollwheel' ] ) : $scrollwheel_default; |
| 231 |
$showinfo = ( isset( $meta_combined[ $meta_prefix . 'showinfo' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'showinfo' ] ) : $showinfo_default; |
| 232 |
$infow_open = ( isset( $meta_combined[ $meta_prefix . 'infow_open' ] ) ) ? intval( $meta_combined[ $meta_prefix . 'infow_open' ] ) : $infow_open_default; |
| 233 |
|
| 234 |
$width = ( isset( $meta_combined[ $meta_prefix . 'width' ] ) && $meta_combined[ $meta_prefix . 'width' ] != '' ) ? $meta_combined[ $meta_prefix . 'width' ] : $width_default; |
| 235 |
$height = ( isset( $meta_combined[ $meta_prefix . 'height' ] ) && intval( $meta_combined[ $meta_prefix . 'height' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'height' ] ) : $height_default; |
| 236 |
|
| 237 |
$address = ( isset( $meta_combined[ $meta_prefix . 'address' ] ) && esc_html( $meta_combined[ $meta_prefix . 'address' ] ) != '' ) ? esc_html( $meta_combined[ $meta_prefix . 'address' ] ) : ''; |
| 238 |
$website = ( isset( $meta_combined[ $meta_prefix . 'website' ] ) && esc_url( $meta_combined[ $meta_prefix . 'website' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'website' ] ) : ''; |
| 239 |
|
| 240 |
$mapicon = ( isset( $meta_combined[ $meta_prefix . 'mapicon' ] ) && esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) != '' ) ? esc_url( $meta_combined[ $meta_prefix . 'mapicon' ] ) : ''; |
| 241 |
$maptype = ( isset( $meta_combined[ $meta_prefix . 'maptype' ] ) && sanitize_text_field( $meta_combined[ $meta_prefix . 'maptype' ] ) != '' ) ? sanitize_text_field( $meta_combined[ $meta_prefix . 'maptype' ] ) : ''; |
| 242 |
|
| 243 |
|
| 244 |
} else { |
| 245 |
//show map from shortcode params only |
| 246 |
$lat = isset( $atts['lat'] ) ? $atts['lat'] : ''; |
| 247 |
$lng = isset( $atts['lat'] ) ? $atts['lng'] : ''; |
| 248 |
|
| 249 |
if ( $lat == '' || $lng == '' ) { |
| 250 |
return esc_html__( 'Please set Latitude and Longitude both to display a map.', 'cbxgooglemap' ); |
| 251 |
} //at least we need lat lng |
| 252 |
|
| 253 |
|
| 254 |
$width = isset( $atts['width'] ) ? $atts['width'] : ''; |
| 255 |
$height = isset( $atts['height'] ) ? intval( $atts['height'] ) : ''; |
| 256 |
|
| 257 |
$zoom = isset( $atts['zoom'] ) ? $atts['zoom'] : $zoom_default; |
| 258 |
$heading = isset( $atts['heading'] ) ? $atts['heading'] : ''; |
| 259 |
$address = isset( $atts['address'] ) ? $atts['address'] : ''; |
| 260 |
$website = isset( $atts['website'] ) ? $atts['website'] : ''; |
| 261 |
$mapicon = isset( $atts['mapicon'] ) ? $atts['mapicon'] : ''; |
| 262 |
$maptype = isset( $atts['maptype'] ) ? $atts['maptype'] : ''; |
| 263 |
|
| 264 |
$showinfo = isset( $atts['showinfo'] ) ? intval( $atts['showinfo'] ) : $showinfo_default; |
| 265 |
$infow_open = isset( $atts['infow_open'] ) ? intval( $atts['infow_open'] ) : $infow_open_default; |
| 266 |
$scrollwheel = isset( $atts['scrollwheel'] ) ? intval( $atts['scrollwheel'] ) : $scrollwheel_default; |
| 267 |
|
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
if ( $heading == '' && $website == '' ) { |
| 272 |
$showinfo = 0; |
| 273 |
} |
| 274 |
|
| 275 |
if ( is_numeric( $width ) ) { |
| 276 |
$width = $width . 'px'; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
$extra_class = ($hide_leaflet)? 'cbxgooglemap_wrapper_hideleaflet' : ''; |
| 281 |
|
| 282 |
$output_html = '<div class="cbxgooglemap_wrapper '.esc_attr($extra_class).'">'; |
| 283 |
$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>'; |
| 284 |
$output_html .= '</div>'; |
| 285 |
|
| 286 |
return $output_html; |
| 287 |
}//end cbxgooglemap_shortcode |
| 288 |
|
| 289 |
/** |
| 290 |
* Classic widget |
| 291 |
*/ |
| 292 |
public function register_widget() { |
| 293 |
if ( ! class_exists( 'CBXGoogleMap_Widget' ) ) { |
| 294 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'widgets/classic-widgets/class-cbxgooglemap-widget.php'; |
| 295 |
} |
| 296 |
register_widget( "CBXGoogleMap_Widget" ); |
| 297 |
}//end register_widget |
| 298 |
|
| 299 |
/** |
| 300 |
* Init elementor widget |
| 301 |
* |
| 302 |
* @throws Exception |
| 303 |
*/ |
| 304 |
public function init_elementor_widgets() { |
| 305 |
if ( ! class_exists( 'CBXGooglemap_ElemWidget' ) ) { |
| 306 |
//include the file |
| 307 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'widgets/elementor-elements/class-cbxgooglemap-elemwidget.php'; |
| 308 |
} |
| 309 |
|
| 310 |
//register the widget |
| 311 |
\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new CBXGooglemapElemWidget\Widgets\CBXGooglemap_ElemWidget() ); |
| 312 |
}//end widgets_registered |
| 313 |
|
| 314 |
/** |
| 315 |
* Add new category to elementor |
| 316 |
* |
| 317 |
* @param $elements_manager |
| 318 |
*/ |
| 319 |
public function add_elementor_widget_categories( $elements_manager ) { |
| 320 |
$elements_manager->add_category( |
| 321 |
'codeboxr', |
| 322 |
array( |
| 323 |
'title' => esc_html__( 'Codeboxr Widgets', 'cbxgooglemap' ), |
| 324 |
'icon' => 'fa fa-plug', |
| 325 |
) |
| 326 |
); |
| 327 |
}//end add_elementor_widget_categories |
| 328 |
|
| 329 |
/** |
| 330 |
* Load Elementor Custom Icon |
| 331 |
*/ |
| 332 |
function elementor_icon_loader() { |
| 333 |
wp_register_style( 'cbxgooglemap_elementor_icon', CBXGOOGLEMAP_ROOT_URL . 'widgets/elementor-elements/elementor-icon/icon.css', false, CBXGOOGLEMAP_PLUGIN_VERSION ); |
| 334 |
wp_enqueue_style( 'cbxgooglemap_elementor_icon' ); |
| 335 |
|
| 336 |
//CBXGooglemapHelper::enqueue_js_css(); |
| 337 |
}//end elementor_icon_loader |
| 338 |
|
| 339 |
|
| 340 |
/** |
| 341 |
* // Before VC Init |
| 342 |
*/ |
| 343 |
public function vc_before_init_actions() { |
| 344 |
if ( ! class_exists( 'CBXGoogleMap_WPBWidget' ) ) { |
| 345 |
require_once CBXGOOGLEMAP_ROOT_PATH . 'widgets/vc-element/class-cbxgooglemap-wpbwidget.php'; |
| 346 |
} |
| 347 |
|
| 348 |
new CBXGoogleMap_WPBWidget(); |
| 349 |
}// end method vc_before_init_actions |
| 350 |
}//end class CBXGoogleMap_Public |
| 351 |
|