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

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