builder
3 weeks ago
form-migrator
3 weeks ago
plugin-updates
8 years ago
settings
3 weeks ago
views
3 weeks ago
class-evf-admin-addons.php
4 months ago
class-evf-admin-assets.php
5 days ago
class-evf-admin-builder.php
2 months ago
class-evf-admin-dashboard.php
3 weeks ago
class-evf-admin-editor.php
4 years ago
class-evf-admin-embed-wizard.php
2 years ago
class-evf-admin-entries-table-list.php
2 months ago
class-evf-admin-entries.php
2 months ago
class-evf-admin-form-templates.php
3 weeks ago
class-evf-admin-forms-table-list.php
4 months ago
class-evf-admin-forms.php
4 months ago
class-evf-admin-import-export.php
3 weeks ago
class-evf-admin-menus.php
3 weeks ago
class-evf-admin-notices.php
4 months ago
class-evf-admin-preview-confirmation.php
11 months ago
class-evf-admin-settings.php
3 weeks ago
class-evf-admin-tools.php
2 months ago
class-evf-admin-welcome.php
2 years ago
class-evf-admin.php
2 months ago
class-evf-base-list-table.php
4 months ago
evf-admin-functions.php
3 weeks ago
class-evf-admin-forms-table-list.php
882 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Forms Table List |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @version 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | use EverestForms\Helpers\FormHelper; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | if ( ! class_exists( 'EVF_Base_List_Table' ) ) { |
| 14 | require_once __DIR__ . '/class-evf-base-list-table.php'; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Forms table list class. |
| 19 | */ |
| 20 | class EVF_Admin_Forms_Table_List extends EVF_Base_List_Table { |
| 21 | |
| 22 | /** |
| 23 | * Initialize the form table list. |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | parent::__construct( |
| 27 | array( |
| 28 | 'singular' => 'form', |
| 29 | 'plural' => 'forms', |
| 30 | 'ajax' => false, |
| 31 | ) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * No items found text. |
| 37 | */ |
| 38 | public function no_items() { |
| 39 | esc_html_e( 'No Forms found.', 'everest-forms' ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get list columns. |
| 44 | * |
| 45 | * @return array |
| 46 | */ |
| 47 | public function get_columns() { |
| 48 | $has_tags = ! empty( FormHelper::get_all_form_tags( 'term_id' ) ); |
| 49 | |
| 50 | $forms_columns = array( |
| 51 | 'cb' => '<input type="checkbox" />', |
| 52 | 'title' => esc_html__( 'Title', 'everest-forms' ), |
| 53 | ); |
| 54 | |
| 55 | if ( $has_tags ) { |
| 56 | $forms_columns['tags'] = esc_html__( 'Tags', 'everest-forms' ); |
| 57 | } |
| 58 | |
| 59 | $forms_columns['shortcode'] = esc_html__( 'Shortcode', 'everest-forms' ); |
| 60 | $forms_columns['enabled'] = esc_html__( 'Status', 'everest-forms' ); |
| 61 | $forms_columns['author'] = esc_html__( 'Author', 'everest-forms' ); |
| 62 | $forms_columns['date'] = esc_html__( 'Date', 'everest-forms' ); |
| 63 | |
| 64 | if ( isset( $_GET['status'] ) && 'trash' === $_GET['status'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 65 | unset( $forms_columns['enabled'] ); |
| 66 | } |
| 67 | |
| 68 | if ( current_user_can( 'everest_forms_view_entries' ) || current_user_can( 'everest_forms_view_others_entries' ) ) { |
| 69 | $forms_columns['entries'] = esc_html__( 'Entries', 'everest-forms' ); |
| 70 | } |
| 71 | |
| 72 | if ( isset( $_GET['status'] ) && 'trash' !== $_GET['status'] && ! current_user_can( 'everest_forms_delete_forms' ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 73 | unset( $forms_columns['cb'] ); |
| 74 | } |
| 75 | |
| 76 | return $forms_columns; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get a list of sortable columns. |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | protected function get_sortable_columns() { |
| 85 | return array( |
| 86 | 'title' => array( 'title', false ), |
| 87 | 'author' => array( 'author', false ), |
| 88 | 'date' => array( 'date', false ), |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Column cb. |
| 94 | * |
| 95 | * @param object $form Form object. |
| 96 | * @return string |
| 97 | */ |
| 98 | public function column_cb( $form ) { |
| 99 | $show = current_user_can( 'everest_forms_edit_form', $form->ID ); |
| 100 | $delete = current_user_can( 'everest_forms_delete_form', $form->ID ); |
| 101 | |
| 102 | /** |
| 103 | * Filters whether to show the bulk edit checkbox for a form in its list table. |
| 104 | * |
| 105 | * By default the checkbox is only shown if the current user can edit the form. |
| 106 | * |
| 107 | * @since 1.7.5 |
| 108 | * |
| 109 | * @param bool $show Whether to show the checkbox. |
| 110 | * @param WP_Post $post The current WP_Post object. |
| 111 | */ |
| 112 | if ( apply_filters( 'everest_forms_list_table_show_form_checkbox', $show, $form ) || apply_filters( 'everest_forms_list_table_delete_form_checkbox', $delete, $form ) ) { |
| 113 | return sprintf( '<input type="checkbox" name="form_id[]" value="%1$s" />', esc_attr( $form->ID ) ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Column enabled. |
| 119 | * |
| 120 | * @param object $posts Form object. |
| 121 | * @return string |
| 122 | */ |
| 123 | public function column_enabled( $posts ) { |
| 124 | $form_data = evf()->form->get( absint( $posts->ID ), array( 'content_only' => true ) ); |
| 125 | $form_enabled = isset( $form_data['form_enabled'] ) ? $form_data['form_enabled'] : 1; |
| 126 | |
| 127 | if ( current_user_can( 'everest_forms_edit_form', $posts->ID ) ) { |
| 128 | return '<label class="everest-forms-toggle-form form-enabled"><input type="checkbox" data-form_id="' . absint( $posts->ID ) . '" value="1" ' . checked( 1, $form_enabled, false ) . '/><span class="slider round"></span></label>'; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Return title column. |
| 134 | * |
| 135 | * @param object $posts Form object. |
| 136 | * @return string |
| 137 | */ |
| 138 | public function column_title( $posts ) { |
| 139 | $edit_link = admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . $posts->ID ); |
| 140 | $preview_link = add_query_arg( |
| 141 | array( |
| 142 | 'form_id' => absint( $posts->ID ), |
| 143 | 'evf_preview' => 'true', |
| 144 | ), |
| 145 | home_url() |
| 146 | ); |
| 147 | $title = _draft_or_post_title( $posts->ID ); |
| 148 | $post_type_object = get_post_type_object( 'everest_form' ); |
| 149 | $post_status = $posts->post_status; |
| 150 | $form_data = ! empty( $posts->post_content ) ? evf_decode( $posts->post_content ) : array(); |
| 151 | // Title. |
| 152 | $output = '<strong>'; |
| 153 | if ( 'trash' === $post_status ) { |
| 154 | $output .= esc_html( $title ); |
| 155 | } else { |
| 156 | $name = esc_html( $title ); |
| 157 | |
| 158 | if ( current_user_can( 'everest_forms_view_form', $posts->ID ) ) { |
| 159 | $name = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_html__( 'View Preview', 'everest-forms' ) . '" class="row-title" target="_blank" rel="noopener noreferrer">' . esc_html( $title ) . '</a>'; |
| 160 | } |
| 161 | |
| 162 | if ( current_user_can( 'everest_forms_view_form_entries', $posts->ID ) ) { |
| 163 | $name = '<a href="' . esc_url( esc_url( admin_url( 'admin.php?page=evf-entries&form_id=' . $posts->ID ) ) ) . '" title="' . esc_html__( 'View Entries', 'everest-forms' ) . '" class="row-title">' . esc_html( $title ) . '</a>'; |
| 164 | } |
| 165 | |
| 166 | if ( current_user_can( 'everest_forms_edit_form', $posts->ID ) ) { |
| 167 | $name = '<a href="' . esc_url( $edit_link ) . '" title="' . esc_html__( 'Edit this Form', 'everest-forms' ) . '" class="row-title">' . esc_html( $title ) . '</a>'; |
| 168 | } |
| 169 | |
| 170 | $output .= $name; |
| 171 | } |
| 172 | $output .= '</strong>'; |
| 173 | |
| 174 | // Get actions. |
| 175 | $actions = array(); |
| 176 | |
| 177 | if ( current_user_can( 'everest_forms_edit_form', $posts->ID ) && 'trash' !== $post_status ) { |
| 178 | $actions['edit'] = '<a href="' . esc_url( $edit_link ) . '" title="' . esc_html__( 'Edit this Form', 'everest-forms' ) . '">' . __( 'Edit', 'everest-forms' ) . '</a>'; |
| 179 | } |
| 180 | |
| 181 | if ( current_user_can( 'everest_forms_view_form_entries', $posts->ID ) && 'trash' !== $post_status ) { |
| 182 | $actions['entries'] = '<a href="' . esc_url( admin_url( 'admin.php?page=evf-entries&form_id=' . $posts->ID ) ) . '" title="' . esc_html__( 'View Entries', 'everest-forms' ) . '">' . __( 'Entries', 'everest-forms' ) . '</a>'; |
| 183 | } |
| 184 | |
| 185 | if ( current_user_can( 'everest_forms_delete_form', $posts->ID ) ) { |
| 186 | if ( 'trash' === $post_status ) { |
| 187 | $actions['untrash'] = '<a aria-label="' . esc_attr__( 'Restore this item from the Trash', 'everest-forms' ) . '" href="' . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $posts->ID ) ), 'untrash-post_' . $posts->ID ) . '">' . esc_html__( 'Restore', 'everest-forms' ) . '</a>'; |
| 188 | } elseif ( EMPTY_TRASH_DAYS ) { |
| 189 | $actions['trash'] = '<a class="submitdelete" aria-label="' . esc_attr__( 'Move this item to the Trash', 'everest-forms' ) . '" href="' . get_delete_post_link( $posts->ID ) . '">' . esc_html__( 'Trash', 'everest-forms' ) . '</a>'; |
| 190 | } |
| 191 | if ( 'trash' === $post_status || ! EMPTY_TRASH_DAYS ) { |
| 192 | $actions['delete'] = '<a class="submitdelete" aria-label="' . esc_attr__( 'Delete this item permanently', 'everest-forms' ) . '" href="' . get_delete_post_link( $posts->ID, '', true ) . '">' . esc_html__( 'Delete permanently', 'everest-forms' ) . '</a>'; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if ( current_user_can( 'everest_forms_view_form', $posts->ID ) ) { |
| 197 | $preview_link = add_query_arg( |
| 198 | array( |
| 199 | 'form_id' => absint( $posts->ID ), |
| 200 | 'evf_preview' => 'true', |
| 201 | ), |
| 202 | home_url() |
| 203 | ); |
| 204 | $duplicate_link = wp_nonce_url( admin_url( 'admin.php?page=evf-builder&action=duplicate_form&form_id=' . absint( $posts->ID ) ), 'everest-forms-duplicate-form_' . $posts->ID ); |
| 205 | $form_settings_link = admin_url( 'admin.php?page=evf-builder&tab=settings&form_id=' . absint( $posts->ID ) ); |
| 206 | |
| 207 | if ( 'trash' !== $post_status ) { |
| 208 | $actions['view'] = '<a href="' . esc_url( $preview_link ) . '" rel="bookmark" target="_blank">' . __( 'Preview', 'everest-forms' ) . '</a>'; |
| 209 | } |
| 210 | |
| 211 | if ( 'publish' === $post_status && current_user_can( 'everest_forms_create_forms' ) ) { |
| 212 | $actions['settings'] = '<a href="' . $form_settings_link . '" target="__blank">' . __( 'Settings', 'everest-forms' ) . '</a>'; |
| 213 | } |
| 214 | |
| 215 | if ( isset( $form_data['settings']['enable_conversational_forms'] ) && $form_data['settings']['enable_conversational_forms'] ) { |
| 216 | $actions['view_conversational_forms'] = '<a href="' . esc_url( home_url( $posts->post_name ) ) . '" title="View ConversationalForm" target="_blank">' . __( 'Conversational Form Preview', 'everest-forms' ) . '</a>'; |
| 217 | } |
| 218 | |
| 219 | if ( 'publish' === $post_status && current_user_can( 'everest_forms_create_forms' ) ) { |
| 220 | $actions['duplicate'] = '<a href="' . esc_url( $duplicate_link ) . '">' . __( 'Duplicate', 'everest-forms' ) . '</a>'; |
| 221 | } |
| 222 | |
| 223 | if ( 'publish' === $post_status && current_user_can( 'everest_forms_create_forms' ) ) { |
| 224 | $actions['locate'] = '<a href="#" class="evf-form-locate" data-id= "' . esc_attr( $posts->ID ) . '">' . __( 'Locate', 'everest-forms' ) . '</a>'; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Filter form list row actions. |
| 230 | * |
| 231 | * @since 3.4.2 |
| 232 | * @param array $actions Array of row actions. |
| 233 | * @param object $posts Form object. |
| 234 | * @return array |
| 235 | */ |
| 236 | $actions = apply_filters( 'everest_forms_form_list_actions', $actions, $posts ); |
| 237 | |
| 238 | $row_actions = array(); |
| 239 | |
| 240 | foreach ( $actions as $action => $link ) { |
| 241 | $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>'; |
| 242 | } |
| 243 | |
| 244 | $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>'; |
| 245 | |
| 246 | return $output; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Return shortcode column. |
| 251 | * |
| 252 | * @param object $posts Form object. |
| 253 | */ |
| 254 | public function column_shortcode( $posts ) { |
| 255 | ?> |
| 256 | <span class="shortcode evf-shortcode-field"> |
| 257 | <input type="text" onfocus="this.select();" readonly="readonly" value="<?php echo esc_attr( '[everest_form id="' . absint( $posts->ID ) . '"]' ); ?> " class="large-text code"> |
| 258 | <button class="button evf-copy-shortcode help_tip" type="button" href="#" data-tip="<?php esc_attr_e( 'Copy Shortcode!', 'everest-forms' ); ?>" data-copied="<?php esc_attr_e( 'Copied!', 'everest-forms' ); ?>"> |
| 259 | <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"> |
| 260 | <path fill="#383838" fill-rule="evenodd" d="M3.116 3.116A1.25 1.25 0 0 1 4 2.75h9A1.25 1.25 0 0 1 14.25 4v1a.75.75 0 0 0 1.5 0V4A2.75 2.75 0 0 0 13 1.25H4A2.75 2.75 0 0 0 1.25 4v9A2.75 2.75 0 0 0 4 15.75h1a.75.75 0 0 0 0-1.5H4A1.25 1.25 0 0 1 2.75 13V4c0-.332.132-.65.366-.884ZM9.75 11c0-.69.56-1.25 1.25-1.25h9c.69 0 1.25.56 1.25 1.25v9c0 .69-.56 1.25-1.25 1.25h-9c-.69 0-1.25-.56-1.25-1.25v-9ZM11 8.25A2.75 2.75 0 0 0 8.25 11v9A2.75 2.75 0 0 0 11 22.75h9A2.75 2.75 0 0 0 22.75 20v-9A2.75 2.75 0 0 0 20 8.25h-9Z" clip-rule="evenodd"></path> |
| 261 | </svg> |
| 262 | </button> |
| 263 | </span> |
| 264 | <?php |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Return author column. |
| 269 | * |
| 270 | * @param object $posts Form object. |
| 271 | * @return string |
| 272 | */ |
| 273 | public function column_author( $posts ) { |
| 274 | $user = get_user_by( 'id', $posts->post_author ); |
| 275 | |
| 276 | if ( ! $user ) { |
| 277 | return '<span class="na">–</span>'; |
| 278 | } |
| 279 | |
| 280 | $user_name = ! empty( $user->data->display_name ) ? $user->data->display_name : $user->data->user_login; |
| 281 | |
| 282 | if ( current_user_can( 'edit_user' ) ) { |
| 283 | return '<a href="' . esc_url( |
| 284 | add_query_arg( |
| 285 | array( |
| 286 | 'user_id' => $user->ID, |
| 287 | ), |
| 288 | admin_url( 'user-edit.php' ) |
| 289 | ) |
| 290 | ) . '">' . esc_html( $user_name ) . '</a>'; |
| 291 | } |
| 292 | |
| 293 | return esc_html( $user_name ); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Return date column. |
| 298 | * |
| 299 | * @param object $posts Form object. |
| 300 | * @return string |
| 301 | */ |
| 302 | public function column_date( $posts ) { |
| 303 | $post = get_post( $posts->ID ); |
| 304 | |
| 305 | if ( ! $post ) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | $t_time = mysql2date( |
| 310 | __( 'Y/m/d g:i:s A', 'everest-forms' ), |
| 311 | $post->post_date, |
| 312 | true |
| 313 | ); |
| 314 | $m_time = $post->post_date; |
| 315 | $time = mysql2date( 'G', $post->post_date ) - get_option( 'gmt_offset' ) * 3600; |
| 316 | |
| 317 | $time_diff = time() - $time; |
| 318 | |
| 319 | if ( $time_diff > 0 && $time_diff < 24 * 60 * 60 ) { |
| 320 | $h_time = sprintf( |
| 321 | /* translators: %s: Time */ |
| 322 | __( '%s ago', 'everest-forms' ), |
| 323 | human_time_diff( $time ) |
| 324 | ); |
| 325 | } else { |
| 326 | $h_time = mysql2date( __( 'Y/m/d', 'everest-forms' ), $m_time ); |
| 327 | } |
| 328 | |
| 329 | return '<abbr title="' . $t_time . '">' . $h_time . '</abbr>'; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Return entries count. |
| 334 | * |
| 335 | * @param object $posts Form object. |
| 336 | * @return string |
| 337 | */ |
| 338 | public function column_entries( $posts ) { |
| 339 | global $wpdb; |
| 340 | |
| 341 | if ( ! current_user_can( 'everest_forms_view_form_entries', $posts->ID ) ) { |
| 342 | return '-'; |
| 343 | } |
| 344 | |
| 345 | $entries = count( $wpdb->get_results( $wpdb->prepare( "SELECT form_id FROM {$wpdb->prefix}evf_entries WHERE `status` != 'trash' AND form_id = %d", $posts->ID ) ) ); // WPCS: cache ok, DB call ok. |
| 346 | |
| 347 | if ( isset( $_GET['status'] ) && 'trash' === $_GET['status'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 348 | return '<strong>' . absint( $entries ) . '</strong>'; |
| 349 | } else { |
| 350 | return '<a href="' . esc_url( admin_url( 'admin.php?page=evf-entries&form_id=' . $posts->ID ) ) . '">' . absint( $entries ) . '</a>'; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Return entries count. |
| 356 | * |
| 357 | * @param object $posts Form object. |
| 358 | * @return string |
| 359 | */ |
| 360 | public function column_tags( $posts ) { |
| 361 | $tags = FormHelper::get_form_tags( $posts->ID ); |
| 362 | |
| 363 | if ( empty( $tags ) ) { |
| 364 | return '<span class="na">–</span>'; |
| 365 | } |
| 366 | |
| 367 | $tag_count = count( $tags ); |
| 368 | $i = 0; |
| 369 | $output = ''; |
| 370 | |
| 371 | foreach ( $tags as $id => $tag ) { |
| 372 | if ( empty( $tag ) ) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | $output .= sprintf( |
| 377 | '<button type="submit" name="tags" class="button button-small evf-form-tags" value="%s" data-tag-id="%s">%s</button>', |
| 378 | esc_attr( $id ), |
| 379 | esc_attr( $id ), |
| 380 | esc_html( $tag ) |
| 381 | ); |
| 382 | |
| 383 | if ( ++$i < $tag_count ) { |
| 384 | $output .= ' '; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | return $output ?: '<span class="na">–</span>'; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Table list views. |
| 393 | * |
| 394 | * @return array |
| 395 | */ |
| 396 | protected function get_views() { |
| 397 | return array(); |
| 398 | // global $wpdb; |
| 399 | |
| 400 | // $class = ''; |
| 401 | // $status_links = array(); |
| 402 | // $num_posts = array(); |
| 403 | |
| 404 | // $total_posts = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'everest_form' AND post_status IN ( 'publish', 'inactive' )" ); // WPCS: cache ok, db call ok. |
| 405 | // $all_args = array( 'page' => 'evf-builder' ); |
| 406 | // if ( empty( $class ) && empty( $_REQUEST['status'] ) ) { |
| 407 | // $class = 'current'; |
| 408 | // } |
| 409 | |
| 410 | // $all_inner_html = sprintf( |
| 411 | // // * translators: %s: count */ |
| 412 | // _nx( |
| 413 | // 'All <span class="count">(%s)</span>', |
| 414 | // 'All <span class="count">(%s)</span>', |
| 415 | // $total_posts, |
| 416 | // 'posts', |
| 417 | // 'everest-forms' |
| 418 | // ), |
| 419 | // number_format_i18n( $total_posts ) |
| 420 | // ); |
| 421 | |
| 422 | // $status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class ); |
| 423 | |
| 424 | // foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) { |
| 425 | // $class = ''; |
| 426 | // $status_name = $status->name; |
| 427 | // $num_posts[ $status_name ] = count( evf()->form->get_multiple( array( 'post_status' => $status_name ) ) ); |
| 428 | |
| 429 | // if ( ! in_array( $status_name, array( 'publish', 'draft', 'pending', 'trash', 'future', 'private', 'auto-draft', 'approved', 'denied' ), true ) || empty( $num_posts[ $status_name ] ) ) { |
| 430 | // continue; |
| 431 | // } |
| 432 | |
| 433 | // if ( isset( $_REQUEST['status'] ) && $status_name === $_REQUEST['status'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 434 | // $class = 'current'; |
| 435 | // } |
| 436 | |
| 437 | // $status_args = array( |
| 438 | // 'page' => 'evf-builder', |
| 439 | // 'status' => $status_name, |
| 440 | // ); |
| 441 | |
| 442 | // $status_label = sprintf( |
| 443 | // translate_nooped_plural( $status->label_count, $num_posts[ $status_name ] ), |
| 444 | // number_format_i18n( $num_posts[ $status_name ] ) |
| 445 | // ); |
| 446 | |
| 447 | // if ( 'publish' === $status_name ) { |
| 448 | // $status_label = str_replace( 'Published', __( 'Active', 'everest-forms' ), $status_label ); |
| 449 | // } |
| 450 | // $status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class ); |
| 451 | // } |
| 452 | |
| 453 | // $inactive_count = count( evf()->form->get_multiple( array( 'post_status' => 'inactive' ) ) ); |
| 454 | |
| 455 | // $inactive_args = array( |
| 456 | // 'page' => 'evf-builder', |
| 457 | // 'status' => 'inactive', |
| 458 | // ); |
| 459 | |
| 460 | // $inactive_label = sprintf( |
| 461 | // _nx( |
| 462 | // 'Inactive <span class="count">(%s)</span>', |
| 463 | // 'Inactive <span class="count">(%s)</span>', |
| 464 | // $inactive_count, |
| 465 | // 'posts', |
| 466 | // 'everest-forms' |
| 467 | // ), |
| 468 | // number_format_i18n( $inactive_count ) |
| 469 | // ); |
| 470 | // $status_links['inactive'] = $this->get_edit_link( $inactive_args, $inactive_label, $class ); |
| 471 | |
| 472 | // return $status_links; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Helper to create links to admin.php with params. |
| 477 | * |
| 478 | * @since 1.5.3 |
| 479 | * |
| 480 | * @param string[] $args Associative array of URL parameters for the link. |
| 481 | * @param string $label Link text. |
| 482 | * @param string $class Optional. Class attribute. Default empty string. |
| 483 | * @return string The formatted link string. |
| 484 | */ |
| 485 | protected function get_edit_link( $args, $label, $class = '' ) { |
| 486 | $url = add_query_arg( $args, 'admin.php' ); |
| 487 | |
| 488 | $class_html = ''; |
| 489 | $aria_current = ''; |
| 490 | |
| 491 | if ( ! empty( $class ) ) { |
| 492 | $class_html = sprintf( |
| 493 | ' class="%s"', |
| 494 | esc_attr( $class ) |
| 495 | ); |
| 496 | |
| 497 | if ( 'current' === $class ) { |
| 498 | $aria_current = ' aria-current="page"'; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | return sprintf( |
| 503 | '<a href="%s"%s%s>%s</a>', |
| 504 | esc_url( $url ), |
| 505 | $class_html, |
| 506 | $aria_current, |
| 507 | $label |
| 508 | ); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Get bulk actions. |
| 513 | * |
| 514 | * @return array |
| 515 | */ |
| 516 | protected function get_bulk_actions() { |
| 517 | $actions = array(); |
| 518 | |
| 519 | $has_tags = ! empty( FormHelper::get_all_form_tags( 'term_id' ) ); |
| 520 | |
| 521 | if ( isset( $_GET['status'] ) && 'trash' === $_GET['status'] ) { |
| 522 | if ( current_user_can( 'everest_forms_edit_forms' ) ) { |
| 523 | $actions['untrash'] = esc_html__( 'Restore', 'everest-forms' ); |
| 524 | } |
| 525 | |
| 526 | if ( current_user_can( 'everest_forms_delete_forms' ) ) { |
| 527 | $actions['delete'] = esc_html__( 'Delete permanently', 'everest-forms' ); |
| 528 | } |
| 529 | } elseif ( current_user_can( 'everest_forms_delete_forms' ) ) { |
| 530 | $actions = array( |
| 531 | 'trash' => esc_html__( 'Move to trash', 'everest-forms' ), |
| 532 | ); |
| 533 | |
| 534 | if ( $has_tags ) { |
| 535 | $actions['edit-tags'] = esc_html__( 'Edit Tags', 'everest-forms' ); |
| 536 | } |
| 537 | |
| 538 | $actions['inactive'] = esc_html__( 'Inactive', 'everest-forms' ); |
| 539 | $actions['active'] = esc_html__( 'Active', 'everest-forms' ); |
| 540 | } |
| 541 | |
| 542 | return $actions; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | /** |
| 547 | * Process bulk actions. |
| 548 | * |
| 549 | * @since 1.2.0 |
| 550 | */ |
| 551 | public function process_bulk_action() { |
| 552 | $action = $this->current_action(); |
| 553 | $form_ids = isset( $_REQUEST['form_id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['form_id'] ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification |
| 554 | $count = 0; |
| 555 | |
| 556 | if ( $form_ids ) { |
| 557 | check_admin_referer( 'bulk-forms' ); |
| 558 | } |
| 559 | |
| 560 | switch ( $action ) { |
| 561 | case 'trash': |
| 562 | foreach ( $form_ids as $form_id ) { |
| 563 | if ( wp_trash_post( $form_id ) ) { |
| 564 | ++$count; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | add_settings_error( |
| 569 | 'bulk_action', |
| 570 | 'bulk_action', |
| 571 | /* translators: %d: number of forms */ |
| 572 | sprintf( _n( '%d form moved to the Trash.', '%d forms moved to the Trash.', $count, 'everest-forms' ), $count ), |
| 573 | 'updated' |
| 574 | ); |
| 575 | break; |
| 576 | case 'untrash': |
| 577 | foreach ( $form_ids as $form_id ) { |
| 578 | if ( wp_untrash_post( $form_id ) ) { |
| 579 | ++$count; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | add_settings_error( |
| 584 | 'bulk_action', |
| 585 | 'bulk_action', |
| 586 | /* translators: %d: number of forms */ |
| 587 | sprintf( _n( '%d form restored from the Trash.', '%d forms restored from the Trash.', $count, 'everest-forms' ), $count ), |
| 588 | 'updated' |
| 589 | ); |
| 590 | break; |
| 591 | case 'delete': |
| 592 | foreach ( $form_ids as $form_id ) { |
| 593 | if ( wp_delete_post( $form_id, true ) ) { |
| 594 | ++$count; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | add_settings_error( |
| 599 | 'bulk_action', |
| 600 | 'bulk_action', |
| 601 | /* translators: %d: number of forms */ |
| 602 | sprintf( _n( '%d form permanently deleted.', '%d forms permanently deleted.', $count, 'everest-forms' ), $count ), |
| 603 | 'updated' |
| 604 | ); |
| 605 | break; |
| 606 | case 'inactive': |
| 607 | foreach ( $form_ids as $form_id ) { |
| 608 | $result = wp_update_post( |
| 609 | array( |
| 610 | 'ID' => $form_id, |
| 611 | 'post_status' => 'inactive', |
| 612 | ) |
| 613 | ); |
| 614 | if ( $result ) { |
| 615 | $form_data = evf()->form->get( absint( $form_id ), array( 'content_only' => true ) ); |
| 616 | $form_data['form_enabled'] = 0; |
| 617 | |
| 618 | evf()->form->update( $form_id, $form_data ); |
| 619 | ++$count; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | add_settings_error( |
| 624 | 'bulk_action', |
| 625 | 'bulk_action', |
| 626 | /* translators: %d: number of forms */ |
| 627 | sprintf( _n( '%d form marked as inactive.', '%d forms marked as inactive.', $count, 'everest-forms' ), $count ), |
| 628 | 'updated' |
| 629 | ); |
| 630 | break; |
| 631 | |
| 632 | case 'active': |
| 633 | foreach ( $form_ids as $form_id ) { |
| 634 | $result = wp_update_post( |
| 635 | array( |
| 636 | 'ID' => $form_id, |
| 637 | 'post_status' => 'publish', |
| 638 | ) |
| 639 | ); |
| 640 | if ( $result ) { |
| 641 | $form_data = evf()->form->get( absint( $form_id ), array( 'content_only' => true ) ); |
| 642 | $form_data['form_enabled'] = 1; |
| 643 | |
| 644 | evf()->form->update( $form_id, $form_data ); |
| 645 | ++$count; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | add_settings_error( |
| 650 | 'bulk_action', |
| 651 | 'bulk_action', |
| 652 | /* translators: %d: number of forms */ |
| 653 | sprintf( _n( '%d form marked as active.', '%d forms marked as active.', $count, 'everest-forms' ), $count ), |
| 654 | 'updated' |
| 655 | ); |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Extra controls to be displayed between bulk actions and pagination. |
| 662 | * |
| 663 | * @param string $which The location of the extra table nav markup. |
| 664 | */ |
| 665 | protected function extra_tablenav( $which ) { |
| 666 | $num_posts = wp_count_posts( 'everest_form', 'readable' ); |
| 667 | $tag_list = FormHelper::get_all_form_tags( 'term_id' ); |
| 668 | |
| 669 | echo '<div class="everest-forms-extra-table-nav">'; |
| 670 | |
| 671 | if ( 'top' === $which ) { |
| 672 | $this->forms_status_dropdown(); |
| 673 | |
| 674 | if ( ! empty( $tag_list ) ) { |
| 675 | $this->tags_dropdown(); |
| 676 | submit_button( __( 'Filter', 'everest-forms' ), '', 'filter_action', false, array( 'category' => 'post-query-submit' ) ); |
| 677 | } |
| 678 | $this->manage_tags(); |
| 679 | if ( $num_posts->trash && isset( $_GET['status'] ) && 'trash' === $_GET['status'] && current_user_can( 'everest_forms_delete_forms' ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 680 | submit_button( __( 'Empty Trash', 'everest-forms' ), 'apply', 'delete_all', false ); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | echo '</div>'; |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Display a status dropdown for filtering forms. |
| 689 | */ |
| 690 | public function forms_status_dropdown() { |
| 691 | global $wpdb; |
| 692 | |
| 693 | $current_status = isset( $_REQUEST['status'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['status'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
| 694 | |
| 695 | $total_posts = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'everest_form' AND post_status IN ( 'publish', 'inactive' )" ); |
| 696 | $active_count = count( evf()->form->get_multiple( array( 'post_status' => 'publish' ) ) ); |
| 697 | $inactive_count = count( evf()->form->get_multiple( array( 'post_status' => 'inactive' ) ) ); |
| 698 | $trash_count = count( evf()->form->get_multiple( array( 'post_status' => 'trash' ) ) ); |
| 699 | |
| 700 | $statuses = array( |
| 701 | '' => sprintf( __( 'All (%d)', 'everest-forms' ), $total_posts ), |
| 702 | 'publish' => sprintf( __( 'Active (%d)', 'everest-forms' ), $active_count ), |
| 703 | 'inactive' => sprintf( __( 'Inactive (%d)', 'everest-forms' ), $inactive_count ), |
| 704 | 'trash' => sprintf( __( 'Trash (%d)', 'everest-forms' ), $trash_count ), |
| 705 | ); |
| 706 | ?> |
| 707 | <label for="filter-by-form-status" class="screen-reader-text"><?php esc_html_e( 'Filter by status', 'everest-forms' ); ?></label> |
| 708 | <select name="status" class="evf-enhanced-select" id="filter-by-form-status" > |
| 709 | <?php foreach ( $statuses as $value => $label ) : ?> |
| 710 | <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current_status, $value ); ?>> |
| 711 | <?php echo esc_html( $label ); ?> |
| 712 | </option> |
| 713 | <?php endforeach; ?> |
| 714 | </select> |
| 715 | <?php |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Prepare table list items. |
| 720 | */ |
| 721 | public function prepare_items() { |
| 722 | $user_id = get_current_user_id(); |
| 723 | $per_page = $this->get_items_per_page( 'evf_forms_per_page' ); |
| 724 | $current_page = $this->get_pagenum(); |
| 725 | |
| 726 | // Query args. |
| 727 | $args = array( |
| 728 | 'post_type' => 'everest_form', |
| 729 | 'posts_per_page' => $per_page, |
| 730 | 'paged' => $current_page, |
| 731 | 'no_found_rows' => false, |
| 732 | 'ignore_sticky_posts' => true, |
| 733 | ); |
| 734 | |
| 735 | /** |
| 736 | * Filter on the basis of tags. |
| 737 | * |
| 738 | * @since 3.2.0 |
| 739 | */ |
| 740 | if ( ! empty( $_REQUEST['tags'] ) ) { |
| 741 | $args['tax_query'] = array( |
| 742 | array( |
| 743 | 'taxonomy' => EVF_Post_Types::TAGS_TAXONOMY, |
| 744 | 'field' => 'term_id', |
| 745 | 'terms' => array_map( 'absint', is_array( $_REQUEST['tags'] ) ? $_REQUEST['tags'] : array( $_REQUEST['tags'] ) ), |
| 746 | 'operator' => 'IN', |
| 747 | ), |
| 748 | ); |
| 749 | } |
| 750 | |
| 751 | if ( ! empty( $_REQUEST['status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 752 | $status = sanitize_text_field( wp_unslash( $_REQUEST['status'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 753 | |
| 754 | if ( $status === 'inactive' ) { |
| 755 | $args['post_status'] = 'inactive'; |
| 756 | } else { |
| 757 | // If it's a valid WordPress status, use it |
| 758 | $args['post_status'] = $status; |
| 759 | } |
| 760 | } else { |
| 761 | $args['post_status'] = array( 'publish', 'inactive' ); |
| 762 | } |
| 763 | |
| 764 | // Handle the search query. |
| 765 | if ( ! empty( $_REQUEST['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 766 | $args['s'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 767 | } |
| 768 | |
| 769 | $args['orderby'] = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) : 'date_created'; // phpcs:ignore WordPress.Security.NonceVerification |
| 770 | $args['order'] = isset( $_REQUEST['order'] ) && 'ASC' === strtoupper( sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) ) ? 'ASC' : 'DESC'; // phpcs:ignore WordPress.Security.NonceVerification |
| 771 | |
| 772 | // Can user interact, lets check the view capabilities? |
| 773 | if ( current_user_can( 'everest_forms_view_forms' ) && ! current_user_can( 'everest_forms_view_others_forms' ) ) { |
| 774 | $args['author'] = $user_id; |
| 775 | } |
| 776 | |
| 777 | if ( ! current_user_can( 'everest_forms_view_forms' ) && current_user_can( 'everest_forms_view_others_forms' ) ) { |
| 778 | $args['author__not_in'] = $user_id; |
| 779 | } |
| 780 | |
| 781 | if ( ! current_user_can( 'everest_forms_view_forms' ) && ! current_user_can( 'everest_forms_view_others_forms' ) ) { |
| 782 | $args['post__in'] = array( 0 ); |
| 783 | } |
| 784 | |
| 785 | // Get the forms. |
| 786 | $posts = new WP_Query( $args ); |
| 787 | $this->items = $posts->posts; |
| 788 | |
| 789 | // Set the pagination. |
| 790 | $this->set_pagination_args( |
| 791 | array( |
| 792 | 'total_items' => $posts->found_posts, |
| 793 | 'per_page' => $per_page, |
| 794 | 'total_pages' => $posts->max_num_pages, |
| 795 | ) |
| 796 | ); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Display a form dropdown for filtering entries. |
| 801 | */ |
| 802 | public function tags_dropdown() { |
| 803 | $tag_list = FormHelper::get_all_form_tags( 'term_id' ); |
| 804 | $selected_tags = isset( $_REQUEST['tags'] ) ? is_array( $_REQUEST['tags'] ) ? $_REQUEST['tags'] : array( $_REQUEST['tags'] ) : array(); // phpcs:ignore WordPress.Security.NonceVerification |
| 805 | ?> |
| 806 | <label for="filter-by-tags" class="screen-reader-text"><?php esc_html_e( 'Filter by Category', 'everest-forms' ); ?></label> |
| 807 | <select name="tags[]" id="filter-by-tags" class="evf-enhanced-select" data-placeholder="<?php echo __( 'Select tags', 'everest-forms' ); ?>" multiple style="min-width: 150px;"> |
| 808 | |
| 809 | <?php foreach ( $tag_list as $id => $tag ) : ?> |
| 810 | <option value="<?php echo esc_attr( $id ); ?>" <?php echo in_array( $id, $selected_tags ) ? 'selected' : ''; ?>><?php echo esc_html( $tag ); ?></option> |
| 811 | <?php endforeach; ?> |
| 812 | </select> |
| 813 | <?php |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Manage tags - delete. |
| 818 | * |
| 819 | * @since 3.2.0 |
| 820 | */ |
| 821 | public function manage_tags() { |
| 822 | $tags = FormHelper::get_all_form_tags( 'term_id' ); |
| 823 | |
| 824 | if ( empty( $tags ) ) { |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | printf( |
| 829 | '<button type="button" data-tags="%s" class="button evf-manage-tags">%s</button>', |
| 830 | htmlspecialchars( json_encode( $tags ) ), |
| 831 | __( 'Delete Tags', 'everest-forms' ) |
| 832 | ); |
| 833 | } |
| 834 | |
| 835 | public function display_tablenav( $which ) { |
| 836 | $action = $this->current_action(); |
| 837 | $form_ids = isset( $_REQUEST['form_id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['form_id'] ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification |
| 838 | |
| 839 | parent::display_tablenav( $which ); |
| 840 | |
| 841 | $tags = array_keys( FormHelper::get_selected_forms_tags( $form_ids ) ); |
| 842 | $tag_list = FormHelper::get_all_form_tags( 'term_id' ); |
| 843 | |
| 844 | $all_forms = array(); |
| 845 | foreach ( $this->items as $items ) { |
| 846 | $all_forms[ $items->ID ] = $items->post_title; |
| 847 | } |
| 848 | |
| 849 | if ( 'edit-tags' === $action && 'top' === $which ) { |
| 850 | ?> |
| 851 | <div class="evf-bulk-edit-forms-tags-container"> |
| 852 | <div class="tags-selects"> |
| 853 | |
| 854 | <label class="label"><?php echo __( 'Selected forms', 'everest-forms' ); ?></label> |
| 855 | <select name="bulk_tag_forms[]" id="bulk_tag_forms" class="evf-enhanced-select" data-placeholder="<?php echo __( 'Select forms', 'everest-forms' ); ?>" multiple style="min-width: 300px;"> |
| 856 | |
| 857 | <?php foreach ( $all_forms as $id => $form_title ) : ?> |
| 858 | <option value="<?php echo esc_attr( $id ); ?>" <?php echo in_array( $id, $form_ids ) ? 'selected' : ''; ?>><?php echo esc_html( $form_title ); ?></option> |
| 859 | <?php endforeach; ?> |
| 860 | </select> |
| 861 | </div> |
| 862 | |
| 863 | <div class="tags-selects"> |
| 864 | <label class="label"><?php echo __( 'Selected Tags', 'everest-forms' ); ?></label> |
| 865 | <select name="bulk_tags[]" id="bulk_tags" class="form-tags-select2" data-placeholder="<?php echo __( 'Select tags', 'everest-forms' ); ?>" multiple style="min-width: 300px;"> |
| 866 | |
| 867 | <?php foreach ( $tag_list as $id => $tag ) : ?> |
| 868 | <option value="<?php echo esc_attr( $id ); ?>" <?php echo in_array( $id, $tags ) ? 'selected' : ''; ?>><?php echo esc_html( $tag ); ?></option> |
| 869 | <?php endforeach; ?> |
| 870 | </select> |
| 871 | </div> |
| 872 | <div class="tags-selects"> |
| 873 | <button type="button" class="button evf-update-bulk-tags"><?php echo __( 'Update Tags', 'everest-forms' ); ?></button> |
| 874 | </div> |
| 875 | |
| 876 | </div> |
| 877 | <?php |
| 878 | |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 |