ListTable.php
293 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Tables; |
| 4 | |
| 5 | use SureCart\Models\Integration; |
| 6 | use SureCart\Support\TimeDate; |
| 7 | |
| 8 | // WP_List_Table is not loaded automatically so we need to load it in our application. |
| 9 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 10 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Base list table class. |
| 15 | */ |
| 16 | abstract class ListTable extends \WP_List_Table { |
| 17 | public $checkbox = true; |
| 18 | |
| 19 | /** |
| 20 | * Show filters in extra tablenav top |
| 21 | * |
| 22 | * @param [type] $which |
| 23 | * @return void |
| 24 | */ |
| 25 | protected function extra_tablenav( $which ) { |
| 26 | if ( 'top' === $which ) { |
| 27 | return $this->views(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Define which columns are hidden |
| 33 | * |
| 34 | * @return Array |
| 35 | */ |
| 36 | public function get_hidden_columns() { |
| 37 | return array(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Sanitised `?page` query arg. |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | protected function getCurrentPage(): string { |
| 46 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 47 | return ! empty( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Override the parent columns method. Defines the columns to use in your listing table |
| 52 | * |
| 53 | * @return Array |
| 54 | */ |
| 55 | public function get_columns() { |
| 56 | $current_page = $this->getCurrentPage(); |
| 57 | |
| 58 | /** |
| 59 | * Filters the columns displayed in the Coupons list table. |
| 60 | * |
| 61 | * @param string[] $coupon_columns An associative array of column headings. |
| 62 | */ |
| 63 | return (array) apply_filters( "manage_{$current_page}_columns", array() ); |
| 64 | } |
| 65 | /** |
| 66 | * Handles the default column output. |
| 67 | * |
| 68 | * @param WP_Post $item The current WP_Post object. |
| 69 | * @param string $column_name The current column name. |
| 70 | */ |
| 71 | public function column_default( $item, $column_name ) { |
| 72 | $current_page = $this->getCurrentPage(); |
| 73 | /** |
| 74 | * Fires for each custom column of any SureCart List Table. |
| 75 | * |
| 76 | * The dynamic portion of the hook name, `$current_page`, refers to the page name. |
| 77 | * |
| 78 | * Possible hook names include: |
| 79 | * |
| 80 | * - `manage_orders_custom_column` |
| 81 | * - `manage_products_custom_column` |
| 82 | * |
| 83 | * @param string $column_name The name of the column to display. |
| 84 | * @param mixed $item The current item's data object. |
| 85 | */ |
| 86 | do_action( "manage_{$current_page}_custom_column", $column_name, $item ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the archive query status. |
| 91 | * |
| 92 | * @return boolean|null |
| 93 | */ |
| 94 | public function getArchiveStatus( $default = 'active' ) { |
| 95 | $status = sanitize_text_field( wp_unslash( $_GET['status'] ?? $default ) ); |
| 96 | $archived = false; |
| 97 | if ( 'archived' === $status ) { |
| 98 | $archived = true; |
| 99 | } |
| 100 | if ( 'all' === $status ) { |
| 101 | $archived = null; |
| 102 | } |
| 103 | |
| 104 | return $archived; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Handle the date |
| 109 | * |
| 110 | * @param \SureCart\Models\Model $model Model. |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public function column_date( $model ) { |
| 115 | $created = sprintf( |
| 116 | '<time datetime="%1$s" title="%2$s">%3$s</time>', |
| 117 | esc_attr( $model->created_at ), |
| 118 | esc_html( $model->created_at_date_time ), |
| 119 | esc_html( TimeDate::humanTimeDiff( $model->created_at ) ) |
| 120 | ); |
| 121 | $updated = sprintf( |
| 122 | '%1$s <time datetime="%2$s" title="%3$s">%4$s</time>', |
| 123 | __( 'Updated', 'surecart' ), |
| 124 | esc_attr( $model->updated_at_date_time ), |
| 125 | esc_html( $model->updated_at_date_time ), |
| 126 | esc_html( TimeDate::humanTimeDiff( $model->updated_at ) ) |
| 127 | ); |
| 128 | |
| 129 | return $created . '<br /><small style="opacity: 0.75">' . $updated . '</small>'; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Handle the created column |
| 134 | * |
| 135 | * @param \SureCart\Models\Model $model Model. |
| 136 | * |
| 137 | * @return string |
| 138 | */ |
| 139 | public function column_created( $model ) { |
| 140 | return esc_attr( $model->created_at_date_time ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * The mode for the model. |
| 145 | * |
| 146 | * @param \SureCart\Models\Model $model Model. |
| 147 | * |
| 148 | * @return string |
| 149 | */ |
| 150 | public function column_mode( $model ) { |
| 151 | return empty( $model->live_mode ) ? '<sc-tag type="warning">' . __( 'Test', 'surecart' ) . '</sc-tag>' : ''; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Show an integrations list based on a product id. |
| 156 | * |
| 157 | * @param string $args The args. |
| 158 | * |
| 159 | * @return string |
| 160 | */ |
| 161 | public function productIntegrationsList( $args ) { |
| 162 | // parse the args. |
| 163 | $args = wp_parse_args( |
| 164 | $args, |
| 165 | [ |
| 166 | 'product_id' => '', |
| 167 | 'price_id' => '', |
| 168 | 'variant_id' => '', |
| 169 | ] |
| 170 | ); |
| 171 | |
| 172 | // get the integration first by product id. |
| 173 | $integrations = Integration::where( 'model_id', $args['product_id'] ); |
| 174 | |
| 175 | $output = ''; |
| 176 | $integrations = $integrations->group_by( 'integration_id' )->get(); |
| 177 | if ( empty( $integrations ) || is_wp_error( $integrations ) ) { |
| 178 | return $output; |
| 179 | } |
| 180 | |
| 181 | foreach ( $integrations as $integration ) { |
| 182 | $provider = (object) apply_filters( "surecart/integrations/providers/find/{$integration->provider}", [] ); |
| 183 | $item = (object) apply_filters( "surecart/integrations/providers/{$integration->provider}/item", $integration->integration_id ); |
| 184 | if ( $integration->price_id && $args['price_id'] && $integration->price_id !== $args['price_id'] ) { |
| 185 | continue; |
| 186 | } |
| 187 | if ( $integration->variant_id && $args['variant_id'] && $integration->variant_id !== $args['variant_id'] ) { |
| 188 | continue; |
| 189 | } |
| 190 | if ( ! empty( $item->label ) ) { |
| 191 | // Tooltip surfaces both the provider ("Zapier") and the item ("New order → Slack") |
| 192 | // so keyboard + screen-reader users aren't limited to the native `title` hover. |
| 193 | $tooltip_text = trim( |
| 194 | sprintf( |
| 195 | '%s%s%s', |
| 196 | ! empty( $provider->label ) ? $provider->label : '', |
| 197 | ! empty( $provider->label ) && ! empty( $item->label ) ? ' — ' : '', |
| 198 | $item->label ?? '' |
| 199 | ) |
| 200 | ); |
| 201 | ob_start(); |
| 202 | ?> |
| 203 | <sc-tooltip |
| 204 | text="<?php echo esc_attr( $tooltip_text ); ?>" |
| 205 | type="text" |
| 206 | style="display: inline-block; cursor: help; margin: 2px;" |
| 207 | > |
| 208 | <?php if ( ! empty( $provider->logo ) ) : ?> |
| 209 | <img |
| 210 | src="<?php echo esc_url( $provider->logo ); ?>" |
| 211 | alt="<?php echo esc_attr( $tooltip_text ); ?>" |
| 212 | style="width: 20px; height: 20px; display: inline-block; vertical-align: middle;" |
| 213 | /> |
| 214 | <?php else : ?> |
| 215 | <span aria-label="<?php echo esc_attr( $tooltip_text ); ?>"> |
| 216 | <?php echo wp_kses_post( $item->label ); ?> |
| 217 | </span> |
| 218 | <?php endif; ?> |
| 219 | </sc-tooltip> |
| 220 | <?php |
| 221 | $output .= ob_get_clean(); |
| 222 | } |
| 223 | } |
| 224 | return $output; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Get the search query from the url args. |
| 229 | * |
| 230 | * @return string |
| 231 | */ |
| 232 | public function get_search_query() { |
| 233 | $search = urldecode( $_GET['s'] ?? '' ); |
| 234 | return str_replace( [ "\r", "\n" ], '', $search ); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the current orderby value. |
| 239 | * |
| 240 | * @return string |
| 241 | */ |
| 242 | protected function get_orderby(): string { |
| 243 | return isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get the current order value. |
| 248 | * |
| 249 | * @return string |
| 250 | */ |
| 251 | protected function get_order(): string { |
| 252 | $order = isset( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_GET['order'] ) ) : 'desc'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 253 | return in_array( strtolower( $order ), [ 'asc', 'desc' ], true ) ? strtolower( $order ) : 'desc'; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Display a search form |
| 258 | * |
| 259 | * @param string $text The 'submit' button label. |
| 260 | * @param string $input_id ID attribute value for the search input field. |
| 261 | * |
| 262 | * @return void |
| 263 | */ |
| 264 | public function search_form( $text, $input_id ) { |
| 265 | ?> |
| 266 | <form id="posts-filter" method="get"> |
| 267 | <?php if ( isset( $_GET['page'] ) ) : ?> |
| 268 | <input type="hidden" name="page" value="<?php echo esc_attr( $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>"> |
| 269 | <?php endif; ?> |
| 270 | <?php if ( isset( $_GET['status'] ) ) : ?> |
| 271 | <input type="hidden" name="status" value="<?php echo esc_attr( $_GET['status'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>"> |
| 272 | <?php endif; ?> |
| 273 | <?php if ( isset( $_GET['live_mode'] ) ) : ?> |
| 274 | <input type="hidden" name="live_mode" value="<?php echo esc_attr( $_GET['live_mode'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>"> |
| 275 | <?php endif; ?> |
| 276 | <?php $this->search_box( $text, $input_id ); ?> |
| 277 | </form> |
| 278 | <?php |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Get the sort map. |
| 283 | * |
| 284 | * @return array |
| 285 | */ |
| 286 | public function get_sort_map(): array { |
| 287 | return array( |
| 288 | 'created_at' => 'created_at', |
| 289 | 'updated_at' => 'updated_at', |
| 290 | ); |
| 291 | } |
| 292 | } |
| 293 |