From 1445a3b8c57fba295ffb1f8ec80b5b2d23f0fcc5 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 2 Apr 2020 12:25:04 -0400 Subject: [PATCH 1/6] Add fallback for displaying single-category restrictions --- packages/block-library/src/latest-posts/index.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index f9002ebde627da..b749fd7268341d 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -47,7 +47,11 @@ function render_block_core_latest_posts( $attributes ) { add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 ); if ( isset( $attributes['categories'] ) ) { - $args['category__in'] = array_column( $attributes['categories'], 'id' ); + if ( is_array( $attributes['categories'] ) ) { + $args['category__in'] = array_column( $attributes['categories'], 'id' ); + } else { + $args['category__in'] = (array) $attributes['categories']; + } } $recent_posts = get_posts( $args ); From 97ab5dde976b3276c5e957ab0648a221ad0c53d3 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 2 Apr 2020 12:25:29 -0400 Subject: [PATCH 2/6] Add a block deprecation to convert string-categories to an array --- .../src/latest-posts/deprecated.js | 31 +++++++++++++++++++ .../block-library/src/latest-posts/index.js | 2 ++ 2 files changed, 33 insertions(+) create mode 100644 packages/block-library/src/latest-posts/deprecated.js diff --git a/packages/block-library/src/latest-posts/deprecated.js b/packages/block-library/src/latest-posts/deprecated.js new file mode 100644 index 00000000000000..d49b8abab56c08 --- /dev/null +++ b/packages/block-library/src/latest-posts/deprecated.js @@ -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, + }, +]; diff --git a/packages/block-library/src/latest-posts/index.js b/packages/block-library/src/latest-posts/index.js index 63f7b7bfae638a..df11a4dda161af 100644 --- a/packages/block-library/src/latest-posts/index.js +++ b/packages/block-library/src/latest-posts/index.js @@ -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'; @@ -23,4 +24,5 @@ export const settings = { html: false, }, edit, + deprecated, }; From cddaaa6d9c9aa76c548c0d2aeb58de9a138252b6 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 2 Apr 2020 12:26:27 -0400 Subject: [PATCH 3/6] Remove extra suggestions object --- packages/block-library/src/latest-posts/edit.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/latest-posts/edit.js b/packages/block-library/src/latest-posts/edit.js index 8e58ddd7260554..9f21499510367f 100644 --- a/packages/block-library/src/latest-posts/edit.js +++ b/packages/block-library/src/latest-posts/edit.js @@ -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, @@ -120,7 +113,8 @@ 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; @@ -128,7 +122,9 @@ class LatestPostsEdit extends Component { // 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. From f9127dfe7320d305651855651eb0da38e7a033b9 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Wed, 15 Apr 2020 11:27:53 +0100 Subject: [PATCH 4/6] Latest Posts: Fix type schema for array "categories" --- packages/block-library/src/latest-posts/block.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/latest-posts/block.json b/packages/block-library/src/latest-posts/block.json index e730d861626502..96285d1f2705ef 100644 --- a/packages/block-library/src/latest-posts/block.json +++ b/packages/block-library/src/latest-posts/block.json @@ -10,7 +10,10 @@ "type": "string" }, "categories": { - "type": "array" + "type": "array", + "items": { + "type": "object" + } }, "postsToShow": { "type": "number", From 9aa653e6d61d9cd699cf191179114ab9c876e915 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Wed, 15 Apr 2020 11:28:52 +0100 Subject: [PATCH 5/6] Latest Posts: Shim server-side deprecation for string-based categories * Reverts the change to `render_block_core_latest_posts` --- .../block-library/src/latest-posts/index.php | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index b749fd7268341d..6a1b7b22f1bf11 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -47,11 +47,7 @@ function render_block_core_latest_posts( $attributes ) { add_filter( 'excerpt_length', 'block_core_latest_posts_get_excerpt_length', 20 ); if ( isset( $attributes['categories'] ) ) { - if ( is_array( $attributes['categories'] ) ) { - $args['category__in'] = array_column( $attributes['categories'], 'id' ); - } else { - $args['category__in'] = (array) $attributes['categories']; - } + $args['category__in'] = array_column( $attributes['categories'], 'id' ); } $recent_posts = get_posts( $args ); @@ -177,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'] && + isset( $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' ); From 0fa0dad4b2d3d29ac5d6512bb7b7c57914f18c93 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Thu, 16 Apr 2020 17:28:24 +0100 Subject: [PATCH 6/6] Update packages/block-library/src/latest-posts/index.php Co-Authored-By: Andrew Duthie --- packages/block-library/src/latest-posts/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/latest-posts/index.php b/packages/block-library/src/latest-posts/index.php index 6a1b7b22f1bf11..ea35484569e02a 100644 --- a/packages/block-library/src/latest-posts/index.php +++ b/packages/block-library/src/latest-posts/index.php @@ -193,7 +193,7 @@ function register_block_core_latest_posts() { function block_core_latest_posts_migrate_categories( $block ) { if ( 'core/latest-posts' === $block['blockName'] && - isset( $block['attrs']['categories'] ) && + ! empty( $block['attrs']['categories'] ) && is_string( $block['attrs']['categories'] ) ) { $block['attrs']['categories'] = array(