| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Api; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
class DeleteData |
| 8 |
{ |
| 9 |
public $prefix = ''; |
| 10 |
public $param = ''; |
| 11 |
public $request = null; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
add_action('rest_api_init', function () { |
| 16 |
register_rest_route('emailkit/v1/delete-data', '(?P<post_id>\d+)', array( |
| 17 |
'methods' => \WP_REST_Server::ALLMETHODS, |
| 18 |
'callback' => [$this, 'delete_action'], |
| 19 |
'permission_callback' => '__return_true', |
| 20 |
)); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
public function delete_action($request) |
| 25 |
{ |
| 26 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 27 |
return [ |
| 28 |
'status' => 'fail', |
| 29 |
'message' => [ __( 'Nonce mismatch.', 'emailkit')] |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
if (!is_user_logged_in() || !current_user_can('publish_posts')) { |
| 34 |
return [ |
| 35 |
'status' => 'fail', |
| 36 |
'message' => [ __( 'Access denied.', 'emailkit') ] |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
$post_id = $request->get_param('post_id'); |
| 41 |
|
| 42 |
$deleted = wp_delete_post($post_id, true); |
| 43 |
|
| 44 |
if ($deleted === false) { |
| 45 |
return [ |
| 46 |
"status" => "failed", |
| 47 |
"message" => [ |
| 48 |
__( "Post Data not deleted ", "emailkit" ), |
| 49 |
], |
| 50 |
]; |
| 51 |
} else { |
| 52 |
return [ |
| 53 |
"status" => "success", |
| 54 |
"result" => $post_id, |
| 55 |
"message" => [ |
| 56 |
__( "Post data deleted successfully.", "emailkit" ), |
| 57 |
], |
| 58 |
]; |
| 59 |
} |
| 60 |
} |
| 61 |
} |