| 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 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class CreateTicketItem extends ActionController |
| 13 |
{ |
| 14 |
public function __invoke() |
| 15 |
{ |
| 16 |
if (!current_user_can('manage_options')) { |
| 17 |
wp_die('دسترسی غیر� |
| 18 |
جاز!'); |
| 19 |
} |
| 20 |
|
| 21 |
$this->processSubmission(); |
| 22 |
} |
| 23 |
|
| 24 |
/** Run reply creation before the optional access replacement. */ |
| 25 |
public function processSubmission(?TicketServiceManager $ticketManager = null): void |
| 26 |
{ |
| 27 |
$ticketManager = $ticketManager ?: new TicketServiceManager(); |
| 28 |
|
| 29 |
$content = isset($_POST['content']) ? TicketExtraInfoFormatter::sanitizeContent($_POST['content']) : ''; |
| 30 |
try { |
| 31 |
$accessData = TicketAccessData::fromRequest($_POST); |
| 32 |
} catch (\InvalidArgumentException $e) { |
| 33 |
wp_die(esc_html($e->getMessage())); |
| 34 |
} |
| 35 |
if ($content === '' && !empty($accessData)) { |
| 36 |
$content = 'اطلاعات دسترسی بهروزرسانی شد.'; |
| 37 |
} |
| 38 |
$ticketId = isset($_POST['ticket_id']) ? intval(\wp_unslash($_POST['ticket_id'])) : 0; |
| 39 |
|
| 40 |
if ($ticketId <= 0) return; |
| 41 |
|
| 42 |
$ticketResponse = $ticketManager->fetchTicket($ticketId); |
| 43 |
$ticket = isset($ticketResponse['body']) ? json_decode($ticketResponse['body'], true) : null; |
| 44 |
|
| 45 |
if (is_array($ticket) && TicketServiceManager::isTicketClosed($ticket)) return; |
| 46 |
|
| 47 |
$fileIds = isset($_POST['file_ids']) && is_array($_POST['file_ids']) |
| 48 |
? array_map('intval', $_POST['file_ids']) |
| 49 |
: []; |
| 50 |
|
| 51 |
$itemResult = $ticketManager->createTicketItem($ticketId, $content, $fileIds); |
| 52 |
if (!TicketServiceManager::isSuccessful($itemResult)) { |
| 53 |
wp_die('خطایی در ارسال پاسخ تیکت رخ داد. لطفا � |
| 54 |
جددا تلاش کنید.'); |
| 55 |
} |
| 56 |
|
| 57 |
if (!empty($accessData)) { |
| 58 |
$accessResult = $ticketManager->upsertTicketSiteAccess($ticketId, $accessData); |
| 59 |
if (!TicketServiceManager::isSuccessful($accessResult)) { |
| 60 |
wp_die(esc_html( |
| 61 |
'پاسخ تیکت ثبت شد، ا� |
| 62 |
ا اطلاعات دسترسی ذخیره نشد. ' |
| 63 |
. 'لطفا دوباره اطلاعات دسترسی را ارسال کنید.' |
| 64 |
)); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|