Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP 6.6 RC 1 🍒 (2) #62812

Merged
merged 3 commits into from
Jun 25, 2024
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
4 changes: 4 additions & 0 deletions backport-changelog/6.6/6864.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
https://github.com/WordPress/wordpress-develop/pull/6864

* https://github.com/WordPress/gutenberg/pull/62488
* https://github.com/WordPress/gutenberg/pull/62696
16 changes: 8 additions & 8 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ function gutenberg_register_wp_rest_post_types_controller_fields() {
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) && ! empty( $post_type->template ) ) {
return $post_type->template;
if ( ! empty( $post_type ) ) {
return $post_type->template ?? array();
}
},
'schema' => array(
'type' => 'array',
'description' => __( 'The template associated with the post type.', 'gutenberg' ),
'description' => __( 'The block template associated with the post type.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
Expand All @@ -48,14 +48,14 @@ function gutenberg_register_wp_rest_post_types_controller_fields() {
array(
'get_callback' => function ( $item ) {
$post_type = get_post_type_object( $item['slug'] );
if ( ! empty( $post_type ) && ! empty( $post_type->template_lock ) && false !== $post_type->template_lock ) {
return $post_type->template_lock;
if ( ! empty( $post_type ) ) {
return ! empty( $post_type->template_lock ) ? $post_type->template_lock : false;
}
},
'schema' => array(
'type' => 'string',
'enum' => array( 'all', 'insert', 'contentOnly' ),
'description' => __( 'The template_lock specified for the post type.', 'gutenberg' ),
'type' => array( 'string', 'boolean' ),
'enum' => array( 'all', 'insert', 'contentOnly', false ),
'description' => __( 'The template_lock associated with the post type, or false if none.', 'gutenberg' ),
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
Expand Down
41 changes: 10 additions & 31 deletions packages/dataviews/src/bulk-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Modal,
} from '@wordpress/components';
import { __, sprintf, _n } from '@wordpress/i18n';
import { useMemo, useState, useCallback, useEffect } from '@wordpress/element';
import { useMemo, useState, useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -194,37 +194,16 @@ export default function BulkActions< Item extends AnyItem >( {
}, [ data, bulkActions ] );

const numberSelectableItems = selectableItems.length;
const areAllSelected =
selection && selection.length === numberSelectableItems;

const selectedItems = useMemo( () => {
return data.filter( ( item ) =>
selection.includes( getItemId( item ) )
return data.filter(
( item ) =>
selection.includes( getItemId( item ) ) &&
selectableItems.includes( item )
);
}, [ selection, data, getItemId ] );
}, [ selection, data, getItemId, selectableItems ] );

const hasNonSelectableItemSelected = useMemo( () => {
return selectedItems.some( ( item ) => {
return ! selectableItems.includes( item );
} );
}, [ selectedItems, selectableItems ] );
useEffect( () => {
if ( hasNonSelectableItemSelected ) {
onSelectionChange(
selectedItems.filter( ( selectedItem ) => {
return selectableItems.some( ( item ) => {
return getItemId( selectedItem ) === getItemId( item );
} );
} )
);
}
}, [
hasNonSelectableItemSelected,
selectedItems,
selectableItems,
getItemId,
onSelectionChange,
] );
const areAllSelected = selectedItems.length === numberSelectableItems;

if ( bulkActions.length === 0 ) {
return null;
Expand All @@ -243,15 +222,15 @@ export default function BulkActions< Item extends AnyItem >( {
variant="tertiary"
size="compact"
>
{ selection.length
{ selectedItems.length
? sprintf(
/* translators: %d: Number of items. */
_n(
'Edit %d item',
'Edit %d items',
selection.length
selectedItems.length
),
selection.length
selectedItems.length
)
: __( 'Bulk edit' ) }
</Button>
Expand Down
32 changes: 9 additions & 23 deletions packages/dataviews/src/dataviews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ComponentType } from 'react';
* WordPress dependencies
*/
import { __experimentalHStack as HStack } from '@wordpress/components';
import { useMemo, useState, useCallback, useEffect } from '@wordpress/element';
import { useMemo, useState, useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -92,25 +92,6 @@ export default function DataViews< Item extends AnyItem >( {
}
const [ openedFilter, setOpenedFilter ] = useState< string | null >( null );

useEffect( () => {
if (
selection.length > 0 &&
selection.some(
( id ) => ! data.some( ( item ) => getItemId( item ) === id )
)
) {
const newSelection = selection.filter( ( id ) =>
data.some( ( item ) => getItemId( item ) === id )
);
setSelection( newSelection );
onSelectionChange(
data.filter( ( item ) =>
newSelection.includes( getItemId( item ) )
)
);
}
}, [ selection, data, getItemId, onSelectionChange ] );

const onSetSelection = useCallback(
( items: Item[] ) => {
setSelection( items.map( ( item ) => getItemId( item ) ) );
Expand All @@ -127,6 +108,11 @@ export default function DataViews< Item extends AnyItem >( {
actions,
data
);
const _selection = useMemo( () => {
return selection.filter( ( id ) =>
data.some( ( item ) => getItemId( item ) === id )
);
}, [ selection, data, getItemId ] );
return (
<div className="dataviews-wrapper">
<HStack
Expand Down Expand Up @@ -160,7 +146,7 @@ export default function DataViews< Item extends AnyItem >( {
actions={ actions }
data={ data }
onSelectionChange={ onSetSelection }
selection={ selection }
selection={ _selection }
getItemId={ getItemId }
/>
) }
Expand All @@ -179,7 +165,7 @@ export default function DataViews< Item extends AnyItem >( {
isLoading={ isLoading }
onChangeView={ onChangeView }
onSelectionChange={ onSetSelection }
selection={ selection }
selection={ _selection }
setOpenedFilter={ setOpenedFilter }
view={ view }
/>
Expand All @@ -193,7 +179,7 @@ export default function DataViews< Item extends AnyItem >( {
<BulkActionsToolbar
data={ data }
actions={ actions }
selection={ selection }
selection={ _selection }
onSelectionChange={ onSetSelection }
getItemId={ getItemId }
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/dataviews/src/single-selection-checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function SingleSelectionCheckbox< Item extends AnyItem >( {
disabled,
}: SingleSelectionCheckboxProps< Item > ) {
const id = getItemId( item );
const isSelected = selection.includes( id );
const isSelected = ! disabled && selection.includes( id );
let selectionLabel;
if ( primaryField?.getValue && item ) {
// eslint-disable-next-line @wordpress/valid-sprintf
Expand Down
17 changes: 14 additions & 3 deletions packages/dataviews/src/view-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ interface BulkSelectionCheckboxProps< Item extends AnyItem > {
onSelectionChange: ( items: Item[] ) => void;
data: Item[];
actions: Action< Item >[];
getItemId: ( item: Item ) => string;
}

interface TableRowProps< Item extends AnyItem > {
Expand Down Expand Up @@ -249,6 +250,7 @@ function BulkSelectionCheckbox< Item extends AnyItem >( {
onSelectionChange,
data,
actions,
getItemId,
}: BulkSelectionCheckboxProps< Item > ) {
const selectableItems = useMemo( () => {
return data.filter( ( item ) => {
Expand All @@ -259,13 +261,18 @@ function BulkSelectionCheckbox< Item extends AnyItem >( {
);
} );
}, [ data, actions ] );
const areAllSelected = selection.length === selectableItems.length;
const selectedItems = data.filter(
( item ) =>
selection.includes( getItemId( item ) ) &&
selectableItems.includes( item )
);
const areAllSelected = selectedItems.length === selectableItems.length;
return (
<CheckboxControl
className="dataviews-view-table-selection-checkbox"
__nextHasNoMarginBottom
checked={ areAllSelected }
indeterminate={ ! areAllSelected && !! selection.length }
indeterminate={ ! areAllSelected && !! selectedItems.length }
onChange={ () => {
if ( areAllSelected ) {
onSelectionChange( [] );
Expand Down Expand Up @@ -293,7 +300,7 @@ function TableRow< Item extends AnyItem >( {
data,
}: TableRowProps< Item > ) {
const hasPossibleBulkAction = useHasAPossibleBulkAction( actions, item );
const isSelected = selection.includes( id );
const isSelected = hasPossibleBulkAction && selection.includes( id );

const [ isHovered, setIsHovered ] = useState( false );

Expand Down Expand Up @@ -323,6 +330,9 @@ function TableRow< Item extends AnyItem >( {
isTouchDevice.current = true;
} }
onClick={ () => {
if ( ! hasPossibleBulkAction ) {
return;
}
if (
! isTouchDevice.current &&
document.getSelection()?.type !== 'Range'
Expand Down Expand Up @@ -495,6 +505,7 @@ function ViewTable< Item extends AnyItem >( {
onSelectionChange={ onSelectionChange }
data={ data }
actions={ actions }
getItemId={ getItemId }
/>
</th>
) }
Expand Down
17 changes: 9 additions & 8 deletions packages/edit-site/src/components/add-new-page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ export default function AddNewPageModal( { onSave, onClose } ) {
status: 'draft',
title,
slug: title || __( 'No title' ),
content: !! pagePostType.template
? serialize(
synchronizeBlocksWithTemplate(
[],
pagePostType.template
)
)
: undefined,
content:
!! pagePostType.template && pagePostType.template.length
? serialize(
synchronizeBlocksWithTemplate(
[],
pagePostType.template
)
)
: undefined,
},
{ throwOnError: true }
);
Expand Down
54 changes: 21 additions & 33 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import AddNewPageModal from '../add-new-page';
import Media from '../media';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';
import { usePrevious } from '@wordpress/compose';

const { usePostActions } = unlock( editorPrivateApis );
const { useLocation, useHistory } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -201,43 +202,18 @@ function FeaturedImage( { item, viewType } ) {
);
}

function usePostIdLinkInSelection(
selection,
setSelection,
isLoadingItems,
items
) {
const {
params: { postId },
} = useLocation();
const [ postIdToSelect, setPostIdToSelect ] = useState( postId );
useEffect( () => {
if ( postId ) {
setPostIdToSelect( postId );
}
}, [ postId ] );

useEffect( () => {
if ( ! postIdToSelect ) {
return;
}
// Only try to select an item if the loading is complete and we have items.
if ( ! isLoadingItems && items && items.length ) {
// If the item is not in the current selection, select it.
if ( selection.length !== 1 || selection[ 0 ] !== postIdToSelect ) {
setSelection( [ postIdToSelect ] );
}
setPostIdToSelect( undefined );
}
}, [ postIdToSelect, selection, setSelection, isLoadingItems, items ] );
function getItemId( item ) {
return item.id.toString();
}

export default function PagePages() {
const postType = 'page';
const [ view, setView ] = useView( postType );
const history = useHistory();

const [ selection, setSelection ] = useState( [] );
const {
params: { postId },
} = useLocation();
const [ selection, setSelection ] = useState( [ postId ] );

const onSelectionChange = useCallback(
( items ) => {
Expand Down Expand Up @@ -299,7 +275,19 @@ export default function PagePages() {
totalPages,
} = useEntityRecords( 'postType', postType, queryArgs );

usePostIdLinkInSelection( selection, setSelection, isLoadingPages, pages );
const ids = pages?.map( ( page ) => getItemId( page ) ) ?? [];
const prevIds = usePrevious( ids ) ?? [];
const deletedIds = prevIds.filter( ( id ) => ! ids.includes( id ) );
const postIdWasDeleted = deletedIds.includes( postId );

useEffect( () => {
if ( postIdWasDeleted ) {
history.push( {
...history.getLocationWithParams().params,
postId: undefined,
} );
}
}, [ postIdWasDeleted, history ] );

const { records: authors, isResolving: isLoadingAuthors } =
useEntityRecords( 'root', 'user', { per_page: -1 } );
Expand Down Expand Up @@ -569,7 +557,7 @@ export default function PagePages() {
selection={ selection }
setSelection={ setSelection }
onSelectionChange={ onSelectionChange }
getItemId={ ( item ) => item.id.toString() }
getItemId={ getItemId }
/>
</Page>
);
Expand Down
Loading