PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.4.0
EmailKit – Email Customizer for WooCommerce & WP v1.4.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 / TemplateData.php

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

142 lines 5.0 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 defined('ABSPATH') || exit;
6 class TemplateData
7 {
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', 'template-data', array(
17 'methods' => \WP_REST_Server::ALLMETHODS,
18 'callback' => [$this, 'action'],
19 'permission_callback' => '__return_true',
20 ));
21 });
22 }
23
24 public function action($request)
25 {
26
27 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
28 return [
29 'status' => 'fail',
30 'message' => ['Nonce mismatch.']
31 ];
32 }
33
34 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
35 return [
36 'status' => 'fail',
37 'message' => ['Access denied.']
38 ];
39 }
40 $template = '';
41 $html = '';
42 $body = $request->get_body();
43 $req = json_decode($body);
44
45 if(file_exists($request->get_param('emailkit-editor-template'))){
46 $template = trim(file_get_contents($request->get_param('emailkit-editor-template')));
47 }
48
49 if(file_exists($request->get_param('emailkit-editor-template'))){
50 $html = file_get_contents(str_replace("content.json", "content.html", $request->get_param('emailkit-editor-template')));
51 }
52
53 $subject = trim($request->get_param('emailkit_template_subject'))??null;
54
55 $post_id = empty($req->postIdField) ? '' : $req->postIdField;
56
57 if (empty($post_id)) {
58 $subject = trim($request->get_param('emailkit_template_subject'));
59 $data = array(
60 'post_type' => 'emailkit',
61 'post_status' => 'publish',
62 'post_author' => get_current_user_id(),
63 'post_title' => $subject !== '' ? $subject : "New Template ".uniqid(),
64 'meta_input' => array(
65 'emailkit_template_content_html' => $html,
66 'emailkit_template_content_object' => $template,
67 'emailkit_email_type' => $request->get_param('emailkit_email_type'),
68 'emailkit_template_type' => $request->get_param('emailkit_template_type'),
69 'emailkit_template_status' => 'inactive'
70 )
71 );
72 if('active' == trim($request->get_param('emailkit_template_status'))){
73 $this->deactivateTemplateTypes(get_post_meta($post_id,'emailkit_template_type', true));
74 }
75 $post_id = wp_insert_post($data);
76
77 $message = 'post created successfully.';
78 } else {
79
80 $data = array(
81 'ID' => $post_id,
82 'post_status' => 'publish',
83 'post_type' => 'emailkit',
84 'post_author' => get_current_user_id(),
85 'meta_input' => array(
86 'emailkit_template_content_html' => $req->html,
87 'emailkit_template_content_object' => $req->object,
88 'emailkit_template_status' => trim($request->get_param('emailkit_template_status') ?? 'inactive')
89 )
90 );
91
92 // Update the post into the database
93 if('active' == trim($request->get_param('emailkit_template_status'))){
94 $this->deactivateTemplateTypes(get_post_meta($post_id,'emailkit_template_type', true));
95 }
96 // $this->deactivateTemplateTypes(get_post_meta($post_id,'emailkit_template_type', true));
97 $post_id = wp_update_post($data);
98 $message = 'post updated successfully.';
99 }
100
101 return [
102 "status" => "success",
103 "data" => [
104 "templateId" => $post_id,
105 'builder_url' => (admin_url("post.php?post={$post_id}&action=emailkit-builder")),
106 ],
107 "message" => [
108 $message,
109 ],
110 ];
111 }
112 public function deactivateTemplateTypes($type)
113 {
114
115 $query = array(
116 'post_type' => 'emailkit',
117 'relation' => 'AND',
118 'meta_query' => array(
119 array(
120 'key' => 'emailkit_template_type',
121 'value' => $type,
122 'compare' => '==',
123 ),
124 array(
125 'key' => 'emailkit_template_status',
126 'value' => 'active',
127 'compare' => '==',
128 ),
129 'fields' => 'ids'
130 )
131 );
132
133 $data = new \WP_Query($query);
134 if (isset($data)) {
135
136 $postsIds = wp_list_pluck($data->posts, 'ID');
137 foreach ($postsIds as $id) {
138 update_post_meta($id, 'emailkit_template_status', 'inactive');
139 }
140 }
141 }
142 }