Skip to content

Commit

Permalink
REST API: Adds the site reading options to the index (WordPress#69106)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: peterwilsoncc <[email protected]>
Co-authored-by: TimothyBJacobs <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: spacedmonkey <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
  • Loading branch information
8 people authored and Kallyan01 committed Feb 24, 2025
1 parent 91de5fa commit 9c72f9f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 47 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.8/8345.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8345

* https://github.com/WordPress/gutenberg/pull/69106
37 changes: 20 additions & 17 deletions lib/compat/wordpress-6.8/preload.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,6 @@ function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) {

$paths[] = '/wp/v2/settings';
$paths[] = array( '/wp/v2/settings', 'OPTIONS' );
$paths[] = '/?_fields=' . implode(
',',
// @see packages/core-data/src/entities.js
array(
'description',
'gmt_offset',
'home',
'name',
'site_icon',
'site_icon_url',
'site_logo',
'timezone_string',
'default_template_part_areas',
'default_template_types',
'url',
)
);
$paths[] = '/wp/v2/templates/lookup?slug=front-page';
$paths[] = '/wp/v2/templates/lookup?slug=home';
}
Expand Down Expand Up @@ -90,6 +73,26 @@ function gutenberg_block_editor_preload_paths_6_8( $paths, $context ) {

// Used by getBlockPatternCategories in useBlockEditorSettings.
$paths[] = '/wp/v2/block-patterns/categories';
$paths[] = '/?_fields=' . implode(
',',
// @see packages/core-data/src/entities.js
array(
'description',
'gmt_offset',
'home',
'name',
'site_icon',
'site_icon_url',
'site_logo',
'timezone_string',
'default_template_part_areas',
'default_template_types',
'url',
'page_for_posts',
'page_on_front',
'show_on_front',
)
);
}
return $paths;
}
Expand Down
15 changes: 15 additions & 0 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ function gutenberg_add_default_template_types_to_index( WP_REST_Response $respon
}
add_filter( 'rest_index', 'gutenberg_add_default_template_types_to_index' );

/**
* Adds the site reading options to the REST API index.
*
* @param WP_REST_Response $response REST API response.
* @return WP_REST_Response Modified REST API response.
*/
function gutenberg_add_rest_index_reading_options( WP_REST_Response $response ) {
$response->data['page_for_posts'] = (int) get_option( 'page_for_posts' );
$response->data['page_on_front'] = (int) get_option( 'page_on_front' );
$response->data['show_on_front'] = get_option( 'show_on_front' );

return $response;
}
add_filter( 'rest_index', 'gutenberg_add_rest_index_reading_options' );

/**
* Adds `ignore_sticky` parameter to the post collection endpoint.
*
Expand Down
3 changes: 3 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export const rootEntitiesConfig = [
'default_template_part_areas',
'default_template_types',
'url',
'page_for_posts',
'page_on_front',
'show_on_front',
].join( ',' ),
},
// The entity doesn't support selecting multiple records.
Expand Down
35 changes: 7 additions & 28 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import { createSelector, createRegistrySelector } from '@wordpress/data';
/**
* Internal dependencies
*/
import {
canUser,
getDefaultTemplateId,
getEntityRecord,
type State,
} from './selectors';
import { getDefaultTemplateId, getEntityRecord, type State } from './selectors';
import { STORE_NAME } from './name';
import { unlock } from './lock-unlock';

Expand Down Expand Up @@ -139,16 +134,9 @@ interface SiteData {
export const getHomePage = createRegistrySelector( ( select ) =>
createSelector(
() => {
const canReadSiteData = select( STORE_NAME ).canUser( 'read', {
kind: 'root',
name: 'site',
} );
if ( ! canReadSiteData ) {
return null;
}
const siteData = select( STORE_NAME ).getEntityRecord(
'root',
'site'
'__unstableBase'
) as SiteData | undefined;
if ( ! siteData ) {
return null;
Expand All @@ -168,10 +156,7 @@ export const getHomePage = createRegistrySelector( ( select ) =>
return { postType: 'wp_template', postId: frontPageTemplateId };
},
( state ) => [
canUser( state, 'read', {
kind: 'root',
name: 'site',
} ) && getEntityRecord( state, 'root', 'site' ),
getEntityRecord( state, 'root', '__unstableBase' ),
getDefaultTemplateId( state, {
slug: 'front-page',
} ),
Expand All @@ -180,16 +165,10 @@ export const getHomePage = createRegistrySelector( ( select ) =>
);

export const getPostsPageId = createRegistrySelector( ( select ) => () => {
const canReadSiteData = select( STORE_NAME ).canUser( 'read', {
kind: 'root',
name: 'site',
} );
if ( ! canReadSiteData ) {
return null;
}
const siteData = select( STORE_NAME ).getEntityRecord( 'root', 'site' ) as
| SiteData
| undefined;
const siteData = select( STORE_NAME ).getEntityRecord(
'root',
'__unstableBase'
) as SiteData | undefined;
return siteData?.show_on_front === 'page'
? normalizePageId( siteData.page_for_posts )
: null;
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/specs/site-editor/preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ test.describe( 'Preload', () => {

// To do: these should all be removed or preloaded.
expect( requests ).toEqual( [
// Seems to be coming from `enableComplementaryArea`.
'/wp/v2/users/me',
// There are two separate settings OPTIONS requests. We should fix
// so the one for canUser and getEntityRecord are reused.
'/wp/v2/settings',
// Seems to be coming from `enableComplementaryArea`.
'/wp/v2/users/me',
] );
} );
} );

0 comments on commit 9c72f9f

Please sign in to comment.