← All changes
|
includes/Actions/Controller/TicketActions/CreateTicketItem.php
+56
-6
1.9.2
→
1.10.20
View file →
| @@ -4,8 +4,10 @@ | ||
| 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 CreateTicketItem extends ActionController |
| @@ -11,24 +13,72 @@ | ||
| 11 | 13 | class CreateTicketItem extends ActionController |
| 12 | 14 | { |
| 13 | 15 | public function __invoke() |
| 14 | 16 | { |
| 15 | - $ticketManager = new TicketServiceManager(); | |
| 17 | + if (!current_user_can('manage_options')) { | |
| 18 | + wp_die('دسترسی غیرمجاز!'); | |
| 19 | + } | |
| 16 | 20 | |
| 17 | - $content = isset($_POST['content']) ? \sanitize_textarea_field(\wp_unslash($_POST['content'])) : ''; | |
| 18 | - $content = TicketExtraInfoFormatter::appendFromRequest($content, $_POST); | |
| 21 | + try { | |
| 22 | + $this->processSubmission(); | |
| 23 | + } catch (\Throwable $e) { | |
| 24 | + $message = $e instanceof \InvalidArgumentException || $e instanceof \RuntimeException | |
| 25 | + ? $e->getMessage() | |
| 26 | + : 'خطای غیرمنتظرهای در ارسال پاسخ تیکت رخ داد. لطفا مجددا تلاش کنید.'; | |
| 27 | + TicketFlashNotice::push($message, 'error'); | |
| 28 | + | |
| 29 | + $ticketId = isset($_POST['ticket_id']) ? intval(wp_unslash($_POST['ticket_id'])) : 0; | |
| 30 | + $redirectUrl = $ticketId > 0 | |
| 31 | + ? admin_url('admin.php?page=sync_basalam_ticket&ticket_id=' . $ticketId) | |
| 32 | + : admin_url('admin.php?page=sync_basalam_tickets'); | |
| 33 | + wp_safe_redirect($redirectUrl); | |
| 34 | + exit(); | |
| 35 | + } | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** Run reply creation before the optional access replacement. */ | |
| 39 | + public function processSubmission(?TicketServiceManager $ticketManager = null): void | |
| 40 | + { | |
| 41 | + $ticketManager = $ticketManager ?: new TicketServiceManager(); | |
| 42 | + | |
| 43 | + $content = isset($_POST['content']) ? TicketExtraInfoFormatter::sanitizeContent($_POST['content']) : ''; | |
| 44 | + try { | |
| 45 | + $accessData = TicketAccessData::fromRequest($_POST); | |
| 46 | + } catch (\InvalidArgumentException $e) { | |
| 47 | + throw $e; | |
| 48 | + } | |
| 49 | + if ($content === '' && !empty($accessData)) { | |
| 50 | + $content = 'اطلاعات دسترسی بهروزرسانی شد.'; | |
| 51 | + } | |
| 19 | 52 | $ticketId = isset($_POST['ticket_id']) ? intval(\wp_unslash($_POST['ticket_id'])) : 0; |
| 20 | 53 | |
| 21 | - if ($ticketId <= 0) return; | |
| 54 | + if ($ticketId <= 0) { | |
| 55 | + throw new \RuntimeException('شناسه تیکت معتبر نیست.'); | |
| 56 | + } | |
| 22 | 57 | |
| 23 | 58 | $ticketResponse = $ticketManager->fetchTicket($ticketId); |
| 24 | 59 | $ticket = isset($ticketResponse['body']) ? json_decode($ticketResponse['body'], true) : null; |
| 25 | 60 | |
| 26 | - if (is_array($ticket) && TicketServiceManager::isTicketClosed($ticket)) return; | |
| 61 | + if (is_array($ticket) && TicketServiceManager::isTicketClosed($ticket)) { | |
| 62 | + throw new \RuntimeException('این تیکت بسته شده است و امکان ارسال پاسخ جدید وجود ندارد.'); | |
| 63 | + } | |
| 27 | 64 | |
| 28 | 65 | $fileIds = isset($_POST['file_ids']) && is_array($_POST['file_ids']) |
| 29 | 66 | ? array_map('intval', $_POST['file_ids']) |
| 30 | 67 | : []; |
| 31 | 68 | |
| 32 | - $ticketManager->createTicketItem($ticketId, $content, $fileIds); | |
| 69 | + $itemResult = $ticketManager->createTicketItem($ticketId, $content, $fileIds); | |
| 70 | + if (!TicketServiceManager::isSuccessful($itemResult)) { | |
| 71 | + throw new \RuntimeException('خطایی در ارسال پاسخ تیکت رخ داد. لطفا مجددا تلاش کنید.'); | |
| 72 | + } | |
| 73 | + | |
| 74 | + if (!empty($accessData)) { | |
| 75 | + $accessResult = $ticketManager->upsertTicketSiteAccess($ticketId, $accessData); | |
| 76 | + if (!TicketServiceManager::isSuccessful($accessResult)) { | |
| 77 | + throw new \RuntimeException( | |
| 78 | + 'پاسخ تیکت ثبت شد، اما اطلاعات دسترسی ذخیره نشد. ' | |
| 79 | + . 'لطفا دوباره اطلاعات دسترسی را ارسال کنید.' | |
| 80 | + ); | |
| 81 | + } | |
| 82 | + } | |
| 33 | 83 | } |
| 34 | 84 | } |