| 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 === (int) $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 |
return $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . $this->table_name . ' WHERE id=%d', $id ) ); |
| 152 |
// @codingStandardsIgnoreEnd |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param int|string $id |
| 157 |
* |
| 158 |
* @return array|object|null |
| 159 |
*/ |
| 160 |
public function get_one( $id ) { |
| 161 |
global $wpdb; |
| 162 |
|
| 163 |
// @codingStandardsIgnoreStart |
| 164 |
return $wpdb->get_row( |
| 165 |
$wpdb->prepare( |
| 166 |
'SELECT * FROM ' . $wpdb->prefix . $this->table_name . ' WHERE id=%d', |
| 167 |
$id |
| 168 |
) |
| 169 |
); |
| 170 |
// @codingStandardsIgnoreEnd |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param int|string $id |
| 175 |
* @param string $field |
| 176 |
* |
| 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 |
/** |
| 200 |
* @param string $value |
| 201 |
* @param string $field |
| 202 |
* |
| 203 |
* @return array |
| 204 |
*/ |
| 205 |
public function get_all_by( $value, $field = 'item_id' ) { |
| 206 |
if ( ! FrmTransLiteAppHelper::payments_table_exists() ) { |
| 207 |
// If no migrations have run yet return nothing. |
| 208 |
return array(); |
| 209 |
} |
| 210 |
|
| 211 |
$field = sanitize_text_field( $field ); |
| 212 |
|
| 213 |
if ( ! in_array( $field, array( 'receipt_id', 'sub_id', 'item_id' ), true ) ) { |
| 214 |
_doing_it_wrong( __METHOD__, 'Items can only be retrieved by item id or sub id.', '6.5' ); |
| 215 |
return array(); |
| 216 |
} |
| 217 |
|
| 218 |
global $wpdb; |
| 219 |
// @codingStandardsIgnoreStart |
| 220 |
return $wpdb->get_results( |
| 221 |
$wpdb->prepare( |
| 222 |
'SELECT * FROM ' . $wpdb->prefix . $this->table_name |
| 223 |
. ' WHERE ' . $field . ' = %s ORDER BY created_at DESC', |
| 224 |
$value |
| 225 |
) |
| 226 |
); |
| 227 |
// @codingStandardsIgnoreEnd |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @param int $user_id |
| 232 |
* |
| 233 |
* @return array |
| 234 |
*/ |
| 235 |
public function get_all_for_user( $user_id ) { |
| 236 |
global $wpdb; |
| 237 |
// @codingStandardsIgnoreStart |
| 238 |
return $wpdb->get_results( |
| 239 |
$wpdb->prepare( |
| 240 |
'SELECT |
| 241 |
*, |
| 242 |
e.id as entry_id, |
| 243 |
p.id as id |
| 244 |
FROM ' . $wpdb->prefix . $this->table_name . ' p ' |
| 245 |
. 'LEFT JOIN ' . $wpdb->prefix . 'frm_items e ON e.id = p.item_id ' |
| 246 |
. 'WHERE e.user_id = %d ' |
| 247 |
. 'ORDER BY p.created_at DESC', |
| 248 |
$user_id |
| 249 |
) |
| 250 |
); |
| 251 |
// @codingStandardsIgnoreEnd |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* @param int|string $id |
| 256 |
* |
| 257 |
* @return array |
| 258 |
*/ |
| 259 |
public function get_all_for_entry( $id ) { |
| 260 |
return $this->get_all_by( $id, 'item_id' ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* @return string |
| 265 |
*/ |
| 266 |
public function get_count() { |
| 267 |
global $wpdb; |
| 268 |
// @codingStandardsIgnoreStart |
| 269 |
return $wpdb->get_var( |
| 270 |
'SELECT COUNT(*) FROM ' . $wpdb->prefix . $this->table_name |
| 271 |
); |
| 272 |
// @codingStandardsIgnoreEnd |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* @return array |
| 277 |
*/ |
| 278 |
public function get_defaults() { |
| 279 |
return array(); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* @param array $values |
| 284 |
* @param array $new_values |
| 285 |
* |
| 286 |
* @return void |
| 287 |
*/ |
| 288 |
private function fill_values( $values, &$new_values ) { |
| 289 |
foreach ( $this->get_defaults() as $val => $default ) { |
| 290 |
if ( isset( $values[ $val ] ) ) { |
| 291 |
if ( $default['sanitize'] === 'float' ) { |
| 292 |
$new_values[ $val ] = (float) $values[ $val ]; |
| 293 |
} elseif ( ! empty( $default['sanitize'] ) ) { |
| 294 |
$new_values[ $val ] = call_user_func( $default['sanitize'], $values[ $val ] ); |
| 295 |
} |
| 296 |
} elseif ( $values['action'] === 'create' ) { |
| 297 |
$new_values[ $val ] = $default['default']; |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Run database migrations starting from a previous DB version. |
| 304 |
* |
| 305 |
* @param int $old_db_version Previous database version. |
| 306 |
* |
| 307 |
* @return void |
| 308 |
*/ |
| 309 |
private function migrate_data( $old_db_version ) { |
| 310 |
$migrations = array( 4 ); |
| 311 |
|
| 312 |
foreach ( $migrations as $migration ) { |
| 313 |
if ( $this->db_version < $migration || $old_db_version >= $migration ) { |
| 314 |
continue; |
| 315 |
} |
| 316 |
|
| 317 |
$function_name = 'migrate_to_' . $migration; |
| 318 |
$this->$function_name(); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* This migration checks for PayPal payments and sets a status value based on the completed status. |
| 324 |
* |
| 325 |
* @return void |
| 326 |
*/ |
| 327 |
private function migrate_to_4() { |
| 328 |
global $wpdb; |
| 329 |
$result = $wpdb->get_results( $wpdb->prepare( 'SHOW COLUMNS FROM ' . $wpdb->prefix . 'frm_payments LIKE %s', 'completed' ) ); |
| 330 |
|
| 331 |
if ( ! $result ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
$payments = $wpdb->get_results( |
| 336 |
"SELECT * FROM {$wpdb->prefix}frm_payments WHERE completed is NOT NULL AND status is NULL" |
| 337 |
); |
| 338 |
|
| 339 |
foreach ( $payments as $payment ) { |
| 340 |
$status = $payment->completed ? 'complete' : 'failed'; |
| 341 |
$wpdb->update( $wpdb->prefix . 'frm_payments', compact( 'status' ), array( 'id' => $payment->id ) ); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|