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

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