CustomersController.php
8 months ago
CustomersListTable.php
2 months ago
CustomersScriptsController.php
3 months ago
CustomersListTable.php
334 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Customers; |
| 4 | |
| 5 | use SureCart\Models\Customer; |
| 6 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 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 CustomersListTable extends ListTable { |
| 13 | use HasModeFilter; |
| 14 | |
| 15 | /** |
| 16 | * The checkbox. |
| 17 | * |
| 18 | * @var bool |
| 19 | */ |
| 20 | public $checkbox = true; |
| 21 | |
| 22 | /** |
| 23 | * The error message. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | public $error = ''; |
| 28 | |
| 29 | /** |
| 30 | * The BulkActionService instance. |
| 31 | * |
| 32 | * @var \SureCart\Background\BulkActionService |
| 33 | */ |
| 34 | public $bulk_actions = null; |
| 35 | |
| 36 | /** |
| 37 | * Constructor. |
| 38 | * |
| 39 | * @param \SureCart\Background\BulkActionService $bulk_actions The BulkActionService instance. |
| 40 | */ |
| 41 | public function __construct( \SureCart\Background\BulkActionService $bulk_actions ) { |
| 42 | parent::__construct(); |
| 43 | |
| 44 | $this->bulk_actions = $bulk_actions; |
| 45 | |
| 46 | add_action( 'admin_notices', [ $this, 'show_bulk_action_admin_notice' ] ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Show bulk action admin notice. |
| 51 | */ |
| 52 | public function show_bulk_action_admin_notice() { |
| 53 | $this->bulk_actions->showBulkActionAdminNotice( 'delete_customers' ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Prepare the items for the table to process |
| 58 | * |
| 59 | * @return Void |
| 60 | */ |
| 61 | public function prepare_items() { |
| 62 | $columns = $this->get_columns(); |
| 63 | $hidden = $this->get_hidden_columns(); |
| 64 | $sortable = $this->get_sortable_columns(); |
| 65 | |
| 66 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 67 | |
| 68 | $query = $this->table_data(); |
| 69 | if ( is_wp_error( $query ) ) { |
| 70 | $this->error = $query->get_error_message(); |
| 71 | $this->items = []; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $this->set_pagination_args( |
| 76 | [ |
| 77 | 'total_items' => $query->pagination->count, |
| 78 | 'per_page' => $this->get_items_per_page( 'customerss' ), |
| 79 | ] |
| 80 | ); |
| 81 | |
| 82 | $this->items = $query->data; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Override the parent columns method. Defines the columns to use in your listing table |
| 87 | * |
| 88 | * @return array |
| 89 | */ |
| 90 | public function get_columns() { |
| 91 | return array_merge( |
| 92 | [ |
| 93 | 'cb' => '<input type="checkbox" />', |
| 94 | 'name' => __( 'Name', 'surecart' ), |
| 95 | 'email' => __( 'Email', 'surecart' ), |
| 96 | 'created' => __( 'Created', 'surecart' ), |
| 97 | 'mode' => '', |
| 98 | ], |
| 99 | parent::get_columns() |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Displays the checkbox column. |
| 105 | * |
| 106 | * @param Customer $customer The customer model. |
| 107 | */ |
| 108 | public function column_cb( $customer ) { |
| 109 | ?> |
| 110 | <label class="screen-reader-text" for="cb-select-<?php echo esc_attr( $customer['id'] ); ?>"><?php esc_html_e( 'Select customer', 'surecart' ); ?></label> |
| 111 | <input id="cb-select-<?php echo esc_attr( $customer['id'] ); ?>" type="checkbox" name="bulk_action_customer_ids[]" value="<?php echo esc_attr( $customer['id'] ); ?>" /> |
| 112 | <?php |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Define which columns are hidden |
| 117 | * |
| 118 | * @return array |
| 119 | */ |
| 120 | public function get_hidden_columns() { |
| 121 | return array(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Define the sortable columns |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | public function get_sortable_columns() { |
| 130 | return array( 'title' => array( 'title', false ) ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the table data |
| 135 | * |
| 136 | * @return object|\WP_Error |
| 137 | */ |
| 138 | private function table_data() { |
| 139 | $mode = sanitize_text_field( wp_unslash( $_GET['mode'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 140 | $conditions = array( |
| 141 | 'query' => $this->get_search_query(), |
| 142 | ); |
| 143 | |
| 144 | if ( ! empty( $mode ) ) { |
| 145 | $conditions['live_mode'] = 'live' === $mode; |
| 146 | } |
| 147 | |
| 148 | return Customer::where( $conditions ) |
| 149 | ->paginate( |
| 150 | [ |
| 151 | 'per_page' => $this->get_items_per_page( 'customers' ), |
| 152 | 'page' => $this->get_pagenum(), |
| 153 | ] |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Nothing found. |
| 159 | * |
| 160 | * @return void |
| 161 | */ |
| 162 | public function no_items() { |
| 163 | if ( $this->error ) { |
| 164 | echo esc_html( $this->error ); |
| 165 | return; |
| 166 | } |
| 167 | echo esc_html_e( 'No customers found.', 'surecart' ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * The customer email. |
| 172 | * |
| 173 | * @param \SureCart\Models\Customer $customer The customer model. |
| 174 | * |
| 175 | * @return string |
| 176 | */ |
| 177 | public function column_email( $customer ) { |
| 178 | return sanitize_email( $customer->email ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Name column |
| 183 | * |
| 184 | * @param \SureCart\Models\Customer $customer Customer model. |
| 185 | * |
| 186 | * @return string |
| 187 | */ |
| 188 | public function column_name( $customer ) { |
| 189 | $pending_record_ids = $this->bulk_actions->getRecordIds( 'delete_customers', 'pending' ); |
| 190 | $processing_record_ids = $this->bulk_actions->getRecordIds( 'delete_customers', 'processing' ); |
| 191 | $succeeded_record_ids = $this->bulk_actions->getRecordIds( 'delete_customers', 'succeeded' ); |
| 192 | $bulk_status = ''; |
| 193 | if ( ! empty( $pending_record_ids ) && in_array( $customer->id, $pending_record_ids, true ) ) { |
| 194 | $bulk_status = 'pending'; |
| 195 | } elseif ( ! empty( $processing_record_ids ) && in_array( $customer->id, $processing_record_ids, true ) ) { |
| 196 | $bulk_status = 'processing'; |
| 197 | } elseif ( ! empty( $succeeded_record_ids ) && in_array( $customer->id, $succeeded_record_ids, true ) ) { |
| 198 | $bulk_status = 'succeeded'; |
| 199 | } |
| 200 | |
| 201 | ob_start(); |
| 202 | ?> |
| 203 | <a class="row-title" aria-label="<?php esc_attr_e( 'Edit Customer', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'customers', $customer->id ) ); ?>"> |
| 204 | <?php echo esc_html( $customer->name ?? $customer->email ); ?> |
| 205 | </a> |
| 206 | |
| 207 | <?php |
| 208 | echo wp_kses_post( $this->getRowActions( $customer, $bulk_status ) ); |
| 209 | ?> |
| 210 | |
| 211 | <?php |
| 212 | return ob_get_clean(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Get row actions. |
| 217 | * |
| 218 | * @param \SureCart\Models\Customer $customer Customer model. |
| 219 | * @param string $bulk_status Bulk status. |
| 220 | * |
| 221 | * @return string |
| 222 | */ |
| 223 | public function getRowActions( $customer, $bulk_status ) { |
| 224 | if ( 'succeeded' === $bulk_status ) { |
| 225 | return '<div>' . esc_html__( 'Successfully deleted.', 'surecart' ) . '</div>'; |
| 226 | } |
| 227 | |
| 228 | if ( 'pending' === $bulk_status || 'processing' === $bulk_status ) { |
| 229 | return '<div>' . esc_html__( 'Queued for deletion.', 'surecart' ) . '</div>'; |
| 230 | } |
| 231 | |
| 232 | return $this->row_actions( |
| 233 | [ |
| 234 | 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->edit( 'customers', $customer->id ) ) . '" aria-label="' . esc_attr__( 'Edit Customer', 'surecart' ) . '">' . __( 'Edit', 'surecart' ) . '</a>', |
| 235 | ], |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Define what data to show on each column of the table |
| 241 | * |
| 242 | * @param Customer $customer Customer model. |
| 243 | * @param string $column_name - Current column name. |
| 244 | * |
| 245 | * @return mixed |
| 246 | */ |
| 247 | public function column_default( $customer, $column_name ) { |
| 248 | // Call the parent method to handle custom columns. |
| 249 | parent::column_default( $customer, $column_name ); |
| 250 | |
| 251 | switch ( $column_name ) { |
| 252 | case 'description': |
| 253 | return $customer->$column_name ?? ''; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Displays extra table navigation. |
| 259 | * |
| 260 | * @param string $which Top or bottom placement. |
| 261 | */ |
| 262 | protected function extra_tablenav( $which ) { |
| 263 | ?> |
| 264 | <input type="hidden" name="page" value="sc-customers" /> |
| 265 | |
| 266 | <div class="alignleft actions"> |
| 267 | <?php |
| 268 | if ( 'top' === $which ) { |
| 269 | ob_start(); |
| 270 | $this->mode_dropdown(); |
| 271 | |
| 272 | /** |
| 273 | * Fires before the Filter button on the Posts and Pages list tables. |
| 274 | * |
| 275 | * The Filter button allows sorting by date and/or category on the |
| 276 | * Posts list table, and sorting by date on the Pages list table. |
| 277 | * |
| 278 | * @since 2.1.0 |
| 279 | * @since 4.4.0 The `$post_type` parameter was added. |
| 280 | * @since 4.6.0 The `$which` parameter was added. |
| 281 | * |
| 282 | * @param string $post_type The post type slug. |
| 283 | * @param string $which The location of the extra table nav markup: |
| 284 | * 'top' or 'bottom' for WP_Posts_List_Table, |
| 285 | * 'bar' for WP_Media_List_Table. |
| 286 | */ |
| 287 | do_action( 'restrict_manage_customers', $this->screen->post_type, $which ); |
| 288 | |
| 289 | $output = ob_get_clean(); |
| 290 | |
| 291 | if ( ! empty( $output ) ) { |
| 292 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 293 | submit_button( __( 'Filter', 'surecart' ), '', 'filter_action', false, array( 'id' => 'filter-by-mode-submit' ) ); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | ?> |
| 298 | </div> |
| 299 | |
| 300 | <?php |
| 301 | /** |
| 302 | * Fires immediately following the closing "actions" div in the tablenav |
| 303 | * for the affiliate referrals list table. |
| 304 | * |
| 305 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 306 | */ |
| 307 | do_action( 'manage_customers_extra_tablenav', $which ); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get bulk actions. |
| 312 | * |
| 313 | * @return array |
| 314 | */ |
| 315 | protected function get_bulk_actions() { |
| 316 | $actions = array(); |
| 317 | $actions['delete'] = __( 'Delete permanently', 'surecart' ); |
| 318 | return $actions; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Gets the current action selected from the bulk actions dropdown. |
| 323 | * |
| 324 | * @return string|false The action name. False if no action was selected. |
| 325 | */ |
| 326 | public function current_action() { |
| 327 | if ( ! empty( $_REQUEST['delete_all'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 328 | return 'delete_all'; |
| 329 | } |
| 330 | |
| 331 | return parent::current_action(); |
| 332 | } |
| 333 | } |
| 334 |