| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Admin\Controllers; |
| 4 |
|
| 5 |
use ElementorOne\Admin\Config; |
| 6 |
use ElementorOne\LogStore; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Internal logs REST API (elementor-one host only). |
| 14 |
*/ |
| 15 |
class Logs extends \WP_REST_Controller { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
$this->namespace = Config::APP_REST_NAMESPACE; |
| 22 |
$this->rest_base = 'logs'; |
| 23 |
|
| 24 |
add_action( 'rest_api_init', [ $this, 'register_routes' ] ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function register_routes() { |
| 31 |
register_rest_route( |
| 32 |
$this->namespace, |
| 33 |
'/' . $this->rest_base, |
| 34 |
[ |
| 35 |
[ |
| 36 |
'methods' => \WP_REST_Server::READABLE, |
| 37 |
'callback' => [ $this, 'get_logs' ], |
| 38 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 39 |
], |
| 40 |
[ |
| 41 |
'methods' => \WP_REST_Server::DELETABLE, |
| 42 |
'callback' => [ $this, 'clear_logs' ], |
| 43 |
'permission_callback' => [ $this, 'permissions_check' ], |
| 44 |
], |
| 45 |
] |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param \WP_REST_Request $request |
| 51 |
* @return \WP_REST_Response|\WP_Error |
| 52 |
*/ |
| 53 |
public function get_logs( \WP_REST_Request $request ) { |
| 54 |
return new \WP_REST_Response( |
| 55 |
[ |
| 56 |
'entries' => LogStore::instance()->all_newest_first(), |
| 57 |
] |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @param \WP_REST_Request $request |
| 63 |
* @return \WP_REST_Response|\WP_Error |
| 64 |
*/ |
| 65 |
public function clear_logs( \WP_REST_Request $request ) { |
| 66 |
LogStore::instance()->clear(); |
| 67 |
|
| 68 |
return new \WP_REST_Response( |
| 69 |
[ |
| 70 |
'success' => true, |
| 71 |
] |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @param \WP_REST_Request $request |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function permissions_check( \WP_REST_Request $request ) { |
| 80 |
return current_user_can( 'manage_options' ); |
| 81 |
} |
| 82 |
} |
| 83 |
|