Tribe__Tickets__Editor__REST__V1__Endpoints__Single_ticket::add_ticket( WP_REST_Request $request, $nonce_action )
Add ticket callback executed to update / add a new ticket.
Contents
Parameters
- $request
-
(WP_REST_Request) (Required)
- $nonce_action
-
(Required)
Return
(WP_Error|WP_REST_Response)
Source
File: src/Tribe/Editor/REST/V1/Endpoints/Single_Ticket.php
public function add_ticket( WP_REST_Request $request, $nonce_action ) {
$ticket_id = empty( $request['id'] ) ? null : $request['id'];
$provider_name = empty( $request['provider'] ) ? null : $request['provider'];
// Merge the defaults to avoid usage of `empty` values
$body = array_merge(
array( 'tribe-ticket' => array() ),
$request->get_default_params(),
$request->get_body_params()
);
$nonce = $body[ $nonce_action ];
$post_id = $body['post_id'];
if ( ! $this->has_permission( $post_id, $nonce, $nonce_action ) ) {
return new WP_Error(
'forbidden',
__( 'Invalid nonce', 'event-tickets' ),
array( 'status' => 403 )
);
}
if ( $ticket_id === null && $provider_name !== null ) {
$provider = call_user_func( array( $provider_name, 'get_instance' ) );
} else {
$provider = tribe_tickets_get_ticket_provider( $ticket_id );
}
if ( ! $provider ) {
return new WP_Error(
'bad_request',
__( 'Commerce Module invalid', 'event-tickets' ),
array( 'status' => 400 )
);
}
$ticket_args = $this->ticket_args();
$ticket_data = array(
'ticket_name' => $body['name'],
'ticket_description' => $body['description'],
'ticket_price' => $body['price'],
'ticket_show_description' => Tribe__Utils__Array::get( $body, 'show_description', $ticket_args['show_description']['default'] ),
'ticket_start_date' => $body['start_date'],
'ticket_start_time' => $body['start_time'],
'ticket_end_date' => $body['end_date'],
'ticket_end_time' => $body['end_time'],
'ticket_sku' => $body['sku'],
'ticket_menu_order' => $body['menu_order'],
'tribe-ticket' => $body['ticket'],
);
if ( $ticket_id !== null ) {
$ticket_data['ticket_id'] = $ticket_id;
}
// Get the Ticket Object
$ticket = $provider->ticket_add( $post_id, $ticket_data );
if ( ! $ticket ) {
return new WP_Error(
'not_acceptable',
__( 'Ticket was not able to be updated', 'event-tickets' ),
array( 'status' => 406 )
);
}
do_action( 'tribe_tickets_ticket_added', $post_id );
$response = new WP_REST_Response( $this->get_readable_ticket_data( $ticket ) );
$response->set_status( 202 );
return $response;
}
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Added support for sale price for Tickets Commerce. |
| 5.6.5 | Validates if price is greater than 0 when provider is PayPal or Tickets Commerce |
| 4.9 | |
| 4.12.3 | Update detecting ticket provider to account for possibly inactive provider. |
| 4.10.9 | Introduced. |