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

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