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

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