PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.27
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.27
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 / FrmFormsListHelper.php

FrmFormsListHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.27, at classes/helpers/FrmFormsListHelper.php

501 lines 13.9 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 FrmFormsListHelper extends FrmListHelper {
7
8 /**
9 * @var string
10 */
11 public $status = '';
12
13 public $total_items = 0;
14
15 public function __construct( $args ) {
16 $this->status = self::get_param( array( 'param' => 'form_type' ) );
17
18 parent::__construct( $args );
19 $this->screen->set_screen_reader_content(
20 array(
21 'heading_list' => esc_html__( 'Forms list', 'formidable' ),
22 )
23 );
24 }
25
26 /**
27 * @return void
28 */
29 public function prepare_items() {
30 global $per_page, $mode;
31
32 $page = $this->get_pagenum();
33 $per_page = $this->get_items_per_page( 'formidable_page_formidable_per_page' );
34
35 $mode = self::get_param(
36 array(
37 'param' => 'mode',
38 'default' => 'list',
39 )
40 );
41 $orderby = self::get_param(
42 array(
43 'param' => 'orderby',
44 'default' => 'name',
45 )
46 );
47 $order = self::get_param(
48 array(
49 'param' => 'order',
50 'default' => 'ASC',
51 )
52 );
53
54 FrmAppController::apply_saved_sort_preference( $orderby, $order );
55
56 $start = self::get_param(
57 array(
58 'param' => 'start',
59 'default' => ( $page - 1 ) * $per_page,
60 )
61 );
62
63 $s_query = array(
64 array(
65 'or' => 1,
66 'parent_form_id' => null,
67 'parent_form_id <' => 1,
68 ),
69 );
70 switch ( $this->status ) {
71 case 'draft':
72 $s_query['is_template'] = 0;
73 $s_query['status'] = 'draft';
74 break;
75 case 'trash':
76 $s_query['status'] = 'trash';
77 break;
78 default:
79 $s_query['is_template'] = 0;
80 $s_query['status !'] = 'trash';
81 break;
82 }
83
84 $s = self::get_param(
85 array(
86 'param' => 's',
87 'sanitize' => 'sanitize_text_field',
88 )
89 );
90
91 if ( $s !== '' ) {
92 preg_match_all( '/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches );
93 $search_terms = array_map( 'trim', $matches[0] );
94
95 foreach ( $search_terms as $term ) {
96 $s_query[] = array(
97 'or' => true,
98 'name LIKE' => $term,
99 'description LIKE' => $term,
100 'created_at LIKE' => $term,
101 'form_key LIKE' => $term,
102 'id' => $term,
103 );
104 unset( $term );
105 }
106 }
107
108 $this->items = FrmForm::getAll( $s_query, $orderby . ' ' . $order, $start . ',' . $per_page );
109 $total_items = FrmDb::get_count( 'frm_forms', $s_query );
110 $this->total_items = $total_items;
111
112 $this->set_pagination_args(
113 array(
114 'total_items' => $total_items,
115 'per_page' => $per_page,
116 )
117 );
118 }
119
120 /**
121 * @return void
122 */
123 public function no_items() {
124 if ( $this->status === 'trash' ) {
125 echo '<p>';
126 esc_html_e( 'No forms found in the trash.', 'formidable' );
127 ?>
128 <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable' ) ); ?>">
129 <?php esc_html_e( 'See all forms.', 'formidable' ); ?>
130 </a>
131 <?php
132 echo '</p>';
133 } else {
134 $title = __( 'No Forms Found', 'formidable' );
135 include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/_no_forms.php';
136 }
137 }
138
139 public function get_bulk_actions() {
140 $actions = array();
141
142 if ( 'trash' === $this->status ) {
143 if ( current_user_can( 'frm_edit_forms' ) ) {
144 $actions['bulk_untrash'] = __( 'Restore', 'formidable' );
145 }
146
147 if ( current_user_can( 'frm_delete_forms' ) ) {
148 $actions['bulk_delete'] = __( 'Delete Permanently', 'formidable' );
149 }
150 } elseif ( EMPTY_TRASH_DAYS && current_user_can( 'frm_delete_forms' ) ) {
151 $actions['bulk_trash'] = __( 'Move to Trash', 'formidable' );
152 } elseif ( current_user_can( 'frm_delete_forms' ) ) {
153 $actions['bulk_delete'] = __( 'Delete', 'formidable' );
154 }
155
156 return $actions;
157 }
158
159 /**
160 * @param string $which
161 *
162 * @return void
163 */
164 public function extra_tablenav( $which ) {
165 if ( 'top' !== $which ) {
166 return;
167 }
168
169 if ( 'trash' === $this->status && current_user_can( 'frm_delete_forms' ) ) {
170 ?>
171 <div class="alignleft actions frm_visible_overflow">
172 <?php submit_button( __( 'Empty Trash', 'formidable' ), 'apply', 'delete_all', false ); ?>
173 </div>
174 <?php
175 }
176 }
177
178 /**
179 * @return array
180 */
181 public function get_views() {
182 $statuses = array(
183 'published' => __( 'My Forms', 'formidable' ),
184 'draft' => __( 'Drafts', 'formidable' ),
185 'trash' => __( 'Trash', 'formidable' ),
186 );
187
188 $links = array();
189 $counts = FrmForm::get_count();
190 $form_type = FrmAppHelper::simple_get( 'form_type', 'sanitize_title', 'published' );
191
192 if ( isset( $statuses[ $form_type ] ) ) {
193 $counts->$form_type = $this->total_items;
194 }
195
196 $form_type = self::get_param(
197 array(
198 'param' => 'form_type',
199 'default' => 'published',
200 )
201 );
202
203 foreach ( $statuses as $status => $name ) {
204 $class = $status == $form_type ? ' class="current"' : ''; // phpcs:ignore Universal.Operators.StrictComparisons
205
206 if ( $counts->{$status} || 'draft' !== $status ) {
207 /* translators: %1$s: Status, %2$s: Number of items */
208 $links[ $status ] = '<a href="' . esc_url( '?page=formidable&form_type=' . $status ) . '" ' . $class . '>' . sprintf( __( '%1$s <span class="count">(%2$s)</span>', 'formidable' ), $name, number_format_i18n( $counts->{$status} ) ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
209 }
210
211 unset( $status, $name );
212 }
213
214 return $links;
215 }
216
217 /**
218 * @param string $which
219 *
220 * @return void
221 */
222 public function pagination( $which ) {
223 global $mode;
224
225 parent::pagination( $which );
226
227 if ( 'top' === $which ) {
228 $this->view_switcher( $mode );
229 }
230 }
231
232 /**
233 * @param stdClass $item
234 * @param string $style
235 *
236 * @return string
237 */
238 public function single_row( $item, $style = '' ) {
239 global $mode;
240
241 // Set up the hover actions for this user
242 $actions = array();
243 $edit_link = FrmForm::get_edit_link( $item->id );
244
245 $this->get_actions( $actions, $item, $edit_link );
246
247 $action_links = $this->row_actions( $actions );
248
249 // Set up the checkbox ( because the user is editable, otherwise its empty )
250 $checkbox = '<input type="checkbox" name="item-action[]" id="cb-item-action-' . absint( $item->id ) . '" value="' . esc_attr( $item->id ) . '" />';
251 $checkbox_label_text = sprintf(
252 // translators: Form title
253 __( 'Select %s', 'formidable' ),
254 ! empty( $item->name ) ? $item->name : FrmFormsHelper::get_no_title_text()
255 );
256
257 $checkbox .= '<label for="cb-item-action-' . absint( $item->id ) . '"><span class="screen-reader-text">' . esc_html( $checkbox_label_text ) . '</span></label>';
258 $r = '<tr id="item-action-' . absint( $item->id ) . '"' . $style . '>';
259
260 list( $columns, $hidden ) = $this->get_column_info();
261
262 $format = 'Y/m/d';
263
264 if ( 'list' !== $mode ) {
265 $format .= ' \<\b\r \/\> g:i:s a';
266 }
267
268 foreach ( $columns as $column_name => $column_display_name ) {
269 $class = $column_name . ' column-' . $column_name . ( 'name' === $column_name ? ' post-title page-title column-title' : '' );
270 $style = '';
271
272 if ( in_array( $column_name, $hidden, true ) ) {
273 $class .= ' frm_hidden';
274 }
275
276 if ( $column_name === 'name' ) {
277 $class .= ' column-primary';
278 }
279
280 $class = 'class="' . esc_attr( $class ) . '"';
281 $data_colname = ' data-colname="' . esc_attr( $column_display_name ) . '"';
282 $attributes = $class . $style . $data_colname;
283
284 switch ( $column_name ) {
285 case 'cb':
286 $r .= '<th scope="row" class="check-column">' . $checkbox . '</th>';
287 break;
288 case 'id':
289 case 'form_key':
290 $val = $item->{$column_name};
291 break;
292 case 'name':
293 $val = $this->get_form_name( $item, $actions, $edit_link, $mode );
294 $val .= $action_links;
295 break;
296 case 'created_at':
297 $date = gmdate( $format, strtotime( $item->created_at ) );
298 $val = '<abbr title="' . esc_attr( gmdate( 'Y/m/d g:i:s A', strtotime( $item->created_at ) ) ) . '">' . $date . '</abbr>';
299 break;
300 case 'entries':
301 if ( ! empty( $item->options['no_save'] ) ) {
302 $val = FrmAppHelper::icon_by_class(
303 'frmfont frm_forbid_icon frm_bstooltip',
304 array(
305 'title' => __( 'Saving entries is disabled for this form', 'formidable' ),
306 'echo' => false,
307 )
308 );
309 } else {
310 $text = FrmEntry::getRecordCount( $item->id );
311 $val = current_user_can( 'frm_view_entries' ) ? '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-entries&form=' . $item->id ) ) . '">' . $text . '</a>' : $text; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
312 unset( $text );
313 }
314 break;
315 default:
316 if ( method_exists( $this, 'column_' . $column_name ) ) {
317 $val = call_user_func( array( $this, 'column_' . $column_name ), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
318 }
319 break;
320 }//end switch
321
322 if ( isset( $val ) ) {
323 $r .= "<td $attributes>";
324 $r .= $val;
325 $r .= '</td>';
326 }
327 unset( $val );
328 }//end foreach
329 return $r . '</tr>';
330 }
331
332 /**
333 * Get the HTML for the Actions column in the form list.
334 * This includes multiple icons for triggering the embed modal, the visual styler, and an active landing page.
335 *
336 * @since 6.0
337 *
338 * @param stdClass $form
339 *
340 * @return string
341 */
342 protected function column_shortcode( $form ) {
343 $val = '<a href="#" class="frm-embed-form" role="button" aria-label="' . esc_attr__( 'Embed Form', 'formidable' ) . '">' . FrmAppHelper::icon_by_class( 'frmfont frm_code_icon', array( 'echo' => false ) ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
344 $val .= $this->column_style( $form );
345 $val .= $this->column_views( $form );
346 $val = apply_filters( 'frm_form_list_actions', $val, array( 'form' => $form ) );
347 // Remove the space hard coded in Landing pages.
348 $val = str_replace( '&nbsp;', '', $val );
349 return '<div>' . $val . '</div>';
350 }
351
352 /**
353 * Get the HTML for the Style column in the form list.
354 *
355 * @since 6.0
356 *
357 * @param stdClass $form
358 *
359 * @return string
360 */
361 protected function column_style( $form ) {
362 $style_setting = $form->options['custom_style'] ?? '';
363 $frm_settings = FrmAppHelper::get_settings();
364
365 if ( $style_setting === '0' || 'none' === $frm_settings->load_style ) {
366 // Don't show a link if styling is off.
367 return '';
368 }
369
370 $style = FrmStylesController::get_form_style( $form );
371
372 if ( ! $style ) {
373 // Do a second pass to avoid null values.
374 $frm_style = new FrmStyle( 'default' );
375 $style = $frm_style->get_one();
376 }
377
378 $href = FrmStylesHelper::get_edit_url( $style, $form->id );
379 return '<a href="' . esc_url( $href ) . '" title="' . esc_attr( $style->post_title ) . '">' . FrmAppHelper::icon_by_class( 'frmfont frm_pallet_icon', array( 'echo' => false ) ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
380 }
381
382 /**
383 * Generate the HTML for the form Views page.
384 *
385 * @since 6.19
386 *
387 * @param stdClass $form Form object.
388 *
389 * @return string
390 */
391 protected function column_views( $form ) {
392 $attributes = array(
393 'href' => admin_url( 'admin.php?page=formidable-views&form=' . absint( $form->id ) . '&show_nav=1' ),
394 'title' => __( 'Link to list of all views for this form.', 'formidable' ),
395 'target' => '_blank',
396 );
397
398 return '<a ' . FrmAppHelper::array_to_html_params( $attributes ) . '>
399 ' . FrmAppHelper::icon_by_class( 'frmfont frm_eye_icon', array( 'echo' => false ) ) .
400 '</a>';
401 }
402
403 /**
404 * @param array $actions
405 * @param object $item
406 * @param string $edit_link
407 *
408 * @return void
409 */
410 private function get_actions( &$actions, $item, $edit_link ) {
411 $new_actions = FrmFormsHelper::get_action_links( $item->id, $item );
412
413 foreach ( $new_actions as $link => $action ) {
414 $new_actions[ $link ] = FrmFormsHelper::format_link_html( $action, 'short' );
415 }
416
417 if ( 'trash' === $this->status ) {
418 $actions = $new_actions;
419 return;
420 }
421
422 if ( current_user_can( 'frm_edit_forms' ) ) {
423 $actions['frm_edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'formidable' ) . '</a>';
424 $actions['frm_settings'] = '<a href="' . esc_url( '?page=formidable&frm_action=settings&id=' . $item->id ) . '">' . __( 'Settings', 'formidable' ) . '</a>';
425 }
426
427 $actions = array_merge( $actions, $new_actions );
428 $actions['view'] = '<a href="' . esc_url( FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" target="_blank">' . __( 'Preview', 'formidable' ) . '</a>';
429 }
430
431 /**
432 * @param object $item
433 * @param array $actions
434 * @param string $edit_link
435 * @param string $mode
436 *
437 * @return string
438 */
439 private function get_form_name( $item, $actions, $edit_link, $mode = 'list' ) {
440 $form_name = $item->name;
441
442 if ( is_null( $form_name ) || trim( $form_name ) === '' ) {
443 $form_name = FrmFormsHelper::get_no_title_text();
444 }
445
446 $form_name = FrmAppHelper::kses( $form_name );
447
448 if ( 'excerpt' !== $mode ) {
449 $form_name = FrmAppHelper::truncate( $form_name, 50 );
450 }
451
452 $val = '<strong>';
453
454 if ( 'trash' === $this->status ) {
455 $val .= $form_name;
456 } else {
457 $val .= '<a href="' . esc_url( isset( $actions['frm_edit'] ) ? $edit_link : FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" class="row-title">' . FrmAppHelper::kses( $form_name ) . '</a> '; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
458 }
459
460 $this->add_draft_label( $item, $val );
461 $val .= '</strong>';
462
463 $this->add_form_description( $item, $val );
464
465 return $val;
466 }
467
468 /**
469 * @param object $item
470 * @param string $val
471 *
472 * @return void
473 */
474 private function add_draft_label( $item, &$val ) {
475 if ( 'draft' === $item->status && 'draft' !== $this->status ) {
476 $val .= ' - <span class="post-state">' . esc_html__( 'Draft', 'formidable' ) . '</span>';
477 }
478 }
479
480 /**
481 * @param object $item
482 * @param string $val
483 *
484 * @return void
485 */
486 private function add_form_description( $item, &$val ) {
487 global $mode;
488
489 if ( 'excerpt' === $mode && ! is_null( $item->description ) ) {
490 $val .= FrmAppHelper::truncate( strip_tags( $item->description ), 50 );
491 }
492 }
493
494 /**
495 * @return string
496 */
497 protected function confirm_bulk_delete() {
498 return __( 'ALL selected forms and their entries will be permanently deleted. Want to proceed?', 'formidable' );
499 }
500 }
501