Skip to content

Commit

Permalink
Latest Posts: Fix selected category on existing blocks (#21359)
Browse files Browse the repository at this point in the history
* Add fallback for displaying single-category restrictions

* Add a block deprecation to convert string-categories to an array

* Remove extra suggestions object

* Latest Posts: Fix type schema for array "categories"

* Latest Posts: Shim server-side deprecation for string-based categories

* Reverts the change to `render_block_core_latest_posts`

* Update packages/block-library/src/latest-posts/index.php

Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>

Co-authored-by: Miguel Fonseca <miguelcsf@gmail.com>
Co-authored-by: Andrew Duthie <andrew@andrewduthie.com>
  • Loading branch information
3 people authored Apr 20, 2020
1 parent d69aae1 commit 0c37ba8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
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' );

0 comments on commit 0c37ba8

Please sign in to comment.