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

Query Loop Block: Add migration of colors to v2 deprecation #46522

Merged
merged 1 commit into from
Dec 14, 2022
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
188 changes: 94 additions & 94 deletions packages/block-library/src/query/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,95 @@ const migrateToTaxQuery = ( attributes ) => {
};
};

const migrateColors = ( attributes, innerBlocks ) => {
// Remove color style attributes from the Query block.
const { style, backgroundColor, gradient, textColor, ...newAttributes } =
attributes;

const hasColorStyles =
backgroundColor ||
gradient ||
textColor ||
style?.color ||
style?.elements?.link;

// If the query block doesn't currently have any color styles,
// nothing needs migrating.
if ( ! hasColorStyles ) {
return [ attributes, innerBlocks ];
}

// Clean color values from style attribute object.
if ( style ) {
newAttributes.style = cleanEmptyObject( {
...style,
color: undefined,
elements: {
...style.elements,
link: undefined,
},
} );
}

// If the inner blocks are already wrapped in a single group
// block, add the color support styles to that group block.
if ( hasSingleInnerGroupBlock( innerBlocks ) ) {
const groupBlock = innerBlocks[ 0 ];

// Create new styles for the group block.
const hasStyles =
style?.color ||
style?.elements?.link ||
groupBlock.attributes.style;

const newStyles = hasStyles
? cleanEmptyObject( {
...groupBlock.attributes.style,
color: style?.color,
elements: style?.elements?.link
? { link: style?.elements?.link }
: undefined,
} )
: undefined;

// Create a new Group block from the original.
const updatedGroupBlock = createBlock(
'core/group',
{
...groupBlock.attributes,
backgroundColor,
gradient,
textColor,
style: newStyles,
},
groupBlock.innerBlocks
);

return [ newAttributes, [ updatedGroupBlock ] ];
}

// When we don't have a single wrapping group block for the inner
// blocks, wrap the current inner blocks in a group applying the
// color styles to that.
const newGroupBlock = createBlock(
'core/group',
{
backgroundColor,
gradient,
textColor,
style: cleanEmptyObject( {
color: style?.color,
elements: style?.elements?.link
? { link: style?.elements?.link }
: undefined,
} ),
},
innerBlocks
);

return [ newAttributes, [ newGroupBlock ] ];
};

const hasSingleInnerGroupBlock = ( innerBlocks = [] ) =>
innerBlocks.length === 1 && innerBlocks[ 0 ].name === 'core/group';

Expand Down Expand Up @@ -127,7 +216,10 @@ const v2 = {
},
isEligible: ( { query: { categoryIds, tagIds } = {} } ) =>
categoryIds || tagIds,
migrate: migrateToTaxQuery,
migrate( attributes, innerBlocks ) {
const withTaxQuery = migrateToTaxQuery( attributes );
return migrateColors( withTaxQuery, innerBlocks );
},
save( { attributes: { tagName: Tag = 'div' } } ) {
const blockProps = useBlockProps.save();
const innerBlocksProps = useInnerBlocksProps.save( blockProps );
Expand Down Expand Up @@ -196,99 +288,7 @@ const v3 = {
style?.elements?.link
);
},
migrate: ( attributes, innerBlocks ) => {
// Remove color style attributes from the Query block.
const {
style,
backgroundColor,
gradient,
textColor,
...newAttributes
} = attributes;

const hasColorStyles =
backgroundColor ||
gradient ||
textColor ||
style?.color ||
style?.elements?.link;

// If the query block doesn't currently have any color styles,
// nothing needs migrating.
if ( ! hasColorStyles ) {
return [ attributes, innerBlocks ];
}

// Clean color values from style attribute object.
if ( style ) {
newAttributes.style = cleanEmptyObject( {
...style,
color: undefined,
elements: {
...style.elements,
link: undefined,
},
} );
}

// If the inner blocks are already wrapped in a single group
// block, add the color support styles to that group block.
if ( hasSingleInnerGroupBlock( innerBlocks ) ) {
const groupBlock = innerBlocks[ 0 ];

// Create new styles for the group block.
const hasStyles =
style?.color ||
style?.elements?.link ||
groupBlock.attributes.style;

const newStyles = hasStyles
? cleanEmptyObject( {
...groupBlock.attributes.style,
color: style?.color,
elements: style?.elements?.link
? { link: style?.elements?.link }
: undefined,
} )
: undefined;

// Create a new Group block from the original.
const updatedGroupBlock = createBlock(
'core/group',
{
...groupBlock.attributes,
backgroundColor,
gradient,
textColor,
style: newStyles,
},
groupBlock.innerBlocks
);

return [ newAttributes, [ updatedGroupBlock ] ];
}

// When we don't have a single wrapping group block for the inner
// blocks, wrap the current inner blocks in a group applying the
// color styles to that.
const newGroupBlock = createBlock(
'core/group',
{
backgroundColor,
gradient,
textColor,
style: cleanEmptyObject( {
color: style?.color,
elements: style?.elements?.link
? { link: style?.elements?.link }
: undefined,
} ),
},
innerBlocks
);

return [ newAttributes, [ newGroupBlock ] ];
},
migrate: migrateColors,
save( { attributes: { tagName: Tag = 'div' } } ) {
const blockProps = useBlockProps.save();
const innerBlocksProps = useInnerBlocksProps.save( blockProps );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!-- wp:query {"queryId":19,"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[3,5],"tagIds":[6],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false},"style":{"color":{"background":"#284d5f"},"elements":{"link":{"color":{"text":"var:preset|color|luminous-vivid-amber"}}}},"textColor":"pale-cyan-blue"} -->
<div class="wp-block-query has-pale-cyan-blue-color has-text-color has-background has-link-color" style="background-color:#284d5f"><!-- wp:post-template -->
<!-- wp:post-title /-->
<!-- /wp:post-template -->
</div>
<!-- /wp:query -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[
{
"name": "core/query",
"isValid": true,
"attributes": {
"queryId": 19,
"query": {
"perPage": 3,
"pages": 0,
"offset": 0,
"postType": "post",
"order": "desc",
"orderBy": "date",
"author": "",
"search": "",
"exclude": [],
"sticky": "",
"inherit": false,
"taxQuery": {
"category": [ 3, 5 ],
"post_tag": [ 6 ]
}
},
"tagName": "div",
"displayLayout": {
"type": "list"
}
},
"innerBlocks": [
{
"name": "core/group",
"isValid": true,
"attributes": {
"tagName": "div",
"textColor": "pale-cyan-blue",
"style": {
"color": {
"background": "#284d5f"
},
"elements": {
"link": {
"color": {
"text": "var:preset|color|luminous-vivid-amber"
}
}
}
}
},
"innerBlocks": [
{
"name": "core/post-template",
"isValid": true,
"attributes": {},
"innerBlocks": [
{
"name": "core/post-title",
"isValid": true,
"attributes": {
"level": 2,
"isLink": false,
"rel": "",
"linkTarget": "_self"
},
"innerBlocks": []
}
]
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[
{
"blockName": "core/query",
"attrs": {
"queryId": 19,
"query": {
"perPage": 3,
"pages": 0,
"offset": 0,
"postType": "post",
"categoryIds": [ 3, 5 ],
"tagIds": [ 6 ],
"order": "desc",
"orderBy": "date",
"author": "",
"search": "",
"exclude": [],
"sticky": "",
"inherit": false
},
"style": {
"color": {
"background": "#284d5f"
},
"elements": {
"link": {
"color": {
"text": "var:preset|color|luminous-vivid-amber"
}
}
}
},
"textColor": "pale-cyan-blue"
},
"innerBlocks": [
{
"blockName": "core/post-template",
"attrs": {},
"innerBlocks": [
{
"blockName": "core/post-title",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
],
"innerHTML": "\n\n",
"innerContent": [ "\n", null, "\n" ]
}
],
"innerHTML": "\n<div class=\"wp-block-query has-pale-cyan-blue-color has-text-color has-background has-link-color\" style=\"background-color:#284d5f\">\n</div>\n",
"innerContent": [
"\n<div class=\"wp-block-query has-pale-cyan-blue-color has-text-color has-background has-link-color\" style=\"background-color:#284d5f\">",
null,
"\n</div>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- wp:query {"queryId":19,"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false,"taxQuery":{"category":[3,5],"post_tag":[6]}},"displayLayout":{"type":"list"}} -->
<div class="wp-block-query"><!-- wp:group {"textColor":"pale-cyan-blue","style":{"color":{"background":"#284d5f"},"elements":{"link":{"color":{"text":"var:preset|color|luminous-vivid-amber"}}}}} -->
<div class="wp-block-group has-pale-cyan-blue-color has-text-color has-background has-link-color" style="background-color:#284d5f"><!-- wp:post-template -->
<!-- wp:post-title /-->
<!-- /wp:post-template --></div>
<!-- /wp:group --></div>
<!-- /wp:query -->