PluginProbe
CBX Map for Google Map & OpenStreetMap / 1.1.12
CBX Map for Google Map & OpenStreetMap v1.1.12
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 / admin / class-cbxgooglemap-admin.php

class-cbxgooglemap-admin.php in CBX Map for Google Map & OpenStreetMap 1.1.12, at admin/class-cbxgooglemap-admin.php

1,126 lines 37.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The admin-specific functionality of the plugin.
5 *
6 * @link https://codeboxr.com
7 * @since 1.0.0
8 *
9 * @package Cbxgooglemap
10 * @subpackage Cbxgooglemap/admin
11 */
12
13 /**
14 * The admin-specific 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/admin
21 * @author Codeboxr <info@codeboxr.com>
22 */
23 class CBXGoogleMap_Admin {
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 * The ID of this plugin.
45 *
46 * @since 1.1.12
47 * @access private
48 * @var string $settings The ID of this plugin.
49 */
50 public $settings;
51
52 /**
53 * Initialize the class and set its properties.
54 *
55 *
56 * @since 1.0.0
57 */
58 public function __construct() {
59 $this->plugin_name = CBXGOOGLEMAP_PLUGIN_NAME;
60 $this->version = CBXGOOGLEMAP_PLUGIN_VERSION;
61
62 //get instance of setting api
63 $this->settings = new CBXGooglemapSettings();
64 }//end of construtor
65
66 /**
67 * Initialize setting
68 */
69 public function setting_init() {
70 //set the settings
71 $this->settings->set_sections( $this->get_settings_sections() );
72 $this->settings->set_fields( $this->get_settings_fields() );
73 //initialize settings
74 $this->settings->admin_init();
75 }//end setting_init
76
77 /**
78 * Global Setting Sections
79 *
80 *
81 * @return array
82 */
83 public function get_settings_sections() {
84 return apply_filters(
85 'cbxgooglemap_setting_sections', [
86 [
87 'id' => 'cbxgooglemap_general',
88 'title' => esc_html__( 'Default Config', 'cbxgooglemap' )
89 ],
90 [
91 'id' => 'cbxgooglemap_demo',
92 'title' => esc_html__( 'Demo & Shortcodes', 'cbxgooglemap' )
93 ]
94 ]
95 );
96 }//end get_settings_sections
97
98 /**
99 * Returns all the settings fields
100 *
101 * @return array settings fields
102 */
103 public function get_settings_fields() {
104 $all_post_types = CBXGooglemapHelper::post_types();
105 $general_settings = get_option( 'cbxgooglemap_general', [] );
106
107 $settings_builtin_fields = [
108 'cbxgooglemap_general' => [
109 'mapsource' => [
110 'name' => 'mapsource',
111 'label' => esc_html__( 'Map Source', 'cbxgooglemap' ),
112 'type' => 'radio',
113 'default' => '1',
114 'options' => [
115 '1' => esc_html__( 'Google Map', 'cbxgooglemap' ),
116 '0' => esc_html__( 'Openstreet Map(leafletjs)', 'cbxgooglemap' )
117 ],
118 'inline' => true,
119 'sanitize_callback' => 'absint'
120 ],
121 'apikey' => [
122 'name' => 'apikey',
123 'label' => esc_html__( 'Api Key', 'cbxgooglemap' ),
124 'desc' => esc_html__( 'Google map api key', 'cbxgooglemap' ),
125 'type' => 'text'
126
127 ],
128 'maptype' => [
129 'name' => 'maptype',
130 'label' => esc_html__( 'Map Type', 'cbxgooglemap' ),
131 'type' => 'select',
132 'default' => 'roadmap',
133 'options' => [
134 'roadmap' => esc_html__( 'Road Map', 'cbxgooglemap' ),
135 'satellite' => esc_html__( 'Satellite Map', 'cbxgooglemap' ),
136 'hybrid' => esc_html__( 'Hybrid Map', 'cbxgooglemap' ),
137 'terrain' => esc_html__( 'Terrain Map', 'cbxgooglemap' ),
138 ],
139 'desc' => esc_html__( 'Google Map only', 'cbxgooglemap' ),
140 'sanitize_callback' => 'sanitize_text_field'
141 ],
142 'zoom' => [
143 'name' => 'zoom',
144 'label' => esc_html__( 'Zoom Level', 'cbxgooglemap' ),
145 'desc' => esc_html__( 'Default Zoom Level', 'cbxgooglemap' ),
146 'type' => 'number',
147 'step' => 1,
148 'default' => 8,
149 'sanitize_callback' => 'absint'
150
151 ],
152 'width' => [
153 'name' => 'with',
154 'label' => esc_html__( 'Width', 'cbxgooglemap' ),
155 'desc' => esc_html__( 'Default is 100% to make the map responsive, if you want any fixed width then don\'t put, % or just put numeric value, don\'t px with numeric value', 'cbxgooglemap' ),
156 'type' => 'text',
157 'default' => '100%',
158
159 ],
160 'height' => [
161 'name' => 'height',
162 'label' => esc_html__( 'Height', 'cbxgooglemap' ),
163 'desc' => esc_html__( 'Default height 300 as px, put any numeric value.', 'cbxgooglemap' ),
164 'type' => 'number',
165 'default' => '300',
166 'sanitize_callback' => 'floatval'
167 ],
168 'scrollwheel' => [
169 'name' => 'scrollwheel',
170 'label' => esc_html__( 'Mouse Scroll Wheel', 'cbxgooglemap' ),
171 'desc' => esc_html__( 'Enable/disable mouse scroll whell', 'cbxgooglemap' ),
172 'type' => 'radio',
173 'default' => '1',
174 'options' => [
175 '1' => esc_html__( 'Enable', 'cbxgooglemap' ),
176 '0' => esc_html__( 'Disable', 'cbxgooglemap' ),
177 ],
178 'inline' => true,
179 'sanitize_callback' => 'absint'
180 ],
181 'showinfo' => [
182 'name' => 'showinfo',
183 'label' => esc_html__( 'Show Info window', 'cbxgooglemap' ),
184 'desc' => esc_html__( 'Show information on click of marker', 'cbxgooglemap' ),
185 'type' => 'radio',
186 'default' => '1',
187 'desc_tip' => true,
188 'options' => [
189 '1' => esc_html__( 'Enable', 'cbxgooglemap' ),
190 '0' => esc_html__( 'Disable', 'cbxgooglemap' ),
191 ],
192 'inline' => true,
193 'sanitize_callback' => 'absint'
194 ],
195 'infow_open' => [
196 'name' => 'infow_open',
197 'label' => esc_html__( 'Info/Popup Window', 'cbxgooglemap' ),
198 'type' => 'radio',
199 'default' => '1',
200 'desc_tip' => true,
201 'options' => [
202 '1' => esc_html__( 'Open(Default)', 'cbxgooglemap' ),
203 '0' => esc_html__( 'On Click', 'cbxgooglemap' ),
204 ],
205 'inline' => true,
206 'sanitize_callback' => 'absint'
207 ],
208 'mapicon' => [
209 'name' => 'mapicon',
210 'label' => esc_html__( 'Map Icon', 'cbxgooglemap' ),
211 'type' => 'file',
212 'default' => '',
213 'desc_tip' => true,
214
215 ],
216 'hide_leaflet' => [
217 'name' => 'hide_leaflet',
218 'label' => esc_html__( 'Hide Leaflet Branding', 'cbxgooglemap' ),
219 'type' => 'radio',
220 'default' => '0',
221 'options' => [
222 '1' => esc_html__( 'Hide', 'cbxgooglemap' ),
223 '0' => esc_html__( 'Show/Default', 'cbxgooglemap' ),
224 ],
225 'inline' => true,
226 'sanitize_callback' => 'absint'
227 ],
228
229 ],
230 'cbxgooglemap_demo' => [
231 'shortcode_demo' => [
232 'name' => 'shortcode_demo',
233 'label' => esc_html__( 'Shortcode & Demo', 'cbxgooglemap' ),
234 'desc' => esc_html__( 'Shortcode and demo based on default setting, please save once to check change.', 'cbxgooglemap' ),
235 'type' => 'shortcode',
236 'class' => 'cbcurrencyconverter_demo_copy',
237 'default' => CBXGooglemapHelper::shortcode_builder( $general_settings ),
238 'sanitize_callback' => 'sanitize_text_field'
239 ]
240
241 ]
242 ];
243
244
245 $settings_fields = []; //final setting array that will be passed to different filters
246 $sections = $this->get_settings_sections();
247
248 foreach ( $sections as $section ) {
249 if ( ! isset( $settings_builtin_fields[ $section['id'] ] ) ) {
250 $settings_builtin_fields[ $section['id'] ] = [];
251 }
252 }
253
254
255 foreach ( $sections as $section ) {
256 $settings_fields[ $section['id'] ] = apply_filters( 'cbxgooglemap_global_' . $section['id'] . '_fields', $settings_builtin_fields[ $section['id'] ] );
257 }
258
259 return apply_filters( 'cbxgooglemap_global_fields', $settings_fields );
260 }//end get_settings_fields
261
262 /**
263 * Register Custom Post Type cbxgooglemap
264 *
265 * @since 3.7.0
266 */
267 public function create_post_type() {
268 CBXGooglemapHelper::create_googlemap_post_type();
269
270 // Check the option we set on activation.
271 if ( get_option( 'cbxgooglemap_flush_rewrite_rules' ) == 'true' ) {
272 flush_rewrite_rules();
273 delete_option( 'cbxgooglemap_flush_rewrite_rules' );
274 }
275 }//end create_post_type
276
277
278 /**
279 * Show menu page
280 */
281 public function menu_pages() {
282 //setting page
283 $setting_page_hook = add_submenu_page( 'edit.php?post_type=cbxgooglemap', esc_html__( 'CBX WP Map Settings', 'cbxgooglemap' ), esc_html__( 'Setting', 'cbxgooglemap' ), 'manage_options', 'cbxgooglemapsettings', [
284 $this,
285 'menu_page_settings'
286 ] );
287 $doc_page_hook = add_submenu_page( 'edit.php?post_type=cbxgooglemap', esc_html__( 'Helps & Updates', 'cbxgooglemap' ), esc_html__( 'Helps & Updates', 'cbxgooglemap' ), 'manage_options', 'cbxgooglemap-help-support', [
288 $this,
289 'menu_page_docs'
290 ] );
291 }//end menu_pages
292
293 /**
294 * Show cbxeventz Setting page
295 */
296 public function menu_page_settings() {
297 echo cbxgooglemap_get_template_html( 'admin/settings-display.php', [ 'ref' => $this ] );
298 }//end menu_page_settings
299
300 /**
301 * Render the help & support page for this plugin.
302 *
303 * @since 1.0.0
304 */
305 public function menu_page_docs() {
306 echo cbxgooglemap_get_template_html( 'admin/dashboard.php' );
307 }//end method menu_page_docs
308
309 /**
310 * Add metabox for custom post type cbxfeedbackform && cbxfeedbackbtn
311 *
312 * @since 1.0.0
313 */
314 public function add_meta_boxes() {
315 add_meta_box( 'cbxgooglemap_metabox', esc_html__( 'Map Parameter', 'cbxgooglemap' ), [
316 $this,
317 'parameter_metabox_display'
318 ], 'cbxgooglemap', 'normal', 'high' );
319 add_meta_box( 'cbxgooglemap_shortcode', esc_html__( 'Shortcode', 'cbxgooglemap' ), [
320 $this,
321 'shortcode_metabox_display'
322 ], 'cbxgooglemap', 'side', 'low' );
323 }//end add_meta_boxes
324
325 /**
326 * Show Shortcode display metabox
327 *
328 * @param $post
329 */
330 public function shortcode_metabox_display( $post ) {
331 if ( isset( $post->ID ) && $post->ID > 0 ) {
332 $post_id = $post->ID;
333 $post_type = $post->post_type;
334
335 echo cbxgooglemap_get_template_html( 'admin/metabox_shortcode.php', [
336 'post_id' => $post_id,
337 'post_type' => $post_type
338 ] );
339 }
340 }//end shortcode_metabox_display
341
342 /**
343 * Render metabox
344 *
345 * @param $post
346 *
347 * since v1.0.0
348 */
349 public function parameter_metabox_display( $post ) {
350 global $post;
351 $post_type = $post->post_type;
352
353
354 $meta_fields = CBXGooglemapHelper::cbxgooglemap_meta_fields();
355
356 $combined_field = '_' . $post_type . '_combined'; //field name for non sortable fields
357 $meta_prefix = '_' . $post_type; //field prefix for sortable fields
358
359
360 CBXGooglemapHelper::render_meta_fields( $post, $meta_fields, $combined_field, $meta_prefix, true ); //
361 }//end parameter_metabox_display
362
363 /**
364 * Save meta box for cbxeventz
365 *
366 * @param $post_id
367 */
368 public function metabox_save( $post_id, $post, $update ) {
369
370 $post_type = $post->post_type;
371
372 // Check if our nonce is set.
373 if ( ! isset( $_POST[ 'cbxmetahelper_' . $post_type . '_meta_box_nonce' ] ) ) {
374 return;
375 }
376
377 // Verify that the nonce is valid.
378 if ( ! wp_verify_nonce( $_POST[ 'cbxmetahelper_' . $post_type . '_meta_box_nonce' ], 'cbxmetahelper_' . $post_type . '_meta_box' ) ) {
379 return;
380 }
381
382 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
383 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
384 return $post_id;
385 }
386
387 // Check the user's permissions.
388 if ( isset( $_POST['post_type'] ) && $post_type == $_POST['post_type'] ) {
389
390 if ( ! current_user_can( 'edit_post', $post_id ) ) {
391 return;
392 }
393 }
394
395 if ( method_exists( 'CBXGooglemapHelper', $post_type . '_meta_fields' ) ) {
396 $meta_fields = call_user_func( 'CBXGooglemapHelper::' . $post_type . '_meta_fields', [
397 $post_id,
398 $post_type
399 ] );
400 } else {
401 return;
402 }
403
404
405 $combined_field = '_' . $post_type . '_combined'; //field name for non sortable fields
406 $meta_prefix = '_' . $post_type; //field prefix for sortable fields
407
408 $meta_combined = get_post_meta( $post_id, $combined_field, true ); //meta fields not sortable
409
410 $meta_combined = ! is_array( $meta_combined ) ? [] : $meta_combined;
411
412
413 $combined_arr = [];
414 foreach ( $meta_fields as $section_id => $fields ) {
415 foreach ( $fields as $id => $field ) {
416
417 $field_type = $field['type'];
418
419 $sanitize_callback = isset( $field['sanitize_callback'] ) ? $field['sanitize_callback'] : null;
420
421 if ( isset( $_POST[ $meta_prefix . $id ] ) ) {
422
423 if ( isset( $_POST[ $meta_prefix . $id ] ) ) {
424 $updated_value = $_POST[ $meta_prefix . $id ];
425
426
427 if ( $field_type == 'text' ) {
428 $updated_value = wp_strip_all_tags( sanitize_text_field( $updated_value ) );
429 } elseif ( $field_type == 'textarea' ) {
430 $updated_value = wp_strip_all_tags( sanitize_textarea_field( $updated_value ) );
431 } else {
432 if ( $sanitize_callback !== null && is_callable( $sanitize_callback ) ) {
433 $updated_value = call_user_func( $sanitize_callback, $updated_value );
434 }
435 }
436
437 $is_sortable = isset( $field['sortable'] ) ? $field['sortable'] : false;
438
439 if ( $is_sortable ) {
440 $ret = update_post_meta( $post_id, $meta_prefix . $id, $updated_value ); //update the combined meta
441 } else {
442 //save as combined meta
443 $meta_combined[ $meta_prefix . $id ] = $updated_value;
444 }
445 }
446
447 }
448
449 }
450 }
451
452 update_post_meta( $post_id, $combined_field, $meta_combined ); //update the combined meta
453
454 do_action( 'cbxgooglemap_metabox_save', $post_id );
455
456 }//end metabox_save
457
458 /**
459 * Add or adjust col for cbxgooglemap post type
460 *
461 * @param $cbxpoll_columns
462 *
463 * @return array
464 *
465 */
466 public function cbxgooglemap_add_new_columns( $columns ) {
467 unset( $columns['date'] );
468 unset( $columns['comments'] );
469
470 $columns['lat'] = esc_attr( 'Latitude', 'cbxgooglemap' );
471 $columns['lng'] = esc_attr( 'Longitude', 'cbxgooglemap' );
472 $columns['zoom'] = esc_attr( 'Zoom', 'cbxgooglemap' );
473 $columns['shortcode'] = esc_attr( 'Shortcode', 'cbxgooglemap' );
474
475 return $columns;
476 }//end cbxgooglemap_add_new_columns
477
478 /**
479 * Add extra cols information for cbxgooglemap post type
480 *
481 * @param $column_name
482 *
483 *
484 * show data to poll table custom column
485 */
486 public function cbxgooglemap_manage_columns( $column_name ) {
487 global $wpdb, $post;
488
489 $post_id = intval( $post->ID );
490 $post_type = $post->post_type;
491
492 $combined_field = '_cbxgooglemap_combined'; //field name for non sortable fields
493 $meta_prefix = '_cbxgooglemap'; //field prefix for sortable fields
494
495 $meta_combined = get_post_meta( $post_id, $combined_field, true );
496
497 $lat = get_post_meta( $post_id, $meta_prefix . 'lat', true );
498 $lat = ( $lat !== false ) ? $lat : '';
499
500 $lng = get_post_meta( $post_id, $meta_prefix . 'lng', true );
501 $lng = ( $lng !== false ) ? $lng : '';
502
503 $zoom = ( isset( $meta_combined[ $meta_prefix . 'zoom' ] ) && intval( $meta_combined[ $meta_prefix . 'zoom' ] ) > 0 ) ? intval( $meta_combined[ $meta_prefix . 'zoom' ] ) : '';
504
505
506 switch ( $column_name ) {
507 case 'shortcode':
508 echo '<span id="cbxgooglemapshortcode-' . intval( $post_id ) . '" class="cbxgooglemapshortcode cbxgooglemapshortcode-' . intval( $post_id ) . '">[cbxgooglemap id="' . intval( $post_id ) . '"]</span>';
509 echo '<span class="cbxgooglemapshortcodecopytrigger" data-clipboard-text=\'[cbxgooglemap id="' . intval( $post_id ) . '"]\' data-success="' . esc_html__( 'Copied', 'cbxgooglemap' ) . '" title="' . esc_attr__( 'Click to copy', 'cbxgooglemap' ) . '"><img class="cbxgooglemapshortcode-copy-image" src="' . CBXGOOGLEMAP_ROOT_URL . 'assets/img/copy.svg" alt="' . esc_attr__( 'CBX GoogleMaps Shortcode Copy', 'cbxgooglemap' ) . '"/></span>';
510 break;
511 case 'lat':
512 echo $lat;
513 break;
514 case 'lng':
515 echo $lng;
516 break;
517 case 'zoom':
518 echo $zoom;
519 break;
520 default:
521 break;
522 } // end switch
523 }//end cbxgooglemap_manage_columns
524
525 /**
526 * Register the stylesheets for the admin area.
527 *
528 * @since 1.0.0
529 */
530 public function enqueue_styles( $hook ) {
531 $current_page = isset( $_GET['page'] ) ? esc_attr( wp_unslash( $_GET['page'] ) ) : '';
532 $version = $this->version;
533
534 $vendor_url = CBXGOOGLEMAP_ROOT_URL . 'assets/vendors/';
535 $css_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
536 $js_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
537
538 global $post_type, $post;
539
540 $api_key = esc_attr( $this->settings->get_option( 'apikey', 'cbxgooglemap_general', '' ) );
541 $map_source = intval( $this->settings->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
542
543
544 //listing mode
545 if ( ( $hook == 'edit.php' ) && $post_type == 'cbxgooglemap' ) {
546 wp_register_style( 'cbxgooglemap-admin', $css_url . 'cbxgooglemap-admin.css', [], $version );
547 wp_enqueue_style( 'cbxgooglemap-admin' );
548 }
549
550
551 //add new, edit mode
552 if ( ( $hook == 'post.php' || $hook == 'post-new.php' ) && $post_type == 'cbxgooglemap' ) {
553
554 if ( $map_source == 0 ) {
555 wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.9.2/dist/leaflet.css', [], $version, 'all' );
556 wp_register_style( 'leaflet-control-geocoder', '//unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css', [], $version, 'all' );
557 }
558
559 wp_register_style( 'jquery-ui', $vendor_url . 'ui-lightness/jquery-ui.min.css', [], $version );
560 wp_register_style( 'select2', $vendor_url . 'select2/css/select2.min.css', [], $version );
561 wp_register_style( 'cbxgooglemap-admin', $css_url . 'cbxgooglemap-admin.css', [
562 'select2',
563 'wp-color-picker',
564 'jquery-ui'
565 ], $version );
566
567 wp_enqueue_style( 'select2' );
568 wp_enqueue_style( 'wp-color-picker' );
569 wp_enqueue_style( 'jquery-ui' );
570
571 if ( $map_source == 0 ) {
572 wp_enqueue_style( 'leaflet' );
573 wp_enqueue_style( 'leaflet-control-geocoder' );
574 }
575
576 wp_enqueue_style( 'cbxgooglemap-admin' );
577 }
578
579 if ( $current_page == 'cbxgooglemapsettings' ) {
580
581 if ( $map_source == 0 ) {
582 wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.9.2/dist/leaflet.css', [], $version, 'all' );
583 wp_register_style( 'leaflet-control-geocoder', '//unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css', [], $version, 'all' );
584 }
585
586
587 wp_register_style( 'jquery-ui', $vendor_url . 'ui-lightness/jquery-ui.min.css', [], $version );
588 wp_register_style( 'select2', $vendor_url . 'select2/css/select2.min.css', [], $version );
589 wp_register_style( 'cbxgooglemap-setting', $css_url . 'cbxgooglemap-setting.css', [
590 'wp-color-picker',
591 'select2',
592 'jquery-ui'
593 ], $version );
594
595 wp_enqueue_style( 'wp-color-picker' );
596
597 wp_enqueue_style( 'select2' );
598 wp_enqueue_style( 'jquery-ui' );
599 wp_enqueue_style( 'cbxgooglemap-setting' );
600
601
602 //for demo css
603 if ( $map_source == 0 ) {
604 wp_register_style( 'cbxgooglemap-public', $css_url . 'cbxgooglemap-public.css', [ 'leaflet' ], $version, 'all' );
605 wp_enqueue_style( 'leaflet' );
606
607 } else {
608 wp_register_style( 'cbxgooglemap-public', $css_url . 'cbxgooglemap-public.css', [], $version, 'all' );
609 }
610
611 wp_enqueue_style( 'cbxgooglemap-public' );
612 //end for demo css
613
614
615 }//end setting
616
617 //apply branding css to necessary pages
618 if ( ( $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'edit.php' ) && $post_type == 'cbxgooglemap' || $current_page == 'cbxgooglemapsettings' || $current_page == 'cbxgooglemap-help-support' ) {
619 wp_register_style( 'cbxgooglemap-branding', $css_url . 'cbxgooglemap-branding.css', [], $version );
620 wp_enqueue_style( 'cbxgooglemap-branding' );
621 }
622 }//end enqueue_styles
623
624 /**
625 * Register the JavaScript for the admin area.
626 *
627 * @since 1.0.0
628 */
629 public function enqueue_scripts( $hook ) {
630 $current_page = isset( $_GET['page'] ) ? esc_attr( wp_unslash( $_GET['page'] ) ) : '';
631 $ver = $this->version;
632
633 $vendor_url = CBXGOOGLEMAP_ROOT_URL . 'assets/vendors/';
634 $css_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
635 $js_url = CBXGOOGLEMAP_ROOT_URL . 'assets/js/';
636
637 $t = true;
638 $f = false;
639
640
641 global $post_type, $post;
642 $post_id = isset( $post->ID ) ? intval( $post->ID ) : 0;
643
644 $api_key = esc_attr( $this->settings->get_option( 'apikey', 'cbxgooglemap_general', '' ) );
645 $map_source = intval( $this->settings->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
646
647 $default_mapicon = esc_url( $this->settings->get_option( 'mapicon', 'cbxgooglemap_general', '' ) );
648
649 /*if($default_mapicon == ''){
650 $default_mapicon = 'https://mt.googleapis.com/vt/icon/name=icons/spotlight/spotlight-poi.png';
651 }*/
652
653 /*$zoom_level_default = intval( $this->settings->get_option( 'zoom', 'cbxgooglemap_general', '8' ) );
654 if ( $zoom_level_default == 0 ) {
655 $zoom_level_default = 8;
656 }*/
657
658
659 //maps listing mode
660 if ( $hook == 'edit.php' && $post_type == 'cbxgooglemap' ) {
661 wp_register_script( 'cbxgooglemap-listing', $js_url . 'cbxgooglemap-listing.js', [ 'jquery' ], $ver, $t );
662
663 wp_enqueue_script( 'jquery' );
664 wp_enqueue_script( 'cbxgooglemap-listing' );
665 }
666
667 //add new or edit mode
668 if ( ( $hook == 'post.php' || $hook == 'post-new.php' ) && $post_type == 'cbxgooglemap' ) {
669 wp_register_script( 'select2', $vendor_url . 'select2/js/select2.full.min.js', [ 'jquery' ], $ver, $t );
670
671
672 wp_register_script( 'cbxgooglemap-events', $js_url . 'cbxgooglemap-events.js', [], $ver, $t );
673 wp_enqueue_script( 'cbxgooglemap-events' );
674
675 $meta_js_dep = [ 'jquery', 'select2' ];
676
677 if ( $map_source == 1 && $api_key != '' ) {
678 wp_register_script( 'coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . $api_key . '&libraries=places&callback=Function.prototype', [], $ver );
679 //wp_register_script( 'locationpicker-jquery', $js_url. 'locationpicker.jquery.js', [ 'coregooglemapapi', 'jquery' ], $ver );
680
681 $meta_js_dep[] = 'coregooglemapapi';
682 //$meta_js_dep[] = 'locationpicker-jquery';
683
684 $meta_js_dep[] = 'cbxgooglemap-events';
685
686 } elseif ( $map_source == 0 ) {
687 wp_register_script( 'coregooglemapapi', '//unpkg.com/leaflet@1.9.2/dist/leaflet.js', [], $ver );
688 //wp_register_script( 'leaflet-control-geocoder', '//unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js', [ 'coregooglemapapi' ], $ver );
689
690 $meta_js_dep[] = 'coregooglemapapi';
691 //$meta_js_dep[] = 'leaflet-control-geocoder';
692
693 $meta_js_dep[] = 'cbxgooglemap-events';
694 }
695
696 $meta_js_dep[] = 'jquery-ui-core';
697 $meta_js_dep[] = 'wp-color-picker';
698
699 wp_enqueue_media();
700
701 //wp_enqueue_script( 'jquery' );
702 //wp_enqueue_script( 'wp-color-picker' );
703 //wp_enqueue_script( 'select2' );
704
705 foreach ( $meta_js_dep as $dep ) {
706 wp_enqueue_script( $dep );
707 }
708
709
710 //wp_enqueue_style( 'jquery-ui-core' );
711
712 $main_marker = [];
713
714
715
716 wp_register_script( 'cbxgooglemap-meta', $js_url . 'cbxgooglemap-meta.js', $meta_js_dep, $ver, $t );
717
718 $cbxgooglemap_meta_js_vars = apply_filters( 'cbxgooglemap_meta_js_vars',
719 [
720 'please_select' => esc_html__( 'Please Select', 'cbxgooglemap' ),
721 'upload_title' => esc_html__( 'Window Title', 'cbxgooglemap' ),
722 //'copy_success' => esc_html__( 'Shortcode copied to clipboard', 'cbxgooglemap' ),
723 //'copy_fail' => esc_html__( 'Oops, unable to copy', 'cbxgooglemap' ),
724 'search_address' => esc_html__( 'Search Address', 'cbxgooglemap' ),
725 'icon_url_default' => $default_mapicon,
726 'api_key' => $api_key,
727 'map_source' => $map_source
728 ],
729 $post_id );
730
731 wp_localize_script( 'cbxgooglemap-meta', 'cbxgooglemap_meta', $cbxgooglemap_meta_js_vars );
732
733
734 /*wp_enqueue_script( 'coregooglemapapi' );
735 if ( $map_source == 0 ) {
736 wp_enqueue_script( 'leaflet-control-geocoder' );
737 }*/
738
739 /*if ( $map_source == 1 && $api_key != '' ) {
740 wp_enqueue_script( 'locationpicker-jquery' );
741 }*/
742
743
744 wp_enqueue_script( 'cbxgooglemap-meta' );
745 }
746
747 if ( $current_page == 'cbxgooglemapsettings' ) {
748 wp_register_script( 'select2', $vendor_url . 'select2/js/select2.full.min.js', [ 'jquery' ], $ver, $t );
749 wp_register_script( 'cbxgooglemap-setting', $js_url . 'cbxgooglemap-setting.js', [
750 'jquery',
751 'wp-color-picker',
752 'select2'
753 ], $ver, $t );
754
755 $cbxgooglemap_setting_js_vars = apply_filters( 'cbxgooglemap_setting_js_vars',
756 [
757 'please_select' => esc_html__( 'Please Select', 'cbxgooglemap' ),
758 'upload_title' => esc_html__( 'Window Title', 'cbxgooglemap' )
759 ] );
760 wp_localize_script( 'cbxgooglemap-setting', 'cbxgooglemap_setting', $cbxgooglemap_setting_js_vars );
761
762 wp_enqueue_media();
763 wp_enqueue_script( 'jquery' );
764 wp_enqueue_script( 'wp-color-picker' );
765
766 wp_enqueue_script( 'select2' );
767 wp_enqueue_script( 'cbxgooglemap-setting' );
768
769 //for demo js
770 if ( ( $map_source == 1 && ! empty( $api_key ) ) || $map_source == 0 ) {
771 if ( $map_source == 1 ) {
772 wp_register_script( 'coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . esc_attr( $api_key ) . '&libraries=places&callback=Function.prototype', [], $ver );
773 wp_register_script( 'cbxgooglemap-public', $js_url . 'cbxgooglemap-public.js', [
774 'jquery',
775 'coregooglemapapi'
776 ], $ver, $t );
777
778 wp_enqueue_script( 'coregooglemapapi' );
779 } else {
780 wp_register_script( 'coregooglemapapi', '//unpkg.com/leaflet@1.9.2/dist/leaflet.js', [], $ver, true );
781 wp_register_script( 'cbxgooglemap-public', $js_url . 'cbxgooglemap-public.js', [
782 'jquery',
783 'coregooglemapapi',
784 //'leaflet-control-geocoder'
785 ], $ver, $t );
786
787 wp_enqueue_script( 'coregooglemapapi' );
788 //wp_enqueue_script( 'leaflet-control-geocoder' );
789 }
790
791 wp_enqueue_script( 'cbxgooglemap-public' );
792 }//end for demo js
793
794 }//end js for setting page
795
796 }//end enqueue_scripts
797
798 /**
799 * Add settings action link to the plugins page.
800 *
801 * @since 1.0.0
802 */
803 public function plugin_listing_setting_link( $links ) {
804 return array_merge( [
805 'settings' => '<a style="color:#39A96B; font-weight: bold;" target="_blank" href="' . admin_url( 'edit.php?post_type=cbxgooglemap&page=cbxgooglemapsettings' ) . '">' . esc_attr__( 'Settings', 'cbxgooglemap' ) . '</a>'
806 ], $links );
807
808 }//end plugin_listing_setting_link
809
810 /**
811 * Add Pro product link in plugin listing
812 *
813 * @param $links
814 * @param $file
815 *
816 * @return array
817 */
818 public function custom_plugin_row_meta( $links, $file ) {
819 if ( strpos( $file, 'cbxgooglemap.php' ) !== false ) {
820 $new_links = [
821 'free_support' => '<a style="color:#39A96B; font-weight: bold;" href="https://wordpress.org/support/plugin/cbxgooglemap/" target="_blank">' . esc_attr__( 'Free Support', 'cbxgooglemap' ) . '</a>',
822 'reviews' => '<a style="color:#39A96B; font-weight: bold;" href="https://wordpress.org/plugins/cbxgooglemap/#reviews" target="_blank">' . esc_attr__( 'Reviews', 'cbxgooglemap' ) . '</a>',
823 'pro' => '<a style="color:#39A96B; font-weight: bold;" href="https://codeboxr.com/product/cbx-google-map-for-wordpress/" target="_blank">' . esc_attr__( 'Try Pro', 'cbxgooglemap' ) . '</a>',
824 'pro_support' => '<a style="color:#39A96B; font-weight: bold;" href="https://codeboxr.com/contact-us/" target="_blank">' . esc_attr__( 'Pro Support', 'cbxgooglemap' ) . '</a>',
825
826 ];
827
828 $links = array_merge( $links, $new_links );
829 }
830
831 return $links;
832 }//end custom_plugin_row_meta
833
834
835 /**
836 * Init all gutenberg blocks
837 */
838 public function gutenberg_blocks() {
839 if ( ! function_exists( 'register_block_type' ) ) {
840 return;
841 }
842
843 $settings = new CBXGooglemapSettings();
844 $general_settings = get_option( 'cbxgooglemap_general', [] );
845
846 //default field values
847 $zoom_default = $settings->get_field( 'zoom', $general_settings, '8' );
848 if ( $zoom_default == 0 ) {
849 $zoom_default = 8;
850 }
851
852
853 $width_default = $settings->get_field( 'width', $general_settings, '100%' );
854 if ( $width_default == '' || $width_default == 0 ) {
855 $width_default = '100%';
856 }
857
858 $height_default = intval( $settings->get_field( 'height', $general_settings, '300' ) );
859 if ( $height_default == 0 ) {
860 $height_default = 300;
861 }
862
863
864 $scrollwheel_default = intval( $settings->get_field( 'scrollwheel', $general_settings, 1 ) );
865 $showinfo_default = intval( $settings->get_field( 'showinfo', $general_settings, 1 ) );
866 $infow_open_default = intval( $settings->get_field( 'infow_open', $general_settings, 1 ) );
867 $maptype_default = esc_attr( $settings->get_field( 'maptype', $general_settings, 'roadmap' ) );
868
869 $query = get_posts( [
870 'post_type' => 'cbxgooglemap',
871 'orderby' => 'date',
872 'posts_per_page' => - 1,
873 'post_status' => 'publish'
874 ] );
875
876 $googleMap_posts = [];
877
878 $googleMap_posts[] = [
879 'label' => esc_html__( 'Select Map', 'cbxgooglemap' ),
880 'value' => '0'
881 ];
882
883 foreach ( $query as $key => $data ) {
884 $googleMap_posts[] = [
885 'label' => esc_html( $data->post_title ),
886 'value' => intval( $data->ID )
887 ];
888 }
889
890
891 wp_register_script( 'cbxgooglemap-block', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-block.js', [
892 'wp-blocks',
893 'wp-element',
894 'wp-components',
895 'wp-editor',
896 //'jquery',
897 //'codeboxrflexiblecountdown-public'
898 ], filemtime( plugin_dir_path( __FILE__ ) . '../assets/js/cbxgooglemap-block.js' ) );
899
900 wp_register_style( 'cbxgooglemap-block', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-block.css', [], filemtime( plugin_dir_path( __FILE__ ) . '../assets/css/cbxgooglemap-block.css' ) );
901
902 $js_vars = apply_filters( 'cbxgooglemap_block_js_vars',
903 [
904 'block_title' => esc_html__( 'CBX Maps', 'cbxgooglemap' ),
905 'block_category' => 'codeboxr',
906 'block_icon' => 'universal-access-alt',
907 'general_settings' => [
908 'title' => esc_html__( 'CBX Map Settings', 'cbxgooglemap' ),
909 'id' => esc_html__( 'Select Map', 'cbxgooglemap' ),
910 'id_default' => '',
911 'id_options' => $googleMap_posts,
912 'id_note' => esc_html__( 'Choose saved map or create from custom attributes below', 'cbxgooglemap' ),
913 'custom_attribute_note' => esc_html__( 'Custom Map Attributes', 'cbxgooglemap' ),
914 'maptype' => esc_html__( 'Map Type(Google Map Only)', 'cbxgooglemap' ),
915 'maptype_options' => CBXGooglemapHelper::maptype_block_options(),
916 'lat' => esc_html__( 'Latitude', 'cbxgooglemap' ),
917 'lng' => esc_html__( 'Longitude', 'cbxgooglemap' ),
918 'width' => esc_html__( 'Width(Numeric, % allowed)', 'cbxgooglemap' ),
919 'height' => esc_html__( 'Height(only Numeric)', 'cbxgooglemap' ),
920 'zoom' => esc_html__( 'zoom', 'cbxgooglemap' ),
921 'scrollwheel' => esc_html__( 'Mouse Scroll Wheel', 'cbxgooglemap' ),
922 'showinfo' => esc_html__( 'Show Popup', 'cbxgooglemap' ),
923 'infow_open' => esc_html__( 'Popup Auto Display ', 'cbxgooglemap' ),
924 'heading' => esc_html__( 'Popup Heading', 'cbxgooglemap' ),
925 'address' => esc_html__( 'Location Address', 'cbxgooglemap' ),
926 'website' => esc_html__( 'Website url', 'cbxgooglemap' ),
927 'mapicon' => esc_html__( 'Map Icon', 'cbxgooglemap' ),
928 'mapicon_select' => esc_html__( 'Select image', 'cbxgooglemap' ),
929 ],
930 ] );
931
932 wp_localize_script( 'cbxgooglemap-block', 'cbxgooglemap_block', $js_vars );
933
934 register_block_type( 'codeboxr/cbxgooglemap', [
935 'editor_script' => 'cbxgooglemap-block',
936 'editor_style' => 'cbxgooglemap-block',
937 'attributes' => apply_filters( 'cbxgooglemap_block_attributes', [
938 //general
939 'id' => [
940 'type' => 'integer',
941 'default' => '0',
942 ],
943 'maptype' => [
944 'type' => 'string',
945 'default' => $maptype_default
946 ],
947 'lat' => [
948 'type' => 'string',
949 'default' => ''
950 ],
951 'lng' => [
952 'type' => 'string',
953 'default' => ''
954 ],
955 'width' => [
956 'type' => 'string',
957 'default' => $width_default,
958 ],
959
960 'height' => [
961 'type' => 'integer',
962 'default' => $height_default
963 ],
964 'zoom' => [
965 'type' => 'string',
966 'default' => $zoom_default
967 ],
968 'scrollwheel' => [
969 'type' => 'boolean',
970 'default' => ( $scrollwheel_default ) ? true : false
971 ],
972 'showinfo' => [
973 'type' => 'boolean',
974 'default' => ( $showinfo_default ) ? true : false
975 ],
976 'infow_open' => [
977 'type' => 'boolean',
978 'default' => ( $infow_open_default ) ? true : false
979 ],
980 'heading' => [
981 'type' => 'string',
982 'default' => ''
983 ],
984 'address' => [
985 'type' => 'string',
986 'default' => ''
987 ],
988 'website' => [
989 'type' => 'string',
990 'default' => ''
991 ],
992 'mapicon' => [
993 'type' => 'string',
994 /*'source' => 'attribute',
995 'selector' => 'img',
996 'attribute' => 'src',*/
997 'default' => '',
998 ],
999 /*'icon_id' => array(
1000 'type' => 'integer',
1001 //'default' => '',
1002 ),*/
1003 ] ),
1004 'render_callback' => [ $this, 'cbxgooglemap_block_render' ]
1005 ] );
1006
1007 }//end gutenberg_blocks
1008
1009 /**
1010 * Getenberg server side render
1011 *
1012 * @param $settings
1013 *
1014 * @return string
1015 */
1016 public function cbxgooglemap_block_render( $attributes ) {
1017 $settings = new CBXGooglemapSettings();
1018 $general_settings = get_option( 'cbxgooglemap_general', [] );
1019
1020 $api_key = $settings->get_field( 'apikey', $general_settings, '' );
1021 $map_source = intval( $settings->get_field( 'mapsource', $general_settings, 1 ) );
1022
1023 if ( $map_source == 1 && $api_key == '' ) {
1024 echo '<p style="text-align: center;">' . esc_html__( 'Google Map Api Key is invalid!', 'cbxgooglemap' ) . '</p>';
1025 } else {
1026 $id = isset( $attributes['id'] ) ? intval( $attributes['id'] ) : 0;
1027
1028 if ( $id > 0 ) {
1029 //render map from saved map
1030 return '[cbxgooglemap id="' . $id . '"]';
1031 } else {
1032 $attr = [];
1033 if ( isset( $attributes['lat'] ) ) {
1034 $attr['lat'] = sanitize_text_field( $attributes['lat'] );
1035 }
1036 if ( isset( $attributes['lng'] ) ) {
1037 $attr['lng'] = sanitize_text_field( $attributes['lng'] );
1038 }
1039 if ( isset( $attributes['width'] ) ) {
1040 $attr['width'] = sanitize_text_field( $attributes['width'] );
1041 }
1042 if ( isset( $attributes['height'] ) ) {
1043 $attr['height'] = intval( $attributes['height'] );
1044 }
1045 if ( isset( $attributes['zoom'] ) ) {
1046 $attr['zoom'] = sanitize_text_field( $attributes['zoom'] );
1047 }
1048
1049 $attr['scrollwheel'] = isset( $attributes['scrollwheel'] ) ? $attributes['scrollwheel'] : 'true';
1050 $attr['infow_open'] = isset( $attributes['infow_open'] ) ? $attributes['infow_open'] : 'true';
1051 $attr['showinfo'] = isset( $attributes['showinfo'] ) ? $attributes['showinfo'] : 'true';
1052
1053 $attr['scrollwheel'] = ( $attr['scrollwheel'] == 'true' ) ? 1 : 0;
1054 $attr['infow_open'] = ( $attr['infow_open'] == 'true' ) ? 1 : 0;
1055 $attr['showinfo'] = ( $attr['showinfo'] == 'true' ) ? 1 : 0;
1056
1057 if ( isset( $attributes['heading'] ) ) {
1058 $attr['heading'] = sanitize_text_field( $attributes['heading'] );
1059 }
1060 if ( isset( $attributes['address'] ) ) {
1061 $attr['address'] = sanitize_text_field( $attributes['address'] );
1062 }
1063 if ( isset( $attributes['website'] ) ) {
1064 $attr['website'] = sanitize_text_field( $attributes['website'] );
1065 }
1066 if ( isset( $attributes['maptype'] ) ) {
1067 $attr['maptype'] = sanitize_text_field( $attributes['maptype'] );
1068 }
1069 if ( isset( $attributes['mapicon'] ) ) {
1070 $attr['mapicon'] = esc_url( $attributes['mapicon'] );
1071 }
1072
1073 $attr = apply_filters( 'cbxgooglemap_block_shortcode_builder_attr', $attr, $attributes );
1074
1075 $attr_html = '';
1076
1077 foreach ( $attr as $key => $value ) {
1078 $attr_html .= ' ' . $key . '="' . $value . '" ';
1079 }
1080
1081 //return do_shortcode( '[cbxgooglemap ' . $attr_html . ']' );
1082 return '[cbxgooglemap ' . $attr_html . ']';
1083 }
1084 }//end if api keys are ok
1085
1086 }//end codeboxrflexiblecountdown_block_render
1087
1088 /**
1089 * Register New Gutenberg block Category if need
1090 *
1091 * @param $categories
1092 * @param $post
1093 *
1094 * @return mixed
1095 */
1096 public function gutenberg_block_categories( $categories, $post ) {
1097 $found = false;
1098 foreach ( $categories as $category ) {
1099 if ( $category['slug'] == 'codeboxr' ) {
1100 $found = true;
1101 break;
1102 }
1103 }
1104
1105 if ( ! $found ) {
1106 return array_merge(
1107 $categories,
1108 [
1109 [
1110 'slug' => 'codeboxr',
1111 'title' => esc_html__( 'CBX Blocks', 'cbxgooglemap' ),
1112 ],
1113 ]
1114 );
1115 }
1116
1117 return $categories;
1118 }//end gutenberg_block_categories
1119
1120
1121 /**
1122 * Enqueue style for block editor
1123 */
1124 public function enqueue_block_editor_assets() {
1125 }//end enqueue_block_editor_assets
1126 }//end class CBXGoogleMap_Admin