surecart
/
app
/
src
/
Controllers
/
Admin
/
AffiliationReferrals
/
AffiliationReferralsListTable.php
AffiliationReferralsController.php
2 years ago
AffiliationReferralsListTable.php
2 years ago
AffiliationReferralsScriptsController.php
2 years ago
AffiliationReferralsListTable.php
427 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\AffiliationReferrals; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 6 | use SureCart\Models\Referral; |
| 7 | use SureCart\Support\Currency; |
| 8 | |
| 9 | /** |
| 10 | * Create a new table class that will extend the WP_List_Table |
| 11 | */ |
| 12 | class AffiliationReferralsListTable extends ListTable { |
| 13 | |
| 14 | /** |
| 15 | * Checkbox |
| 16 | * |
| 17 | * @var boolean |
| 18 | */ |
| 19 | public $checkbox = true; |
| 20 | |
| 21 | /** |
| 22 | * Error |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | public $error = ''; |
| 27 | |
| 28 | /** |
| 29 | * Pages |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | public $pages = array(); |
| 34 | |
| 35 | /** |
| 36 | * Prepare the items for the table to process |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function prepare_items() { |
| 41 | $columns = $this->get_columns(); |
| 42 | $hidden = $this->get_hidden_columns(); |
| 43 | $sortable = $this->get_sortable_columns(); |
| 44 | |
| 45 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 46 | |
| 47 | $query = $this->table_data(); |
| 48 | |
| 49 | if ( is_wp_error( $query ) ) { |
| 50 | $this->error = $query->get_error_message(); |
| 51 | $this->items = array(); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | $this->set_pagination_args( |
| 56 | array( |
| 57 | 'total_items' => $query->pagination->count, |
| 58 | 'per_page' => $this->get_items_per_page( 'affiliate_referrals' ), |
| 59 | ) |
| 60 | ); |
| 61 | |
| 62 | $this->items = $query->data; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get views for the list table status links. |
| 67 | * |
| 68 | * @global int $post_id |
| 69 | * @global string $comment_status |
| 70 | * @global string $comment_type |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | protected function get_views() { |
| 75 | foreach ( $this->getStatuses() as $status => $label ) { |
| 76 | $link = admin_url( 'admin.php?page=sc-affiliate-referrals' ); |
| 77 | |
| 78 | $current_link_attributes = ''; |
| 79 | |
| 80 | if ( ! empty( $_GET['status'] ) ) { |
| 81 | if ( $status === $_GET['status'] ) { |
| 82 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 83 | } |
| 84 | } elseif ( 'all' === $status ) { |
| 85 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 86 | } |
| 87 | |
| 88 | $link = add_query_arg( 'status', $status, $link ); |
| 89 | if ( isset( $_GET['live_mode'] ) ) { |
| 90 | $link = add_query_arg( 'live_mode', sanitize_text_field($_GET['live_mode']), $link ); |
| 91 | } |
| 92 | |
| 93 | $link = esc_url( $link ); |
| 94 | |
| 95 | $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . $label . '</a>'; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Filters the affiliation referrals status links. |
| 100 | * |
| 101 | * @since 2.5.0 |
| 102 | * @since 5.1.0 The 'Mine' link was added. |
| 103 | * |
| 104 | * @param string[] $status_links An associative array of fully-formed affiliation referrals status links. Includes 'All', 'Reviewing', 'Approved', 'Denied', 'Cancelled' and 'Paid Out'. |
| 105 | * */ |
| 106 | return apply_filters( 'sc_affiliation_referrals_status_links', $status_links ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Override the parent columns method. Defines the columns to use in your listing table |
| 111 | * |
| 112 | * @return array |
| 113 | */ |
| 114 | public function get_columns() { |
| 115 | return array( |
| 116 | 'date' => esc_html__( 'Date', 'surecart' ), |
| 117 | 'status' => esc_html__( 'Status', 'surecart' ), |
| 118 | 'payout_status' => esc_html__( 'Payout Status', 'surecart' ), |
| 119 | 'payout_date' => esc_html__( 'Payout Date', 'surecart' ), |
| 120 | 'affiliate' => esc_html__( 'Affiliate', 'surecart' ), |
| 121 | 'description' => esc_html__( 'Description', 'surecart' ), |
| 122 | 'order' => esc_html__( 'Order', 'surecart' ), |
| 123 | 'commission' => esc_html__( 'Commission', 'surecart' ), |
| 124 | 'mode' => '', |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Handle the date column. |
| 130 | * |
| 131 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 132 | * |
| 133 | * @return string |
| 134 | */ |
| 135 | public function column_date( $referral ) { |
| 136 | ob_start(); |
| 137 | echo wp_kses_post( date_i18n( get_option( 'date_format' ), $referral->created_at ) ); |
| 138 | ?> |
| 139 | <?php |
| 140 | echo wp_kses_post( |
| 141 | $this->row_actions( |
| 142 | array_filter( |
| 143 | array( |
| 144 | 'view' => '<a href="' . esc_url( \SureCart::getUrl()->edit( 'affiliate-referrals', $referral->id ) ) . '" aria-label="' . esc_attr__( 'View', 'surecart' ) . '">' . esc_html__( 'View', 'surecart' ) . '</a>', |
| 145 | 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->edit( 'affiliate-referrals', $referral->id ) ) . '" aria-label="' . esc_attr__( 'Edit', 'surecart' ) . '">' . esc_html__( 'Edit', 'surecart' ) . '</a>', |
| 146 | 'approve' => '<a href="' . esc_url( $this->get_action_url( $referral->id, 'approve' ) ) . '" aria-label="' . esc_attr__( 'Approve', 'surecart' ) . '">' . esc_html__( 'Approve', 'surecart' ) . '</a>', |
| 147 | 'deny' => '<a href="' . esc_url( $this->get_action_url( $referral->id, 'deny' ) ) . '" aria-label="' . esc_attr__( 'Deny', 'surecart' ) . '">' . esc_html__( 'Deny', 'surecart' ) . '</a>', |
| 148 | 'make_reviewing' => '<a href="' . esc_url( $this->get_action_url( $referral->id, 'make_reviewing' ) ) . '" aria-label="' . esc_attr__( 'Make Reviewing', 'surecart' ) . '">' . esc_html__( 'Make Reviewing', 'surecart' ) . '</a>', |
| 149 | 'delete' => '<a href="' . esc_url( $this->get_action_url( $referral->id, 'delete' ) ) . '" aria-label="' . esc_attr__( 'Delete', 'surecart' ) . '">' . esc_html__( 'Delete', 'surecart' ) . '</a>', |
| 150 | ), |
| 151 | function ( $action, $key ) use ( $referral ) { |
| 152 | // only view a paid referral. |
| 153 | if ( 'view' === $key && 'paid' !== $referral->status ) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | // don't edit a paid referral. |
| 158 | if ( 'paid' === $referral->status && 'view' !== $key ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // don't approve approved referral. |
| 163 | if ( 'approve' === $key && 'approved' === $referral->status ) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // don't deny denied referral. |
| 168 | if ( 'deny' === $key && 'denied' === $referral->status ) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // don't make reviewing reviewing referral. |
| 173 | if ( 'make_reviewing' === $key && 'reviewing' === $referral->status ) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | }, |
| 179 | ARRAY_FILTER_USE_BOTH |
| 180 | ) |
| 181 | ) |
| 182 | ) |
| 183 | ?> |
| 184 | <?php |
| 185 | return ob_get_clean(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Handle the status column. |
| 190 | * |
| 191 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 192 | * |
| 193 | * @return string |
| 194 | */ |
| 195 | public function column_status( $referral ) { |
| 196 | if ( empty( $referral->status_display_text ) ) { |
| 197 | return ''; |
| 198 | } |
| 199 | ob_start(); |
| 200 | ?> |
| 201 | <sc-tag type="<?php echo esc_attr( $referral->status_type ); ?>"> |
| 202 | <?php echo esc_html( $referral->status_display_text ); ?> |
| 203 | </sc-tag> |
| 204 | <?php |
| 205 | return ob_get_clean(); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Handle the payout status column. |
| 210 | * |
| 211 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 212 | * |
| 213 | * @return string |
| 214 | */ |
| 215 | public function column_payout_status( $referral ) { |
| 216 | if ( empty( $referral->payout ) ) { |
| 217 | return '_'; |
| 218 | } |
| 219 | |
| 220 | $type = 'completed' === $referral->payout->status ? 'success' : 'warning'; |
| 221 | |
| 222 | return '<sc-tag type="' . $type . '">' . $referral->payout->status_display_text . ' </sc-tag>'; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Handle the payout date column. |
| 227 | * |
| 228 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 229 | * |
| 230 | * @return string |
| 231 | */ |
| 232 | public function column_payout_date( $referral ) { |
| 233 | if ( empty( $referral->payout ) ) { |
| 234 | return '_'; |
| 235 | } |
| 236 | |
| 237 | return date_i18n( get_option( 'date_format' ), $referral->payout->created_at ); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /** |
| 242 | * Handle the affiliate column. |
| 243 | * |
| 244 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 245 | * |
| 246 | * @return string |
| 247 | */ |
| 248 | public function column_affiliate( $referral ) { |
| 249 | $affiliation = $referral->affiliation ?? null; |
| 250 | if ( empty( $affiliation->id ) ) { |
| 251 | return '-'; |
| 252 | } |
| 253 | ob_start(); |
| 254 | ?> |
| 255 | <div class="sc-affiliate-name"> |
| 256 | <a href="<?php echo esc_url( \SureCart::getUrl()->edit( 'affiliates', $affiliation->id ) ); ?>"> |
| 257 | <?php echo esc_html( $affiliation->display_name ); ?> |
| 258 | </a> |
| 259 | </div> |
| 260 | <?php |
| 261 | return ob_get_clean(); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Handle the description column. |
| 266 | * |
| 267 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 268 | * |
| 269 | * @return string |
| 270 | */ |
| 271 | public function column_description( $referral ) { |
| 272 | return esc_html( $referral->description ); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Handle the order column. |
| 277 | * |
| 278 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 279 | * |
| 280 | * @return string |
| 281 | */ |
| 282 | public function column_order( $referral ) { |
| 283 | $checkout = $referral->checkout ?? null; |
| 284 | if ( empty( $checkout->order->id ) ) { |
| 285 | return '-'; |
| 286 | } |
| 287 | ob_start(); |
| 288 | ?> |
| 289 | <a aria-label="<?php echo esc_attr__( 'View Order', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'order', $checkout->order->id ) ); ?>"> |
| 290 | #<?php echo esc_html( $checkout->order->number ?? $checkout->order->id ); ?> |
| 291 | </a> |
| 292 | <?php |
| 293 | return ob_get_clean(); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Handle the commission column. |
| 298 | * |
| 299 | * @param \SureCart\Models\Referral $referral The Referral model. |
| 300 | * |
| 301 | * @return string |
| 302 | */ |
| 303 | public function column_commission( $referral ) { |
| 304 | return Currency::format( $referral->commission_amount, $referral->currency ); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Get the table data |
| 309 | * |
| 310 | * @return \SureCart\Models\Collection; |
| 311 | */ |
| 312 | private function table_data() { |
| 313 | return Referral::where( |
| 314 | array( |
| 315 | 'query' => $this->get_search_query(), |
| 316 | 'status' => [ $this->getFilteredStatus() ], |
| 317 | 'live_mode' => 'false' !== sanitize_text_field( wp_unslash( $_GET['live_mode'] ?? '' ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 318 | 'expand' => [ |
| 319 | 'affiliation', |
| 320 | 'checkout', |
| 321 | 'checkout.order', |
| 322 | 'payout', |
| 323 | ], |
| 324 | ) |
| 325 | )->paginate( |
| 326 | array( |
| 327 | 'per_page' => $this->get_items_per_page( 'affiliate_referrals' ), |
| 328 | 'page' => $this->get_pagenum(), |
| 329 | ) |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Nothing found. |
| 335 | * |
| 336 | * @return void |
| 337 | */ |
| 338 | public function no_items() { |
| 339 | if ( $this->error ) { |
| 340 | echo esc_html( $this->error ); |
| 341 | return; |
| 342 | } |
| 343 | echo esc_html_e( 'No affiliate referrals found . ', 'surecart' ); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Displays extra table navigation. |
| 348 | * |
| 349 | * @param string $which Top or bottom placement. |
| 350 | */ |
| 351 | protected function extra_tablenav( $which ) { |
| 352 | ?> |
| 353 | <input type="hidden" name="page" value="sc-affiliate-requests" /> |
| 354 | |
| 355 | <?php if ( ! empty( $_GET['status'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?> |
| 356 | <input type="hidden" name="status" value="<?php echo esc_attr( $_GET['status'] ); ?>" /> |
| 357 | <?php endif; ?> |
| 358 | |
| 359 | <?php |
| 360 | /** |
| 361 | * Fires immediately following the closing "actions" div in the tablenav |
| 362 | * for the affiliate referrals list table. |
| 363 | * |
| 364 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 365 | */ |
| 366 | do_action( 'manage_affiliate_referrals_extra_tablenav', $which ); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Get filtered status / default status. |
| 371 | * |
| 372 | * @return string|null |
| 373 | */ |
| 374 | private function getFilteredStatus() { |
| 375 | if ( empty( $_GET['status'] ) || 'all' === $_GET['status'] ) { |
| 376 | return null; |
| 377 | } |
| 378 | |
| 379 | // transform in payout status. |
| 380 | if ( 'in_payout' === $_GET['status'] ) { |
| 381 | return 'paid'; |
| 382 | } |
| 383 | |
| 384 | return sanitize_text_field( wp_unslash( $_GET['status'] ) ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Get all statuses. |
| 389 | * |
| 390 | * @return array |
| 391 | */ |
| 392 | private function getStatuses(): array { |
| 393 | return array( |
| 394 | 'all' => __( 'All', 'surecart' ), |
| 395 | 'reviewing' => __( 'Reviewing', 'surecart' ), |
| 396 | 'approved' => __( 'Approved', 'surecart' ), |
| 397 | 'denied' => __( 'Denied', 'surecart' ), |
| 398 | 'canceled' => __( 'Canceled', 'surecart' ), |
| 399 | 'in_payout' => __( 'In Payout', 'surecart' ), |
| 400 | ); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Get action url. |
| 405 | * |
| 406 | * @param int $id The id. |
| 407 | * @param string $action The action. |
| 408 | * |
| 409 | * @return string |
| 410 | */ |
| 411 | public function get_action_url( $id, $action ) { |
| 412 | return esc_url( |
| 413 | add_query_arg( |
| 414 | array_filter( |
| 415 | [ |
| 416 | 'action' => $action, |
| 417 | 'nonce' => wp_create_nonce( $action . '_affiliation' ), |
| 418 | 'id' => $id, |
| 419 | 'live_mode' => $_GET['live_mode'] ?? '', |
| 420 | ] |
| 421 | ), |
| 422 | esc_url_raw( admin_url( 'admin.php?page=sc-affiliate-referrals' ) ) |
| 423 | ) |
| 424 | ); |
| 425 | } |
| 426 | } |
| 427 |