From 9538bf5bdcba60f84aa39d94e99b543e880e6312 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Tue, 13 Jan 2026 13:03:04 -0700 Subject: [PATCH 01/16] Forms: add forms dataviews list --- .../forms/src/dashboard/class-dashboard.php | 10 ++ .../src/dashboard/class-forms-endpoint.php | 131 +++++++++++++++ .../forms/src/dashboard/forms/index.tsx | 152 ++++++++++++++++-- .../forms/src/dashboard/forms/style.scss | 15 ++ .../forms/src/dashboard/forms/views.ts | 69 ++++++++ .../src/dashboard/hooks/use-forms-data.ts | 85 ++++++++++ .../forms/src/dashboard/inbox/style.scss | 2 +- 7 files changed, 453 insertions(+), 11 deletions(-) create mode 100644 projects/packages/forms/src/dashboard/class-forms-endpoint.php create mode 100644 projects/packages/forms/src/dashboard/forms/style.scss create mode 100644 projects/packages/forms/src/dashboard/forms/views.ts create mode 100644 projects/packages/forms/src/dashboard/hooks/use-forms-data.ts diff --git a/projects/packages/forms/src/dashboard/class-dashboard.php b/projects/packages/forms/src/dashboard/class-dashboard.php index 48ba05f9f2b5..ec0d596f2742 100644 --- a/projects/packages/forms/src/dashboard/class-dashboard.php +++ b/projects/packages/forms/src/dashboard/class-dashboard.php @@ -69,6 +69,8 @@ public function init() { self::load_wp_build(); } + Forms_Endpoint::init(); + add_action( 'admin_menu', array( $this, 'add_new_admin_submenu' ), self::MENU_PRIORITY ); if ( $is_wp_build_enabled ) { @@ -131,6 +133,13 @@ public function load_admin_scripts() { ); $filters_path = '/wp/v2/feedback/filters'; $filters_locale_path = \add_query_arg( array( '_locale' => 'user' ), $filters_path ); + $initial_forms_path = \add_query_arg( + array( + 'page' => 1, + 'per_page' => 20, + ), + '/jetpack-forms/v1/forms' + ); $preload_paths = array( '/wp/v2/types?context=view', '/wp/v2/feedback/config', @@ -140,6 +149,7 @@ public function load_admin_scripts() { $filters_locale_path, $initial_responses_path, $initial_responses_locale_path, + $initial_forms_path, ); $preload_data_raw = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); diff --git a/projects/packages/forms/src/dashboard/class-forms-endpoint.php b/projects/packages/forms/src/dashboard/class-forms-endpoint.php new file mode 100644 index 000000000000..75449511b459 --- /dev/null +++ b/projects/packages/forms/src/dashboard/class-forms-endpoint.php @@ -0,0 +1,131 @@ + \WP_REST_Server::READABLE, + 'callback' => array( __CLASS__, 'get_forms' ), + 'permission_callback' => array( __CLASS__, 'permissions_check' ), + 'args' => array( + 'page' => array( + 'type' => 'integer', + 'default' => 1, + 'sanitize_callback' => 'absint', + ), + 'per_page' => array( + 'type' => 'integer', + 'default' => 20, + 'sanitize_callback' => 'absint', + ), + 'search' => array( + 'type' => 'string', + 'required' => false, + 'sanitize_callback' => 'sanitize_text_field', + ), + ), + ) + ); + } + + /** + * Permissions check. + * + * @return bool + */ + public static function permissions_check() { + return current_user_can( 'edit_pages' ); + } + + /** + * Returns a paginated list of reusable forms (`jetpack_form` posts), including entry counts. + * + * @param WP_REST_Request $request Request. + * @return \WP_REST_Response + */ + public static function get_forms( WP_REST_Request $request ) { + $page = max( 1, (int) $request->get_param( 'page' ) ); + $per_page = max( 1, min( 100, (int) $request->get_param( 'per_page' ) ) ); + $search = $request->get_param( 'search' ); + + $args = array( + 'post_type' => Contact_Form::POST_TYPE, + 'post_status' => array( 'publish', 'draft' ), + 'posts_per_page' => $per_page, + 'paged' => $page, + 'orderby' => 'modified', + 'order' => 'DESC', + ); + + if ( ! empty( $search ) ) { + $args['s'] = $search; + } + + $query = new WP_Query( $args ); + $items = array(); + + foreach ( $query->posts as $post ) { + // Count entries associated with this form via feedback.post_parent. + $entries_query = new WP_Query( + array( + 'post_type' => Feedback::POST_TYPE, + 'post_status' => array( 'publish', 'draft', 'spam', 'trash', 'jp-temp-feedback' ), + 'posts_per_page' => 1, + 'fields' => 'ids', + 'post_parent' => $post->ID, + ) + ); + + $items[] = array( + 'id' => $post->ID, + 'title' => get_the_title( $post ), + 'status' => $post->post_status, + 'modified' => $post->post_modified, + 'entriesCount' => (int) $entries_query->found_posts, + 'editUrl' => get_edit_post_link( $post->ID, 'raw' ), + ); + } + + return rest_ensure_response( + array( + 'items' => $items, + 'totalItems' => (int) $query->found_posts, + 'totalPages' => (int) $query->max_num_pages, + 'currentPage' => $page, + ) + ); + } +} diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index af283e90b357..5068823a2a7e 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -2,33 +2,127 @@ * External dependencies */ import { JetpackLogo } from '@automattic/jetpack-components'; -import { useEffect } from '@wordpress/element'; +import { DataViews } from '@wordpress/dataviews/wp'; +import { dateI18n, getSettings as getDateSettings } from '@wordpress/date'; +import { useCallback, useEffect, useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { useNavigate } from 'react-router'; /** * Internal dependencies */ import useConfigValue from '../../hooks/use-config-value.ts'; +import CreateFormButton from '../components/create-form-button/index.tsx'; import FormsResponsesTabs from '../components/forms-responses-tabs/index.tsx'; import Page from '../components/page/index.tsx'; +import useFormsData from '../hooks/use-forms-data.ts'; +import { defaultLayouts, useView } from './views.ts'; +import './style.scss'; +import type { FormListItem } from '../hooks/use-forms-data.ts'; /** - * Forms dashboard "Forms" route placeholder. + * Forms dashboard "Forms" route. * - * @return {JSX.Element|null} The placeholder page, or null when redirecting. + * @return {JSX.Element|null} The Forms list page, or null when redirecting. */ export default function FormsDashboardForms(): JSX.Element | null { const navigate = useNavigate(); const isCentralFormManagementEnabled = useConfigValue( 'isCentralFormManagementEnabled' ); + const isCentralFormManagementDisabled = isCentralFormManagementEnabled === false; + + const dateSettings = getDateSettings(); + const [ view, setView ] = useView(); + const { records, isLoading, totalItems, totalPages } = useFormsData( + view.page, + view.perPage, + view.search + ); useEffect( () => { - if ( isCentralFormManagementEnabled === false ) { + if ( isCentralFormManagementDisabled ) { navigate( '/responses', { replace: true } ); } - }, [ isCentralFormManagementEnabled, navigate ] ); + }, [ isCentralFormManagementDisabled, navigate ] ); + + const statusLabel = useCallback( ( status: string ) => { + switch ( status ) { + case 'publish': + return __( 'Published', 'jetpack-forms' ); + case 'draft': + return __( 'Draft', 'jetpack-forms' ); + default: + return status; + } + }, [] ); + + const fields = useMemo( + () => [ + { + id: 'title', + label: __( 'Form name', 'jetpack-forms' ), + getValue: ( { item }: { item: FormListItem } ) => item.title, + render: ( { item }: { item: FormListItem } ) => + item.title || __( '(no title)', 'jetpack-forms' ), + enableSorting: false, + enableHiding: false, + }, + { + id: 'entries', + label: __( 'Entries', 'jetpack-forms' ), + getValue: ( { item }: { item: FormListItem } ) => item.entriesCount ?? 0, + render: ( { item }: { item: FormListItem } ) => item.entriesCount ?? 0, + enableSorting: false, + }, + { + id: 'status', + label: __( 'Status', 'jetpack-forms' ), + getValue: ( { item }: { item: FormListItem } ) => item.status, + render: ( { item }: { item: FormListItem } ) => statusLabel( item.status ), + enableSorting: false, + }, + { + id: 'modified', + label: __( 'Last updated', 'jetpack-forms' ), + render: ( { item }: { item: FormListItem } ) => + dateI18n( dateSettings.formats.datetime, item.modified ), + enableSorting: false, + }, + { + id: 'actions', + label: __( 'Actions', 'jetpack-forms' ), + enableSorting: false, + render: ( { item }: { item: FormListItem } ) => { + const fallbackEditUrl = `post.php?post=${ item.id }&action=edit&post_type=jetpack_form`; + const editUrl = item.editUrl || fallbackEditUrl; + + return ( +
+ + { __( 'Edit form', 'jetpack-forms' ) } + +
+ ); + }, + }, + ], + [ dateSettings.formats.datetime, statusLabel ] + ); - // Avoid rendering the placeholder if the flag is off (we'll redirect). - if ( isCentralFormManagementEnabled === false ) { + const paginationInfo = useMemo( + () => ( { totalItems, totalPages } ), + [ totalItems, totalPages ] + ); + + const onChangeView = useCallback( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ( newView: any ) => setView( newView ), + [ setView ] + ); + + const headerActions = useMemo( () => [ ], [] ); + const getItemId = useCallback( ( item: FormListItem ) => String( item.id ), [] ); + + // Avoid rendering if the flag is off (we'll redirect). + if ( isCentralFormManagementDisabled ) { return null; } @@ -43,11 +137,49 @@ export default function FormsDashboardForms(): JSX.Element | null { } subTitle={ __( 'View and manage all your forms in one place.', 'jetpack-forms' ) } tabs={ } + actions={ headerActions } hasPadding={ false } > -

- { __( 'Forms will appear here', 'jetpack-forms' ) } -

+ +

+ { __( "You're set up. No forms yet.", 'jetpack-forms' ) } +

+ + + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + view={ view as any } + onChangeView={ onChangeView } + getItemId={ getItemId } + defaultLayouts={ defaultLayouts } + > +
+
+ +
+
+ + + +
+
+
+ + +
+
); diff --git a/projects/packages/forms/src/dashboard/forms/style.scss b/projects/packages/forms/src/dashboard/forms/style.scss new file mode 100644 index 000000000000..5c4d8d1886f0 --- /dev/null +++ b/projects/packages/forms/src/dashboard/forms/style.scss @@ -0,0 +1,15 @@ +@use "@wordpress/base-styles/breakpoints"; +@use "@wordpress/base-styles/variables"; + +.jp-forms-filters-bar { + border-bottom: 1px solid var(--wpds-color-stroke-surface-neutral-weak); + padding-inline: variables.$grid-unit * 2.5; + display: flex; + align-items: center; + justify-content: space-between; + flex-wrap: wrap; + gap: variables.$grid-unit * 1.25; + background: var(--wpds-color-bg-surface-neutral-strong, #fff); + padding: 12px 16px; +} + diff --git a/projects/packages/forms/src/dashboard/forms/views.ts b/projects/packages/forms/src/dashboard/forms/views.ts new file mode 100644 index 000000000000..a2f35f43b4ab --- /dev/null +++ b/projects/packages/forms/src/dashboard/forms/views.ts @@ -0,0 +1,69 @@ +import { useCallback, useEffect, useState } from '@wordpress/element'; +import { useSearchParams } from 'react-router'; + +const LAYOUT_TABLE = 'table'; + +export const defaultView = { + type: LAYOUT_TABLE, + search: '', + filters: [], + page: 1, + perPage: 20, + fields: [ 'title', 'entries', 'status', 'modified', 'actions' ], +}; + +export const defaultLayouts = { + [ LAYOUT_TABLE ]: {}, +}; + +/** + * Manage the DataViews view state for the Forms list and keep `search` in sync with the URL. + * + * @return {[Object, Function]} The current DataViews view and a setter that updates the URL. + */ +export function useView() { + const [ searchParams, setSearchParams ] = useSearchParams(); + const urlSearch = searchParams.get( 'search' ); + + const [ view, setView ] = useState( () => ( { + ...defaultView, + search: urlSearch ?? '', + } ) ); + + const setViewWithUrlUpdate = useCallback( + newView => { + setView( newView ); + if ( newView.search !== urlSearch ) { + setSearchParams( previousSearchParams => { + const _searchParams = new URLSearchParams( previousSearchParams ); + if ( newView.search ) { + _searchParams.set( 'search', newView.search ); + } else { + _searchParams.delete( 'search' ); + } + return _searchParams; + } ); + } + }, + [ setSearchParams, urlSearch ] + ); + + const onUrlSearchChange = useCallback( () => { + setView( previousView => { + const newValue = urlSearch ?? ''; + if ( newValue === previousView.search ) { + return previousView; + } + return { + ...previousView, + search: newValue, + }; + } ); + }, [ urlSearch ] ); + + useEffect( () => { + onUrlSearchChange(); + }, [ onUrlSearchChange, urlSearch ] ); + + return [ view, setViewWithUrlUpdate ] as const; +} diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts new file mode 100644 index 000000000000..d8eee02ee76b --- /dev/null +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -0,0 +1,85 @@ +import apiFetch from '@wordpress/api-fetch'; +import { useEffect, useState } from '@wordpress/element'; + +export type FormListItem = { + id: number; + title: string; + status: string; + modified: string; + entriesCount: number; + editUrl?: string; +}; + +type FormsListResponse = { + items: FormListItem[]; + totalItems: number; + totalPages: number; + currentPage: number; +}; + +type UseFormsDataReturn = { + records: FormListItem[]; + isLoading: boolean; + totalItems: number; + totalPages: number; +}; + +/** + * Fetch Forms list records for the Forms dashboard table. + * + * @param page - Current page number. + * @param perPage - Items per page. + * @param search - Search term. + * + * @return {UseFormsDataReturn} Forms list data for the current query. + */ +export default function useFormsData( + page: number, + perPage: number, + search: string +): UseFormsDataReturn { + const [ records, setRecords ] = useState< FormListItem[] >( [] ); + const [ isLoading, setIsLoading ] = useState( false ); + const [ totalItems, setTotalItems ] = useState( 0 ); + const [ totalPages, setTotalPages ] = useState( 0 ); + + useEffect( () => { + let isMounted = true; + const params = new URLSearchParams(); + params.set( 'page', String( page ) ); + params.set( 'per_page', String( perPage ) ); + if ( search ) { + params.set( 'search', search ); + } + + setIsLoading( true ); + + apiFetch< FormsListResponse >( { + path: `/jetpack-forms/v1/forms?${ params.toString() }`, + } ) + .then( response => { + if ( ! isMounted ) { + return; + } + setRecords( response.items || [] ); + setTotalItems( response.totalItems || 0 ); + setTotalPages( response.totalPages || 0 ); + setIsLoading( false ); + } ) + .catch( () => { + if ( ! isMounted ) { + return; + } + setRecords( [] ); + setTotalItems( 0 ); + setTotalPages( 0 ); + setIsLoading( false ); + } ); + + return () => { + isMounted = false; + }; + }, [ page, perPage, search ] ); + + return { records, isLoading, totalItems, totalPages }; +} diff --git a/projects/packages/forms/src/dashboard/inbox/style.scss b/projects/packages/forms/src/dashboard/inbox/style.scss index 34fb385efa96..4e1bfed4af86 100644 --- a/projects/packages/forms/src/dashboard/inbox/style.scss +++ b/projects/packages/forms/src/dashboard/inbox/style.scss @@ -66,13 +66,13 @@ flex-wrap: wrap; gap: variables.$grid-unit * 1.25; background: var(--wpds-color-bg-surface-neutral-strong, #fff); + padding: 12px 16px; &__chips { display: flex; flex: 1 1 auto; min-width: 0; align-items: center; - padding: variables.$grid-unit * 1.5 0; gap: 8px; } From 9b9b1733add3412d4dbce70e169e5bdcf22f63e8 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Tue, 13 Jan 2026 13:04:10 -0700 Subject: [PATCH 02/16] changelog --- .../packages/forms/changelog/add-forms-list-dataviews-table | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/packages/forms/changelog/add-forms-list-dataviews-table diff --git a/projects/packages/forms/changelog/add-forms-list-dataviews-table b/projects/packages/forms/changelog/add-forms-list-dataviews-table new file mode 100644 index 000000000000..ad5d6876f24b --- /dev/null +++ b/projects/packages/forms/changelog/add-forms-list-dataviews-table @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Forms: add reusable forms list to dashboard. From d5d13a7ecb0ba48454d89839e7db11716cc65c23 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Wed, 14 Jan 2026 12:14:52 -0700 Subject: [PATCH 03/16] Move form actions to three dots --- .../forms/src/dashboard/forms/index.tsx | 36 ++++++++++++------- .../forms/src/dashboard/forms/views.ts | 2 +- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index 5068823a2a7e..02aed4126c6d 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -2,10 +2,12 @@ * External dependencies */ import { JetpackLogo } from '@automattic/jetpack-components'; +import { Icon } from '@wordpress/components'; import { DataViews } from '@wordpress/dataviews/wp'; import { dateI18n, getSettings as getDateSettings } from '@wordpress/date'; import { useCallback, useEffect, useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; +import { backup } from '@wordpress/icons'; import { useNavigate } from 'react-router'; /** * Internal dependencies @@ -86,25 +88,32 @@ export default function FormsDashboardForms(): JSX.Element | null { dateI18n( dateSettings.formats.datetime, item.modified ), enableSorting: false, }, + ], + [ dateSettings.formats.datetime, statusLabel ] + ); + + const actions = useMemo( + () => [ { - id: 'actions', - label: __( 'Actions', 'jetpack-forms' ), - enableSorting: false, - render: ( { item }: { item: FormListItem } ) => { + id: 'edit-form', + isPrimary: false, + icon: , + label: __( 'Edit form', 'jetpack-forms' ), + supportsBulk: false, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async callback( items: any[] ) { + const [ item ] = items as FormListItem[]; + if ( ! item ) { + return; + } const fallbackEditUrl = `post.php?post=${ item.id }&action=edit&post_type=jetpack_form`; const editUrl = item.editUrl || fallbackEditUrl; - - return ( - - ); + const url = new URL( editUrl, window.location.origin ); + window.location.href = url.toString(); }, }, ], - [ dateSettings.formats.datetime, statusLabel ] + [] ); const paginationInfo = useMemo( @@ -143,6 +152,7 @@ export default function FormsDashboardForms(): JSX.Element | null { Date: Wed, 14 Jan 2026 12:31:45 -0700 Subject: [PATCH 04/16] Only preload if feature flag is on --- .../forms/src/dashboard/class-dashboard.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/projects/packages/forms/src/dashboard/class-dashboard.php b/projects/packages/forms/src/dashboard/class-dashboard.php index ec0d596f2742..e75fd35f3616 100644 --- a/projects/packages/forms/src/dashboard/class-dashboard.php +++ b/projects/packages/forms/src/dashboard/class-dashboard.php @@ -133,13 +133,6 @@ public function load_admin_scripts() { ); $filters_path = '/wp/v2/feedback/filters'; $filters_locale_path = \add_query_arg( array( '_locale' => 'user' ), $filters_path ); - $initial_forms_path = \add_query_arg( - array( - 'page' => 1, - 'per_page' => 20, - ), - '/jetpack-forms/v1/forms' - ); $preload_paths = array( '/wp/v2/types?context=view', '/wp/v2/feedback/config', @@ -149,9 +142,20 @@ public function load_admin_scripts() { $filters_locale_path, $initial_responses_path, $initial_responses_locale_path, - $initial_forms_path, ); - $preload_data_raw = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); + + // Only preload the Forms list endpoint when centralized form management is enabled. + if ( Contact_Form_Plugin::has_editor_feature_flag( 'central-form-management' ) ) { + $initial_forms_path = \add_query_arg( + array( + 'page' => 1, + 'per_page' => 20, + ), + '/jetpack-forms/v1/forms' + ); + $preload_paths[] = $initial_forms_path; + } + $preload_data_raw = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); // Normalize keys to match what apiFetch will request (without domain). $preload_data = array(); From 145654761a8cd64e6772b25350ccb631d35f8044 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Wed, 14 Jan 2026 12:40:10 -0700 Subject: [PATCH 05/16] Fetch and handle all post statuses --- .../packages/forms/src/dashboard/class-forms-endpoint.php | 3 ++- projects/packages/forms/src/dashboard/forms/index.tsx | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/projects/packages/forms/src/dashboard/class-forms-endpoint.php b/projects/packages/forms/src/dashboard/class-forms-endpoint.php index 75449511b459..27e442f54eb6 100644 --- a/projects/packages/forms/src/dashboard/class-forms-endpoint.php +++ b/projects/packages/forms/src/dashboard/class-forms-endpoint.php @@ -83,7 +83,8 @@ public static function get_forms( WP_REST_Request $request ) { $args = array( 'post_type' => Contact_Form::POST_TYPE, - 'post_status' => array( 'publish', 'draft' ), + // Include all "normal" statuses but exclude trash (handled in a future PR). + 'post_status' => array( 'publish', 'draft', 'pending', 'future', 'private' ), 'posts_per_page' => $per_page, 'paged' => $page, 'orderby' => 'modified', diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index 02aed4126c6d..8a5f0c456e7c 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -51,6 +51,12 @@ export default function FormsDashboardForms(): JSX.Element | null { return __( 'Published', 'jetpack-forms' ); case 'draft': return __( 'Draft', 'jetpack-forms' ); + case 'pending': + return __( 'Pending review', 'jetpack-forms' ); + case 'future': + return __( 'Scheduled', 'jetpack-forms' ); + case 'private': + return __( 'Private', 'jetpack-forms' ); default: return status; } From 4c811293c43eb47527656ee7cd1ddd4ab4eea1cd Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 10:05:01 -0700 Subject: [PATCH 06/16] Use existing jetpack_form endpoint --- .../class-jetpack-form-endpoint.php | 117 ++++++++++++++++ .../forms/src/dashboard/class-dashboard.php | 19 +-- .../src/dashboard/class-forms-endpoint.php | 132 ------------------ .../src/dashboard/hooks/use-forms-data.ts | 78 ++++++++--- 4 files changed, 187 insertions(+), 159 deletions(-) delete mode 100644 projects/packages/forms/src/dashboard/class-forms-endpoint.php diff --git a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php index 21552d641c58..8de42c248831 100644 --- a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php @@ -7,10 +7,19 @@ namespace Automattic\Jetpack\Forms\ContactForm; +use WP_REST_Request; + /** * REST endpoint for the jetpack_form custom post type. */ class Jetpack_Form_Endpoint extends \WP_REST_Posts_Controller { + /** + * Cached map of form_id => entriesCount for the current request. + * + * @var array|null + */ + private $entries_count_by_form_id = null; + /** * Constructor. */ @@ -18,6 +27,114 @@ public function __construct() { parent::__construct( Contact_Form::POST_TYPE ); } + /** + * Add opt-in dashboard fields. + * + * @return array + */ + public function get_collection_params() { + $params = parent::get_collection_params(); + + // Note: We do not use the built-in WP REST "context" param for this, because it's validated + // against core values (view/embed/edit). This param is for Jetpack Forms dashboard usage only. + $params['jetpack_forms_context'] = array( + 'description' => __( 'Request context for Jetpack Forms. Use "dashboard" to include dashboard-only fields.', 'jetpack-forms' ), + 'type' => 'string', + 'default' => '', + 'enum' => array( '', 'dashboard' ), + 'sanitize_callback' => 'sanitize_key', + ); + + return $params; + } + + /** + * Return a collection of forms. + * + * We override this to compute dashboard aggregate fields in a single pass. + * + * @param WP_REST_Request $request Full details about the request. + * @return \WP_REST_Response|\WP_Error + */ + public function get_items( $request ) { + $response = parent::get_items( $request ); + + if ( is_wp_error( $response ) ) { + return $response; + } + + $forms_context = (string) $request->get_param( 'jetpack_forms_context' ); + if ( 'dashboard' !== $forms_context ) { + return $response; + } + + $data = $response->get_data(); + if ( ! is_array( $data ) || empty( $data ) ) { + return $response; + } + + $form_ids = array(); + foreach ( $data as $item ) { + if ( isset( $item['id'] ) ) { + $form_ids[] = (int) $item['id']; + } + } + $form_ids = array_values( array_unique( array_filter( $form_ids ) ) ); + + $this->entries_count_by_form_id = $this->get_entries_count_by_form_id( $form_ids ); + + foreach ( $data as &$item ) { + $form_id = isset( $item['id'] ) ? (int) $item['id'] : 0; + $item['entriesCount'] = (int) ( $this->entries_count_by_form_id[ $form_id ] ?? 0 ); + if ( $form_id ) { + $item['editUrl'] = get_edit_post_link( $form_id, 'raw' ); + } + } + unset( $item ); + + $response->set_data( $data ); + return $response; + } + + /** + * Batch compute feedback counts for a list of form IDs. + * + * @param int[] $form_ids Form IDs to count entries for. + * @return array Map of form_id => count + */ + private function get_entries_count_by_form_id( array $form_ids ): array { + global $wpdb; + + $form_ids = array_values( array_unique( array_map( 'absint', $form_ids ) ) ); + if ( empty( $form_ids ) ) { + return array(); + } + + // Match dashboard behavior: count feedback statuses we display/manage. + $statuses = array( 'publish', 'draft', 'spam', 'trash' ); + + $args = array_merge( array( Feedback::POST_TYPE ), $form_ids, $statuses ); + + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching + $rows = $wpdb->get_results( + $wpdb->prepare( + "SELECT post_parent, COUNT(1) AS entry_count + FROM {$wpdb->posts} + WHERE post_type = %s + AND post_parent IN (" . implode( ',', array_fill( 0, count( $form_ids ), '%d' ) ) . ') + AND post_status IN (' . implode( ',', array_fill( 0, count( $statuses ), '%s' ) ) . ') + GROUP BY post_parent', + $args + ) + ); + $counts_by_form_id = array(); + foreach ( (array) $rows as $row ) { + $counts_by_form_id[ (int) $row->post_parent ] = (int) $row->entry_count; + } + + return $counts_by_form_id; + } + /** * Checks if a given request has access to get items. * diff --git a/projects/packages/forms/src/dashboard/class-dashboard.php b/projects/packages/forms/src/dashboard/class-dashboard.php index e75fd35f3616..68607498b18a 100644 --- a/projects/packages/forms/src/dashboard/class-dashboard.php +++ b/projects/packages/forms/src/dashboard/class-dashboard.php @@ -69,8 +69,6 @@ public function init() { self::load_wp_build(); } - Forms_Endpoint::init(); - add_action( 'admin_menu', array( $this, 'add_new_admin_submenu' ), self::MENU_PRIORITY ); if ( $is_wp_build_enabled ) { @@ -146,14 +144,17 @@ public function load_admin_scripts() { // Only preload the Forms list endpoint when centralized form management is enabled. if ( Contact_Form_Plugin::has_editor_feature_flag( 'central-form-management' ) ) { - $initial_forms_path = \add_query_arg( - array( - 'page' => 1, - 'per_page' => 20, - ), - '/jetpack-forms/v1/forms' + $forms_preload_params = array( + 'context' => 'edit', + 'page' => 1, + 'jetpack_forms_context' => 'dashboard', + 'order' => 'desc', + 'orderby' => 'modified', + 'per_page' => 20, + 'status' => 'publish,draft,pending,future,private', ); - $preload_paths[] = $initial_forms_path; + ksort( $forms_preload_params ); + $preload_paths[] = add_query_arg( $forms_preload_params, '/wp/v2/jetpack-forms' ); } $preload_data_raw = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); diff --git a/projects/packages/forms/src/dashboard/class-forms-endpoint.php b/projects/packages/forms/src/dashboard/class-forms-endpoint.php deleted file mode 100644 index 27e442f54eb6..000000000000 --- a/projects/packages/forms/src/dashboard/class-forms-endpoint.php +++ /dev/null @@ -1,132 +0,0 @@ - \WP_REST_Server::READABLE, - 'callback' => array( __CLASS__, 'get_forms' ), - 'permission_callback' => array( __CLASS__, 'permissions_check' ), - 'args' => array( - 'page' => array( - 'type' => 'integer', - 'default' => 1, - 'sanitize_callback' => 'absint', - ), - 'per_page' => array( - 'type' => 'integer', - 'default' => 20, - 'sanitize_callback' => 'absint', - ), - 'search' => array( - 'type' => 'string', - 'required' => false, - 'sanitize_callback' => 'sanitize_text_field', - ), - ), - ) - ); - } - - /** - * Permissions check. - * - * @return bool - */ - public static function permissions_check() { - return current_user_can( 'edit_pages' ); - } - - /** - * Returns a paginated list of reusable forms (`jetpack_form` posts), including entry counts. - * - * @param WP_REST_Request $request Request. - * @return \WP_REST_Response - */ - public static function get_forms( WP_REST_Request $request ) { - $page = max( 1, (int) $request->get_param( 'page' ) ); - $per_page = max( 1, min( 100, (int) $request->get_param( 'per_page' ) ) ); - $search = $request->get_param( 'search' ); - - $args = array( - 'post_type' => Contact_Form::POST_TYPE, - // Include all "normal" statuses but exclude trash (handled in a future PR). - 'post_status' => array( 'publish', 'draft', 'pending', 'future', 'private' ), - 'posts_per_page' => $per_page, - 'paged' => $page, - 'orderby' => 'modified', - 'order' => 'DESC', - ); - - if ( ! empty( $search ) ) { - $args['s'] = $search; - } - - $query = new WP_Query( $args ); - $items = array(); - - foreach ( $query->posts as $post ) { - // Count entries associated with this form via feedback.post_parent. - $entries_query = new WP_Query( - array( - 'post_type' => Feedback::POST_TYPE, - 'post_status' => array( 'publish', 'draft', 'spam', 'trash', 'jp-temp-feedback' ), - 'posts_per_page' => 1, - 'fields' => 'ids', - 'post_parent' => $post->ID, - ) - ); - - $items[] = array( - 'id' => $post->ID, - 'title' => get_the_title( $post ), - 'status' => $post->post_status, - 'modified' => $post->post_modified, - 'entriesCount' => (int) $entries_query->found_posts, - 'editUrl' => get_edit_post_link( $post->ID, 'raw' ), - ); - } - - return rest_ensure_response( - array( - 'items' => $items, - 'totalItems' => (int) $query->found_posts, - 'totalPages' => (int) $query->max_num_pages, - 'currentPage' => $page, - ) - ); - } -} diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts index d8eee02ee76b..22398877485a 100644 --- a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -1,5 +1,6 @@ import apiFetch from '@wordpress/api-fetch'; import { useEffect, useState } from '@wordpress/element'; +import { decodeEntities } from '@wordpress/html-entities'; export type FormListItem = { id: number; @@ -10,11 +11,13 @@ export type FormListItem = { editUrl?: string; }; -type FormsListResponse = { - items: FormListItem[]; - totalItems: number; - totalPages: number; - currentPage: number; +type JetpackFormRestItem = { + id: number; + title?: { rendered?: string }; + status: string; + modified: string; + entriesCount?: number; + editUrl?: string; }; type UseFormsDataReturn = { @@ -24,6 +27,23 @@ type UseFormsDataReturn = { totalPages: number; }; +type FormsQueryParams = { + context: string; + jetpack_forms_context: string; + order: string; + orderby: string; + page: string; + per_page: string; + status: string; + search?: string; +}; + +const toSortedSearchParams = ( queryParams: FormsQueryParams ): URLSearchParams => { + const entries = Object.entries( queryParams ); + entries.sort( ( [ a ], [ b ] ) => ( a > b ? 1 : -1 ) ); + return new URLSearchParams( entries ); +}; + /** * Fetch Forms list records for the Forms dashboard table. * @@ -38,39 +58,61 @@ export default function useFormsData( perPage: number, search: string ): UseFormsDataReturn { - const [ records, setRecords ] = useState< FormListItem[] >( [] ); + const [ formsList, setFormsList ] = useState< FormListItem[] >( [] ); const [ isLoading, setIsLoading ] = useState( false ); const [ totalItems, setTotalItems ] = useState( 0 ); const [ totalPages, setTotalPages ] = useState( 0 ); useEffect( () => { let isMounted = true; - const params = new URLSearchParams(); - params.set( 'page', String( page ) ); - params.set( 'per_page', String( perPage ) ); + const queryParams: FormsQueryParams = { + context: 'edit', + jetpack_forms_context: 'dashboard', + order: 'desc', + orderby: 'modified', + page: String( page ), + per_page: String( perPage ), + status: 'publish,draft,pending,future,private', + }; if ( search ) { - params.set( 'search', search ); + queryParams.search = search; } + // Keep query string ordering stable so apiFetch preloading keys match. + const params = toSortedSearchParams( queryParams ); setIsLoading( true ); - apiFetch< FormsListResponse >( { - path: `/jetpack-forms/v1/forms?${ params.toString() }`, + apiFetch( { + path: `/wp/v2/jetpack-forms?${ params.toString() }`, + parse: false, } ) - .then( response => { + .then( async ( res: Response ) => { if ( ! isMounted ) { return; } - setRecords( response.items || [] ); - setTotalItems( response.totalItems || 0 ); - setTotalPages( response.totalPages || 0 ); + const wpTotalItems = Number( res.headers.get( 'X-WP-Total' ) || 0 ); + const wpTotalPages = Number( res.headers.get( 'X-WP-TotalPages' ) || 0 ); + const data = ( await res.json() ) as JetpackFormRestItem[]; + + setFormsList( + ( data || [] ).map( item => ( { + id: item.id, + title: decodeEntities( item.title?.rendered || '' ), + status: item.status, + modified: item.modified, + entriesCount: item.entriesCount ?? 0, + editUrl: item.editUrl, + } ) ) + ); + setTotalItems( wpTotalItems ); + setTotalPages( wpTotalPages ); setIsLoading( false ); } ) .catch( () => { if ( ! isMounted ) { return; } - setRecords( [] ); + setFormsList( [] ); setTotalItems( 0 ); setTotalPages( 0 ); setIsLoading( false ); @@ -81,5 +123,5 @@ export default function useFormsData( }; }, [ page, perPage, search ] ); - return { records, isLoading, totalItems, totalPages }; + return { records: formsList, isLoading, totalItems, totalPages }; } From 9442f8586fcf0fc6847adbc84e33fe660be1e17c Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 11:11:25 -0700 Subject: [PATCH 07/16] Address PR feedback --- .../components/empty-responses/index.tsx | 2 +- .../forms/src/dashboard/forms/index.tsx | 49 +++++++++---------- .../forms/src/dashboard/forms/style.scss | 7 ++- .../src/dashboard/hooks/use-forms-data.ts | 6 +-- .../forms/src/dashboard/inbox/style.scss | 1 - 5 files changed, 32 insertions(+), 33 deletions(-) diff --git a/projects/packages/forms/src/dashboard/components/empty-responses/index.tsx b/projects/packages/forms/src/dashboard/components/empty-responses/index.tsx index dafa76f4f67b..63ca8c7054bc 100644 --- a/projects/packages/forms/src/dashboard/components/empty-responses/index.tsx +++ b/projects/packages/forms/src/dashboard/components/empty-responses/index.tsx @@ -151,7 +151,7 @@ const useInstallAkismet = (): UseInstallAkismetReturn => { }; }; -const EmptyWrapper = ( { heading = '', body = '', actions = null }: EmptyWrapperProps ) => ( +export const EmptyWrapper = ( { heading = '', body = '', actions = null }: EmptyWrapperProps ) => ( { heading && ( diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index 8a5f0c456e7c..ef80ed3c2f3f 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -2,18 +2,17 @@ * External dependencies */ import { JetpackLogo } from '@automattic/jetpack-components'; -import { Icon } from '@wordpress/components'; import { DataViews } from '@wordpress/dataviews/wp'; import { dateI18n, getSettings as getDateSettings } from '@wordpress/date'; import { useCallback, useEffect, useMemo } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { backup } from '@wordpress/icons'; import { useNavigate } from 'react-router'; /** * Internal dependencies */ import useConfigValue from '../../hooks/use-config-value.ts'; import CreateFormButton from '../components/create-form-button/index.tsx'; +import { EmptyWrapper } from '../components/empty-responses/index.tsx'; import FormsResponsesTabs from '../components/forms-responses-tabs/index.tsx'; import Page from '../components/page/index.tsx'; import useFormsData from '../hooks/use-forms-data.ts'; @@ -103,12 +102,10 @@ export default function FormsDashboardForms(): JSX.Element | null { { id: 'edit-form', isPrimary: false, - icon: , - label: __( 'Edit form', 'jetpack-forms' ), + label: __( 'Edit', 'jetpack-forms' ), supportsBulk: false, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async callback( items: any[] ) { - const [ item ] = items as FormListItem[]; + async callback( items: FormListItem[] ) { + const [ item ] = items; if ( ! item ) { return; } @@ -127,11 +124,9 @@ export default function FormsDashboardForms(): JSX.Element | null { [ totalItems, totalPages ] ); - const onChangeView = useCallback( - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ( newView: any ) => setView( newView ), - [ setView ] - ); + type DataViewsView = Parameters< typeof DataViews >[ 0 ][ 'view' ]; + + const onChangeView = useCallback( ( newView: DataViewsView ) => setView( newView ), [ setView ] ); const headerActions = useMemo( () => [ ], [] ); const getItemId = useCallback( ( item: FormListItem ) => String( item.id ), [] ); @@ -162,18 +157,21 @@ export default function FormsDashboardForms(): JSX.Element | null { data={ records || [] } isLoading={ isLoading } empty={ -
-

- { __( "You're set up. No forms yet.", 'jetpack-forms' ) } -

- -
+ + } + /> } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - view={ view as any } + view={ view as unknown as DataViewsView } onChangeView={ onChangeView } getItemId={ getItemId } defaultLayouts={ defaultLayouts } @@ -182,10 +180,7 @@ export default function FormsDashboardForms(): JSX.Element | null {
-
+
diff --git a/projects/packages/forms/src/dashboard/forms/style.scss b/projects/packages/forms/src/dashboard/forms/style.scss index 5c4d8d1886f0..e0fcd738163f 100644 --- a/projects/packages/forms/src/dashboard/forms/style.scss +++ b/projects/packages/forms/src/dashboard/forms/style.scss @@ -3,7 +3,6 @@ .jp-forms-filters-bar { border-bottom: 1px solid var(--wpds-color-stroke-surface-neutral-weak); - padding-inline: variables.$grid-unit * 2.5; display: flex; align-items: center; justify-content: space-between; @@ -13,3 +12,9 @@ padding: 12px 16px; } +.jp-forms-filters-bar__controls { + display: flex; + gap: 8px; + justify-content: flex-end; +} + diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts index 22398877485a..e9794f14d6c9 100644 --- a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -47,9 +47,9 @@ const toSortedSearchParams = ( queryParams: FormsQueryParams ): URLSearchParams /** * Fetch Forms list records for the Forms dashboard table. * - * @param page - Current page number. - * @param perPage - Items per page. - * @param search - Search term. + * @param {number} page - Current page number. + * @param {number} perPage - Items per page. + * @param {string} search - Search term. * * @return {UseFormsDataReturn} Forms list data for the current query. */ diff --git a/projects/packages/forms/src/dashboard/inbox/style.scss b/projects/packages/forms/src/dashboard/inbox/style.scss index 4e1bfed4af86..3e613b90ff6b 100644 --- a/projects/packages/forms/src/dashboard/inbox/style.scss +++ b/projects/packages/forms/src/dashboard/inbox/style.scss @@ -59,7 +59,6 @@ .jp-forms-filters-bar { border-bottom: 1px solid var(--wpds-color-stroke-surface-neutral-weak); - padding-inline: variables.$grid-unit * 2.5; display: flex; align-items: center; justify-content: space-between; From 1bb0ade4b0437803efb8ab7e8fc9bbeb38d56ab6 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 11:24:01 -0700 Subject: [PATCH 08/16] Address more PR feedback --- .../class-jetpack-form-endpoint.php | 26 +++++++++---------- .../forms/src/dashboard/forms/index.tsx | 2 +- .../src/dashboard/hooks/use-forms-data.ts | 8 +++--- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php index 8de42c248831..5c56d30a4a3b 100644 --- a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php @@ -14,7 +14,7 @@ */ class Jetpack_Form_Endpoint extends \WP_REST_Posts_Controller { /** - * Cached map of form_id => entriesCount for the current request. + * Cached map of form_id => entries count for the current request. * * @var array|null */ @@ -68,31 +68,30 @@ public function get_items( $request ) { return $response; } - $data = $response->get_data(); - if ( ! is_array( $data ) || empty( $data ) ) { + $forms = $response->get_data(); + if ( ! is_array( $forms ) || empty( $forms ) ) { return $response; } $form_ids = array(); - foreach ( $data as $item ) { - if ( isset( $item['id'] ) ) { - $form_ids[] = (int) $item['id']; + foreach ( $forms as $form ) { + if ( isset( $form['id'] ) ) { + $form_ids[] = (int) $form['id']; } } $form_ids = array_values( array_unique( array_filter( $form_ids ) ) ); $this->entries_count_by_form_id = $this->get_entries_count_by_form_id( $form_ids ); - foreach ( $data as &$item ) { - $form_id = isset( $item['id'] ) ? (int) $item['id'] : 0; - $item['entriesCount'] = (int) ( $this->entries_count_by_form_id[ $form_id ] ?? 0 ); + foreach ( $forms as &$form ) { + $form_id = isset( $form['id'] ) ? (int) $form['id'] : 0; + $form['entries_count'] = (int) ( $this->entries_count_by_form_id[ $form_id ] ?? 0 ); if ( $form_id ) { - $item['editUrl'] = get_edit_post_link( $form_id, 'raw' ); + $form['edit_url'] = get_edit_post_link( $form_id, 'raw' ); } } - unset( $item ); - $response->set_data( $data ); + $response->set_data( $forms ); return $response; } @@ -110,8 +109,7 @@ private function get_entries_count_by_form_id( array $form_ids ): array { return array(); } - // Match dashboard behavior: count feedback statuses we display/manage. - $statuses = array( 'publish', 'draft', 'spam', 'trash' ); + $statuses = array( 'publish', 'draft' ); $args = array_merge( array( Feedback::POST_TYPE ), $form_ids, $statuses ); diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index ef80ed3c2f3f..92f89ad28b18 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -101,7 +101,7 @@ export default function FormsDashboardForms(): JSX.Element | null { () => [ { id: 'edit-form', - isPrimary: false, + isPrimary: true, label: __( 'Edit', 'jetpack-forms' ), supportsBulk: false, async callback( items: FormListItem[] ) { diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts index e9794f14d6c9..7e70f52b2b97 100644 --- a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -16,8 +16,8 @@ type JetpackFormRestItem = { title?: { rendered?: string }; status: string; modified: string; - entriesCount?: number; - editUrl?: string; + entries_count?: number; + edit_url?: string; }; type UseFormsDataReturn = { @@ -100,8 +100,8 @@ export default function useFormsData( title: decodeEntities( item.title?.rendered || '' ), status: item.status, modified: item.modified, - entriesCount: item.entriesCount ?? 0, - editUrl: item.editUrl, + entriesCount: item.entries_count ?? 0, + editUrl: item.edit_url, } ) ) ); setTotalItems( wpTotalItems ); From 42517855100ef05dcaadf1a591db1ccfad01ca7d Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 11:37:10 -0700 Subject: [PATCH 09/16] Address yet more PR feedback --- .../forms/src/contact-form/class-jetpack-form-endpoint.php | 2 ++ projects/packages/forms/src/dashboard/forms/views.ts | 2 +- .../packages/forms/src/dashboard/hooks/use-forms-data.ts | 6 +++--- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php index 5c56d30a4a3b..3097f07b0fcf 100644 --- a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php @@ -109,6 +109,8 @@ private function get_entries_count_by_form_id( array $form_ids ): array { return array(); } + // Count only "inbox-visible" feedback statuses. + // Note: This is about feedback (response) statuses, not form post statuses (publish/draft/pending/future/private). $statuses = array( 'publish', 'draft' ); $args = array_merge( array( Feedback::POST_TYPE ), $form_ids, $statuses ); diff --git a/projects/packages/forms/src/dashboard/forms/views.ts b/projects/packages/forms/src/dashboard/forms/views.ts index 069a41d1ad2f..62cf11d775f2 100644 --- a/projects/packages/forms/src/dashboard/forms/views.ts +++ b/projects/packages/forms/src/dashboard/forms/views.ts @@ -19,7 +19,7 @@ export const defaultLayouts = { /** * Manage the DataViews view state for the Forms list and keep `search` in sync with the URL. * - * @return {[Object, Function]} The current DataViews view and a setter that updates the URL. + * @return {[typeof defaultView, (newView: typeof defaultView) => void]} The current DataViews view and a setter that updates the URL. */ export function useView() { const [ searchParams, setSearchParams ] = useSearchParams(); diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts index 7e70f52b2b97..901e1226774c 100644 --- a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -47,9 +47,9 @@ const toSortedSearchParams = ( queryParams: FormsQueryParams ): URLSearchParams /** * Fetch Forms list records for the Forms dashboard table. * - * @param {number} page - Current page number. - * @param {number} perPage - Items per page. - * @param {string} search - Search term. + * @param page - Current page number. + * @param perPage - Items per page. + * @param search - Search term. * * @return {UseFormsDataReturn} Forms list data for the current query. */ From bf9f2dd56370ea1d7663a042116bb519a9644f01 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 15:50:33 -0700 Subject: [PATCH 10/16] Add server caching for expensive query --- .../src/contact-form/class-jetpack-form-endpoint.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php index 3097f07b0fcf..3f8a150c6f7d 100644 --- a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php @@ -113,6 +113,16 @@ private function get_entries_count_by_form_id( array $form_ids ): array { // Note: This is about feedback (response) statuses, not form post statuses (publish/draft/pending/future/private). $statuses = array( 'publish', 'draft' ); + // Cache the grouped counts briefly to avoid repeated DB hits (e.g. on reload / concurrent requests). + sort( $form_ids ); + $cache_key = 'feedback_counts_' . md5( implode( ',', $form_ids ) . '|' . implode( ',', $statuses ) ); + $cache_group = 'jetpack_forms'; + $cache_ttl = 15; + $cached = wp_cache_get( $cache_key, $cache_group ); + if ( false !== $cached && is_array( $cached ) ) { + return $cached; + } + $args = array_merge( array( Feedback::POST_TYPE ), $form_ids, $statuses ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching @@ -132,6 +142,7 @@ private function get_entries_count_by_form_id( array $form_ids ): array { $counts_by_form_id[ (int) $row->post_parent ] = (int) $row->entry_count; } + wp_cache_set( $cache_key, $counts_by_form_id, $cache_group, $cache_ttl ); return $counts_by_form_id; } From 666a994b694470596978233b5a4342d65d0ab2f7 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 17:51:47 -0700 Subject: [PATCH 11/16] Use useEntityRecords for query --- .../src/dashboard/hooks/use-forms-data.ts | 106 ++++++------------ 1 file changed, 34 insertions(+), 72 deletions(-) diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts index 901e1226774c..932ed2dc448d 100644 --- a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -1,5 +1,5 @@ -import apiFetch from '@wordpress/api-fetch'; -import { useEffect, useState } from '@wordpress/element'; +import { useEntityRecords } from '@wordpress/core-data'; +import { useMemo } from '@wordpress/element'; import { decodeEntities } from '@wordpress/html-entities'; export type FormListItem = { @@ -27,23 +27,6 @@ type UseFormsDataReturn = { totalPages: number; }; -type FormsQueryParams = { - context: string; - jetpack_forms_context: string; - order: string; - orderby: string; - page: string; - per_page: string; - status: string; - search?: string; -}; - -const toSortedSearchParams = ( queryParams: FormsQueryParams ): URLSearchParams => { - const entries = Object.entries( queryParams ); - entries.sort( ( [ a ], [ b ] ) => ( a > b ? 1 : -1 ) ); - return new URLSearchParams( entries ); -}; - /** * Fetch Forms list records for the Forms dashboard table. * @@ -58,70 +41,49 @@ export default function useFormsData( perPage: number, search: string ): UseFormsDataReturn { - const [ formsList, setFormsList ] = useState< FormListItem[] >( [] ); - const [ isLoading, setIsLoading ] = useState( false ); - const [ totalItems, setTotalItems ] = useState( 0 ); - const [ totalPages, setTotalPages ] = useState( 0 ); - - useEffect( () => { - let isMounted = true; - const queryParams: FormsQueryParams = { + const query = useMemo( () => { + const queryParams: Record< string, unknown > = { context: 'edit', jetpack_forms_context: 'dashboard', order: 'desc', orderby: 'modified', - page: String( page ), - per_page: String( perPage ), + page, + per_page: perPage, status: 'publish,draft,pending,future,private', }; + if ( search ) { queryParams.search = search; } - // Keep query string ordering stable so apiFetch preloading keys match. - const params = toSortedSearchParams( queryParams ); - - setIsLoading( true ); - apiFetch( { - path: `/wp/v2/jetpack-forms?${ params.toString() }`, - parse: false, - } ) - .then( async ( res: Response ) => { - if ( ! isMounted ) { - return; - } - const wpTotalItems = Number( res.headers.get( 'X-WP-Total' ) || 0 ); - const wpTotalPages = Number( res.headers.get( 'X-WP-TotalPages' ) || 0 ); - const data = ( await res.json() ) as JetpackFormRestItem[]; + return queryParams; + }, [ page, perPage, search ] ); - setFormsList( - ( data || [] ).map( item => ( { - id: item.id, - title: decodeEntities( item.title?.rendered || '' ), - status: item.status, - modified: item.modified, - entriesCount: item.entries_count ?? 0, - editUrl: item.edit_url, - } ) ) - ); - setTotalItems( wpTotalItems ); - setTotalPages( wpTotalPages ); - setIsLoading( false ); - } ) - .catch( () => { - if ( ! isMounted ) { - return; - } - setFormsList( [] ); - setTotalItems( 0 ); - setTotalPages( 0 ); - setIsLoading( false ); - } ); + const { + records: rawRecords, + hasResolved, + totalItems, + totalPages, + } = useEntityRecords( 'postType', 'jetpack_form', query ); - return () => { - isMounted = false; - }; - }, [ page, perPage, search ] ); + const records = useMemo( () => { + return ( rawRecords || [] ).map( item => { + const typedItem = item as JetpackFormRestItem; + return { + id: typedItem.id, + title: decodeEntities( typedItem.title?.rendered || '' ), + status: typedItem.status, + modified: typedItem.modified, + entriesCount: typedItem.entries_count ?? 0, + editUrl: typedItem.edit_url, + }; + } ); + }, [ rawRecords ] ); - return { records: formsList, isLoading, totalItems, totalPages }; + return { + records, + isLoading: ! hasResolved, + totalItems, + totalPages, + }; } From 417d58a36f3dcc8717ca4003527948fa0e0ae1e0 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 18:04:01 -0700 Subject: [PATCH 12/16] Update query params for preloading --- projects/packages/forms/src/dashboard/class-dashboard.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/projects/packages/forms/src/dashboard/class-dashboard.php b/projects/packages/forms/src/dashboard/class-dashboard.php index 68607498b18a..90b5a16dd7dc 100644 --- a/projects/packages/forms/src/dashboard/class-dashboard.php +++ b/projects/packages/forms/src/dashboard/class-dashboard.php @@ -155,6 +155,13 @@ public function load_admin_scripts() { ); ksort( $forms_preload_params ); $preload_paths[] = add_query_arg( $forms_preload_params, '/wp/v2/jetpack-forms' ); + $preload_paths[] = add_query_arg( + array_merge( + $forms_preload_params, + array( '_locale' => 'user' ) + ), + '/wp/v2/jetpack-forms' + ); } $preload_data_raw = array_reduce( $preload_paths, 'rest_preload_api_request', array() ); From b159924b7f7434bf98006fa0f05760c8ec5287fe Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Thu, 15 Jan 2026 19:44:59 -0700 Subject: [PATCH 13/16] Add fallbacks for totalItems and totalPages Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- projects/packages/forms/src/dashboard/forms/index.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index 92f89ad28b18..734f26a6083a 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -120,7 +120,10 @@ export default function FormsDashboardForms(): JSX.Element | null { ); const paginationInfo = useMemo( - () => ( { totalItems, totalPages } ), + () => ( { + totalItems: totalItems ?? 0, + totalPages: totalPages ?? 0, + } ), [ totalItems, totalPages ] ); From fc4335abcd2effceb92259009cc76cfcd0407941 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Fri, 16 Jan 2026 09:37:25 -0700 Subject: [PATCH 14/16] Address more PR feedback --- .../class-jetpack-form-endpoint.php | 3 +-- .../forms/src/dashboard/forms/index.tsx | 7 +++--- .../forms/src/dashboard/forms/views.ts | 3 ++- .../src/dashboard/hooks/use-forms-data.ts | 24 +++++++++---------- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php index 3f8a150c6f7d..5f0bc4a7f7c8 100644 --- a/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php +++ b/projects/packages/forms/src/contact-form/class-jetpack-form-endpoint.php @@ -117,7 +117,6 @@ private function get_entries_count_by_form_id( array $form_ids ): array { sort( $form_ids ); $cache_key = 'feedback_counts_' . md5( implode( ',', $form_ids ) . '|' . implode( ',', $statuses ) ); $cache_group = 'jetpack_forms'; - $cache_ttl = 15; $cached = wp_cache_get( $cache_key, $cache_group ); if ( false !== $cached && is_array( $cached ) ) { return $cached; @@ -142,7 +141,7 @@ private function get_entries_count_by_form_id( array $form_ids ): array { $counts_by_form_id[ (int) $row->post_parent ] = (int) $row->entry_count; } - wp_cache_set( $cache_key, $counts_by_form_id, $cache_group, $cache_ttl ); + wp_cache_set( $cache_key, $counts_by_form_id, $cache_group, 15 ); // 15 seconds. return $counts_by_form_id; } diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index 734f26a6083a..93463eb54eae 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -89,6 +89,7 @@ export default function FormsDashboardForms(): JSX.Element | null { { id: 'modified', label: __( 'Last updated', 'jetpack-forms' ), + type: 'date' as const, render: ( { item }: { item: FormListItem } ) => dateI18n( dateSettings.formats.datetime, item.modified ), enableSorting: false, @@ -127,9 +128,7 @@ export default function FormsDashboardForms(): JSX.Element | null { [ totalItems, totalPages ] ); - type DataViewsView = Parameters< typeof DataViews >[ 0 ][ 'view' ]; - - const onChangeView = useCallback( ( newView: DataViewsView ) => setView( newView ), [ setView ] ); + const onChangeView = useCallback( newView => setView( newView ), [ setView ] ); const headerActions = useMemo( () => [ ], [] ); const getItemId = useCallback( ( item: FormListItem ) => String( item.id ), [] ); @@ -174,7 +173,7 @@ export default function FormsDashboardForms(): JSX.Element | null { } /> } - view={ view as unknown as DataViewsView } + view={ view } onChangeView={ onChangeView } getItemId={ getItemId } defaultLayouts={ defaultLayouts } diff --git a/projects/packages/forms/src/dashboard/forms/views.ts b/projects/packages/forms/src/dashboard/forms/views.ts index 62cf11d775f2..88cfdc604927 100644 --- a/projects/packages/forms/src/dashboard/forms/views.ts +++ b/projects/packages/forms/src/dashboard/forms/views.ts @@ -1,9 +1,10 @@ import { useCallback, useEffect, useState } from '@wordpress/element'; import { useSearchParams } from 'react-router'; +import type { View } from '@wordpress/dataviews/wp'; const LAYOUT_TABLE = 'table'; -export const defaultView = { +export const defaultView: View = { type: LAYOUT_TABLE, search: '', filters: [], diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts index 932ed2dc448d..a6c8edd4ff1b 100644 --- a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -66,19 +66,17 @@ export default function useFormsData( totalPages, } = useEntityRecords( 'postType', 'jetpack_form', query ); - const records = useMemo( () => { - return ( rawRecords || [] ).map( item => { - const typedItem = item as JetpackFormRestItem; - return { - id: typedItem.id, - title: decodeEntities( typedItem.title?.rendered || '' ), - status: typedItem.status, - modified: typedItem.modified, - entriesCount: typedItem.entries_count ?? 0, - editUrl: typedItem.edit_url, - }; - } ); - }, [ rawRecords ] ); + const records = ( rawRecords || [] ).map( item => { + const typedItem = item as JetpackFormRestItem; + return { + id: typedItem.id, + title: decodeEntities( typedItem.title?.rendered || '' ), + status: typedItem.status, + modified: typedItem.modified, + entriesCount: typedItem.entries_count ?? 0, + editUrl: typedItem.edit_url, + }; + } ); return { records, From 2f006755f310781dbe80a044257c40feb9bc65e0 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Fri, 16 Jan 2026 10:23:43 -0700 Subject: [PATCH 15/16] Response to more PR feedback --- .../forms/src/dashboard/hooks/use-forms-data.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts index a6c8edd4ff1b..20edf107cb5a 100644 --- a/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts +++ b/projects/packages/forms/src/dashboard/hooks/use-forms-data.ts @@ -30,11 +30,11 @@ type UseFormsDataReturn = { /** * Fetch Forms list records for the Forms dashboard table. * - * @param page - Current page number. - * @param perPage - Items per page. - * @param search - Search term. + * @param page - Current page number. + * @param perPage - Items per page. + * @param search - Search term. * - * @return {UseFormsDataReturn} Forms list data for the current query. + * @return Forms list data for the current query. */ export default function useFormsData( page: number, @@ -81,7 +81,7 @@ export default function useFormsData( return { records, isLoading: ! hasResolved, - totalItems, - totalPages, + totalItems: totalItems ?? 0, + totalPages: totalPages ?? 0, }; } From 2a016dc4f77cbaf87152576d579e20fe5147c994 Mon Sep 17 00:00:00 2001 From: Erick Danzer Date: Fri, 16 Jan 2026 10:28:49 -0700 Subject: [PATCH 16/16] Re-add actions dropdown menu --- projects/packages/forms/src/dashboard/forms/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/forms/src/dashboard/forms/index.tsx b/projects/packages/forms/src/dashboard/forms/index.tsx index 93463eb54eae..900bd578fd3b 100644 --- a/projects/packages/forms/src/dashboard/forms/index.tsx +++ b/projects/packages/forms/src/dashboard/forms/index.tsx @@ -102,7 +102,7 @@ export default function FormsDashboardForms(): JSX.Element | null { () => [ { id: 'edit-form', - isPrimary: true, + isPrimary: false, label: __( 'Edit', 'jetpack-forms' ), supportsBulk: false, async callback( items: FormListItem[] ) {