PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.5.5
EmailKit – Email Customizer for WooCommerce & WP v1.5.5
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 / UpdateData.php

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

129 lines 3.1 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\Emails\Helpers\Utils;
6
7 defined('ABSPATH') || exit;
8
9 class UpdateData
10 {
11
12 public $prefix = '';
13 public $param = '';
14 public $request = null;
15
16
17 public function __construct()
18 {
19
20 add_action('rest_api_init', function () {
21 register_rest_route('emailkit/v1/update-data', '(?P<post_id>\d+)', array(
22 'methods' => \WP_REST_Server::ALLMETHODS,
23 'callback' => [$this, 'update_action'],
24 'permission_callback' => '__return_true',
25 ));
26 });
27 }
28
29 public function update_action($request)
30 {
31
32 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
33 return [
34 'status' => 'fail',
35 'message' => [ __('Nonce mismatch.', 'emailkit') ]
36 ];
37 }
38
39
40 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
41 return [
42 'status' => 'fail',
43 'message' => [ __('Access denied.', 'emailkit') ]
44 ];
45 }
46
47 $body = $request->get_body();
48 $req = json_decode($body);
49
50
51 $post_id = (int) $request->get_param('post_id');
52 if (!$post_id || !get_post($post_id) || get_post_type($post_id) !== 'emailkit') {
53 return [
54 'status' => 'fail',
55 'message' => ['Invalid post ID.']
56 ];
57 }
58
59 $data = array(
60 'ID' => $post_id,
61 'meta_input' => array(
62 'emailkit_template_content_html' => $req->html,
63 'emailkit_template_content_object' => Utils::escape_quotes($req->object),
64 'emailkit_template_status' => trim($request->get_param('emailkit_template_status') ?? 'inactive'),
65 'emailkit_email_subject' => $request->get_param('emailkit_email_subject'),
66 'emailkit_email_preheader' => $request->get_param('emailkit_email_preheader'),
67
68 ),
69 );
70
71 if('active' == trim($request->get_param('emailkit_template_status'))){
72
73 $this->deactivateTemplateTypes(get_post_meta($post_id,'emailkit_template_type', true));
74 }
75
76 $post_id = wp_update_post($data);
77
78 if (is_wp_error($post_id)) {
79 return [
80 "status" => "failed",
81 "message" => [
82 __( 'Post updated successfully.', 'emailkit' ),
83 ],
84 ];
85 } else {
86 return [
87
88 "status" => "success",
89 "data" => [
90 "templateId" => $post_id,
91 ],
92 "message" => [
93 __( 'Post updated successfully.', 'emailkit' ),
94 ],
95 ];
96 }
97 }
98
99 public function deactivateTemplateTypes($type)
100 {
101
102 $query = array(
103 'post_type' => 'emailkit',
104 'meta_query' => array(
105 array(
106 'key' => 'emailkit_template_type',
107 'value' => $type,
108 'compare' => '=',
109 ),
110 array(
111 'key' => 'emailkit_template_status',
112 'value' => 'active',
113 'compare' => '=',
114 ),
115 'relation' => 'AND',
116 'fields' => 'ids'
117 )
118 );
119
120 $data = new \WP_Query($query);
121 if (isset($data)) {
122 $postsIds = wp_list_pluck($data->posts, 'ID');
123 foreach ($postsIds as $id) {
124 update_post_meta($id, 'emailkit_template_status', 'inactive');
125 }
126 }
127 }
128
129 }