Addons.php
11 months ago
Admin.php
9 months ago
Ajax.php
9 months ago
Announcements.php
1 year ago
Assets.php
8 months ago
Backend_Page_Trait.php
1 year ago
BaseController.php
1 year ago
Config.php
11 months ago
Container.php
11 months ago
Course.php
9 months ago
Course_Embed.php
3 years ago
Course_Filter.php
1 year ago
Course_List.php
10 months ago
Course_Settings_Tabs.php
1 year ago
Course_Widget.php
1 year ago
Custom_Validation.php
3 years ago
Dashboard.php
1 year ago
Earnings.php
9 months ago
FormHandler.php
2 years ago
Frontend.php
1 year ago
Gutenberg.php
1 year ago
Icon.php
8 months ago
Input.php
1 year ago
Instructor.php
1 year ago
Instructors_List.php
11 months ago
Lesson.php
8 months ago
Options_V2.php
9 months ago
Permalink.php
2 years ago
Post_types.php
1 year ago
Private_Course_Access.php
1 year ago
Q_And_A.php
10 months ago
Question_Answers_List.php
11 months ago
Quiz.php
9 months ago
QuizBuilder.php
11 months ago
Quiz_Attempts_List.php
9 months ago
RestAPI.php
2 years ago
Reviews.php
9 months ago
Rewrite_Rules.php
2 years ago
Shortcode.php
9 months ago
Singleton.php
1 year ago
Student.php
1 year ago
Students_List.php
1 year ago
Taxonomies.php
1 year ago
Template.php
9 months ago
Theme_Compatibility.php
3 years ago
Tools.php
1 year ago
Tools_V2.php
1 year ago
Tutor.php
9 months ago
TutorEDD.php
1 year ago
Tutor_Base.php
2 years ago
Tutor_Setup.php
1 year ago
Upgrader.php
9 months ago
User.php
1 year ago
Utils.php
8 months ago
Video_Stream.php
3 years ago
WhatsNew.php
9 months ago
Withdraw.php
1 year ago
Withdraw_Requests_List.php
11 months ago
WooCommerce.php
9 months ago
Withdraw_Requests_List.php
255 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage withdrawals |
| 4 | * |
| 5 | * @package Tutor\Withdraw |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace TUTOR; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | /** |
| 17 | * Handle withdraw request logic |
| 18 | * |
| 19 | * @since 1.0.0 |
| 20 | */ |
| 21 | class Withdraw_Requests_List { |
| 22 | /** |
| 23 | * List page slug |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | const WITHDRAW_REQUEST_LIST_PAGE = 'tutor_withdraw_requests'; |
| 30 | |
| 31 | /** |
| 32 | * Register hooks, resolve dependencies |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | /** |
| 38 | * Approve or reject withdraw request |
| 39 | */ |
| 40 | add_action( 'wp_ajax_tutor_admin_withdraw_action', array( $this, 'update_withdraw_status' ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Page title fallback |
| 45 | * |
| 46 | * @since 3.5.0 |
| 47 | * |
| 48 | * @param string $name Property name. |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | public function __get( $name ) { |
| 53 | if ( 'page_title' === $name ) { |
| 54 | return esc_html__( 'Withdraw Requests', 'tutor' ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Available tabs that will visible on the right side of page navbar |
| 60 | * |
| 61 | * @since 2.0.0 |
| 62 | * |
| 63 | * @param string $date withdraw request date | optional. |
| 64 | * @param string $search search by instructor name or email | optional. |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | public function tabs_key_value( $date = '', $search = '' ): array { |
| 69 | $approved = self::tabs_data( 'approved', $date, $search ); |
| 70 | $pending = self::tabs_data( 'pending', $date, $search ); |
| 71 | $rejected = self::tabs_data( 'rejected', $date, $search ); |
| 72 | |
| 73 | $url = apply_filters( 'tutor_data_tab_base_url', get_pagenum_link() ); |
| 74 | $tabs = array( |
| 75 | array( |
| 76 | 'key' => '', |
| 77 | 'title' => __( 'All', 'tutor' ), |
| 78 | 'value' => $approved + $pending + $rejected, |
| 79 | 'url' => $url . '&data=all', |
| 80 | ), |
| 81 | array( |
| 82 | 'key' => 'approved', |
| 83 | 'title' => __( 'Approved', 'tutor' ), |
| 84 | 'value' => $approved, |
| 85 | 'url' => $url . '&data=approved', |
| 86 | ), |
| 87 | array( |
| 88 | 'key' => 'pending', |
| 89 | 'title' => __( 'Pending', 'tutor' ), |
| 90 | 'value' => $pending, |
| 91 | 'url' => $url . '&data=pending', |
| 92 | ), |
| 93 | array( |
| 94 | 'key' => 'rejected', |
| 95 | 'title' => __( 'Rejected', 'tutor' ), |
| 96 | 'value' => $rejected, |
| 97 | 'url' => $url . '&data=rejected', |
| 98 | ), |
| 99 | ); |
| 100 | return $tabs; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get counted number of withdraw list by status ex: approved | pending | rejected |
| 105 | * |
| 106 | * @since 2.0.0 |
| 107 | * |
| 108 | * @param string $status status required | available : (approved | pending | rejected). |
| 109 | * @param string $date withdraw request date | optional | YYYY-MM-DD. |
| 110 | * @param string $search search by instructor name or email | optional. |
| 111 | * |
| 112 | * @return int |
| 113 | */ |
| 114 | public static function tabs_data( string $status, $date = '', $search = '' ): int { |
| 115 | global $wpdb; |
| 116 | $withdraw_table = $wpdb->prefix . 'tutor_withdraws'; |
| 117 | $user_table = $wpdb->users; |
| 118 | $status = sanitize_text_field( $status ); |
| 119 | $date = sanitize_text_field( $date ); |
| 120 | $search = sanitize_text_field( $search ); |
| 121 | // Prepare date query. |
| 122 | $date_query = ''; |
| 123 | if ( '' !== $date ) { |
| 124 | $date_query = "AND DATE(withdraw.created_at) = CAST('{$date}' AS DATE) "; |
| 125 | } |
| 126 | |
| 127 | // Prepare search query. |
| 128 | $search = '%' . $wpdb->esc_like( $search ) . '%'; |
| 129 | |
| 130 | $count = $wpdb->get_var( |
| 131 | $wpdb->prepare( |
| 132 | "SELECT count(*) FROM {$withdraw_table} AS withdraw |
| 133 | INNER JOIN {$user_table} AS user |
| 134 | ON user.ID = withdraw.user_id |
| 135 | WHERE withdraw.status = %s |
| 136 | {$date_query} |
| 137 | AND ( user.user_login LIKE %s OR user.user_nicename LIKE %s OR user.user_email LIKE %s OR user.display_name LIKE %s ) |
| 138 | ", |
| 139 | $status, |
| 140 | $search, |
| 141 | $search, |
| 142 | $search, |
| 143 | $search |
| 144 | ) |
| 145 | ); |
| 146 | return $count ? $count : 0; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Handle ajax request for updating withdraw status | available status (approved, rejected, pending) |
| 151 | * |
| 152 | * @since 2.0.0 |
| 153 | * |
| 154 | * @return string json response. |
| 155 | */ |
| 156 | public function update_withdraw_status() { |
| 157 | tutor_utils()->checking_nonce(); |
| 158 | |
| 159 | // Check if user is privileged. |
| 160 | if ( ! current_user_can( 'administrator' ) ) { |
| 161 | wp_send_json_error( tutor_utils()->error_message() ); |
| 162 | } |
| 163 | |
| 164 | $status = Input::post( 'action-type', '' ); |
| 165 | $withdraw_id = Input::post( 'withdraw-id', '' ); |
| 166 | $reject_type = Input::post( 'reject-type', '' ); |
| 167 | $reject_comment = Input::post( 'reject-comment', '' ); |
| 168 | |
| 169 | if ( '' === $withdraw_id ) { |
| 170 | return false; |
| 171 | } else { |
| 172 | $update = self::update( $status, $withdraw_id, $reject_type, $reject_comment ); |
| 173 | return $update ? wp_send_json( true ) : false; |
| 174 | } |
| 175 | exit; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Update withdraw status | available status (approved, rejected, pending) |
| 180 | * |
| 181 | * @since v2.0.0 |
| 182 | * |
| 183 | * @param string $status | required. |
| 184 | * @param int $withdraw_id | required. |
| 185 | * @param string $reject_type | optional. |
| 186 | * @param string $reject_comment | optional. |
| 187 | * |
| 188 | * @return bool json response. |
| 189 | */ |
| 190 | public static function update( string $status, int $withdraw_id, $reject_type = '', $reject_comment = '' ): bool { |
| 191 | global $wpdb; |
| 192 | $withdraw_table = $wpdb->prefix . 'tutor_withdraws'; |
| 193 | $withdraw_id = sanitize_text_field( $withdraw_id ); |
| 194 | $status = sanitize_text_field( $status ); |
| 195 | |
| 196 | // Prepare data for update. |
| 197 | $data = array( |
| 198 | 'status' => $status, |
| 199 | 'updated_at' => gmdate( 'Y-m-d H:i:s' ), |
| 200 | ); |
| 201 | |
| 202 | // If rejected then append reject_type and comment with method_data. |
| 203 | if ( 'rejected' === $status ) { |
| 204 | $withdraw = self::get_withdraw_by_id( $withdraw_id ); |
| 205 | if ( $withdraw ) { |
| 206 | $details = unserialize( $withdraw->method_data ); |
| 207 | |
| 208 | $details['rejects'] = array( |
| 209 | 'reject_type' => sanitize_text_field( $reject_type ), |
| 210 | 'reject_comment' => sanitize_text_field( $reject_comment ), |
| 211 | ); |
| 212 | $data['method_data'] = maybe_serialize( $details ); |
| 213 | |
| 214 | // Trigger email after rejecting withdraw. |
| 215 | do_action( 'tutor_after_rejected_withdraw', $withdraw_id ); |
| 216 | } |
| 217 | } else { |
| 218 | do_action( 'tutor_after_approved_withdraw', $withdraw_id ); |
| 219 | } |
| 220 | |
| 221 | // Update. |
| 222 | $update = $wpdb->update( |
| 223 | $withdraw_table, |
| 224 | $data, |
| 225 | array( |
| 226 | 'withdraw_id' => $withdraw_id, |
| 227 | ) |
| 228 | ); |
| 229 | return $update ? true : false; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Get withdraw by id |
| 235 | * |
| 236 | * @since v2.0.0 |
| 237 | * |
| 238 | * @param int $withdraw_id | required. |
| 239 | * |
| 240 | * @return object withdraw list. |
| 241 | */ |
| 242 | public static function get_withdraw_by_id( int $withdraw_id ) { |
| 243 | global $wpdb; |
| 244 | $withdraw_table = $wpdb->prefix . 'tutor_withdraws'; |
| 245 | return $wpdb->get_row( |
| 246 | $wpdb->prepare( |
| 247 | " SELECT *FROM {$withdraw_table} |
| 248 | WHERE withdraw_id = %d |
| 249 | ", |
| 250 | $withdraw_id |
| 251 | ) |
| 252 | ); |
| 253 | } |
| 254 | } |
| 255 |