PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.0.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.0.3
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Database / Model.php

Model.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder V3.0.3, at includes/Core/Database/Model.php

506 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Provides Base Model Class
5 */
6
7 namespace BitCode\BitForm\Core\Database;
8
9 /**
10 * Undocumented class
11 */
12
13 use WP_Error;
14
15 class Model
16 {
17 protected static $table;
18 protected static $primary_key;
19 protected $app_db;
20 protected $table_name;
21 protected $db_response;
22
23 /**
24 * Undocumented function
25 */
26 public function __construct()
27 {
28 global $wpdb;
29 $this->app_db = $wpdb;
30 $this->table_name = $wpdb->prefix . static::$table;
31 }
32
33 /**
34 * Undocumented function
35 *
36 * @return void
37 */
38 public function insert($data = [])
39 {
40 if (is_null($data)) {
41 return new WP_Error('empty_data', 'Form data is empty');
42 }
43 $result = $this->app_db->insert(
44 $this->table_name,
45 $data
46 );
47 return $this->getResult($result);
48 }
49
50 /**
51 * Undocumented function
52 *
53 * @param string $item
54 * @param array $condition
55 *
56 * @return mixed
57 */
58 public function get($item = '*', $condition = [], $limit = null, $offset = null, $order_by = null, $order_follow = null)
59 {
60 static $tableExistsCache = [];
61 if (!isset($tableExistsCache[$this->table_name])) {
62 $tableExistsCache[$this->table_name] = !is_null(
63 $this->app_db->get_var(
64 $this->app_db->prepare('SHOW TABLES LIKE %s', $this->table_name)
65 )
66 );
67 }
68 if (!$tableExistsCache[$this->table_name]) {
69 return [];
70 }
71 if (\is_array($item)) {
72 $column_to_select = implode(',', $item);
73 } else {
74 $column_to_select = $item;
75 }
76 $checkCondition = $this->checkCondition($condition);
77 if (is_wp_error($checkCondition)) {
78 return $checkCondition;
79 }
80 $order = null;
81 if (!\is_null($order_by)) {
82 $order_follow = \is_null($order_follow) ? 'ASC' : $order_follow;
83 $order .= " ORDER BY $order_by $order_follow";
84 }
85 $paginate = null;
86 if (!\is_null($limit)) {
87 $limit = \intval($limit);
88 $paginate .= " LIMIT $limit ";
89 }
90 if (!\is_null($offset)) {
91 $offset = \intval($offset);
92 $paginate .= " OFFSET $offset ";
93 }
94 if (empty($condition)) {
95 $sql = "SELECT $column_to_select FROM `$this->table_name` $order $paginate";
96 $all_values = null;
97 } else {
98 $formatted_conditions = $this->getFormatedCondition($condition);
99 if ($formatted_conditions) {
100 $condition_to_check = $formatted_conditions['conditions'];
101 $all_values = $formatted_conditions['values'];
102 } else {
103 $condition_to_check = null;
104 $all_values = null;
105 }
106 $sql = "SELECT $column_to_select FROM `$this->table_name`"
107 . $condition_to_check . $order . $paginate;
108 }
109 return $this->execute($sql, $all_values)->getResult();
110 }
111
112 /**
113 * Undocumented function
114 *
115 * @param string $item
116 * @param array $condition
117 *
118 * @return void
119 */
120 public function count($condition = null)
121 {
122 $checkCondition = $this->checkCondition($condition);
123 if (is_wp_error($checkCondition)) {
124 return $checkCondition;
125 }
126 if (empty($condition)) {
127 $result = $this->app_db->query(
128 "SELECT COUNT(*) FROM `$this->table_name`"
129 );
130 } else {
131 $formatted_conditions = $this->getFormatedCondition($condition);
132 if ($formatted_conditions) {
133 $condition_to_check = $formatted_conditions['conditions'];
134 $all_values = $formatted_conditions['values'];
135 } else {
136 $condition_to_check = null;
137 $all_values = null;
138 }
139 $result = $this->app_db->query(
140 $this->app_db->prepare(
141 "SELECT COUNT(*) as count FROM `{$this->table_name}`"
142 . $condition_to_check,
143 $all_values
144 )
145 );
146 }
147 if (!$result) {
148 if ($this->app_db->last_error) {
149 return new WP_Error('db_error', $this->app_db->last_error);
150 }
151 return new WP_Error('db_error', 'Result is empty');
152 } else {
153 return $this->app_db->last_result;
154 }
155 }
156
157 /**
158 * Undocumented function
159 *
160 * @param array $data_to_update
161 * @param array $condition
162 *
163 * @return void
164 */
165 public function update(array $data, array $condition)
166 {
167 if (
168 !\is_null($data)
169 && \is_array($data)
170 && array_keys($data) !== range(0, count($data) - 1)
171 ) {
172 $data_to_update = $data;
173 } else {
174 return new WP_Error(
175 'update_error',
176 'Nothing to update'
177 );
178 }
179 $update_condition = (!\is_null($condition) &&
180 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
181 $result = $this->app_db->update(
182 $this->table_name,
183 $data_to_update,
184 $update_condition
185 );
186 return $this->getResult($result);
187 }
188
189 /**
190 * Undocumented function
191 *
192 * @param array $data_to_update
193 * @param array $condition
194 *
195 * @return void
196 */
197 public function bulkUpdate(array $data = null, array $condition = null)
198 {
199 if (
200 !\is_null($data)
201 && \is_array($data)
202 && array_keys($data) !== range(0, count($data) - 1)
203 ) {
204 $data_to_update = $data;
205 } else {
206 return new WP_Error(
207 'update_error',
208 'Nothing to update'
209 );
210 }
211
212 $update_fields = '';
213 $all_values = [];
214 $index_checker = 0;
215 $data_count = count($data_to_update) - 1;
216 foreach ($data_to_update as $field_name => $field_value) {
217 $update_fields .= $field_name . ' = ' . $this->getFieldFormat($field_value);
218 if ($index_checker < $data_count) {
219 $update_fields .= ',';
220 }
221 $index_checker = $index_checker + 1;
222 $all_values[] = $field_value;
223 }
224 $update_condition = (!\is_null($condition) &&
225 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
226 $formatted_conditions = $this->getFormatedCondition($update_condition);
227 if ($formatted_conditions) {
228 $condition_to_check = $formatted_conditions['conditions'];
229 $all_values = array_merge($all_values, $formatted_conditions['values']);
230 } else {
231 $condition_to_check = null;
232 }
233 $result = $this->app_db->query(
234 $this->app_db->prepare(
235 "UPDATE `{$this->table_name}` SET $update_fields $condition_to_check",
236 $all_values
237 )
238 );
239 return $this->getResult($result);
240 }
241
242 /**
243 * Duplicate's row
244 *
245 * @param array $data_to_update
246 * @param array $condition
247 *
248 * @return void
249 */
250 public function duplicate(array $columns, array $duplicate, array $condition)
251 {
252 if (!(!\is_null($columns)
253 && \is_array($columns)
254 && array_keys($columns) === range(0, count($columns) - 1)
255 && !\is_null($duplicate)
256 && \is_array($duplicate)
257 && array_keys($duplicate) === range(0, count($duplicate) - 1))) {
258 return new WP_Error(
259 'duplicate_error',
260 'Nothing to duplicate'
261 );
262 }
263
264 $dupCol = '';
265 $insCol = \implode(',', $columns);
266 $all_values = [];
267 $data_count = count($duplicate) - 1;
268 foreach ($duplicate as $dupKey => $dupColName) {
269 if (in_array($dupColName, $columns)) {
270 $dupCol .= $dupColName;
271 } else {
272 $dupCol .= $this->getFieldFormat($dupColName);
273 $all_values[] = $dupColName;
274 }
275 if ($dupKey < $data_count) {
276 $dupCol .= ',';
277 }
278 }
279 $condition_to_check = null;
280 $update_condition = (!\is_null($condition) &&
281 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
282 $formatted_conditions = $this->getFormatedCondition($update_condition);
283 if ($formatted_conditions) {
284 $condition_to_check = $formatted_conditions['conditions'];
285 $all_values = array_merge($all_values, $formatted_conditions['values']);
286 }
287 $query = "INSERT INTO `{$this->table_name}` ($insCol)
288 SELECT $dupCol FROM `{$this->table_name}` $condition_to_check";
289 $this->execute($query, $all_values);
290 return $this->getResult();
291 }
292
293 public function trash(array $condition = null)
294 {
295 if (
296 !\is_null($condition)
297 && \is_array($condition)
298 && array_keys($condition) !== range(0, count($condition) - 1)
299 ) {
300 $delete_condition = $condition;
301 } else {
302 return new WP_Error(
303 'deletion_error',
304 'At least 1 condition needed'
305 );
306 }
307 $update_condition = (!\is_null($condition) &&
308 array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null;
309 $result = $this->app_db->update(
310 $this->table_name,
311 $data_to_update,
312 $update_condition
313 );
314 return $this->getResult($result);
315 }
316
317 public function delete(array $condition = null)
318 {
319 if (
320 !\is_null($condition)
321 && \is_array($condition)
322 && array_keys($condition) !== range(0, count($condition) - 1)
323 ) {
324 $delete_condition = $condition;
325 } else {
326 return new WP_Error(
327 'deletion_error',
328 'At least 1 condition needed'
329 );
330 }
331 $result = $this->app_db->delete(
332 $this->table_name,
333 $delete_condition
334 );
335 return $this->getResult($result);
336 }
337
338 public function bulkDelete(array $condition = null)
339 {
340 if (
341 !\is_null($condition)
342 && \is_array($condition)
343 && array_keys($condition) !== range(0, count($condition) - 1)
344 ) {
345 $delete_condition = $condition;
346 } else {
347 return new WP_Error(
348 'deletion_error',
349 'At least 1 condition needed'
350 );
351 }
352 // $formatted_conditions = $this->getFormatedCondition($delete_condition, $check_operator);
353 $formatted_conditions = $this->getFormatedCondition($delete_condition);
354 if ($formatted_conditions) {
355 $condition_to_check = $formatted_conditions['conditions'];
356 $all_values = $formatted_conditions['values'];
357 } else {
358 $condition_to_check = null;
359 return new WP_Error(
360 'deletion_error',
361 'At least 1 condition needed'
362 );
363 }
364 $result = $this->app_db->query(
365 $this->app_db->prepare(
366 "DELETE FROM `{$this->table_name}` $condition_to_check",
367 $all_values
368 )
369 );
370 return $this->getResult($result);
371 }
372
373 protected function getFieldFormat($value)
374 {
375 return ('integer' === gettype($value)) ?
376 '%d' : (('double' === gettype($value)) ? '%f' : '%s');
377 }
378
379 protected function getFormatedCondition($condition, $check_operator = null, $join_operator = ' AND ')
380 {
381 if (\is_null($condition)) {
382 return false;
383 }
384 $no_condition = count($condition);
385 $index_checker = 0;
386 $condition_to_check = ' WHERE ';
387 $all_values = [];
388 foreach ($condition as $key => $value) {
389 $value_type = '';
390 if (is_array($value)) {
391 // Check for raw SQL values first
392 if (isset($value['raw'])) {
393 // Handle raw SQL - don't format or add to prepared values
394 $set_check_operator = isset($value['operator']) ? $value['operator'] : '=';
395 $value_type = $value['raw']; // Use raw SQL directly
396 // Don't add to $all_values since it's raw SQL
397 } elseif (isset($value['operator'])) {
398 // logic for operator arrays
399 $set_check_operator = $value['operator'];
400 $value_type .= $this->getFieldFormat($value['value']);
401 $all_values[] = $value['value'];
402 } else {
403 // logic for IN conditions
404 $set_check_operator = \is_null($check_operator) ? 'in' : $check_operator;
405 $value_type .= ' ( ';
406 $value_index_checker = 0;
407 $value_count = count($value) - 1;
408 foreach ($value as $condKey => $condValue) {
409 $value_type .= $this->getFieldFormat($condValue);
410 $all_values[] = $condValue;
411 if ($value_index_checker < $value_count) {
412 $value_type .= ', ';
413 }
414 $value_index_checker = $value_index_checker + 1;
415 }
416 $value_type .= ' )';
417 }
418 } else {
419 // logic for simple values
420 $set_check_operator = \is_null($check_operator) ? '=' : $check_operator;
421 $value_type .= $this->getFieldFormat($value);
422 $all_values[] = $value;
423 }
424 $condition_to_check = $condition_to_check . $key . " $set_check_operator " . $value_type;
425 if ($index_checker < $no_condition - 1) {
426 $condition_to_check = $condition_to_check . " $join_operator ";
427 }
428 $index_checker = $index_checker + 1;
429 }
430 return [
431 'conditions' => $condition_to_check,
432 'values' => $all_values
433 ];
434 }
435
436 protected function checkCondition(array $condition)
437 {
438 if (!is_null($condition) && array_keys($condition) === range(0, count($condition) - 1)) {
439 return new WP_Error(
440 'get_condition',
441 'Require ASSOC_ARRAY but found N_ARRAY'
442 );
443 }
444 return true;
445 }
446
447 protected function execute($sql, $values = null)
448 {
449 if (is_null($values)) {
450 $preparedQuery = $sql;
451 } else {
452 $preparedQuery = $this->app_db->prepare($sql, $values);
453 }
454 // echo " Q S " . $preparedQuery . " Q EE";
455 if (empty($preparedQuery)) {
456 $this->db_response = new WP_Error('null_query', 'prepared query is empty');
457 } else {
458 $this->db_response = false !== stripos($preparedQuery, 'DELETE') ? $this->app_db->query($preparedQuery)
459 : $this->app_db->get_results($preparedQuery, OBJECT_K);
460 }
461 // print_r($this->app_db->last_query);
462 return $this;
463 }
464
465 protected function getResult($db_response = null)
466 {
467 $db_response = !empty($this->db_response) ? $this->db_response : $db_response;
468 if (!empty($this->app_db->last_error)) {
469 return new WP_Error('db_error', $this->app_db->last_error);
470 }
471 if (!$db_response) {
472 if ($this->app_db->num_rows > 0) {
473 $response = $this->app_db->num_rows;
474 }
475 if (is_wp_error($db_response)) {
476 $response = $db_response;
477 }
478 $response = new WP_Error('result_empty', 'Result is empty');
479 } elseif (is_array($this->app_db->last_result) && !empty($this->app_db->last_result)) {
480 $response = $this->app_db->last_result;
481 } elseif ($this->app_db->insert_id) {
482 $response = $this->app_db->insert_id;
483 } else {
484 $response = $db_response;
485 }
486 $this->app_db->flush();
487 return $response;
488 }
489
490 /**
491 * Get last inserted id
492 *
493 * @return int
494 */
495 public function lastId()
496 {
497 $sql = "SELECT id FROM `{$this->table_name}`
498 ORDER BY id DESC LIMIT 1";
499 $result = $this->execute($sql)->getResult();
500 if (is_wp_error($result)) {
501 return 0;
502 }
503 return $result[0]->id;
504 }
505 }
506