-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Library: Add Query block. (#22199)
* Lib: Support forcing non dynamic renders in WP_Block. * Block Editor: Support live block previews in BlockPreview. * Block Library: Add query block. * Query Block: Add query toolbar with per_page control. * Components: Fix toolbar CSS selectors. * Query Block: Unfocus posts on Query block blur. * Query Block: Add pagination on the edit side. * Query Block: Add pagination on the front end side. * Block Library: Break Query block into multiple sub-blocks. * Query Block: Support Post Content blocks. * Query Block: Add translations and icon. * Update packages/block-library/src/query-pagination/index.php Co-authored-by: Andrew Duthie <andrew@andrewduthie.com> * Update packages/block-library/src/query-pagination/index.php Co-authored-by: Andrew Duthie <andrew@andrewduthie.com> * Lib: Wrap the new block render parameter in an options array. * E2E Tests: Add fixtures. * Query Block: Refactor variable names. Co-authored-by: Andrew Duthie <andrew@andrewduthie.com>
- Loading branch information
Showing
35 changed files
with
675 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/block-editor/src/components/block-preview/live.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Disabled } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockList from '../block-list'; | ||
|
||
export default function LiveBlockPreview( { onClick } ) { | ||
return ( | ||
<div | ||
tabIndex={ 0 } | ||
role="button" | ||
onClick={ onClick } | ||
onKeyPress={ onClick } | ||
> | ||
<Disabled> | ||
<BlockList /> | ||
</Disabled> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "core/query-loop", | ||
"category": "layout", | ||
"context": [ "queryId", "query" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useMemo } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { | ||
BlockContextProvider, | ||
InnerBlocks, | ||
BlockPreview, | ||
} from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useQueryContext } from '../query'; | ||
|
||
const TEMPLATE = [ [ 'core/post-title' ], [ 'core/post-content' ] ]; | ||
export default function QueryLoopEdit( { clientId, context: { query } } ) { | ||
const [ { page } ] = useQueryContext(); | ||
const [ activeBlockContext, setActiveBlockContext ] = useState(); | ||
|
||
const { posts, blocks } = useSelect( | ||
( select ) => ( { | ||
posts: select( 'core' ).getEntityRecords( 'postType', 'post', { | ||
...query, | ||
offset: query.per_page * ( page - 1 ) + query.offset, | ||
page, | ||
} ), | ||
blocks: select( 'core/block-editor' ).getBlocks( clientId ), | ||
} ), | ||
[ query, page, clientId ] | ||
); | ||
|
||
const blockContexts = useMemo( | ||
() => | ||
posts?.map( ( post ) => ( { | ||
postType: post.type, | ||
postId: post.id, | ||
} ) ), | ||
[ posts ] | ||
); | ||
return blockContexts | ||
? blockContexts.map( ( blockContext ) => ( | ||
<BlockContextProvider | ||
key={ blockContext.postId } | ||
value={ blockContext } | ||
> | ||
{ blockContext === | ||
( activeBlockContext || blockContexts[ 0 ] ) ? ( | ||
<InnerBlocks template={ TEMPLATE } /> | ||
) : ( | ||
<BlockPreview | ||
blocks={ blocks } | ||
__experimentalLive | ||
__experimentalOnClick={ () => | ||
setActiveBlockContext( blockContext ) | ||
} | ||
/> | ||
) } | ||
</BlockContextProvider> | ||
) ) | ||
: null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { loop } from '@wordpress/icons'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
import save from './save'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Query Loop' ), | ||
icon: loop, | ||
parent: [ 'core/query' ], | ||
supports: { | ||
inserter: false, | ||
reusable: false, | ||
html: false, | ||
}, | ||
edit, | ||
save, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/query-loop` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/query-loop` block on the server. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block default content. | ||
* @param WP_Block $block Block instance. | ||
* | ||
* @return string Returns the output of the query, structured using the layout defined by the block's inner blocks. | ||
*/ | ||
function render_block_core_query_loop( $attributes, $content, $block ) { | ||
$page_key = 'query-' . $block->context['queryId'] . '-page'; | ||
$page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); | ||
$posts = get_posts( | ||
array( | ||
'post_type' => 'post', | ||
'posts_per_page' => $block->context['query']['per_page'], | ||
'offset' => $block->context['query']['per_page'] * ( $page - 1 ) + $block->context['query']['offset'], | ||
) | ||
); | ||
|
||
$content = ''; | ||
foreach ( $posts as $post ) { | ||
$content .= ( | ||
new WP_Block( | ||
$block->parsed_block, | ||
array_merge( | ||
$block->available_context ? $block->available_context : array(), | ||
array( | ||
'postType' => $post->post_type, | ||
'postId' => $post->ID, | ||
) | ||
) | ||
) | ||
)->render( array( 'dynamic' => false ) ); | ||
} | ||
return $content; | ||
} | ||
|
||
/** | ||
* Registers the `core/query-loop` block on the server. | ||
*/ | ||
function register_block_core_query_loop() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/query-loop', | ||
array( | ||
'render_callback' => 'render_block_core_query_loop', | ||
'skip_inner_blocks' => true, | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_query_loop' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
export default function QueryLoopSave() { | ||
return <InnerBlocks.Content />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "core/query-pagination", | ||
"category": "layout", | ||
"context": [ "queryId", "query" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Button, ButtonGroup } from '@wordpress/components'; | ||
import { chevronLeft, chevronRight } from '@wordpress/icons'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useQueryContext } from '../query'; | ||
|
||
export default function QueryPaginationEdit( { | ||
context: { | ||
query: { pages }, | ||
}, | ||
} ) { | ||
const [ { page }, setQueryContext ] = useQueryContext(); | ||
|
||
let previous; | ||
if ( page > 1 ) { | ||
previous = ( | ||
<Button | ||
isPrimary | ||
icon={ chevronLeft } | ||
onClick={ () => setQueryContext( { page: page - 1 } ) } | ||
> | ||
{ __( 'Previous' ) } | ||
</Button> | ||
); | ||
} | ||
let next; | ||
if ( page < pages ) { | ||
next = ( | ||
<Button | ||
isPrimary | ||
icon={ chevronRight } | ||
onClick={ () => setQueryContext( { page: page + 1 } ) } | ||
> | ||
{ __( 'Next' ) } | ||
</Button> | ||
); | ||
} | ||
return previous || next ? ( | ||
<ButtonGroup> | ||
{ previous } | ||
{ next } | ||
</ButtonGroup> | ||
) : ( | ||
__( 'No pages to paginate.' ) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Query Pagination' ), | ||
parent: [ 'core/query' ], | ||
supports: { | ||
inserter: false, | ||
reusable: false, | ||
html: false, | ||
}, | ||
edit, | ||
}; |
Oops, something went wrong.