Skip to content
Merged
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
3 changes: 3 additions & 0 deletions backport-changelog/7.2/13266.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/13266

* https://github.com/WordPress/gutenberg/pull/81987
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ function example_filter_page_view_config( $data ) {
add_filter( 'get_entity_view_config_posttype_page', 'example_filter_page_view_config' );
```

### Filter priority

Hook the callback at the default priority (`10`) or later. Priorities below `10` are reserved for WordPress Core, which uses them to build the configuration a screen starts from. A callback registered earlier than that runs before Core has finished assembling the config, so its changes are overwritten.

### Update entries with `merge`

Filter callbacks receive a `Gutenberg_View_Config_Data` object that encodes the current view config for the entity. To update the given configuration, call its `merge( $patch, $version )` method where:
Expand All @@ -125,6 +129,8 @@ Filter callbacks receive a `Gutenberg_View_Config_Data` object that encodes the

For example, the following filter callback is applied to the _Pages_ screen. It makes the default view type a grid, sorts the grid by ascending title, and makes the `date` field visible (in addition to the existing fields). It also appends `my_custom_field` to the Quick Edit `form`, keeping the form's existing fields (note that registering a field from the server is not yet possible).

Entries merged into a list are always appended to the end of it; choosing where a new entry lands is not supported yet.

```php
function example_filter_page_view_config( $data ) {
$patch = array(
Expand Down
80 changes: 80 additions & 0 deletions lib/compat/wordpress-7.2/view-config-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Entity view configuration additions, layered on top of the base
* configurations in `lib/compat/wordpress-7.1/view-config-api.php`.
*
* @package gutenberg
*/

/**
* Adds the `reading_settings` field to the `wp_template` form configuration.
*
* The `fields` list is pinned rather than merged because a merged member is
* appended to the end of the list, and the link belongs above the last edited
* date. Keep it in sync with the list built in
* _gutenberg_get_entity_view_config_posttype_wp_template().
*
* @param Gutenberg_View_Config_Data $data The view configuration container for the entity.
* @return Gutenberg_View_Config_Data The updated view configuration container.
*/
function _gutenberg_add_reading_settings_to_wp_template_view_config( $data ) {
return $data->replace(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think changes are fine here and replace at that priority is what makes such core changes possible.

It's worth updating the docs to mention that consumers should not use a priority lower of 10 (reserved by core).

3rd party consumers right now can only append new fields when merging and that was intentional for now, due to the challenges to properly support ordering in a filter.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think the content added in e34dbff is sufficient?

array(
'form' => array(
'fields' => array(
array(
'id' => 'description',
'layout' => array(
'type' => 'panel',
'labelPosition' => 'top',
),
),
array(
'id' => 'description_readonly',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
array(
'id' => 'reading_settings',
'layout' => array(
'type' => 'regular',
'labelPosition' => 'none',
),
),
array(
'id' => 'last_edited_date',
'layout' => array(
'type' => 'panel',
'labelPosition' => 'none',
),
),
'revisions',
// The following fields are only meaningful in the `home`/`index`
// template summary. They edit other entities (`root/site` and the
// posts page); the editor merges those records into the form data
// under a namespace and controls when the fields are shown.
'posts_page_title',
'posts_per_page',
'default_comment_status',
),
),
),
1
);
}

/**
* Registers the entity view configuration filters that layer on top of the base
* definitions, at a priority between those (5) and third-party callbacks (10).
*/
function gutenberg_register_entity_view_config_filters_7_2() {
add_filter(
gutenberg_get_entity_view_config_hook_name( 'postType', 'wp_template' ),
'_gutenberg_add_reading_settings_to_wp_template_view_config',
6,
1
);
}
add_action( 'init', 'gutenberg_register_entity_view_config_filters_7_2' );
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-7.1/query-block.php';
require __DIR__ . '/compat/wordpress-7.1/block-comments.php';

// WordPress 7.2 compat.
require __DIR__ . '/compat/wordpress-7.2/view-config-api.php';

// Real-time collaboration.
require __DIR__ . '/experimental/collaboration/class-gutenberg-rest-autosaves-controller.php';
require __DIR__ . '/experimental/collaboration/rest-api.php';
Expand Down
36 changes: 36 additions & 0 deletions packages/editor/src/components/reading-settings-link/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { Link } from '@wordpress/ui';
import { store as editorStore } from '../../store';
import { TEMPLATE_POST_TYPE } from '../../store/constants';

/**
* Links to the Reading settings screen from the Front Page template summary.
* That template resolves to the site homepage whether the homepage shows the
* latest posts or a static page, so it points at where that choice is made.
*
* @return The rendered component.
*/
export default function ReadingSettingsLink() {
const isVisible = useSelect( ( select ) => {
const { getCurrentPostType, getEditedPostAttribute } =
select( editorStore );
const { canUser } = select( coreStore );
return (
getCurrentPostType() === TEMPLATE_POST_TYPE &&
getEditedPostAttribute( 'slug' ) === 'front-page' &&
!! canUser( 'update', { kind: 'root', name: 'site' } )
);
}, [] );

if ( ! isVisible ) {
return null;
}

return (
<div>
<Link href="options-reading.php">{ __( 'Reading settings' ) }</Link>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import PostTrash from '../post-trash';
import usePostFields from '../post-fields';
import { usePostTemplatePanelMode } from '../post-template/hooks';
import revisionsField from '../../dataviews/fields/revisions';
import readingSettingsField from '../../dataviews/fields/reading-settings';

const EMPTY_FORM = { layout: { type: 'panel' }, fields: [] };
const VIEW_CONFIG_FIELDS = [ 'form' ];
Expand Down Expand Up @@ -58,6 +59,7 @@ function useInspectorPanelVisibility( form ) {
featured_media: featuredImageEnabled,
excerpt: excerptEnabled,
'post-content-info': true,
reading_settings: true,
discussion: discussionEnabled && ! isPostStatusRemoved,
parent: pageAttributesEnabled && ! isPostStatusRemoved,
};
Expand Down Expand Up @@ -319,9 +321,9 @@ export default function DataFormPostSummary( { onActionPerformed } ) {
return field;
} )
.filter( Boolean )
// Editor-only field, injected here rather than registered
// so it never leaks into the site editor list / quick-edit fields.
.concat( revisionsField ),
// Editor-only fields, injected here rather than registered
// so they never leak into the site editor list / quick-edit fields.
.concat( revisionsField, readingSettingsField ),
[
_fields,
templatePanelMode,
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/components/sidebar/post-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import PostFeaturedImagePanel from '../post-featured-image/panel';
import PostFormatPanel from '../post-format/panel';
import PostLastEditedPanel from '../post-last-edited-panel';
import PostPanelSection from '../post-panel-section';
import ReadingSettingsLink from '../reading-settings-link';
import PostSchedulePanel from '../post-schedule/panel';
import PostStatusPanel from '../post-status';
import PostSyncStatus from '../post-sync-status';
Expand Down Expand Up @@ -59,6 +60,7 @@ export default function PostSummary( { onActionPerformed } ) {
/>
<PostFeaturedImagePanel withPanelBody={ false } />
<PostExcerptPanel />
<ReadingSettingsLink />
<Stack direction="column" gap="xs">
<PostContentInformation />
<PostLastEditedPanel />
Expand Down
16 changes: 16 additions & 0 deletions packages/editor/src/dataviews/fields/reading-settings/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Field } from '@wordpress/dataviews';
import type { BasePost } from '@wordpress/fields';
import { __ } from '@wordpress/i18n';
import ReadingSettingsLink from '../../../components/reading-settings-link';

const readingSettingsField: Field< BasePost > = {
id: 'reading_settings',
type: 'text',
label: __( 'Reading settings' ),
readOnly: true,
enableSorting: false,
render: ReadingSettingsLink,
isVisible: ( item ) => item.slug === 'front-page',
};

export default readingSettingsField;
Loading