Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions includes/Api/class-api-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public function register_routes(): void
new Automation_Controller,
new Dashboard_Controller,
new Api_Token_Controller,

new Ticket_Snooze_Controller,

new Ticket_Split_Controller,

];

foreach ($controllers as $controller) {
Expand Down
136 changes: 136 additions & 0 deletions includes/Api/class-ticket-snooze-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

/**
* Ticket Snooze Controller - REST API endpoints for snoozing/unsnoozing tickets.
*/

namespace Escalated\Api;

use Escalated\Services\TicketSnoozeService;
use WP_REST_Request;
use WP_REST_Server;

class Ticket_Snooze_Controller extends Base_Controller
{
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'tickets';

/**
* Regex pattern for ticket ID parameter.
*
* @var string
*/
private const ID_PATTERN = '(?P<id>[\d]+)';

/**
* Register ticket snooze routes.
*/
public function register_routes(): void
{
// Snooze a ticket.
register_rest_route(
$this->namespace,
'/'.$this->rest_base.'/'.self::ID_PATTERN.'/snooze',
[
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'snooze'],
'permission_callback' => [$this, 'token_permissions_check'],
'args' => [
'id' => [
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
],
'until' => [
'required' => true,
'type' => 'string',
'description' => __('Datetime to snooze until (Y-m-d H:i:s).', 'escalated'),
],
],
],
]
);

// Unsnooze a ticket.
register_rest_route(
$this->namespace,
'/'.$this->rest_base.'/'.self::ID_PATTERN.'/unsnooze',
[
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'unsnooze'],
'permission_callback' => [$this, 'token_permissions_check'],
'args' => [
'id' => [
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
],
],
],
]
);
}

/**
* Snooze a ticket.
*
* @param WP_REST_Request $request The incoming request.
* @return \WP_REST_Response|\WP_Error
*/
public function snooze(WP_REST_Request $request)
{
$ticket_id = (int) $request->get_param('id');
$until = sanitize_text_field($request->get_param('until'));
$user_id = $this->check_token_permission($request, 'ticket.edit');

if ($user_id === null) {
return $this->error('escalated_unauthorized', __('Insufficient permissions.', 'escalated'), 403);
}

try {
$service = new TicketSnoozeService;
$ticket = $service->snooze_ticket($ticket_id, $until, $user_id);

return $this->success([
'message' => __('Ticket snoozed successfully.', 'escalated'),
'ticket' => $ticket,
]);
} catch (\InvalidArgumentException $e) {
return $this->error('escalated_snooze_failed', $e->getMessage());
}
}

/**
* Unsnooze a ticket.
*
* @param WP_REST_Request $request The incoming request.
* @return \WP_REST_Response|\WP_Error
*/
public function unsnooze(WP_REST_Request $request)
{
$ticket_id = (int) $request->get_param('id');
$user_id = $this->check_token_permission($request, 'ticket.edit');

if ($user_id === null) {
return $this->error('escalated_unauthorized', __('Insufficient permissions.', 'escalated'), 403);
}

try {
$service = new TicketSnoozeService;
$ticket = $service->unsnooze_ticket($ticket_id, $user_id);

return $this->success([
'message' => __('Ticket unsnoozed successfully.', 'escalated'),
'ticket' => $ticket,
]);
} catch (\InvalidArgumentException $e) {
return $this->error('escalated_unsnooze_failed', $e->getMessage());
}
}
}
25 changes: 25 additions & 0 deletions includes/Cron/class-snooze-check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Escalated\Cron;

use Escalated\Services\TicketSnoozeService;

class Snooze_Check
{
/**
* Register the cron hook.
*/
public function register(): void
{
add_action('escalated_check_snoozed_tickets', [$this, 'run']);
}

/**
* Check for snoozed tickets that need to be woken up.
*/
public function run(): void
{
$service = new TicketSnoozeService;
$service->wake_snoozed_tickets();
}
}
Loading
Loading