AutoFeesController.php
5 months ago
AutoFeesListTable.php
1 week ago
AutoFeesScriptsController.php
5 months ago
AutoFeesListTable.php
369 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\AutoFees; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 6 | use SureCart\Models\AutoFee; |
| 7 | |
| 8 | /** |
| 9 | * Create a new table class that will extend the WP_List_Table |
| 10 | */ |
| 11 | class AutoFeesListTable 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 | |
| 21 | $this->_column_headers = array( $columns, $hidden ); |
| 22 | |
| 23 | $query = $this->table_data(); |
| 24 | |
| 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( 'auto-fees' ), |
| 34 | ] |
| 35 | ); |
| 36 | |
| 37 | $this->items = $query->data; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Search form for the table. |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function search() { |
| 46 | ?> |
| 47 | <form class="search-form" |
| 48 | method="get"> |
| 49 | <?php $this->search_box( __( 'Search Dynamic Prices', 'surecart' ), 'order' ); ?> |
| 50 | <input type="hidden" |
| 51 | name="id" |
| 52 | value="1" /> |
| 53 | </form> |
| 54 | <?php |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the views for the list table. |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | protected function get_views() { |
| 63 | $stati = [ |
| 64 | 'all' => __( 'All', 'surecart' ), |
| 65 | 'active' => __( 'Active', 'surecart' ), |
| 66 | 'inactive' => __( 'Inactive', 'surecart' ), |
| 67 | ]; |
| 68 | |
| 69 | foreach ( $stati as $status => $label ) { |
| 70 | $link = esc_url_raw( \SureCart::getUrl()->index( 'auto-fees' ) ); |
| 71 | $current_link_attributes = ''; |
| 72 | |
| 73 | if ( ! empty( $_GET['status'] ) ) { |
| 74 | if ( $status === sanitize_text_field( wp_unslash( $_GET['status'] ) ) ) { |
| 75 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 76 | } |
| 77 | } elseif ( 'all' === $status ) { |
| 78 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 79 | } |
| 80 | |
| 81 | $link = add_query_arg( 'status', $status, $link ); |
| 82 | |
| 83 | $link = esc_url( $link ); |
| 84 | |
| 85 | $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . $label . '</a>'; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Filters the comment status links. |
| 90 | * |
| 91 | * @since 2.5.0 |
| 92 | * @since 5.1.0 The 'Mine' link was added. |
| 93 | * |
| 94 | * @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine', |
| 95 | * 'Pending', 'Approved', 'Spam', and 'Trash'. |
| 96 | */ |
| 97 | return apply_filters( 'surecart/auto_fees/index/links', $status_links ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Override the parent columns method. Defines the columns to use in your listing table |
| 102 | * |
| 103 | * @return array |
| 104 | */ |
| 105 | public function get_columns() { |
| 106 | return array_merge( |
| 107 | [ |
| 108 | 'internal_name' => __( 'Name', 'surecart' ), |
| 109 | 'name' => __( 'Display Name', 'surecart' ), |
| 110 | 'status' => __( 'Status', 'surecart' ), |
| 111 | 'type' => __( 'Type', 'surecart' ), |
| 112 | 'date' => __( 'Date', 'surecart' ), |
| 113 | ], |
| 114 | parent::get_columns() |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Define which columns are hidden |
| 120 | * |
| 121 | * @return array |
| 122 | */ |
| 123 | public function get_hidden_columns() { |
| 124 | return array(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the table data |
| 129 | * |
| 130 | * @return \SureCart\Models\AutoFee[]|\WP_Error |
| 131 | */ |
| 132 | protected function table_data() { |
| 133 | $conditions = [ |
| 134 | 'query' => $this->get_search_query(), |
| 135 | ]; |
| 136 | |
| 137 | if ( 'active' === $this->getStatus() ) { |
| 138 | $conditions['enabled'] = true; |
| 139 | } |
| 140 | |
| 141 | if ( 'inactive' === $this->getStatus() ) { |
| 142 | $conditions['enabled'] = false; |
| 143 | } |
| 144 | |
| 145 | return AutoFee::where( $conditions ) |
| 146 | ->paginate( |
| 147 | [ |
| 148 | 'per_page' => $this->get_items_per_page( 'auto-fees' ), |
| 149 | 'page' => $this->get_pagenum(), |
| 150 | ] |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the archive query status. |
| 156 | * |
| 157 | * @return bool|null |
| 158 | */ |
| 159 | public function getStatus() { |
| 160 | return sanitize_text_field( wp_unslash( $_GET['status'] ?? false ) ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Handle the status |
| 165 | * |
| 166 | * @param \SureCart\Models\AutoFee $auto_fees Auto Fee model. |
| 167 | * |
| 168 | * @return void |
| 169 | */ |
| 170 | public function column_status( $auto_fees ) { |
| 171 | $toggle_url = add_query_arg( |
| 172 | [ |
| 173 | 'action' => 'toggle_active', |
| 174 | 'nonce' => wp_create_nonce( 'archive_dynamic_price' ), |
| 175 | 'id' => $auto_fees->id, |
| 176 | 'status' => 'all', |
| 177 | ], |
| 178 | \SureCart::getUrl()->index( 'auto-fees' ) |
| 179 | ); |
| 180 | ?> |
| 181 | <sc-switch checked="<?php echo esc_attr( $auto_fees->enabled ) ? 'true' : 'false'; ?>" |
| 182 | onClick="window.location.assign('<?php echo esc_url( $toggle_url ); ?>'); document.querySelector('#loading-<?php echo esc_attr( $auto_fees->id ); ?>').style.display = '';"></sc-switch> |
| 183 | <sc-block-ui id="loading-<?php echo esc_attr( $auto_fees->id ); ?>" spinner style="display: none;"></sc-block-ui> |
| 184 | <?php |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Name |
| 189 | * |
| 190 | * @param \SureCart\Models\AutoFees $auto_fees AutoFees model. |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | public function column_name( $auto_fees ) { |
| 195 | return esc_html( $auto_fees->name ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Internal name. |
| 200 | * |
| 201 | * @param \SureCart\Models\AutoFees $auto_fees AutoFees model. |
| 202 | * |
| 203 | * @return string |
| 204 | */ |
| 205 | public function column_internal_name( $auto_fees ) { |
| 206 | ob_start(); |
| 207 | ?> |
| 208 | <a href="<?php echo esc_url_raw( \SureCart::getUrl()->edit( 'auto-fee', $auto_fees->id ) ); ?>"> |
| 209 | <?php echo esc_html( $auto_fees->metadata->internal_name ?? '' ); ?> |
| 210 | </a> |
| 211 | <?php echo wp_kses_post( $this->getRowActions( $auto_fees ) ); ?> |
| 212 | |
| 213 | <?php |
| 214 | return ob_get_clean(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Handle the target. |
| 219 | * |
| 220 | * @param \SureCart\Models\AutoFees $auto_fees AutoFees model. |
| 221 | * |
| 222 | * @return string |
| 223 | */ |
| 224 | public function column_type( $auto_fees ) { |
| 225 | $translations = [ |
| 226 | 'checkout' => [ |
| 227 | 'label' => __( 'Checkout', 'surecart' ), |
| 228 | 'icon' => 'shopping-cart', |
| 229 | ], |
| 230 | 'line_item' => [ |
| 231 | 'label' => __( 'Line Item', 'surecart' ), |
| 232 | 'icon' => 'layout-list', |
| 233 | ], |
| 234 | 'shipping' => [ |
| 235 | 'label' => __( 'Shipping', 'surecart' ), |
| 236 | 'icon' => 'truck', |
| 237 | ], |
| 238 | ]; |
| 239 | |
| 240 | // Determine fee/discount type. |
| 241 | $is_discount = (bool) $auto_fees->discount; |
| 242 | $type_label = $is_discount ? __( 'Discount', 'surecart' ) : __( 'Fee', 'surecart' ); |
| 243 | $type_tag_type = $is_discount ? 'success' : 'danger'; |
| 244 | $type_icon_name = $is_discount ? 'arrow-down-right' : 'arrow-up-right'; |
| 245 | |
| 246 | // Get target information. |
| 247 | $target_data = $translations[ $auto_fees->fee_target ] ?? $translations['checkout']; |
| 248 | $target_label = $target_data['label']; |
| 249 | $target_icon = $target_data['icon']; |
| 250 | |
| 251 | // Build the fee/discount tag. |
| 252 | $type_tag = sprintf( |
| 253 | '<sc-tag type="%s" pill><div style="display: flex; align-items: center; gap: 0.5em;"><sc-icon name="%s"></sc-icon>%s</div></sc-tag>', |
| 254 | esc_attr( $type_tag_type ), |
| 255 | esc_attr( $type_icon_name ), |
| 256 | esc_html( $type_label ) |
| 257 | ); |
| 258 | |
| 259 | // Build the target tag. |
| 260 | $target_tag = sprintf( |
| 261 | '<sc-tag type="info" pill><div style="display: flex; align-items: center; gap: 0.5em;"><sc-icon name="%s"></sc-icon>%s</div></sc-tag>', |
| 262 | esc_attr( $target_icon ), |
| 263 | esc_html( $target_label ) |
| 264 | ); |
| 265 | |
| 266 | // Build the translated sentence with flex container for vertical alignment. |
| 267 | return sprintf( |
| 268 | /* translators: %1$s: Fee or Discount tag HTML, %2$s: Target tag HTML (e.g., Checkout, Line Item, Shipping) */ |
| 269 | '<sc-flex align-items="center" justify-content="flex-start" style="--sc-flex-column-gap: 0.35em; flex-wrap: wrap;">' . __( 'Apply a %1$s to %2$s', 'surecart' ) . '</sc-flex>', |
| 270 | $type_tag, |
| 271 | $target_tag |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get row actions. |
| 277 | * |
| 278 | * @param \SureCart\Models\AutoFee $auto_fees Auto Fee model. |
| 279 | * |
| 280 | * @return array |
| 281 | */ |
| 282 | public function getRowActions( $auto_fees ) { |
| 283 | return $this->row_actions( |
| 284 | array_filter( |
| 285 | [ |
| 286 | 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->edit( 'auto-fee', $auto_fees->id ) ) . '" aria-label="' . esc_attr( 'Edit Dynamic Pricing', 'surecart' ) . '">' . esc_html__( 'Edit', 'surecart' ) . '</a>', |
| 287 | 'delete' => '<a href="' . esc_url( $this->get_action_url( $auto_fees->id, 'delete' ) ) . '">' . esc_html__( 'Delete', 'surecart' ) . '</a>', |
| 288 | ] |
| 289 | ) |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Displays extra table navigation. |
| 295 | * |
| 296 | * @param string $which Top or bottom placement. |
| 297 | */ |
| 298 | protected function extra_tablenav( $which ) { |
| 299 | ?> |
| 300 | <input type="hidden" name="page" value="sc-auto-fees" /> |
| 301 | |
| 302 | <div class="alignleft actions"> |
| 303 | <?php |
| 304 | if ( 'top' === $which ) { |
| 305 | ob_start(); |
| 306 | $this->mode_dropdown(); |
| 307 | |
| 308 | /** |
| 309 | * Fires before the Filter button on the Auto fees list tables. |
| 310 | * |
| 311 | * The Filter button allows sorting by date and/or category on the |
| 312 | * auto fees list table, and sorting by date on the Pages list table. |
| 313 | * |
| 314 | * @since 2.1.0 |
| 315 | * @since 4.4.0 The `$post_type` parameter was added. |
| 316 | * @since 4.6.0 The `$which` parameter was added. |
| 317 | * |
| 318 | * @param string $post_type The post type slug. |
| 319 | * @param string $which The location of the extra table nav markup: |
| 320 | * 'top' or 'bottom' for WP_Posts_List_Table, |
| 321 | * 'bar' for WP_Media_List_Table. |
| 322 | */ |
| 323 | do_action( 'restrict_manage_auto_fees', $this->screen->post_type, $which ); |
| 324 | |
| 325 | $output = ob_get_clean(); |
| 326 | |
| 327 | if ( ! empty( $output ) ) { |
| 328 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 329 | submit_button( __( 'Filter', 'surecart' ), '', 'filter_action', false, array( 'id' => 'filter-by-mode-submit' ) ); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | ?> |
| 334 | </div> |
| 335 | |
| 336 | <?php |
| 337 | /** |
| 338 | * Fires immediately following the closing "actions" div in the tablenav for the auto fees |
| 339 | * list table. |
| 340 | * |
| 341 | * @since 4.4.0 |
| 342 | * |
| 343 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 344 | */ |
| 345 | do_action( 'manage_auto_fees_extra_tablenav', $which ); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Get action url. |
| 350 | * |
| 351 | * @param int $id The id. |
| 352 | * @param string $action The action. |
| 353 | * |
| 354 | * @return string |
| 355 | */ |
| 356 | public function get_action_url( $id, $action ) { |
| 357 | return esc_url( |
| 358 | add_query_arg( |
| 359 | [ |
| 360 | 'action' => $action, |
| 361 | 'nonce' => wp_create_nonce( $action . '_auto_fee' ), |
| 362 | 'id' => $id, |
| 363 | ], |
| 364 | esc_url_raw( \SureCart::getUrl()->index( 'auto-fees' ) ) |
| 365 | ) |
| 366 | ); |
| 367 | } |
| 368 | } |
| 369 |