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