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

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