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 / TemplateData.php

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

127 lines 4.7 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 $message = '';
45
46
47
48 if(!empty($request->get_param( 'emailkit-editor-template' ) && trim($request->get_param( 'emailkit-editor-template' )) !== '')){
49 $template = file_get_contents($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 = !empty($request->get_param( 'emailkit_template_title' ))? trim($request->get_param( 'emailkit_template_title' )) : null;
54
55 $post_id = empty($req->postIdField) ? '' : $req->postIdField;
56 $form_id = $request->get_param('form_id') ?? '';
57
58 if (empty($post_id)) {
59 $subject = ($request->get_param('emailkit_template_title') !== null) ? trim($request->get_param('emailkit_template_title')) : '';
60 $data = array(
61 'post_type' => 'emailkit',
62 'post_status' => 'publish',
63 'post_author' => get_current_user_id(),
64 'post_title' => $subject !== '' ? $subject : "New Template ".uniqid(),
65 'meta_input' => array(
66 'emailkit_template_content_html' => $html,
67 'emailkit_template_content_object' => $template,
68 'emailkit_email_type' => $request->get_param('emailkit_email_type'),
69 'emailkit_template_type' => $request->get_param('emailkit_template_type'),
70 'emailkit_template_status' => $request->get_param('emailkit_template_status') ?? 'inactive',
71 'emailkit_email_subject' => $req->subject??'',
72 'emailkit_email_preheader' => $req->preheader??'',
73 'emailkit_template_initial_content_object' => $template,
74 'emailkit_form_id' => $form_id,
75 )
76 );
77
78 if(!empty($request->get_param('emailkit_template_status')) && trim($request->get_param('emailkit_template_status')) == 'active'){
79 $this->deactivateTemplateTypes(get_post_meta($post_id,'emailkit_template_type', true));
80 }
81 $post_id = wp_insert_post($data);
82
83 $message = __( 'Post created successfully.', 'emailkit' );
84 }
85
86 return [
87 "status" => "success",
88 "data" => [
89 "templateId" => $post_id,
90 'builder_url' => (admin_url("post.php?post={$post_id}&action=emailkit-builder")),
91 ],
92 "message" => [
93 $message,
94 ],
95 ];
96 }
97 public function deactivateTemplateTypes($type)
98 {
99
100 $query = array(
101 'post_type' => 'emailkit',
102 'relation' => 'AND',
103 'meta_query' => array(
104 array(
105 'key' => 'emailkit_template_type',
106 'value' => $type,
107 'compare' => '==',
108 ),
109 array(
110 'key' => 'emailkit_template_status',
111 'value' => 'active',
112 'compare' => '==',
113 ),
114 'fields' => 'ids'
115 )
116 );
117
118 $data = new \WP_Query($query);
119 if (isset($data)) {
120
121 $postsIds = wp_list_pluck($data->posts, 'ID');
122 foreach ($postsIds as $id) {
123 update_post_meta($id, 'emailkit_template_status', 'inactive');
124 }
125 }
126 }
127 }