| 1 |
<?php |
| 2 |
/** |
| 3 |
* Api reports class |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Reports; |
| 9 |
|
| 10 |
use Timetics\Base\Api; |
| 11 |
use Timetics\Utils\Singleton; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Api Reports |
| 15 |
*/ |
| 16 |
class Api_Report extends Api { |
| 17 |
use Singleton; |
| 18 |
|
| 19 |
/** |
| 20 |
* Store api namespace |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $namespace = 'timetics/v1'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Store rest base |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $rest_base = 'reports'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Register rest routes |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function register_routes() { |
| 39 |
/** |
| 40 |
* Register route |
| 41 |
* |
| 42 |
* @var void |
| 43 |
*/ |
| 44 |
register_rest_route( |
| 45 |
$this->namespace, $this->rest_base . '/overview', [ |
| 46 |
[ |
| 47 |
'methods' => \WP_REST_Server::READABLE, |
| 48 |
'callback' => [$this, 'get_overview'], |
| 49 |
'permission_callback' => function () { |
| 50 |
return current_user_can( 'manage_timetics' ); |
| 51 |
}, |
| 52 |
], |
| 53 |
] |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get overview |
| 59 |
* |
| 60 |
* @param WP_Rest_Request |
| 61 |
* |
| 62 |
* @return JSON |
| 63 |
*/ |
| 64 |
public function get_overview( $request ) { |
| 65 |
$start_date = isset( $request['start_date'] ) ? sanitize_text_field( $request['start_date'] ) : ''; |
| 66 |
|
| 67 |
$end_date = isset( $request['end_date'] ) ? sanitize_text_field( $request['end_date'] ) : ''; |
| 68 |
|
| 69 |
$data = [ |
| 70 |
'total_meetings' => timetics_count_posts( ['post_type' => 'timetics-appointment'] ), |
| 71 |
'total_bookings' => timetics_count_posts( ['post_type' => 'timetics-booking'] ), |
| 72 |
'total_sales' => number_format(timetics_get_total_sale(), 2), |
| 73 |
'total_customer' => timetics_count_users( ['role' => 'timetics-customer'] ), |
| 74 |
'total_staff' => timetics_count_users( ['role' => 'timetics-staff'] ), |
| 75 |
]; |
| 76 |
|
| 77 |
$response = [ |
| 78 |
'status_code' => 200, |
| 79 |
'success' => 1, |
| 80 |
'data' => $data, |
| 81 |
]; |
| 82 |
|
| 83 |
return rest_ensure_response( $response ); |
| 84 |
} |
| 85 |
} |
| 86 |
|