-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhumbee_reservation.php
212 lines (182 loc) · 5.88 KB
/
humbee_reservation.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* Plugin Name: Humbee Reservation Plugin
* Description: A react application
* Version: 0.1
*/
define('MYHUMBEE_PLUGIN_VERSION', '1.0.1');
define('MYHUMBEE_PLUGIN_MAIN_FILE', __FILE__);
define('MYHUMBEE_PLUGIN_URL', untrailingslashit(plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__))));
define('MYHUMBEE_PLUGIN_DIR', untrailingslashit(plugin_dir_path(__FILE__)));
class MYHUMBEE_PLUGIN
{
/**
* @var Singleton The reference the *Singleton* instance of this class
*/
private static $instance;
/**
* Returns the *Singleton* instance of this class.
*
* @return Singleton The *Singleton* instance.
*/
public static function get_instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
private function __construct()
{
$this->init();
}
/**
* Init the plugin after plugins_loaded so environment variables are set.
*
* @since 1.0.0
* @version 4.0.0
*/
public function install()
{
}
public function init()
{
register_activation_hook(MYHUMBEE_PLUGIN_MAIN_FILE, array($this, 'install'));
add_action('admin_menu', array($this, 'myhumbee_plugin_admin_page'));
}
public function myhumbee_plugin_admin_page()
{
add_menu_page(
'Humbee Widget',
// Page Title
'Humbee Widget',
// Menu Title
'manage_options',
// Capability
'myhumbee-api-settings',
// Page slug
array($this, 'myhumbee_plugin_admin_page_html'),
// Callback to print html
'',
6,
);
}
public function myhumbee_plugin_admin_page_html()
{
if (!current_user_can('manage_options')) {
return;
}
// Check if the button has been clicked or the API key is known
$apiKeyKnown = !empty(get_option('location_api_key'));
$buttonDisabled = !$apiKeyKnown && !isset($_POST['myhumbee_form']);
?>
<div class="wrap">
<h1>
<?php echo esc_html(get_admin_page_title()); ?>
</h1>
<?php
if (isset($_POST['myhumbee_form'])) {
update_option('location_api_key', $_POST['location_api_key']);
}
?>
<form class="myhumbee_form" method="post">
<h3>Instellingen</h3>
<div class="cin_input_wrap">
<label for="location_api_key">Location API Key</label><br>
<input type="text" id="location_api_key" name="location_api_key"
value="<?php echo get_option('location_api_key') ?>" oninput="enableDisableSaveButton(this)">
</div>
<div>
<button type="submit" class="button-primary" name="myhumbee_form" <?php if ($buttonDisabled)
echo 'disabled="disabled"'; ?>>
Save
</button>
</div>
</form>
<script>
function enableDisableSaveButton(inputElement) {
var saveButton = document.querySelector('.button-primary[name="myhumbee_form"]');
if (inputElement.value.trim() === '') {
saveButton.disabled = true;
} else {
saveButton.disabled = false;
}
}
</script>
</div>
<?php
}
}
add_action('plugins_loaded', array('MYHUMBEE_PLUGIN', 'get_instance'));
add_action('rest_api_init', 'register_custom_api_endpoints');
function register_custom_api_endpoints()
{
register_rest_route(
'my_custom_namespace/v1',
'/my_data/',
array(
'methods' => 'GET',
'callback' => 'fetch_my_custom_data'
)
);
}
function fetch_my_custom_data()
{
// Here you would fetch your custom data. For this example, let's assume it's an option.
$my_data = get_option('location_api_key');
return new WP_REST_Response($my_data, 200);
}
// Registering scripts and styles.
function my_react_app_init()
{
$path = "/frontend/build/static";
if (getenv('WP_ENV') == "development") {
$path = "/frontend/build/static";
}
wp_register_script("my_react_app_js", plugins_url($path . "/js/main.js", __FILE__), array(), "1.0", true);
wp_register_style("my_react_app_css", plugins_url($path . "/css/main.css", __FILE__), array(), "1.0", "all");
// Localize the script with the data you want to pass
wp_localize_script(
'my_react_app_js',
'myReactAppData',
array(
'location_api_key' => get_option('location_api_key')
)
);
}
// Function to check if a post has our shortcode and enqueue necessary scripts/styles.
function check_for_shortcode($posts)
{
if (empty($posts)) {
return $posts;
}
// Check each post for the shortcode.
foreach ($posts as $post) {
if (has_shortcode($post->post_content, 'my_react_app')) {
my_react_app_init();
break;
}
}
return $posts;
}
add_action('the_posts', 'check_for_shortcode');
function add_react_app_to_footer()
{
my_react_app_init();
wp_enqueue_script("my_react_app_js");
wp_enqueue_style("my_react_app_css");
echo "<div id=\"my_react_app\"></div>";
}
add_action('wp_footer', 'add_react_app_to_footer');
// Function for the shortcode that calls React app.
function AAA()
{
wp_enqueue_script("my_react_app_js");
wp_enqueue_style("my_react_app_css");
return "<div id=\"my_react_app\"></div>";
}
add_shortcode('my_react_app', 'AAA');