Skip to content

Commit

Permalink
feat: add store contact rest api (#680)
Browse files Browse the repository at this point in the history
* feat: add store contact rest api

* fix: increase refund table data length
  • Loading branch information
saimonh3 authored and sabbir1991 committed Nov 7, 2019
1 parent 116bbd3 commit 6b4dfe2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
6 changes: 3 additions & 3 deletions classes/installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ function create_refund_table() {
`seller_id` bigint(20) NOT NULL,
`refund_amount` float(11) NOT NULL,
`refund_reason` text NULL,
`item_qtys` varchar(50) NULL,
`item_totals` varchar(50) NULL,
`item_tax_totals` varchar(50) NULL,
`item_qtys` varchar(200) NULL,
`item_totals` varchar(200) NULL,
`item_tax_totals` varchar(200) NULL,
`restock_items` varchar(10) NULL,
`date` timestamp NOT NULL,
`status` int(1) NOT NULL,
Expand Down
84 changes: 84 additions & 0 deletions includes/api/class-store-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@ public function register_routes() {
'callback' => [ $this, 'check_store_availability' ]
]
] );

register_rest_route( $this->namespace, '/' . $this->base . '/(?P<id>[\d]+)/contact' , array(
'args' => [
'id' => [
'description' => __( 'Unique identifier for the object.', 'dokan-lite' ),
'type' => 'integer',
],
],
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'send_email' ),
'args' => [
'name' => [
'type' => 'string',
'description' => __( 'Your Name', 'dokan-lite' ),
'required' => true
],
'email' => [
'type' => 'string',
'format' => 'email',
'description' => __( 'Your email', 'dokan-lite' ),
'required' => true
],
'message' => [
'type' => 'string',
'description' => __( 'Your Message', 'dokan-lite' ),
'required' => true
]
]
],
) );
}

/**
Expand Down Expand Up @@ -643,4 +674,57 @@ public function check_store_availability( $request ) {

return rest_ensure_response( [] );
}

/**
* Send email to vendor
*
* @since DOKAN_LITE_SINCE
*
* @param WP_REST_Request
*
* @return WP_REST_Response
*/
public function send_email( $request ) {
$params = $request->get_params();
$vendor = dokan()->vendor->get( $params['id'] );

if ( ! $vendor instanceof Dokan_Vendor || ! $vendor->get_id() ) {
return rest_ensure_response(
new WP_Error(
'vendor_not_found',
__( 'No vendor is found to be send an email.', 'dokan-lite' ),
[
'status' => 404
]
)
);
}

/**
* Fires before sedning email to vendor via the REST API.
*
* @since DOKAN_LITE_SINCE
*
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating object, false when updating.
*/
do_action( "dokan_rest_pre_{$this->base}_send_email", $request, true );

$vendor_email = $vendor->get_email();
$sender_name = $params['name'];
$sender_email = $params['email'];
$sender_message = $params['message'];

WC()->mailer()->emails['Dokan_Email_Contact_Seller']->trigger( $vendor_email, $sender_name, $sender_email, $sender_message );

$response = apply_filters( "dokan_{$this->base}_send_email_response", [
'store_id' => $vendor->get_id(),
'data' => __( 'Email sent successfully!', 'dokan-lite' ),
'sender_name' => $sender_name,
'sender_email' => $sender_email,
'sender_message' => $sender_message,
] );

return rest_ensure_response( $response );
}
}

0 comments on commit 6b4dfe2

Please sign in to comment.