| @@ -9,12 +9,8 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace Tutor\Models; |
| 12 | 12 | |
| 13 | -use Tutor\Helpers\QueryHelper; | |
| 14 | -use Tutor\Helpers\UrlHelper; | |
| 15 | -use TUTOR\Input; | |
| 16 | - | |
| 17 | 13 | /** |
| 18 | 14 | * WithdrawModel Class |
| 19 | 15 | * |
| 20 | 16 | * @since 2.0.7 |
| @@ -27,138 +23,21 @@ | ||
| 27 | 23 | const STATUS_APPROVED = 'approved'; |
| 28 | 24 | const STATUS_REJECTED = 'rejected'; |
| 29 | 25 | |
| 30 | 26 | /** |
| 31 | - * Withdrawal method keys | |
| 32 | - */ | |
| 33 | - const METHOD_BANK_TRANSFER_WITHDRAW = 'bank_transfer_withdraw'; | |
| 34 | - const METHOD_PAYPAL_WITHDRAW = 'paypal_withdraw'; | |
| 35 | - const METHOD_ECHECK_WITHDRAW = 'echeck_withdraw'; | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * Get withdrawal status list | |
| 39 | - * | |
| 40 | - * @since 4.0.0 | |
| 41 | - * | |
| 42 | - * @return array | |
| 43 | - */ | |
| 44 | - public static function get_withdrawal_status_list() { | |
| 45 | - return array( | |
| 46 | - self::STATUS_PENDING => __( 'Pending', 'tutor' ), | |
| 47 | - self::STATUS_APPROVED => __( 'Approved', 'tutor' ), | |
| 48 | - self::STATUS_REJECTED => __( 'Rejected', 'tutor' ), | |
| 49 | - ); | |
| 50 | - } | |
| 51 | - | |
| 52 | - /** | |
| 53 | - * Get withdrawal method list | |
| 54 | - * | |
| 55 | - * @since 4.0.0 | |
| 56 | - * | |
| 57 | - * @return array | |
| 58 | - */ | |
| 59 | - public static function get_withdrawal_method_list() { | |
| 60 | - return array( | |
| 61 | - self::METHOD_BANK_TRANSFER_WITHDRAW => __( 'Bank Transfer', 'tutor' ), | |
| 62 | - self::METHOD_PAYPAL_WITHDRAW => __( 'PayPal', 'tutor' ), | |
| 63 | - self::METHOD_ECHECK_WITHDRAW => __( 'E-Check', 'tutor' ), | |
| 64 | - ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * Get withdrawal method icons | |
| 69 | - * | |
| 70 | - * @since 4.0.0 | |
| 71 | - * | |
| 72 | - * @return array | |
| 73 | - */ | |
| 74 | - public static function get_method_icons() { | |
| 75 | - return array( | |
| 76 | - self::METHOD_BANK_TRANSFER_WITHDRAW => UrlHelper::asset( 'images/icon-bank.svg' ), | |
| 77 | - self::METHOD_PAYPAL_WITHDRAW => UrlHelper::asset( 'images/icon-paypal.svg' ), | |
| 78 | - self::METHOD_ECHECK_WITHDRAW => UrlHelper::asset( 'images/icon-echeck.svg' ), | |
| 79 | - ); | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Get withdrawal count | |
| 84 | - * | |
| 85 | - * @since 4.0.0 | |
| 86 | - * | |
| 87 | - * @param array $where where. | |
| 88 | - * | |
| 89 | - * @return int | |
| 90 | - */ | |
| 91 | - public static function get_withdrawal_count( $where = array() ) { | |
| 92 | - return QueryHelper::get_count( 'tutor_withdraws', $where, array(), 'withdraw_id' ); | |
| 93 | - } | |
| 94 | - | |
| 95 | - /** | |
| 96 | - * Get status count | |
| 97 | - * | |
| 98 | - * @since 4.0.0 | |
| 99 | - */ | |
| 100 | - public static function get_status_filter_options() { | |
| 101 | - $url = get_pagenum_link(); | |
| 102 | - $where = array( | |
| 103 | - 'user_id' => get_current_user_id(), | |
| 104 | - ); | |
| 105 | - | |
| 106 | - $start_date = Input::get( 'start_date' ); | |
| 107 | - $end_date = Input::get( 'end_date' ); | |
| 108 | - | |
| 109 | - if ( ! empty( $start_date ) && ! empty( $end_date ) ) { | |
| 110 | - $where['DATE(created_at)'] = array( 'BETWEEN', array( $start_date, $end_date ) ); | |
| 111 | - } | |
| 112 | - | |
| 113 | - $tabs [] = array( | |
| 114 | - 'key' => '', | |
| 115 | - 'title' => __( 'All', 'tutor' ), | |
| 116 | - 'value' => self::get_withdrawal_count( $where ), | |
| 117 | - 'url' => UrlHelper::add_query_params( $url, array( 'data' => 'all' ) ), | |
| 118 | - ); | |
| 119 | - | |
| 120 | - foreach ( self::get_withdrawal_status_list() as $status => $title ) { | |
| 121 | - $where['status'] = $status; | |
| 122 | - | |
| 123 | - $tabs[] = array( | |
| 124 | - 'key' => $status, | |
| 125 | - 'title' => $title, | |
| 126 | - 'value' => self::get_withdrawal_count( $where ), | |
| 127 | - 'url' => UrlHelper::add_query_params( $url, array( 'data' => $status ) ), | |
| 128 | - ); | |
| 129 | - } | |
| 130 | - | |
| 131 | - return $tabs; | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | 27 | * Get withdraw summary info for an user |
| 136 | 28 | * |
| 137 | 29 | * @since 2.0.7 |
| 138 | - * @since 4.0.0 $args parameter added. | |
| 139 | 30 | * |
| 140 | - * @param int $instructor_id instructor id. | |
| 141 | - * @param array $args Optional additional WHERE conditions. | |
| 31 | + * @param int $instructor_id instructor id. | |
| 142 | 32 | * @return array|object|null|void |
| 143 | 33 | */ |
| 144 | - public static function get_withdraw_summary( $instructor_id, $args = array() ) { | |
| 34 | + public static function get_withdraw_summary( $instructor_id ) { | |
| 145 | 35 | global $wpdb; |
| 146 | 36 | |
| 147 | - $args = tutor_utils()->sanitize_array( $args ); | |
| 148 | - $date_clause = ''; | |
| 149 | - | |
| 150 | - if ( ! empty( $args['from'] ) && ! empty( $args['to'] ) ) { | |
| 151 | - $from = Input::sanitize( $args['from'] ); | |
| 152 | - $to = Input::sanitize( $args['to'] ); | |
| 153 | - | |
| 154 | - $where['created_at'] = array( 'BETWEEN', array( $from, $to ) ); | |
| 155 | - $date_clause = ' AND ' . QueryHelper::prepare_where_clause( $where ); | |
| 156 | - } | |
| 157 | - | |
| 158 | 37 | $maturity_days = tutor_utils()->get_option( 'minimum_days_for_balance_to_be_available' ); |
| 159 | 38 | |
| 160 | - //phpcs:disable | |
| 39 | + //phpcs:disable WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder | |
| 161 | 40 | $data = $wpdb->get_row( |
| 162 | 41 | $wpdb->prepare( |
| 163 | 42 | "SELECT ID, display_name, |
| 164 | 43 | total_income, |
| @@ -169,13 +48,13 @@ | ||
| 169 | 48 | greatest(0, total_matured - total_withdraw) available_for_withdraw |
| 170 | 49 | |
| 171 | 50 | FROM ( |
| 172 | 51 | SELECT ID,display_name, |
| 173 | - COALESCE((SELECT SUM(instructor_amount) FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s' {$date_clause} GROUP BY user_id HAVING user_id=u.ID),0) total_income, | |
| 52 | + COALESCE((SELECT SUM(instructor_amount) FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s' GROUP BY user_id HAVING user_id=u.ID),0) total_income, | |
| 174 | 53 | |
| 175 | 54 | COALESCE(( |
| 176 | 55 | SELECT sum(amount) total_withdraw FROM {$wpdb->prefix}tutor_withdraws |
| 177 | - WHERE status='%s' {$date_clause} | |
| 56 | + WHERE status='%s' | |
| 178 | 57 | GROUP BY user_id |
| 179 | 58 | HAVING user_id=u.ID |
| 180 | 59 | ),0) total_withdraw, |
| 181 | 60 | |
| @@ -180,9 +59,9 @@ | ||
| 180 | 59 | ),0) total_withdraw, |
| 181 | 60 | |
| 182 | 61 | COALESCE(( |
| 183 | 62 | SELECT sum(amount) total_pending FROM {$wpdb->prefix}tutor_withdraws |
| 184 | - WHERE status='pending' {$date_clause} | |
| 63 | + WHERE status='pending' | |
| 185 | 64 | GROUP BY user_id |
| 186 | 65 | HAVING user_id=u.ID |
| 187 | 66 | ),0) total_pending, |
| 188 | 67 | |
| @@ -187,9 +66,9 @@ | ||
| 187 | 66 | ),0) total_pending, |
| 188 | 67 | |
| 189 | 68 | COALESCE(( |
| 190 | 69 | SELECT SUM(instructor_amount) FROM( |
| 191 | - SELECT user_id, instructor_amount, created_at, DATEDIFF(NOW(),created_at) AS days_old FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s' {$date_clause} | |
| 70 | + SELECT user_id, instructor_amount, created_at, DATEDIFF(NOW(),created_at) AS days_old FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s' | |
| 192 | 71 | ) a |
| 193 | 72 | WHERE days_old >= %d |
| 194 | 73 | GROUP BY user_id |
| 195 | 74 | HAVING user_id = u.ID |
| @@ -205,9 +84,9 @@ | ||
| 205 | 84 | $instructor_id |
| 206 | 85 | ) |
| 207 | 86 | ); |
| 208 | 87 | |
| 209 | - //phpcs:enable | |
| 88 | + //phpcs:enable WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder | |
| 210 | 89 | |
| 211 | 90 | return $data; |
| 212 | 91 | } |
| 213 | 92 | |
| @@ -226,60 +105,95 @@ | ||
| 226 | 105 | public static function get_withdrawals_history( $user_id = 0, $filter = array(), $start = 0, $limit = 20 ) { |
| 227 | 106 | global $wpdb; |
| 228 | 107 | |
| 229 | 108 | $filter = (array) $filter; |
| 109 | + extract( $filter ); //phpcs:ignore WordPress.PHP.DontExtract.extract_extract | |
| 230 | 110 | |
| 231 | - $where = array(); | |
| 111 | + $query_by_status_sql = ''; | |
| 112 | + $query_by_user_sql = ''; | |
| 113 | + | |
| 114 | + if ( ! empty( $status ) && in_array( $status, array( self::STATUS_PENDING, self::STATUS_APPROVED, self::STATUS_REJECTED ), true ) ) { | |
| 115 | + $status = (array) $status; | |
| 116 | + $placeholder = implode( ',', array_fill( 0, count( $status ), '%s' ) ); | |
| 117 | + | |
| 118 | + $query_by_status_sql = $wpdb->prepare( "AND status IN($placeholder)", ...$status ); //phpcs:ignore | |
| 119 | + } | |
| 120 | + | |
| 232 | 121 | if ( $user_id ) { |
| 233 | - $where['withdraw_tbl.user_id'] = $user_id; | |
| 122 | + $query_by_user_sql = " AND user_id = {$user_id} "; | |
| 234 | 123 | } |
| 235 | 124 | |
| 236 | - if ( isset( $filter['status'] ) && ! empty( $filter['status'] ) ) { | |
| 237 | - $where['withdraw_tbl.status'] = (array) $filter['status']; | |
| 125 | + // Order query @since 2.0.0. | |
| 126 | + $order_query = ''; | |
| 127 | + if ( isset( $order ) && '' !== $order ) { | |
| 128 | + $is_valid_sql = sanitize_sql_orderby( $order ); | |
| 129 | + if ( $is_valid_sql ) { | |
| 130 | + $order_query = "ORDER BY created_at {$order}"; | |
| 131 | + } | |
| 132 | + } else { | |
| 133 | + $order_query = 'ORDER BY created_at DESC'; | |
| 238 | 134 | } |
| 239 | 135 | |
| 240 | - if ( isset( $filter['date'] ) && ! empty( $filter['date'] ) ) { | |
| 241 | - $where['DATE(withdraw_tbl.created_at) = %s'] = array( 'RAW', array( $filter['date'] ) ); | |
| 136 | + // Date query @since 2.0.0. | |
| 137 | + $date_query = ''; | |
| 138 | + if ( isset( $date ) && '' !== $date ) { | |
| 139 | + $date_query = "AND DATE(created_at) = CAST( '$date' AS DATE )"; | |
| 242 | 140 | } |
| 243 | 141 | |
| 244 | - if ( isset( $filter['start_date'], $filter['end_date'] ) ) { | |
| 245 | - $where['DATE(withdraw_tbl.created_at)'] = array( 'BETWEEN', array( $filter['start_date'], $filter['end_date'] ) ); | |
| 142 | + // Search query @since 2.0.0. | |
| 143 | + $search_term_raw = empty( $search ) ? '' : $search; | |
| 144 | + $search_query = '%%'; | |
| 145 | + if ( ! empty( $search_term_raw ) ) { | |
| 146 | + $search_query = '%' . $wpdb->esc_like( $search_term_raw ) . '%'; | |
| 246 | 147 | } |
| 247 | 148 | |
| 248 | - if ( isset( $filter['search'] ) && ! empty( $filter['search'] ) ) { | |
| 249 | - $term = $filter['search']; | |
| 250 | - $like = '%' . $wpdb->esc_like( $term ) . '%'; | |
| 149 | + //phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 150 | + $count = (int) $wpdb->get_var( | |
| 151 | + $wpdb->prepare( | |
| 152 | + "SELECT COUNT(withdraw_id) | |
| 153 | + FROM {$wpdb->prefix}tutor_withdraws withdraw_tbl | |
| 154 | + INNER JOIN {$wpdb->users} user_tbl | |
| 155 | + ON withdraw_tbl.user_id = user_tbl.ID | |
| 156 | + WHERE 1 = 1 | |
| 157 | + {$query_by_user_sql} | |
| 158 | + {$query_by_status_sql} | |
| 159 | + {$date_query} | |
| 160 | + AND (user_tbl.display_name LIKE %s OR user_tbl.user_login LIKE %s OR user_tbl.user_nicename LIKE %s OR user_tbl.user_email = %s) | |
| 161 | + ", | |
| 162 | + $search_query, | |
| 163 | + $search_query, | |
| 164 | + $search_query, | |
| 165 | + $search_term_raw | |
| 166 | + ) | |
| 167 | + ); | |
| 251 | 168 | |
| 252 | - $where['(user_tbl.display_name LIKE %s OR user_tbl.user_login LIKE %s OR user_tbl.user_nicename LIKE %s OR user_tbl.user_email = %s)'] = array( | |
| 253 | - 'RAW', | |
| 254 | - array( $like, $like, $like, $term ), | |
| 255 | - ); | |
| 256 | - } | |
| 169 | + $results = $wpdb->get_results( | |
| 170 | + $wpdb->prepare( | |
| 171 | + "SELECT withdraw_tbl.*, | |
| 172 | + user_tbl.display_name AS user_name, | |
| 173 | + user_tbl.user_email | |
| 174 | + FROM {$wpdb->prefix}tutor_withdraws withdraw_tbl | |
| 175 | + INNER JOIN {$wpdb->users} user_tbl | |
| 176 | + ON withdraw_tbl.user_id = user_tbl.ID | |
| 177 | + WHERE 1 = 1 | |
| 178 | + {$query_by_user_sql} | |
| 179 | + {$query_by_status_sql} | |
| 180 | + {$date_query} | |
| 257 | 181 | |
| 258 | - $orderby = 'withdraw_tbl.created_at'; | |
| 259 | - $order = isset( $filter['order'] ) ? QueryHelper::get_valid_sort_order( $filter['order'] ) : 'DESC'; | |
| 260 | - | |
| 261 | - $args = array( | |
| 262 | - 'select' => array( 'withdraw_tbl.*', 'user_tbl.display_name AS user_name', 'user_tbl.user_email' ), | |
| 263 | - 'alias' => 'withdraw_tbl', | |
| 264 | - 'joins' => array( | |
| 265 | - array( | |
| 266 | - 'type' => 'INNER', | |
| 267 | - 'table' => $wpdb->users . ' AS user_tbl', | |
| 268 | - 'on' => 'withdraw_tbl.user_id = user_tbl.ID', | |
| 269 | - ), | |
| 270 | - ), | |
| 271 | - 'where' => $where, | |
| 272 | - 'orderby' => $orderby, | |
| 273 | - 'order' => $order, | |
| 274 | - 'limit' => (int) $limit, | |
| 275 | - 'offset' => (int) $start, | |
| 182 | + AND (user_tbl.display_name LIKE %s OR user_tbl.user_login LIKE %s OR user_tbl.user_nicename LIKE %s OR user_tbl.user_email = %s) | |
| 183 | + {$order_query} | |
| 184 | + LIMIT %d, %d | |
| 185 | + ", | |
| 186 | + $search_query, | |
| 187 | + $search_query, | |
| 188 | + $search_query, | |
| 189 | + $search_term_raw, | |
| 190 | + $start, | |
| 191 | + $limit | |
| 192 | + ) | |
| 276 | 193 | ); |
| 277 | 194 | |
| 278 | - $results = QueryHelper::query( 'tutor_withdraws', $args ); | |
| 279 | - | |
| 280 | - $args['count'] = true; | |
| 281 | - $count = QueryHelper::query( 'tutor_withdraws', $args ); | |
| 195 | + //phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 282 | 196 | |
| 283 | 197 | $withdraw_history = array( |
| 284 | 198 | 'count' => $count ? $count : 0, |
| 285 | 199 | 'results' => is_array( $results ) ? $results : array(), |