AbandonedCheckoutListTable.php
1 year ago
AbandonedCheckoutScriptsController.php
3 years ago
AbandonedCheckoutViewController.php
1 year ago
AbandonedCheckoutListTable.php
308 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Abandoned; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 6 | use SureCart\Models\AbandonedCheckout; |
| 7 | use SureCart\Controllers\Admin\Tables\HasModeFilter; |
| 8 | |
| 9 | /** |
| 10 | * Create a new table class that will extend the WP_List_Table |
| 11 | */ |
| 12 | class AbandonedCheckoutListTable extends ListTable { |
| 13 | use HasModeFilter; |
| 14 | |
| 15 | /** |
| 16 | * Prepare the items for the table to process |
| 17 | * |
| 18 | * @return Void |
| 19 | */ |
| 20 | public function prepare_items() { |
| 21 | $columns = $this->get_columns(); |
| 22 | $hidden = $this->get_hidden_columns(); |
| 23 | $sortable = $this->get_sortable_columns(); |
| 24 | |
| 25 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 26 | |
| 27 | $query = $this->table_data(); |
| 28 | if ( is_wp_error( $query ) ) { |
| 29 | $this->items = []; |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | $this->set_pagination_args( |
| 34 | [ |
| 35 | 'total_items' => $query->pagination->count, |
| 36 | 'per_page' => $this->get_items_per_page( 'abandoned_checkout' ), |
| 37 | ] |
| 38 | ); |
| 39 | |
| 40 | $this->items = $query->data; |
| 41 | } |
| 42 | |
| 43 | protected function get_views() { |
| 44 | $stati = [ |
| 45 | 'all' => __( 'All', 'surecart' ), |
| 46 | 'scheduled' => __( 'Scheduled', 'surecart' ), |
| 47 | 'sent' => __( 'Sent', 'surecart' ), |
| 48 | 'not_scheduled' => __( 'Not Scheduled', 'surecart' ), |
| 49 | ]; |
| 50 | |
| 51 | foreach ( $stati as $status => $label ) { |
| 52 | $link = \SureCart::getUrl()->index( 'abandoned-checkout' ); |
| 53 | |
| 54 | $current_link_attributes = ''; |
| 55 | |
| 56 | if ( ! empty( $_GET['status'] ) ) { |
| 57 | if ( sanitize_text_field( wp_unslash( $_GET['status'] ) ) === $status ) { |
| 58 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 59 | } |
| 60 | } elseif ( 'all' === $status ) { |
| 61 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 62 | } |
| 63 | |
| 64 | $link = add_query_arg( 'status', $status, $link ); |
| 65 | |
| 66 | $link = esc_url( $link ); |
| 67 | |
| 68 | $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . $label . '</a>'; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Filters the comment status links. |
| 73 | * |
| 74 | * @since 2.5.0 |
| 75 | * @since 5.1.0 The 'Mine' link was added. |
| 76 | * |
| 77 | * @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine', |
| 78 | * 'Pending', 'Approved', 'Spam', and 'Trash'. |
| 79 | */ |
| 80 | return apply_filters( 'abandoned_order_status_links', $status_links ); |
| 81 | } |
| 82 | |
| 83 | public function search() { |
| 84 | ?> |
| 85 | <form class="search-form" |
| 86 | method="get"> |
| 87 | <?php $this->search_box( __( 'Search Abanonded Orders', 'surecart' ), 'abandoned_order' ); ?> |
| 88 | <input type="hidden" |
| 89 | name="id" |
| 90 | value="1" /> |
| 91 | </form> |
| 92 | <?php |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Override the parent columns method. Defines the columns to use in your listing table |
| 97 | * |
| 98 | * @return Array |
| 99 | */ |
| 100 | public function get_columns() { |
| 101 | return [ |
| 102 | 'placed_by' => __( 'Placed By', 'surecart' ), |
| 103 | 'date' => __( 'Date', 'surecart' ), |
| 104 | 'notification_status' => __( 'Email Status', 'surecart' ), |
| 105 | 'recovery_status' => __( 'Recovery Status', 'surecart' ), |
| 106 | 'total' => __( 'Total', 'surecart' ), |
| 107 | 'mode' => '', |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the table data |
| 113 | * |
| 114 | * @return Array |
| 115 | */ |
| 116 | protected function table_data() { |
| 117 | $mode = sanitize_text_field( wp_unslash( $_GET['mode'] ?? '' ) ); |
| 118 | $status = $this->getStatus(); |
| 119 | $where = array(); |
| 120 | |
| 121 | if ( ! empty( $mode ) ) { |
| 122 | $where['live_mode'] = 'live' === $mode; |
| 123 | } |
| 124 | |
| 125 | if ( $status ) { |
| 126 | $where['notification_status'] = [ $status ]; |
| 127 | } |
| 128 | |
| 129 | return AbandonedCheckout::where( $where ) |
| 130 | ->with( [ 'recovered_checkout', 'checkout', 'customer' ] ) |
| 131 | ->paginate( |
| 132 | [ |
| 133 | 'per_page' => $this->get_items_per_page( 'abandoned-checkouts' ), |
| 134 | 'page' => $this->get_pagenum(), |
| 135 | 'expand' => [ 'checkout' ], |
| 136 | ] |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the archive query status. |
| 142 | * |
| 143 | * @return boolean|null |
| 144 | */ |
| 145 | public function getStatus() { |
| 146 | $status = sanitize_text_field( wp_unslash( $_GET['status'] ?? 'all' ) ); |
| 147 | if ( 'all' === $status ) { |
| 148 | return null; |
| 149 | } |
| 150 | return $status ? [ esc_html( $status ) ] : []; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Handle the total column |
| 155 | * |
| 156 | * @param \SureCart\Models\AbandonedCheckout $checkout Checkout Session Model. |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | public function column_total( $abandoned ) { |
| 161 | return '<sc-format-number type="currency" currency="' . strtoupper( esc_html( $abandoned->checkout->currency ?? 'usd' ) ) . '" value="' . (float) $abandoned->checkout->total_amount . '"></sc-format-number>'; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Handle the total column |
| 166 | * |
| 167 | * @param \SureCart\Models\AbandonedCheckout $abandoned Abandoned checkout model. |
| 168 | * |
| 169 | * @return string |
| 170 | */ |
| 171 | public function column_date( $abandoned ) { |
| 172 | return isset( $abandoned->created_at ) ? '<sc-format-date date="' . (int) $abandoned->created_at . '" type="timestamp" month="short" day="numeric" year="numeric" hour="numeric" minute="numeric"></sc-format-date>' : '--'; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Handle the notification status |
| 177 | * |
| 178 | * @param \SureCart\Models\AbandonedCheckout $abandoned Abandoned checkout session. |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | public function column_notification_status( $abandoned ) { |
| 183 | if ( $abandoned->recovered_checkout ) { |
| 184 | return '--'; |
| 185 | } |
| 186 | |
| 187 | switch ( $abandoned->notification_status ?? '' ) { |
| 188 | case 'scheduled': |
| 189 | return '<sc-tag type="info">' . __( 'Scheduled', 'surecart' ) . '</sc-tag>'; |
| 190 | case 'not_scheduled': |
| 191 | return '<sc-tag type="warning">' . __( 'Not Scheduled', 'surecart' ) . '</sc-tag>'; |
| 192 | case 'sent': |
| 193 | return '<sc-tag type="success">' . __( 'Email Sent', 'surecart' ) . '</sc-tag>'; |
| 194 | } |
| 195 | return '<sc-tag>' . esc_html( $abandoned->notification_status ?? 'Unknown' ) . '</sc-tag>'; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Handle the recovery status |
| 200 | * |
| 201 | * @param \SureCart\Models\AbandonedCheckout $abandoned Abandoned checkout session. |
| 202 | * |
| 203 | * @return string |
| 204 | */ |
| 205 | public function column_recovery_status( $abandoned ) { |
| 206 | switch ( $abandoned->recovery_status ) { |
| 207 | case 'abandoned': |
| 208 | return '<sc-tag type="warning">' . __( 'Abandoned', 'surecart' ) . '</sc-tag>'; |
| 209 | case 'assisted_recovered': |
| 210 | return '<sc-tag type="success">' . __( 'Recovered', 'surecart' ) . '</sc-tag>'; |
| 211 | case 'unassisted_recovered': |
| 212 | return '<sc-tag type="info">' . __( 'Recovered Before Email Was Sent', 'surecart' ) . '</sc-tag>'; |
| 213 | } |
| 214 | return '<sc-tag type="success">' . $abandoned->recovery_status . '</sc-tag>'; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Handle the mode column |
| 219 | * |
| 220 | * @param \SureCart\Models\AbandonedCheckout $abandoned Abandoned checkout model. |
| 221 | * |
| 222 | * @return string |
| 223 | */ |
| 224 | public function column_mode( $abandoned ) { |
| 225 | return empty( $abandoned->checkout->live_mode ) ? '<sc-tag type="warning">' . __( 'Test', 'surecart' ) . '</sc-tag>' : ''; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Email of customer |
| 230 | * |
| 231 | * @param \SureCart\Models\AbandonedCheckout $abandoned Abandoned checkout model. |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | public function column_placed_by( $abandoned ) { |
| 236 | ob_start(); |
| 237 | ?> |
| 238 | <a class="row-title" aria-label="<?php esc_attr_e( 'Edit Order', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'abandoned-checkout', $abandoned->id ) ); ?>"> |
| 239 | <?php |
| 240 | // translators: Customer name. |
| 241 | echo sprintf( esc_html__( 'By %s', 'surecart' ), esc_html( $abandoned->customer->name ?? $abandoned->customer->email ) ); |
| 242 | ?> |
| 243 | </a> |
| 244 | <br /> |
| 245 | <a aria-label="<?php esc_attr_e( 'View Checkout', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'abandoned-checkout', $abandoned->id ) ); ?>"> |
| 246 | <?php esc_attr_e( 'View Checkout', 'surecart' ); ?> |
| 247 | </a> |
| 248 | <?php |
| 249 | |
| 250 | return ob_get_clean(); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Displays extra table navigation. |
| 255 | * |
| 256 | * @param string $which Top or bottom placement. |
| 257 | */ |
| 258 | protected function extra_tablenav( $which ) { |
| 259 | ?> |
| 260 | <input type="hidden" name="page" value="sc-abandoned-checkouts" /> |
| 261 | |
| 262 | <div class="alignleft actions"> |
| 263 | <?php |
| 264 | if ( 'top' === $which ) { |
| 265 | ob_start(); |
| 266 | $this->mode_dropdown(); |
| 267 | |
| 268 | /** |
| 269 | * Fires before the Filter button on the Posts and Pages list tables. |
| 270 | * |
| 271 | * The Filter button allows sorting by date and/or category on the |
| 272 | * Posts list table, and sorting by date on the Pages list table. |
| 273 | * |
| 274 | * @since 2.1.0 |
| 275 | * @since 4.4.0 The `$post_type` parameter was added. |
| 276 | * @since 4.6.0 The `$which` parameter was added. |
| 277 | * |
| 278 | * @param string $post_type The post type slug. |
| 279 | * @param string $which The location of the extra table nav markup: |
| 280 | * 'top' or 'bottom' for WP_Posts_List_Table, |
| 281 | * 'bar' for WP_Media_List_Table. |
| 282 | */ |
| 283 | do_action( 'restrict_manage_abandoned_checkouts', $this->screen->post_type, $which ); |
| 284 | |
| 285 | $output = ob_get_clean(); |
| 286 | |
| 287 | if ( ! empty( $output ) ) { |
| 288 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 289 | submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'filter-by-mode-submit' ) ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | ?> |
| 294 | </div> |
| 295 | |
| 296 | <?php |
| 297 | /** |
| 298 | * Fires immediately following the closing "actions" div in the tablenav for the posts |
| 299 | * list table. |
| 300 | * |
| 301 | * @since 4.4.0 |
| 302 | * |
| 303 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 304 | */ |
| 305 | do_action( 'manage_abandoned_checkouts_extra_tablenav', $which ); |
| 306 | } |
| 307 | } |
| 308 |