PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.0
EmailKit – Email Customizer for WooCommerce & WP v1.6.0
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.0, at includes/Admin/Api/TemplateTypesData.php

112 lines 4.2 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 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 // Check if the file path is empty and the package is 'pro'
68 if (empty($matching_template['file']) && $matching_template['package'] === 'pro') {
69 $template_data = array(
70 'date' => get_the_date('Y-m-d H:i:s', $matching_template['id']),
71 'package' => $matching_template['package'],
72 'mail_type' => $matching_template['mail_type'],
73 'id' => $matching_template['id'],
74 'template_title' => $matching_template['title'],
75 'object' => '', // No template object since the file is empty
76 'template_thumbnail' => $matching_template['preview-thumb'], // Only include the thumbnail
77 );
78
79 $templates_data[] = $template_data;
80 continue; // Skip further processing for this template
81 }
82
83 // Continue processing templates with a valid file path
84 if (!empty($matching_template['file'])) {
85 $json_content = file_get_contents($matching_template['file']);
86 $template_string = stripslashes($json_content);
87 $template_object = json_decode($template_string, true);
88
89 $template_data = array(
90 'date' => get_the_date('Y-m-d H:i:s', $matching_template['id']),
91 'package' => $matching_template['package'],
92 'mail_type' => $matching_template['mail_type'],
93 'id' => $matching_template['id'],
94 'template_title' => $matching_template['title'],
95 'object' => $template_object,
96 'template_thumbnail' => $matching_template['preview-thumb'],
97 );
98
99 $templates_data[] = $template_data;
100 }
101 }
102
103 return [
104 "status" => "success",
105 "data" => $templates_data,
106 "message" => [
107 __( "Template files retrieved successfully.", 'emailkit' ),
108 ],
109 ];
110
111 }
112 }