-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwc-google-map.php
More file actions
139 lines (123 loc) · 3.75 KB
/
wc-google-map.php
File metadata and controls
139 lines (123 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Plugin Name: WC Google Map
* Description: A simple Google Map plugin for showcasing blocks and settings page. This plugin was created for WordCamp to teach about block and settings page development.
* Author: Rakesh Lawaju
* Author URI: https://www.racase.com.np
* Plugin URI: https://www.racase.com.np/plugins/wc-google-map
* Version: 1.0.1
* Text Domain: wc-google-map
* Domain Path: /languages
* Tested up to: 6.3
* Requires at least: 6.1
* Requires PHP: 5.5
*
* WC Google Map is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* You should have received a copy of the GNU General Public License
* along with WC Google Map. If not, see <http://www.gnu.org/licenses/>.
*
* @package WC Google Map
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
// Include Google Map Block.
include_once 'dist/blocks/google-map/index.php';
/**
* Setting page callback.
*
* @return void
*/
function wc_google_map_submenu_page_callback() {
wp_enqueue_style( 'wc-google-map-admin-settings' );
wp_enqueue_script( 'wc-google-map-admin-settings' );
echo '<div class="wrap">
<div id="google-map-settings-app">Loading...</div>';
echo '</div>';
}
/**
* Register setting submenu.
*
* @return void
*/
function wc_google_map_register_custom_submenu_page() {
add_submenu_page(
'tools.php',
'Google Map',
'Google Map',
'manage_options',
'wc-google-map',
'wc_google_map_submenu_page_callback' );
}
add_action('admin_menu', 'wc_google_map_register_custom_submenu_page');
/**
* Register assets.
*
* @return void
*/
function wc_google_map_register_assets() {
$assets = include plugin_dir_path( __FILE__ ) . '/dist/admin/settings/index.asset.php';
wp_register_script( 'wc-google-map-admin-settings', plugin_dir_url( __FILE__ ) . '/dist/admin/settings/index.js', $assets['dependencies'], $assets['version'], true );
wp_register_style( 'wc-google-map-admin-settings', plugin_dir_url( __FILE__ ) . '/dist/admin/settings/index.css', array( 'wp-components' ), $assets['version'] );
}
add_action('init', 'wc_google_map_register_assets');
/**
* Register rest route.
*
* @return void
*/
function wc_google_map_add_settings_api(){
register_rest_route( 'wc-google-map/v1', '/settings/', array(
'methods' => 'GET',
'callback' => 'wc_google_map_api_get_settings',
'permission_callback' => function() {
return current_user_can( 'manage_options' );
}
) );
register_rest_route( 'wc-google-map/v1', '/settings/', array(
'methods' => 'POST',
'callback' => 'wc_google_map_api_update_settings',
'permission_callback' => function() {
return current_user_can( 'manage_options' );
}
) );
}
add_action( 'rest_api_init', 'wc_google_map_add_settings_api');
/**
* Get setting data to api.
*
* @return void
*/
function wc_google_map_get_settings() {
$default_values = array(
'enableGoogleMapBlock' => true
);
$settings = get_option( 'wc_google_map_settings' );
return wp_parse_args( $settings, $default_values );
}
/**
* Get setting values.
*
* @return void
*/
function wc_google_map_api_get_settings() {
$settings = wc_google_map_get_settings();
wp_send_json( $settings );
}
/**
* Setting API update call back.
*
* @return void
*/
function wc_google_map_api_update_settings() {
$data = json_decode(file_get_contents('php://input'), true);
$settings = array();
$settings['enableGoogleMapBlock'] = isset( $data['enableGoogleMapBlock'] ) && true === $data['enableGoogleMapBlock'];
if ( ! empty( $settings ) ) {
update_option( 'wc_google_map_settings', $settings );
}
wp_send_json( $settings );
}