| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Integrations; |
| 4 |
|
| 5 |
/** |
| 6 |
* Dokan Integration Class |
| 7 |
*/ |
| 8 |
class Dokan { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'woocommerce_order_status_changed', [ $this, 'order_status_changed' ], 99, 4 ); |
| 15 |
add_action( 'dokan_new_seller_created', [ $this, 'update_vendor_phone' ], 35, 2 ); |
| 16 |
add_action( 'dokan_store_profile_saved', [ $this, 'update_vendor_phone' ], 35, 2 ); |
| 17 |
add_action( 'texty_register_notifications', [ $this, 'register_notifications' ] ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Register Dokan notification types. |
| 22 |
* |
| 23 |
* @param \Texty\Notifications $notifications The notifications manager |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function register_notifications( $notifications ) { |
| 28 |
if ( ! class_exists( 'WeDevs_Dokan' ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$namespace = 'Texty\Notifications\Dokan\\'; |
| 33 |
|
| 34 |
$notifications->register( 'order_dokan_processing', $namespace . 'ProcessingVendor' ); |
| 35 |
$notifications->register( 'order_dokan_complete', $namespace . 'CompleteVendor' ); |
| 36 |
$notifications->register( 'order_dokan_cancelled', $namespace . 'CancelledVendor' ); |
| 37 |
$notifications->register( 'order_dokan_failed', $namespace . 'FailedVendor' ); |
| 38 |
$notifications->register( 'order_dokan_refunded', $namespace . 'RefundedVendor' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Send a message when an order status changes |
| 43 |
* |
| 44 |
* @param int $order_id |
| 45 |
* @param string $old_status |
| 46 |
* @param string $order_status |
| 47 |
* @param WC_Order $order |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function order_status_changed( $order_id, $old_status, $order_status, $order ) { |
| 52 |
$vendor_id = $this->vendor_id( $order ); |
| 53 |
|
| 54 |
if ( ! $vendor_id ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
switch ( $order_status ) { |
| 59 |
case 'processing': |
| 60 |
$this->send( 'order_dokan_processing', $order, $vendor_id ); |
| 61 |
break; |
| 62 |
|
| 63 |
case 'completed': |
| 64 |
$this->send( 'order_dokan_complete', $order, $vendor_id ); |
| 65 |
break; |
| 66 |
|
| 67 |
case 'cancelled': |
| 68 |
$this->send( 'order_dokan_cancelled', $order, $vendor_id ); |
| 69 |
break; |
| 70 |
|
| 71 |
case 'failed': |
| 72 |
$this->send( 'order_dokan_failed', $order, $vendor_id ); |
| 73 |
break; |
| 74 |
|
| 75 |
case 'refunded': |
| 76 |
$this->send( 'order_dokan_refunded', $order, $vendor_id ); |
| 77 |
break; |
| 78 |
|
| 79 |
default: |
| 80 |
// code... |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Save texty phone number when a seller add/update their phone number |
| 87 |
* |
| 88 |
* @param int $user_id |
| 89 |
* @param array $settings |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function update_vendor_phone( $user_id, $settings ) { |
| 94 |
if ( ! isset( $settings['phone'] ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
update_user_meta( $user_id, 'texty_phone', $settings['phone'] ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get the vendor ID from order |
| 103 |
* |
| 104 |
* @param WC_Order $order |
| 105 |
* |
| 106 |
* @return int |
| 107 |
*/ |
| 108 |
protected function vendor_id( $order ) { |
| 109 |
return (int) $order->get_meta( '_dokan_vendor_id' ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Send notification by event |
| 114 |
* |
| 115 |
* @param string $event |
| 116 |
* @param WC_Order $order |
| 117 |
* @param int $vendor_id |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
private function send( $event, $order, $vendor_id ) { |
| 122 |
$class = texty()->notifications()->get( $event ); |
| 123 |
$notification = new $class(); |
| 124 |
|
| 125 |
$notification->set_order( $order ); |
| 126 |
$notification->set_vendor( $vendor_id ); |
| 127 |
$notification->send(); |
| 128 |
} |
| 129 |
} |
| 130 |
|