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