PluginProbe
CBX Map for Google Map & OpenStreetMap / 2.0.3
CBX Map for Google Map & OpenStreetMap v2.0.3
trunk 1.1.10 1.1.11 1.1.12 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5
cbxgooglemap / includes / CBXGoogleMapAdmin.php

CBXGoogleMapAdmin.php in CBX Map for Google Map & OpenStreetMap 2.0.3, at includes/CBXGoogleMapAdmin.php

1,493 lines 57.6 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
24 namespace Cbx\Googlemap;
25
26 // If this file is called directly, abort.
27 if ( ! defined('WPINC')) {
28 die;
29 }
30
31 use Cbx\Googlemap\Helpers\CBXGooglemapHelper;
32 use Cbx\Googlemap\Helpers\CBXGooglemapMetaHelper;
33
34 final class CBXGoogleMapAdmin
35 {
36
37 /**
38 * The ID of this plugin.
39 *
40 * @since 1.0.0
41 * @access private
42 * @var string $plugin_name The ID of this plugin.
43 */
44 private $plugin_name;
45
46 /**
47 * The version of this plugin.
48 *
49 * @since 1.0.0
50 * @access private
51 * @var string $version The current version of this plugin.
52 */
53 private $version;
54
55 /**
56 * The ID of this plugin.
57 *
58 * @since 1.1.12
59 * @access private
60 * @var string $settings The ID of this plugin.
61 */
62 public $settings;
63
64 /**
65 * Initialize the class and set its properties.
66 *
67 *
68 * @since 1.0.0
69 */
70 public function __construct()
71 {
72 $this->plugin_name = CBXGOOGLEMAP_PLUGIN_NAME;
73 $this->version = CBXGOOGLEMAP_PLUGIN_VERSION;
74
75 //get instance of setting api
76 $this->settings = new CBXGooglemapSettings();
77 }//end of construtor
78
79 /**
80 * Initialize setting
81 */
82 public function setting_init()
83 {
84 //set the settings
85 $this->settings->set_sections($this->get_settings_sections());
86 $this->settings->set_fields($this->get_settings_fields());
87 //initialize settings
88 $this->settings->admin_init();
89 }//end setting_init
90
91 /**
92 * Global Setting Sections
93 *
94 *
95 * @return array
96 */
97 public function get_settings_sections()
98 {
99 return apply_filters(
100 'cbxgooglemap_setting_sections', [
101 [
102 'id' => 'cbxgooglemap_general',
103 'title' => esc_html__('Default Config', 'cbxgooglemap')
104 ],
105 [
106 'id' => 'cbxgooglemap_demo',
107 'title' => esc_html__('Demo & Shortcodes', 'cbxgooglemap')
108 ],
109 [
110 'id' => 'cbxgooglemap_tools',
111 'title' => esc_html__('Tools', 'cbxgooglemap'),
112 ]
113 ]
114 );
115 }//end get_settings_sections
116
117 /**
118 * Returns all the settings fields
119 *
120 * @return array settings fields
121 */
122 public function get_settings_fields()
123 {
124 $general_settings = get_option('cbxgooglemap_general', []);
125
126 $tools_delete_table_html = '<div id="setting_resetinfo">'.esc_html__('Loading ...', 'cbxgooglemap').'</div>';
127
128 $settings_builtin_fields = [
129 'cbxgooglemap_general' => [
130 'general_heading' => [
131 'name' => 'general_heading',
132 'label' => esc_html__('Default Config', 'cbxgooglemap'),
133 'type' => 'heading',
134 'default' => '',
135 ],
136 'mapsource' => [
137 'name' => 'mapsource',
138 'label' => esc_html__('Map Source', 'cbxgooglemap'),
139 'type' => 'radio',
140 'default' => 1,
141 'options' => [
142 0 => esc_html__('Openstreet Map(leafletjs)', 'cbxgooglemap'),
143 1 => esc_html__('Google Map', 'cbxgooglemap')
144 ],
145 'inline' => 1,
146 'sanitize_callback' => 'absint'
147 ],
148 'apikey' => [
149 'name' => 'apikey',
150 'label' => esc_html__('Api Key', 'cbxgooglemap'),
151 'desc' => esc_html__('Google map api key', 'cbxgooglemap'),
152 'type' => 'text'
153
154 ],
155 'maptype' => [
156 'name' => 'maptype',
157 'label' => esc_html__('Map Type', 'cbxgooglemap'),
158 'type' => 'select',
159 'default' => 'roadmap',
160 'options' => [
161 'roadmap' => esc_html__('Road Map', 'cbxgooglemap'),
162 'satellite' => esc_html__('Satellite Map', 'cbxgooglemap'),
163 'hybrid' => esc_html__('Hybrid Map', 'cbxgooglemap'),
164 'terrain' => esc_html__('Terrain Map', 'cbxgooglemap'),
165 ],
166 'desc' => esc_html__('Google Map only', 'cbxgooglemap'),
167 'sanitize_callback' => 'sanitize_text_field'
168 ],
169 'zoom' => [
170 'name' => 'zoom',
171 'label' => esc_html__('Zoom Level', 'cbxgooglemap'),
172 'desc' => esc_html__('Default Zoom Level', 'cbxgooglemap'),
173 'type' => 'number',
174 'step' => 1,
175 'default' => 8,
176 'sanitize_callback' => 'absint'
177
178 ],
179 'width' => [
180 'name' => 'with',
181 'label' => esc_html__('Width', 'cbxgooglemap'),
182 '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'),
183 'type' => 'text',
184 'default' => '100%',
185
186 ],
187 'height' => [
188 'name' => 'height',
189 'label' => esc_html__('Height', 'cbxgooglemap'),
190 'desc' => esc_html__('Default height 300 as px, put any numeric value.', 'cbxgooglemap'),
191 'type' => 'number',
192 'default' => '300',
193 'sanitize_callback' => 'floatval'
194 ],
195 'scrollwheel' => [
196 'name' => 'scrollwheel',
197 'label' => esc_html__('Mouse Scroll Wheel', 'cbxgooglemap'),
198 'desc' => esc_html__('Enable/disable mouse scroll whell', 'cbxgooglemap'),
199 'type' => 'radio',
200 'default' => 1,
201 'options' => [
202 '0' => esc_html__('Disable', 'cbxgooglemap'),
203 '1' => esc_html__('Enable', 'cbxgooglemap')
204 ],
205 'inline' => 1,
206 'sanitize_callback' => 'absint'
207 ],
208 'showinfo' => [
209 'name' => 'showinfo',
210 'label' => esc_html__('Show Info window', 'cbxgooglemap'),
211 'desc' => esc_html__('Show information on click of marker', 'cbxgooglemap'),
212 'type' => 'radio',
213 'default' => 1,
214 'options' => [
215 '0' => esc_html__('Disable', 'cbxgooglemap'),
216 '1' => esc_html__('Enable', 'cbxgooglemap')
217 ],
218 'inline' => 1,
219 'sanitize_callback' => 'absint'
220 ],
221 'infow_open' => [
222 'name' => 'infow_open',
223 'label' => esc_html__('Info/Popup Window', 'cbxgooglemap'),
224 'type' => 'radio',
225 'default' => 1,
226 'options' => [
227 '0' => esc_html__('On Click', 'cbxgooglemap'),
228 '1' => esc_html__('Open(Default)', 'cbxgooglemap'),
229 ],
230 'inline' => 1,
231 'sanitize_callback' => 'absint'
232 ],
233 'mapicon' => [
234 'name' => 'mapicon',
235 'label' => esc_html__('Map Icon', 'cbxgooglemap'),
236 'type' => 'file',
237 'default' => ''
238 ],
239 'hide_leaflet' => [
240 'name' => 'hide_leaflet',
241 'label' => esc_html__('Hide Leaflet Branding', 'cbxgooglemap'),
242 'type' => 'radio',
243 'default' => 0,
244 'options' => [
245 0 => esc_html__('Show/Default', 'cbxgooglemap'),
246 1 => esc_html__('Hide', 'cbxgooglemap'),
247 ],
248 'inline' => 1,
249 'sanitize_callback' => 'absint'
250 ],
251
252 ],
253 'cbxgooglemap_demo' => [
254 'demo_heading' => [
255 'name' => 'demo_heading',
256 'label' => esc_html__('Demo & Shortcodes', 'cbxgooglemap'),
257 'type' => 'heading',
258 'default' => '',
259 ],
260 'shortcode_demo' => [
261 'name' => 'shortcode_demo',
262 'label' => esc_html__('Shortcode & Demo', 'cbxgooglemap'),
263 'desc' => esc_html__('Shortcode and demo based on default setting, please save once to check change.', 'cbxgooglemap'),
264 'type' => 'shortcode',
265 'class' => 'cbcurrencyconverter_demo_copy',
266 'default' => CBXGooglemapHelper::shortcode_builder($general_settings),
267 'sanitize_callback' => 'sanitize_text_field'
268 ]
269 ],
270 'cbxgooglemap_tools' => [
271 'tools_heading' => [
272 'name' => 'tools_heading',
273 'label' => esc_html__('Tools Settings', 'cbxgooglemap'),
274 'type' => 'heading',
275 'default' => '',
276 ],
277 'delete_global_config' => [
278 'name' => 'delete_global_config',
279 'label' => esc_html__('On Uninstall delete plugin data', 'cbxgooglemap'),
280 'desc' => '<p>'.esc_html__('Delete Global Config data(options/plugin settings), custom table(s), files/folders, all map custom post type created by this plugin on uninstall. Please note that this process can not be undone and it is recommended to keep full database and files backup before doing this.', 'cbxgooglemap').'</p>',
281 'type' => 'radio',
282 'options' => [
283 'yes' => esc_html__('Yes', 'cbxgooglemap'),
284 'no' => esc_html__('No', 'cbxgooglemap'),
285 ],
286 'default' => 'no',
287 ],
288 'reset_data' => [
289 'name' => 'reset_data',
290 'label' => esc_html__('Reset all section', 'cbxgooglemap'),
291 'desc' => $tools_delete_table_html.'<p>'.esc_html__('This will reset all option/section created by this plugin.',
292 'cbxgooglemap').'<a data-busy="0" class="button secondary ml-20" id="reset_data_trigger" href="#">'.esc_html__('Reset Sections',
293 'cbxgooglemap').'</a></p>',
294 'type' => 'html',
295 'default' => 'off'
296 ],
297 ],
298 ];
299
300
301 $settings_fields = []; //final setting array that will be passed to different filters
302 $sections = $this->get_settings_sections();
303
304 foreach ($sections as $section) {
305 if ( ! isset($settings_builtin_fields[$section['id']])) {
306 $settings_builtin_fields[$section['id']] = [];
307 }
308 }
309
310
311 foreach ($sections as $section) {
312 $settings_fields[$section['id']] = apply_filters('cbxgooglemap_global_'.$section['id'].'_fields', $settings_builtin_fields[$section['id']]);
313 }
314
315 return apply_filters('cbxgooglemap_global_fields', $settings_fields);
316 }//end get_settings_fields
317
318 /**
319 * Register Custom Post Type cbxgooglemap
320 *
321 * @since 3.7.0
322 */
323 public function create_post_type()
324 {
325 CBXGooglemapHelper::create_googlemap_post_type();
326
327 // Check the option we set on activation.
328 if (get_option('cbxgooglemap_flush_rewrite_rules') === 'true') {
329 flush_rewrite_rules();
330 delete_option('cbxgooglemap_flush_rewrite_rules');
331 }
332 }//end create_post_type
333
334
335 /**
336 * Show menu page
337 */
338 public function menu_pages()
339 {
340 //setting page
341 $setting_page_hook = add_submenu_page('edit.php?post_type=cbxgooglemap', esc_html__('CBX Map: Settings', 'cbxgooglemap'), esc_html__('Settings', 'cbxgooglemap'), 'manage_options', 'cbxgooglemap_settings', [
342 $this,
343 'menu_page_settings'
344 ]);
345
346 $doc_page_hook = add_submenu_page('edit.php?post_type=cbxgooglemap', esc_html__('CBX Map Helps & Updates', 'cbxgooglemap'), esc_html__('Helps & Updates', 'cbxgooglemap'), 'manage_options', 'cbxgooglemap_support', [
347 $this,
348 'menu_page_docs'
349 ]);
350 }//end menu_pages
351
352 /**
353 * Show cbxeventz Setting page
354 */
355 public function menu_page_settings()
356 {
357 //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
358 echo cbxgooglemap_get_template_html('admin/settings.php',
359 [
360 'ref' => $this,
361 'settings' => $this->settings
362 ]
363 );
364 }//end menu_page_settings
365
366 /**
367 * Render the help & support page for this plugin.
368 *
369 * @since 1.0.0
370 */
371 public function menu_page_docs()
372 {
373 echo cbxgooglemap_get_template_html('admin/support.php');//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
374 }//end method menu_page_docs
375
376 /**
377 * Add metabox for custom post type cbxfeedbackform && cbxfeedbackbtn
378 *
379 * @since 1.0.0
380 */
381 public function add_meta_boxes()
382 {
383 add_meta_box('cbxgooglemap_metabox', esc_html__('Map Parameters', 'cbxgooglemap'), [
384 $this,
385 'parameter_metabox_display'
386 ], 'cbxgooglemap', 'normal', 'high');
387
388
389 add_meta_box('cbxgooglemap_shortcode', esc_html__('Shortcode', 'cbxgooglemap'), [
390 $this,
391 'shortcode_metabox_display'
392 ], 'cbxgooglemap', 'side', 'low');
393 }//end add_meta_boxes
394
395 /**
396 * Show Shortcode display metabox
397 *
398 * @param $post
399 */
400 public function shortcode_metabox_display($post)
401 {
402 if (isset($post->ID) && $post->ID > 0) {
403 $post_id = $post->ID;
404 $post_type = $post->post_type;
405
406 echo cbxgooglemap_get_template_html('admin/metabox_shortcode.php', [//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
407 'post_id' => $post_id,
408 'post_type' => $post_type
409 ]);
410 }
411 }//end shortcode_metabox_display
412
413 /**
414 * Render metabox
415 *
416 * @param $post
417 *
418 * since v1.0.0
419 */
420 public function parameter_metabox_display($post)
421 {
422 global $post;
423 $post_type = $post->post_type;
424
425
426 $meta_fields = CBXGooglemapMetaHelper::cbxgooglemap_meta_fields();
427
428 $combined_field = '_'.$post_type.'_combined'; //field name for non sortable fields
429 $meta_prefix = '_'.$post_type; //field prefix for sortable fields
430
431
432 CBXGooglemapMetaHelper::render_meta_fields($post, $meta_fields, $combined_field, $meta_prefix, true); //
433 }//end parameter_metabox_display
434
435 /**
436 * Save meta box for cbxeventz
437 *
438 * @param $post_id
439 */
440 public function metabox_save($post_id, $post, $update)
441 {
442
443 $post_type = $post->post_type;
444
445 // Check if our nonce is set.
446 if ( ! isset($_POST['cbxmetahelper_'.$post_type.'_meta_box_nonce'])) {
447 return $post_id;
448 }
449
450 // Verify that the nonce is valid.
451 if ( ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['cbxmetahelper_'.$post_type.'_meta_box_nonce'])), 'cbxmetahelper_'.$post_type.'_meta_box')) {
452 return $post_id;
453 }
454
455 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
456 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
457 return $post_id;
458 }
459
460 // Check the user's permissions.
461 if (isset($_POST['post_type']) && $post_type == $_POST['post_type']) {
462
463 if ( ! current_user_can('edit_post', $post_id)) {
464 return $post_id;
465 }
466 }
467
468 $meta_fields = CBXGooglemapMetaHelper::cbxgooglemap_meta_fields($post_id);
469
470
471 $combined_field = '_'.$post_type.'_combined'; //field name for non sortable fields
472 $meta_prefix = '_'.$post_type; //field prefix for sortable fields
473
474 $meta_combined = get_post_meta($post_id, $combined_field, true); //meta fields not sortable
475
476 $meta_combined = ! is_array($meta_combined) ? [] : $meta_combined;
477
478
479 $combined_arr = [];
480 foreach ($meta_fields as $section_id => $fields) {
481 foreach ($fields as $id => $field) {
482
483 $field_type = $field['type'];
484
485 $sanitize_callback = isset($field['sanitize_callback']) ? $field['sanitize_callback'] : null;
486
487 if (isset($_POST[$meta_prefix.$id])) {
488
489 if (isset($_POST[$meta_prefix.$id])) {
490 $updated_value = wp_unslash($_POST[$meta_prefix.$id]);//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
491 $updated_value = is_array($updated_value) ? cbxgooglemap_decode_entities_array($updated_value) : html_entity_decode($updated_value);
492
493 $sanitized = false;
494
495 if ($sanitize_callback !== null && is_callable($sanitize_callback)) {
496 $sanitized = true;
497 $updated_value = call_user_func($sanitize_callback, $updated_value);
498 }
499 elseif ($field_type == 'text') {
500 $sanitized = true;
501 $updated_value = sanitize_text_field($updated_value);
502 } elseif ($field_type == 'textarea') {
503 $sanitized = true;
504 $updated_value = sanitize_textarea_field( $updated_value );
505 }
506
507 if(!$sanitized) {
508 $updated_value = wp_kses_post($updated_value);//general sanitization
509 }
510
511 $updated_value = apply_filters('cbxgooglemap_meta_sanitize_admin', $updated_value, $id, $field, $fields);
512
513 $is_sortable = isset($field['sortable']) ? $field['sortable'] : false;
514
515 if ($is_sortable) {
516 $ret = update_post_meta($post_id, $meta_prefix.$id, $updated_value); //update the combined meta
517 } else {
518 //save as combined meta
519 $meta_combined[$meta_prefix.$id] = $updated_value;
520 }
521 }
522
523 }
524
525 }
526 }
527
528 update_post_meta($post_id, $combined_field, $meta_combined); //update the combined meta
529
530 do_action('cbxgooglemap_metabox_save', $post_id);
531
532 }//end metabox_save
533
534 /**
535 * Add or adjust col for cbxgooglemap post type
536 *
537 * @param $cbxpoll_columns
538 *
539 * @return array
540 *
541 */
542 public function cbxgooglemap_add_new_columns($columns)
543 {
544 unset($columns['date']);
545 unset($columns['comments']);
546
547 $columns['lat'] = esc_attr('Latitude', 'cbxgooglemap');
548 $columns['lng'] = esc_attr('Longitude', 'cbxgooglemap');
549 $columns['zoom'] = esc_attr('Zoom', 'cbxgooglemap');
550
551 //$columns['shortcode'] = esc_attr( 'Shortcode', 'cbxgooglemap' );
552
553 return $columns;
554 }//end cbxgooglemap_add_new_columns
555
556 /**
557 * Add extra cols information for cbxgooglemap post type
558 *
559 * @param $column_name
560 *
561 *
562 * show data to poll table custom column
563 */
564 public function cbxgooglemap_manage_columns($column_name)
565 {
566 global $wpdb, $post;
567
568 $post_id = intval($post->ID);
569 $post_type = $post->post_type;
570
571 $combined_field = '_cbxgooglemap_combined'; //field name for non sortable fields
572 $meta_prefix = '_cbxgooglemap'; //field prefix for sortable fields
573
574 $meta_combined = get_post_meta($post_id, $combined_field, true);
575
576 $lat = get_post_meta($post_id, $meta_prefix.'lat', true);
577 $lat = ($lat !== false) ? $lat : '';
578
579 $lng = get_post_meta($post_id, $meta_prefix.'lng', true);
580 $lng = ($lng !== false) ? $lng : '';
581
582 $zoom = (isset($meta_combined[$meta_prefix.'zoom']) && intval($meta_combined[$meta_prefix.'zoom']) > 0) ? intval($meta_combined[$meta_prefix.'zoom']) : '';
583
584
585 switch ($column_name) {
586 /*case 'shortcode':
587 echo '<div class="cbxshortcode-wrap">';
588 echo '<span data-clipboard-text=\'[cbxgooglemap id="' . absint( $post_id ) . '"]\' title="' . esc_html__( 'Click to clipboard', 'cbxgooglemap' ) . '" id="cbxgooglemapshortcode-' . absint( $post_id ) . '" class="cbxshortcode cbxshortcode-edit cbxshortcode-' . absint( $post_id ) . '">[cbxgooglemap id="' . absint( $post_id ) . '"]</span>';
589 echo '<span class="cbxballon_ctp_btn cbxballon_ctp" aria-label="' . esc_html__( 'Click to copy', 'cbxgooglemap' ) . '" data-balloon-pos="up"><i></i></span>';
590 echo '</div>';
591 break;*/
592 case 'lat':
593 echo esc_html($lat);
594 break;
595 case 'lng':
596 echo esc_html($lng);
597 break;
598 case 'zoom':
599 echo esc_html($zoom);
600 break;
601 default:
602 break;
603 } // end switch
604 }//end cbxgooglemap_manage_columns
605
606 /**
607 * Register the stylesheets for the admin area.
608 *
609 * @since 1.0.0
610 */
611 public function enqueue_styles($hook)
612 {
613 $current_page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
614 $ver = $this->version;
615 $in_footer = [
616 'in_footer' => true,
617 ];
618
619 $vendor_url = CBXGOOGLEMAP_ROOT_URL.'assets/vendors/';
620 //$css_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
621 //$js_url = CBXGOOGLEMAP_ROOT_URL . 'assets/css/';
622
623 $css_url_part = CBXGOOGLEMAP_ROOT_URL.'assets/css/';
624 $css_url_part_vendors = CBXGOOGLEMAP_ROOT_URL.'assets/vendors/';
625 $js_url_part_vendors = CBXGOOGLEMAP_ROOT_URL.'assets/vendors/';
626
627 global $post_type, $post;
628
629 $api_key = esc_attr($this->settings->get_option('apikey', 'cbxgooglemap_general', ''));
630 $map_source = absint($this->settings->get_option('mapsource', 'cbxgooglemap_general', 1));
631
632 wp_register_style('awesome-notifications', $css_url_part_vendors.'awesome-notifications/style.css', [], $ver);
633 wp_register_style('flatpickr', $css_url_part_vendors.'flatpickr/flatpickr.min.css', [], $ver);
634 wp_register_style('cbxgooglemap-admin', $css_url_part.'cbxgooglemap-admin.css', [], $ver);
635
636 //listing mode
637 if (($hook == 'edit.php') && $post_type == 'cbxgooglemap') {
638 wp_register_style('cbxgooglemap-admin', $css_url_part.'cbxgooglemap-admin.css', [], $ver);
639 wp_enqueue_style('cbxgooglemap-admin');
640 }
641
642 //add new, edit mode
643 if (($hook == 'post.php' || $hook == 'post-new.php') && $post_type == 'cbxgooglemap') {
644
645 if ($map_source == 0) {
646 wp_register_style('leaflet', $vendor_url.'leaflet/leaflet.css', [], $ver, 'all');
647 wp_register_style('leaflet-control-geocoder', $vendor_url.'leaflet-control-geocoder/Control.Geocoder.css', [], $ver, 'all');
648 }
649
650 wp_register_style('select2', $vendor_url.'select2/select2.min.css', [], $ver);
651 wp_register_style('cbxgooglemap-admin', $css_url_part.'cbxgooglemap-admin.css', [
652 'select2',
653 ], $ver);
654
655 wp_enqueue_style('select2');
656
657 if ($map_source == 0) {
658 wp_enqueue_style('leaflet');
659 wp_enqueue_style('leaflet-control-geocoder');
660 }
661
662 wp_enqueue_style('awesome-notifications');
663 wp_enqueue_style('cbxgooglemap-admin');
664 }//add new, edit mode
665
666 if ($current_page == 'cbxgooglemap_settings') {
667 wp_register_style('pickr', $css_url_part_vendors.'pickr/classic.min.css', [], $ver);
668 wp_register_style('select2', $css_url_part_vendors.'select2/select2.min.css', [], $ver);
669
670 wp_register_style('cbxgooglemap-setting', $css_url_part.'cbxgooglemap-setting.css', [
671 'pickr',
672 'awesome-notifications',
673 'select2',
674 'cbxgooglemap-admin'
675 ], $ver);
676
677 wp_enqueue_style('pickr');
678 wp_enqueue_style('awesome-notifications');
679 wp_enqueue_style('select2');
680
681 //for demo css
682 if ($map_source == 0) {
683 wp_register_style('leaflet', $vendor_url.'leaflet/leaflet.css', [], $ver, 'all');
684 wp_register_style('cbxgooglemap-public', $css_url_part.'cbxgooglemap-public.css', ['leaflet'], $ver, 'all');
685 wp_enqueue_style('leaflet');
686
687 } else {
688 wp_register_style('cbxgooglemap-public', $css_url_part.'cbxgooglemap-public.css', [], $ver, 'all');
689 }
690
691 wp_enqueue_style('cbxgooglemap-public');
692 wp_enqueue_style('cbxgooglemap-admin');
693 wp_enqueue_style('cbxgooglemap-setting');
694
695 //end for demo css
696 }//end setting
697
698 if ($current_page == 'cbxgooglemap_support') {
699 wp_enqueue_style('cbxgooglemap-admin');
700 }//end style adding for doc page
701
702 //tax pages
703 if (($hook == 'edit-tags.php' || $hook == 'term.php') && $post_type == 'cbxgooglemap' && $current_page == '') {
704 wp_enqueue_style('awesome-notifications');
705 wp_enqueue_style('cbxgooglemap-admin');
706 }
707 }//end enqueue_styles
708
709 /**
710 * Register the JavaScript for the admin area.
711 *
712 * @since 1.0.0
713 */
714 public function enqueue_scripts($hook)
715 {
716 $current_page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';// phpcs:ignore WordPress.Security.NonceVerification.Recommended
717 $ver = $this->version;
718 $in_footer = [
719 'in_footer' => true,
720 ];
721
722 $vendor_url = CBXGOOGLEMAP_ROOT_URL.'assets/vendors/';
723 $css_url = CBXGOOGLEMAP_ROOT_URL.'assets/css/';
724 $js_url = CBXGOOGLEMAP_ROOT_URL.'assets/js/';
725
726 //assets urls
727 $js_url_part = CBXGOOGLEMAP_ROOT_URL.'assets/js/';
728 $js_url_part_vendors = CBXGOOGLEMAP_ROOT_URL.'assets/vendors/';
729 $js_url_part_vanila = CBXGOOGLEMAP_ROOT_URL.'assets/js/vanila/';
730 $js_url_part_build = CBXGOOGLEMAP_ROOT_URL.'assets/js/build/';
731
732
733 $t = true;
734 $f = false;
735
736
737 $in_footer = [
738 'in_footer' => $t,
739 ];
740
741 $in_footer_async = [
742 'in_footer' => $t,
743 'strategy' => 'async'
744 ];
745
746 $global_translation = CBXGooglemapHelper::global_translation_strings();
747 $plus_svg = cbxgooglemap_esc_svg(cbxgooglemap_load_svg('icon_plus'));
748
749
750 global $post_type, $post;
751 $post_id = isset($post->ID) ? absint($post->ID) : 0;
752
753 $api_key = esc_attr($this->settings->get_option('apikey', 'cbxgooglemap_general', ''));
754 $map_source = absint($this->settings->get_option('mapsource', 'cbxgooglemap_general', 1));
755 $default_mapicon = esc_url($this->settings->get_option('mapicon', 'cbxgooglemap_general', ''));
756
757 /*if($default_mapicon == ''){
758 $default_mapicon = 'https://mt.googleapis.com/vt/icon/name=icons/spotlight/spotlight-poi.png';
759 }*/
760
761 /*$zoom_level_default = intval( $this->settings->get_option( 'zoom', 'cbxgooglemap_general', '8' ) );
762 if ( $zoom_level_default == 0 ) {
763 $zoom_level_default = 8;
764 }*/
765
766
767 wp_register_script('cbxgooglemap-events', $js_url.'cbxgooglemap-events.js', [], $ver, $in_footer);
768 wp_register_script('awesome-notifications', $js_url_part_vendors.'awesome-notifications/script.js', [], $ver, $in_footer);
769 wp_register_script('select2', $js_url_part_vendors.'select2/select2.min.js', ['jquery'], $ver, $in_footer);
770 //wp_register_script( 'flatpickr', $js_url_part_vendors . 'flatpickr/flatpickr.min.js', [ 'jquery' ], $ver, true );
771 wp_register_script('pickr', $js_url_part_vendors.'pickr/pickr.min.js', [], $ver, $in_footer);
772 //wp_register_script( 'jquery-validate', $js_url_part_vendors . 'jquery-validation/jquery.validate.min.js', [ 'jquery' ], $ver, $in_footer );
773
774 //maps listing mode
775 if ($hook == 'edit.php' && $post_type == 'cbxgooglemap') {
776 wp_register_script('cbxgooglemap-listing', $js_url.'cbxgooglemap-listing.js', ['jquery'], $ver, $t);
777
778 $cbxgooglemap_listing_js_vars = apply_filters('cbxgooglemap_listing_js_vars', $global_translation);
779 wp_localize_script('cbxgooglemap-listing', 'cbxgooglemap_listing', $cbxgooglemap_listing_js_vars);
780
781 wp_enqueue_script('jquery');
782 wp_enqueue_script('cbxgooglemap-listing');
783 }
784
785 //add new or edit mode
786 if (($hook == 'post.php' || $hook == 'post-new.php') && $post_type == 'cbxgooglemap') {
787
788 wp_enqueue_script('cbxgooglemap-events');
789
790
791 $edit_js_dep = ['jquery', 'select2'];
792
793 if ($map_source == 1 && $api_key != '') {
794 wp_register_script('coregooglemapapi', '//maps.googleapis.com/maps/api/js?key='.$api_key.'&libraries=places&callback=Function.prototype', [], $ver, $in_footer_async);
795
796 $edit_js_dep[] = 'awesome-notifications';
797 $edit_js_dep[] = 'coregooglemapapi';
798 $edit_js_dep[] = 'cbxgooglemap-events';
799
800 } elseif ($map_source == 0) {
801 wp_register_script('coregooglemapapi', $vendor_url.'leaflet/leaflet.js', [], $ver, $in_footer_async);
802 wp_register_script('leaflet-control-geocoder', $vendor_url.'leaflet-control-geocoder/Control.Geocoder.js', [], $ver, $in_footer_async);
803
804 $edit_js_dep[] = 'awesome-notifications';
805 $edit_js_dep[] = 'coregooglemapapi';
806 $edit_js_dep[] = 'leaflet-control-geocoder';
807 $edit_js_dep[] = 'cbxgooglemap-events';
808 }
809
810 wp_enqueue_media();
811 $main_marker = [];
812
813 wp_register_script('cbxgooglemap-edit', $js_url.'cbxgooglemap-edit.js', $edit_js_dep, $ver, $in_footer);
814
815 $edit_js_vars = [
816 'search_address' => esc_html__('Search Address', 'cbxgooglemap'),
817 'icon_url_default' => $default_mapicon,
818 'api_key' => $api_key,
819 'map_source' => $map_source,
820 'map_title_placeholder' => esc_html__('Map Title Here', 'cbxgooglemap'),
821 'no_address_found' => esc_html__('No details available for current input', 'cbxgooglemap'),
822 ];
823
824 $edit_js_vars = array_merge($edit_js_vars, $global_translation);
825
826 $edit_js_vars = apply_filters('cbxgooglemap_edit_js_vars', $edit_js_vars, $post_id);
827
828 wp_localize_script('cbxgooglemap-edit', 'cbxgooglemap_edit', $edit_js_vars);
829
830 //enqueue dependency js cripts
831 foreach ($edit_js_dep as $dep) {
832 wp_enqueue_script($dep);
833 }
834
835 //enqueue main edit scripts
836 wp_enqueue_script('cbxgooglemap-edit');
837 }
838
839 if ($current_page == 'cbxgooglemap_settings') {
840 $setting_js_deps = [
841 'jquery',
842 'jquery-ui-sortable',
843 'select2',
844 'pickr',
845 'awesome-notifications'
846 ];
847
848 $setting_js_deps = apply_filters('cbxgooglemap_setting_js_deps', $setting_js_deps);
849
850 $blog_id = is_multisite() ? get_current_blog_id() : null;
851
852 // Localize the script with new data
853 $setting_js_vars = [
854 'global_setting_link_html' => '<a href="'.esc_url(admin_url('edit.php?post_type=cbxgooglemap&page=cbxgooglemap_settings')).'" class="button outline primary pull-right">'.esc_html__('Global Settings', 'cbxgooglemap').'</a>',
855 'lang' => get_user_locale(),
856 'clearPermalinks' => esc_url_raw(get_rest_url($blog_id, 'cbxgooglemap/v1/admin/clear-permalinks')),
857 'rest_end_points' => [
858 'migrate_wp_wpsimplegooglemap' => esc_url_raw(get_rest_url('', 'cbxgooglemap/v1/migrate-wp-wpsimplegooglemap-data')),
859 ],
860 ];
861
862 $import_modal_html = '<div id="cbxgooglemap_settings_import_modal_wrap" class="cbx-chota">';
863 $import_modal_html .= '<h2>'.esc_html__('Import CBX Google Map Setting: Json file', 'cbxgooglemap').'</h2>';
864 $import_modal_html .= '<form method="post" id="cbxgooglemap_settings_import_form">';
865 $import_modal_html .= '<input type="file" name="file" id="cbxgooglemap_settings_import_file" accept="application/json" />';
866 $import_modal_html .= '</form>';
867 $import_modal_html .= '</div>';
868
869 $setting_js_vars['import_modal'] = $import_modal_html;
870 $setting_js_vars['import_modal_progress'] = '<p>'.esc_html__('Please wait, Importing...', 'cbxgooglemap').'</p>';
871
872 $cbxgooglemap_setting_js_vars = apply_filters('cbxgooglemap_setting_js_vars', array_merge($setting_js_vars, $global_translation));
873
874 wp_register_script('cbxgooglemap-setting', $js_url.'cbxgooglemap-setting.js', $setting_js_deps, $ver, $in_footer);
875
876 wp_localize_script('cbxgooglemap-setting', 'cbxgooglemap_setting_js_var', $cbxgooglemap_setting_js_vars);
877
878 //core
879 wp_enqueue_script('jquery');
880 wp_enqueue_media();
881
882 //vendors
883 wp_enqueue_script('jquery-ui-sortable');
884 wp_enqueue_script('select2');
885 wp_enqueue_script('pickr');
886 wp_enqueue_script('awesome-notifications');
887
888 //custom
889 wp_enqueue_script('cbxgooglemap-setting');
890
891 //for demo js (we need this but we will sort out later)
892 if (($map_source == 1 && ! empty($api_key)) || $map_source == 0) {
893 if ($map_source == 1) {
894 //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
895 wp_register_script('coregooglemapapi', '//maps.googleapis.com/maps/api/js?key='.esc_attr($api_key).'&libraries=places&callback=Function.prototype', [], $ver, $in_footer_async);
896 wp_register_script('cbxgooglemap-public', $js_url.'cbxgooglemap-public.js', [
897 'jquery',
898 'cbxgooglemap-events',
899 'coregooglemapapi'
900 ], $ver, $in_footer);
901
902 wp_enqueue_script('coregooglemapapi');
903 } else {
904 wp_register_script('coregooglemapapi', $vendor_url.'leaflet/leaflet.js', [], $ver, $in_footer_async);
905 wp_register_script('cbxgooglemap-public', $js_url.'cbxgooglemap-public.js', [
906 'jquery',
907 'cbxgooglemap-events',
908 'coregooglemapapi',
909 //'leaflet-control-geocoder'
910 ], $ver, $in_footer);
911
912 wp_enqueue_script('cbxgooglemap-events');
913 wp_enqueue_script('coregooglemapapi');
914 //wp_enqueue_script( 'leaflet-control-geocoder' );
915 }
916
917 wp_enqueue_script('cbxgooglemap-public');
918 }//end for demo js
919
920 }//end js for setting page
921
922 //enqueue js for tax page
923 if (($hook == 'edit-tags.php' || $hook == 'term.php') && $post_type == 'cbxgooglemap' && $current_page == '') {
924 //tax js dependencies
925
926 $tax_js_deps = [
927 //core
928 'jquery',
929 //vendors
930 //'jquery-validate'
931 ];
932
933 $tax_js_deps = apply_filters('cbxgooglemap_tax_js_deps', $tax_js_deps);
934
935 $new_map_link_html = '<a href="'.esc_url(admin_url('edit.php?post_type=cbxgooglemap')).'" class="button secondary icon icon-right icon-inline mr-5"><i class="cbx-icon">'.$plus_svg.'</i>'.esc_html__('New Map',
936 'cbxgooglemap').'</a>';
937
938 $global_setting_link_html = '<a href="'.esc_url(admin_url('edit.php?post_type=cbxgooglemap&page=cbxgooglemap_settings')).'" class="button outline primary">'.esc_html__('Global Settings', 'cbxgooglemap').'</a>';
939
940 $tax_js_vars =
941 [
942 'tax_title_prefix' => esc_html__('Map:', 'cbxgooglemap'),
943 'tags_title' => esc_html__('Map: Tags', 'cbxgooglemap'),
944 'category_title' => esc_html__('Map: Category', 'cbxgooglemap'),
945 'tax_new_setting' => '<div class="wp-heading-wrap-right pull-right">'.$new_map_link_html.$global_setting_link_html.'</div>'
946 ];
947
948 $tax_js_vars = apply_filters('cbxgooglemap_tax_js_vars', array_merge($tax_js_vars, $global_translation));
949
950 wp_register_script('cbxgooglemap-tax', $js_url_part.'cbxgooglemap-tax.js', $tax_js_deps, $ver, true);
951 wp_localize_script('cbxgooglemap-tax', 'cbxgooglemap_tax', $tax_js_vars);
952
953
954 //core
955 wp_enqueue_script('jquery');
956
957 //vendors
958 //wp_enqueue_script( 'jquery-validate' );
959
960 //custom
961 wp_enqueue_script('cbxgooglemap-tax');
962 }//end enqueue js for tax page
963 }//end enqueue_scripts
964
965 /**
966 * Init all gutenberg blocks
967 */
968 public function gutenberg_blocks(){
969 if ( ! function_exists('register_block_type')) {
970 return;
971 }
972
973 $settings = $this->settings;
974 $in_footer = [
975 'in_footer' => true,
976 ];
977
978
979 //$general_settings = get_option( 'cbxgooglemap_general', [] );
980
981 //default field values
982 $zoom_default = $settings->get_field('zoom', 'cbxgooglemap_general', '8');
983 if ($zoom_default == 0) {
984 $zoom_default = 8;
985 }
986
987 $width_default = $settings->get_field('width', 'cbxgooglemap_general', '100%');
988 if ($width_default == '' || $width_default == 0) {
989 $width_default = '100%';
990 }
991
992 $height_default = absint($settings->get_field('height', 'cbxgooglemap_general', '300'));
993 if ($height_default == 0) {
994 $height_default = 300;
995 }
996
997
998 $scrollwheel_default = absint($settings->get_field('scrollwheel', 'cbxgooglemap_general', 1));
999 $showinfo_default = absint($settings->get_field('showinfo', 'cbxgooglemap_general', 1));
1000 $infow_open_default = absint($settings->get_field('infow_open', 'cbxgooglemap_general', 1));
1001 $maptype_default = esc_attr($settings->get_field('maptype', 'cbxgooglemap_general', 'roadmap'));
1002
1003 $query = get_posts([
1004 'post_type' => 'cbxgooglemap',
1005 'orderby' => 'date',
1006 'posts_per_page' => -1,
1007 'post_status' => 'publish'
1008 ]);
1009
1010 $googleMap_posts = [];
1011
1012 $googleMap_posts[] = [
1013 'label' => esc_html__('Select Map', 'cbxgooglemap'),
1014 'value' => '0'
1015 ];
1016
1017 foreach ($query as $key => $data) {
1018 $googleMap_posts[] = [
1019 'label' => esc_html($data->post_title),
1020 'value' => absint($data->ID)
1021 ];
1022 }
1023
1024
1025 wp_register_script('cbxgooglemap-block', plugin_dir_url(__FILE__).'../assets/js/blocks/cbxgooglemap-block.js', [
1026 'wp-blocks',
1027 'wp-element',
1028 'wp-components',
1029 'wp-editor',
1030 ], filemtime(plugin_dir_path(__FILE__).'../assets/js/blocks/cbxgooglemap-block.js'), $in_footer);
1031
1032 wp_register_style('cbxgooglemap-block', plugin_dir_url(__FILE__).'../assets/css/cbxgooglemap-block.css', [], filemtime(plugin_dir_path(__FILE__).'../assets/css/cbxgooglemap-block.css'));
1033
1034 $js_vars = apply_filters('cbxgooglemap_block_js_vars',
1035 [
1036 'block_title' => esc_html__('CBX Map: Location Map', 'cbxgooglemap'),
1037 'block_category' => 'codeboxr',
1038 'block_icon' => 'universal-access-alt',
1039 'general_settings' => [
1040 'title' => esc_html__('CBX Map Settings', 'cbxgooglemap'),
1041 'id' => esc_html__('Select Map', 'cbxgooglemap'),
1042 'id_default' => '',
1043 'id_options' => $googleMap_posts,
1044 'id_note' => esc_html__('Choose saved map or create from custom attributes below', 'cbxgooglemap'),
1045 'custom_attribute_note' => esc_html__('Custom Map Attributes', 'cbxgooglemap'),
1046 'maptype' => esc_html__('Map Type(Google Map Only)', 'cbxgooglemap'),
1047 'maptype_options' => CBXGooglemapHelper::maptype_block_options(),
1048 'lat' => esc_html__('Latitude', 'cbxgooglemap'),
1049 'lng' => esc_html__('Longitude', 'cbxgooglemap'),
1050 'width' => esc_html__('Width(Numeric, % allowed)', 'cbxgooglemap'),
1051 'height' => esc_html__('Height(only Numeric)', 'cbxgooglemap'),
1052 'zoom' => esc_html__('zoom', 'cbxgooglemap'),
1053 'scrollwheel' => esc_html__('Mouse Scroll Wheel', 'cbxgooglemap'),
1054 'showinfo' => esc_html__('Show Popup', 'cbxgooglemap'),
1055 'infow_open' => esc_html__('Popup Auto Display ', 'cbxgooglemap'),
1056 'heading' => esc_html__('Popup Heading', 'cbxgooglemap'),
1057 'address' => esc_html__('Location Address', 'cbxgooglemap'),
1058 'website' => esc_html__('Website url', 'cbxgooglemap'),
1059 'mapicon' => esc_html__('Map Icon', 'cbxgooglemap'),
1060 'mapicon_select' => esc_html__('Select image', 'cbxgooglemap'),
1061 ],
1062 ]);
1063
1064 wp_localize_script('cbxgooglemap-block', 'cbxgooglemap_block', $js_vars);
1065
1066 register_block_type('codeboxr/cbxgooglemap', [
1067 'editor_script' => 'cbxgooglemap-block',
1068 'editor_style' => 'cbxgooglemap-block',
1069 'attributes' => apply_filters('cbxgooglemap_block_attributes', [
1070 //general
1071 'id' => [
1072 'type' => 'integer',
1073 'default' => '0',
1074 ],
1075 'maptype' => [
1076 'type' => 'string',
1077 'default' => $maptype_default
1078 ],
1079 'lat' => [
1080 'type' => 'string',
1081 'default' => ''
1082 ],
1083 'lng' => [
1084 'type' => 'string',
1085 'default' => ''
1086 ],
1087 'width' => [
1088 'type' => 'string',
1089 'default' => $width_default,
1090 ],
1091
1092 'height' => [
1093 'type' => 'integer',
1094 'default' => $height_default
1095 ],
1096 'zoom' => [
1097 'type' => 'string',
1098 'default' => $zoom_default
1099 ],
1100 'scrollwheel' => [
1101 'type' => 'boolean',
1102 'default' => ($scrollwheel_default) ? true : false
1103 ],
1104 'showinfo' => [
1105 'type' => 'boolean',
1106 'default' => ($showinfo_default) ? true : false
1107 ],
1108 'infow_open' => [
1109 'type' => 'boolean',
1110 'default' => ($infow_open_default) ? true : false
1111 ],
1112 'heading' => [
1113 'type' => 'string',
1114 'default' => ''
1115 ],
1116 'address' => [
1117 'type' => 'string',
1118 'default' => ''
1119 ],
1120 'website' => [
1121 'type' => 'string',
1122 'default' => ''
1123 ],
1124 'mapicon' => [
1125 'type' => 'string',
1126 'default' => '',
1127 ]
1128 ]),
1129 'render_callback' => [$this, 'render_block']
1130 ]);
1131
1132 }//end gutenberg_blocks
1133
1134 /**
1135 * Getenberg server side render
1136 *
1137 * @param $settings
1138 *
1139 * @return string
1140 */
1141 public function render_block($attributes)
1142 {
1143 $settings = $this->settings;
1144 //$general_settings = get_option( 'cbxgooglemap_general', [] );
1145
1146 $api_key = $settings->get_field('apikey', 'cbxgooglemap_general', '');
1147 $map_source = (int) $settings->get_field( 'mapsource', 'cbxgooglemap_general', 1 );
1148
1149 if ($map_source == 1 && $api_key == '') {
1150 echo '<p style="text-align: center;">'.esc_html__('Google Map Api Key is invalid!', 'cbxgooglemap').'</p>';
1151 } else {
1152 $id = isset($attributes['id']) ? absint($attributes['id']) : 0;
1153
1154 if ($id > 0) {
1155 //render map from saved map
1156 return '[cbxgooglemap id="'.$id.'"]';
1157 } else {
1158 $attr = [];
1159
1160 if (isset($attributes['lat'])) {
1161 $attr['lat'] = sanitize_text_field(wp_unslash($attributes['lat']));
1162 }
1163
1164 if (isset($attributes['lng'])) {
1165 $attr['lng'] = sanitize_text_field(wp_unslash($attributes['lng']));
1166 }
1167
1168 if (isset($attributes['width'])) {
1169 $attr['width'] = sanitize_text_field($attributes['width']);
1170 }
1171
1172 if (isset($attributes['height'])) {
1173 $attr['height'] = (int) $attributes['height'];
1174 }
1175
1176 if (isset($attributes['zoom'])) {
1177 $attr['zoom'] = sanitize_text_field($attributes['zoom']);
1178 }
1179
1180
1181 $attr['scrollwheel'] = ! empty($attributes['scrollwheel']) ? 1 : 0;
1182 $attr['infow_open'] = ! empty($attributes['infow_open']) ? 1 : 0;
1183 $attr['showinfo'] = ! empty($attributes['showinfo']) ? 1 : 0;
1184
1185 if (isset($attributes['heading'])) {
1186 $attr['heading'] = sanitize_text_field(wp_unslash($attributes['heading']));
1187 }
1188 if (isset($attributes['address'])) {
1189 $attr['address'] = sanitize_text_field(wp_unslash($attributes['address']));
1190 }
1191 if (isset($attributes['website'])) {
1192 $attr['website'] = sanitize_text_field(wp_unslash($attributes['website']));
1193 }
1194 if (isset($attributes['maptype'])) {
1195 $attr['maptype'] = sanitize_text_field(wp_unslash($attributes['maptype']));
1196 }
1197 if (isset($attributes['mapicon'])) {
1198 $attr['mapicon'] = esc_url($attributes['mapicon']);
1199 }
1200
1201 $attr = apply_filters('cbxgooglemap_block_shortcode_builder_attr', $attr, $attributes);
1202
1203 $attr_html = '';
1204
1205 foreach ($attr as $key => $value) {
1206 $attr_html .= ' '.$key.'="'.$value.'" ';
1207 }
1208
1209 //return do_shortcode( '[cbxgooglemap ' . $attr_html . ']' );
1210 return '[cbxgooglemap '.$attr_html.']';
1211 }
1212 }//end if api keys are ok
1213
1214 }//end codeboxrflexiblecountdown_block_render
1215
1216 /**
1217 * Register New Gutenberg block Category if need
1218 *
1219 * @param $categories
1220 * @param $post
1221 *
1222 * @return mixed
1223 */
1224 public function gutenberg_block_categories($categories, $post)
1225 {
1226 $found = false;
1227 foreach ($categories as $category) {
1228 if ($category['slug'] == 'codeboxr') {
1229 $found = true;
1230 break;
1231 }
1232 }
1233
1234 if ( ! $found) {
1235 return array_merge(
1236 $categories,
1237 [
1238 [
1239 'slug' => 'codeboxr',
1240 'title' => esc_html__('CBX Blocks', 'cbxgooglemap'),
1241 ],
1242 ]
1243 );
1244 }
1245
1246 return $categories;
1247 }//end gutenberg_block_categories
1248
1249
1250 /**
1251 * Enqueue style for block editor
1252 */
1253 public function enqueue_block_editor_assets()
1254 {
1255 }//end enqueue_block_editor_assets
1256
1257 /**
1258 * Load setting html
1259 *
1260 * @return void
1261 */
1262 public function settings_reset_load()
1263 {
1264 //security check
1265 check_ajax_referer('cbxgooglemap_nonce', 'security');
1266
1267 $msg = [];
1268 $msg['html'] = '';
1269 $msg['message'] = esc_html__('CBX Google Map reset setting html loaded successfully', 'cbxgooglemap');
1270 $msg['success'] = 1;
1271
1272 if ( ! current_user_can('manage_options')) {
1273 $msg['message'] = esc_html__('Sorry, you don\'t have enough permission', 'cbxgooglemap');
1274 $msg['success'] = 0;
1275 wp_send_json($msg);
1276 }
1277
1278 $msg['html'] = CBXGooglemapHelper::setting_reset_html_table();
1279
1280 wp_send_json($msg);
1281 } //end method settings_reset_load
1282
1283 /**
1284 * Full plugin reset and redirect
1285 */
1286 public function plugin_options_reset()
1287 {
1288
1289 //security check
1290 check_ajax_referer('cbxgooglemap_nonce', 'security');
1291
1292 $url = admin_url('admin.php?page=cbxgooglemap_settings');
1293
1294 $msg = [];
1295 $msg['message'] = esc_html__('CBX Google Map setting options reset successfully', 'cbxgooglemap');
1296 $msg['success'] = 1;
1297 $msg['url'] = $url;
1298
1299 if ( ! current_user_can('manage_options')) {
1300 $msg['message'] = esc_html__('Sorry, you don\'t have enough permission', 'cbxgooglemap');
1301 $msg['success'] = 0;
1302 wp_send_json($msg);
1303 }
1304
1305 do_action('cbxgooglemap_plugin_reset_before');
1306
1307 $plugin_resets = wp_unslash($_POST); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
1308
1309 //delete options
1310 $reset_options = isset($plugin_resets['reset_options']) ? $plugin_resets['reset_options'] : [];
1311 $option_values = (is_array($reset_options) && sizeof($reset_options) > 0) ? array_values($reset_options) : array_values(CBXGooglemapHelper::getAllOptionNames());
1312
1313 foreach ($option_values as $key => $option) {
1314 delete_option($option);
1315 }
1316
1317 do_action('cbxgooglemap_plugin_option_delete');
1318 do_action('cbxgooglemap_plugin_reset_after');
1319 do_action('cbxgooglemap_plugin_reset');
1320
1321 wp_send_json($msg);
1322 } //end plugin_reset
1323
1324 /**
1325 * Export plugin global settings
1326 *
1327 * @return void
1328 */
1329 public function settings_export()
1330 {
1331
1332 if (isset($_GET['cbxgooglemap_settings_export'])) {
1333 $url = admin_url('admin.php?page=cbxgooglemap_settings');
1334
1335 $msg = [];
1336 $msg['message'] = esc_html__('Google Map setting exported successfully', 'cbxgooglemap');
1337 $msg['success'] = 1;
1338 $msg['url'] = $url;
1339
1340 $passed = true;
1341
1342 $nonce = isset($_GET['security']) ? sanitize_text_field(wp_unslash($_GET['security'])) : '';
1343 if ( ! wp_verify_nonce($nonce, 'cbxgooglemap_settings_nonce')) {
1344 $passed = false;
1345 $msg['success'] = 0;
1346 $msg['message'] = esc_html__('Security check failed', 'cbxgooglemap');
1347 }
1348
1349 if ( ! current_user_can('manage_options')) {
1350 $passed = true;
1351 $msg['message'] = esc_html__('Sorry, you don\'t have enough permission', 'cbxgooglemap');
1352 $msg['success'] = 0;
1353 }
1354
1355 if ( ! $passed) {
1356 wp_send_json($msg);
1357 }
1358
1359 // Get Meta box data
1360 $sections = CBXGoogleMapAdmin::get_settings_sections();
1361
1362 $data = [
1363 'cbxgooglemap_settings' => true
1364 ];
1365
1366 $single_section = isset($_REQUEST['section']) ? sanitize_text_field(wp_unslash($_REQUEST['section'])) : '';
1367
1368 foreach ($sections as $section) {
1369 if ($single_section != '' && $section['id'] != $single_section) {
1370 continue;
1371 }
1372
1373
1374 $sections_fields = get_option($section['id']);
1375 if ( ! is_array($sections_fields)) {
1376 $sections_fields = [];
1377 }
1378
1379 $data[$section['id']] = $sections_fields;
1380
1381 if ($single_section != '' && $section['id'] == $single_section) {
1382 break;
1383 }
1384 }
1385
1386 // Create JSON
1387 $generate_json = wp_json_encode($data, JSON_PRETTY_PRINT);
1388
1389 // Create filename
1390 $filename = 'cbxgooglemap_settings_json';
1391
1392 // Force download .json file with JSON in it
1393 header('Content-type: application/vnd.ms-excel');
1394 header('Content-Type: application/force-download');
1395 header('Content-Type: application/download');
1396 header('Content-disposition: '.$filename.'.json');
1397 header('Content-disposition: filename='.$filename.'.json');
1398
1399 print $generate_json;//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1400 exit;
1401 }//end if export mode
1402 }//end method settings_export
1403
1404 /**
1405 * Import settings
1406 *
1407 * @return void
1408 */
1409 public function settings_import()
1410 {
1411 //security check
1412 check_ajax_referer('cbxgooglemap_nonce', 'security');
1413
1414 $msg = [];
1415 $msg['message'] = esc_html__('CBX Google Map Settings imported successfully', 'cbxgooglemap');
1416 $msg['success'] = 1;
1417
1418 if ( ! current_user_can('manage_options')) {
1419 $msg['message'] = esc_html__('Sorry, you don\'t have enough permission', 'cbxgooglemap');
1420 $msg['success'] = 0;
1421 wp_send_json($msg);
1422 }
1423
1424 // Import settings api data
1425 $settings_data = isset($_REQUEST['settings_data']) ? sanitize_text_field(wp_unslash($_REQUEST['settings_data'])) : '';
1426 $settings_data = str_replace('data:application/json;base64,', '', $settings_data);
1427 $settings_data = base64_decode($settings_data);
1428
1429 $settings_data = json_decode($settings_data, true);
1430 // Redirect to settings page
1431 $msg['url'] = admin_url('admin.php?page=cbxgooglemap_settings');
1432
1433 if (isset($settings_data['cbxgooglemap_settings'])) {
1434 unset($settings_data['cbxgooglemap_settings']);
1435
1436 if (is_array($settings_data)) {
1437 foreach ($settings_data as $key => $value) {
1438 update_option($key, $value);
1439 }
1440 }
1441 wp_send_json($msg);
1442 }
1443
1444 $msg['message'] = esc_html__('Sorry, wrong format data', 'cbxgooglemap');
1445 $msg['success'] = 0;
1446 wp_send_json($msg);
1447 }//end method settings_import
1448
1449 /**
1450 * Reset single section
1451 *
1452 * @return void
1453 */
1454 public function plugin_reset_section()
1455 {
1456
1457 //security check
1458 check_ajax_referer('cbxgooglemap_nonce', 'security');
1459
1460 $url = admin_url('admin.php?page=cbxgooglemap_settings');
1461
1462 $msg = [];
1463 $msg['message'] = esc_html__('Comfort Google Map setting section reset successfully', 'cbxgooglemap');
1464 $msg['success'] = 1;
1465 $msg['url'] = $url;
1466
1467 if ( ! current_user_can('manage_options')) {
1468 $msg['message'] = esc_html__('Sorry, you don\'t have enough permission', 'cbxgooglemap');
1469 $msg['success'] = 0;
1470 wp_send_json($msg);
1471 }
1472
1473 $section = isset($_POST['section']) ? sanitize_text_field(wp_unslash($_POST['section'])) : '';
1474 if ($section == '') {
1475 $msg['message'] = esc_html__('Option section setting failed, as no section id is empty', 'cbxgooglemap');
1476 $msg['success'] = 0;
1477 wp_send_json($msg);
1478 }
1479
1480 //before hooks
1481 do_action('cbxgooglemap_plugin_reset_section_before');
1482
1483
1484 //delete the section
1485 delete_option($section);
1486
1487 //after hoooks
1488 do_action('cbxgooglemap_plugin_reset_section_after');
1489 do_action('cbxgooglemap_plugin_reset_section');
1490
1491 wp_send_json($msg);
1492 }//end method plugin_reset_section
1493 }//end class CBXGoogleMapAdmin