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
5 changes: 4 additions & 1 deletion src/blocks/post-carousel/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
"default": "date"
},
"categories": {
"type": "array"
"type": "array",
"items": {
"type": "object"
}
}
}
}
31 changes: 31 additions & 0 deletions src/blocks/post-carousel/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,
},
];
2 changes: 2 additions & 0 deletions src/blocks/post-carousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import transforms from './transforms';
import deprecated from './deprecated';

/**
* WordPress dependencies
Expand Down Expand Up @@ -41,6 +42,7 @@ const settings = {
},
transforms,
edit,
deprecated,
save() {
return null;
},
Expand Down
34 changes: 33 additions & 1 deletion src/blocks/post-carousel/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function coblocks_render_post_carousel_block( $attributes ) {

if ( isset( $attributes['categories'] ) ) {

$args['category'] = $attributes['categories'];
$args['category__in'] = array_column( $attributes['categories'], 'id' );

}

Expand Down Expand Up @@ -341,3 +341,35 @@ function coblocks_register_post_carousel_block() {
);
}
add_action( 'init', 'coblocks_register_post_carousel_block' );



/**
* Handles outdated versions of the `coblocks/post-carousel` block by converting
* attribute `categories` from a numeric string to an array with key `id`.
*
* This is done to accommodate the changes introduced in https://github.com/WordPress/gutenberg/pull/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_coblocks_post_carousel_migrate_categories( $block ) {
if (
'coblocks/post-carousel' === $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_coblocks_post_carousel_migrate_categories' );
Comment thread
jrtashjian marked this conversation as resolved.
Resolved
5 changes: 4 additions & 1 deletion src/blocks/posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
"default": "date"
},
"categories": {
"type": "array"
"type": "array",
"items": {
"type": "object"
}
}
}
}
31 changes: 31 additions & 0 deletions src/blocks/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 {
Comment thread
jrtashjian marked this conversation as resolved.
Resolved
...oldAttributes,
categories: [ { id: Number( oldAttributes.categories ) } ],
};
},
isEligible: ( { categories } ) =>
categories && 'string' === typeof categories,
save: () => null,
},
];
2 changes: 2 additions & 0 deletions src/blocks/posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import transforms from './transforms';
import deprecated from './deprecated';

/**
* WordPress dependencies
Expand Down Expand Up @@ -39,6 +40,7 @@ const settings = {
},
transforms,
edit,
deprecated,
save() {
return null;
},
Expand Down
32 changes: 31 additions & 1 deletion src/blocks/posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function coblocks_render_posts_block( $attributes ) {

if ( isset( $attributes['categories'] ) ) {

$args['category'] = $attributes['categories'];
$args['category__in'] = array_column( $attributes['categories'], 'id' );

}

Expand Down Expand Up @@ -329,3 +329,33 @@ function coblocks_register_posts_block() {
);
}
add_action( 'init', 'coblocks_register_posts_block' );

/**
* Handles outdated versions of the `coblocks/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 https://github.com/WordPress/gutenberg/pull/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_coblocks_posts_migrate_categories( $block ) {
if (
'coblocks/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_coblocks_posts_migrate_categories' );
Comment thread
jrtashjian marked this conversation as resolved.
Resolved