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

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