emails = $emails; $this->runner = $runner; } /** * Registers the routes. * * @since 0.1.0 * * @return void */ public function register_routes() { $permission = Permissions::require_capability( Capabilities::MANAGE_SETTINGS ); $base = '/' . $this->rest_base; register_rest_route( $this->namespace, $base, array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => $permission, 'args' => array_merge( $this->list_params( array( 'id' ), 'id' ), array( 'view' => array( 'type' => 'string', 'default' => 'queue', 'enum' => array( 'queue', 'log' ), ), 'status' => array( 'type' => 'string', 'default' => '', 'enum' => array( '', 'pending', 'sending', 'failed', 'sent' ), ), ) ), ) ); register_rest_route( $this->namespace, $base . '/process', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'process' ), 'permission_callback' => $permission, ) ); register_rest_route( $this->namespace, $base . '/(?P\d+)/retry', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'retry' ), 'permission_callback' => $permission, ) ); register_rest_route( $this->namespace, $base . '/(?P\d+)', array( 'methods' => WP_REST_Server::DELETABLE, 'callback' => array( $this, 'delete_item' ), 'permission_callback' => $permission, ) ); } /** * Lists the queue or the log. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_items( $request ) { $args = array( 'search' => (string) $request['search'], 'status' => (string) $request['status'], 'page' => (int) $request['page'], 'per_page' => (int) $request['per_page'], ); $result = 'log' === $request['view'] ? $this->emails->log_entries( $args ) : $this->emails->queue( $args ); $items = array_map( function ( array $email ) { return array( 'id' => $email['id'], 'to_email' => $email['to_email'], 'to_name' => $email['to_name'] ?? '', 'subject' => $email['subject'], 'template_key' => $email['template_key'], 'related_type' => $email['related_type'], 'related_id' => $email['related_id'], 'status' => $email['status'], 'attempts' => $email['attempts'] ?? 0, 'error' => $email['last_error'] ?? $email['error'] ?? '', 'send_after' => isset( $email['send_after_gmt'] ) ? str_replace( ' ', 'T', (string) $email['send_after_gmt'] ) . 'Z' : '', 'created_at' => str_replace( ' ', 'T', (string) $email['created_at_gmt'] ) . 'Z', ); }, $result['items'] ); $response = $this->collection_response( $items, $result['total'], $request ); $response->header( 'X-HostKhata-Status-Counts', wp_json_encode( $this->emails->queue_counts() ) ); return $response; } /** * Sends a batch now instead of waiting for WP-Cron. * * @since 0.1.0 * * @return WP_REST_Response */ public function process(): WP_REST_Response { return rest_ensure_response( $this->runner->run() ); } /** * Puts a failed email back on the queue. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function retry( $request ) { if ( ! $this->runner->retry( (int) $request['id'] ) ) { return $this->error( 'hostkhata_not_failed', __( 'Only a failed email can be retried.', 'hostkhata' ), 409 ); } return rest_ensure_response( array( 'queued' => true ) ); } /** * Removes a failed email from the queue. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function delete_item( $request ) { $email = $this->emails->find( (int) $request['id'] ); if ( null === $email ) { return $this->error( 'hostkhata_not_found', __( 'Email not found.', 'hostkhata' ), 404 ); } if ( 'failed' !== $email['status'] ) { return $this->error( 'hostkhata_not_failed', __( 'Only a failed email can be removed.', 'hostkhata' ), 409 ); } $this->emails->delete( $email['id'] ); return rest_ensure_response( array( 'deleted' => true ) ); } }