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