PluginProbe
CBX Map for Google Map & OpenStreetMap / 1.1.9
CBX Map for Google Map & OpenStreetMap v1.1.9
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.9, at admin/class-cbxgooglemap-admin.php

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