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

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

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