PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.19
ووسلام – همگام سازی ووکامرس و باسلام v1.10.19
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
← All changes | includes/Actions/Controller/TicketActions/CreateTicket.php +95 -9 1.10.31.10.19 View file →
@@ -4,32 +4,118 @@
4 4
5 5 use SyncBasalam\Actions\Controller\ActionController;
6 6 use SyncBasalam\Services\TicketServiceManager;
7 7 use SyncBasalam\Utilities\TicketExtraInfoFormatter;
8 +use SyncBasalam\Utilities\TicketAccessData;
9 +use SyncBasalam\Utilities\TicketFlashNotice;
8 10
9 11 defined('ABSPATH') || exit;
10 12
11 13 class CreateTicket extends ActionController
12 14 {
15 + private $createdTicketId = 0;
16 +
13 17 public function __invoke()
14 18 {
15 - $ticketManager = new TicketServiceManager();
19 + if (!current_user_can('manage_options')) {
20 + if (wp_doing_ajax()) {
21 + wp_send_json_error(['message' => 'دسترسی غیرمجاز!'], 403);
22 + }
23 + wp_die('دسترسی غیرمجاز!');
24 + }
16 25
26 + try {
27 + $ticketId = $this->processSubmission();
28 + } catch (\Throwable $e) {
29 + $message = $e instanceof \InvalidArgumentException || $e instanceof \RuntimeException
30 + ? $e->getMessage()
31 + : 'خطای غیرمنتظره‌ای در ثبت تیکت رخ داد. لطفا مجددا تلاش کنید.';
32 +
33 + if (wp_doing_ajax()) {
34 + wp_send_json_error([
35 + 'message' => $message,
36 + 'ticket_id' => $this->createdTicketId,
37 + ], 400);
38 + }
39 +
40 + TicketFlashNotice::push($message, 'error');
41 +
42 + $redirectUrl = $this->createdTicketId > 0
43 + ? admin_url('admin.php?page=sync_basalam_ticket&ticket_id=' . $this->createdTicketId)
44 + : admin_url('admin.php?page=sync_basalam_new_ticket');
45 + wp_safe_redirect($redirectUrl);
46 + exit();
47 + }
48 +
49 + $redirectUrl = admin_url('admin.php?page=sync_basalam_ticket&ticket_id=' . $ticketId);
50 + if (wp_doing_ajax()) {
51 + wp_send_json_success([
52 + 'ticket_id' => $ticketId,
53 + 'redirect_url' => $redirectUrl,
54 + ]);
55 + }
56 +
57 + wp_safe_redirect($redirectUrl);
58 + exit();
59 + }
60 +
61 + /**
62 + * Run the two-step submission without redirecting.
63 + *
64 + * The optional manager keeps the action flow directly testable while the
65 + * normal WordPress invocation still creates the production manager.
66 + */
67 + public function processSubmission(?TicketServiceManager $ticketManager = null): int
68 + {
69 + $ticketManager = $ticketManager ?: new TicketServiceManager();
70 +
17 71 $title = isset($_POST['title']) ? \sanitize_text_field(\wp_unslash($_POST['title'])) : null;
18 72 $subject = isset($_POST['subject']) ? \sanitize_text_field(\wp_unslash($_POST['subject'])) : null;
19 - $content = isset($_POST['content']) ? \sanitize_textarea_field(\wp_unslash($_POST['content'])) : '';
20 - $content = TicketExtraInfoFormatter::appendFromRequest($content, $_POST);
73 + $content = isset($_POST['content']) ? TicketExtraInfoFormatter::sanitizeContent($_POST['content']) : '';
21 74
75 + try {
76 + $accessData = TicketAccessData::fromRequest($_POST);
77 + } catch (\InvalidArgumentException $e) {
78 + throw $e;
79 + }
80 +
22 81 $fileIds = isset($_POST['file_ids']) && is_array($_POST['file_ids'])
23 82 ? array_map('intval', $_POST['file_ids'])
24 83 : [];
25 84
26 - $result = $ticketManager->createTicket($title, $subject, $content, $fileIds);
27 - if (isset($result['body'])) $ticket = json_decode($result['body'], true);
28 - else {
29 - wp_die('خطایی در ارسال تیکت رخ داده است. لطفا مجددا تلاش کنید.');
85 + $existingTicketId = isset($_POST['existing_ticket_id'])
86 + ? intval(wp_unslash($_POST['existing_ticket_id']))
87 + : 0;
88 + if ($existingTicketId > 0) {
89 + if (empty($accessData)) {
90 + throw new \RuntimeException('اطلاعات دسترسی برای ارسال مجدد کامل نیست.');
91 + }
92 + $ticketId = $existingTicketId;
93 + $this->createdTicketId = $ticketId;
94 + } else {
95 + $result = $ticketManager->createTicket($title, $subject, $content, $fileIds);
96 + if (TicketServiceManager::isSuccessful($result) && isset($result['body'])) {
97 + $ticket = json_decode($result['body'], true);
98 + } else {
99 + throw new \RuntimeException('خطایی در ارسال تیکت رخ داده است. لطفا مجددا تلاش کنید.');
100 + }
101 +
102 + $ticketId = intval($ticket['data']['id'] ?? 0);
103 + if ($ticketId <= 0) {
104 + throw new \RuntimeException('پاسخ ایجاد تیکت معتبر نیست. لطفا مجددا تلاش کنید.');
105 + }
106 + $this->createdTicketId = $ticketId;
30 107 }
31 108
32 - wp_safe_redirect(admin_url("admin.php?page=sync_basalam_ticket&ticket_id=" . $ticket['data']['id']));
33 - exit();
109 + if (!empty($accessData)) {
110 + $accessResult = $ticketManager->upsertTicketSiteAccess($ticketId, $accessData);
111 + if (!TicketServiceManager::isSuccessful($accessResult)) {
112 + throw new \RuntimeException(
113 + 'تیکت شماره ' . $ticketId . ' ثبت شد، اما اطلاعات دسترسی ذخیره نشد. '
114 + . 'لطفا از صفحه تیکت دوباره اطلاعات دسترسی را ارسال کنید.'
115 + );
116 + }
117 + }
118 +
119 + return $ticketId;
34 120 }
35 121 }