surecart
/
app
/
src
/
Controllers
/
Admin
/
CancellationInsights
/
CancellationInsightsListTable.php
CancellationInsightsListTable.php
285 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\CancellationInsights; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 6 | use SureCart\Models\CancellationAct; |
| 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 CancellationInsightsListTable 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( 'orders' ), |
| 37 | ] |
| 38 | ); |
| 39 | |
| 40 | $this->items = $query->data; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Override the parent columns method. Defines the columns to use in your listing table |
| 46 | * |
| 47 | * @return Array |
| 48 | */ |
| 49 | public function get_columns() { |
| 50 | return array_merge( |
| 51 | [ |
| 52 | 'customer' => __( 'Customer', 'surecart' ), |
| 53 | 'plan' => __( 'Plan', 'surecart' ), |
| 54 | 'cancellation_reason' => __( 'Cancellation Reason', 'surecart' ), |
| 55 | 'comment' => __( 'Comment', 'surecart' ), |
| 56 | 'status' => __( 'Plan Status', 'surecart' ), |
| 57 | 'date' => __( 'Date', 'surecart' ), |
| 58 | 'mode' => '', |
| 59 | ], |
| 60 | parent::get_columns() |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * The subscription product. |
| 66 | * |
| 67 | * @param \SureCart\Models\CancellationAct $act Cancellation reason model. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public function column_plan( $act ) { |
| 72 | if ( empty( $act->subscription->price->product ) ) { |
| 73 | return __( 'No product', 'surecart' ); |
| 74 | } |
| 75 | return '<a href="' . esc_url( \SureCart::getUrl()->show( 'subscription', $act->subscription->id ) ) . '">' . esc_html( $act->subscription->price->product->name ) . '</a>'; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * The subscription product. |
| 80 | * |
| 81 | * @param \SureCart\Models\CancellationAct $act Cancellation reason model. |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | public function column_date( $act ) { |
| 86 | return $act->performed_at_date_time; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * We only have one default view. |
| 91 | */ |
| 92 | protected function get_views() { |
| 93 | return [ 'all' => __( 'All Cancellation Attempts', 'surecart' ) ]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the table data |
| 98 | * |
| 99 | * @return Object |
| 100 | */ |
| 101 | protected function table_data() { |
| 102 | $mode = sanitize_text_field( wp_unslash( $_GET['mode'] ?? '' ) ); |
| 103 | $conditions = array(); |
| 104 | |
| 105 | if ( ! empty( $mode ) ) { |
| 106 | $conditions['live_mode'] = 'live' === $mode; |
| 107 | } |
| 108 | |
| 109 | return CancellationAct::where( $conditions ) |
| 110 | ->with( |
| 111 | [ |
| 112 | 'subscription', |
| 113 | 'subscription.customer', |
| 114 | 'cancellation_reason', |
| 115 | 'subscription.price', |
| 116 | 'price.product', |
| 117 | ] |
| 118 | ) |
| 119 | ->paginate( |
| 120 | [ |
| 121 | 'per_page' => $this->get_items_per_page( 'cancellation_acts' ), |
| 122 | 'page' => $this->get_pagenum(), |
| 123 | ] |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Nothing found. |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | public function no_items() { |
| 133 | echo esc_html_e( 'No cancellation acts.', 'surecart' ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * The cancellation reason. |
| 138 | * |
| 139 | * @param \SureCart\Models\CancellationAct $act Cancellation act. |
| 140 | * |
| 141 | * @return string |
| 142 | */ |
| 143 | public function column_cancellation_reason( $act ) { |
| 144 | return esc_html( $act->cancellation_reason->label ?? '-' ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * The customer. |
| 149 | * |
| 150 | * @param \SureCart\Models\CancellationAct $act Cancellation act model. |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | public function column_customer( $act ) { |
| 155 | ob_start(); |
| 156 | ?> |
| 157 | <a class="row-title" aria-label="<?php esc_attr_e( 'Edit Customer', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->edit( 'customers', $act->subscription->customer->id ) ); ?>"> |
| 158 | <?php echo esc_html( $act->subscription->customer->name ?? $act->subscription->customer->email ?? __( 'No name provided', 'surecart' ) ); ?> |
| 159 | </a> |
| 160 | |
| 161 | <?php |
| 162 | echo $this->row_actions( |
| 163 | [ |
| 164 | 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->edit( 'customers', $act->subscription->customer->id ) ) . '" aria-label="' . esc_attr__( 'View Customer', 'surecart' ) . '">' . esc_html__( 'View Customer', 'surecart' ) . '</a>', |
| 165 | ], |
| 166 | ); |
| 167 | ?> |
| 168 | |
| 169 | <?php |
| 170 | return ob_get_clean(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Cancellation comment. |
| 175 | * |
| 176 | * @param \SureCart\Models\CancellationAct $act Cancellation act model. |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | public function column_comment( $act ) { |
| 181 | return esc_html( $act->comment ?? '-' ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * The mode for the model. |
| 186 | * |
| 187 | * @param SureCart\Model $model Model. |
| 188 | * |
| 189 | * @return string |
| 190 | */ |
| 191 | public function column_mode( $act ) { |
| 192 | return empty( $act->subscription->live_mode ) ? '<sc-tag type="warning">' . __( 'Test', 'surecart' ) . '</sc-tag>' : ''; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * The status |
| 197 | * |
| 198 | * @param \SureCart\Models\CancellationAct $act Cancellation act model. |
| 199 | * |
| 200 | * @return string |
| 201 | */ |
| 202 | public function column_status( $act ) { |
| 203 | if ( $act->preserved ) { |
| 204 | ob_start(); |
| 205 | ?> |
| 206 | <sc-tag type="success"> |
| 207 | <?php |
| 208 | // translators: Subscription saver feature. This is displayed if the subscription has been "Saved" from cancellation. |
| 209 | echo esc_html__( 'Saved', 'surecart' ); |
| 210 | ?> |
| 211 | </sc-tag> |
| 212 | <?php if ( $act->coupon_applied ) { ?> |
| 213 | <sc-tag type="info"><?php echo esc_html__( 'Coupon Applied', 'surecart' ); ?></sc-tag> |
| 214 | <?php } ?> |
| 215 | <?php |
| 216 | return ob_get_clean(); |
| 217 | } |
| 218 | |
| 219 | $badge = '<sc-subscription-status-badge id="act-' . esc_attr( $act->id ) . '"></sc-subscription-status-badge>'; |
| 220 | |
| 221 | \SureCart::assets()->addComponentData( |
| 222 | 'sc-subscription-status-badge', |
| 223 | '#act-' . esc_attr( $act->id ), |
| 224 | [ |
| 225 | 'subscription' => $act->subscription, |
| 226 | ] |
| 227 | ); |
| 228 | |
| 229 | return $badge; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Displays extra table navigation. |
| 234 | * |
| 235 | * @param string $which Top or bottom placement. |
| 236 | */ |
| 237 | protected function extra_tablenav( $which ) { |
| 238 | ?> |
| 239 | <input type="hidden" name="page" value="sc-cancellation-insights" /> |
| 240 | |
| 241 | <div class="alignleft actions"> |
| 242 | <?php |
| 243 | if ( 'top' === $which ) { |
| 244 | ob_start(); |
| 245 | $this->mode_dropdown(); |
| 246 | |
| 247 | /** |
| 248 | * Fires before the Filter button on the Posts and Pages list tables. |
| 249 | * |
| 250 | * The Filter button allows sorting by date and/or category on the |
| 251 | * Posts list table, and sorting by date on the Pages list table. |
| 252 | * |
| 253 | * @since 2.1.0 |
| 254 | * @since 4.4.0 The `$post_type` parameter was added. |
| 255 | * @since 4.6.0 The `$which` parameter was added. |
| 256 | * |
| 257 | * @param string $post_type The post type slug. |
| 258 | * @param string $which The location of the extra table nav markup: |
| 259 | * 'top' or 'bottom' for WP_Posts_List_Table, |
| 260 | * 'bar' for WP_Media_List_Table. |
| 261 | */ |
| 262 | do_action( 'restrict_manage_cancellation_insights', $this->screen->post_type, $which ); |
| 263 | |
| 264 | $output = ob_get_clean(); |
| 265 | |
| 266 | if ( ! empty( $output ) ) { |
| 267 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 268 | submit_button( __( 'Filter', 'surecart' ), '', 'filter_action', false, array( 'id' => 'filter-by-mode-submit' ) ); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | ?> |
| 273 | </div> |
| 274 | |
| 275 | <?php |
| 276 | /** |
| 277 | * Fires immediately following the closing "actions" div in the tablenav |
| 278 | * for the affiliate referrals list table. |
| 279 | * |
| 280 | * @param string $which The location of the extra table nav markup: 'top' or 'bottom'. |
| 281 | */ |
| 282 | do_action( 'manage_cancellation_insights_extra_tablenav', $which ); |
| 283 | } |
| 284 | } |
| 285 |