| 1 |
<?php |
| 2 |
namespace Elementor\Core\Common\Modules\EventTracker\Data; |
| 3 |
|
| 4 |
use Elementor\Core\Common\Modules\EventTracker\DB as Events_DB_Manager; |
| 5 |
use Elementor\Plugin; |
| 6 |
use WP_REST_Server; |
| 7 |
use Elementor\Data\V2\Base\Controller as Controller_Base; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Controller extends Controller_Base { |
| 14 |
|
| 15 |
public function get_name() { |
| 16 |
return 'send-event'; |
| 17 |
} |
| 18 |
|
| 19 |
public function register_endpoints() { |
| 20 |
$this->index_endpoint->register_items_route( \WP_REST_Server::CREATABLE, [ |
| 21 |
'event_data' => [ |
| 22 |
'description' => 'All the recorded event data in JSON format', |
| 23 |
'type' => 'object', |
| 24 |
'required' => true, |
| 25 |
], |
| 26 |
] ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get Permissions Callback |
| 31 |
* |
| 32 |
* This endpoint should only accept POST requests, and currently we only track site administrator actions. |
| 33 |
* |
| 34 |
* @since 3.6.0 |
| 35 |
* |
| 36 |
* @param \WP_REST_Request $request |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public function get_permission_callback( $request ) { |
| 40 |
if ( WP_REST_Server::CREATABLE !== $request->get_method() ) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
return current_user_can( 'manage_options' ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Create Items |
| 49 |
* |
| 50 |
* Receives a request for adding an event data entry into the database. If the request contains event data, this |
| 51 |
* method initiates creation of a database entry with the event data in the Events DB table. |
| 52 |
* |
| 53 |
* @since 3.6.0 |
| 54 |
* |
| 55 |
* @param \WP_REST_Request $request |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public function create_items( $request ) { |
| 59 |
$request_body = $request->get_json_params(); |
| 60 |
|
| 61 |
if ( empty( $request_body['event_data'] ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
/** @var Events_DB_Manager $event_tracker_db_manager */ |
| 66 |
$event_tracker_db_manager = Plugin::$instance->common |
| 67 |
->get_component( 'event-tracker' ) |
| 68 |
->get_component( 'events-db' ); |
| 69 |
|
| 70 |
$event_tracker_db_manager->create_entry( $request_body['event_data'] ); |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
} |
| 75 |
|