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

Add Latest Pages - Query block variation #26025

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions packages/block-library/src/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { __ } from '@wordpress/i18n';
import metadata from './block.json';
import edit from './edit';
import save from './save';
import variations from './variations';

const { name } = metadata;
export { metadata, name };
Expand All @@ -17,6 +18,7 @@ export const settings = {
title: __( 'Query' ),
edit,
save,
variations,
};

export { useQueryContext } from './edit';
19 changes: 19 additions & 0 deletions packages/block-library/src/query/variations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { page } from '@wordpress/icons';

const variations = [
{
name: 'latest-pages',
title: 'Latest Pages',
icon: page,
keywords: [ __( 'pages' ) ],
description: __( 'Display a list of your most recent pages.' ),
attributes: {
query: { postType: 'page', order: 'desc', orderBy: 'date' },
},
},
];
export default variations;
9 changes: 6 additions & 3 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ export function createBlock( name, attributes = {}, innerBlocks = [] ) {
blockType.attributes,
( accumulator, schema, key ) => {
const value = attributes[ key ];

const hasDefault = schema.hasOwnProperty( 'default' );
if ( undefined !== value ) {
accumulator[ key ] = value;
} else if ( schema.hasOwnProperty( 'default' ) ) {
accumulator[ key ] =
schema.type === 'object' && hasDefault
? { ...schema.default, ...value }
: value;
} else if ( hasDefault ) {
accumulator[ key ] = schema.default;
}

Expand Down
48 changes: 48 additions & 0 deletions packages/blocks/src/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,54 @@ describe( 'block factory', () => {
expect( typeof block.clientId ).toBe( 'string' );
} );

it( 'should create a block respecting the defaults in an property with `object` type', () => {
registerBlockType( 'core/test-block', {
attributes: {
align: {
type: 'string',
},
includesDefault: {
type: 'boolean',
default: true,
},
includesFalseyDefault: {
type: 'number',
default: 0,
},
objectProp: {
type: 'object',
default: {
content: 'ping',
offset: 0,
level: 2,
},
},
},
category: 'text',
title: 'test block',
} );
const block = createBlock( 'core/test-block', {
align: 'left',
objectProp: { content: 'pong', level: 9 },
} );
expect( block ).toEqual(
expect.objectContaining( {
name: 'core/test-block',
innerBlocks: [],
attributes: {
includesDefault: true,
includesFalseyDefault: 0,
align: 'left',
objectProp: {
content: 'pong',
level: 9,
offset: 0,
},
},
} )
);
} );

it( 'should cast children and node source attributes with default undefined', () => {
registerBlockType( 'core/test-block', {
...defaultBlockSettings,
Expand Down
28 changes: 14 additions & 14 deletions packages/e2e-tests/fixtures/blocks/core__embed.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[
{
"clientId": "_clientId_0",
"name": "core/embed",
"isValid": true,
"attributes": {
"url": "https://example.com/",
"caption": "Embedded content from an example URL",
"allowResponsive": true,
"responsive": false,
"previewable": true
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-embed\">\n <div class=\"wp-block-embed__wrapper\">\n https://example.com/\n </div>\n <figcaption>Embedded content from an example URL</figcaption>\n</figure>"
}
{
"clientId": "_clientId_0",
"name": "core/embed",
"isValid": true,
"attributes": {
"url": "https://example.com/",
"caption": "Embedded content from an example URL",
"allowResponsive": true,
"responsive": false,
"previewable": true
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-embed\">\n <div class=\"wp-block-embed__wrapper\">\n https://example.com/\n </div>\n <figcaption>Embedded content from an example URL</figcaption>\n</figure>"
}
]
48 changes: 24 additions & 24 deletions packages/e2e-tests/fixtures/blocks/core__latest-posts.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
[
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": false,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null,
"addLinkToFeaturedImage": false
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": false,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null,
"addLinkToFeaturedImage": false
},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
[
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": true,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null,
"addLinkToFeaturedImage": false
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/latest-posts",
"isValid": true,
"attributes": {
"postsToShow": 5,
"displayPostContent": false,
"displayPostContentRadio": "excerpt",
"excerptLength": 55,
"displayAuthor": false,
"displayPostDate": true,
"postLayout": "list",
"columns": 3,
"order": "desc",
"orderBy": "date",
"displayFeaturedImage": false,
"featuredImageSizeSlug": "thumbnail",
"featuredImageSizeWidth": null,
"featuredImageSizeHeight": null,
"addLinkToFeaturedImage": false
},
"innerBlocks": [],
"originalContent": ""
}
]
20 changes: 10 additions & 10 deletions packages/e2e-tests/fixtures/blocks/core__post-featured-image.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-featured-image",
"isValid": true,
"attributes": {
"isLink": false
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/post-featured-image",
"isValid": true,
"attributes": {
"isLink": false
},
"innerBlocks": [],
"originalContent": ""
}
]
26 changes: 13 additions & 13 deletions packages/e2e-tests/fixtures/blocks/core__post-title.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-title",
"isValid": true,
"attributes": {
"level": 2,
"isLink": false,
"linkTarget": "_self",
"rel": ""
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/post-title",
"isValid": true,
"attributes": {
"level": 2,
"isLink": false,
"rel": "",
"linkTarget": "_self"
},
"innerBlocks": [],
"originalContent": ""
}
]
44 changes: 22 additions & 22 deletions packages/e2e-tests/fixtures/blocks/core__query.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[
{
"clientId": "_clientId_0",
"name": "core/query",
"isValid": true,
"attributes": {
"query": {
"perPage": null,
"pages": 1,
"offset": 0,
"postType": "post",
"categoryIds": [],
"tagIds": [],
"order": "desc",
"orderBy": "date",
"author": "",
"search": "",
"exclude": []
}
},
"innerBlocks": [],
"originalContent": ""
}
{
"clientId": "_clientId_0",
"name": "core/query",
"isValid": true,
"attributes": {
"query": {
"perPage": null,
"pages": 1,
"offset": 0,
"postType": "post",
"categoryIds": [],
"tagIds": [],
"order": "desc",
"orderBy": "date",
"author": "",
"search": "",
"exclude": []
}
},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- wp:query /-->
<!-- wp:query {"query":{"perPage":null,"pages":1,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[]}} /-->
6 changes: 3 additions & 3 deletions packages/e2e-tests/fixtures/blocks/core__search.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"name": "core/search",
"isValid": true,
"attributes": {
"buttonPosition": "button-outside",
"buttonUseIcon": false,
"showLabel": true,
"placeholder": "",
"showLabel": true
"buttonPosition": "button-outside",
"buttonUseIcon": false
},
"innerBlocks": [],
"originalContent": ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"name": "core/search",
"isValid": true,
"attributes": {
"buttonPosition": "button-outside",
"label": "Custom label",
"showLabel": true,
"placeholder": "Custom placeholder",
"buttonText": "Custom button text",
"buttonUseIcon": false,
"showLabel": true
"buttonPosition": "button-outside",
"buttonUseIcon": false
},
"innerBlocks": [],
"originalContent": ""
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e-tests/fixtures/blocks/core__social-links.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"name": "core/social-links",
"isValid": true,
"attributes": {
"openInNewTab": false
},
"openInNewTab": false
},
"innerBlocks": [
{
"clientId": "_clientId_0",
Expand Down