PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.9
EmailKit – Email Customizer for WooCommerce & WP v1.6.9
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Api / TemplateTypesData.php

TemplateTypesData.php in EmailKit – Email Customizer for WooCommerce & WP 1.6.9, at includes/Admin/Api/TemplateTypesData.php

116 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\Api;
4
5 use EmailKit\Admin\TemplateList;
6 use EmailKit\Admin\Emails\Helpers\Utils;
7
8 defined('ABSPATH') || exit;
9
10 class TemplateTypesData {
11
12 public $prefix = '';
13 public $param = '';
14 public $request = null;
15
16 public function __construct()
17 {
18
19 add_action('rest_api_init', function () {
20 register_rest_route('emailkit/v1', 'template-types-data/(?P<template_type>\w+)', array(
21 'methods' => 'GET',
22 'callback' => [$this, 'get_template_data'],
23 'permission_callback' => '__return_true',
24 ));
25 });
26 }
27
28 public function get_template_data($request) {
29
30 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
31 return [
32 'status' => 'fail',
33 'message' => [ __( 'Nonce mismatch.', 'emailkit' ) ]
34 ];
35 }
36
37 if (!is_user_logged_in() || !current_user_can('manage_options')) {
38 return [
39 'status' => 'fail',
40 'message' => [ __('Access denied.', 'emailkit') ]
41 ];
42 }
43
44
45 $template_type = $request->get_param('template_type');
46
47 // Retrieve template list from TemplateList class
48 $template_list = TemplateList::get_templates();
49
50 // Find the templates matching the provided template type
51 $matching_templates = [];
52 foreach ($template_list as $template) {
53 if ($template['title'] === $template_type) {
54 $matching_templates[] = $template;
55 }
56 }
57
58 if (empty($matching_templates)) {
59 return [
60 "status" => "fail",
61 "message" => [
62 __( "No templates found for the provided template type.", 'emailkit' ),
63 ],
64 ];
65 }
66
67 $templates_data = [];
68 foreach ($matching_templates as $matching_template) {
69 // Check if the file path is empty and the package is 'pro'
70 if (empty($matching_template['file']) && $matching_template['package'] === 'pro') {
71 $template_data = array(
72 'date' => get_the_date('Y-m-d H:i:s', $matching_template['id']),
73 'package' => $matching_template['package'],
74 'mail_type' => $matching_template['mail_type'],
75 'id' => $matching_template['id'],
76 'template_title' => $matching_template['title'],
77 'object' => '', // No template object since the file is empty
78 'template_thumbnail' => $matching_template['preview-thumb'], // Only include the thumbnail
79 );
80
81 $templates_data[] = $template_data;
82 continue; // Skip further processing for this template
83 }
84
85 // Continue processing templates with a valid file path
86 if (!empty($matching_template['file'])) {
87 $json_content = file_get_contents($matching_template['file']);
88 $json_content = Utils::normalize_template_asset_urls($json_content);
89 $template_string = stripslashes($json_content);
90 $template_object = json_decode($template_string, true);
91
92 $template_data = array(
93 'date' => get_the_date('Y-m-d H:i:s', $matching_template['id']),
94 'package' => $matching_template['package'],
95 'mail_type' => $matching_template['mail_type'],
96 'id' => $matching_template['id'],
97 'template_title' => $matching_template['title'],
98 'object' => $template_object,
99 'template_thumbnail' => $matching_template['preview-thumb'],
100 );
101
102 $templates_data[] = $template_data;
103 }
104 }
105
106 return [
107 "status" => "success",
108 "data" => $templates_data,
109 "message" => [
110 __( "Template files retrieved successfully.", 'emailkit' ),
111 ],
112 ];
113
114 }
115
116 }