| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class responsible for database work |
| 5 |
* using wordpress functionality |
| 6 |
* get_option and update_option. |
| 7 |
*/ |
| 8 |
class BetterPayment_DB |
| 9 |
{ |
| 10 |
public static function get_table_name() |
| 11 |
{ |
| 12 |
global $wpdb; |
| 13 |
|
| 14 |
$table = "{$wpdb->prefix}betterpayment"; |
| 15 |
return $table; |
| 16 |
} |
| 17 |
/** |
| 18 |
* Get all default settings value. |
| 19 |
* |
| 20 |
* @param string $name |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
public static function default_settings() |
| 24 |
{ |
| 25 |
return apply_filters('betterpayment_option_default_settings', array( |
| 26 |
'betterpayment_settings_general_general_stripe' => esc_html__('yes', 'better-payment'), |
| 27 |
'betterpayment_settings_general_general_paypal' => esc_html__('yes', 'better-payment'), |
| 28 |
'betterpayment_settings_general_general_email' => esc_html__('yes', 'better-payment'), |
| 29 |
'betterpayment_settings_general_general_currency' => esc_html__('USD', 'better-payment'), |
| 30 |
|
| 31 |
'betterpayment_settings_general_email_to' => esc_html__('', 'better-payment'), |
| 32 |
'betterpayment_settings_general_email_subject' => esc_html__('', 'better-payment'), |
| 33 |
'betterpayment_settings_general_email_message_admin' => esc_html__('', 'better-payment'), |
| 34 |
'betterpayment_settings_general_email_from_email' => esc_html__('', 'better-payment'), |
| 35 |
'betterpayment_settings_general_email_from_name' => esc_html__('', 'better-payment'), |
| 36 |
'betterpayment_settings_general_email_reply_to' => esc_html__('', 'better-payment'), |
| 37 |
'betterpayment_settings_general_email_cc' => esc_html__('', 'better-payment'), |
| 38 |
'betterpayment_settings_general_email_bcc' => esc_html__('', 'better-payment'), |
| 39 |
'betterpayment_settings_general_email_send_as' => esc_html__('html', 'better-payment'), |
| 40 |
|
| 41 |
'betterpayment_settings_general_email_subject_customer' => esc_html__('', 'better-payment'), |
| 42 |
'betterpayment_settings_general_email_message_customer' => esc_html__('', 'better-payment'), |
| 43 |
'betterpayment_settings_general_email_from_email_customer' => esc_html__('', 'better-payment'), |
| 44 |
'betterpayment_settings_general_email_from_name_customer' => esc_html__('', 'better-payment'), |
| 45 |
'betterpayment_settings_general_email_reply_to_customer' => esc_html__('', 'better-payment'), |
| 46 |
'betterpayment_settings_general_email_cc_customer' => esc_html__('', 'better-payment'), |
| 47 |
'betterpayment_settings_general_email_bcc_customer' => esc_html__('', 'better-payment'), |
| 48 |
'betterpayment_settings_general_email_send_as_customer' => esc_html__('html', 'better-payment'), |
| 49 |
|
| 50 |
'betterpayment_settings_payment_stripe_live_mode' => esc_html__('no', 'better-payment'), |
| 51 |
'betterpayment_settings_payment_stripe_live_public' => esc_html__('', 'better-payment'), |
| 52 |
'betterpayment_settings_payment_stripe_live_secret' => esc_html__('', 'better-payment'), |
| 53 |
'betterpayment_settings_payment_stripe_test_public' => esc_html__('', 'better-payment'), |
| 54 |
'betterpayment_settings_payment_stripe_test_secret' => esc_html__('', 'better-payment'), |
| 55 |
'betterpayment_settings_payment_paypal_live_mode' => esc_html__('no', 'better-payment'), |
| 56 |
'betterpayment_settings_payment_paypal_email' => esc_html__('', 'better-payment'), |
| 57 |
)); |
| 58 |
} |
| 59 |
/** |
| 60 |
* Get all settings value from options table. |
| 61 |
* |
| 62 |
* @param string $name |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public static function get_settings($name = '') |
| 66 |
{ |
| 67 |
$settings = get_option('betterpayment_settings', true); |
| 68 |
$default = self::default_settings(); |
| 69 |
if (!empty($name) && isset($settings[$name])) { |
| 70 |
return $settings[$name]; |
| 71 |
} |
| 72 |
|
| 73 |
if (!empty($name) && !isset($settings[$name]) && isset($default[$name])) { |
| 74 |
return $default[$name]; |
| 75 |
} |
| 76 |
|
| 77 |
if (!empty($name) && !isset($settings[$name]) && !isset($default[$name])) { |
| 78 |
return ''; |
| 79 |
} |
| 80 |
|
| 81 |
return is_array($settings) ? $settings : []; |
| 82 |
} |
| 83 |
/** |
| 84 |
* Update settings |
| 85 |
* @param array $value |
| 86 |
* @return boolean |
| 87 |
*/ |
| 88 |
public static function update_settings($value, $key = '') |
| 89 |
{ |
| 90 |
if (!empty($key)) { |
| 91 |
return update_option($key, $value); |
| 92 |
} |
| 93 |
return update_option('betterpayment_settings', $value); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get all transactions from betterpayment table. |
| 98 |
* |
| 99 |
* @param string $args |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
public static function get_transactions($args = [], $count_only=0) |
| 103 |
{ |
| 104 |
global $wpdb; |
| 105 |
$items = ''; |
| 106 |
|
| 107 |
$defaults = array( |
| 108 |
'search_text' => '', |
| 109 |
'payment_date_from' => '', |
| 110 |
'payment_date_to' => '', |
| 111 |
'order_by' => 'id', |
| 112 |
'order' => 'DESC', |
| 113 |
'source' => '', |
| 114 |
'paged' => 1, |
| 115 |
'per_page' => 20, |
| 116 |
'offset' => 0, |
| 117 |
); |
| 118 |
|
| 119 |
$allowed_order_by = array( |
| 120 |
'id', |
| 121 |
'payment_date', |
| 122 |
'email', |
| 123 |
'amount', |
| 124 |
'status' |
| 125 |
); |
| 126 |
|
| 127 |
$allowed_order = array( |
| 128 |
'DESC', |
| 129 |
'ASC' |
| 130 |
); |
| 131 |
|
| 132 |
$args = wp_parse_args($args, $defaults); |
| 133 |
$table = self::get_table_name(); |
| 134 |
|
| 135 |
$search_text = sanitize_text_field($args['search_text']); |
| 136 |
$search_text = empty($search_text) ? '' : "%$search_text%"; |
| 137 |
|
| 138 |
$order_by = in_array($args['order_by'], $allowed_order_by) ? sanitize_text_field($args['order_by']) : $defaults['order_by']; |
| 139 |
$order = in_array($args['order'], $allowed_order) ? sanitize_text_field($args['order']) : $defaults['order']; |
| 140 |
$offset = $args['offset'] > 0 ? intval($args['offset']) : $defaults['offset']; |
| 141 |
$per_page = $args['per_page'] > 0 ? intval($args['per_page']) : $defaults['per_page']; |
| 142 |
|
| 143 |
$whereQuery = ''; |
| 144 |
|
| 145 |
if($search_text != '' && strlen($search_text) >= 2){ |
| 146 |
$whereQuery = $wpdb->prepare(' AND email LIKE %s OR transaction_id LIKE %s OR amount LIKE %s OR source LIKE %s ', $search_text, $search_text, $search_text, $search_text ); |
| 147 |
} |
| 148 |
|
| 149 |
if( $args['source'] != '' ){ |
| 150 |
$whereQuery .= $wpdb->prepare(" AND source = %s", $args['source']); |
| 151 |
} |
| 152 |
|
| 153 |
if( $args['payment_date_from'] != '' && $args['payment_date_to'] != ''){ |
| 154 |
$whereQuery .= $wpdb->prepare(" AND payment_date BETWEEN %s AND %s", $args['payment_date_from'], $args['payment_date_to']); |
| 155 |
} |
| 156 |
|
| 157 |
if($count_only === 1){ |
| 158 |
$items = $wpdb->get_var( |
| 159 |
$wpdb->prepare( |
| 160 |
"SELECT count(id) FROM $table WHERE 1 |
| 161 |
$whereQuery |
| 162 |
ORDER BY $order_by $order |
| 163 |
LIMIT %d, %d |
| 164 |
", |
| 165 |
$offset, |
| 166 |
$per_page |
| 167 |
) |
| 168 |
); |
| 169 |
}else { |
| 170 |
$items = $wpdb->get_results( |
| 171 |
$wpdb->prepare( |
| 172 |
"SELECT * FROM $table WHERE 1 |
| 173 |
$whereQuery |
| 174 |
ORDER BY $order_by $order |
| 175 |
LIMIT %d, %d |
| 176 |
", |
| 177 |
$offset, |
| 178 |
$per_page |
| 179 |
) |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
return $items; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get the count of total transactions |
| 188 |
* |
| 189 |
* @return int |
| 190 |
*/ |
| 191 |
public static function get_transaction_count($args = '') |
| 192 |
{ |
| 193 |
global $wpdb; |
| 194 |
$table = self::get_table_name(); |
| 195 |
|
| 196 |
$count = 0; |
| 197 |
|
| 198 |
if($args !== ''){ |
| 199 |
$count = self::get_transactions($args, 1); |
| 200 |
}else { |
| 201 |
$count = $wpdb->get_var( |
| 202 |
"SELECT count(id) FROM $table" |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
return $count; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get transactions analytics |
| 211 |
* |
| 212 |
* @return int |
| 213 |
*/ |
| 214 |
public static function get_transactions_analytics() |
| 215 |
{ |
| 216 |
global $wpdb; |
| 217 |
$transaction_analytics = []; |
| 218 |
|
| 219 |
$table = self::get_table_name(); |
| 220 |
|
| 221 |
$count_all = (int) $wpdb->get_var( |
| 222 |
"SELECT count(id) FROM $table" |
| 223 |
); |
| 224 |
|
| 225 |
$count_paid = (int) $wpdb->get_var($wpdb->prepare( |
| 226 |
"SELECT count(id) FROM $table WHERE status LIKE %s", 'paid' |
| 227 |
)); |
| 228 |
|
| 229 |
$transaction_analytics['all'] = $count_all; |
| 230 |
$transaction_analytics['paid'] = $count_paid; |
| 231 |
|
| 232 |
return $transaction_analytics; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get transaction details from betterpayment table. |
| 237 |
* |
| 238 |
* @param string $args |
| 239 |
* @return object |
| 240 |
*/ |
| 241 |
public static function get_transaction($id) |
| 242 |
{ |
| 243 |
global $wpdb; |
| 244 |
|
| 245 |
$table = self::get_table_name(); |
| 246 |
$transaction_id = (int) sanitize_text_field($id); |
| 247 |
|
| 248 |
$item = $wpdb->get_row( |
| 249 |
$wpdb->prepare( |
| 250 |
"SELECT * FROM $table WHERE id = %d", |
| 251 |
$transaction_id |
| 252 |
) |
| 253 |
); |
| 254 |
|
| 255 |
return $item; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Delete a transaction |
| 260 |
* |
| 261 |
* @param int $id |
| 262 |
* |
| 263 |
* @return int|boolean |
| 264 |
*/ |
| 265 |
public static function delete_transaction($id) |
| 266 |
{ |
| 267 |
global $wpdb; |
| 268 |
$table = self::get_table_name(); |
| 269 |
|
| 270 |
return $wpdb->delete( |
| 271 |
$table, |
| 272 |
['id' => $id], |
| 273 |
['%d'] |
| 274 |
); |
| 275 |
} |
| 276 |
} |
| 277 |
|