Skip to content

Commit

Permalink
Query Block: Refactor variable names.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed May 13, 2020
1 parent fb75adc commit 30e0684
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
7 changes: 5 additions & 2 deletions lib/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ public function __get( $name ) {
/**
* Generates the render output for the block.
*
* @param array $options Optional options object.
* bool $options.dynamic Optionally set to false to avoid using the block's render_callback.
* @param array $options {
* Optional options object.
*
* @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback.
* }
*
* @return string Rendered block output.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/query-loop/block.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "core/query-loop",
"category": "layout",
"context": [ "id", "query" ]
"context": [ "queryId", "query" ]
}
4 changes: 2 additions & 2 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* @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['id'] . '-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : $_GET[ $page_key ];
$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',
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/query-pagination/block.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "core/query-pagination",
"category": "layout",
"context": [ "id", "query" ]
"context": [ "queryId", "query" ]
}
4 changes: 2 additions & 2 deletions packages/block-library/src/query-pagination/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* @return string Returns the pagination for the query.
*/
function render_block_core_query_pagination( $attributes, $content, $block ) {
$page_key = 'query-' . $block->context['id'] . '-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : $_GET[ $page_key ];
$page_key = 'query-' . $block->context['queryId'] . '-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );

$content = '';
if ( 1 !== $page ) {
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "core/query",
"category": "layout",
"attributes": {
"id": {
"queryId": {
"type": "number"
},
"query": {
Expand All @@ -15,7 +15,7 @@
}
},
"providesContext": {
"id": "id",
"queryId": "queryId",
"query": "query"
}
}
8 changes: 4 additions & 4 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import QueryProvider from './query-provider';

const TEMPLATE = [ [ 'core/query-loop' ], [ 'core/query-pagination' ] ];
export default function QueryEdit( {
attributes: { id, query },
attributes: { queryId, query },
setAttributes,
} ) {
const instanceId = useInstanceId( QueryEdit );
// We need this for multi-query block pagination.
// Query parameters for each block are scoped to their ID.
useEffect( () => {
if ( ! id ) {
setAttributes( { id: instanceId } );
if ( ! queryId ) {
setAttributes( { queryId: instanceId } );
}
}, [ id, instanceId ] );
}, [ queryId, instanceId ] );
return (
<>
<BlockControls>
Expand Down

0 comments on commit 30e0684

Please sign in to comment.