PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / stripe / models / FrmTransLiteDb.php

FrmTransLiteDb.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26, at stripe/models/FrmTransLiteDb.php

351 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmTransLiteDb {
7
8 /**
9 * @var int
10 */
11 public $db_version = 6;
12
13 /**
14 * @var string
15 */
16 public $db_opt_name = 'frm_trans_db_version';
17
18 /**
19 * @var string
20 */
21 public $table_name = '';
22
23 /**
24 * @var string
25 */
26 public $singular = '';
27
28 /**
29 * @param mixed $old_db_version
30 *
31 * @return void
32 */
33 public function upgrade( $old_db_version = false ) {
34 if ( ! $old_db_version ) {
35 $old_db_version = get_option( $this->db_opt_name );
36 }
37
38 if ( $this->db_version == $old_db_version ) {
39 return;
40 }
41
42 global $wpdb;
43
44 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
45
46 $frm_db = new FrmMigrate();
47 $charset_collate = $frm_db->collation();
48
49 /* Create/Upgrade Payments Table */
50 $sql = "CREATE TABLE {$wpdb->prefix}frm_payments (
51 id bigint(20) NOT NULL auto_increment,
52 meta_value longtext default NULL,
53 receipt_id varchar(100) default NULL,
54 invoice_id varchar(100) default NULL,
55 sub_id varchar(100) default NULL,
56 item_id bigint(20) NOT NULL,
57 action_id bigint(20) NOT NULL,
58 amount decimal(12,2) NOT NULL default '0.00',
59 status varchar(100) default NULL,
60 begin_date date NOT NULL,
61 expire_date date default NULL,
62 paysys varchar(100) default NULL,
63 created_at datetime NOT NULL,
64 test TINYINT(1) NULL DEFAULT NULL,
65 PRIMARY KEY (id),
66 KEY item_id (item_id)
67 ) {$charset_collate};";
68
69 dbDelta( $sql );
70
71 /* Create/Upgrade Subscriptions Table */
72 $sql = "CREATE TABLE {$wpdb->prefix}frm_subscriptions (
73 id bigint(20) NOT NULL auto_increment,
74 sub_id varchar(100) default NULL,
75 meta_value longtext default NULL,
76 item_id bigint(20) NOT NULL,
77 action_id bigint(20) NOT NULL,
78 amount decimal(12,2) NOT NULL default '0.00',
79 first_amount decimal(12,2) NOT NULL default '0.00',
80 interval_count bigint(20) default 1,
81 time_interval varchar(100) default NULL,
82 fail_count bigint(20) default 0,
83 end_count bigint(20) default NULL,
84 next_bill_date date default NULL,
85 status varchar(100) default NULL,
86 paysys varchar(100) default NULL,
87 created_at datetime NOT NULL,
88 test TINYINT(1) NULL DEFAULT NULL,
89 PRIMARY KEY (id),
90 KEY item_id (item_id)
91 ) {$charset_collate};";
92
93 dbDelta( $sql );
94
95 // SAVE DB VERSION.
96 update_option( $this->db_opt_name, $this->db_version );
97
98 $this->migrate_data( $old_db_version );
99 }
100
101 /**
102 * @param array $values
103 *
104 * @return int
105 */
106 public function create( $values ) {
107 global $wpdb;
108
109 $values['action'] = 'create';
110 $new_values = array();
111 $this->fill_values( $values, $new_values );
112
113 $wpdb->insert( $wpdb->prefix . $this->table_name, $new_values );
114
115 return $wpdb->insert_id;
116 }
117
118 /**
119 * @param int|string $id
120 * @param array $values
121 *
122 * @return false|int
123 */
124 public function update( $id, $values ) {
125 global $wpdb;
126
127 $values['action'] = 'update';
128 $new_values = array();
129 $this->fill_values( $values, $new_values );
130
131 return $wpdb->update( $wpdb->prefix . $this->table_name, $new_values, compact( 'id' ) );
132 }
133
134 /**
135 * @param int|string $id
136 *
137 * @return bool|int
138 */
139 public function &destroy( $id ) {
140 FrmAppHelper::permission_check( 'administrator' );
141
142 global $wpdb;
143 $id = absint( $id );
144
145 /**
146 * @param int $id
147 */
148 do_action( 'frm_before_destroy_' . $this->singular, $id );
149
150 // @codingStandardsIgnoreStart
151 $result = $wpdb->query(
152 $wpdb->prepare(
153 'DELETE FROM ' . $wpdb->prefix . $this->table_name . ' WHERE id=%d',
154 $id
155 )
156 );
157 // @codingStandardsIgnoreEnd
158
159 return $result;
160 }
161
162 /**
163 * @param int|string $id
164 *
165 * @return array|object|null
166 */
167 public function get_one( $id ) {
168 global $wpdb;
169 // @codingStandardsIgnoreStart
170 return $wpdb->get_row(
171 $wpdb->prepare(
172 'SELECT * FROM ' . $wpdb->prefix . $this->table_name . ' WHERE id=%d',
173 $id
174 )
175 );
176 // @codingStandardsIgnoreEnd
177 }
178
179 /**
180 * @param int|string $id
181 * @param string $field
182 *
183 * @return object|null
184 */
185 public function get_one_by( $id, $field = 'receipt_id' ) {
186 if ( ! in_array( $field, array( 'receipt_id', 'sub_id', 'item_id' ), true ) ) {
187 _doing_it_wrong( __METHOD__, 'Items can only be retrieved by receipt id or sub id.', '6.5' );
188 return null;
189 }
190
191 global $wpdb;
192 // Can this be exploited?
193 $field = sanitize_text_field( $field );
194 // @codingStandardsIgnoreStart
195 return $wpdb->get_row(
196 $wpdb->prepare(
197 'SELECT * FROM ' . $wpdb->prefix . $this->table_name
198 . ' WHERE ' . $field . ' = %s ORDER BY created_at DESC',
199 $id
200 )
201 );
202 // @codingStandardsIgnoreEnd
203 }
204
205 /**
206 * @param string $value
207 * @param string $field
208 *
209 * @return array
210 */
211 public function get_all_by( $value, $field = 'item_id' ) {
212 if ( ! FrmTransLiteAppHelper::payments_table_exists() ) {
213 // If no migrations have run yet return nothing.
214 return array();
215 }
216
217 $field = sanitize_text_field( $field );
218
219 if ( ! in_array( $field, array( 'receipt_id', 'sub_id', 'item_id' ), true ) ) {
220 _doing_it_wrong( __METHOD__, 'Items can only be retrieved by item id or sub id.', '6.5' );
221 return array();
222 }
223
224 global $wpdb;
225 // @codingStandardsIgnoreStart
226 return $wpdb->get_results(
227 $wpdb->prepare(
228 'SELECT * FROM ' . $wpdb->prefix . $this->table_name
229 . ' WHERE ' . $field . ' = %s ORDER BY created_at DESC',
230 $value
231 )
232 );
233 // @codingStandardsIgnoreEnd
234 }
235
236 /**
237 * @param int $user_id
238 *
239 * @return array
240 */
241 public function get_all_for_user( $user_id ) {
242 global $wpdb;
243 // @codingStandardsIgnoreStart
244 return $wpdb->get_results(
245 $wpdb->prepare(
246 'SELECT
247 *,
248 e.id as entry_id,
249 p.id as id
250 FROM ' . $wpdb->prefix . $this->table_name . ' p '
251 . 'LEFT JOIN ' . $wpdb->prefix . 'frm_items e ON e.id = p.item_id '
252 . 'WHERE e.user_id = %d '
253 . 'ORDER BY p.created_at DESC',
254 $user_id
255 )
256 );
257 // @codingStandardsIgnoreEnd
258 }
259
260 /**
261 * @param int|string $id
262 *
263 * @return array
264 */
265 public function get_all_for_entry( $id ) {
266 return $this->get_all_by( $id, 'item_id' );
267 }
268
269 /**
270 * @return string
271 */
272 public function get_count() {
273 global $wpdb;
274 // @codingStandardsIgnoreStart
275 return $wpdb->get_var(
276 'SELECT COUNT(*) FROM ' . $wpdb->prefix . $this->table_name
277 );
278 // @codingStandardsIgnoreEnd
279 }
280
281 /**
282 * @return array
283 */
284 public function get_defaults() {
285 return array();
286 }
287
288 /**
289 * @param array $values
290 * @param array $new_values
291 *
292 * @return void
293 */
294 private function fill_values( $values, &$new_values ) {
295 $defaults = $this->get_defaults();
296
297 foreach ( $defaults as $val => $default ) {
298 if ( isset( $values[ $val ] ) ) {
299 if ( $default['sanitize'] === 'float' ) {
300 $new_values[ $val ] = (float) $values[ $val ];
301 } elseif ( ! empty( $default['sanitize'] ) ) {
302 $new_values[ $val ] = call_user_func( $default['sanitize'], $values[ $val ] );
303 }
304 } elseif ( $values['action'] === 'create' ) {
305 $new_values[ $val ] = $default['default'];
306 }
307 }
308 }
309
310 /**
311 * Run database migrations starting from a previous DB version.
312 *
313 * @param int $old_db_version Previous database version.
314 *
315 * @return void
316 */
317 private function migrate_data( $old_db_version ) {
318 $migrations = array( 4 );
319
320 foreach ( $migrations as $migration ) {
321 if ( $this->db_version >= $migration && $old_db_version < $migration ) {
322 $function_name = 'migrate_to_' . $migration;
323 $this->$function_name();
324 }
325 }
326 }
327
328 /**
329 * This migration checks for PayPal payments and sets a status value based on the completed status.
330 *
331 * @return void
332 */
333 private function migrate_to_4() {
334 global $wpdb;
335 $result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . 'frm_payments LIKE %s', 'completed' ) );
336
337 if ( empty( $result ) ) {
338 return;
339 }
340
341 $payments = $wpdb->get_results(
342 "SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL AND status is NULL"
343 );
344
345 foreach ( $payments as $payment ) {
346 $status = $payment->completed ? 'complete' : 'failed';
347 $wpdb->update( $wpdb->prefix . 'frm_payments', compact( 'status' ), array( 'id' => $payment->id ) );
348 }
349 }
350 }
351