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

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