PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.23
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.23
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.23, at stripe/models/FrmTransLiteDb.php

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