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

Make mid size parameter settable for Query Pagination block. #51216

Merged
merged 18 commits into from
Aug 28, 2023
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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ Displays a list of page numbers for pagination ([Source](https://github.com/Word
- **Category:** theme
- **Parent:** core/query-pagination
- **Supports:** color (background, gradients, ~~text~~), typography (fontSize, lineHeight), ~~html~~, ~~reusable~~
- **Attributes:**
- **Attributes:** midSize

## Previous Page

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"parent": [ "core/query-pagination" ],
"description": "Displays a list of page numbers for pagination",
"textdomain": "default",
"attributes": {
"midSize": {
"type": "number",
"default": 2
}
},
"usesContext": [ "queryId", "query" ],
"supports": {
"reusable": false,
Expand Down
80 changes: 64 additions & 16 deletions packages/block-library/src/query-pagination-numbers/edit.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,73 @@
/**
* WordPress dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { PanelBody, RangeControl } from '@wordpress/components';

const createPaginationItem = ( content, Tag = 'a', extraClass = '' ) => (
<Tag className={ `page-numbers ${ extraClass }` }>{ content }</Tag>
<Tag key={ content } className={ `page-numbers ${ extraClass }` }>
{ content }
</Tag>
);

const previewPaginationNumbers = () => (
<>
{ createPaginationItem( 1 ) }
{ createPaginationItem( 2 ) }
{ createPaginationItem( 3, 'span', 'current' ) }
{ createPaginationItem( 4 ) }
{ createPaginationItem( 5 ) }
{ createPaginationItem( '...', 'span', 'dots' ) }
{ createPaginationItem( 8 ) }
</>
);
const previewPaginationNumbers = ( midSize ) => {
const paginationItems = [];

// First set of pagination items.
for ( let i = 1; i <= midSize; i++ ) {
paginationItems.push( createPaginationItem( i ) );
}

// Current pagination item.
paginationItems.push(
createPaginationItem( midSize + 1, 'span', 'current' )
);

// Second set of pagination items.
for ( let i = 1; i <= midSize; i++ ) {
paginationItems.push( createPaginationItem( midSize + 1 + i ) );
}

// Dots.
paginationItems.push( createPaginationItem( '...', 'span', 'dots' ) );

// Last pagination item.
paginationItems.push( createPaginationItem( midSize * 2 + 3 ) );

return <>{ paginationItems }</>;
};

export default function QueryPaginationNumbersEdit() {
const paginationNumbers = previewPaginationNumbers();
return <div { ...useBlockProps() }>{ paginationNumbers }</div>;
export default function QueryPaginationNumbersEdit( {
attributes,
setAttributes,
} ) {
const { midSize } = attributes;
const paginationNumbers = previewPaginationNumbers(
parseInt( midSize, 10 )
);
return (
<>
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<RangeControl
label={ __( 'Number of links' ) }
help={ __(
'Specify how many links can appear before and after the current page number. Links to the first, current and last page are always visible.'
) }
value={ midSize }
onChange={ ( value ) => {
setAttributes( {
midSize: parseInt( value, 10 ),
} );
} }
min={ 0 }
max={ 5 }
withInputField={ false }
/>
</PanelBody>
</InspectorControls>
<div { ...useBlockProps() }>{ paginationNumbers }</div>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function render_block_core_query_pagination_numbers( $attributes, $content, $blo
$wrapper_attributes = get_block_wrapper_attributes();
$content = '';
global $wp_query;
$mid_size = isset( $block->attributes['midSize'] ) ? (int) $block->attributes['midSize'] : null;
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
// Take into account if we have set a bigger `max page`
// than what the query has.
Expand All @@ -30,7 +31,10 @@ function render_block_core_query_pagination_numbers( $attributes, $content, $blo
'prev_next' => false,
'total' => $total,
);
$content = paginate_links( $paginate_args );
if ( null !== $mid_size ) {
$paginate_args['mid_size'] = $mid_size;
}
$content = paginate_links( $paginate_args );
} else {
$block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
// `paginate_links` works with the global $wp_query, so we have to
Expand All @@ -45,6 +49,9 @@ function render_block_core_query_pagination_numbers( $attributes, $content, $blo
'total' => $total,
'prev_next' => false,
);
if ( null !== $mid_size ) {
$paginate_args['mid_size'] = $mid_size;
}
if ( 1 !== $page ) {
/**
* `paginate_links` doesn't use the provided `format` when the page is `1`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{
"name": "core/query-pagination-numbers",
"isValid": true,
"attributes": {},
"attributes": {
"midSize": 2
},
"innerBlocks": []
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@
{
"name": "core/query-pagination-numbers",
"isValid": true,
"attributes": {},
"attributes": {
"midSize": 2
},
"innerBlocks": []
},
{
Expand Down