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