AffiliationPayoutsController.php
2 years ago
AffiliationPayoutsListTable.php
1 year ago
AffiliationPayoutsScriptsController.php
2 years ago
AffiliationPayoutsListTable.php
343 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\AffiliationPayouts; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 6 | use SureCart\Models\Payout; |
| 7 | |
| 8 | /** |
| 9 | * Create a new table class that will extend the WP_List_Table |
| 10 | */ |
| 11 | class AffiliationPayoutsListTable extends ListTable { |
| 12 | |
| 13 | /** |
| 14 | * Checkbox column. |
| 15 | * |
| 16 | * @var boolean |
| 17 | */ |
| 18 | public $checkbox = true; |
| 19 | |
| 20 | /** |
| 21 | * Error message. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public $error = ''; |
| 26 | |
| 27 | /** |
| 28 | * Pages. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | public $pages = array(); |
| 33 | |
| 34 | /** |
| 35 | * Prepare the items for the table to process |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function prepare_items() { |
| 40 | $columns = $this->get_columns(); |
| 41 | $hidden = $this->get_hidden_columns(); |
| 42 | $sortable = $this->get_sortable_columns(); |
| 43 | |
| 44 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 45 | |
| 46 | $query = $this->table_data(); |
| 47 | |
| 48 | if ( is_wp_error( $query ) ) { |
| 49 | $this->error = $query->get_error_message(); |
| 50 | $this->items = array(); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $this->set_pagination_args( |
| 55 | array( |
| 56 | 'total_items' => $query->pagination->count, |
| 57 | 'per_page' => $this->get_items_per_page( 'affiliate_payouts' ), |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | $this->items = $query->data; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get views for the list table status links. |
| 66 | * |
| 67 | * @global int $post_id |
| 68 | * @global string $comment_status |
| 69 | * @global string $comment_type |
| 70 | */ |
| 71 | protected function get_views() { |
| 72 | foreach ( $this->getStatuses() as $status => $label ) { |
| 73 | $current_link_attributes = ''; |
| 74 | $link = admin_url( 'admin.php?page=sc-affiliate-payouts' ); |
| 75 | |
| 76 | if ( ! empty( $_GET['status'] ) ) { |
| 77 | if ( $status === $_GET['status'] ) { |
| 78 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 79 | } |
| 80 | } elseif ( 'all' === $status ) { |
| 81 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 82 | } |
| 83 | |
| 84 | $link = add_query_arg( 'status', $status, $link ); |
| 85 | |
| 86 | $link = esc_url( $link ); |
| 87 | |
| 88 | $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . $label . '</a>'; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Filters the comment status links. |
| 93 | * |
| 94 | * @since 2.5.0 |
| 95 | * @since 5.1.0 The 'Mine' link was added. |
| 96 | * |
| 97 | * @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine','Pending', 'Approved', 'Spam', and 'Trash'. |
| 98 | * */ |
| 99 | return apply_filters( 'payout_status_links', $status_links ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Override the parent columns method. Defines the columns to use in your listing table |
| 104 | * |
| 105 | * @return array |
| 106 | */ |
| 107 | public function get_columns() { |
| 108 | return array( |
| 109 | 'date' => __( 'Date', 'surecart' ), |
| 110 | 'status' => __( 'Status', 'surecart' ), |
| 111 | 'affiliate' => __( 'Affiliate', 'surecart' ), |
| 112 | 'payout_email' => __( 'Payout Email', 'surecart' ), |
| 113 | 'period_end' => __( 'Period End', 'surecart' ), |
| 114 | 'referrals' => __( 'Referrals', 'surecart' ), |
| 115 | 'amount' => __( 'Amount', 'surecart' ), |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Handle the date column. |
| 121 | * |
| 122 | * @param \SureCart\Models\Payout $payout The Payout model. |
| 123 | * |
| 124 | * @return string |
| 125 | */ |
| 126 | public function column_date( $payout ) { |
| 127 | $date = date_i18n( get_option( 'date_format' ), $payout->created_at ); |
| 128 | |
| 129 | $actions = wp_kses_post( |
| 130 | $this->row_actions( |
| 131 | array_filter( |
| 132 | array( |
| 133 | 'complete' => '<a href="' . esc_url( $this->get_action_url( $payout->id, 'complete' ) ) . '">' . esc_html__( 'Complete', 'surecart' ) . '</a>', |
| 134 | 'make_processing' => '<a href="' . esc_url( $this->get_action_url( $payout->id, 'make_processing' ) ) . '">' . esc_html__( 'Make Processing', 'surecart' ) . '</a>', |
| 135 | 'delete' => '<a href="' . esc_url( $this->get_action_url( $payout->id, 'delete' ) ) . '">' . esc_html__( 'Delete', 'surecart' ) . '</a>', |
| 136 | ), |
| 137 | function( $action, $key ) use ( $payout ) { |
| 138 | if ( 'complete' === $key && 'completed' === $payout->status ) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | if ( 'make_processing' === $key && 'processing' === $payout->status ) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | return true; |
| 147 | }, |
| 148 | ARRAY_FILTER_USE_BOTH |
| 149 | ) |
| 150 | ) |
| 151 | ); |
| 152 | |
| 153 | return $date . $actions; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Handle the status column. |
| 158 | * |
| 159 | * @param \SureCart\Models\Payout $payout The Payout model. |
| 160 | * |
| 161 | * @return string |
| 162 | */ |
| 163 | public function column_status( $payout ) { |
| 164 | $type = 'completed' === $payout->status ? 'success' : 'warning'; |
| 165 | |
| 166 | return '<sc-tag type="' . $type . '">' . $payout->status_display_text . ' </sc-tag>'; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Handle the affiliate column. |
| 171 | * |
| 172 | * @param \SureCart\Models\Payout $payout The Payout model. |
| 173 | * |
| 174 | * @return string |
| 175 | */ |
| 176 | public function column_affiliate( $payout ) { |
| 177 | $affiliation = $payout->affiliation ?? null; |
| 178 | if ( empty( $affiliation->id ) ) { |
| 179 | return ''; |
| 180 | } |
| 181 | |
| 182 | ob_start(); |
| 183 | ?> |
| 184 | <div class="sc-affiliate-name"> |
| 185 | <a href="<?php echo esc_url( \SureCart::getUrl()->edit( 'affiliates', $affiliation->id ) ); ?>"> |
| 186 | <?php echo esc_html( $affiliation->display_name ); ?> |
| 187 | </a> |
| 188 | </div> |
| 189 | <?php |
| 190 | return ob_get_clean(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Handle the payout email column. |
| 195 | * |
| 196 | * @param \SureCart\Models\Payout $payout The Payout model. |
| 197 | * |
| 198 | * @return string |
| 199 | */ |
| 200 | public function column_payout_email( $payout ) { |
| 201 | return esc_html( $payout->affiliation->payout_email ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Handle the period end column. |
| 206 | * |
| 207 | * @param \SureCart\Models\Payout $payout The Payout model. |
| 208 | * |
| 209 | * @return string |
| 210 | */ |
| 211 | public function column_period_end( $payout ) { |
| 212 | return "<sc-format-date date='$payout->end_date' type='timestamp' month='short' day='numeric' year='numeric' ></sc-format-date>"; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Handle the referrals column. |
| 217 | * |
| 218 | * @param \SureCart\Models\Payout $payout The Payout model. |
| 219 | * |
| 220 | * @return string |
| 221 | */ |
| 222 | public function column_referrals( $payout ) { |
| 223 | return (int) $payout->referrals->pagination->count; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Handle the amount column. |
| 228 | * |
| 229 | * @param \SureCart\Models\Payout $payout The Payout model. |
| 230 | * |
| 231 | * @return string |
| 232 | */ |
| 233 | public function column_amount( $payout ) { |
| 234 | return '<sc-format-number type="currency" currency="' . $payout->currency . '" value="' . $payout->total_commission_amount . '"></sc-format-number>'; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the table data |
| 239 | * |
| 240 | * @return \SureCart\Models\Collection |
| 241 | */ |
| 242 | private function table_data() { |
| 243 | return Payout::where( |
| 244 | array( |
| 245 | 'query' => $this->get_search_query(), |
| 246 | 'status' => [ $this->getFilteredStatus() ], |
| 247 | 'expand' => [ |
| 248 | 'affiliation', |
| 249 | 'referrals', |
| 250 | ], |
| 251 | ) |
| 252 | )->paginate( |
| 253 | array( |
| 254 | 'per_page' => $this->get_items_per_page( 'affiliate_payouts' ), |
| 255 | 'page' => $this->get_pagenum(), |
| 256 | ) |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Nothing found. |
| 262 | * |
| 263 | * @return void |
| 264 | */ |
| 265 | public function no_items() { |
| 266 | if ( $this->error ) { |
| 267 | echo esc_html( $this->error ); |
| 268 | return; |
| 269 | } |
| 270 | echo esc_html_e( 'No affiliate payouts found.', 'surecart' ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Displays extra table navigation. |
| 275 | * |
| 276 | * @param string $which Top or bottom placement. |
| 277 | */ |
| 278 | protected function extra_tablenav( $which ) { |
| 279 | ?> |
| 280 | <input type="hidden" name="page" value="sc-affiliate-payouts" /> |
| 281 | |
| 282 | <?php if ( ! empty( $_GET['status'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?> |
| 283 | <input type="hidden" name="status" value="<?php echo esc_attr( $_GET['status'] ); ?>" /> |
| 284 | <?php endif; ?> |
| 285 | |
| 286 | <?php |
| 287 | /** |
| 288 | * Fires immediately following the closing "actions" div in the tablenav |
| 289 | * for the affiliate_payouts list table. |
| 290 | * |
| 291 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 292 | */ |
| 293 | do_action( 'manage_affiliate_payouts_extra_tablenav', $which ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Get filtered status / default status. |
| 298 | * |
| 299 | * @return string|null |
| 300 | */ |
| 301 | private function getFilteredStatus() { |
| 302 | if ( empty( $_GET['status'] ) || 'all' === $_GET['status'] ) { |
| 303 | return null; |
| 304 | } |
| 305 | |
| 306 | return sanitize_text_field( wp_unslash( $_GET['status'] ) ); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Get all statuses. |
| 311 | * |
| 312 | * @return array |
| 313 | */ |
| 314 | private function getStatuses(): array { |
| 315 | return array( |
| 316 | 'all' => __( 'All', 'surecart' ), |
| 317 | 'processing' => __( 'Processing', 'surecart' ), |
| 318 | 'completed' => __( 'Completed', 'surecart' ), |
| 319 | ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Get action url. |
| 324 | * |
| 325 | * @param int $id The id. |
| 326 | * @param string $action The action. |
| 327 | * |
| 328 | * @return string |
| 329 | */ |
| 330 | public function get_action_url( $id, $action ) { |
| 331 | return esc_url( |
| 332 | add_query_arg( |
| 333 | [ |
| 334 | 'action' => $action, |
| 335 | 'nonce' => wp_create_nonce( $action . '_affiliation_payout' ), |
| 336 | 'id' => $id, |
| 337 | ], |
| 338 | esc_url_raw( admin_url( 'admin.php?page=sc-affiliate-payouts' ) ) |
| 339 | ) |
| 340 | ); |
| 341 | } |
| 342 | } |
| 343 |