SubscriptionsListTable.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Subscriptions; |
| 4 | |
| 5 | use SureCart\Support\Currency; |
| 6 | use SureCart\Controllers\Admin\Tables\ListTable; |
| 7 | use SureCart\Models\Subscription; |
| 8 | |
| 9 | /** |
| 10 | * Create a new table class that will extend the WP_List_Table |
| 11 | */ |
| 12 | class SubscriptionsListTable extends ListTable { |
| 13 | /** |
| 14 | * Prepare the items for the table to process |
| 15 | * |
| 16 | * @return Void |
| 17 | */ |
| 18 | public function prepare_items() { |
| 19 | $columns = $this->get_columns(); |
| 20 | $hidden = $this->get_hidden_columns(); |
| 21 | $sortable = $this->get_sortable_columns(); |
| 22 | |
| 23 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 24 | |
| 25 | $query = $this->table_data(); |
| 26 | if ( is_wp_error( $query ) ) { |
| 27 | $this->items = []; |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $this->set_pagination_args( |
| 32 | [ |
| 33 | 'total_items' => $query->pagination->count, |
| 34 | 'per_page' => $this->get_items_per_page( 'orders' ), |
| 35 | ] |
| 36 | ); |
| 37 | |
| 38 | $this->items = $query->data; |
| 39 | } |
| 40 | |
| 41 | public function search() { |
| 42 | ?> |
| 43 | <form class="search-form" |
| 44 | method="get"> |
| 45 | <?php $this->search_box( __( 'Search Subscriptions', 'surecart' ), 'order' ); ?> |
| 46 | <input type="hidden" |
| 47 | name="id" |
| 48 | value="1" /> |
| 49 | </form> |
| 50 | <?php |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @global int $post_id |
| 55 | * @global string $comment_status |
| 56 | * @global string $comment_type |
| 57 | */ |
| 58 | protected function get_views() { |
| 59 | $stati = [ |
| 60 | 'all' => __( 'All', 'surecart' ), |
| 61 | 'active' => __( 'Active', 'surecart' ), |
| 62 | 'canceled' => __( 'Canceled', 'surecart' ), |
| 63 | ]; |
| 64 | |
| 65 | $link = \SureCart::getUrl()->index( 'subscriptions' ); |
| 66 | |
| 67 | foreach ( $stati as $status => $label ) { |
| 68 | $current_link_attributes = ''; |
| 69 | |
| 70 | if ( ! empty( $_GET['status'] ) ) { |
| 71 | if ( $status === $_GET['status'] ) { |
| 72 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 73 | } |
| 74 | } elseif ( 'all' === $status ) { |
| 75 | $current_link_attributes = ' class="current" aria-current="page"'; |
| 76 | } |
| 77 | |
| 78 | $link = add_query_arg( 'status', $status, $link ); |
| 79 | |
| 80 | $status_links[ $status ] = "<a href='$link'$current_link_attributes>" . $label . '</a>'; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Filters the comment status links. |
| 85 | * |
| 86 | * @since 2.5.0 |
| 87 | * @since 5.1.0 The 'Mine' link was added. |
| 88 | * |
| 89 | * @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine', |
| 90 | * 'Pending', 'Approved', 'Spam', and 'Trash'. |
| 91 | */ |
| 92 | return apply_filters( 'surecart/subscription/index/links', $status_links ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Override the parent columns method. Defines the columns to use in your listing table |
| 97 | * |
| 98 | * @return Array |
| 99 | */ |
| 100 | public function get_columns() { |
| 101 | return [ |
| 102 | 'customer' => __( 'Customer', 'surecart' ), |
| 103 | 'status' => __( 'Status', 'surecart' ), |
| 104 | 'plan' => __( 'Plan', 'surecart' ), |
| 105 | 'product' => __( 'Product', 'surecart' ), |
| 106 | 'integrations' => __( 'Integrations', 'surecart' ), |
| 107 | 'remaining_payments' => __( 'Remaining Payments', 'surecart' ), |
| 108 | 'type' => __( 'Type', 'surecart' ), |
| 109 | 'created' => __( 'Created', 'surecart' ), |
| 110 | 'mode' => '', |
| 111 | ]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Displays the checkbox column. |
| 116 | * |
| 117 | * @param Product $product The product model. |
| 118 | */ |
| 119 | public function column_cb( $product ) { |
| 120 | ?> |
| 121 | <label class="screen-reader-text" for="cb-select-<?php echo esc_attr( $product['id'] ); ?>"><?php _e( 'Select comment', 'surecart' ); ?></label> |
| 122 | <input id="cb-select-<?php echo esc_attr( $product['id'] ); ?>" type="checkbox" name="delete_comments[]" value="<?php echo esc_attr( $product['id'] ); ?>" /> |
| 123 | <?php |
| 124 | } |
| 125 | |
| 126 | public function column_product( $subscription ) { |
| 127 | if ( empty( $subscription->price->product ) ) { |
| 128 | return __( 'No product', 'surecart' ); |
| 129 | } |
| 130 | return '<a href="' . esc_url( \SureCart::getUrl()->edit( 'product', $subscription->price->product->id ) ) . '">' . $subscription->price->product->name . '</a>'; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Show any integrations. |
| 135 | * |
| 136 | * @param \SureCart\Models\Subscription $subscription The subscription model. |
| 137 | * @return string |
| 138 | */ |
| 139 | public function column_integrations( $subscription ) { |
| 140 | $output = $this->productIntegrationsList( $subscription->purchase->product ); |
| 141 | return $output ? $output : '-'; |
| 142 | } |
| 143 | /** |
| 144 | * Define which columns are hidden |
| 145 | * |
| 146 | * @return Array |
| 147 | */ |
| 148 | public function get_hidden_columns() { |
| 149 | return array(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Define the sortable columns |
| 154 | * |
| 155 | * @return Array |
| 156 | */ |
| 157 | public function get_sortable_columns() { |
| 158 | return array( 'title' => array( 'title', false ) ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the table data |
| 163 | * |
| 164 | * @return Array |
| 165 | */ |
| 166 | protected function table_data() { |
| 167 | return Subscription::where( |
| 168 | [ |
| 169 | 'status' => $this->getStatus(), |
| 170 | ] |
| 171 | )->with( [ 'customer', 'price', 'price.product', 'latest_invoice', 'purchase' ] ) |
| 172 | ->paginate( |
| 173 | [ |
| 174 | 'per_page' => $this->get_items_per_page( 'subscriptions' ), |
| 175 | 'page' => $this->get_pagenum(), |
| 176 | ] |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get the archive query status. |
| 182 | * |
| 183 | * @return boolean|null |
| 184 | */ |
| 185 | public function getStatus() { |
| 186 | $status = sanitize_text_field( wp_unslash( $_GET['status'] ?? null ) ); |
| 187 | if ( 'all' === $status ) { |
| 188 | $status = null; |
| 189 | } |
| 190 | return $status ? [ esc_html( $status ) ] : []; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * The remaining payments for the subscription |
| 195 | * |
| 196 | * @param \SureCart\Models\Subscription $subscription The subscription model. |
| 197 | * |
| 198 | * @return string |
| 199 | */ |
| 200 | public function column_remaining_payments( $subscription ) { |
| 201 | if ( null === $subscription->remaining_period_count ) { |
| 202 | if ( 'completed' === $subscription->status ) { |
| 203 | return '-'; |
| 204 | } else { |
| 205 | return '∞'; |
| 206 | } |
| 207 | } |
| 208 | if ( 0 === $subscription->remaining_period_count ) { |
| 209 | return __( 'None', 'surecart' ); |
| 210 | } |
| 211 | |
| 212 | return (int) $subscription->remaining_period_count; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * The subscription type |
| 217 | * |
| 218 | * @param \SureCart\Models\Subscription $subscription The subscription model. |
| 219 | * |
| 220 | * @return string |
| 221 | */ |
| 222 | public function column_type( $subscription ) { |
| 223 | if ( null === $subscription->remaining_period_count ) { |
| 224 | return '<sc-tag type="success">' . __( 'Subscription', 'surecart' ) . '</sc-tag>'; |
| 225 | } |
| 226 | return '<sc-tag type="info">' . __( 'Payment Plan', 'surecart' ) . '</sc-tag>'; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Handle the total column |
| 231 | * |
| 232 | * @param \SureCart\Models\Order $subscription Checkout Session Model. |
| 233 | * |
| 234 | * @return string |
| 235 | */ |
| 236 | public function column_plan( $subscription ) { |
| 237 | $interval = $subscription->price->recurring_interval ?? ''; |
| 238 | $count = $subscription->price->recurring_interval_count ?? 1; |
| 239 | $period_count = $subscription->price->recurring_period_count ?? null; |
| 240 | ob_start(); |
| 241 | echo '<sc-format-number type="currency" currency="' . esc_html( strtoupper( $subscription->latest_invoice->currency ?? 'usd' ) ) . '" value="' . (float) ( $subscription->latest_invoice->total_amount ?? 0 ) . '"></sc-format-number>'; |
| 242 | echo esc_html( $this->getInterval( $interval, $count ) ); |
| 243 | if ( null !== $period_count ) { |
| 244 | echo esc_html( $this->getInterval( $interval, $period_count, __( 'for', 'surecart' ) ) ); |
| 245 | } |
| 246 | return ob_get_clean(); |
| 247 | } |
| 248 | |
| 249 | public function getInterval( $interval, $count, $separator = '/', $show_single = false ) { |
| 250 | switch ( $interval ) { |
| 251 | case 'day': |
| 252 | return " $separator " . sprintf( |
| 253 | // translators: number of days. |
| 254 | $show_single ? _n( '%d day', '%d days', $count, 'surecart' ) : _n( 'day', '%d days', $count, 'surecart' ), |
| 255 | $count |
| 256 | ); |
| 257 | case 'week': |
| 258 | return " $separator " . sprintf( |
| 259 | // translators: number of weeks. |
| 260 | $show_single ? _n( '%d week', '%d weeks', $count, 'surecart' ) : _n( 'week', '%d weeks', $count, 'surecart' ), |
| 261 | $count |
| 262 | ); |
| 263 | case 'month': |
| 264 | return " $separator " . sprintf( |
| 265 | // translators: number of months |
| 266 | $show_single ? _n( '%d month', '%d months', $count, 'surecart' ) : _n( 'month', '%d months', $count, 'surecart' ), |
| 267 | $count |
| 268 | ); |
| 269 | case 'year': |
| 270 | return " $separator " . sprintf( |
| 271 | // translators: number of yearls |
| 272 | $show_single ? _n( '%d year', '%d years', $count, 'surecart' ) : _n( 'year', '%d years', $count, 'surecart' ), |
| 273 | $count |
| 274 | ); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Output the Promo Code |
| 280 | * |
| 281 | * @param Promotion $promotion Promotion model. |
| 282 | * |
| 283 | * @return string |
| 284 | */ |
| 285 | public function column_usage( $promotion ) { |
| 286 | $max = $promotion->max_redemptions ?? '∞'; |
| 287 | ob_start(); |
| 288 | ?> |
| 289 | <?php echo \esc_html( "$promotion->times_redeemed / $max" ); ?> |
| 290 | <br /> |
| 291 | <div style="opacity: 0.75"><?php echo \esc_html( $this->get_expiration_string( $promotion->redeem_by ) ); ?></div> |
| 292 | <?php |
| 293 | return ob_get_clean(); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Render the "Redeem By". |
| 298 | * |
| 299 | * @param string $timestamp Redeem timestamp. |
| 300 | * @return string |
| 301 | */ |
| 302 | public function get_expiration_string( $timestamp = '' ) { |
| 303 | if ( ! $timestamp ) { |
| 304 | return ''; |
| 305 | } |
| 306 | // translators: coupon expiration date. |
| 307 | return sprintf( __( 'Valid until %s', 'surecart' ), date_i18n( get_option( 'date_format' ), $timestamp / 1000 ) ); |
| 308 | } |
| 309 | |
| 310 | public function get_price_string( $coupon = '' ) { |
| 311 | if ( ! $coupon || empty( $coupon->duration ) ) { |
| 312 | return; |
| 313 | } |
| 314 | if ( ! empty( $coupon->percent_off ) ) { |
| 315 | // translators: Coupon % off. |
| 316 | return sprintf( esc_html( __( '%1d%% off', 'surecart' ) ), $coupon->percent_off ); |
| 317 | } |
| 318 | |
| 319 | if ( ! empty( $coupon->amount_off ) ) { |
| 320 | // translators: Coupon amount off. |
| 321 | return Currency::formatCurrencyNumber( $coupon->amount_off ) . ' <small style="opacity: 0.75;">' . strtoupper( esc_html( $coupon->currency ) ) . '</small>'; |
| 322 | } |
| 323 | |
| 324 | return esc_html__( 'No discount.', 'surecart' ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Get the duration string |
| 329 | * |
| 330 | * @param Coupon|boolean $coupon Coupon object. |
| 331 | * @return string|void; |
| 332 | */ |
| 333 | public function get_duration_string( $coupon = '' ) { |
| 334 | if ( ! $coupon || empty( $coupon->duration ) ) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | if ( 'forever' === $coupon->duration ) { |
| 339 | return __( 'Forever', 'surecart' ); |
| 340 | } |
| 341 | if ( 'repeating' === $coupon->duration ) { |
| 342 | // translators: number of months. |
| 343 | return sprintf( __( 'For %d months', 'surecart' ), $coupon->duration_in_months ?? 1 ); |
| 344 | } |
| 345 | |
| 346 | return __( 'Once', 'surecart' ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Handle the status |
| 351 | * |
| 352 | * @param \SureCart\Models\Price $product Product model. |
| 353 | * |
| 354 | * @return string |
| 355 | */ |
| 356 | public function column_status( $subscription ) { |
| 357 | return wp_kses_post( "<sc-subscription-status-badge status='$subscription->status'></sc-subscription-status-badge>" ); |
| 358 | // switch ( $subscription->status ) { |
| 359 | // case 'active': |
| 360 | // $status = '<sc-tag type="success">' . __( 'Active', 'surecart' ) . '</sc-tag>'; |
| 361 | // break; |
| 362 | // case 'canceled': |
| 363 | // $status = '<sc-tag type="danger">' . __( 'Canceled', 'surecart' ) . '</sc-tag>'; |
| 364 | // break; |
| 365 | // case 'trialing': |
| 366 | // $status = '<sc-tag type="primary">' . __( 'Trialing', 'surecart' ) . '</sc-tag>'; |
| 367 | // break; |
| 368 | // case 'draft': |
| 369 | // $status = '<sc-tag>' . __( 'Draft', 'surecart' ) . '</sc-tag>'; |
| 370 | // break; |
| 371 | // default: |
| 372 | // $status = '<sc-tag>' . $subscription->status . '</sc-tag>'; |
| 373 | // break; |
| 374 | // } |
| 375 | |
| 376 | // if ( ! empty( (array) $subscription->pending_update ) ) { |
| 377 | // $status .= ' <sc-tag type="info">' . __( 'Update Pending', 'surecart' ) . '</sc-tag>'; |
| 378 | // } |
| 379 | |
| 380 | // return $status; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Name of the coupon |
| 385 | * |
| 386 | * @param \SureCart\Models\Promotion $promotion Promotion model. |
| 387 | * |
| 388 | * @return string |
| 389 | */ |
| 390 | public function column_customer( $subscription ) { |
| 391 | ob_start(); |
| 392 | $name = $subscription->customer->name ?? ''; |
| 393 | if ( ! $name ) { |
| 394 | $name = $subscription->customer->email ?? __( 'No name provided', 'surecart' ); |
| 395 | } |
| 396 | ?> |
| 397 | <a class="row-title" aria-label="<?php echo esc_attr__( 'Edit Subscription', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->show( 'subscription', $subscription->id ) ); ?>"> |
| 398 | <?php echo esc_html( $name ); ?> |
| 399 | </a> |
| 400 | |
| 401 | <?php |
| 402 | echo $this->row_actions( |
| 403 | [ |
| 404 | 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->show( 'subscription', $subscription->id ) ) . '" aria-label="' . esc_attr( 'Edit Subscription', 'surecart' ) . '">' . __( 'Edit', 'surecart' ) . '</a>', |
| 405 | ], |
| 406 | ); |
| 407 | ?> |
| 408 | <?php |
| 409 | return ob_get_clean(); |
| 410 | } |
| 411 | } |
| 412 |