ProductCollectionsController.php
2 years ago
ProductCollectionsListTable.php
2 years ago
ProductCollectionsScriptsController.php
2 years ago
ProductCollectionsListTable.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\ProductCollections; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 6 | use SureCart\Models\ProductCollection; |
| 7 | |
| 8 | /** |
| 9 | * Create a new table class that will extend the WP_List_Table |
| 10 | */ |
| 11 | class ProductCollectionsListTable extends ListTable { |
| 12 | /** |
| 13 | * Prepare the items for the table to process |
| 14 | * |
| 15 | * @return void |
| 16 | */ |
| 17 | public function prepare_items(): void { |
| 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( 'product_collections' ), |
| 34 | ] |
| 35 | ); |
| 36 | |
| 37 | $this->items = $query->data; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Override the parent columns method. Defines the columns to use in your listing table |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public function get_columns(): array { |
| 46 | return [ |
| 47 | 'name' => __( 'Name', 'surecart' ), |
| 48 | 'products_count' => __( 'Products', 'surecart' ), |
| 49 | 'description' => __( 'Description', 'surecart' ), |
| 50 | 'created' => __( 'Created', 'surecart' ), |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Define which columns are hidden. |
| 56 | * |
| 57 | * @return array |
| 58 | */ |
| 59 | public function get_hidden_columns(): array { |
| 60 | return []; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Define the sortable columns. |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | public function get_sortable_columns(): array { |
| 69 | return []; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the table data. |
| 74 | * |
| 75 | * @return object |
| 76 | */ |
| 77 | protected function table_data() { |
| 78 | return ProductCollection::where( |
| 79 | [ |
| 80 | 'archived' => $this->getArchiveStatus(), |
| 81 | 'query' => $this->get_search_query(), |
| 82 | ] |
| 83 | )->paginate( |
| 84 | [ |
| 85 | 'per_page' => $this->get_items_per_page( 'product_collection' ), |
| 86 | 'page' => $this->get_pagenum(), |
| 87 | ] |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Handle the description column. |
| 93 | * |
| 94 | * @param \SureCart\Models\ProductCollection $product_collection Product collection object. |
| 95 | * |
| 96 | * @return string |
| 97 | */ |
| 98 | public function column_description( $product_collection ) { |
| 99 | $description = wp_strip_all_tags( $product_collection->description ); |
| 100 | $trimmed_description = substr( $description, 0, 30 ); |
| 101 | if ( strlen( $description ) > 30 ) { |
| 102 | $trimmed_description .= '...'; |
| 103 | } |
| 104 | return $trimmed_description; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Handle the name column. |
| 109 | * |
| 110 | * @param \SureCart\Models\ProductCollection $collection Product collection object. |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public function column_name( $collection ) { |
| 115 | ob_start(); |
| 116 | ?> |
| 117 | <a class="row-title" aria-label="<?php echo esc_attr__( 'Edit Product Collection', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'product_collection', $collection->id ) ); ?>"> |
| 118 | <?php echo esc_html( $collection->name ); ?> |
| 119 | </a> |
| 120 | <?php |
| 121 | echo wp_kses_post( |
| 122 | $this->row_actions( |
| 123 | [ |
| 124 | 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->edit( 'product_collection', $collection->id ) ) . '" aria-label="' . esc_attr__( 'Edit Product Collection', 'surecart' ) . '">' . esc_attr__( 'Edit', 'surecart' ) . '</a>', |
| 125 | 'view' => '<a href="' . esc_url( $collection->permalink ) . '" aria-label="' . esc_attr__( 'View', 'surecart' ) . '">' . esc_html__( 'View', 'surecart' ) . '</a>', |
| 126 | ], |
| 127 | ) |
| 128 | ); |
| 129 | return ob_get_clean(); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Handle the products_count column. |
| 134 | * |
| 135 | * @param \SureCart\Models\ProductCollection $collection Product collection object. |
| 136 | * |
| 137 | * @return int |
| 138 | */ |
| 139 | public function column_products_count( $collection ) { |
| 140 | return '<a href="' . esc_url( add_query_arg( [ 'sc_collection' => $collection->id ], \SureCart::getUrl()->index( 'product' ) ) ) . '">' . $collection->products_count ?? 0 . '</a>'; |
| 141 | } |
| 142 | } |
| 143 |