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

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

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