builder
6 years ago
plugin-updates
8 years ago
settings
6 years ago
views
6 years ago
class-evf-admin-addons.php
6 years ago
class-evf-admin-assets.php
6 years ago
class-evf-admin-builder.php
8 years ago
class-evf-admin-editor.php
6 years ago
class-evf-admin-entries-table-list.php
6 years ago
class-evf-admin-entries.php
6 years ago
class-evf-admin-forms-table-list.php
6 years ago
class-evf-admin-forms.php
6 years ago
class-evf-admin-import-export.php
6 years ago
class-evf-admin-menus.php
6 years ago
class-evf-admin-notices.php
6 years ago
class-evf-admin-settings.php
6 years ago
class-evf-admin-tools.php
6 years ago
class-evf-admin-welcome.php
6 years ago
class-evf-admin.php
6 years ago
evf-admin-functions.php
6 years ago
class-evf-admin-forms-table-list.php
495 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Forms Table List |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @version 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 12 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Forms table list class. |
| 17 | */ |
| 18 | class EVF_Admin_Forms_Table_List extends WP_List_Table { |
| 19 | |
| 20 | /** |
| 21 | * Initialize the form table list. |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | parent::__construct( |
| 25 | array( |
| 26 | 'singular' => 'form', |
| 27 | 'plural' => 'forms', |
| 28 | 'ajax' => false, |
| 29 | ) |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * No items found text. |
| 35 | */ |
| 36 | public function no_items() { |
| 37 | esc_html_e( 'No Forms found.', 'everest-forms' ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get list columns. |
| 42 | * |
| 43 | * @return array |
| 44 | */ |
| 45 | public function get_columns() { |
| 46 | return array( |
| 47 | 'cb' => '<input type="checkbox" />', |
| 48 | 'enabled' => '', |
| 49 | 'title' => __( 'Title', 'everest-forms' ), |
| 50 | 'shortcode' => __( 'Shortcode', 'everest-forms' ), |
| 51 | 'author' => __( 'Author', 'everest-forms' ), |
| 52 | 'date' => __( 'Date', 'everest-forms' ), |
| 53 | 'entries' => __( 'Entries', 'everest-forms' ), |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get a list of sortable columns. |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | protected function get_sortable_columns() { |
| 63 | return array( |
| 64 | 'title' => array( 'title', false ), |
| 65 | 'author' => array( 'author', false ), |
| 66 | 'date' => array( 'date', false ), |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Column cb. |
| 72 | * |
| 73 | * @param object $form Form object. |
| 74 | * @return string |
| 75 | */ |
| 76 | public function column_cb( $form ) { |
| 77 | return sprintf( '<input type="checkbox" name="form_id[]" value="%1$s" />', esc_attr( $form->ID ) ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Column enabled. |
| 82 | * |
| 83 | * @param object $posts Form object. |
| 84 | * @return string |
| 85 | */ |
| 86 | public function column_enabled( $posts ) { |
| 87 | $form_data = evf()->form->get( absint( $posts->ID ), array( 'content_only' => true ) ); |
| 88 | $form_enabled = isset( $form_data['form_enabled'] ) ? $form_data['form_enabled'] : 1; |
| 89 | |
| 90 | 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>'; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Return title column. |
| 95 | * |
| 96 | * @param object $posts Form object. |
| 97 | * @return string |
| 98 | */ |
| 99 | public function column_title( $posts ) { |
| 100 | $edit_link = admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . $posts->ID ); |
| 101 | $title = _draft_or_post_title( $posts->ID ); |
| 102 | $post_type_object = get_post_type_object( 'everest_form' ); |
| 103 | $post_status = $posts->post_status; |
| 104 | |
| 105 | // Title. |
| 106 | $output = '<strong>'; |
| 107 | if ( 'trash' === $post_status ) { |
| 108 | $output .= esc_html( $title ); |
| 109 | } else { |
| 110 | $output .= '<a href="' . esc_url( $edit_link ) . '" class="row-title">' . esc_html( $title ) . '</a>'; |
| 111 | } |
| 112 | $output .= '</strong>'; |
| 113 | |
| 114 | // Get actions. |
| 115 | if ( current_user_can( $post_type_object->cap->edit_post, $posts->ID ) && 'trash' !== $post_status ) { |
| 116 | $actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'everest-forms' ) . '</a>'; |
| 117 | $actions['entries'] = '<a href="' . esc_url( admin_url( 'admin.php?page=evf-entries&form_id=' . $posts->ID ) ) . '">' . __( 'Entries', 'everest-forms' ) . '</a>'; |
| 118 | } |
| 119 | |
| 120 | if ( current_user_can( $post_type_object->cap->delete_post, $posts->ID ) ) { |
| 121 | if ( 'trash' === $post_status ) { |
| 122 | $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>'; |
| 123 | } elseif ( EMPTY_TRASH_DAYS ) { |
| 124 | $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>'; |
| 125 | } |
| 126 | if ( 'trash' === $post_status || ! EMPTY_TRASH_DAYS ) { |
| 127 | $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>'; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if ( current_user_can( $post_type_object->cap->edit_post, $posts->ID ) ) { |
| 132 | $preview_link = add_query_arg( |
| 133 | array( |
| 134 | 'form_id' => absint( $posts->ID ), |
| 135 | 'evf_preview' => 'true', |
| 136 | ), |
| 137 | home_url() |
| 138 | ); |
| 139 | $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 ); |
| 140 | |
| 141 | if ( 'trash' !== $post_status ) { |
| 142 | $actions['view'] = '<a href="' . esc_url( $preview_link ) . '" rel="bookmark" target="_blank">' . __( 'Preview', 'everest-forms' ) . '</a>'; |
| 143 | } |
| 144 | |
| 145 | if ( 'publish' === $post_status ) { |
| 146 | $actions['duplicate'] = '<a href="' . esc_url( $duplicate_link ) . '">' . __( 'Duplicate', 'everest-forms' ) . '</a>'; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | $row_actions = array(); |
| 151 | |
| 152 | foreach ( $actions as $action => $link ) { |
| 153 | $row_actions[] = '<span class="' . esc_attr( $action ) . '">' . $link . '</span>'; |
| 154 | } |
| 155 | |
| 156 | $output .= '<div class="row-actions">' . implode( ' | ', $row_actions ) . '</div>'; |
| 157 | |
| 158 | return $output; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Return shortcode column. |
| 163 | * |
| 164 | * @param object $posts Form object. |
| 165 | * @return string |
| 166 | */ |
| 167 | public function column_shortcode( $posts ) { |
| 168 | $shortcode = '[everest_form id="' . $posts->ID . '"]'; |
| 169 | |
| 170 | return sprintf( '<span class="shortcode"><input type="text" onfocus="this.select();" readonly="readonly" value=\'%s\' class="regular-text code"></span>', $shortcode ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Return author column. |
| 175 | * |
| 176 | * @param object $posts Form object. |
| 177 | * @return string |
| 178 | */ |
| 179 | public function column_author( $posts ) { |
| 180 | $user = get_user_by( 'id', $posts->post_author ); |
| 181 | |
| 182 | if ( ! $user ) { |
| 183 | return '<span class="na">–</span>'; |
| 184 | } |
| 185 | |
| 186 | $user_name = ! empty( $user->data->display_name ) ? $user->data->display_name : $user->data->user_login; |
| 187 | |
| 188 | if ( current_user_can( 'edit_user' ) ) { |
| 189 | return '<a href="' . esc_url( |
| 190 | add_query_arg( |
| 191 | array( |
| 192 | 'user_id' => $user->ID, |
| 193 | ), |
| 194 | admin_url( 'user-edit.php' ) |
| 195 | ) |
| 196 | ) . '">' . esc_html( $user_name ) . '</a>'; |
| 197 | } |
| 198 | |
| 199 | return esc_html( $user_name ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Return date column. |
| 204 | * |
| 205 | * @param object $posts Form object. |
| 206 | * @return string |
| 207 | */ |
| 208 | public function column_date( $posts ) { |
| 209 | $post = get_post( $posts->ID ); |
| 210 | |
| 211 | if ( ! $post ) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | $t_time = mysql2date( |
| 216 | __( 'Y/m/d g:i:s A', 'everest-forms' ), |
| 217 | $post->post_date, |
| 218 | true |
| 219 | ); |
| 220 | $m_time = $post->post_date; |
| 221 | $time = mysql2date( 'G', $post->post_date ) - get_option( 'gmt_offset' ) * 3600; |
| 222 | |
| 223 | $time_diff = time() - $time; |
| 224 | |
| 225 | if ( $time_diff > 0 && $time_diff < 24 * 60 * 60 ) { |
| 226 | $h_time = sprintf( |
| 227 | /* translators: %s: Time */ |
| 228 | __( '%s ago', 'everest-forms' ), |
| 229 | human_time_diff( $time ) |
| 230 | ); |
| 231 | } else { |
| 232 | $h_time = mysql2date( __( 'Y/m/d', 'everest-forms' ), $m_time ); |
| 233 | } |
| 234 | |
| 235 | return '<abbr title="' . $t_time . '">' . $h_time . '</abbr>'; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Return entries count. |
| 240 | * |
| 241 | * @param object $posts Form object. |
| 242 | * @return string |
| 243 | */ |
| 244 | public function column_entries( $posts ) { |
| 245 | global $wpdb; |
| 246 | |
| 247 | $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. |
| 248 | |
| 249 | if ( isset( $_GET['status'] ) && 'trash' === $_GET['status'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 250 | return '<strong>' . absint( $entries ) . '</strong>'; |
| 251 | } else { |
| 252 | return '<a href="' . esc_url( admin_url( 'admin.php?page=evf-entries&form_id=' . $posts->ID ) ) . '">' . absint( $entries ) . '</a>'; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Table list views. |
| 258 | * |
| 259 | * @return array |
| 260 | */ |
| 261 | protected function get_views() { |
| 262 | $status_links = array(); |
| 263 | $num_posts = wp_count_posts( 'everest_form', 'readable' ); |
| 264 | $total_posts = array_sum( (array) $num_posts ); |
| 265 | $all_args = array( 'page' => 'evf-builder' ); |
| 266 | |
| 267 | $class = ''; |
| 268 | |
| 269 | // Subtract post types that are not included in the admin all list. |
| 270 | $post_stati = get_post_stati( array( 'show_in_admin_all_list' => false ) ); |
| 271 | foreach ( $post_stati as $state ) { |
| 272 | $total_posts -= $num_posts->$state; |
| 273 | } |
| 274 | |
| 275 | if ( empty( $class ) && empty( $_REQUEST['status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 276 | $class = 'current'; |
| 277 | } |
| 278 | |
| 279 | $all_inner_html = sprintf( |
| 280 | /* translators: %s: count */ |
| 281 | _nx( |
| 282 | 'All <span class="count">(%s)</span>', |
| 283 | 'All <span class="count">(%s)</span>', |
| 284 | $total_posts, |
| 285 | 'posts', |
| 286 | 'everest-forms' |
| 287 | ), |
| 288 | number_format_i18n( $total_posts ) |
| 289 | ); |
| 290 | |
| 291 | $status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class ); |
| 292 | |
| 293 | foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) { |
| 294 | $class = ''; |
| 295 | $status_name = $status->name; |
| 296 | |
| 297 | if ( ! in_array( $status_name, array( 'publish', 'draft', 'pending', 'trash', 'future', 'private', 'auto-draft' ), true ) || empty( $num_posts->$status_name ) ) { |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | if ( isset( $_REQUEST['status'] ) && $status_name === $_REQUEST['status'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 302 | $class = 'current'; |
| 303 | } |
| 304 | |
| 305 | $status_args = array( |
| 306 | 'page' => 'evf-builder', |
| 307 | 'status' => $status_name, |
| 308 | ); |
| 309 | |
| 310 | $status_label = sprintf( |
| 311 | translate_nooped_plural( $status->label_count, $num_posts->$status_name ), |
| 312 | number_format_i18n( $num_posts->$status_name ) |
| 313 | ); |
| 314 | |
| 315 | $status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class ); |
| 316 | } |
| 317 | |
| 318 | return $status_links; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Helper to create links to admin.php with params. |
| 323 | * |
| 324 | * @since 1.5.3 |
| 325 | * |
| 326 | * @param string[] $args Associative array of URL parameters for the link. |
| 327 | * @param string $label Link text. |
| 328 | * @param string $class Optional. Class attribute. Default empty string. |
| 329 | * @return string The formatted link string. |
| 330 | */ |
| 331 | protected function get_edit_link( $args, $label, $class = '' ) { |
| 332 | $url = add_query_arg( $args, 'admin.php' ); |
| 333 | |
| 334 | $class_html = ''; |
| 335 | $aria_current = ''; |
| 336 | |
| 337 | if ( ! empty( $class ) ) { |
| 338 | $class_html = sprintf( |
| 339 | ' class="%s"', |
| 340 | esc_attr( $class ) |
| 341 | ); |
| 342 | |
| 343 | if ( 'current' === $class ) { |
| 344 | $aria_current = ' aria-current="page"'; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return sprintf( |
| 349 | '<a href="%s"%s%s>%s</a>', |
| 350 | esc_url( $url ), |
| 351 | $class_html, |
| 352 | $aria_current, |
| 353 | $label |
| 354 | ); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Get bulk actions. |
| 359 | * |
| 360 | * @return array |
| 361 | */ |
| 362 | protected function get_bulk_actions() { |
| 363 | if ( isset( $_GET['status'] ) && 'trash' === $_GET['status'] ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 364 | return array( |
| 365 | 'untrash' => __( 'Restore', 'everest-forms' ), |
| 366 | 'delete' => __( 'Delete permanently', 'everest-forms' ), |
| 367 | ); |
| 368 | } |
| 369 | |
| 370 | return array( |
| 371 | 'trash' => __( 'Move to trash', 'everest-forms' ), |
| 372 | ); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Process bulk actions. |
| 377 | * |
| 378 | * @since 1.2.0 |
| 379 | */ |
| 380 | public function process_bulk_action() { |
| 381 | $action = $this->current_action(); |
| 382 | $form_ids = isset( $_REQUEST['form_id'] ) ? wp_parse_id_list( wp_unslash( $_REQUEST['form_id'] ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification |
| 383 | $count = 0; |
| 384 | |
| 385 | if ( $form_ids ) { |
| 386 | check_admin_referer( 'bulk-forms' ); |
| 387 | } |
| 388 | |
| 389 | switch ( $action ) { |
| 390 | case 'trash': |
| 391 | foreach ( $form_ids as $form_id ) { |
| 392 | if ( wp_trash_post( $form_id ) ) { |
| 393 | $count ++; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | add_settings_error( |
| 398 | 'bulk_action', |
| 399 | 'bulk_action', |
| 400 | /* translators: %d: number of forms */ |
| 401 | sprintf( _n( '%d form moved to the Trash.', '%d forms moved to the Trash.', $count, 'everest-forms' ), $count ), |
| 402 | 'updated' |
| 403 | ); |
| 404 | break; |
| 405 | case 'untrash': |
| 406 | foreach ( $form_ids as $form_id ) { |
| 407 | if ( wp_untrash_post( $form_id ) ) { |
| 408 | $count ++; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | add_settings_error( |
| 413 | 'bulk_action', |
| 414 | 'bulk_action', |
| 415 | /* translators: %d: number of forms */ |
| 416 | sprintf( _n( '%d form restored from the Trash.', '%d forms restored from the Trash.', $count, 'everest-forms' ), $count ), |
| 417 | 'updated' |
| 418 | ); |
| 419 | break; |
| 420 | case 'delete': |
| 421 | foreach ( $form_ids as $form_id ) { |
| 422 | if ( wp_delete_post( $form_id, true ) ) { |
| 423 | $count ++; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | add_settings_error( |
| 428 | 'bulk_action', |
| 429 | 'bulk_action', |
| 430 | /* translators: %d: number of forms */ |
| 431 | sprintf( _n( '%d form permanently deleted.', '%d forms permanently deleted.', $count, 'everest-forms' ), $count ), |
| 432 | 'updated' |
| 433 | ); |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Extra controls to be displayed between bulk actions and pagination. |
| 440 | * |
| 441 | * @param string $which The location of the extra table nav markup. |
| 442 | */ |
| 443 | protected function extra_tablenav( $which ) { |
| 444 | $num_posts = wp_count_posts( 'everest_form', 'readable' ); |
| 445 | |
| 446 | if ( $num_posts->trash && isset( $_GET['status'] ) && 'trash' === $_GET['status'] && current_user_can( 'delete_posts' ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 447 | echo '<div class="alignleft actions">'; |
| 448 | submit_button( __( 'Empty Trash', 'everest-forms' ), 'apply', 'delete_all', false ); |
| 449 | echo '</div>'; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Prepare table list items. |
| 455 | */ |
| 456 | public function prepare_items() { |
| 457 | $per_page = $this->get_items_per_page( 'evf_forms_per_page' ); |
| 458 | $current_page = $this->get_pagenum(); |
| 459 | |
| 460 | // Query args. |
| 461 | $args = array( |
| 462 | 'post_type' => 'everest_form', |
| 463 | 'posts_per_page' => $per_page, |
| 464 | 'ignore_sticky_posts' => true, |
| 465 | 'paged' => $current_page, |
| 466 | ); |
| 467 | |
| 468 | // Handle the status query. |
| 469 | if ( ! empty( $_REQUEST['status'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 470 | $args['post_status'] = sanitize_text_field( wp_unslash( $_REQUEST['status'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 471 | } |
| 472 | |
| 473 | // Handle the search query. |
| 474 | if ( ! empty( $_REQUEST['s'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 475 | $args['s'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 476 | } |
| 477 | |
| 478 | $args['orderby'] = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) : 'date_created'; // phpcs:ignore WordPress.Security.NonceVerification |
| 479 | $args['order'] = isset( $_REQUEST['order'] ) && 'ASC' === strtoupper( evf_clean( wp_unslash( $_REQUEST['order'] ) ) ) ? 'ASC' : 'DESC'; // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 480 | |
| 481 | // Get the forms. |
| 482 | $posts = new WP_Query( $args ); |
| 483 | $this->items = $posts->posts; |
| 484 | |
| 485 | // Set the pagination. |
| 486 | $this->set_pagination_args( |
| 487 | array( |
| 488 | 'total_items' => $posts->found_posts, |
| 489 | 'per_page' => $per_page, |
| 490 | 'total_pages' => $posts->max_num_pages, |
| 491 | ) |
| 492 | ); |
| 493 | } |
| 494 | } |
| 495 |