| 1 |
<?php |
| 2 |
namespace CBXGooglemapElemWidget\Widgets; |
| 3 |
|
| 4 |
use Elementor\Widget_Base; |
| 5 |
use Elementor\Controls_Manager; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Google Maps Widget |
| 13 |
*/ |
| 14 |
class CBXGooglemap_ElemWidget extends \Elementor\Widget_Base { |
| 15 |
|
| 16 |
/** |
| 17 |
* Retrieve google maps widget name. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* @access public |
| 21 |
* |
| 22 |
* @return string Widget name. |
| 23 |
*/ |
| 24 |
public function get_name() { |
| 25 |
return 'cbxgooglemap_google_map'; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Retrieve google maps widget title. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* @access public |
| 33 |
* |
| 34 |
* @return string Widget title. |
| 35 |
*/ |
| 36 |
public function get_title() { |
| 37 |
return esc_html__( 'CBX Map', 'cbxgooglemap' ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get widget categories. |
| 42 |
* |
| 43 |
* Retrieve the widget categories. |
| 44 |
* |
| 45 |
* @since 1.0.10 |
| 46 |
* @access public |
| 47 |
* |
| 48 |
* @return array Widget categories. |
| 49 |
*/ |
| 50 |
public function get_categories() { |
| 51 |
return array( 'codeboxr' ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Retrieve google maps widget icon. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* @access public |
| 59 |
* |
| 60 |
* @return string Widget icon. |
| 61 |
*/ |
| 62 |
public function get_icon() { |
| 63 |
return 'cbxmap-icon'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register google maps widget controls. |
| 68 |
* |
| 69 |
* Adds different input fields to allow the user to change and customize the widget settings. |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* @access protected |
| 73 |
*/ |
| 74 |
protected function _register_controls() { |
| 75 |
$settings = new \CBXGooglemapSettings(); |
| 76 |
$general_settings = get_option( 'cbxgooglemap_general', array() ); |
| 77 |
|
| 78 |
//default field values |
| 79 |
$zoom_default = $settings->get_field( 'zoom', $general_settings, '8' ); |
| 80 |
if ( $zoom_default == 0 ) { |
| 81 |
$zoom_default = 8; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
$width_default = $settings->get_field( 'width', $general_settings, '100%' ); |
| 86 |
if ( $width_default == '' || $width_default == 0 ) { |
| 87 |
$width_default = '100%'; |
| 88 |
} |
| 89 |
|
| 90 |
$height_default = intval( $settings->get_field( 'height', $general_settings, '300' ) ); |
| 91 |
if ( $height_default == 0 ) { |
| 92 |
$height_default = 300; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
$scrollwheel_default = intval( $settings->get_field( 'scrollwheel', $general_settings, 1 ) ); |
| 97 |
$showinfo_default = intval( $settings->get_field( 'showinfo', $general_settings, 1 ) ); |
| 98 |
$infow_open_default = intval( $settings->get_field( 'infow_open', $general_settings, 1 ) ); |
| 99 |
$maptype_default = $settings->get_field( 'maptype', $general_settings, 'roadmap' ); |
| 100 |
|
| 101 |
|
| 102 |
$this->start_controls_section( |
| 103 |
'section_cbxmap', |
| 104 |
array( |
| 105 |
'label' => esc_html__( 'CBX Map', 'cbxgooglemap' ), |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
$query = get_posts( array( |
| 110 |
'post_type' => 'cbxgooglemap', |
| 111 |
'orderby' => 'date', |
| 112 |
'posts_per_page' => - 1, |
| 113 |
'post_status' => 'publish', |
| 114 |
) ); |
| 115 |
|
| 116 |
$googleMap_posts = array(); |
| 117 |
|
| 118 |
$googleMap_posts[''] = esc_html__('Choose Ready Map or Use custom params', 'cbxgooglemap'); |
| 119 |
|
| 120 |
foreach ( $query as $key => $data ) { |
| 121 |
$googleMap_posts[ $data->ID ] = $data->post_title; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
$this->add_control( |
| 126 |
'cbxgooglemap_post_id', |
| 127 |
array( |
| 128 |
'label' => esc_html__( 'Predefined Map', 'cbxgooglemap' ), |
| 129 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 130 |
'placeholder' => esc_html__( 'Choose Ready Map or Use custom params', 'cbxgooglemap' ), |
| 131 |
'default' => '', |
| 132 |
'label_block' => true, |
| 133 |
'options' => $googleMap_posts, |
| 134 |
'description' => esc_html__( 'Choose predefined map or create from custom attributes below', 'cbxgooglemap' ), |
| 135 |
) |
| 136 |
); |
| 137 |
|
| 138 |
$this->add_control( |
| 139 |
'hr', |
| 140 |
array( |
| 141 |
'type' => \Elementor\Controls_Manager::DIVIDER, |
| 142 |
|
| 143 |
) |
| 144 |
); |
| 145 |
|
| 146 |
//regular fields |
| 147 |
$this->add_control( |
| 148 |
'cbxgooglemap_custom_1', |
| 149 |
array( |
| 150 |
'label' => esc_html__( 'Custom Map Attributes', 'cbxgooglemap' ), |
| 151 |
'type' => \Elementor\Controls_Manager::HEADING, |
| 152 |
'default' => '', |
| 153 |
) |
| 154 |
); |
| 155 |
|
| 156 |
$this->add_control( |
| 157 |
'cbxgooglemap_maptype', |
| 158 |
array( |
| 159 |
'label' => esc_html__( 'Map Type', 'cbxgooglemap' ), |
| 160 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 161 |
'options' => array( |
| 162 |
'roadmap' => esc_html__( 'Road Map', 'cbxgooglemap' ), |
| 163 |
'satellite' => esc_html__( 'Satellite Map', 'cbxgooglemap' ), |
| 164 |
'hybrid' => esc_html__( 'Hybrid Map', 'cbxgooglemap' ), |
| 165 |
'terrain' => esc_html__( 'Terrain Map', 'cbxgooglemap' ), |
| 166 |
), |
| 167 |
'default' => $maptype_default, |
| 168 |
'description' => esc_html__( 'Google Map only', 'cbxgooglemap' ), |
| 169 |
) |
| 170 |
); |
| 171 |
|
| 172 |
$this->add_control( |
| 173 |
'cbxgooglemap_lat', |
| 174 |
array( |
| 175 |
'label' => esc_html__( 'Latitude', 'cbxgooglemap' ), |
| 176 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 177 |
'placeholder' => esc_html__( 'Latitude', 'cbxgooglemap' ), |
| 178 |
'default' => '', |
| 179 |
) |
| 180 |
); |
| 181 |
|
| 182 |
$this->add_control( |
| 183 |
'cbxgooglemap_lng', |
| 184 |
array( |
| 185 |
'label' => esc_html__( 'Longitude', 'cbxgooglemap' ), |
| 186 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 187 |
'placeholder' => esc_html__( 'Longitude', 'cbxgooglemap' ), |
| 188 |
'default' => '', |
| 189 |
) |
| 190 |
); |
| 191 |
|
| 192 |
$this->add_control( |
| 193 |
'cbxgooglemap_width', |
| 194 |
array( |
| 195 |
'label' => esc_html__( 'Width(Numeric or with %)', 'cbxgooglemap' ), |
| 196 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 197 |
'placeholder' => esc_html__( 'Width', 'cbxgooglemap' ), |
| 198 |
'default' => $width_default, |
| 199 |
) |
| 200 |
); |
| 201 |
|
| 202 |
$this->add_control( |
| 203 |
'cbxgooglemap_height', |
| 204 |
array( |
| 205 |
'label' => esc_html__( 'Height', 'cbxgooglemap' ), |
| 206 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 207 |
'placeholder' => esc_html__( 'Height', 'cbxgooglemap' ), |
| 208 |
'default' => $height_default, |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
$this->add_control( |
| 213 |
'cbxgooglemap_zoom', |
| 214 |
array( |
| 215 |
'label' => esc_html__( 'Zoom(Numeric Value)', 'cbxgooglemap' ), |
| 216 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 217 |
'placeholder' => esc_html__( 'Zoom', 'cbxgooglemap' ), |
| 218 |
'default' => $zoom_default, |
| 219 |
) |
| 220 |
); |
| 221 |
|
| 222 |
$this->add_control( |
| 223 |
'cbxgooglemap_scrollwheel', |
| 224 |
array( |
| 225 |
'label' => esc_html__( 'Mouse Scroll Wheel', 'cbxgooglemap' ), |
| 226 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 227 |
'options' => array( |
| 228 |
'1' => esc_html__( 'Enable', 'cbxgooglemap' ), |
| 229 |
'0' => esc_html__( 'Disable', 'cbxgooglemap' ), |
| 230 |
), |
| 231 |
'default' => $scrollwheel_default, |
| 232 |
) |
| 233 |
); |
| 234 |
|
| 235 |
$this->add_control( |
| 236 |
'cbxgooglemap_showinfo', |
| 237 |
array( |
| 238 |
'label' => esc_html__( 'Show Popup', 'cbxgooglemap' ), |
| 239 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 240 |
'options' => array( |
| 241 |
'1' => esc_html__( 'Enable', 'cbxgooglemap' ), |
| 242 |
'0' => esc_html__( 'Disable', 'cbxgooglemap' ), |
| 243 |
), |
| 244 |
'default' => $showinfo_default, |
| 245 |
) |
| 246 |
); |
| 247 |
|
| 248 |
$this->add_control( |
| 249 |
'cbxgooglemap_infow_open', |
| 250 |
array( |
| 251 |
'label' => esc_html__( 'Info/Popup Window', 'cbxgooglemap' ), |
| 252 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 253 |
'options' => array( |
| 254 |
'1' => esc_html__( 'Open(Default)', 'cbxgooglemap' ), |
| 255 |
'0' => esc_html__( 'On Click', 'cbxgooglemap' ), |
| 256 |
), |
| 257 |
'default' => $infow_open_default, |
| 258 |
) |
| 259 |
); |
| 260 |
|
| 261 |
$this->add_control( |
| 262 |
'cbxgooglemap_heading', |
| 263 |
array( |
| 264 |
'label' => esc_html__( 'Popup Heading', 'cbxgooglemap' ), |
| 265 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 266 |
'placeholder' => esc_html__( 'Popup Heading', 'cbxgooglemap' ), |
| 267 |
'default' => '', |
| 268 |
) |
| 269 |
); |
| 270 |
|
| 271 |
$this->add_control( |
| 272 |
'cbxgooglemap_address', |
| 273 |
array( |
| 274 |
'label' => esc_html__( 'Location Address', 'cbxgooglemap' ), |
| 275 |
'type' => \Elementor\Controls_Manager::TEXT, |
| 276 |
'placeholder' => esc_html__( 'Address', 'cbxgooglemap' ), |
| 277 |
'default' => '', |
| 278 |
) |
| 279 |
); |
| 280 |
|
| 281 |
$this->add_control( |
| 282 |
'cbxgooglemap_website', |
| 283 |
array( |
| 284 |
'label' => esc_html__( 'Website url', 'cbxgooglemap' ), |
| 285 |
'type' => \Elementor\Controls_Manager::URL, |
| 286 |
'placeholder' => esc_html__( 'Website', 'cbxgooglemap' ), |
| 287 |
'default' => array( |
| 288 |
'url' => '', |
| 289 |
), |
| 290 |
'show_external' => false, |
| 291 |
) |
| 292 |
); |
| 293 |
|
| 294 |
$this->add_control( |
| 295 |
'cbxgooglemap_mapicon', |
| 296 |
array( |
| 297 |
'label' => esc_html__( 'Map Icon', 'cbxgooglemap' ), |
| 298 |
'type' => \Elementor\Controls_Manager::MEDIA, |
| 299 |
'default' => array( |
| 300 |
'url' => '', |
| 301 |
), |
| 302 |
) |
| 303 |
); |
| 304 |
|
| 305 |
$this->end_controls_section(); |
| 306 |
}//end _register_controls |
| 307 |
|
| 308 |
/** |
| 309 |
* Render google maps widget output on the frontend. |
| 310 |
* |
| 311 |
* Written in PHP and used to generate the final HTML. |
| 312 |
* |
| 313 |
* @since 1.0.0 |
| 314 |
* @access protected |
| 315 |
*/ |
| 316 |
protected function render() { |
| 317 |
if ( ! class_exists( 'CBXGooglemapSettings' ) ) { |
| 318 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-cbxgooglemap-settings.php'; |
| 319 |
} |
| 320 |
|
| 321 |
$settings_api = new \CBXGooglemapSettings(); |
| 322 |
$general_settings = get_option( 'cbxgooglemap_general', array() ); |
| 323 |
|
| 324 |
$googlemap_api_key = $settings_api->get_field( 'apikey', $general_settings, '' ); |
| 325 |
$mapsource = intval( $settings_api->get_field( 'mapsource', $general_settings, 1 ) ); |
| 326 |
|
| 327 |
if ( $mapsource == 1 && $googlemap_api_key == '' ) { |
| 328 |
echo '<p style="text-align: center;">' . esc_html__( 'Google Map Api Key is invalid!', |
| 329 |
'cbxgooglemap' ) . '</p>'; |
| 330 |
} else { |
| 331 |
$settings = $this->get_settings(); |
| 332 |
|
| 333 |
$id = intval( $settings['cbxgooglemap_post_id'] ); |
| 334 |
|
| 335 |
if ( $id > 0 ) { |
| 336 |
//render map from saved map |
| 337 |
echo do_shortcode( '[cbxgooglemap id="' . $id . '"]' ); |
| 338 |
} else { |
| 339 |
|
| 340 |
//render map from custom attributes |
| 341 |
$lat = sanitize_text_field( $settings['cbxgooglemap_lat'] ); |
| 342 |
$lng = sanitize_text_field( $settings['cbxgooglemap_lng'] ); |
| 343 |
$width = sanitize_text_field( $settings['cbxgooglemap_width'] ); |
| 344 |
$height = sanitize_text_field( $settings['cbxgooglemap_height'] ); |
| 345 |
$zoom = $settings['cbxgooglemap_zoom']; |
| 346 |
$scrollwheel = intval( $settings['cbxgooglemap_scrollwheel'] ); |
| 347 |
$showinfo = intval( $settings['cbxgooglemap_showinfo'] ); |
| 348 |
$infow_open = intval( $settings['cbxgooglemap_infow_open'] ); |
| 349 |
$heading = sanitize_text_field( $settings['cbxgooglemap_heading'] ); |
| 350 |
$address = sanitize_text_field( $settings['cbxgooglemap_address'] ); |
| 351 |
$maptype = sanitize_text_field( $settings['cbxgooglemap_maptype'] ); |
| 352 |
|
| 353 |
$website_data = $settings['cbxgooglemap_website']; |
| 354 |
$mapicon_data = $settings['cbxgooglemap_mapicon']; |
| 355 |
$website = isset( $website_data['url'] ) ? $website_data['url'] : ''; |
| 356 |
$mapicon = isset( $mapicon_data['url'] ) ? $mapicon_data['url'] : ''; |
| 357 |
|
| 358 |
|
| 359 |
echo do_shortcode( '[cbxgooglemap lat="' . $lat . '" lng="' . $lng . '" zoom="' . $zoom . '" scrollwheel="' . $scrollwheel . '" showinfo="' . $showinfo . '" infow_open="' . $infow_open . '" heading="' . $heading . '" maptype="' . $maptype . '" website="' . $website . '" address="' . $address . '" mapicon="' . $mapicon . '" width="' . $width . '" height="' . $height . '"]' ); |
| 360 |
} |
| 361 |
} |
| 362 |
}//end render |
| 363 |
|
| 364 |
/** |
| 365 |
* Render google maps widget output in the editor. |
| 366 |
* |
| 367 |
* Written as a Backbone JavaScript template and used to generate the live preview. |
| 368 |
* |
| 369 |
* @access protected |
| 370 |
*/ |
| 371 |
protected function _content_template() { |
| 372 |
}//end _content_template |
| 373 |
}//end CBXGooglemap_ElemWidget |
| 374 |
|