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