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

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