PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.28
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.28
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 / classes / helpers / FrmEntriesListHelper.php

FrmEntriesListHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.28, at classes/helpers/FrmEntriesListHelper.php

580 lines 14.5 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 FrmEntriesListHelper extends FrmListHelper {
7
8 /**
9 * @var string|null
10 */
11 protected $column_name;
12
13 /**
14 * @since 4.07
15 *
16 * @var bool|int
17 */
18 public $total_items = 0;
19
20 /**
21 * @param array $args
22 */
23 public function __construct( $args ) {
24 parent::__construct( $args );
25 $this->screen->set_screen_reader_content(
26 array(
27 'heading_list' => esc_html__( 'Entries list', 'formidable' ),
28 )
29 );
30 }
31
32 /**
33 * @return void
34 */
35 public function prepare_items() {
36 $this->set_per_page();
37 $s_query = array();
38 $join_form_in_query = false;
39
40 $this->items = $this->get_entry_items( $s_query, $join_form_in_query );
41 $this->set_total_items( $s_query );
42 $this->prepare_pagination();
43 }
44
45 /**
46 * @param array $s_query
47 *
48 * @return void
49 */
50 protected function set_total_items( $s_query ) {
51 $this->total_items = FrmEntry::getRecordCount( $s_query );
52 }
53
54 /**
55 * Prepares pagination.
56 *
57 * @since 6.5.4
58 *
59 * @return void
60 */
61 protected function prepare_pagination() {
62 global $per_page;
63
64 $this->set_pagination_args(
65 array(
66 'total_items' => $this->total_items,
67 'per_page' => $per_page,
68 )
69 );
70 }
71
72 /**
73 * Sets the global $per_page variable
74 *
75 * @since 6.5.4
76 *
77 * @return void
78 */
79 protected function set_per_page() {
80 global $per_page;
81 $per_page = $this->get_items_per_page( 'formidable_page_formidable_entries_per_page' );
82 }
83
84 /**
85 * @since 6.5.4
86 *
87 * @param array $s_query
88 * @param bool $join_form_in_query
89 *
90 * @return array
91 */
92 protected function get_entry_items( &$s_query, &$join_form_in_query ) {
93 global $per_page;
94 $s_query = $this->get_search_query( $join_form_in_query );
95 $order = $this->get_order_by();
96 $limit = $this->get_limit( $per_page );
97
98 return FrmEntry::getAll( $s_query, $order, $limit, true, $join_form_in_query );
99 }
100
101 /**
102 * @since 6.5.4
103 *
104 * @return string
105 */
106 protected function get_order_by() {
107 $orderby = self::get_param(
108 array(
109 'param' => 'orderby',
110 'default' => 'id',
111 )
112 );
113 $order = self::get_param(
114 array(
115 'param' => 'order',
116 'default' => 'DESC',
117 )
118 );
119
120 FrmAppController::apply_saved_sort_preference( $orderby, $order );
121
122 if ( str_contains( $orderby, 'meta' ) ) {
123 $order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) );
124
125 if ( in_array( $order_field_type, array( 'number', 'scale', 'star' ), true ) ) {
126 $orderby .= '+0';
127 }
128 }
129
130 return FrmDb::esc_order( $orderby . ' ' . $order );
131 }
132
133 /**
134 * @since 6.5.4
135 *
136 * @param int $per_page
137 *
138 * @return string
139 */
140 protected function get_limit( $per_page ) {
141 $page = $this->get_pagenum();
142 $start = (int) self::get_param(
143 array(
144 'param' => 'start',
145 'default' => ( $page - 1 ) * $per_page,
146 )
147 );
148
149 return FrmDb::esc_limit( $start . ',' . $per_page );
150 }
151
152 /**
153 * @since 6.5.4
154 *
155 * @param bool $join_form_in_query
156 *
157 * @return array
158 */
159 protected function get_search_query( &$join_form_in_query ) {
160 $form_id = $this->params['form'];
161 $s_query = array();
162
163 if ( $form_id ) {
164 $form_ids = $this->get_form_ids( $form_id );
165 $s_query['it.form_id'] = count( $form_ids ) > 1 ? $form_ids : $form_ids[0];
166 } else {
167 $s_query[] = array(
168 'or' => 1,
169 'parent_form_id' => null,
170 'parent_form_id <' => 1,
171 );
172 $join_form_in_query = true;
173 }
174
175 $s = self::get_param(
176 array(
177 'param' => 's',
178 'sanitize' => 'sanitize_text_field',
179 )
180 );
181
182 if ( $s !== '' && FrmAppHelper::pro_is_installed() ) {
183 $fid = self::get_param( array( 'param' => 'fid' ) );
184 $s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid );
185 }
186
187 return apply_filters( 'frm_entries_list_query', $s_query, compact( 'form_id' ) );
188 }
189
190 /**
191 * @since 6.5.4
192 *
193 * @param int|string $form_id
194 *
195 * @return array<int>
196 */
197 protected function get_form_ids( $form_id ) {
198 return array( (int) $form_id );
199 }
200
201 /**
202 * @return void
203 */
204 public function no_items() {
205 $s = self::get_param(
206 array(
207 'param' => 's',
208 'sanitize' => 'sanitize_text_field',
209 )
210 );
211
212 if ( $s ) {
213 esc_html_e( 'No Entries Found', 'formidable' );
214 return;
215 }
216
217 $form_id = $this->params['form'];
218 $form = $this->params['form'];
219
220 if ( $form_id ) {
221 $form = FrmForm::getOne( $form_id );
222 }
223
224 $has_form = ! empty( $form );
225
226 if ( ! $has_form ) {
227 $has_form = FrmForm::getAll( array(), '', 1 );
228 $has_form = ! empty( $has_form );
229 }
230
231 $colspan = $this->get_column_count();
232
233 include FrmAppHelper::plugin_path() . '/classes/views/frm-entries/no_entries.php';
234 }
235
236 /**
237 * @param string $text Text.
238 * @param string $input_id Input ID.
239 *
240 * @return void
241 */
242 public function search_box( $text, $input_id ) {
243 // Searching is a pro feature
244 }
245
246 /**
247 * @param string $which
248 *
249 * @return void
250 */
251 protected function display_tablenav( $which ) {
252 $is_footer = $which !== 'top';
253
254 if ( $is_footer && ! empty( $this->items ) ) {
255 $utm = array(
256 'campaign' => 'spam-protection',
257 'content' => 'entries-list-spam-protection',
258 );
259 // phpcs:disable Generic.WhiteSpace.ScopeIndent
260 ?>
261 <p>
262 <?php esc_html_e( 'Getting spam form submissions?', 'formidable' ); ?>
263 <a href="<?php echo esc_url( FrmAppHelper::admin_upgrade_link( $utm, 'knowledgebase/add-spam-protection/' ) ); ?>" target="_blank">
264 <?php esc_html_e( 'Learn how to prevent them.', 'formidable' ); ?>
265 </a>
266 </p>
267 <?php
268 // phpcs:enable Generic.WhiteSpace.ScopeIndent
269 }
270 parent::display_tablenav( $which );
271 }
272
273 /**
274 * @param string $which
275 *
276 * @return void
277 */
278 protected function extra_tablenav( $which ) {
279 $form_id = FrmAppHelper::simple_get( 'form', 'absint' );
280
281 if ( $which !== 'top' || $form_id ) {
282 return;
283 }
284
285 echo '<div class="alignleft actions">';
286
287 // Override the referrer to prevent it from being used for the screen options.
288 echo '<input type="hidden" name="_wp_http_referer" value="" />';
289
290 echo '<label for="form" class="screen-reader-text">' . esc_html__( 'Filter by form', 'formidable' ) . '</label>';
291
292 FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) );
293 submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) );
294 echo '</div>';
295 }
296
297 /**
298 * Gets the name of the primary column in the Entries screen
299 *
300 * @since 2.0.14
301 *
302 * @return string Primary column.
303 */
304 protected function get_primary_column_name() {
305 $columns = get_column_headers( $this->screen );
306 $hidden = get_hidden_columns( $this->screen );
307 $primary_column = '';
308
309 foreach ( $columns as $column_key => $column_display_name ) {
310 if ( 'cb' !== $column_key && ! in_array( $column_key, $hidden, true ) ) {
311 $primary_column = $column_key;
312 break;
313 }
314 }
315
316 return $primary_column;
317 }
318
319 /**
320 * @since 6.12
321 *
322 * @param object $item
323 *
324 * @return string
325 */
326 private static function get_entry_label( $item ) {
327 if ( $item->name ) {
328 return $item->name;
329 }
330 /* translators: %d: Entry id */
331 return sprintf( __( 'Entry %d', 'formidable' ), $item->id );
332 }
333
334 /**
335 * @param object $item
336 * @param string $style
337 *
338 * @return string
339 */
340 public function single_row( $item, $style = '' ) {
341 // Set up the hover actions for this user
342 $actions = array();
343 $view_link = '?page=formidable-entries&frm_action=show&id=' . $item->id;
344
345 $this->get_actions( $actions, $item, $view_link );
346
347 $action_links = $this->row_actions( $actions );
348
349 // Set up the checkbox ( because the user is editable, otherwise its empty )
350 $checkbox = "<input type='checkbox' name='item-action[]' id='cb-item-action-{$item->id}' value='{$item->id}' />";
351 /* translators: %s: Form name */
352 $checkbox .= "<label for='cb-item-action-{$item->id}'><span class='screen-reader-text'>" . esc_html( sprintf( __( 'Select %s', 'formidable' ), self::get_entry_label( $item ) ) ) . '</span></label>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
353
354 $r = "<tr id='item-action-{$item->id}'$style>";
355
356 list( $columns, $hidden, , $primary ) = $this->get_column_info();
357 $action_col = false;
358 $action_columns = $this->get_action_columns();
359
360 foreach ( $columns as $column_name => $column_display_name ) {
361 $class = $column_name . ' column-' . $column_name;
362
363 if ( $column_name === $primary ) {
364 $class .= ' column-primary';
365 }
366
367 if ( in_array( $column_name, $hidden, true ) ) {
368 $class .= ' frm_hidden';
369 } elseif ( ! $action_col && ! in_array( $column_name, $action_columns, true ) ) {
370 $action_col = $column_name;
371 }
372
373 $attributes = 'class="' . esc_attr( $class ) . '"';
374 unset( $class );
375 $attributes .= ' data-colname="' . $column_display_name . '"';
376 $form_id = $this->params['form'] ? $this->params['form'] : 0;
377 $this->column_name = preg_replace( '/^(' . $form_id . '_)/', '', $column_name );
378
379 if ( $this->column_name === 'cb' ) {
380 $r .= "<th scope='row' class='check-column'>$checkbox</th>";
381 } else {
382 $val = in_array( $column_name, $hidden, true ) ? '' : $this->column_value( $item );
383 $r .= "<td $attributes>";
384
385 // phpcs:ignore Universal.Operators.StrictComparisons
386 if ( $column_name == $action_col ) {
387 $edit_link = admin_url( 'admin.php?page=formidable-entries&frm_action=edit&id=' . $item->id );
388 $r .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> ';
389 $r .= $action_links;
390 } else {
391 $r .= $val;
392 }
393
394 $r .= '</td>';
395 }//end if
396 unset( $val );
397 }//end foreach
398 return $r . '</tr>';
399 }
400
401 /**
402 * Get the column names that the logged in user can action on
403 *
404 * @return string[]
405 */
406 private function get_action_columns() {
407 return array( 'cb', 'form_id', 'id', 'post_id' );
408 }
409
410 /**
411 * @param object $item
412 *
413 * @return mixed
414 */
415 private function column_value( $item ) {
416 $col_name = $this->maybe_fix_column_name( $this->column_name );
417
418 switch ( $col_name ) {
419 case 'ip':
420 case 'id':
421 case 'item_key':
422 $val = $item->{$col_name};
423 break;
424 case 'name':
425 $val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 );
426 break;
427 case 'created_at':
428 case 'updated_at':
429 $date = FrmAppHelper::get_formatted_time( $item->{$col_name} );
430 $val = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
431 break;
432 case 'is_draft':
433 $entry_status = FrmEntriesHelper::get_entry_status_label( $item->is_draft );
434 $val = sprintf(
435 '<span class="frm-meta-tag frm-entry-status frm-entry-status-%s">%s</span>',
436 sanitize_html_class( $item->is_draft ),
437 esc_html( $entry_status )
438 );
439 break;
440 case 'form_id':
441 $form_id = $item->form_id;
442 $user_can_edit_forms = false === FrmAppHelper::permission_nonce_error( 'frm_edit_forms' );
443
444 if ( $user_can_edit_forms ) {
445 $val = FrmFormsHelper::edit_form_link( $form_id );
446 } else {
447 $val = FrmFormsHelper::edit_form_link_label( $form_id );
448 }
449 break;
450 case 'post_id':
451 $val = FrmAppHelper::post_edit_link( $item->post_id );
452 break;
453 case 'user_id':
454 $user = get_userdata( $item->user_id );
455 $val = $user ? $user->user_login : '';
456 break;
457 case 'parent_item_id':
458 $val = $item->parent_item_id;
459 break;
460 default:
461 $val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
462
463 if ( $val === false ) {
464 $this->get_column_value( $item, $val );
465 }
466
467 /**
468 * Allows changing entries list column value.
469 *
470 * @since 5.1
471 *
472 * @param mixed $val Column value.
473 * @param array $args Contains `item` and `col_name`.
474 */
475 $val = apply_filters( 'frm_entries_column_value', $val, compact( 'item', 'col_name' ) );
476 }//end switch
477
478 return $val;
479 }
480
481 /**
482 * When a form has entries with the 0 item meta value, the values do not appear properly in the entries list.
483 *
484 * @since 6.11.2
485 *
486 * @param string $column_name
487 *
488 * @return string
489 */
490 private function maybe_fix_column_name( $column_name ) {
491 if ( str_starts_with( $column_name, '0_' ) ) {
492 return substr( $column_name, 2 );
493 }
494 return $column_name;
495 }
496
497 /**
498 * @param array $actions
499 * @param object $item
500 * @param string $view_link
501 *
502 * @return void
503 */
504 private function get_actions( &$actions, $item, $view_link ) {
505 $actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . esc_html__( 'View', 'formidable' ) . '</a>';
506
507 if ( current_user_can( 'frm_delete_entries' ) ) {
508 $delete_link = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
509 $actions['delete'] = '<a href="' . esc_url( wp_nonce_url( $delete_link ) ) . '" class="submitdelete" data-frmverify="' . esc_attr__( 'Permanently delete this entry?', 'formidable' ) . '" data-frmverify-btn="frm-button-red">' . esc_html__( 'Delete', 'formidable' ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
510 }
511
512 $actions = apply_filters( 'frm_row_actions', $actions, $item );
513 }
514
515 /**
516 * @param object $item
517 * @param mixed $val
518 *
519 * @return void
520 */
521 private function get_column_value( $item, &$val ) {
522 $col_name = $this->column_name;
523
524 if ( str_starts_with( $col_name, 'frmsep_' ) ) {
525 $sep_val = true;
526 $col_name = str_replace( 'frmsep_', '', $col_name );
527 } else {
528 $sep_val = false;
529 }
530
531 if ( str_contains( $col_name, '-_-' ) ) {
532 list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
533 }
534
535 $field = FrmField::getOne( $col_name );
536
537 if ( ! $field ) {
538 return;
539 }
540
541 $atts = array(
542 'type' => $field->type,
543 'truncate' => true,
544 'post_id' => $item->post_id,
545 'entry_id' => $item->id,
546 'embedded_field_id' => 0,
547 );
548
549 if ( $sep_val ) {
550 $atts['saved_value'] = true;
551 }
552
553 if ( isset( $embedded_field_id ) ) {
554 $atts['embedded_field_id'] = $embedded_field_id;
555 unset( $embedded_field_id );
556 }
557
558 $val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
559 }
560
561 /**
562 * @return string
563 */
564 protected function confirm_bulk_delete() {
565 return sprintf(
566 /* translators: %1$s: HTML break line, %2$s: HTML bold text */
567 esc_html__( 'ALL entries in this form will be permanently deleted.%1$sWant to proceed? Type %2$s below.', 'formidable' ),
568 '<br/>',
569 '<b>DELETE ALL</b>'
570 );
571 }
572
573 /**
574 * @return string
575 */
576 protected function loaded_from() {
577 return 'entries-list';
578 }
579 }
580