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

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