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