PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.9
EmailKit – Email Customizer for WooCommerce & WP v1.6.9
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.6.9, at includes/Admin/Api/UpdateData.php

131 lines 3.3 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('manage_options')) {
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' => [esc_html__( 'Invalid post ID.', 'emailkit' )]
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 $template_type = get_post_meta($post_id, 'emailkit_template_type', true);
72
73 if('active' == trim($request->get_param('emailkit_template_status'))){
74
75 $this->deactivateTemplateTypes($template_type);
76 }
77
78 $post_id = wp_update_post($data);
79
80 if (is_wp_error($post_id)) {
81 return [
82 "status" => "failed",
83 "message" => [
84 __( 'Post updated successfully.', 'emailkit' ),
85 ],
86 ];
87 } else {
88 return [
89
90 "status" => "success",
91 "data" => [
92 "templateId" => $post_id,
93 ],
94 "message" => [
95 __( 'Post updated successfully.', 'emailkit' ),
96 ],
97 ];
98 }
99 }
100
101 public function deactivateTemplateTypes($type)
102 {
103
104 $query = array(
105 'post_type' => 'emailkit',
106 'meta_query' => array(
107 array(
108 'key' => 'emailkit_template_type',
109 'value' => $type,
110 'compare' => '=',
111 ),
112 array(
113 'key' => 'emailkit_template_status',
114 'value' => 'active',
115 'compare' => '=',
116 ),
117 'relation' => 'AND',
118 'fields' => 'ids'
119 )
120 );
121
122 $data = new \WP_Query($query);
123 if (isset($data)) {
124 $postsIds = wp_list_pluck($data->posts, 'ID');
125 foreach ($postsIds as $id) {
126 update_post_meta($id, 'emailkit_template_status', 'inactive');
127 }
128 }
129 }
130
131 }