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

1,055 lines 36.9 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__('Google Map Parameter', $this->plugin_name), array($this, 'parameter_metabox_display'), 'cbxgooglemap', 'normal', 'high'
283 );
284
285
286 add_meta_box(
287 'cbxgooglemap_shortcode', esc_html__('Shortcode', $this->plugin_name), 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 $columns['shortcode'] = esc_html__('Shortcode', 'cbxgooglemap');
414 $columns['lat'] = esc_html__('Latitude', 'cbxgooglemap');
415 $columns['lng'] = esc_html__('Longitude', 'cbxgooglemap');
416 $columns['zoom'] = esc_html__('Zoom', 'cbxgooglemap');
417
418 return $columns;
419 }//end cbxgooglemap_add_new_columns
420
421 /**
422 * Add extra cols information for cbxgooglemap post type
423 *
424 * @param $column_name
425 *
426 *
427 * show data to poll table custom column
428 */
429 public function cbxgooglemap_manage_columns($column_name){
430 global $wpdb, $post;
431 $post_id = $post->ID;
432 $post_type = $post->post_type;
433
434 $combined_field = '_cbxgooglemap_combined'; //field name for non sortable fields
435 $meta_prefix = '_cbxgooglemap'; //field prefix for sortable fields
436
437 $meta_combined = get_post_meta($post_id, $combined_field, true);
438
439 $lat = get_post_meta($post_id, $meta_prefix.'lat', true);
440 $lat = ($lat !== false) ? $lat: '';
441
442 $lng = get_post_meta($post_id, $meta_prefix.'lng', true);
443 $lng = ($lng !== false) ? $lng: '';
444
445 $zoom = (isset($meta_combined[$meta_prefix.'zoom']) && intval($meta_combined[$meta_prefix.'zoom']) > 0 ) ? intval($meta_combined[$meta_prefix.'zoom']): '';
446
447
448 switch ($column_name) {
449 case 'shortcode':
450 echo '<span id="cbxgooglemapshortcode-' . $post_id . '" class="cbxgooglemapshortcode cbxgooglemapshortcode-' . $post_id . '">[cbxgooglemap id="' . $post_id . '"]</span>';
451 echo '<span class="cbxgooglemapshortcodetrigger" data-clipboard-text=\'[cbxgooglemap id="'.$post_id.'"]\' title="' . esc_html__("Copy to clipboard", 'cbxgooglemap') . '">
452 </span>';
453 break;
454 case 'lat':
455 echo $lat;
456 break;
457 case 'lng':
458 echo $lng;
459 break;
460 case 'zoom':
461 echo $zoom;
462 break;
463 default:
464 break;
465 } // end switch
466 }//end cbxgooglemap_manage_columns
467
468 /**
469 * Register the stylesheets for the admin area.
470 *
471 * @since 1.0.0
472 */
473 public function enqueue_styles($hook) {
474
475 $current_page = isset($_GET['page'])? esc_attr($_GET['page']): '';
476 global $post_type, $post;
477
478 $googlemap_api_key = $this->settings_api->get_option('apikey', 'cbxgooglemap_general', '');
479 $mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
480
481 //wp_register_style('chosen', plugin_dir_url( __FILE__ ).'../assets/css/chosen.min.css', array(), $this->version);
482 wp_register_style( 'select2',
483 plugin_dir_url( __FILE__ ) . '../assets/select2/css/select2.min.css',
484 array(),
485 $this->version );
486
487 wp_register_style('jquery-ui.min', plugin_dir_url( __FILE__ ).'../assets/css/ui-lightness/jquery-ui.min.css', array(), $this->version);
488 //wp_register_style('jquery-ui-timepicker-addon.min', plugin_dir_url( __FILE__ ) .'../assets/css/jquery-ui-timepicker-addon.min.css', array(), $this->version);
489
490 if($mapsource == 0){
491 //wp_register_style( 'ol-geocoder', '//unpkg.com/ol-geocoder/dist/ol-geocoder.min.css', array(), $this->version, 'all' );
492 //wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.3.4/dist/leaflet.css', array('ol-geocoder'), $this->version, 'all' );
493 wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.3.4/dist/leaflet.css', array(), $this->version, 'all' );
494 }
495
496 wp_register_style( 'cbxgooglemap-admin', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-admin.css', array(), $this->version );
497 wp_register_style( 'cbxgooglemap-setting', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-setting.css', array(), $this->version );
498
499
500 //listing mode
501 if(($hook == 'edit.php') && $post_type == 'cbxgooglemap'){
502 wp_enqueue_style('cbxgooglemap-admin');
503 }
504
505 //add new, edit mode
506 if(($hook == 'post.php' || $hook == 'post-new.php') && $post_type == 'cbxgooglemap'){
507
508 wp_enqueue_style( 'wp-color-picker' );
509 //wp_enqueue_style('chosen');
510 wp_enqueue_style('select2');
511 wp_enqueue_style('jquery-ui.min');
512 //wp_enqueue_style('jquery-ui-timepicker-addon.min');
513 if($mapsource == 0){
514 wp_enqueue_style( 'ol-geocoder');
515 wp_enqueue_style( 'leaflet');
516 }
517 wp_enqueue_style('cbxgooglemap-admin');
518 }
519
520 if($current_page == 'cbxgooglemapsettings'){
521 wp_enqueue_style( 'wp-color-picker' );
522 //wp_enqueue_style('chosen');
523 wp_enqueue_style('select2');
524 wp_enqueue_style('jquery-ui.min');
525 wp_enqueue_style('cbxgooglemap-setting');
526
527 //for demo
528 $googlemap_api_key = $this->settings_api->get_option( 'apikey', 'cbxgooglemap_general', '' );
529 $mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
530
531 if ( ( $mapsource == 1 && ! empty( $googlemap_api_key ) ) || $mapsource == 0 ) {
532 wp_enqueue_script( 'jquery' );
533
534 if ( $mapsource == 1 ) {
535 wp_register_script( 'coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . $googlemap_api_key . '&libraries=places', array(), $this->version );
536 wp_register_script( 'jqcbxgooglemap', plugin_dir_url( __FILE__ ) . '../assets/js/jqcbxgooglemap.js', array(
537 'jquery',
538 'coregooglemapapi'
539 ), $this->version, true );
540 wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array(
541 'jquery',
542 'jqcbxgooglemap'
543 ), $this->version, true );
544
545 wp_enqueue_script( 'coregooglemapapi' );
546 wp_enqueue_script( 'jqcbxgooglemap' );
547 } else {
548 wp_register_script( 'coregooglemapapi', '//unpkg.com/leaflet@1.3.4/dist/leaflet.js', array(), $this->version );
549 wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array(
550 'jquery',
551 'coregooglemapapi'
552 ), $this->version, true );
553 wp_enqueue_script( 'coregooglemapapi' );
554 }
555
556 wp_enqueue_script( 'cbxgooglemap-public' );
557 }
558 }//end setting
559
560 if ( ( $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'edit.php' ) && $post_type == 'cbxgooglemap' || $current_page =='cbxgooglemapsettings' ) {
561 wp_register_style( 'cbxgooglemap-branding',
562 plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-branding.css',
563 array( ),
564 $this->version );
565 wp_enqueue_style( 'cbxgooglemap-branding' );
566 }
567 }//end enqueue_styles
568
569 /**
570 * Register the JavaScript for the admin area.
571 *
572 * @since 1.0.0
573 */
574 public function enqueue_scripts($hook) {
575
576 $current_page = isset($_GET['page'])? esc_attr($_GET['page']): '';
577 global $post_type, $post;
578
579 $googlemap_api_key = $this->settings_api->get_option('apikey', 'cbxgooglemap_general', '');
580 $mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
581
582 $zoom_level_default = $this->settings_api->get_option('zoom', 'cbxgooglemap_general', '8');
583 if($zoom_level_default == 0) $zoom_level_default = 8;
584
585
586 $meta_js_dep = array('jquery', 'select2');
587
588 if($mapsource == 1 && $googlemap_api_key != ''){
589 wp_register_script('coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . $googlemap_api_key.'&libraries=places', array(), $this->version);
590 wp_register_script('locationpicker-jquery', plugin_dir_url( __FILE__ ).'../assets/js/locationpicker.jquery.js', array('coregooglemapapi', 'jquery'), $this->version);
591
592 $meta_js_dep[] = 'locationpicker-jquery';
593 }
594 else if($mapsource == 0){
595 //wp_register_script('openlayers', '//cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.2.0/build/ol.js', array(), $this->version);
596 //wp_register_script('ol-geocoder', '//unpkg.com/ol-geocoder', array('openlayers'), $this->version);
597 //wp_register_script('coregooglemapapi', '//unpkg.com/leaflet@1.3.4/dist/leaflet.js', array('openlayers','ol-geocoder'), $this->version);
598 wp_register_script('coregooglemapapi', '//unpkg.com/leaflet@1.3.4/dist/leaflet.js', array(), $this->version);
599 }
600
601
602 wp_register_script( 'select2', plugin_dir_url( __FILE__ ) . '../assets/select2/js/select2.min.js', array('jquery'),$this->version, true );
603 //wp_register_script('chosen.jquery', plugin_dir_url( __FILE__ ).'../assets/js/chosen.jquery.min.js', array('jquery'), $this->version);
604
605 //wp_register_script('jquery-ui-timepicker-addon', plugin_dir_url( __FILE__ ).'../assets/js/jquery-ui-timepicker-addon.js', array('jquery', 'jquery-ui-datepicker'), $this->version);
606 //wp_register_script('clipboard.min', plugin_dir_url( __FILE__ ).'../assets/js/clipboard.min.js', array('jquery'), $this->version);
607
608 //listing mode
609 if(($hook == 'edit.php') && $post_type == 'cbxgooglemap'){
610
611 wp_register_script( 'cbxgooglemap-admin', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-admin.js', array( 'jquery'), $this->version, true );
612
613 wp_enqueue_script( 'jquery' );
614 //wp_enqueue_script('clipboard.min');
615 wp_enqueue_script('cbxgooglemap-admin');
616 }
617
618 //add new or edit mode
619 if(($hook == 'post.php' || $hook == 'post-new.php') && $post_type == 'cbxgooglemap'){
620
621 wp_enqueue_media();
622 wp_enqueue_script('jquery' );
623 wp_enqueue_script('wp-color-picker' );
624 //wp_enqueue_script('chosen.jquery');
625 wp_enqueue_script('select2');
626 wp_enqueue_style('jquery-ui-core');
627
628
629 /*$combined_field = '_'.$post_type.'_combined'; //field name for non sortable fields
630 $meta_prefix = '_'.$post_type; //field prefix for sortable fields
631
632 $meta_combined = get_post_meta( $post->ID, $combined_field, true ); //meta fields not sortable
633 if(isset($meta_combined[$meta_prefix.'zoom']) && $meta_combined[$meta_prefix.'zoom'] != ''){
634 $zoom_level_default = intval($meta_combined[$meta_prefix.'zoom']);
635 if($zoom_level_default == 0) $zoom_level_default = 8;
636 }*/
637
638 wp_register_script( 'cbxgooglemap-meta', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-meta.js', $meta_js_dep, $this->version, true );
639 $cbxgooglemap_meta_js_vars = apply_filters( 'cbxgooglemap_meta_js_vars',
640 array(
641 'please_select' => esc_html__( 'Please Select', 'cbxgooglemap' ),
642 'upload_title' => esc_html__( 'Window Title', 'cbxgooglemap' ),
643 'copy_success' => esc_html__( 'Shortcode copied to clipboard', 'cbxgooglemap' ),
644 'copy_fail' => esc_html__( 'Oops, unable to copy', 'cbxgooglemap' )
645 ) );
646
647 wp_localize_script( 'cbxgooglemap-meta', 'cbxgooglemap_meta', $cbxgooglemap_meta_js_vars );
648
649 /*$translation_array = array(
650 'zoom' => $zoom_level_default
651 );
652
653 wp_localize_script( 'cbxgooglemap-meta', 'cbxgooglemapmeta', $translation_array );*/
654
655 if($mapsource == 0){
656 //wp_enqueue_script('openlayers');
657 //wp_enqueue_script('ol-geocoder');
658 }
659 //wp_enqueue_script('clipboard.min');
660 wp_enqueue_script('coregooglemapapi');
661 if($mapsource == 1 && $googlemap_api_key != ''){
662 wp_enqueue_script('locationpicker-jquery');
663 }
664 //wp_enqueue_script('jquery-ui-timepicker-addon');
665 wp_enqueue_script('cbxgooglemap-meta');
666 }
667
668 if($current_page == 'cbxgooglemapsettings'){
669
670 wp_register_script( 'cbxgooglemap-setting', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-setting.js', array('jquery', 'wp-color-picker', 'select2'), $this->version, true );
671
672 $cbxgooglemap_setting_js_vars = apply_filters( 'cbxgooglemap_setting_js_vars',
673 array(
674 'please_select' => esc_html__( 'Please Select', 'cbxgooglemap' ),
675 'upload_title' => esc_html__( 'Window Title', 'cbxgooglemap' )
676 ) );
677 wp_localize_script( 'cbxgooglemap-setting', 'cbxgooglemap_setting', $cbxgooglemap_setting_js_vars );
678
679 wp_enqueue_media();
680 wp_enqueue_script( 'jquery' );
681 wp_enqueue_script( 'wp-color-picker' );
682 //wp_enqueue_script( 'chosen.jquery');
683 wp_enqueue_script( 'select2');
684 wp_enqueue_script( 'cbxgooglemap-setting');
685
686
687 //for demo
688 $mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
689 if ( $mapsource == 0 ) {
690 wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.3.4/dist/leaflet.css', array(), $this->version, 'all' );
691 wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array( 'leaflet' ), $this->version, 'all' );
692 wp_enqueue_style( 'leaflet' );
693 } else {
694 wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array(), $this->version, 'all' );
695 }
696
697 wp_enqueue_style( 'cbxgooglemap-public' );
698
699
700
701 }
702 }//end enqueue_scripts
703
704 /**
705 * Add settings action link to the plugins page.
706 *
707 * @since 1.0.0
708 */
709 public function plugin_listing_setting_link( $links ) {
710 return array_merge( array(
711 '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>'
712 ), $links );
713
714 }//end plugin_listing_setting_link
715
716 /**
717 * Add Pro product link in plugin listing
718 *
719 * @param $links
720 * @param $file
721 *
722 * @return array
723 */
724 public function custom_plugin_row_meta( $links, $file ) {
725 if ( strpos( $file, 'cbxgooglemap.php' ) !== false ) {
726 $new_links = array(
727 '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>',
728 //'doc' => '<a href="https://codeboxr.com/product/cbx-google-map-for-wordpress/" target="_blank">' . esc_html__( 'Documentation', 'cbxgooglemap' ) . '</a>'
729 );
730
731 $links = array_merge( $links, $new_links );
732 }
733
734 return $links;
735 }//end custom_plugin_row_meta
736
737
738
739 /**
740 * Init all gutenberg blocks
741 */
742 public function gutenberg_blocks() {
743 $settings = new CBXGooglemapSettings();
744 $general_settings = get_option( 'cbxgooglemap_general', array() );
745
746 //default field values
747 $zoom_default = $settings->get_field( 'zoom', $general_settings, '8' );
748 if ( $zoom_default == 0 ) {
749 $zoom_default = 8;
750 }
751
752
753 $width_default = $settings->get_field( 'width', $general_settings, '100%' );
754 if ( $width_default == '' || $width_default == 0 ) {
755 $width_default = '100%';
756 }
757
758 $height_default = intval( $settings->get_field( 'height', $general_settings, '300' ) );
759 if ( $height_default == 0 ) {
760 $height_default = 300;
761 }
762
763
764 $scrollwheel_default = intval( $settings->get_field( 'scrollwheel', $general_settings, 1 ) );
765 $showinfo_default = intval( $settings->get_field( 'showinfo', $general_settings, 1 ) );
766 $infow_open_default = intval( $settings->get_field( 'infow_open', $general_settings, 1 ) );
767 $maptype_default = $settings->get_field( 'maptype', $general_settings, 'roadmap' );
768
769 $query = get_posts( array(
770 'post_type' => 'cbxgooglemap',
771 'orderby' => 'date',
772 'posts_per_page' => - 1,
773 'post_status' => 'publish'
774 ) );
775
776 $googleMap_posts = array();
777
778 $googleMap_posts[] = array(
779 'label' => esc_html__('Select Map', 'cbxgooglemap'),
780 'value' => '0'
781 );
782
783 foreach ( $query as $key => $data ) {
784 $googleMap_posts[] = array(
785 'label' => esc_html($data->post_title),
786 'value' => intval($data->ID)
787 );
788 }
789
790
791
792 wp_register_script( 'cbxgooglemap-block', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-block.js', array(
793 'wp-blocks',
794 'wp-element',
795 'wp-components',
796 'wp-editor',
797 //'jquery',
798 //'codeboxrflexiblecountdown-public'
799 ), filemtime(plugin_dir_path( __FILE__ ) . '../assets/js/cbxgooglemap-block.js') );
800
801 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') );
802
803 $js_vars = apply_filters( 'cbxgooglemap_block_js_vars',
804 array(
805 'block_title' => esc_html__( 'CBX Maps', 'cbxgooglemap' ),
806 'block_category' => 'codeboxr',
807 'block_icon' => 'universal-access-alt',
808 'general_settings' => array(
809 'title' => esc_html__( 'CBX Map Settings', 'cbxgooglemap' ),
810 'id' => esc_html__( 'Select Map', 'cbxgooglemap' ),
811 'id_default' => '',
812 'id_options' => $googleMap_posts,
813 'id_note' => esc_html__( 'Choose saved map or create from custom attributes below', 'cbxgooglemap' ),
814 'custom_attribute_note' => esc_html__( 'Custom Map Attributes', 'cbxgooglemap' ),
815 'maptype' => esc_html__( 'Map Type(Google Map Only)', 'cbxgooglemap' ),
816 'maptype_options' => CBXGooglemapHelper::maptype_block_options(),
817 'lat' => esc_html__( 'Latitude', 'cbxgooglemap' ),
818 'lng' => esc_html__( 'Longitude', 'cbxgooglemap' ),
819 'width' => esc_html__( 'Width(Numeric, % allowed)', 'cbxgooglemap' ),
820 'height' => esc_html__( 'Height(only Numeric)', 'cbxgooglemap' ),
821 'zoom' => esc_html__( 'zoom', 'cbxgooglemap' ),
822 'scrollwheel' => esc_html__( 'Mouse Scroll Wheel', 'cbxgooglemap' ),
823 'showinfo' => esc_html__( 'Show Popup', 'cbxgooglemap' ),
824 'infow_open' => esc_html__( 'Popup Auto Display ', 'cbxgooglemap' ),
825 'heading' => esc_html__( 'Popup Heading', 'cbxgooglemap' ),
826 'address' => esc_html__( 'Location Address', 'cbxgooglemap' ),
827 'website' => esc_html__( 'Website url', 'cbxgooglemap' ),
828 'mapicon' => esc_html__( 'Map Icon', 'cbxgooglemap' ),
829 'mapicon_select' => esc_html__( 'Select image', 'cbxgooglemap' ),
830 ),
831 ) );
832
833 wp_localize_script( 'cbxgooglemap-block', 'cbxgooglemap_block', $js_vars );
834
835 register_block_type( 'codeboxr/cbxgooglemap', array(
836 'editor_script' => 'cbxgooglemap-block',
837 'editor_style' => 'cbxgooglemap-block',
838 'attributes' => apply_filters('cbxgooglemap_block_attributes', array(
839 //general
840 'id' => array(
841 'type' => 'integer',
842 'default' => '0',
843 ),
844 'maptype' => array(
845 'type' => 'string',
846 'default' => $maptype_default
847 ),
848 'lat' => array(
849 'type' => 'string',
850 'default' => ''
851 ),
852 'lng' => array(
853 'type' => 'string',
854 'default' => ''
855 ),
856 'width' => array(
857 'type' => 'string',
858 'default' => $width_default,
859 ),
860
861 'height' => array(
862 'type' => 'integer',
863 'default' => $height_default
864 ),
865 'zoom' => array(
866 'type' => 'string',
867 'default' => $zoom_default
868 ),
869 'scrollwheel' => array(
870 'type' => 'boolean',
871 'default' => ($scrollwheel_default)? true: false
872 ),
873 'showinfo' => array(
874 'type' => 'boolean',
875 'default' => ($showinfo_default)? true: false
876 ),
877 'infow_open' => array(
878 'type' => 'boolean',
879 'default' => ($infow_open_default)? true: false
880 ),
881 'heading' => array(
882 'type' => 'string',
883 'default' => ''
884 ),
885 'address' => array(
886 'type' => 'string',
887 'default' => ''
888 ),
889 'website' => array(
890 'type' => 'string',
891 'default' => ''
892 ),
893 'mapicon' => array(
894 'type' => 'string',
895 /*'source' => 'attribute',
896 'selector' => 'img',
897 'attribute' => 'src',*/
898 'default' => '',
899 ),
900 /*'icon_id' => array(
901 'type' => 'integer',
902 //'default' => '',
903 ),*/
904 )),
905 'render_callback' => array($this, 'cbxgooglemap_block_render')
906 ) );
907
908 }//end gutenberg_blocks
909
910 /**
911 * Getenberg server side render
912 *
913 * @param $settings
914 *
915 * @return string
916 */
917 public function cbxgooglemap_block_render($attributes){
918 $settings_api = new CBXGooglemapSettings();
919 $general_settings = get_option( 'cbxgooglemap_general', array() );
920
921 $googlemap_api_key = $settings_api->get_field( 'apikey', $general_settings, '' );
922 $mapsource = intval( $settings_api->get_field( 'mapsource', $general_settings, 1 ) );
923
924 if ( $mapsource == 1 && $googlemap_api_key == '' ) {
925 echo '<p style="text-align: center;">' . esc_html__( 'Google Map Api Key is invalid!', 'cbxgooglemap' ) . '</p>';
926 }
927 else {
928 $id = isset($attributes['id'])? intval( $attributes['id'] ): 0;
929
930 if ( $id > 0 ) {
931 //render map from saved map
932 return '[cbxgooglemap id="' . $id . '"]' ;
933 }
934 else {
935 $attr = array();
936 if(isset($attributes['lat'])) $attr['lat'] = sanitize_text_field( $attributes['lat'] );
937 if(isset($attributes['lng'])) $attr['lng'] = sanitize_text_field( $attributes['lng'] );
938 if(isset($attributes['width'])) $attr['width'] = sanitize_text_field( $attributes['width'] );
939 if(isset($attributes['height'])) $attr['height'] = intval( $attributes['height'] );
940 if(isset($attributes['zoom'])) $attr['zoom'] = sanitize_text_field( $attributes['zoom'] );
941
942 $attr['scrollwheel'] = isset($attributes['scrollwheel'])? $attributes['scrollwheel'] : 'true';
943 $attr['infow_open'] = isset($attributes['infow_open'])? $attributes['infow_open']: 'true';
944 $attr['showinfo'] = isset($attributes['showinfo'])? $attributes['showinfo']: 'true';
945
946 $attr['scrollwheel'] = ($attr['scrollwheel'] == 'true')? 1: 0;
947 $attr['infow_open'] = ($attr['infow_open'] == 'true')? 1: 0;
948 $attr['showinfo'] = ($attr['showinfo'] == 'true')? 1: 0;
949
950 if(isset($attributes['heading'])) $attr['heading'] = sanitize_text_field( $attributes['heading'] );
951 if(isset($attributes['address'])) $attr['address'] = sanitize_text_field( $attributes['address'] );
952 if(isset($attributes['website'])) $attr['website'] = sanitize_text_field( $attributes['website'] );
953 if(isset($attributes['maptype'])) $attr['maptype'] = sanitize_text_field( $attributes['maptype'] );
954 if(isset($attributes['mapicon'])) $attr['mapicon'] = sanitize_text_field( $attributes['mapicon'] );
955
956 $attr = apply_filters( 'cbxgooglemap_block_shortcode_builder_attr', $attr, $attributes );
957
958 $attr_html = '';
959
960 foreach ( $attr as $key => $value ) {
961 $attr_html .= ' ' . $key . '="' . $value . '" ';
962 }
963
964 //return do_shortcode( '[cbxgooglemap ' . $attr_html . ']' );
965 return '[cbxgooglemap ' . $attr_html . ']' ;
966 }
967 }//end if api keys are ok
968
969 }//end codeboxrflexiblecountdown_block_render
970
971 /**
972 * Register New Gutenberg block Category if need
973 *
974 * @param $categories
975 * @param $post
976 *
977 * @return mixed
978 */
979 public function gutenberg_block_categories( $categories, $post ) {
980 $found = false;
981 foreach ($categories as $category){
982 if($category['slug'] == 'codeboxr'){
983 $found = true;
984 break;
985 }
986 }
987
988 if(!$found){
989 return array_merge(
990 $categories,
991 array(
992 array(
993 'slug' => 'codeboxr',
994 'title' => esc_html__('CBX Blocks', 'cbxgooglemap'),
995 ),
996 )
997 );
998 }
999
1000 return $categories;
1001 }//end gutenberg_block_categories
1002
1003
1004 /**
1005 * Enqueue style for block editor
1006 */
1007 public function enqueue_block_editor_assets(){
1008 /*$mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
1009 if ( $mapsource == 0 ) {
1010 wp_register_style( 'leaflet', '//unpkg.com/leaflet@1.3.4/dist/leaflet.css', array(), $this->version, 'all' );
1011 wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array( 'leaflet' ), $this->version, 'all' );
1012 wp_enqueue_style( 'leaflet' );
1013 } else {
1014 wp_register_style( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/css/cbxgooglemap-public.css', array(), $this->version, 'all' );
1015 }
1016
1017 wp_enqueue_style( 'cbxgooglemap-public' );
1018
1019
1020 $googlemap_api_key = $this->settings_api->get_option( 'apikey', 'cbxgooglemap_general', '' );
1021 $mapsource = intval( $this->settings_api->get_option( 'mapsource', 'cbxgooglemap_general', 1 ) );
1022
1023 if ( ( $mapsource == 1 && ! empty( $googlemap_api_key ) ) || $mapsource == 0 ) {
1024 wp_enqueue_script( 'jquery' );
1025
1026 if ( $mapsource == 1 ) {
1027 wp_register_script( 'coregooglemapapi', '//maps.googleapis.com/maps/api/js?key=' . $googlemap_api_key . '&libraries=places', array(), $this->version );
1028 wp_register_script( 'jqcbxgooglemap', plugin_dir_url( __FILE__ ) . '../assets/js/jqcbxgooglemap.js', array(
1029 'jquery',
1030 'coregooglemapapi'
1031 ), $this->version, true );
1032 wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array(
1033 'jquery',
1034 'jqcbxgooglemap',
1035 'cbxgooglemap-block'
1036 ), $this->version, true );
1037
1038 wp_enqueue_script( 'coregooglemapapi' );
1039 wp_enqueue_script( 'jqcbxgooglemap' );
1040 } else {
1041 wp_register_script( 'coregooglemapapi', '//unpkg.com/leaflet@1.3.4/dist/leaflet.js', array(), $this->version );
1042 wp_register_script( 'cbxgooglemap-public', plugin_dir_url( __FILE__ ) . '../assets/js/cbxgooglemap-public.js', array(
1043 'jquery',
1044 'coregooglemapapi'
1045 ), $this->version, true );
1046 wp_enqueue_script( 'coregooglemapapi' );
1047 }
1048
1049 wp_enqueue_script( 'cbxgooglemap-public' );
1050 }*/
1051
1052 }//end enqueue_block_editor_assets
1053
1054 }//end class CBXGoogleMap_Admin
1055