← All changes
|
jetpack_vendor/automattic/jetpack-forms/src/dashboard/class-dashboard.php
+51
-80
16.3-a.1
→
16.3
View file →
| @@ -7,9 +7,8 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Forms\Dashboard; |
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 11 | -use Automattic\Jetpack\Assets; | |
| 12 | 11 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 13 | 12 | use Automattic\Jetpack\Forms\ContactForm\Contact_Form; |
| 14 | 13 | use Automattic\Jetpack\Forms\ContactForm\Contact_Form_Plugin; |
| 15 | 14 | use Automattic\Jetpack\Tracking; |
| @@ -57,14 +56,10 @@ | ||
| 57 | 56 | // When no route path is specified, redirect to the default view |
| 58 | 57 | // so the client-side router doesn't need a catch-all root route. |
| 59 | 58 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 60 | 59 | if ( ! isset( $_GET['p'] ) ) { |
| 61 | - $default_tab = Contact_Form_Plugin::has_editor_feature_flag( 'central-form-management' ) | |
| 62 | - ? 'forms' | |
| 63 | - : 'inbox'; | |
| 60 | + wp_safe_redirect( self::get_forms_admin_url( self::get_default_tab() ) ); | |
| 64 | 61 | |
| 65 | - wp_safe_redirect( self::get_forms_admin_url( $default_tab ) ); | |
| 66 | - | |
| 67 | 62 | exit; |
| 68 | 63 | } |
| 69 | 64 | |
| 70 | 65 | // Register polyfills for WP < 7.0 (must run before enqueue). |
| @@ -99,8 +94,9 @@ | ||
| 99 | 94 | /** |
| 100 | 95 | * Script handle for the JS file we enqueue in the Feedback admin page. |
| 101 | 96 | * |
| 102 | 97 | * @var string |
| 98 | + * @deprecated 8.2.0 The legacy dashboard bundle was removed. | |
| 103 | 99 | */ |
| 104 | 100 | const SCRIPT_HANDLE = 'jp-forms-dashboard'; |
| 105 | 101 | |
| 106 | 102 | const ADMIN_SLUG = 'jetpack-forms-admin'; |
| @@ -114,8 +110,24 @@ | ||
| 114 | 110 | */ |
| 115 | 111 | const FORMS_WPBUILD_ADMIN_SLUG = 'jetpack-forms-responses-wp-admin'; |
| 116 | 112 | |
| 117 | 113 | /** |
| 114 | + * Cookie holding the top tab the user last chose. | |
| 115 | + * | |
| 116 | + * Written by src/dashboard/last-tab-cookie.ts; the two names have to stay in step. | |
| 117 | + */ | |
| 118 | + const LAST_TAB_COOKIE = 'jetpack_forms_last_tab'; | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Top tabs the dashboard can reopen on. | |
| 122 | + * | |
| 123 | + * Mirrored by TOP_TABS in src/dashboard/constants.ts. | |
| 124 | + * | |
| 125 | + * @var string[] | |
| 126 | + */ | |
| 127 | + private const TOP_TABS = array( 'forms', 'responses' ); | |
| 128 | + | |
| 129 | + /** | |
| 118 | 130 | * Priority for the dashboard menu. |
| 119 | 131 | * Needs to be high enough for us to be able to unregister the default edit.php menu item. |
| 120 | 132 | * |
| 121 | 133 | * @var int |
| @@ -204,38 +216,19 @@ | ||
| 204 | 216 | if ( ! self::is_jetpack_forms_admin_page() ) { |
| 205 | 217 | return; |
| 206 | 218 | } |
| 207 | 219 | |
| 208 | - // The wp-build (script-module) dashboard renders its own UI from build/pages/…, | |
| 209 | - // so the legacy SPA bundle is dead weight there. Only enqueue it on the legacy | |
| 210 | - // dashboard. The shared inline data below (connection initial state + REST | |
| 211 | - // preload) is instead attached to the always-present wp-api-fetch handle so the | |
| 212 | - // wp-build app still receives it. | |
| 213 | - if ( self::is_wp_build_dashboard_page() ) { | |
| 214 | - $inline_handle = 'wp-api-fetch'; | |
| 215 | - $preload_position = 'after'; | |
| 220 | + // Attach the shared inline data (connection initial state + REST preload) to | |
| 221 | + // wp-api-fetch, which is always on the page. | |
| 222 | + $inline_handle = 'wp-api-fetch'; | |
| 223 | + $preload_position = 'after'; | |
| 216 | 224 | |
| 217 | - // The i18n loader is registered on every admin page by jetpack-assets but | |
| 218 | - // only enqueued when depended on; the esbuild bundles don't pull it in. | |
| 219 | - // Enqueue it so the wp-build dashboard's init module can download its JS | |
| 220 | - // translation catalogs. | |
| 221 | - if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) { | |
| 222 | - wp_enqueue_script( 'wp-jp-i18n-loader' ); | |
| 223 | - } | |
| 224 | - } else { | |
| 225 | - $inline_handle = self::SCRIPT_HANDLE; | |
| 226 | - $preload_position = 'before'; | |
| 227 | - | |
| 228 | - Assets::register_script( | |
| 229 | - self::SCRIPT_HANDLE, | |
| 230 | - '../../dist/dashboard/jetpack-forms-dashboard.js', | |
| 231 | - __FILE__, | |
| 232 | - array( | |
| 233 | - 'in_footer' => true, | |
| 234 | - 'textdomain' => 'jetpack-forms', | |
| 235 | - 'enqueue' => true, | |
| 236 | - ) | |
| 237 | - ); | |
| 225 | + // The i18n loader is registered on every admin page by jetpack-assets but | |
| 226 | + // only enqueued when depended on; the esbuild bundles don't pull it in. | |
| 227 | + // Enqueue it so the wp-build dashboard's init module can download its JS | |
| 228 | + // translation catalogs. | |
| 229 | + if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) { | |
| 230 | + wp_enqueue_script( 'wp-jp-i18n-loader' ); | |
| 238 | 231 | } |
| 239 | 232 | |
| 240 | 233 | if ( Contact_Form_Plugin::can_use_analytics() ) { |
| 241 | 234 | Tracking::register_tracks_functions_scripts( true ); |
| @@ -390,9 +383,8 @@ | ||
| 390 | 383 | * |
| 391 | 384 | * The wp-build dashboard renders through a callback generated into `build/build.php`. |
| 392 | 385 | * That file is missing when the package ships without a complete build, and it is |
| 393 | 386 | * never loaded when a host application filters `jetpack_forms_load_wp_build` to false. |
| 394 | - * The legacy bundle is no fallback here: load_admin_scripts() skips it on this screen. | |
| 395 | 387 | * So report the problem instead of rendering a blank page. |
| 396 | 388 | * |
| 397 | 389 | * @since 7.25.0 |
| 398 | 390 | */ |
| @@ -575,11 +567,10 @@ | ||
| 575 | 567 | |
| 576 | 568 | /** |
| 577 | 569 | * Returns the URL of the standalone single response page for a given response. |
| 578 | 570 | * |
| 579 | - * The standalone page is a wp-build route (`/response/<id>`). The legacy | |
| 580 | - * dashboard has no equivalent, so it falls back to the responses list with the | |
| 581 | - * response selected — as does a missing/empty post ID. | |
| 571 | + * The standalone page is a wp-build route (`/response/<id>`). A missing or empty | |
| 572 | + * post ID falls back to the responses list. | |
| 582 | 573 | * |
| 583 | 574 | * @since 7.25.0 |
| 584 | 575 | * |
| 585 | 576 | * @param int|null $post_id Post ID of the response to open. |
| @@ -588,15 +579,31 @@ | ||
| 588 | 579 | */ |
| 589 | 580 | public static function get_single_response_admin_url( $post_id = null ) { |
| 590 | 581 | $post_id = ! empty( $post_id ) ? absint( $post_id ) : null; |
| 591 | 582 | |
| 592 | - // `get_forms_admin_url()` owns the URL scheme for both dashboards. The | |
| 593 | - // 'response' tab resolves to the standalone page on wp-build, and falls | |
| 594 | - // through to the responses list on legacy, which has no such route. | |
| 595 | 583 | return self::get_forms_admin_url( $post_id ? 'response' : 'inbox', $post_id ); |
| 596 | 584 | } |
| 597 | 585 | |
| 598 | 586 | /** |
| 587 | + * The tab the standalone Forms page opens on when the URL names no route. | |
| 588 | + * | |
| 589 | + * A cookie, not a user preference: this redirect runs before any script could read one. | |
| 590 | + * | |
| 591 | + * @return string Tab slug understood by get_forms_admin_url(). | |
| 592 | + */ | |
| 593 | + public static function get_default_tab() { | |
| 594 | + if ( ! Contact_Form_Plugin::has_editor_feature_flag( 'central-form-management' ) ) { | |
| 595 | + // The Forms tab is not rendered at all here, so honouring a stored 'forms' | |
| 596 | + // would strand the user on a tab they cannot leave. | |
| 597 | + return 'inbox'; | |
| 598 | + } | |
| 599 | + | |
| 600 | + $remembered = sanitize_key( wp_unslash( $_COOKIE[ self::LAST_TAB_COOKIE ] ?? '' ) ); | |
| 601 | + | |
| 602 | + return in_array( $remembered, self::TOP_TABS, true ) ? $remembered : 'forms'; | |
| 603 | + } | |
| 604 | + | |
| 605 | + /** | |
| 599 | 606 | * WP-Build path for the forms admin URL. |
| 600 | 607 | * |
| 601 | 608 | * @param string|null $tab Tab to open. |
| 602 | 609 | * @param int|null $post_id Post ID of response. |
| @@ -616,8 +623,9 @@ | ||
| 616 | 623 | 'inbox' => '/responses/inbox', |
| 617 | 624 | 'spam' => '/responses/spam', |
| 618 | 625 | 'trash' => '/responses/trash', |
| 619 | 626 | 'forms' => '/forms', |
| 627 | + 'responses' => '/responses/inbox', | |
| 620 | 628 | 'responses/inbox' => '/responses/inbox', |
| 621 | 629 | ); |
| 622 | 630 | |
| 623 | 631 | if ( $tab !== null && $tab !== '' && isset( $path_map[ $tab ] ) ) { |
| @@ -631,40 +639,8 @@ | ||
| 631 | 639 | return '/responses/inbox'; |
| 632 | 640 | } |
| 633 | 641 | |
| 634 | 642 | /** |
| 635 | - * Legacy (hash-based) URL suffix for the forms admin page. | |
| 636 | - * | |
| 637 | - * Unused since the legacy dashboard was retired: get_forms_admin_url() always builds | |
| 638 | - * the wp-build URL now. Private, so nothing outside this class ever called it, which | |
| 639 | - * is why it carries no deprecation notice — there is no audience for one. Goes with | |
| 640 | - * the rest of the legacy tree. | |
| 641 | - * | |
| 642 | - * @param string|null $tab Tab to open. | |
| 643 | - * @param int|null $post_id Post ID of response. | |
| 644 | - * @return string URL suffix (e.g. '#/responses?status=inbox&r=123', or '#/forms'). | |
| 645 | - */ | |
| 646 | - private static function get_forms_admin_suffix_legacy( $tab, $post_id ) { | |
| 647 | - $post_id = ! empty( $post_id ) ? absint( $post_id ) : null; | |
| 648 | - $valid_tabs = array( 'spam', 'inbox', 'trash' ); | |
| 649 | - $r_param = ! empty( $post_id ) ? '&r=' . $post_id : ''; | |
| 650 | - | |
| 651 | - if ( in_array( $tab, $valid_tabs, true ) ) { | |
| 652 | - return '#/responses?status=' . $tab . $r_param; | |
| 653 | - } | |
| 654 | - | |
| 655 | - if ( $tab === 'forms' ) { | |
| 656 | - return '#/forms'; | |
| 657 | - } | |
| 658 | - | |
| 659 | - if ( ! empty( $post_id ) ) { | |
| 660 | - return '#/responses?status=inbox' . $r_param; | |
| 661 | - } | |
| 662 | - | |
| 663 | - return ''; | |
| 664 | - } | |
| 665 | - | |
| 666 | - /** | |
| 667 | 643 | * Returns true if the current screen is the Jetpack Forms admin page. |
| 668 | 644 | * |
| 669 | 645 | * @return boolean |
| 670 | 646 | */ |
| @@ -678,14 +654,9 @@ | ||
| 678 | 654 | if ( ! $screen || ! isset( $screen->id ) ) { |
| 679 | 655 | return false; |
| 680 | 656 | } |
| 681 | 657 | |
| 682 | - $forms_admin_screens = array( | |
| 683 | - 'jetpack_page_' . self::ADMIN_SLUG, | |
| 684 | - 'jetpack_page_' . self::FORMS_WPBUILD_ADMIN_SLUG, | |
| 685 | - ); | |
| 686 | - | |
| 687 | - return in_array( $screen->id, $forms_admin_screens, true ); | |
| 658 | + return $screen->id === 'jetpack_page_' . self::FORMS_WPBUILD_ADMIN_SLUG; | |
| 688 | 659 | } |
| 689 | 660 | |
| 690 | 661 | /** |
| 691 | 662 | * Returns true if form notes feature is enabled. |