PluginProbe
Tutor LMS – eLearning and online course solution / 2.2.2
Tutor LMS – eLearning and online course solution v2.2.2
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
← All changes | classes/Withdraw.php +57 -109 trunk2.2.2 View file →
@@ -9,11 +9,9 @@
9 9 */
10 10
11 11 namespace TUTOR;
12 12
13 -use Exception;
14 13 use Tutor\Models\WithdrawModel;
15 -use Tutor\Traits\JsonResponse;
16 14
17 15 if ( ! defined( 'ABSPATH' ) ) {
18 16 exit;
19 17 }
@@ -23,9 +21,8 @@
23 21 *
24 22 * @since 1.0.0
25 23 */
26 24 class Withdraw {
27 - use JsonResponse;
28 25
29 26 /**
30 27 * Withdraw method
31 28 *
@@ -114,14 +111,8 @@
114 111 ),
115 112 ),
116 113 );
117 114
118 - $saved_options = (array) get_option( 'tutor_option', array() );
119 - $withdrawal_payment_methods = $saved_options['tutor_withdrawal_methods'] ?? array();
120 - foreach ( $methods as $key => $method ) {
121 - $methods[ $key ]['enabled'] = in_array( $key, $withdrawal_payment_methods, true );
122 - }
123 -
124 115 return apply_filters( 'tutor_withdraw_methods', $methods );
125 116 }
126 117
127 118 /**
@@ -223,15 +214,13 @@
223 214 wp_send_json_success( array( 'msg' => $msg ) );
224 215 }
225 216
226 217 /**
227 - * Handle withdraw request form submit.
218 + * Handle withdraw request form submit
228 219 *
229 220 * @since 1.0.0
230 221 *
231 222 * @return void
232 - *
233 - * @throws Exception If any validation fails.
234 223 */
235 224 public function tutor_make_an_withdraw() {
236 225 global $wpdb;
237 226
@@ -236,120 +225,79 @@
236 225 global $wpdb;
237 226
238 227 tutor_utils()->checking_nonce();
239 228
240 - $user_id = get_current_user_id();
241 - if ( ! tutor_utils()->is_instructor( $user_id ) ) {
242 - wp_send_json_error( array( 'msg' => tutor_utils()->error_message() ) );
243 - }
229 + $user_id = get_current_user_id();
230 + $withdraw_amount = Input::post( 'tutor_withdraw_amount' );
244 231
245 - $lock_name = 'tutor_withdraw_lock_' . $user_id;
246 - $locked = $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 10)', $lock_name ) );
232 + $earning_summary = WithdrawModel::get_withdraw_summary( $user_id );
233 + $min_withdraw = tutor_utils()->get_option( 'min_withdraw_amount' );
247 234
248 - if ( 1 !== (int) $locked ) {
249 - wp_send_json_error(
250 - array(
251 - 'msg' => __( 'Another withdrawal request is in progress. Please try again.', 'tutor' ),
252 - )
253 - );
235 + $saved_withdraw_account = WithdrawModel::get_user_withdraw_method();
236 + $formatted_min_withdraw_amount = tutor_utils()->tutor_price( $min_withdraw );
237 +
238 + if ( ! tutor_utils()->count( $saved_withdraw_account ) ) {
239 + $no_withdraw_method = apply_filters( 'tutor_no_withdraw_method_msg', __( 'Please save withdraw method ', 'tutor' ) );
240 + wp_send_json_error( array( 'msg' => $no_withdraw_method ) );
254 241 }
255 242
256 - try {
257 - $withdraw_amount = (float) Input::post( 'amount' );
258 - $earning_summary = WithdrawModel::get_withdraw_summary( $user_id );
259 - $min_withdraw = (float) tutor_utils()->get_option( 'min_withdraw_amount' );
243 + if ( ( ! is_numeric( $withdraw_amount ) && ! is_float( $withdraw_amount ) ) || $withdraw_amount < $min_withdraw ) {
244 + $required_min_withdraw = apply_filters( 'tutor_required_min_amount_msg', sprintf( __( 'Minimum withdrawal amount is %1$s %2$s %3$s ', 'tutor' ), '<strong>', $formatted_min_withdraw_amount, '</strong>' ) );
245 + wp_send_json_error( array( 'msg' => $required_min_withdraw ) );
246 + }
260 247
261 - if ( ( $earning_summary->total_pending + $withdraw_amount ) > $earning_summary->available_for_withdraw ) {
262 - throw new Exception(
263 - wp_sprintf(
264 - /* translators: 1: total pending withdraw request 2: available for withdraw */
265 - __( "You have total %1\$s pending withdraw request. You can't make more than %2\$s withdraw request at a time", 'tutor' ),
266 - $earning_summary->total_pending,
267 - $earning_summary->available_for_withdraw
268 - )
269 - );
270 - }
248 + if ( $earning_summary->available_for_withdraw < $withdraw_amount ) {
249 + $insufficient_balence = apply_filters( 'tutor_withdraw_insufficient_balance_msg', __( 'Insufficient balance.', 'tutor' ) );
271 250
272 - $saved_withdraw_account = WithdrawModel::get_user_withdraw_method();
273 - $formatted_min_withdraw_amount = tutor_utils()->tutor_price( $min_withdraw );
251 + wp_send_json_error( array( 'msg' => $insufficient_balence ) );
252 + }
274 253
275 - if ( ! tutor_utils()->count( $saved_withdraw_account ) ) {
276 - $no_withdraw_method = apply_filters( 'tutor_no_withdraw_method_msg', __( 'Please save withdraw method ', 'tutor' ) );
277 - throw new Exception( $no_withdraw_method );
278 - }
254 + $date = gmdate( 'Y-m-d H:i:s', tutor_time() );
279 255
280 - if ( ( ! is_numeric( $withdraw_amount ) && ! is_float( $withdraw_amount ) ) || $withdraw_amount < $min_withdraw ) {
281 - /* translators: 1: strong tag start 2: min withdrawal amount 3: strong tag end */
282 - $required_min_withdraw = apply_filters( 'tutor_required_min_amount_msg', sprintf( __( 'Minimum withdrawal amount is %1$s %2$s %3$s ', 'tutor' ), '<strong>', $formatted_min_withdraw_amount, '</strong>' ) );
283 - throw new Exception( $required_min_withdraw );
284 - }
256 + $withdraw_data = apply_filters(
257 + 'tutor_pre_withdraw_data',
258 + array(
259 + 'user_id' => $user_id,
260 + 'amount' => $withdraw_amount,
261 + 'method_data' => maybe_serialize( $saved_withdraw_account ),
262 + 'status' => 'pending',
263 + 'created_at' => $date,
264 + )
265 + );
285 266
286 - if ( $earning_summary->available_for_withdraw < $withdraw_amount ) {
287 - $insufficient_balence = apply_filters( 'tutor_withdraw_insufficient_balance_msg', __( 'Insufficient balance.', 'tutor' ) );
288 - throw new Exception( $insufficient_balence );
289 - }
267 + $date = gmdate( 'Y-m-d H:i:s', tutor_time() );
290 268
291 - $date = gmdate( 'Y-m-d H:i:s', tutor_time() );
269 + $withdraw_data = apply_filters(
270 + 'tutor_pre_withdraw_data',
271 + array(
272 + 'user_id' => $user_id,
273 + 'amount' => $withdraw_amount,
274 + 'method_data' => maybe_serialize( $saved_withdraw_account ),
275 + 'status' => 'pending',
276 + 'created_at' => $date,
277 + )
278 + );
292 279
293 - $withdraw_data = apply_filters(
294 - 'tutor_pre_withdraw_data',
295 - array(
296 - 'user_id' => $user_id,
297 - 'amount' => $withdraw_amount,
298 - 'method_data' => maybe_serialize( $saved_withdraw_account ),
299 - 'status' => 'pending',
300 - 'created_at' => $date,
301 - )
302 - );
280 + do_action( 'tutor_insert_withdraw_before', $withdraw_data );
303 281
304 - do_action( 'tutor_insert_withdraw_before', $withdraw_data );
282 + $wpdb->insert( $wpdb->prefix . 'tutor_withdraws', $withdraw_data );
283 + $withdraw_id = $wpdb->insert_id;
305 284
306 - $inserted = $wpdb->insert( $wpdb->prefix . 'tutor_withdraws', $withdraw_data );
307 - if ( false === $inserted ) {
308 - throw new Exception( __( 'Unable to process withdrawal request. Please try again.', 'tutor' ) );
309 - }
285 + do_action( 'tutor_insert_withdraw_after', $withdraw_id, $withdraw_data );
310 286
311 - $withdraw_id = $wpdb->insert_id;
287 + /**
288 + * Getting earning and balance data again
289 + */
290 + $earning = WithdrawModel::get_withdraw_summary( $user_id );
291 + $new_available_balance = tutor_utils()->tutor_price( $earning->available_for_withdraw );
312 292
313 - do_action( 'tutor_insert_withdraw_after', $withdraw_id, $withdraw_data );
293 + do_action( 'tutor_withdraw_after' );
314 294
315 - /**
316 - * Getting earning and balance data again
317 - */
318 - $earning = WithdrawModel::get_withdraw_summary( $user_id );
319 - $new_available_balance = tutor_utils()->tutor_price( $earning->available_for_withdraw );
320 -
321 - do_action( 'tutor_withdraw_after' );
322 -
323 - $response = array(
324 - 'msg' => apply_filters( 'tutor_withdraw_successful_msg', __( 'Withdrawal Request Sent!', 'tutor' ) ),
295 + $withdraw_successfull_msg = apply_filters( 'tutor_withdraw_successful_msg', __( 'Withdrawal Request Sent!', 'tutor' ) );
296 + wp_send_json_success(
297 + array(
298 + 'msg' => $withdraw_successfull_msg,
325 299 'available_balance' => $new_available_balance,
326 - );
327 -
328 - } catch ( Exception $e ) {
329 -
330 - $response = array(
331 - 'error' => true,
332 - 'msg' => $e->getMessage(),
333 - );
334 -
335 - } finally {
336 -
337 - $wpdb->query(
338 - $wpdb->prepare(
339 - 'SELECT RELEASE_LOCK(%s)',
340 - $lock_name
341 - )
342 - );
343 - }
344 -
345 - if ( ! empty( $response['error'] ) ) {
346 - wp_send_json_error(
347 - array(
348 - 'msg' => $response['msg'],
349 - )
350 - );
351 - }
352 -
353 - wp_send_json_success( $response );
300 + )
301 + );
354 302 }
355 303 }