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

Latest Posts: Fix selected category on existing blocks #21359

Merged
merged 6 commits into from
Apr 20, 2020
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
5 changes: 4 additions & 1 deletion packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"type": "string"
},
"categories": {
"type": "array"
"type": "array",
"items": {
"type": "object"
}
},
"postsToShow": {
"type": "number",
Expand Down
31 changes: 31 additions & 0 deletions packages/block-library/src/latest-posts/deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Internal dependencies
*/
import metadata from './block.json';

const { attributes } = metadata;

export default [
{
attributes: {
...attributes,
categories: {
type: 'string',
},
},
supports: {
align: true,
html: false,
},
migrate: ( oldAttributes ) => {
// This needs the full category object, not just the ID.
return {
...oldAttributes,
categories: [ { id: Number( oldAttributes.categories ) } ],
};
},
isEligible: ( { categories } ) =>
categories && 'string' === typeof categories,
save: () => null,
},
];
14 changes: 5 additions & 9 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ class LatestPostsEdit extends Component {
featuredImageSizeWidth,
featuredImageSizeHeight,
} = attributes;
const suggestions = categoriesList.reduce(
( accumulator, category ) => ( {
...accumulator,
[ category.name ]: category,
} ),
{}
);
const categorySuggestions = categoriesList.reduce(
( accumulator, category ) => ( {
...accumulator,
Expand All @@ -120,15 +113,18 @@ class LatestPostsEdit extends Component {
);
const selectCategories = ( tokens ) => {
const hasNoSuggestion = tokens.some(
( token ) => typeof token === 'string' && ! suggestions[ token ]
( token ) =>
typeof token === 'string' && ! categorySuggestions[ token ]
);
if ( hasNoSuggestion ) {
return;
}
// Categories that are already will be objects, while new additions will be strings (the name).
// allCategories nomalizes the array so that they are all objects.
const allCategories = tokens.map( ( token ) => {
return typeof token === 'string' ? suggestions[ token ] : token;
return typeof token === 'string'
? categorySuggestions[ token ]
: token;
} );
// We do nothing if the category is not selected
// from suggestions.
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { postList as icon } from '@wordpress/icons';
/**
* Internal dependencies
*/
import deprecated from './deprecated';
import edit from './edit';
import metadata from './block.json';

Expand All @@ -23,4 +24,5 @@ export const settings = {
html: false,
},
edit,
deprecated,
};
31 changes: 31 additions & 0 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,34 @@ function register_block_core_latest_posts() {
);
}
add_action( 'init', 'register_block_core_latest_posts' );

/**
* Handles outdated versions of the `core/latest-posts` block by converting
* attribute `categories` from a numeric string to an array with key `id`.
*
* This is done to accommodate the changes introduced in #20781 that sought to
* add support for multiple categories to the block. However, given that this
* block is dynamic, the usual provisions for block migration are insufficient,
* as they only act when a block is loaded in the editor.
*
* TODO: Remove when and if the bottom client-side deprecation for this block
* is removed.
*
* @param array $block A single parsed block object.
*
* @return array The migrated block object.
*/
function block_core_latest_posts_migrate_categories( $block ) {
if (
'core/latest-posts' === $block['blockName'] &&
! empty( $block['attrs']['categories'] ) &&
is_string( $block['attrs']['categories'] )
) {
$block['attrs']['categories'] = array(
array( 'id' => absint( $block['attrs']['categories'] ) ),
);
}

return $block;
}
add_filter( 'render_block_data', 'block_core_latest_posts_migrate_categories' );