First step, you need to create a custom plugin. After that, create a file and paste this code:
<?php
if ( !defined( 'ABSPATH' ) ){ exit; }
if( class_exists( 'WPCARGO_API' ) ) {
class WPCARGO_CUSTOM_CONTAINER_API extends WPCARGO_API {
/**
* register new custom routes
*/
public function custom_container_routes() {
$base = 'api';
$namespace = $this->wpcargo_namespace.$this->wpcargo_api_version;
/**
* add container route
*/
register_rest_route( $namespace, '/' . $base.'/(?P[a-zA-Z0-9-]+)/container/add', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'add_container' ),
'permission_callback' => array( $this, 'wpcargo_api_premission' )
));
/**
* update container route
*/
register_rest_route( $namespace, '/' . $base.'/(?P[a-zA-Z0-9-]+)/container/update', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_container' ),
'permission_callback' => array( $this, 'wpcargo_api_premission' )
));
}
/**
* register the custom route
*/
function initialize_custom_routes(){
add_action( 'rest_api_init', array( $this, 'custom_container_routes' ) );
}
/**
* add container callback
*/
function add_container( $request ) {
$timezone = get_option('timezone_string');
date_default_timezone_set( $timezone );
global $wpcargo;
$response = array();
$user_id = (int)trim( wpcapi_user_id( $request->get_param( 'apikey' ) ) );
$con_title = $request->get_param( '_title' ) && !empty( $request->get_param( '_title' ) ) ? trim( $request->get_param( '_title' ) ) : '';
$agent_id = $request->get_param( '_agent_id' ) && !empty( $request->get_param( '_agent_id' ) ) ? (int)trim( $request->get_param( '_agent_id' ) ) : '';
$flight_num = $request->get_param( '_flight_number' ) && !empty( $request->get_param( '_flight_number' ) ) ? trim( $request->get_param( '_flight_number' ) ) : '';
$driver_id = $request->get_param( '_driver_id' ) && !empty( $request->get_param( '_driver_id' ) ) ? (int)trim( $request->get_param( '_driver_id' ) ) : '';
$con_status = $request->get_param( '_status' ) && !empty( $request->get_param( '_status' ) ) ? trim( $request->get_param( '_status' ) ) : '';
if( $con_title ) {
/**
* prepare arguments
*/
$args = array(
'post_title' => $con_title,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_id,
'post_type' => 'shipment_container'
);
/**
* check if container title already exists
*/
$post_checker = get_posts( array( 'post_type' => 'shipment_container', 'title' => $con_title ) );
if( empty( $post_checker ) ) {
$container_id = wp_insert_post( $args );
if( $container_id ) {
/**
* update post metas
*/
$assigned_agent = in_array( $agent_id, wpc_agent_ids() ) ? $agent_id : '';
$assigned_driver = in_array( $driver_id, wpc_driver_ids() ) ? $driver_id : '';
$status = in_array( $con_status, $wpcargo->status ) ? $con_status : '';
update_post_meta( $container_id, 'agent_fields', $assigned_agent );
update_post_meta( $container_id, 'wpcargo_driver', $assigned_driver );
update_post_meta( $container_id, 'container_no', $flight_num );
update_post_meta( $container_id, 'container_status', $status );
do_action( 'wpcargo_api_after_add_container', $container_id, $request );
$response = array(
'status' => 'success',
'container_id' => $container_id,
'container_title' => $con_title,
'message' => wpcapi_container_added_message()
);
}
} else {
$response = array(
'status' => 'error',
'message' => __( 'Container title already exists', 'wpcargo-customizer' )
);
}
} else {
$response = array(
'status' => 'error',
'message' => __( 'Missing container title', 'wpcargo-customizer' )
);
}
return $response;
}
/**
* update existing container callback
*/
function update_container( $request ) {
$timezone = get_option('timezone_string');
date_default_timezone_set( $timezone );
global $wpcargo;
$response = array();
$user_id = (int)trim( wpcapi_user_id( $request->get_param( 'apikey' ) ) );
$con_title = $request->get_param( '_title' ) && !empty( $request->get_param( '_title' ) ) ? trim( $request->get_param( '_title' ) ) : '';
$date = date( 'Y-m-d' );
$time = date( 'H:i a' );
$location = $request->get_param( '_location' ) && !empty( $request->get_param( '_location' ) ) ? trim( $request->get_param( '_location' ) ) : '';
$con_status = $request->get_param( '_status' ) && !empty( $request->get_param( '_status' ) ) ? trim( $request->get_param( '_status' ) ) : '';
$con_remark = $request->get_param( '_remarks' ) && !empty( $request->get_param( '_remarks' ) ) ? trim( $request->get_param( '_remarks' ) ) : '';
$updated_by = $wpcargo->user_fullname( $user_id );
$update_assigned_shipments = $request->get_param( '_update_assigned_shipments' ) && !empty( $request->get_param( '_update_assigned_shipments' ) ) ? $request->get_param( '_update_assigned_shipments' ) : '';
/**
* check if container title is set
*/
if( $con_title ) {
/**
* get container id by title
*
* prepare args
*/
$args_for_cont_id = array(
'post_type' => 'shipment_container',
'status' => 'publish',
'title' => $con_title,
'fields' => 'ids',
'author' => $user_id,
);
/**
* query the args to get the results
*/
$cont_id_result = get_posts( $args_for_cont_id );
/**
* get the container id
*/
$cont_id = (int)get_array_first_value( $cont_id_result );
/**
* check if container id exists
*/
if( $cont_id ) {
$prev_status = get_post_meta( $cont_id, 'container_status', true );
$curr_status = in_array( $con_status, $wpcargo->status ) ? $con_status : '';
if( $curr_status ) {
/**
* update container status
*/
update_post_meta( $cont_id, 'container_status', $curr_status );
/**
* check if "_update_assigned_shipments" is true
*/
if( $update_assigned_shipments ) {
/**
* prepare args to get ids of shipments assigned to container
*/
$args_for_assigned_shipment_ids = array(
'post_type' => 'wpcargo_shipment',
'status' => 'publish',
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'shipment_container',
'value' => $cont_id,
'compare' => '='
)
)
);
$shipment_ids = get_posts( $args_for_assigned_shipment_ids );
if( !empty( $shipment_ids ) && is_array( $shipment_ids ) ) {
foreach( $shipment_ids as $id ) {
$old_shipment_history = maybe_unserialize( get_post_meta( $id, 'wpcargo_shipments_update', true ) );
$temp_history = array();
if( !empty( wpcargo_history_fields() ) ) {
$row = array();
foreach( wpcargo_history_fields() as $key => $value ) {
$value = '';
if ( $key == 'date' ) {
if( $date ) { $value = $date; }
} elseif ( $key == 'time' ) {
if( $time ) { $value = $time; }
} elseif ( $key == 'location' ) {
if( $location ) { $value = $location; }
} elseif ( $key == 'remarks' ) {
if( $con_remark ) { $value = $con_remark; }
} elseif ( $key == 'status' ) {
if( $con_status ) { $value = $con_status; }
} elseif ( $key == 'updated-name' ) {
if( $updated_by ) { $value = $updated_by; }
}
$row[$key] = $value;
}
$temp_history[] = $row;
}
$new_shipment_history = array_merge( $old_shipment_history, $temp_history );
/**
* update shipment history
*/
update_post_meta( $id, 'wpcargo_shipments_update', maybe_serialize( $new_shipment_history ) );
/**
* update shipment status alone
*/
update_post_meta( $id, 'wpcargo_status', $con_status );
wpcargo_send_email_notificatio( $id, $con_status );
}
}
}
$response = array(
'status' => 'success',
'container_id' => $cont_id,
'container_title' => $con_title,
'old_status' => $prev_status,
'new_status' => $curr_status,
'message' => wpcapi_container_updated_message(),
'updated_by' => $updated_by
);
} else {
$response = array(
'status' => 'error',
'message' => __('Select a valid status. Check your wpcargo status list', 'wpcargo-customizer')
);
}
} else {
$response = array(
'status' => 'error',
'message' => __('Container title doesn\'t exist', 'wpcargo-customizer')
);
}
} else {
$response = array(
'status' => 'error',
'message' => __( 'Missing container title', 'wpcargo-customizer' )
);
}
return $response;
}
}
$custom_container_route = new WPCARGO_CUSTOM_CONTAINER_API;
$custom_container_route->initialize_custom_routes();
}
/**
* get all wpcargo agent ids
*/
function wpc_agent_ids() {
$query=get_users(array(
'role' => 'cargo_agent',
'fields' => 'ID'
));
return $query;
}
/**
* get all wpcargo driver ids
*/
function wpc_driver_ids() {
$query=get_users(array(
'role' => 'wpcargo_driver',
'fields' => 'ID'
));
return $query;
}
/**
* container creation success message
*/
function wpcapi_container_added_message(){
return __('Container added', 'wpcargo-customizer' );
}
/**
* container update success message
*/
function wpcapi_container_updated_message(){
return __('Container updated', 'wpcargo-customizer' );
}
/**
* get array first value
*/
function get_array_first_value( $array ) {
if( !is_array( $array ) ) { return; }
foreach( $array as $value ) { return $value; }
}