| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Api; |
| 4 |
use EmailKit\Admin\TemplateList; |
| 5 |
|
| 6 |
defined('ABSPATH') || exit; |
| 7 |
|
| 8 |
class TemplateTypesData { |
| 9 |
|
| 10 |
public $prefix = ''; |
| 11 |
public $param = ''; |
| 12 |
public $request = null; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
|
| 17 |
add_action('rest_api_init', function () { |
| 18 |
register_rest_route('emailkit/v1', 'template-types-data/(?P<template_type>\w+)', array( |
| 19 |
'methods' => 'GET', |
| 20 |
'callback' => [$this, 'get_template_data'], |
| 21 |
'permission_callback' => '__return_true', |
| 22 |
)); |
| 23 |
}); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_template_data($request) { |
| 27 |
|
| 28 |
if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) { |
| 29 |
return [ |
| 30 |
'status' => 'fail', |
| 31 |
'message' => [ __( 'Nonce mismatch.', 'emailkit' ) ] |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
if (!is_user_logged_in() || !current_user_can('publish_posts')) { |
| 36 |
return [ |
| 37 |
'status' => 'fail', |
| 38 |
'message' => [ __('Access denied.', 'emailkit') ] |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
$template_type = $request->get_param('template_type'); |
| 44 |
|
| 45 |
// Retrieve template list from TemplateList class |
| 46 |
$template_list = TemplateList::get_templates(); |
| 47 |
|
| 48 |
// Find the templates matching the provided template type |
| 49 |
$matching_templates = []; |
| 50 |
foreach ($template_list as $template) { |
| 51 |
if ($template['title'] === $template_type) { |
| 52 |
$matching_templates[] = $template; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if (empty($matching_templates)) { |
| 57 |
return [ |
| 58 |
"status" => "fail", |
| 59 |
"message" => [ |
| 60 |
__( "No templates found for the provided template type.", 'emailkit' ), |
| 61 |
], |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
$templates_data = []; |
| 66 |
foreach ($matching_templates as $matching_template) { |
| 67 |
|
| 68 |
$json_content = file_get_contents($matching_template['file']); |
| 69 |
|
| 70 |
// Decode the JSON content |
| 71 |
$template_object = json_decode($json_content, true); |
| 72 |
|
| 73 |
$post_creation_date = get_the_date('Y-m-d H:i:s', $matching_template['id']); |
| 74 |
|
| 75 |
$template_data = array( |
| 76 |
'date' => $post_creation_date, |
| 77 |
'package' => $matching_template['package'], |
| 78 |
'mail_type' => $matching_template['mail_type'], |
| 79 |
'id' => $matching_template['id'], |
| 80 |
'template_title' => $matching_template['title'], |
| 81 |
'object' => $template_object, |
| 82 |
'template_thumbnail' => $matching_template['preview-thumb'], |
| 83 |
); |
| 84 |
|
| 85 |
$templates_data[] = $template_data; |
| 86 |
} |
| 87 |
|
| 88 |
return [ |
| 89 |
"status" => "success", |
| 90 |
"data" => $templates_data, |
| 91 |
"message" => [ |
| 92 |
__( "Template files retrieved successfully.", 'emailkit' ), |
| 93 |
], |
| 94 |
]; |
| 95 |
|
| 96 |
} |
| 97 |
} |