-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCore.php
More file actions
343 lines (292 loc) · 11.3 KB
/
Core.php
File metadata and controls
343 lines (292 loc) · 11.3 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/*
Plugin Name: پلاگین اشتراک سفارشات ووکامرس در ایتا
Plugin URI: https://hamidarab.ir
Description: پلاگین اشتراک گذاری سفارشات ووکامرسی در ایتا - توسعه دهنده حمید اعراب
Author: حمید اعراب
Version: 2.0.0
Licence: GPLv2 or Later
Author URI: https://hamidarab.ir
*/
defined('ABSPATH') || exit;
define('EITA_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('EITA_PLUGIN_URL', plugin_dir_url(__FILE__));
require_once EITA_PLUGIN_DIR . '/class/EitaaAPI.php';
//EitaaAPI::send_order_to_eitaa_group();
add_action('admin_init', 'eitaa_register_settings');
add_action('admin_menu', 'eitaa_add_settings_page');
add_action('update_option_eitaa_api_token', 'eitaa_handle_credentials_change', 10, 3);
add_action('update_option_eitaa_chat_id', 'eitaa_handle_credentials_change', 10, 3);
add_action('add_option_eitaa_api_token', 'eitaa_handle_credentials_change_on_add', 10, 2);
add_action('add_option_eitaa_chat_id', 'eitaa_handle_credentials_change_on_add', 10, 2);
add_action('admin_bar_menu', 'eitaa_add_toolbar_status_node', 100);
add_action('admin_head', 'eitaa_toolbar_inline_styles');
add_action('wp_head', 'eitaa_toolbar_inline_styles');
function eitaa_register_settings()
{
register_setting(
'eitaa_settings_group',
'eitaa_api_token',
[
'sanitize_callback' => 'eitaa_encode_setting_value',
]
);
register_setting(
'eitaa_settings_group',
'eitaa_chat_id',
[
'sanitize_callback' => 'eitaa_encode_setting_value',
]
);
}
function eitaa_add_settings_page()
{
add_menu_page(
'تنظیمات ایتا',
'ایتــا',
'manage_options',
'eitaa-settings',
'eitaa_render_settings_page',
'dashicons-format-chat'
);
}
function eitaa_render_settings_page()
{
if (!current_user_can('manage_options')) {
return;
}
$api_token = eitaa_get_decoded_option_value('eitaa_api_token');
$chat_id = eitaa_get_decoded_option_value('eitaa_chat_id');
$status = wp_parse_args(
EitaaAPI::get_api_status(false),
[
'status' => 'unknown',
'label' => 'نامشخص',
'description' => '',
]
);
?>
<div class="wrap">
<h1>تنظیمات اتصال ایتا</h1>
<form method="post" action="options.php">
<?php settings_fields('eitaa_settings_group'); ?>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="eitaa_api_token">توکن ربات</label></th>
<td>
<input type="text" class="regular-text" id="eitaa_api_token" name="eitaa_api_token" value="<?php echo esc_attr($api_token); ?>" autocomplete="off" />
<p class="description">توکن دریافتی از وبسرویس ایتا را وارد کنید.</p>
</td>
</tr>
<tr>
<th scope="row"><label for="eitaa_chat_id">شناسه چت</label></th>
<td>
<input type="text" class="regular-text" id="eitaa_chat_id" name="eitaa_chat_id" value="<?php echo esc_attr($chat_id); ?>" autocomplete="off" />
<p class="description">شناسه گروه یا کانالی که اطلاعرسانی باید به آن ارسال شود.</p>
</td>
</tr>
</table>
<?php submit_button('ذخیره تنظیمات'); ?>
</form>
<h2>وضعیت اتصال</h2>
<p>
وضعیت :
<span>
<?php echo wp_kses_post(eitaa_get_status_badge_html($status)); ?>
</span>
</p>
<?php if (!empty($status['description']) && 'connected' !== $status['status']) : ?>
<p class="description"><?php echo esc_html($status['description']); ?></p>
<?php endif; ?>
</div>
<?php
}
function eitaa_get_decoded_option_value($option_name)
{
$value = get_option($option_name, '');
if (empty($value)) {
return '';
}
$decoded = base64_decode($value, true);
return $decoded !== false ? $decoded : $value;
}
function eitaa_encode_setting_value($value)
{
if (!is_string($value)) {
return '';
}
$value = trim(wp_unslash($value));
if ('' === $value) {
return '';
}
$sanitized = base64_encode(sanitize_text_field($value));
EitaaAPI::flush_status_cache();
return $sanitized;
}
function eitaa_get_status_badge_html($status)
{
$status_key = isset($status['status']) ? $status['status'] : 'unknown';
$label = isset($status['label']) ? $status['label'] : 'نامشخص';
$color = '#555d66';
if ('connected' === $status_key) {
$color = '#1a7f1a';
} elseif ('disconnected' === $status_key) {
$color = '#d63638';
} elseif ('error' === $status_key) {
$color = '#d63638';
} elseif ('not_configured' === $status_key) {
$color = '#dba617';
}
$class = 'eitaa-status-badge eitaa-status-' . sanitize_html_class($status_key);
return sprintf(
'<span class="%s" style="font-weight:600;color:%s;">%s</span>',
esc_attr($class),
esc_attr($color),
esc_html($label)
);
}
function eitaa_handle_credentials_change($old_value, $value, $option)
{
unset($old_value, $value, $option);
EitaaAPI::flush_status_cache();
}
function eitaa_handle_credentials_change_on_add($option, $value)
{
unset($option, $value);
EitaaAPI::flush_status_cache();
}
function eitaa_add_toolbar_status_node($wp_admin_bar)
{
if (!is_user_logged_in() || !is_admin_bar_showing() || !current_user_can('manage_options')) {
return;
}
$status = wp_parse_args(
EitaaAPI::get_api_status(),
[
'status' => 'unknown',
'label' => 'نامشخص',
'description' => '',
]
);
$badge_html = wp_kses_post(eitaa_get_status_badge_html($status));
$title = sprintf(
'<span class="ab-icon dashicons dashicons-testimonial" style="font-family: dashicons !important;"></span><span class="ab-label">%s</span>%s',
esc_html__('وضعیت ایتا', 'wp-eitaa'),
$badge_html
);
$tooltip = !empty($status['description'])
? wp_strip_all_tags($status['description'])
: esc_html__('تنظیمات اتصال ایتا', 'wp-eitaa');
$wp_admin_bar->add_node(
[
'id' => 'eitaa-status',
'parent' => 'top-secondary',
'title' => $title,
'href' => admin_url('admin.php?page=eitaa-settings'),
'meta' => [
'class' => 'eitaa-toolbar-status-node',
'title' => $tooltip,
],
]
);
}
function eitaa_toolbar_inline_styles()
{
if (!is_user_logged_in() || !is_admin_bar_showing()) {
return;
}
echo '<style id="eitaa-toolbar-status-styles">#wp-admin-bar-eitaa-status > .ab-item{display:flex;align-items:center;gap:6px;}#wp-admin-bar-eitaa-status .eitaa-status-badge{margin-inline-start:6px;}</style>';
}
add_action('woocommerce_order_status_processing', 'EitaaAPI::send_order_to_eitaa_group', 10);
// add_action('woocommerce_admin_order_data_after_order_details', 'EitaaAPI::send_order_to_eitaa_group', 10);
add_action('woocommerce_admin_order_data_after_order_details', 'eitaa_order_button');
function eitaa_order_button($order) {
$order_id = $order->get_id();
$nonce = wp_create_nonce('send_order_to_eitaa_nonce');
?>
<script>
jQuery(document).ready(function($) {
$('.send-order-to-eitaa').on('click', function(e) {
e.preventDefault();
var $button = $(this);
var order_id = $button.data('order-id');
var originalText = $button.text();
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'send_order_to_eitaa',
order_id: order_id,
nonce: '<?php echo $nonce; ?>'
},
beforeSend: function() {
$button.prop('disabled', true).text('در حال ارسال...');
},
success: function(response) {
var defaultSuccess = 'سفارش با موفقیت به ایتا ارسال شد.';
var defaultError = 'ارسال سفارش با خطا مواجه شد.';
var message = defaultSuccess;
if (response && typeof response === 'object') {
if (response.success) {
if (response.data && response.data.message) {
message = response.data.message;
}
} else {
message = defaultError;
if (response.data) {
message = response.data.message ? response.data.message : response.data;
}
}
}
alert(message);
},
error: function(xhr) {
var message = 'ارسال سفارش با خطا مواجه شد.';
if (xhr && xhr.responseJSON && xhr.responseJSON.data) {
var data = xhr.responseJSON.data;
message = data.message ? data.message : data;
} else if (xhr && xhr.responseText) {
message = xhr.responseText;
}
alert(message);
},
complete: function() {
$button.prop('disabled', false).text(originalText);
}
});
});
});
</script>
<button class="button button-primary send-order-to-eitaa" data-order-id="<?php echo $order_id; ?>">
ارسال به ایتا
</button>
<?php
}
add_action('wp_ajax_send_order_to_eitaa', 'send_order_to_eitaa_function');
function send_order_to_eitaa_function() {
check_ajax_referer('send_order_to_eitaa_nonce', 'nonce');
if (isset($_POST['order_id'])) {
$order_id = intval($_POST['order_id']);
if (class_exists('EitaaAPI')) {
$result = EitaaAPI::send_order_to_eitaa_group($order_id);
if (is_wp_error($result)) {
wp_send_json_error([
'message' => $result->get_error_message(),
]);
}
$success_message = isset($result['message']) ? $result['message'] : __('سفارش با موفقیت به ایتا ارسال شد.', 'wp-eitaa');
wp_send_json_success([
'message' => $success_message,
]);
} else {
wp_send_json_error([
'message' => __('کلاس ایتا در دسترس نیست.', 'wp-eitaa'),
]);
}
} else {
wp_send_json_error([
'message' => __('شناسه سفارش ارسال نشده است.', 'wp-eitaa'),
]);
}
wp_die();
}