-
Notifications
You must be signed in to change notification settings - Fork 4
/
settings-page.php
280 lines (245 loc) · 9.92 KB
/
settings-page.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
defined('ABSPATH') || exit;
defined( 'PTC_PLUGIN_ORDERS_PAGE_TYPE' ) || define( 'PTC_PLUGIN_PAGE_TYPE', 'pt_hms_orders' );
defined( 'PTC_PLUGIN_SETTINGS_PAGE_TYPE' ) || define( 'PTC_PLUGIN_SETTINGS_PAGE_TYPE', 'pt_hms_settings' );
add_action('wp_ajax_get_token', 'ajax_get_token');
add_action('wp_ajax_reset_token', 'ajax_reset_token');
add_action('update_option_pt_hms_settings', 'pt_hms_on_option_update', 10, 3);
add_action('admin_menu', 'pt_hms_menu_page'); // Admin menu setup, Pathao Courier page
add_action('admin_menu', 'pt_hms_orders_page'); // submenu settings page
add_action('admin_init', 'pt_hms_settings_init');
function ajax_get_token()
{
$data = issue_access_token(
$_POST['client_id'] ?? '',
$_POST['client_secret'] ?? '',
$_POST['environment'] ?? ''
);
$token = $data['access_token'] ?? null;
if ($token) {
wp_send_json_success($data);
} else {
wp_send_json_error(array('message' => 'Failed to retrieve the token.'));
}
}
function ajax_reset_token()
{
$token = pt_hms_get_token(true);
if ($token) {
wp_send_json_success(array('access_token' => $token));
} else {
wp_send_json_error(array('message' => 'Failed to retrieve the token.'));
}
}
function pt_hms_on_option_update($old_value, $new_value, $option)
{
// Reset the token stored in the database.
delete_option('pt_hms_token_data');
// Fetch a new token.
pt_hms_get_token();
}
function pt_hms_menu_page()
{
add_menu_page(
'Pathao Courier',
'Pathao Courier',
'manage_options',
PTC_PLUGIN_SETTINGS_PAGE_TYPE,
'pt_hms_settings_page_callback',
'dashicons-move',
6
);
}
// Render the settings page
function pt_hms_settings_page_callback()
{
$options = get_option('pt_hms_settings');
$all_fields_filled = isset(
$options['client_id'],
$options['client_secret'],
// $options['username'],
// $options['password'],
$options['environment']
);
$token = $all_fields_filled ? pt_hms_get_token() : null;
?>
<div class="wrap">
<h2>Pathao Courier Settings</h2>
<?php if ($all_fields_filled && !$token): ?>
<div class="notice notice-error">
<p>API credentials are invalid. Please check your credentials and try again.</p>
</div>
<?php endif; ?>
<form method="post" action="options.php">
<?php
settings_fields('pt_hms_settings_group');
do_settings_sections('pt_hms_settings');
submit_button();
?>
</form>
<!-- Token Fetch Button -->
<section>
<h3>Test Credentials</h3>
<button type="button" id="fetch-token-btn">Test credentials validity</button>
<?php if ($token): ?>
<button type="button" id="reset-token-btn">Reset token</button>
<?php endif; ?>
</section>
<!-- JavaScript for AJAX call -->
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('#fetch-token-btn').on('click', function () {
let clientId = $('#client_id').val();
let clientSecret = $('#client_secret').val();
let environment = $('#client_environment').val();
console.log({clientId, clientSecret, environment})
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'get_token',
client_id: clientId,
client_secret: clientSecret,
environment: environment
},
success: function (response) {
if (response.success) {
alert('API credentials valid');
} else {
alert('Error: ' + response.data.message);
}
},
error: function () {
alert('An error occurred.');
}
});
});
$('#reset-token-btn').on('click', function () {
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'reset_token',
},
success: function (response) {
if (response.success) {
alert('Token Reset Successful');
} else {
alert('Error: ' + response.data.message);
}
},
error: function () {
alert('An error occurred.');
}
});
});
});
</script>
</div>
<?php
}
// Add submenu page to the 'Settings' menu
function pt_hms_orders_page() {
add_submenu_page(
PTC_PLUGIN_SETTINGS_PAGE_TYPE,
'Orders',
'Orders',
'manage_options',
PTC_PLUGIN_PAGE_TYPE,
'pt_hms_pathao_courier_page_callback'
);
}
// Callback function to display the content of the submenu page
function pt_hms_pathao_courier_page_callback() {
echo '<div class="wrap">';
echo '<h2>Pathao Courier Order Page</h2>';
echo '<p>Manage your deliveries without any distraction</p>';
ob_start();
include_once PTC_PLUGIN_TEMPLATE_DIR . 'ptc-hms-list-template.php';
echo ob_get_clean();
echo '</div>';
}
// Admin init callback
function pt_hms_settings_init()
{
register_setting('pt_hms_settings_group', 'pt_hms_settings');
// API Credentials
add_settings_section('section_one', 'API Credentials', 'section_one_callback', 'pt_hms_settings');
add_settings_field('client_id', 'Client ID', 'field_client_id_callback', 'pt_hms_settings', 'section_one');
add_settings_field('client_secret', 'Client Secret', 'field_client_secret_callback', 'pt_hms_settings', 'section_one');
add_settings_field('environment', 'Environment', 'field_environment_callback', 'pt_hms_settings', 'section_one');
add_settings_field('client_webhook', 'Client Default Webhook', 'field_webhook_callback', 'pt_hms_settings', 'section_one');
add_settings_field('client_webhook_secret', 'Client Webhook Secret', 'field_webhook_secret_callback', 'pt_hms_settings', 'section_one');
}
function section_one_callback()
{
echo 'Enter your API credentials below:';
}
function field_client_id_callback()
{
$options = get_option('pt_hms_settings');
$value = is_array($options) && isset($options['client_id']) ? $options['client_id'] : '';
echo "<input type='text' id='client_id' name='pt_hms_settings[client_id]' value='{$value}' style='width: 300px;' />";
}
function field_client_secret_callback()
{
$options = get_option('pt_hms_settings');
$value = is_array($options) && isset($options['client_secret']) ? $options['client_secret'] : '';
echo "<input type='password' id='client_secret' name='pt_hms_settings[client_secret]' value='{$value}' style='width: 300px;' />";
}
function field_webhook_callback()
{
$baseUrl = get_site_url();
echo "{$baseUrl}/wp-json/ptc/v1/webhook";
echo "<p class='description'>
This is the default <a href=\"https://merchant.pathao.com/courier/developer-api\">webhook</a> URL that will be used for all orders.
</p>";
}
function field_webhook_secret_callback()
{
$options = get_option('pt_hms_settings');
$clientSecret = $options['client_secret'] ?? '';
$webhookSecret = $options['webhook_secret'] ?? '';
$value = $webhookSecret ? $webhookSecret : $clientSecret;
echo "<input type='text' name='pt_hms_settings[webhook_secret]' value='{$value}' style='width: 300px;' />";
echo "<p class='description'>
The default <a href=\"https://merchant.pathao.com/courier/developer-api\">webhook</a> secret will be your client secret if you don't provide any webhook secret.
</p>";
}
function field_username_callback()
{
$options = get_option('pt_hms_settings');
$value = is_array($options) && isset($options['username']) ? $options['username'] : '';
echo "<input type='text' name='pt_hms_settings[username]' value='{$value}' style='width: 300px;' />";
}
function field_password_callback()
{
$options = get_option('pt_hms_settings');
$value = is_array($options) && isset($options['password']) ? $options['password'] : '';
echo "<input type='password' name='pt_hms_settings[password]' value='{$value}' style='width: 300px;' />";
}
function field_environment_callback()
{
$options = get_option('pt_hms_settings');
$selected = is_array($options) && isset($options['environment']) ? $options['environment'] : '';
echo "<select name='pt_hms_settings[environment]' id='client_environment' style='width: 300px;'>
<option value='live' " . selected($selected, 'live', false) . ">Live</option>
<option value='staging' " . selected($selected, 'staging', false) . ">Staging</option>
</select>";
}
function field_default_store_callback()
{
$stores = pt_hms_get_stores(); // Assuming this function returns an array of stores
if (!$stores || !is_array($stores) || empty($stores)) {
echo "No stores found.";
return;
}
$options = get_option('pt_hms_settings');
$selected_store = is_array($options) && isset($options['default_store']) ? $options['default_store'] : '';
echo "<select name='pt_hms_settings[default_store]' style='width: 300px;'>";
foreach ($stores as $store) {
$selected = ($selected_store == $store['store_id']) ? 'selected' : '';
echo "<option value='{$store['store_id']}' $selected>{$store['store_name']}</option>";
}
echo "</select>";
}