| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Admin\Controllers; |
| 4 |
|
| 5 |
use Elementor\WPNotificationsPackage\V120\Notifications as NotificationsSDK; |
| 6 |
use ElementorOne\Admin\Services\Feedback as FeedbackService; |
| 7 |
use ElementorOne\Admin\Config; |
| 8 |
use ElementorOne\Common\RestError; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class TopBar |
| 16 |
* Handles all top bar-related REST API endpoints |
| 17 |
*/ |
| 18 |
class TopBar extends \WP_REST_Controller { |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
$this->namespace = Config::APP_REST_NAMESPACE; |
| 25 |
$this->rest_base = 'top-bar'; |
| 26 |
|
| 27 |
add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Register all notifications-related routes |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function register_routes() { |
| 35 |
// GET /top-bar/notifications |
| 36 |
register_rest_route( |
| 37 |
$this->namespace, |
| 38 |
'/' . $this->rest_base . '/notifications', |
| 39 |
[ |
| 40 |
[ |
| 41 |
'methods' => \WP_REST_Server::READABLE, |
| 42 |
'callback' => [ $this, 'get_notifications' ], |
| 43 |
'permission_callback' => [ $this, 'create_item_permissions_check' ], |
| 44 |
'args' => [ |
| 45 |
'app_name' => [ |
| 46 |
'type' => 'string', |
| 47 |
'required' => true, |
| 48 |
], |
| 49 |
'app_version' => [ |
| 50 |
'type' => 'string', |
| 51 |
'required' => true, |
| 52 |
], |
| 53 |
], |
| 54 |
], |
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
// POST /top-bar/feedback |
| 59 |
register_rest_route( |
| 60 |
$this->namespace, |
| 61 |
'/' . $this->rest_base . '/feedback', |
| 62 |
[ |
| 63 |
[ |
| 64 |
'methods' => \WP_REST_Server::CREATABLE, |
| 65 |
'callback' => [ $this, 'send_product_feedback' ], |
| 66 |
'permission_callback' => [ $this, 'create_item_permissions_check' ], |
| 67 |
'args' => [ |
| 68 |
'product' => [ |
| 69 |
'type' => 'string', |
| 70 |
'required' => true, |
| 71 |
], |
| 72 |
'subject' => [ |
| 73 |
'type' => 'string', |
| 74 |
'required' => true, |
| 75 |
], |
| 76 |
'title' => [ |
| 77 |
'type' => 'string', |
| 78 |
'required' => true, |
| 79 |
], |
| 80 |
'description' => [ |
| 81 |
'type' => 'string', |
| 82 |
'required' => true, |
| 83 |
], |
| 84 |
'countryCode' => [ |
| 85 |
'type' => 'string', |
| 86 |
'required' => false, |
| 87 |
], |
| 88 |
], |
| 89 |
], |
| 90 |
] |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Get notifications |
| 96 |
* @param \WP_REST_Request $request |
| 97 |
* @return \WP_REST_Response|\WP_Error |
| 98 |
*/ |
| 99 |
public function get_notifications( \WP_REST_Request $request ) { |
| 100 |
try { |
| 101 |
$app_name = $request->get_param( 'app_name' ); |
| 102 |
$app_version = $request->get_param( 'app_version' ); |
| 103 |
|
| 104 |
$notifications_sdk = new NotificationsSDK( [ |
| 105 |
'app_name' => $app_name, |
| 106 |
'app_version' => $app_version, |
| 107 |
] ); |
| 108 |
|
| 109 |
$notifications = $notifications_sdk->get_notifications_by_conditions( true ); |
| 110 |
|
| 111 |
return new \WP_REST_Response( $notifications ); |
| 112 |
} catch ( \Throwable $th ) { |
| 113 |
return RestError::internal_server_error( $th->getMessage() ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Send feedback |
| 119 |
* @param \WP_REST_Request $request |
| 120 |
* @return \WP_REST_Response|\WP_Error |
| 121 |
*/ |
| 122 |
public function send_product_feedback( \WP_REST_Request $request ) { |
| 123 |
try { |
| 124 |
$product = $request->get_param( 'product' ); |
| 125 |
$subject = $request->get_param( 'subject' ); |
| 126 |
$title = $request->get_param( 'title' ); |
| 127 |
$description = $request->get_param( 'description' ); |
| 128 |
$country_code = $request->get_param( 'countryCode' ); |
| 129 |
|
| 130 |
FeedbackService::instance()->send_product_feedback( $product, $subject, $title, $description, $country_code ); |
| 131 |
|
| 132 |
return new \WP_REST_Response( null, \WP_Http::NO_CONTENT ); |
| 133 |
} catch ( \Throwable $th ) { |
| 134 |
if ( \WP_Http::UNAUTHORIZED === $th->getCode() ) { |
| 135 |
return RestError::unauthorized( $th->getMessage() ); |
| 136 |
} |
| 137 |
return RestError::bad_request( $th->getMessage() ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if the user has permission to create an item |
| 143 |
* @param \WP_REST_Request $request |
| 144 |
* @return true|\WP_Error |
| 145 |
*/ |
| 146 |
public function create_item_permissions_check( $request ) { |
| 147 |
return current_user_can( 'manage_options' ); |
| 148 |
} |
| 149 |
} |
| 150 |
|