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

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