PluginProbe
MailerLite – WooCommerce integration / 3.1.21
MailerLite – WooCommerce integration v3.1.21
3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 All 147 releases
woo-mailerlite / includes / controllers / WooMailerLiteController.php

WooMailerLiteController.php in MailerLite – WooCommerce integration 3.1.21, at includes/controllers/WooMailerLiteController.php

183 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WooMailerLiteController
4 {
5 public static $instance;
6
7 public $request = [];
8
9 public $loader;
10
11 public $validated = [];
12
13 public static function instance($params = [])
14 {
15 $class = static::class;
16 if (!isset(static::$instance[$class])) {
17 static::$instance[$class] = new static();
18 }
19 static::$instance[$class]->request = $_REQUEST;
20 static::$instance[$class]->loader = new WooMailerLiteLoader();
21 return static::$instance[$class];
22 }
23
24 /**
25 * @return $this
26 */
27 protected function authorize()
28 {
29 if (!current_user_can('manage_options')) {
30 wp_send_json(['success' => false, 'error' => true, 'message' => 'Unauthorized'], 403);
31 }
32
33 $this->validate('nonce');
34
35 return $this;
36 }
37
38 public function validate($validations)
39 {
40 try {
41 if ($validations === 'nonce' || (is_array($validations) && in_array('nonce', $validations))) {
42 check_ajax_referer('woo_mailerlite_admin', 'nonce');
43 }
44
45 if (empty($this->request)) {
46 $this->request = $_REQUEST;
47 }
48
49 $skipSometimes = false;
50 $keysToUnset = [];
51
52 if (is_array($validations)) {
53 foreach ($validations as $key => $validation) {
54
55 if (is_array($validation)) {
56 if ($skipSometimes === $key) {
57 continue;
58 }
59
60 if (strpos($key, '.') !== false) {
61 $needle = explode('.', $key);
62 if (!is_array($this->request[$needle[0]])) {
63 $this->sanitizeRequestKey($needle[0]);
64 }
65
66 if (isset($this->request[$needle[0]][$needle[1]])) {
67 $this->request[$key] = $this->request[$needle[0]][$needle[1]];
68 $keysToUnset[] = $key;
69 }
70 }
71
72 if (in_array('required', $validation) && empty($this->request[$key])) {
73 throw new Exception("The $key field is required.");
74 }
75
76 if (in_array('sometimes', $validation)) {
77 $skipSometimes = $key;
78 if (isset($this->request[$key]) && !empty($this->request[$key])) {
79 $this->validated[$key] = $this->request[$key];
80 }
81 continue;
82 }
83
84 if (in_array('string', $validation) && !is_string($this->request[$key])) {
85 throw new Exception("The $key field must be a string.");
86 }
87
88 if (in_array('int', $validation) && (!ctype_digit(strval($this->request[$key])) || (int)$this->request[$key] <= 0)) {
89 throw new Exception("The $key field must be a positive integer.");
90 }
91
92 if (in_array('bool', $validation) && !is_bool(filter_var($this->request[$key], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) {
93 throw new Exception("The $key field must be a boolean.");
94 }
95 }
96
97 if (isset($this->request[$key])) {
98 $this->validated[$key] = $this->request[$key];
99 }
100 }
101 }
102
103 foreach ($keysToUnset as $key) {
104 unset($this->request[$key]);
105 }
106
107 return true;
108 } catch (Exception $e) {
109 wp_send_json([
110 'success' => false,
111 'error' => true,
112 'message' => $e->getMessage(),
113 ], 403);
114 }
115 }
116
117 public function request($key = null)
118 {
119 if (strpos($key, '.') !== false) {
120 $needle = explode('.', $key);
121 return $this->request[$needle[0]][$needle[1]] ?? false;
122 }
123 if ($key) {
124 return $_POST[$key] ?? false;
125 }
126 return $_POST;
127 }
128
129 public function requestHas($key)
130 {
131 return isset($_POST[$key]) || isset($_GET[$key]);
132 }
133
134 public function response($response, $status, $message = '')
135 {
136 if ($response instanceof WooMailerLiteApiResponse) {
137 $response->message = $message;
138 return wp_send_json($response, $status);
139 }
140 if (isset($response['message'])) {
141 $response['message'] = $message;
142 }
143
144 if (is_array($response) && (isset($response['success']) || isset($status))) {
145 $response['success'] = false;
146 if ($status == 200) {
147 $response['success'] = true;
148 }
149 }
150 return wp_send_json($response, $status);
151 }
152
153 public function apiClient($apiKey = "")
154 {
155 return WooMailerLiteApi::client($apiKey);
156 }
157
158 public function resolveResource($model, $id)
159 {
160 return $model::where('id', $id)->first();
161 }
162
163 public function sanitizeRequestKey($key)
164 {
165 $this->request[$key] = json_decode($this->request[$key], true);
166 }
167
168 public function isJson($array, $key)
169 {
170 if (!isset($array[$key]) || !is_string($array[$key])) {
171 return false;
172 }
173
174 $decoded = json_decode($array[$key], true);
175 return json_last_error() === JSON_ERROR_NONE && (is_array($decoded) || is_object($decoded));
176 }
177
178 public function validateAfterMerge($key, $newKey)
179 {
180 $this->request[$newKey] = $this->request[$key];
181 }
182 }
183