PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.32.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.32.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / stripe / helpers / FrmTransLiteListHelper.php

FrmTransLiteListHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.32.1, at stripe/helpers/FrmTransLiteListHelper.php

661 lines 16.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmTransLiteListHelper extends FrmListHelper {
7
8 /**
9 * @var string
10 */
11 private $table = '';
12
13 /**
14 * An array of all valid entry ids.
15 * This is retrieved all at once with a single database query.
16 * This is used to determine if a specific entry is deleted.
17 * When an entry is deleted, there is no link to the deleted entry.
18 *
19 * @var int[]
20 */
21 private $valid_entry_ids = array();
22
23 /**
24 * @param array $args
25 */
26 public function __construct( $args ) {
27 $this->table = FrmAppHelper::get_simple_request(
28 array(
29 'param' => 'trans_type',
30 'type' => 'request',
31 )
32 );
33
34 parent::__construct( $args );
35 $this->screen->set_screen_reader_content(
36 array(
37 'heading_list' => esc_html__( 'Payments list', 'formidable' ),
38 )
39 );
40 }
41
42 /**
43 * @return void
44 */
45 public function prepare_items() {
46 global $wpdb;
47
48 $orderby = FrmAppHelper::get_param( 'orderby', 'id', 'get', 'sanitize_title' );
49 $order = FrmAppHelper::get_param( 'order', 'DESC', 'get', 'sanitize_text_field' );
50
51 if ( ! is_string( $orderby ) ) {
52 $orderby = 'id';
53 }
54
55 if ( ! is_string( $order ) || ! in_array( strtoupper( $order ), array( 'ASC', 'DESC' ), true ) ) {
56 $order = 'DESC';
57 }
58
59 $page = $this->get_pagenum();
60 $per_page = $this->get_items_per_page( 'formidable_page_formidable_payments_per_page' );
61 $start = ( $page - 1 ) * $per_page;
62 $start = FrmAppHelper::get_param( 'start', $start, 'get', 'absint' );
63 $query = $this->get_table_query();
64 $order_query = FrmDb::esc_order( "ORDER BY p.{$orderby} $order" );
65
66 // @codingStandardsIgnoreStart
67 $this->items = $wpdb->get_results(
68 $wpdb->prepare(
69 'SELECT p.* ' . $query . $order_query . ' LIMIT %d, %d',
70 $start,
71 $per_page
72 )
73 );
74 $total_items = $wpdb->get_var( 'SELECT COUNT(*) ' . $query );
75 // @codingStandardsIgnoreEnd
76
77 $this->set_pagination_args(
78 array(
79 'total_items' => $total_items,
80 'per_page' => $per_page,
81 )
82 );
83 }
84
85 /**
86 * @return string
87 */
88 private function get_table_query() {
89 global $wpdb;
90
91 $table_name = $this->table === 'subscriptions' ? 'frm_subscriptions' : 'frm_payments';
92 $form_id = FrmAppHelper::get_param( 'form', 0, 'get', 'absint' );
93
94 if ( ! $form_id ) {
95 return "FROM `{$wpdb->prefix}{$table_name}` p";
96 }
97
98 // @codingStandardsIgnoreStart
99 return $wpdb->prepare(
100 "FROM `{$wpdb->prefix}{$table_name}` p
101 JOIN `{$wpdb->prefix}frm_items` i ON p.item_id = i.id
102 WHERE i.form_id = %d",
103 $form_id
104 );
105 // @codingStandardsIgnoreEnd
106 }
107
108 /**
109 * @return void
110 */
111 public function no_items() {
112 esc_html_e( 'No payments found.', 'formidable' );
113 }
114
115 /**
116 * @return array
117 */
118 public function get_views() {
119 $statuses = array(
120 'payments' => __( 'Payments', 'formidable' ),
121 'subscriptions' => __( 'Subscriptions', 'formidable' ),
122 );
123
124 $links = array();
125 $frm_payment = new FrmTransLitePayment();
126 $frm_sub = new FrmTransLiteSubscription();
127 $counts = array(
128 'payments' => $frm_payment->get_count(),
129 'subscriptions' => $frm_sub->get_count(),
130 );
131 $type = FrmAppHelper::get_simple_request(
132 array(
133 'param' => 'trans_type',
134 'type' => 'request',
135 'default' => 'payments',
136 )
137 );
138
139 foreach ( $statuses as $status => $name ) {
140 $class = $status === $type ? ' class="current"' : '';
141
142 if ( $counts[ $status ] || 'payments' === $status ) {
143 // phpcs:disable Generic.WhiteSpace.ScopeIndent
144 $links[ $status ] = '<a href="' . esc_url( '?page=formidable-payments&trans_type=' . $status ) . '" ' . $class . '>'
145 // translators: %1$s: Transaction type (Payments or Subscriptions), %2$s: Span start tag, %3$s: Count, %4$s: Span close tag.
146 . sprintf( esc_html__( '%1$s %2$s(%3$s)%4$s', 'formidable' ), esc_html( $name ), '<span class="count">', number_format_i18n( $counts[ $status ] ), '</span>' )
147 . '</a>';
148 // phpcs:enable Generic.WhiteSpace.ScopeIndent
149 }
150
151 unset( $status, $name );
152 }
153
154 return $links;
155 }
156
157 /**
158 * @return array
159 */
160 public function get_columns() {
161 return FrmTransLiteListsController::payment_columns();
162 }
163
164 /**
165 * @return array
166 */
167 public function get_sortable_columns() {
168 return array(
169 'item_id' => 'item_id',
170 'amount' => 'amount',
171 'created_at' => 'created_at',
172 'receipt_id' => 'receipt_id',
173 'sub_id' => 'sub_id',
174 'begin_date' => 'begin_date',
175 'expire_date' => 'expire_date',
176 'status' => 'status',
177 'next_bill_date' => 'next_bill_date',
178 );
179 }
180
181 /**
182 * If the Payments submodule or the PayPal add-on is active, add a bulk delete action.
183 *
184 * @since 6.27
185 *
186 * @return array
187 */
188 public function get_bulk_actions() {
189 $actions = array();
190
191 if ( $this->payments_addon_list_helper_exists() ) {
192 $actions['bulk_delete'] = __( 'Delete', 'formidable' );
193 }
194
195 return $actions;
196 }
197
198 /**
199 * @param string $which
200 *
201 * @return void
202 */
203 public function extra_tablenav( $which ) {
204 $footer = $which !== 'top';
205
206 if ( $footer ) {
207 return;
208 }
209
210 $form_id = FrmAppHelper::simple_get( 'form', 'absint', 0 );
211
212 if ( $form_id ) {
213 // Don't show the switcher if it's already in the header.
214 return;
215 }
216
217 FrmFormsHelper::forms_dropdown(
218 'form',
219 $form_id,
220 array( 'blank' => __( 'View all forms', 'formidable' ) )
221 );
222
223 echo '<input type="hidden" name="trans_type" value="' . esc_attr( FrmAppHelper::get_param( 'trans_type', 'payments', 'get', 'sanitize_text_field' ) ) . '">';
224 echo '<input id="post-query-submit" class="button" type="submit" value="Filter" name="filter_action" />';
225 }
226
227 /**
228 * @return void
229 */
230 public function display_rows() {
231 $date_format = FrmTransLiteAppHelper::get_date_format();
232 $gateways = FrmTransLiteAppHelper::get_gateways();
233 $alt = 0;
234 $form_ids = $this->get_form_ids();
235 $args = compact( 'form_ids', 'date_format', 'gateways' );
236 // $form_ids is indexed by entry ID.
237 $this->valid_entry_ids = array_keys( $form_ids );
238
239 foreach ( $this->items as $item ) {
240 echo '<tr id="payment-' . esc_attr( $item->id ) . '" ';
241
242 $is_alternate = 0 === $alt % 2;
243 ++$alt;
244
245 if ( $is_alternate ) {
246 echo 'class="alternate"';
247 }
248
249 echo '>';
250 $this->display_columns( $item, $args );
251 echo '</tr>';
252
253 unset( $item );
254 }
255 }
256
257 /**
258 * @param object $item
259 * @param array $args
260 *
261 * @return void
262 */
263 private function display_columns( $item, $args ) {
264 list( $columns, $hidden ) = $this->get_column_info();
265
266 foreach ( $columns as $column_name => $column_display_name ) {
267 $attributes = self::get_row_classes( compact( 'column_name', 'hidden' ) );
268 $args['column_name'] = $column_name;
269 $val = $this->get_column_value( $item, $args );
270
271 echo '<td ' . $attributes . '>' . $val . '</td>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
272 unset( $val );
273 }
274 }
275
276 protected function get_column_info() {
277 $column_info = parent::get_column_info();
278
279 if ( ! $this->payments_addon_list_helper_exists() ) {
280 // Remove the checkbox column for Lite only.
281 unset( $column_info[0]['cb'] );
282 }
283
284 return $column_info;
285 }
286
287 /**
288 * Check for Payments submodule (Stripe, Authorize.Net add-ons), as well as PayPal.
289 *
290 * @since 6.27
291 *
292 * @return bool
293 */
294 private function payments_addon_list_helper_exists() {
295 return class_exists( 'FrmTransListHelper' ) || class_exists( 'FrmPaymentsListHelper' );
296 }
297
298 /**
299 * @param object $item
300 * @param array $args
301 *
302 * @return string
303 */
304 private function get_column_value( $item, $args ) {
305 $column_name = $args['column_name'];
306 $function_name = 'get_' . $column_name . '_column';
307
308 if ( method_exists( $this, $function_name ) ) {
309 $val = $this->$function_name( $item, $args );
310 } else {
311 $val = $item->$column_name ? $item->$column_name : '';
312
313 if ( str_contains( $column_name, '_date' ) ) {
314 if ( ! empty( $item->$column_name ) && $item->$column_name !== '0000-00-00' ) {
315 $val = FrmTransLiteAppHelper::format_the_date( $item->$column_name, $args['date_format'] );
316 } else {
317 $val = '';
318 }
319 }
320 }
321
322 return $val;
323 }
324
325 /**
326 * @return array
327 */
328 private function get_form_ids() {
329 $entry_ids = array();
330
331 foreach ( $this->items as $item ) {
332 $entry_ids[] = absint( $item->item_id );
333 unset( $item );
334 }
335
336 global $wpdb;
337 // @codingStandardsIgnoreStart
338 $forms = $wpdb->get_results(
339 "SELECT
340 fo.id as form_id,
341 fo.name,
342 e.id
343 FROM {$wpdb->prefix}frm_items e
344 LEFT JOIN {$wpdb->prefix}frm_forms fo ON e.form_id = fo.id
345 WHERE e.id in (" . implode( ',', $entry_ids ) . ')'
346 );
347 // @codingStandardsIgnoreEnd
348 unset( $entry_ids );
349
350 $form_ids = array();
351
352 foreach ( $forms as $form ) {
353 $form_ids[ $form->id ] = $form;
354 unset( $form );
355 }
356
357 return $form_ids;
358 }
359
360 /**
361 * @param array $atts
362 *
363 * @return string
364 */
365 private function get_row_classes( $atts ) {
366 $class = 'column-' . $atts['column_name'];
367
368 if ( in_array( $atts['column_name'], $atts['hidden'], true ) ) {
369 $class .= ' frm_hidden';
370 }
371
372 return 'class="' . esc_attr( $class ) . '"';
373 }
374
375 /**
376 * Get the checkbox for bulk actions.
377 * This is only required when the Payments submodule or PayPal is active.
378 *
379 * @since 6.27
380 *
381 * @param object $item
382 *
383 * @return string
384 */
385 private function get_cb_column( $item ) {
386 return '<input type="checkbox" name="item-action[]" value="' . esc_attr( $item->id ) . '" />';
387 }
388
389 /**
390 * @param object $item
391 *
392 * @return string
393 */
394 private function get_receipt_id_column( $item ) {
395 return $this->get_action_column( $item, 'receipt_id' );
396 }
397
398 /**
399 * @param object $item
400 * @param string $field
401 *
402 * @return string
403 */
404 private function get_action_column( $item, $field ) {
405 $link = $this->get_view_payment_link( $item, $field );
406 return '<strong>' . $link . '</strong><br />' . $this->row_actions( $this->get_row_actions( $item ) );
407 }
408
409 /**
410 * @since 6.27
411 *
412 * @param object $item
413 * @param string $field
414 *
415 * @return string
416 */
417 private function get_view_payment_link( $item, $field ) {
418 $link_params = array(
419 'class' => 'rot-title',
420 'href' => esc_url( $this->get_url_to_payment( $item->id, 'show' ) ),
421 'title' => __( 'View', 'formidable' ),
422 );
423 return '<a ' . FrmAppHelper::array_to_html_params( $link_params ) . '>' . esc_html( $item->{ $field } ) . '</a>';
424 }
425
426 /**
427 * @param int|string $payment_id
428 * @param string $action Supports 'show' and 'edit'.
429 *
430 * @return string
431 */
432 private function get_url_to_payment( $payment_id, $action = 'show' ) {
433 return add_query_arg(
434 array(
435 'action' => $action,
436 'id' => $payment_id,
437 'type' => $this->table,
438 'page' => FrmAppHelper::simple_get( 'page' ),
439 ),
440 admin_url( 'admin.php' )
441 );
442 }
443
444 /**
445 * @param object $item
446 *
447 * @return array
448 */
449 private function get_row_actions( $item ) {
450 $base_link = '?page=formidable-payments&action=';
451 $view_link = $base_link . 'show&id=' . $item->id . '&type=' . $this->table;
452 $delete_link = $base_link . 'destroy&id=' . $item->id . '&type=' . $this->table;
453
454 $actions = array();
455 $actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . esc_html__( 'View', 'formidable' ) . '</a>';
456
457 if ( $this->supports_edit_link() ) {
458 $edit_link = $base_link . 'edit&id=' . $item->id;
459 $actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . esc_html__( 'Edit', 'formidable' ) . '</a>';
460 }
461
462 $actions['delete'] = '<a href="' . esc_url( wp_nonce_url( $delete_link ) ) . '" data-frmverify="' . esc_attr__( 'Permanently delete this payment?', 'formidable' ) . '" data-frmverify-btn="frm-button-red">' . esc_html__( 'Delete', 'formidable' ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
463
464 return $actions;
465 }
466
467 /**
468 * @since 6.27
469 *
470 * @return bool
471 */
472 private function supports_edit_link() {
473 if ( $this->table === 'subscriptions' ) {
474 return false;
475 }
476
477 return class_exists( 'FrmTransAppController', false ) || class_exists( 'FrmPaymentsController', false );
478 }
479
480 /**
481 * Get the column value for displaying an entry ID.
482 *
483 * @param object $item A payment or subscription object.
484 *
485 * @return string
486 */
487 private function get_item_id_column( $item ) {
488 $entry_id = (int) $item->item_id;
489 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
490 $entry_is_deleted = ! in_array( $entry_id, $this->valid_entry_ids );
491
492 if ( $entry_is_deleted ) {
493 // translators: %d: ID of the deleted entry.
494 return sprintf( __( '%d (Deleted)', 'formidable' ), $entry_id );
495 }
496
497 return '<a href="' . esc_url( '?page=formidable-entries&frm_action=show&action=show&id=' . $entry_id ) . '">' . absint( $entry_id ) . '</a>';
498 }
499
500 /**
501 * @param object $item
502 * @param array $atts
503 *
504 * @return mixed
505 */
506 private function get_form_id_column( $item, $atts ) {
507 if ( isset( $atts['form_ids'][ $item->item_id ] ) ) {
508 return FrmFormsHelper::edit_form_link( $atts['form_ids'][ $item->item_id ]->form_id );
509 }
510
511 return '';
512 }
513
514 /**
515 * @param object $item
516 *
517 * @return string
518 */
519 private function get_user_id_column( $item ) {
520 $val = FrmDb::get_var( 'frm_items', array( 'id' => $item->item_id ), 'user_id' );
521 return FrmTransLiteAppHelper::get_user_link( $val );
522 }
523
524 /**
525 * @param object $item
526 * @param array $atts
527 *
528 * @return string
529 */
530 private function get_created_at_column( $item, $atts ) {
531 if ( empty( $item->created_at ) || $item->created_at === '0000-00-00 00:00:00' ) {
532 return '';
533 }
534
535 $date = FrmAppHelper::get_localized_date( $atts['date_format'], $item->created_at );
536 $date_title = FrmAppHelper::get_localized_date( $atts['date_format'] . ' g:i:s A', $item->created_at );
537 return '<abbr title="' . esc_attr( $date_title ) . '">' . $date . '</abbr>';
538 }
539
540 /**
541 * @param object $item
542 *
543 * @return string
544 */
545 private function get_amount_column( $item ) {
546 if ( $this->table === 'subscriptions' ) {
547 return FrmTransLiteAppHelper::format_billing_cycle( $item );
548 }
549 return FrmTransLiteAppHelper::formatted_amount( $item );
550 }
551
552 /**
553 * @param object $item
554 *
555 * @return string
556 */
557 private function get_end_count_column( $item ) {
558 $limit = $item->end_count >= 9999 ? __( 'unlimited', 'formidable' ) : $item->end_count;
559 $frm_payment = new FrmTransLitePayment();
560 $completed_payments = $frm_payment->get_all_by( $item->id, 'sub_id' );
561 $count = FrmTransLiteAppHelper::count_completed_payments( $completed_payments );
562
563 // translators: %1$s: Count, %2$s: Limit.
564 return sprintf( __( '%1$s of %2$s', 'formidable' ), $count, $limit );
565 }
566
567 /**
568 * @param object $item
569 *
570 * @return string
571 */
572 private function get_status_column( $item ) {
573 $status = esc_html( FrmTransLiteAppHelper::show_status( FrmTransLiteAppHelper::get_payment_status( $item ) ) );
574
575 if ( 'processing' === ( $item->status ?? '' ) ) {
576 $status .= $this->get_processing_tooltip();
577 }
578
579 return $status;
580 }
581
582 /**
583 * @since 6.5, introduced in v2.04 of the Payments submodule.
584 *
585 * @return string
586 */
587 private function get_processing_tooltip() {
588 return FrmAppHelper::clip(
589 function () {
590 FrmAppHelper::tooltip_icon( __( 'This payment method may take between 4-5 business days to process.', 'formidable' ) );
591 }
592 );
593 }
594
595 /**
596 * @param object $item
597 *
598 * @return string
599 */
600 private function get_sub_id_column( $item ) {
601 if ( empty( $item->sub_id ) ) {
602 return '';
603 }
604
605 if ( $this->table === 'subscriptions' ) {
606 return $this->get_action_column( $item, 'sub_id' );
607 }
608
609 return '<a href="' . esc_url( '?page=formidable-payments&action=show&type=subscriptions&id=' . $item->sub_id ) . '">' . $item->sub_id . '</a>';
610 }
611
612 /**
613 * Get the string for the "Processor" column.
614 * Convert the supported pay systems to their proper case.
615 *
616 * @param object $item
617 * @param array $atts
618 *
619 * @return string
620 */
621 private function get_paysys_column( $item, $atts ) {
622 if ( isset( $atts['gateways'][ $item->paysys ] ) ) {
623 return $atts['gateways'][ $item->paysys ]['label'];
624 }
625
626 return $item->paysys;
627 }
628
629 /**
630 * Display 'Test' or 'Live' in a mode column if the value is known.
631 * Old payment entries will have a NULL 'test' column value.
632 *
633 * @since 6.6
634 *
635 * @param stdClass $item Payment or Subscription object.
636 *
637 * @return string
638 */
639 private function get_mode_column( $item ) {
640 return esc_html( FrmTransLiteAppHelper::get_test_mode_display_string( $item ) );
641 }
642
643 /**
644 * Render the tabs for the payments list, if the user has access to coupons.
645 *
646 * @since 6.27
647 *
648 * @param string $active_tab
649 *
650 * @return void
651 */
652 public static function render_tabs( $active_tab = 'payments' ) {
653 if ( ! current_user_can( 'frm_change_settings' ) ) {
654 // Coupons are only available to people who can access global settings.
655 return;
656 }
657
658 include FrmTransLiteAppHelper::plugin_path() . '/views/lists/tabs.php';
659 }
660 }
661