| @@ -6,11 +6,15 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace SRFM\Admin; |
| 9 | 9 | |
| 10 | -use SRFM\Inc\Traits\Get_Instance; | |
| 10 | +use SRFM\Admin\Views\Entries_List_Table; | |
| 11 | +use SRFM\Admin\Views\Single_Entry; | |
| 11 | 12 | use SRFM\Inc\AI_Form_Builder\AI_Helper; |
| 13 | +use SRFM\Inc\Database\Tables\Entries; | |
| 12 | 14 | use SRFM\Inc\Helper; |
| 15 | +use SRFM\Inc\Post_Types; | |
| 16 | +use SRFM\Inc\Traits\Get_Instance; | |
| 13 | 17 | |
| 14 | 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | 19 | exit; // Exit if accessed directly. |
| 16 | 20 | } |
| @@ -19,9 +23,8 @@ | ||
| 19 | 23 | * |
| 20 | 24 | * @since 0.0.1 |
| 21 | 25 | */ |
| 22 | 26 | class Admin { |
| 23 | - | |
| 24 | 27 | use Get_Instance; |
| 25 | 28 | |
| 26 | 29 | /** |
| 27 | 30 | * Class constructor. |
| @@ -42,8 +45,12 @@ | ||
| 42 | 45 | // this action is used to restrict Spectra's quick action bar on SureForms CPTS. |
| 43 | 46 | add_action( 'uag_enable_quick_action_sidebar', [ $this, 'restrict_spectra_quick_action_bar' ] ); |
| 44 | 47 | |
| 45 | 48 | add_action( 'current_screen', [ $this, 'enable_gutenberg_for_sureforms' ], 100 ); |
| 49 | + | |
| 50 | + // Handle entry actions. | |
| 51 | + add_action( 'admin_init', [ $this, 'handle_entry_actions' ] ); | |
| 52 | + add_action( 'admin_notices', [ Entries_List_Table::class, 'display_bulk_action_notice' ] ); | |
| 46 | 53 | } |
| 47 | 54 | |
| 48 | 55 | /** |
| 49 | 56 | * Enable Gutenberg for SureForms associated post types. |
| @@ -64,9 +71,8 @@ | ||
| 64 | 71 | add_filter( 'gutenberg_can_edit_post_type', '__return_true', 110 ); |
| 65 | 72 | } |
| 66 | 73 | } |
| 67 | 74 | |
| 68 | - | |
| 69 | 75 | /** |
| 70 | 76 | * Sureforms editor header styles. |
| 71 | 77 | * |
| 72 | 78 | * @since 0.0.1 |
| @@ -107,9 +113,10 @@ | ||
| 107 | 113 | __( 'SureForms', 'sureforms' ), |
| 108 | 114 | __( 'SureForms', 'sureforms' ), |
| 109 | 115 | 'edit_others_posts', |
| 110 | 116 | $menu_slug, |
| 111 | - function () {}, | |
| 117 | + static function () { | |
| 118 | + }, | |
| 112 | 119 | 'data:image/svg+xml;base64,' . base64_encode( $logo ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 113 | 120 | 30 |
| 114 | 121 | ); |
| 115 | 122 | |
| @@ -185,8 +192,17 @@ | ||
| 185 | 192 | 'add-new-form', |
| 186 | 193 | [ $this, 'add_new_form_callback' ], |
| 187 | 194 | 2 |
| 188 | 195 | ); |
| 196 | + add_submenu_page( | |
| 197 | + 'sureforms_menu', | |
| 198 | + __( 'Entries', 'sureforms' ), | |
| 199 | + __( 'Entries', 'sureforms' ), | |
| 200 | + 'edit_others_posts', | |
| 201 | + SRFM_ENTRIES, | |
| 202 | + [ $this, 'render_entries' ], | |
| 203 | + 3 | |
| 204 | + ); | |
| 189 | 205 | } |
| 190 | 206 | |
| 191 | 207 | /** |
| 192 | 208 | * Add new form mentu item callback. |
| @@ -198,8 +214,40 @@ | ||
| 198 | 214 | echo '<div id="srfm-add-new-form-container"></div>'; |
| 199 | 215 | } |
| 200 | 216 | |
| 201 | 217 | /** |
| 218 | + * Entries page callback. | |
| 219 | + * | |
| 220 | + * @since 0.0.13 | |
| 221 | + * @return void | |
| 222 | + */ | |
| 223 | + public function render_entries() { | |
| 224 | + // Render single entry view. | |
| 225 | + // Adding the phpcs ignore nonce verification as no database operations are performed in this function, it is used to display the single entry view. | |
| 226 | + if ( isset( $_GET['entry_id'] ) && is_numeric( $_GET['entry_id'] ) && isset( $_GET['view'] ) && 'details' === $_GET['view'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 227 | + $single_entry_view = new Single_Entry(); | |
| 228 | + $single_entry_view->render(); | |
| 229 | + return; | |
| 230 | + } | |
| 231 | + | |
| 232 | + // Render all entries view. | |
| 233 | + $entries_table = new Entries_List_Table(); | |
| 234 | + $entries_table->prepare_items(); | |
| 235 | + echo '<div class="wrap"><h1 class="wp-heading-inline">Entries</h1>'; | |
| 236 | + if ( empty( $entries_table->all_entries_count ) && empty( $entries_table->trash_entries_count ) ) { | |
| 237 | + $instance = Post_Types::get_instance(); | |
| 238 | + $instance->sureforms_render_blank_state( SRFM_ENTRIES ); | |
| 239 | + $instance->get_blank_state_styles(); | |
| 240 | + return; | |
| 241 | + } | |
| 242 | + echo '<form method="get">'; | |
| 243 | + echo '<input type="hidden" name="page" value="sureforms_entries">'; | |
| 244 | + $entries_table->display(); | |
| 245 | + echo '</form>'; | |
| 246 | + echo '</div>'; | |
| 247 | + } | |
| 248 | + | |
| 249 | + /** | |
| 202 | 250 | * Adds a settings link to the plugin action links on the plugins page. |
| 203 | 251 | * |
| 204 | 252 | * @param array $links An array of plugin action links. |
| 205 | 253 | * @param string $file The plugin file path. |
| @@ -293,13 +341,8 @@ | ||
| 293 | 341 | $breadcrumbs[] = [ |
| 294 | 342 | 'title' => 'Forms', |
| 295 | 343 | 'link' => '', |
| 296 | 344 | ]; |
| 297 | - } elseif ( $current_screen && 'sureforms_entry' === $current_screen->post_type ) { | |
| 298 | - $breadcrumbs[] = [ | |
| 299 | - 'title' => 'Entries', | |
| 300 | - 'link' => '', | |
| 301 | - ]; | |
| 302 | 345 | } else { |
| 303 | 346 | $breadcrumbs[] = [ |
| 304 | 347 | 'title' => '', |
| 305 | 348 | 'link' => '', |
| @@ -309,9 +352,8 @@ | ||
| 309 | 352 | |
| 310 | 353 | return $breadcrumbs; |
| 311 | 354 | } |
| 312 | 355 | |
| 313 | - | |
| 314 | 356 | /** |
| 315 | 357 | * Enqueue Admin Scripts. |
| 316 | 358 | * |
| 317 | 359 | * @return void |
| @@ -337,8 +379,9 @@ | ||
| 337 | 379 | 'plugin_version' => SRFM_VER, |
| 338 | 380 | 'global_settings_nonce' => current_user_can( 'manage_options' ) ? wp_create_nonce( 'wp_rest' ) : '', |
| 339 | 381 | 'is_pro_active' => defined( 'SRFM_PRO_VER' ), |
| 340 | 382 | 'pro_plugin_version' => defined( 'SRFM_PRO_VER' ) ? SRFM_PRO_VER : '', |
| 383 | + 'pro_plugin_name' => defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_PRODUCT' ) ? SRFM_PRO_PRODUCT : 'SureForms Pro', | |
| 341 | 384 | 'sureforms_pricing_page' => $this->get_sureforms_website_url( 'pricing' ), |
| 342 | 385 | 'field_spacing_vars' => Helper::get_css_vars(), |
| 343 | 386 | ]; |
| 344 | 387 | |
| @@ -346,11 +389,9 @@ | ||
| 346 | 389 | $license_active = \SRFM_PRO\Admin\Licensing::is_license_active(); |
| 347 | 390 | $localization_data['is_license_active'] = $license_active; |
| 348 | 391 | } |
| 349 | 392 | |
| 350 | - if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type || 'toplevel_page_sureforms_menu' === $current_screen->base || SRFM_ENTRIES_POST_TYPE === $current_screen->post_type | |
| 351 | - || 'sureforms_page_sureforms_form_settings' === $current_screen->id | |
| 352 | - ) { | |
| 393 | + if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type || 'toplevel_page_sureforms_menu' === $current_screen->base || 'sureforms_page_sureforms_form_settings' === $current_screen->id || 'sureforms_page_' . SRFM_ENTRIES === $current_screen->id ) { | |
| 353 | 394 | $asset_handle = '-dashboard'; |
| 354 | 395 | |
| 355 | 396 | wp_enqueue_style( SRFM_SLUG . $asset_handle . '-font', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap', [], SRFM_VER ); |
| 356 | 397 | |
| @@ -381,12 +422,19 @@ | ||
| 381 | 422 | if ( 'sureforms_page_sureforms_form_settings' === $current_screen->id ) { |
| 382 | 423 | wp_enqueue_style( SRFM_SLUG . '-settings', $css_uri . 'backend/settings' . $file_prefix . '.css', [], SRFM_VER ); |
| 383 | 424 | } |
| 384 | 425 | |
| 426 | + // Enqueue styles for the entries page. | |
| 427 | + if ( 'sureforms_page_' . SRFM_ENTRIES === $current_screen->id ) { | |
| 428 | + $asset_handle = '-entries'; | |
| 429 | + wp_enqueue_style( SRFM_SLUG . $asset_handle, $css_uri . 'backend/entries' . $file_prefix . '.css', [], SRFM_VER ); | |
| 430 | + wp_enqueue_script( SRFM_SLUG . $asset_handle, SRFM_URL . 'assets/build/entries.js', $script_info['dependencies'], SRFM_VER, true ); | |
| 431 | + } | |
| 432 | + | |
| 385 | 433 | // Admin Submenu Styles. |
| 386 | 434 | wp_enqueue_style( SRFM_SLUG . '-admin', $css_uri . 'backend/admin' . $file_prefix . '.css', [], SRFM_VER ); |
| 387 | 435 | |
| 388 | - if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id || 'edit-' . SRFM_ENTRIES_POST_TYPE === $current_screen->id ) { | |
| 436 | + if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id ) { | |
| 389 | 437 | $asset_handle = 'page_header'; |
| 390 | 438 | |
| 391 | 439 | $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php'; |
| 392 | 440 | $script_info = file_exists( $script_asset_path ) |
| @@ -427,9 +475,9 @@ | ||
| 427 | 475 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 428 | 476 | 'srfm_export_nonce' => wp_create_nonce( 'export_form_nonce' ), |
| 429 | 477 | 'site_url' => get_site_url(), |
| 430 | 478 | 'srfm_import_endpoint' => '/wp-json/sureforms/v1/sureforms_import', |
| 431 | - 'import_form_nonce' => ( current_user_can( 'edit_posts' ) ) ? wp_create_nonce( 'wp_rest' ) : '', | |
| 479 | + 'import_form_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '', | |
| 432 | 480 | ] |
| 433 | 481 | ); |
| 434 | 482 | |
| 435 | 483 | wp_enqueue_script( SRFM_SLUG . '-backend', $js_uri . 'backend' . $file_prefix . '.js', [], SRFM_VER, true ); |
| @@ -527,9 +575,9 @@ | ||
| 527 | 575 | * Enqueuing SureTriggers Integration script. |
| 528 | 576 | * This script loads suretriggers iframe in Intergations tab. |
| 529 | 577 | */ |
| 530 | 578 | if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type ) { |
| 531 | - wp_enqueue_script( SRFM_SLUG . '-suretriggers-integration', SRFM_SURETRIGGERS_INTERGATION_BASE_URL . 'js/v2/embed.js', [], SRFM_VER, true ); | |
| 579 | + wp_enqueue_script( SRFM_SLUG . '-suretriggers-integration', SRFM_SURETRIGGERS_INTEGRATION_BASE_URL . 'js/v2/embed.js', [], SRFM_VER, true ); | |
| 532 | 580 | } |
| 533 | 581 | } |
| 534 | 582 | |
| 535 | 583 | /** |
| @@ -542,9 +590,8 @@ | ||
| 542 | 590 | $theme_builder_class = isset( $_GET['page'] ) && 'add-new-form' === $_GET['page'] ? 'srfm-template-picker' : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Fetching a $_GET value, no nonce available to validate. |
| 543 | 591 | $classes .= ' ' . $theme_builder_class . ' '; |
| 544 | 592 | |
| 545 | 593 | return $classes; |
| 546 | - | |
| 547 | 594 | } |
| 548 | 595 | |
| 549 | 596 | /** |
| 550 | 597 | * Disable spectra's quick action bar in sureforms CPT. |
| @@ -576,5 +623,42 @@ | ||
| 576 | 623 | } |
| 577 | 624 | |
| 578 | 625 | return esc_url( $url ); |
| 579 | 626 | } |
| 627 | + | |
| 628 | + // Entries methods. | |
| 629 | + | |
| 630 | + /** | |
| 631 | + * Handle entry actions. | |
| 632 | + * | |
| 633 | + * @since 0.0.13 | |
| 634 | + * @return void | |
| 635 | + */ | |
| 636 | + public function handle_entry_actions() { | |
| 637 | + if ( isset( $_GET['entry'] ) && isset( $_GET['action'] ) ) { | |
| 638 | + Entries_List_Table::process_bulk_actions(); | |
| 639 | + return; | |
| 640 | + } | |
| 641 | + if ( ! isset( $_GET['page'] ) || SRFM_ENTRIES !== $_GET['page'] ) { | |
| 642 | + return; | |
| 643 | + } | |
| 644 | + if ( ! isset( $_GET['entry_id'] ) || ! isset( $_GET['action'] ) ) { | |
| 645 | + return; | |
| 646 | + } | |
| 647 | + if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'srfm_entries_action' ) ) { | |
| 648 | + wp_die( esc_html__( 'Nonce verification failed.', 'sureforms' ) ); | |
| 649 | + } | |
| 650 | + $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; | |
| 651 | + $entry_id = Helper::get_integer_value( sanitize_text_field( wp_unslash( $_GET['entry_id'] ) ) ); | |
| 652 | + $view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : ''; | |
| 653 | + if ( $entry_id > 0 ) { | |
| 654 | + if ( 'read' === $action && 'details' === $view ) { | |
| 655 | + $entry_status = Entries::get( $entry_id )['status']; | |
| 656 | + if ( 'trash' === $entry_status ) { | |
| 657 | + wp_die( esc_html__( 'You cannot view this entry because it is in trash.', 'sureforms' ) ); | |
| 658 | + } | |
| 659 | + } | |
| 660 | + Entries_List_Table::handle_entry_status( $entry_id, $action, $view ); | |
| 661 | + } | |
| 662 | + } | |
| 663 | + | |
| 580 | 664 | } |