| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\admin\api; |
| 4 |
|
| 5 |
use Leadin\api\Base_Api_Controller; |
| 6 |
use Leadin\admin\Connection; |
| 7 |
use Leadin\data\Portal_Options; |
| 8 |
|
| 9 |
/** |
| 10 |
* Portal Api, used to clean portal id and domain from the WordPress options. |
| 11 |
*/ |
| 12 |
class Portal_Api_Controller extends Base_Api_Controller { |
| 13 |
|
| 14 |
/** |
| 15 |
* Class constructor, register route. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
self::register_leadin_admin_route( |
| 19 |
'/portal', |
| 20 |
\WP_REST_Server::DELETABLE, |
| 21 |
array( $this, 'disconnect_portal' ) |
| 22 |
); |
| 23 |
self::register_leadin_admin_route( |
| 24 |
'/business-unit', |
| 25 |
\WP_REST_Server::READABLE, |
| 26 |
array( $this, 'get_business_unit_option' ) |
| 27 |
); |
| 28 |
self::register_leadin_admin_route( |
| 29 |
'/business-unit', |
| 30 |
\WP_REST_Server::EDITABLE, |
| 31 |
array( $this, 'set_business_unit_option' ) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Removes portal id and domain from the WordPress options. |
| 37 |
*/ |
| 38 |
public function disconnect_portal() { |
| 39 |
if ( Connection::is_connected() ) { |
| 40 |
Connection::disconnect(); |
| 41 |
return new \WP_REST_Response( 'OK', 200 ); |
| 42 |
} |
| 43 |
return new \WP_REST_Response( 'No leadin_portal_id found, cannot disconnect', 400 ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get business unit id option. |
| 48 |
* |
| 49 |
* @return string Business Unit Id |
| 50 |
*/ |
| 51 |
public function get_business_unit_option() { |
| 52 |
return new \WP_REST_Response( Portal_Options::get_business_unit_id(), 200 ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Set business unit id option. |
| 57 |
* |
| 58 |
* @param number $request Request body. |
| 59 |
* |
| 60 |
* @return string OK response message. |
| 61 |
*/ |
| 62 |
public function set_business_unit_option( $request ) { |
| 63 |
$data = json_decode( $request->get_body(), true ); |
| 64 |
$business_unit_id = $data['businessUnitId']; |
| 65 |
Portal_Options::set_business_unit_id( $business_unit_id ); |
| 66 |
return new \WP_REST_Response( 'OK', 200 ); |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|