PluginProbe
PayPlug for WooCommerce (Official) / 2.18.0
PayPlug for WooCommerce (Official) v2.18.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Model / Lock.php

Lock.php in PayPlug for WooCommerce (Official) 2.18.0, at src/Model/Lock.php

158 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Model;
4
5 class Lock
6 {
7 /**
8 * create Lock table
9 */
10 public static function create_lock_table()
11 {
12 global $wpdb;
13
14 $sql = 'CREATE TABLE IF NOT EXISTS `' . $wpdb->base_prefix . 'woocommerce_payplug_lock` (';
15 $sql .= ' `payment_id` VARCHAR(50) NOT NULL,';
16 $sql .= ' `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,';
17 $sql .= ' PRIMARY KEY (`payment_id`));';
18
19 $table_name = $wpdb->base_prefix . 'woocommerce_payplug_lock';
20 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
21 maybe_create_table($table_name, $sql);
22
23 $result = $wpdb->get_row(
24 $wpdb->prepare(
25 "SELECT count(column_name) as column_exists FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{$wpdb->base_prefix}woocommerce_payplug_lock' AND column_name = %s ",
26 ['id']
27 )
28 );
29
30 if (!empty($result) && !$result->column_exists) {
31 static::update_lock_table();
32 }
33 }
34
35 /**
36 * update table
37 */
38 public static function update_lock_table()
39 {
40 global $wpdb;
41
42 $sql = "ALTER TABLE `{$wpdb->base_prefix}woocommerce_payplug_lock` ";
43 $sql .= ' ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,';
44 $sql .= ' DROP PRIMARY KEY,';
45 $sql .= ' ADD PRIMARY KEY (`id`);';
46 $wpdb->query($sql);
47 }
48
49 /**
50 * @param $payment_id
51 *
52 * @return bool|int
53 */
54 public static function insert_lock($payment_id)
55 {
56 global $wpdb;
57 $wpdb->hide_errors();
58 $lock_table_exists = self::check_table_exists();
59 if (!$lock_table_exists) {
60 self::create_lock_table();
61 }
62 $wpdb->insert($wpdb->base_prefix . 'woocommerce_payplug_lock', ['payment_id' => $payment_id]);
63
64 return $wpdb->insert_id;
65 }
66
67 /**
68 * @param $id
69 *
70 * @return bool
71 */
72 public static function delete_lock($id)
73 {
74 global $wpdb;
75 $table_name = $wpdb->base_prefix . 'woocommerce_payplug_lock';
76 $wpdb->query($wpdb->prepare("DELETE FROM {$table_name} WHERE id = %s OR created < NOW() - INTERVAL 1 DAY;", [$id]));
77
78 return true;
79 }
80
81 /**
82 * @param $payment_id
83 *
84 * @return bool
85 */
86 public static function delete_lock_by_payment_id($payment_id)
87 {
88 global $wpdb;
89 $table_name = $wpdb->base_prefix . 'woocommerce_payplug_lock';
90 $wpdb->query($wpdb->prepare("DELETE FROM {$table_name} WHERE payment_id = %s OR created < NOW() - INTERVAL 1 DAY;", [$payment_id]));
91
92 return true;
93 }
94
95 /**
96 * @param $payment_id
97 * @param $id
98 *
99 * @return bool
100 */
101 public static function locked($payment_id, $id)
102 {
103 global $wpdb;
104
105 $table_name = $wpdb->base_prefix . 'woocommerce_payplug_lock';
106
107 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
108 $result = $wpdb->get_row($wpdb->prepare("SELECT id FROM {$table_name} WHERE `payment_id` = %s AND `id` <> %s ", [$payment_id, $id]));
109
110 if (empty($result)) {
111 return false;
112 }
113
114 return true;
115 }
116
117 /**
118 * @param $id
119 *
120 * @return bool|int
121 */
122 public static function get_lock_by_payment_id($payment_id)
123 {
124 global $wpdb;
125
126 $table_name = $wpdb->base_prefix . 'woocommerce_payplug_lock';
127
128 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
129 $result = $wpdb->get_row($wpdb->prepare("SELECT id FROM {$table_name} WHERE payment_id = %s", [$payment_id]));
130 if (!$result) {
131 return false;
132 }
133
134 return $result;
135 }
136
137 public static function delete_lock_table()
138 {
139 global $wpdb;
140
141 $table_name = $wpdb->base_prefix . 'woocommerce_payplug_lock';
142 $sql = "DROP TABLE IF EXISTS $table_name;";
143 $wpdb->query($sql);
144 }
145
146 public static function check_table_exists()
147 {
148 global $wpdb;
149 $table_name = $wpdb->base_prefix . 'woocommerce_payplug_lock';
150 $table_exists = $wpdb->get_var($wpdb->prepare(
151 'SHOW TABLES LIKE %s',
152 $table_name
153 ));
154
155 return $table_exists === $table_name;
156 }
157 }
158