PluginProbe
CBX Map for Google Map & OpenStreetMap / 2.0.0
CBX Map for Google Map & OpenStreetMap v2.0.0
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 / Widgets / Elementor / CBXGooglemapElemWidget.php

CBXGooglemapElemWidget.php in CBX Map for Google Map & OpenStreetMap 2.0.0, at includes/Widgets/Elementor/CBXGooglemapElemWidget.php

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