| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Api; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
use WP_Query; |
| 8 |
|
| 9 |
class FetchData |
| 10 |
{ |
| 11 |
|
| 12 |
public $prefix = ''; |
| 13 |
public $param = ''; |
| 14 |
public $request = null; |
| 15 |
|
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
add_action('rest_api_init', function () { |
| 20 |
register_rest_route('emailkit/v1', 'fetch-data', array( |
| 21 |
'methods' => \WP_REST_Server::ALLMETHODS, |
| 22 |
'callback' => [$this, 'action'], |
| 23 |
'permission_callback' => '__return_true', |
| 24 |
)); |
| 25 |
}); |
| 26 |
} |
| 27 |
|
| 28 |
public function action($request) |
| 29 |
{ |
| 30 |
|
| 31 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 32 |
return [ |
| 33 |
'status' => 'fail', |
| 34 |
'message' => ['Nonce mismatch.'] |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
if (!is_user_logged_in() || !current_user_can('publish_posts')) { |
| 39 |
return [ |
| 40 |
'status' => 'fail', |
| 41 |
'message' => ['Access denied.'] |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
$args = array( |
| 46 |
|
| 47 |
'post_type' => 'emailkit', |
| 48 |
'post_status' => 'publish', |
| 49 |
'orderby' => 'date', |
| 50 |
'order' => 'DESC', |
| 51 |
'posts_per_page' => -1, |
| 52 |
|
| 53 |
); |
| 54 |
|
| 55 |
|
| 56 |
$loop = new \WP_Query($args); |
| 57 |
|
| 58 |
$email_data = []; |
| 59 |
|
| 60 |
while ($loop->have_posts()) : $loop->the_post(); |
| 61 |
|
| 62 |
$email_data[] = [ |
| 63 |
"id" => get_the_ID(), |
| 64 |
"date" => get_the_date('Y-m-d H:i:s'), |
| 65 |
"object" => get_post_meta(get_the_ID(), 'emailkit_template_content_object', true), |
| 66 |
"html" => get_post_meta(get_the_ID(), 'emailkit_template_content_html', true), |
| 67 |
'emailkit_template_type' => get_post_meta(get_the_ID(),'emailkit_template_status', true), |
| 68 |
'emailkit_template_status' => get_post_meta(get_the_ID(),'emailkit_template_status', true), |
| 69 |
|
| 70 |
]; |
| 71 |
|
| 72 |
endwhile; |
| 73 |
|
| 74 |
wp_reset_postdata(); |
| 75 |
|
| 76 |
return [ |
| 77 |
"status" => "success", |
| 78 |
"data" => [ |
| 79 |
"history" => $email_data, |
| 80 |
], |
| 81 |
"message" => [ |
| 82 |
"Email list has been fetched successfully.", |
| 83 |
], |
| 84 |
]; |
| 85 |
} |
| 86 |
} |