Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/compat/wordpress-7.0/preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,37 @@
return $paths;
}
add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_root_fields' );

/**
* Preload the notes-presence probes for the post editor.
*
* The collab notes feature drives sidebar visibility and the
* floating-sidebar auto-open from two count probes:
* - total note count (`status=all`)
* - unresolved count (`status=hold`)
* Each is a tiny list call (`per_page=1&_fields=id`) whose
* `X-WP-Total` header carries the count. Preloading them keeps the
* boot path cache-only — the full per-page comments fetch only fires
* when the total count is > 0.
*
* @param array $paths REST API paths to preload.
* @param WP_Block_Editor_Context $context Current block editor context.
* @return array Filtered preload paths.
*/
function gutenberg_block_editor_preload_paths_notes_counts( $paths, $context ) {
if ( 'core/edit-post' !== $context->name || ! isset( $context->post ) ) {
return $paths;
}
$post_id = (int) $context->post->ID;
if ( ! $post_id ) {
return $paths;
}
$base = sprintf(

Check warning on line 85 in lib/compat/wordpress-7.0/preload.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
'/wp/v2/comments?context=edit&post=%d&type=note&per_page=1&_fields=id',
$post_id
);
$paths[] = $base . '&status=all';
$paths[] = $base . '&status=hold';
return $paths;
}
add_filter( 'block_editor_rest_api_preload_paths', 'gutenberg_block_editor_preload_paths_notes_counts', 10, 2 );
20 changes: 20 additions & 0 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,26 @@ async function preloadResolutions( postType, postId ) {
postId
),
core.getAutosaves( postType, postId ),
// Collab notes presence probes — paired with the
// preload entries in `wordpress-7.0/preload.php`.
// The hook in `useNoteThreads` reads `totalItems`
// from these to drive the sidebar/floating gates
// without doing the full per-page comments fetch
// on every boot.
core.getEntityRecords( 'root', 'comment', {
post: postId,
type: 'note',
status: 'all',
per_page: 1,
_fields: 'id',
} ),
core.getEntityRecords( 'root', 'comment', {
post: postId,
type: 'note',
status: 'hold',
per_page: 1,
_fields: 'id',
} ),
]
: [] ),
] );
Expand Down
40 changes: 39 additions & 1 deletion packages/editor/src/components/collab-sidebar/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,54 @@ import {
const { cleanEmptyObject } = unlock( blockEditorPrivateApis );

export function useNoteThreads( postId ) {
const enabled = !! postId && typeof postId === 'number';

// Cheap presence probes: 1-record list calls whose response body is
// effectively empty but whose `X-WP-Total` header gives the exact
// count. The kickoff's PHP preload covers these (see
// `lib/compat/wordpress-7.0/preload.php`), so the boot path resolves
// them from cache. The counts then gate the heavier full fetch
// below.
const { totalItems: notesCount } = useEntityRecords(
'root',
'comment',
{
post: postId,
type: 'note',
status: 'all',
per_page: 1,
_fields: 'id',
},
{ enabled }
);
const { totalItems: unresolvedCount } = useEntityRecords(
'root',
'comment',
{
post: postId,
type: 'note',
status: 'hold',
per_page: 1,
_fields: 'id',
},
{ enabled }
);

const queryArgs = {
post: postId,
type: 'note',
status: 'all',
per_page: -1,
};

// Full fetch only when there's at least one note. Posts with no
// notes pay just the cached count probes; posts with notes pay the
// same fetch they pay today.
const { records: threads } = useEntityRecords(
'root',
'comment',
queryArgs,
{ enabled: !! postId && typeof postId === 'number' }
{ enabled: enabled && ( notesCount ?? 0 ) > 0 }
);

const { getBlockAttributes } = useSelect( blockEditorStore );
Expand Down Expand Up @@ -142,6 +178,8 @@ export function useNoteThreads( postId ) {
return {
notes,
unresolvedNotes,
hasNotes: ( notesCount ?? 0 ) > 0,
hasUnresolvedNotes: ( unresolvedCount ?? 0 ) > 0,
};
}

Expand Down
7 changes: 4 additions & 3 deletions packages/editor/src/components/collab-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ function NotesSidebar( { postId } ) {
[]
);

const { notes, unresolvedNotes } = useNoteThreads( postId );
const { notes, unresolvedNotes, hasNotes, hasUnresolvedNotes } =
useNoteThreads( postId );

// Only enable the floating sidebar for large viewports.
const showFloatingSidebar = isLargeViewport;
// Fallback to "All notes" sidebar on smaller viewports.
const showAllNotesSidebar = notes.length > 0 || ! showFloatingSidebar;
const showAllNotesSidebar = hasNotes || ! showFloatingSidebar;
useEnableFloatingSidebar(
showFloatingSidebar &&
( unresolvedNotes.length > 0 || selectedNote !== undefined )
( hasUnresolvedNotes || selectedNote !== undefined )
);

async function focusNote( {
Expand Down
Loading