PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.25.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.25.2
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 in SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments 2.25.2, at app/src/Controllers/Admin/Subscriptions/SubscriptionsListTable.php

407 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 $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 'integrations' => __( 'Integrations', '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 /**
127 * Show any integrations.
128 *
129 * @param \SureCart\Models\Subscription $subscription The subscription model.
130 * @return string
131 */
132 public function column_integrations( $subscription ) {
133 $product = $subscription->purchase->product ?? null;
134 $output = $product ? $this->productIntegrationsList( [ 'product_id' => $product ] ) : false;
135 return $output ? $output : '-';
136 }
137 /**
138 * Define which columns are hidden
139 *
140 * @return Array
141 */
142 public function get_hidden_columns() {
143 return array();
144 }
145
146 /**
147 * Define the sortable columns
148 *
149 * @return Array
150 */
151 public function get_sortable_columns() {
152 return array( 'title' => array( 'title', false ) );
153 }
154
155 /**
156 * Get the table data
157 *
158 * @return Array
159 */
160 protected function table_data() {
161 return Subscription::where(
162 [
163 'status' => $this->getStatus(),
164 'query' => $this->get_search_query(),
165 ]
166 )->with( [ 'customer', 'price', 'price.product', 'current_period', 'purchase' ] )
167 ->paginate(
168 [
169 'per_page' => $this->get_items_per_page( 'subscriptions' ),
170 'page' => $this->get_pagenum(),
171 ]
172 );
173 }
174
175 /**
176 * Get the archive query status.
177 *
178 * @return boolean|null
179 */
180 public function getStatus() {
181 $status = sanitize_text_field( wp_unslash( $_GET['status'] ?? null ) );
182 if ( 'all' === $status ) {
183 $status = null;
184 }
185 return $status ? [ esc_html( $status ) ] : [];
186 }
187
188 /**
189 * The remaining payments for the subscription
190 *
191 * @param \SureCart\Models\Subscription $subscription The subscription model.
192 *
193 * @return string
194 */
195 public function column_remaining_payments( $subscription ) {
196 // handle non-finite subscriptions.
197 if ( ! $subscription->finite ) {
198 return 'completed' === $subscription->status ? '-' : '&infin;';
199 }
200
201 // handle payment plans.
202 return 0 === $subscription->remaining_period_count ? __( 'None', 'surecart' ) : (int) $subscription->remaining_period_count;
203 }
204
205 /**
206 * The subscription type
207 *
208 * @param \SureCart\Models\Subscription $subscription The subscription model.
209 *
210 * @return string
211 */
212 // public function column_type( $subscription ) {
213 // if ( null === $subscription->remaining_period_count ) {
214 // return '<sc-tag type="success">' . __( 'Subscription', 'surecart' ) . '</sc-tag>';
215 // }
216 // return '<sc-tag type="info">' . __( 'Payment Plan', 'surecart' ) . '</sc-tag>';
217 // }
218
219 /**
220 * Handle the total column
221 *
222 * @param \SureCart\Models\Subscription $subscription Checkout Session Model.
223 *
224 * @return string
225 */
226 public function column_plan( $subscription ) {
227 $amount = $subscription->ad_hoc_amount ?? $subscription->price->amount ?? 0;
228 $interval = $subscription->price->recurring_interval ?? '';
229 $count = $subscription->price->recurring_interval_count ?? 1;
230 $period_count = $subscription->price->recurring_period_count ?? null;
231
232 ob_start();
233 echo '<sc-format-number type="currency" currency="' . esc_html( strtoupper( $subscription->currency ?? 'usd' ) ) . '" value="' . (float) $amount . '"></sc-format-number>';
234 echo esc_html( $this->getInterval( $interval, $count ) );
235 if ( null !== $period_count ) {
236 if ( 1 === $period_count ) {
237 echo ' ' . esc_html( __( '(one time)', 'surecart' ) );
238 } else {
239 echo esc_html( $this->getInterval( $interval, $period_count, __( 'for', 'surecart' ) ) );
240 }
241 }
242 echo $this->getProductDisplay( $subscription );
243 if ( ! empty( $subscription->variant_options ) ) {
244 echo '<div>' . esc_html( implode( ' / ', $subscription->variant_options ) ) . '</div>';
245 }
246
247 return ob_get_clean();
248 }
249
250 private function getProductDisplay( $subscription ) {
251 if ( empty( $subscription->price->product->name ) ) {
252 return $subscription->price->name ?? __( 'No Product', 'surecart' );
253 }
254
255 $price_name = ! empty( $subscription->price->name ) ? ' - ' . $subscription->price->name : '';
256
257 return '<br/><a href="' . esc_url( \SureCart::getUrl()->edit( 'product', $subscription->price->product->id ) ) . '">' . $subscription->price->product->name . '</a>' . $price_name;
258 }
259
260 public function getInterval( $interval, $count, $separator = '/', $show_single = false ) {
261 switch ( $interval ) {
262 case 'day':
263 return " $separator " . sprintf(
264 // translators: number of days.
265 $show_single ? _n( '%d day', '%d days', $count, 'surecart' ) : _n( 'day', '%d days', $count, 'surecart' ),
266 $count
267 );
268 case 'week':
269 return " $separator " . sprintf(
270 // translators: number of weeks.
271 $show_single ? _n( '%d week', '%d weeks', $count, 'surecart' ) : _n( 'week', '%d weeks', $count, 'surecart' ),
272 $count
273 );
274 case 'month':
275 return " $separator " . sprintf(
276 // translators: number of months
277 $show_single ? _n( '%d month', '%d months', $count, 'surecart' ) : _n( 'month', '%d months', $count, 'surecart' ),
278 $count
279 );
280 case 'year':
281 return " $separator " . sprintf(
282 // translators: number of yearls
283 $show_single ? _n( '%d year', '%d years', $count, 'surecart' ) : _n( 'year', '%d years', $count, 'surecart' ),
284 $count
285 );
286 }
287 }
288
289 /**
290 * Output the Promo Code
291 *
292 * @param Promotion $promotion Promotion model.
293 *
294 * @return string
295 */
296 public function column_usage( $promotion ) {
297 $max = $promotion->max_redemptions ?? '&infin;';
298 ob_start();
299 ?>
300 <?php echo \esc_html( "$promotion->times_redeemed / $max" ); ?>
301 <br />
302 <div style="opacity: 0.75"><?php echo \esc_html( $this->get_expiration_string( $promotion->redeem_by ) ); ?></div>
303 <?php
304 return ob_get_clean();
305 }
306
307 /**
308 * Render the "Redeem By".
309 *
310 * @param string $timestamp Redeem timestamp.
311 * @return string
312 */
313 public function get_expiration_string( $timestamp = '' ) {
314 if ( ! $timestamp ) {
315 return '';
316 }
317 // translators: coupon expiration date.
318 return sprintf( __( 'Valid until %s', 'surecart' ), date_i18n( get_option( 'date_format' ), $timestamp / 1000 ) );
319 }
320
321 public function get_price_string( $coupon = '' ) {
322 if ( ! $coupon || empty( $coupon->duration ) ) {
323 return;
324 }
325 if ( ! empty( $coupon->percent_off ) ) {
326 // translators: Coupon % off.
327 return sprintf( esc_html( __( '%1d%% off', 'surecart' ) ), $coupon->percent_off );
328 }
329
330 if ( ! empty( $coupon->amount_off ) ) {
331 // translators: Coupon amount off.
332 return Currency::formatCurrencyNumber( $coupon->amount_off ) . ' <small style="opacity: 0.75;">' . strtoupper( esc_html( $coupon->currency ) ) . '</small>';
333 }
334
335 return esc_html__( 'No discount.', 'surecart' );
336 }
337
338 /**
339 * Get the duration string
340 *
341 * @param Coupon|boolean $coupon Coupon object.
342 * @return string|void;
343 */
344 public function get_duration_string( $coupon = '' ) {
345 if ( ! $coupon || empty( $coupon->duration ) ) {
346 return;
347 }
348
349 if ( 'forever' === $coupon->duration ) {
350 return __( 'Forever', 'surecart' );
351 }
352 if ( 'repeating' === $coupon->duration ) {
353 // translators: number of months.
354 return sprintf( __( 'For %d months', 'surecart' ), $coupon->duration_in_months ?? 1 );
355 }
356
357 return __( 'Once', 'surecart' );
358 }
359
360 /**
361 * Handle the status
362 *
363 * @param \SureCart\Models\Subscription $subscription Subscription model.
364 *
365 * @return string
366 */
367 public function column_status( $subscription ) {
368 \SureCart::assets()->addComponentData(
369 'sc-subscription-status-badge',
370 '#subscription-' . esc_attr( $subscription->id ),
371 [
372 'subscription' => $subscription,
373 ]
374 );
375 return '<sc-subscription-status-badge id="subscription-' . esc_attr( $subscription->id ) . '"></sc-subscription-status-badge>';
376 }
377
378 /**
379 * Name of the coupon
380 *
381 * @param \SureCart\Models\Promotion $promotion Promotion model.
382 *
383 * @return string
384 */
385 public function column_customer( $subscription ) {
386 ob_start();
387 $name = $subscription->customer->name ?? '';
388 if ( ! $name ) {
389 $name = $subscription->customer->email ?? __( 'No name provided', 'surecart' );
390 }
391 ?>
392 <a class="row-title" aria-label="<?php echo esc_attr__( 'Edit Subscription', 'surecart' ); ?>" href="<?php echo esc_url( \SureCart::getUrl()->show( 'subscription', $subscription->id ) ); ?>">
393 <?php echo esc_html( $name ); ?>
394 </a>
395
396 <?php
397 echo $this->row_actions(
398 [
399 'edit' => '<a href="' . esc_url( \SureCart::getUrl()->show( 'subscription', $subscription->id ) ) . '" aria-label="' . esc_attr( 'Edit Subscription', 'surecart' ) . '">' . __( 'Edit', 'surecart' ) . '</a>',
400 ],
401 );
402 ?>
403 <?php
404 return ob_get_clean();
405 }
406 }
407