OrderScriptsController.php
3 years ago
OrdersListTable.php
2 years ago
OrdersViewController.php
2 years ago
OrdersListTable.php
455 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Orders; |
| 4 | |
| 5 | use SureCart\Models\Order; |
| 6 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 7 | |
| 8 | /** |
| 9 | * Create a new table class that will extend the WP_List_Table |
| 10 | */ |
| 11 | class OrdersListTable extends ListTable { |
| 12 | /** |
| 13 | * Prepare the items for the table to process |
| 14 | * |
| 15 | * @return Void |
| 16 | */ |
| 17 | public function prepare_items() { |
| 18 | $columns = $this->get_columns(); |
| 19 | $hidden = $this->get_hidden_columns(); |
| 20 | $sortable = $this->get_sortable_columns(); |
| 21 | |
| 22 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 23 | |
| 24 | $query = $this->table_data(); |
| 25 | if ( is_wp_error( $query ) ) { |
| 26 | $this->items = []; |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | $this->set_pagination_args( |
| 31 | [ |
| 32 | 'total_items' => $query->pagination->count, |
| 33 | 'per_page' => $this->get_items_per_page( 'orders' ), |
| 34 | ] |
| 35 | ); |
| 36 | |
| 37 | $this->items = $query->data; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Show any integrations. |
| 42 | * |
| 43 | * @param \SureCart\Models\Order $order Order. |
| 44 | */ |
| 45 | public function column_integrations( $order ) { |
| 46 | $output = ''; |
| 47 | |
| 48 | // loop through each purchase. |
| 49 | if ( ! empty( $order->checkout->purchases->data ) ) { |
| 50 | foreach ( $order->checkout->purchases->data as $purchase ) { |
| 51 | $output .= $this->productIntegrationsList( $purchase->product ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return $output ? $output : '-'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @global int $post_id |
| 60 | * @global string $comment_status |
| 61 | * @global string $comment_type |
| 62 | */ |
| 63 | protected function get_views() { |
| 64 | $stati = [ |
| 65 | 'all' => __( 'All', 'surecart' ), |
| 66 | 'paid' => __( 'Paid', 'surecart' ), |
| 67 | 'processing' => __( 'Processing', 'surecart' ), |
| 68 | 'payment_failed' => __( 'Failed', 'surecart' ), |
| 69 | 'canceled' => __( 'Canceled', 'surecart' ), |
| 70 | ]; |
| 71 | |
| 72 | $link = \SureCart::getUrl()->index( 'orders' ); |
| 73 | |
| 74 | foreach ( $stati as $status => $label ) { |
| 75 | $current_link_attributes = ''; |
| 76 | |
| 77 | if ( ! empty( $_GET['status'] ) ) { |
| 78 | if ( $status === $_GET['status'] ) { |
| 79 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 80 | } |
| 81 | } elseif ( 'all' === $status ) { |
| 82 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 83 | } |
| 84 | |
| 85 | $link = add_query_arg( 'status', $status, $link ); |
| 86 | |
| 87 | $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . $label . '</a>'; |
| 88 | } |
| 89 | |
| 90 | // filter links. |
| 91 | return apply_filters( 'sc_order_status_links', $status_links ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Override the parent columns method. Defines the columns to use in your listing table |
| 96 | * |
| 97 | * @return array |
| 98 | */ |
| 99 | public function get_columns() { |
| 100 | |
| 101 | $columns = [ |
| 102 | // 'cb' => '<input type="checkbox" />', |
| 103 | 'order' => __( 'Order', 'surecart' ), |
| 104 | 'status' => __( 'Status', 'surecart' ), |
| 105 | 'fulfillment_status' => __( 'Fulfillment', 'surecart' ), |
| 106 | 'shipment_status' => __( 'Shipping', 'surecart' ), |
| 107 | 'method' => __( 'Method', 'surecart' ), |
| 108 | 'integrations' => __( 'Integrations', 'surecart' ), |
| 109 | 'total' => __( 'Total', 'surecart' ), |
| 110 | 'type' => __( 'Type', 'surecart' ), |
| 111 | 'created' => __( 'Date', 'surecart' ), |
| 112 | 'mode' => '', |
| 113 | ]; |
| 114 | |
| 115 | return $columns; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Displays the checkbox column. |
| 120 | * |
| 121 | * @param Product $product The product model. |
| 122 | */ |
| 123 | public function column_cb( $product ) { |
| 124 | ?> |
| 125 | <label class="screen-reader-text" for="cb-select-<?php echo esc_attr( $product['id'] ); ?>"><?php _e( 'Select comment', 'surecart' ); ?></label> |
| 126 | <input id="cb-select-<?php echo esc_attr( $product['id'] ); ?>" type="checkbox" name="delete_comments[]" value="<?php echo esc_attr( $product['id'] ); ?>" /> |
| 127 | <?php |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Define which columns are hidden |
| 132 | * |
| 133 | * @return Array |
| 134 | */ |
| 135 | public function get_hidden_columns() { |
| 136 | return ( is_array( get_user_meta( get_current_user_id(), 'managesurecart_page_sc-orderscolumnshidden', true ) ) ) ? get_user_meta( get_current_user_id(), 'managesurecart_page_sc-orderscolumnshidden', true ) : array(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Define the sortable columns |
| 141 | * |
| 142 | * @return Array |
| 143 | */ |
| 144 | public function get_sortable_columns() { |
| 145 | return array( 'title' => array( 'title', false ) ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get the table data |
| 150 | * |
| 151 | * @return Array |
| 152 | */ |
| 153 | protected function table_data() { |
| 154 | return Order::where( |
| 155 | [ |
| 156 | 'status' => $this->getStatus(), |
| 157 | 'fulfillment_status' => ! empty( $_GET['fulfillment_status'] ) ? [ $_GET['fulfillment_status'] ] : [], |
| 158 | 'shipment_status' => ! empty( $_GET['shipment_status'] ) ? [ $_GET['shipment_status'] ] : [], |
| 159 | 'query' => $this->get_search_query(), |
| 160 | ] |
| 161 | )->with( [ 'checkout', 'checkout.charge', 'checkout.customer', 'checkout.payment_method', 'checkout.manual_payment_method', 'checkout.purchases', 'payment_method.card', 'payment_method.payment_instrument', 'payment_method.paypal_account', 'payment_method.bank_account' ] ) |
| 162 | ->paginate( |
| 163 | [ |
| 164 | 'per_page' => $this->get_items_per_page( 'orders' ), |
| 165 | 'page' => $this->get_pagenum(), |
| 166 | ] |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get the archive query status. |
| 172 | * |
| 173 | * @return boolean|null |
| 174 | */ |
| 175 | public function getStatus() { |
| 176 | $status = sanitize_text_field( wp_unslash( $_GET['status'] ?? 'all' ) ); |
| 177 | if ( 'paid' === $status ) { |
| 178 | return [ 'paid' ]; |
| 179 | } |
| 180 | if ( 'payment_failed' === $status ) { |
| 181 | return [ 'payment_failed' ]; |
| 182 | } |
| 183 | if ( 'processing' === $status ) { |
| 184 | return [ 'processing' ]; |
| 185 | } |
| 186 | if ( 'canceled' === $status ) { |
| 187 | return [ 'canceled' ]; |
| 188 | } |
| 189 | if ( 'all' === $status ) { |
| 190 | return []; |
| 191 | } |
| 192 | return $status ? [ esc_html( $status ) ] : []; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Handle the total column |
| 197 | * |
| 198 | * @param \SureCart\Models\Order $order The order model. |
| 199 | * |
| 200 | * @return string |
| 201 | */ |
| 202 | public function column_total( $order ) { |
| 203 | return '<sc-format-number type="currency" currency="' . strtoupper( esc_html( $order->checkout->currency ) ) . '" value="' . (float) $order->checkout->amount_due . '"></sc-format-number>'; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Show the payment method for the order. |
| 208 | * |
| 209 | * @param \SureCart\Models\Order $order The order model. |
| 210 | * |
| 211 | * @return string |
| 212 | */ |
| 213 | public function column_method( $order ) { |
| 214 | if ( isset( $order->checkout->manual_payment_method->name ) ) { |
| 215 | return '<sc-tag>' . $order->checkout->manual_payment_method->name . '</sc-tag>'; |
| 216 | } |
| 217 | ob_start(); |
| 218 | ?> |
| 219 | <sc-payment-method id="sc-method-<?php echo esc_attr( $order->id ); ?>"></sc-payment-method> |
| 220 | <script> |
| 221 | document.getElementById('sc-method-<?php echo esc_attr( $order->id ); ?>').paymentMethod = <?php echo wp_json_encode( $order->checkout->payment_method ); ?>; |
| 222 | </script> |
| 223 | <?php |
| 224 | return ob_get_clean(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * The type of order. |
| 229 | * |
| 230 | * @param \SureCart\Models\Order $order The order model. |
| 231 | * |
| 232 | * @return string |
| 233 | */ |
| 234 | public function column_type( $order ) { |
| 235 | if ( ! empty( $order->order_type ) && 'subscription' === $order->order_type ) { |
| 236 | return '<sc-tag type="success">' . esc_html__( 'Plan Renewal', 'surecart' ) . '</sc-tag>'; |
| 237 | } |
| 238 | |
| 239 | return '<sc-tag type="info">' . esc_html__( 'Checkout', 'surecart' ) . '</sc-tag>'; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Handle the status |
| 244 | * |
| 245 | * @param \SureCart\Models\Order $order Order Model. |
| 246 | * |
| 247 | * @return string |
| 248 | */ |
| 249 | public function column_status( $order ) { |
| 250 | if ( ! empty( $order->checkout->charge->fully_refunded ) ) { |
| 251 | return '<sc-tag type="danger">' . __( 'Refunded', 'surecart' ) . '</sc-tag>'; |
| 252 | } |
| 253 | |
| 254 | if ( ! empty( $order->checkout->payment_method->processor_type ) && 'paypal' === $order->checkout->payment_method->processor_type ) { |
| 255 | if ( 'requires_approval' === $order->status ) { |
| 256 | return '<sc-tooltip text="' . __( 'Paypal is taking a closer look at this payment. It’s required for some payments and normally takes up to 3 business days.', 'surecart' ) . '" type="warning"><sc-order-status-badge status="' . esc_attr( $order->status ) . '"></sc-order-status-badge></sc-tooltip>'; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | return '<sc-order-status-badge status="' . esc_attr( $order->status ) . '"></sc-order-status-badge>'; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Handle the status |
| 265 | * |
| 266 | * @param \SureCart\Models\Order $order Order Model. |
| 267 | * |
| 268 | * @return string |
| 269 | */ |
| 270 | public function column_fulfillment_status( $order ) { |
| 271 | ob_start(); |
| 272 | ?> |
| 273 | <sc-order-fulfillment-badge status="<?php echo esc_attr( $order->fulfillment_status ); ?>"></sc-order-fulfillment-badge> |
| 274 | <?php |
| 275 | return ob_get_clean(); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Handle the status |
| 280 | * |
| 281 | * @param \SureCart\Models\Order $order Order Model. |
| 282 | * |
| 283 | * @return string |
| 284 | */ |
| 285 | public function column_shipment_status( $order ) { |
| 286 | ob_start(); |
| 287 | ?> |
| 288 | <?php if ( 'unshippable' === $order->shipment_status ) : ?> |
| 289 | - |
| 290 | <?php else : ?> |
| 291 | <sc-order-shipment-badge status="<?php echo esc_attr( $order->shipment_status ); ?>"></sc-order-shipment-badge> |
| 292 | <?php endif; ?> |
| 293 | <?php |
| 294 | return ob_get_clean(); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Name of the coupon |
| 299 | * |
| 300 | * @param \SureCart\Models\Promotion $promotion Promotion model. |
| 301 | * |
| 302 | * @return string |
| 303 | */ |
| 304 | public function column_order( $order ) { |
| 305 | ob_start(); |
| 306 | ?> |
| 307 | <a class="row-title" aria-label="<?php echo esc_attr__( 'Edit Order', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'order', $order->id ) ); ?>"> |
| 308 | #<?php echo sanitize_text_field( $order->number ?? $order->id ); ?> |
| 309 | </a> |
| 310 | <br /> |
| 311 | <a aria-label="<?php echo esc_attr__( 'Edit Order', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'order', $order->id ) ); ?>" style="word-break: break-word"> |
| 312 | <?php |
| 313 | // translators: Customer name. |
| 314 | echo sprintf( esc_html__( 'By %s', 'surecart' ), esc_html( $order->checkout->customer->name ?? $order->checkout->customer->email ) ); |
| 315 | ?> |
| 316 | </a> |
| 317 | <?php |
| 318 | |
| 319 | return ob_get_clean(); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Displays a formats drop-down for filtering items. |
| 324 | * |
| 325 | * @since 5.2.0 |
| 326 | * @access protected |
| 327 | * |
| 328 | * @param string $post_type Post type slug. |
| 329 | */ |
| 330 | protected function fulfillment_dropdown() { |
| 331 | /** |
| 332 | * Filters whether to remove the 'Formats' drop-down from the post list table. |
| 333 | * |
| 334 | * @param bool $disable Whether to disable the drop-down. Default false. |
| 335 | */ |
| 336 | if ( apply_filters( 'surecart/disable_fulfillment_dropdown', false ) ) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | $displayed_order_fulfillment = isset( $_GET['fulfillment_status'] ) ? $_GET['fulfillment_status'] : ''; |
| 341 | ?> |
| 342 | |
| 343 | <label for="filter-by-fulfillment" class="screen-reader-text"> |
| 344 | <?php |
| 345 | /* translators: Hidden accessibility text. */ |
| 346 | esc_html_e( 'Filter by fulfillment', 'surecart' ); |
| 347 | ?> |
| 348 | </label> |
| 349 | <select name="fulfillment_status" id="filter-by-fulfillment"> |
| 350 | <option<?php selected( $displayed_order_fulfillment, '' ); ?> value=""><?php esc_html_e( 'All Fulfillments', 'surecart' ); ?></option> |
| 351 | <option<?php selected( $displayed_order_fulfillment, 'unfulfilled' ); ?> value="unfulfilled"><?php echo esc_html_e( 'Unfulfilled', 'surecart' ); ?></option> |
| 352 | <option<?php selected( $displayed_order_fulfillment, 'fulfilled' ); ?> value="fulfilled"><?php echo esc_html_e( 'Fulfilled', 'surecart' ); ?></option> |
| 353 | <option<?php selected( $displayed_order_fulfillment, '"partially_fulfilled' ); ?> value="fulfilled"><?php echo esc_html_e( 'Partially Fulfilled', 'surecart' ); ?></option> |
| 354 | </select> |
| 355 | <?php |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Displays a formats drop-down for filtering items. |
| 360 | * |
| 361 | * @since 5.2.0 |
| 362 | * @access protected |
| 363 | * |
| 364 | * @param string $post_type Post type slug. |
| 365 | */ |
| 366 | protected function shipment_dropdown() { |
| 367 | /** |
| 368 | * Filters whether to remove the 'Formats' drop-down from the post list table. |
| 369 | * |
| 370 | * @param bool $disable Whether to disable the drop-down. Default false. |
| 371 | */ |
| 372 | if ( apply_filters( 'surecart/disable_shipment_dropdown', false ) ) { |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | $displayed_order_shipment = isset( $_GET['shipment_status'] ) ? $_GET['shipment_status'] : ''; |
| 377 | ?> |
| 378 | |
| 379 | <label for="filter-by-shipment" class="screen-reader-text"> |
| 380 | <?php |
| 381 | /* translators: Hidden accessibility text. */ |
| 382 | esc_html_e( 'Filter by shipment', 'surecart' ); |
| 383 | ?> |
| 384 | </label> |
| 385 | <select name="shipment_status" id="filter-by-shipment"> |
| 386 | <option<?php selected( $displayed_order_shipment, '' ); ?> value=""><?php esc_html_e( 'All Shipment Statuses', 'surecart' ); ?></option> |
| 387 | <option<?php selected( $displayed_order_shipment, 'unshipped' ); ?> value="unshipped"><?php echo esc_html_e( 'Not Shipped', 'surecart' ); ?></option> |
| 388 | <option<?php selected( $displayed_order_shipment, 'shipped' ); ?> value="shipped"><?php echo esc_html_e( 'Shipped', 'surecart' ); ?></option> |
| 389 | <option<?php selected( $displayed_order_shipment, 'partially_shipped' ); ?> value="partially_shipped"><?php echo esc_html_e( 'Partially Shipped', 'surecart' ); ?></option> |
| 390 | <option<?php selected( $displayed_order_shipment, 'delivered' ); ?> value="delivered"><?php echo esc_html_e( 'Delivered', 'surecart' ); ?></option> |
| 391 | </select> |
| 392 | <?php |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Displays extra table navigation. |
| 397 | * |
| 398 | * @param string $which Top or bottom placement. |
| 399 | */ |
| 400 | protected function extra_tablenav( $which ) { |
| 401 | ?> |
| 402 | <input type="hidden" name="page" value="sc-orders" /> |
| 403 | |
| 404 | <?php if ( ! empty( $_GET['status'] ) ) : ?> |
| 405 | <input type="hidden" name="status" value="<?php echo esc_attr( $_GET['status'] ); ?>" /> |
| 406 | <?php endif; ?> |
| 407 | |
| 408 | <div class="alignleft actions"> |
| 409 | <?php |
| 410 | if ( 'top' === $which ) { |
| 411 | ob_start(); |
| 412 | $this->fulfillment_dropdown(); |
| 413 | $this->shipment_dropdown(); |
| 414 | |
| 415 | /** |
| 416 | * Fires before the Filter button on the Posts and Pages list tables. |
| 417 | * |
| 418 | * The Filter button allows sorting by date and/or category on the |
| 419 | * Posts list table, and sorting by date on the Pages list table. |
| 420 | * |
| 421 | * @since 2.1.0 |
| 422 | * @since 4.4.0 The `$post_type` parameter was added. |
| 423 | * @since 4.6.0 The `$which` parameter was added. |
| 424 | * |
| 425 | * @param string $post_type The post type slug. |
| 426 | * @param string $which The location of the extra table nav markup: |
| 427 | * 'top' or 'bottom' for WP_Posts_List_Table, |
| 428 | * 'bar' for WP_Media_List_Table. |
| 429 | */ |
| 430 | do_action( 'restrict_manage_orders', $this->screen->post_type, $which ); |
| 431 | |
| 432 | $output = ob_get_clean(); |
| 433 | |
| 434 | if ( ! empty( $output ) ) { |
| 435 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 436 | submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'filter-by-fulfillment-submit' ) ); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | ?> |
| 441 | </div> |
| 442 | |
| 443 | <?php |
| 444 | /** |
| 445 | * Fires immediately following the closing "actions" div in the tablenav for the posts |
| 446 | * list table. |
| 447 | * |
| 448 | * @since 4.4.0 |
| 449 | * |
| 450 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 451 | */ |
| 452 | do_action( 'manage_orders_extra_tablenav', $which ); |
| 453 | } |
| 454 | } |
| 455 |