| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Order; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 |
|
| 7 |
use Booktics\Contracts\Hookable_Service_Contract; |
| 8 |
use Booktics\Models\Appointment_Model; |
| 9 |
|
| 10 |
class Order_Hooks implements Hookable_Service_Contract { |
| 11 |
|
| 12 |
/** |
| 13 |
* Register the hooks for the order model. |
| 14 |
* |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
public function register() { |
| 18 |
add_action( 'booktics_order_filters', [ |
| 19 |
$this, |
| 20 |
'team_member_order_filters' |
| 21 |
] ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Filter order conditions based on the current user's role. |
| 26 |
* |
| 27 |
* @param array $conditions The order conditions. |
| 28 |
* |
| 29 |
* @return array The filtered order conditions. |
| 30 |
*/ |
| 31 |
public function team_member_order_filters( $conditions ) { |
| 32 |
$current_user = wp_get_current_user(); |
| 33 |
$roles = $current_user->roles; |
| 34 |
|
| 35 |
if ( in_array( 'administrator', $roles, true ) ) { |
| 36 |
return $conditions; |
| 37 |
} |
| 38 |
|
| 39 |
if ( in_array( 'booktics_team_member', $roles, true ) ) { |
| 40 |
$order_ids = $this->get_order_ids(); |
| 41 |
$conditions['id'] = [ 'in', $order_ids ]; |
| 42 |
} |
| 43 |
|
| 44 |
return $conditions; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get order ids for a date range |
| 49 |
* |
| 50 |
* @param array $dates |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
private function get_order_ids() { |
| 55 |
$order_ids = ['null']; |
| 56 |
$args = []; |
| 57 |
$args = apply_filters( 'booktics_appointment_args', $args ); |
| 58 |
$appointments = ( new Appointment_Model() )->all( $args ); |
| 59 |
foreach ( $appointments as $appointment ) { |
| 60 |
$order_ids[] = $appointment->order_id; |
| 61 |
} |
| 62 |
|
| 63 |
return $order_ids; |
| 64 |
} |
| 65 |
} |