| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Notification; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 11 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 12 |
use IvyForms\Common\Exceptions\NotFoundException; |
| 13 |
use IvyForms\Common\Exceptions\QueryExecutionException; |
| 14 |
use IvyForms\Common\Exceptions\ValidationException; |
| 15 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 16 |
use IvyForms\Controllers\Controller; |
| 17 |
use IvyForms\Factory\Notification\NotificationFactory; |
| 18 |
use IvyForms\Services\Notification\NotificationService; |
| 19 |
use IvyForms\Services\Translations\BackendStrings; |
| 20 |
use WP_REST_Request; |
| 21 |
use WP_REST_Response; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class DuplicateNotificationController |
| 25 |
* |
| 26 |
* @package IvyForms\Controllers\Notification |
| 27 |
*/ |
| 28 |
class DuplicateNotificationController extends Controller |
| 29 |
{ |
| 30 |
private NotificationService $notificationService; |
| 31 |
|
| 32 |
public function __construct(NotificationService $notificationService) |
| 33 |
{ |
| 34 |
$this->notificationService = $notificationService; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param WP_REST_Request $data |
| 39 |
* |
| 40 |
* @return WP_REST_Response |
| 41 |
* |
| 42 |
* @throws QueryExecutionException |
| 43 |
* @throws InvalidArgumentException |
| 44 |
* @throws NotFoundException |
| 45 |
* @throws ValidationException |
| 46 |
* @throws ForbiddenException |
| 47 |
*/ |
| 48 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 49 |
{ |
| 50 |
// Verify the nonce |
| 51 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 52 |
// Validate request data |
| 53 |
$notificationId = Sanitizer::sanitizeId($data->get_param('id')); |
| 54 |
if (empty($notificationId)) { |
| 55 |
throw new InvalidArgumentException( |
| 56 |
BackendStrings::getExceptionStrings()['notification_id_required'] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
// Retrieve the original notification |
| 61 |
$originalNotification = $this->notificationService->getNotificationById($notificationId); |
| 62 |
|
| 63 |
// Duplicate the notification's parameters, excluding the ID |
| 64 |
$newNotificationData = $originalNotification->toArray(); |
| 65 |
unset($newNotificationData['id']); // Exclude the ID |
| 66 |
$newNotificationData['name'] .= ' (Copy)'; // Append "(Copy)" to the name |
| 67 |
$newNotification = NotificationFactory::create($newNotificationData); |
| 68 |
|
| 69 |
// Save the new notification to the database |
| 70 |
$newNotificationId = $this->notificationService->createNotification($newNotification); |
| 71 |
if (!$newNotificationId) { |
| 72 |
throw new QueryExecutionException( |
| 73 |
BackendStrings::getSettingsFormBuilderStrings()['failed_to_duplicate_notification' . '.'] |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
// Set the new notification ID |
| 78 |
$newNotification->setId($newNotificationId); |
| 79 |
|
| 80 |
return new WP_REST_Response([ |
| 81 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 82 |
'data' => $newNotification->toArray() |
| 83 |
], 200); |
| 84 |
} |
| 85 |
} |
| 86 |
|