-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 Archives block #7949
Merged
Merged
Add Archives block #7949
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
07da41e
Changes from PR #5495 squashed into single commit
talldan a15f0e0
Remove archived keywords, since the title is implicitly a keyword alr…
talldan eca4224
Use already destructured property `attributes` over `this.props.attri…
talldan 3677a65
Use title case for copy
talldan 480338c
Use `Disabled` element to prevent interaction with Archives Block edi…
talldan c785d48
Rename block.js to edit.js for consistency with other blocks
talldan ad5aa1d
Convert ArchivesEdit to a function component
talldan 0696d4c
Remove `isSelected` boolean and inline InspectorControls
talldan 65025c1
Render archives list container as `ul` when a list and `div` when a d…
talldan 3c964d3
Remove unnecessary React keys
mcsf 5c3ba36
Improve description copy
mcsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fragment } from '@wordpress/element'; | ||
import { | ||
PanelBody, | ||
ServerSideRender, | ||
ToggleControl, | ||
Disabled, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
InspectorControls, | ||
BlockAlignmentToolbar, | ||
BlockControls, | ||
} from '@wordpress/editor'; | ||
|
||
export default function ArchivesEdit( { attributes, setAttributes } ) { | ||
const { align, showPostCounts, displayAsDropdown } = attributes; | ||
|
||
return ( | ||
<Fragment> | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Archives Settings' ) }> | ||
<ToggleControl | ||
label={ __( 'Show Post Counts' ) } | ||
checked={ showPostCounts } | ||
onChange={ () => setAttributes( { showPostCounts: ! showPostCounts } ) } | ||
/> | ||
<ToggleControl | ||
label={ __( 'Display as Dropdown' ) } | ||
checked={ displayAsDropdown } | ||
onChange={ () => setAttributes( { displayAsDropdown: ! displayAsDropdown } ) } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<BlockControls> | ||
<BlockAlignmentToolbar | ||
value={ align } | ||
onChange={ ( nextAlign ) => { | ||
setAttributes( { align: nextAlign } ); | ||
} } | ||
controls={ [ 'left', 'center', 'right' ] } | ||
/> | ||
</BlockControls> | ||
<Disabled> | ||
<ServerSideRender block="core/archives" attributes={ attributes } /> | ||
</Disabled> | ||
</Fragment> | ||
); | ||
} |
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,39 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import edit from './edit'; | ||
|
||
export const name = 'core/archives'; | ||
|
||
export const settings = { | ||
title: __( 'Archives' ), | ||
|
||
description: __( 'Display a monthly archive of your site’s Posts.' ), | ||
|
||
icon: 'calendar-alt', | ||
|
||
category: 'widgets', | ||
|
||
supports: { | ||
html: false, | ||
}, | ||
|
||
getEditWrapperProps( attributes ) { | ||
const { align } = attributes; | ||
if ( 'left' === align || 'right' === align || 'center' === align ) { | ||
return { 'data-align': align }; | ||
} | ||
}, | ||
|
||
edit, | ||
|
||
save() { | ||
// Handled by PHP. | ||
return 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,111 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/archives` block. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Renders the `core/archives` block on server. | ||
* | ||
* @see WP_Widget_Archives | ||
* | ||
* @param array $attributes The block attributes. | ||
* | ||
* @return string Returns the post content with archives added. | ||
*/ | ||
function render_block_core_archives( $attributes ) { | ||
$show_post_count = ! empty( $attributes['showPostCounts'] ); | ||
$class = "wp-block-archives align{$attributes['align']}"; | ||
|
||
if ( ! empty( $attributes['displayAsDropdown'] ) ) { | ||
|
||
$dropdown_id = esc_attr( uniqid( 'wp-block-archives-' ) ); | ||
$title = __( 'Archives', 'gutenberg' ); | ||
|
||
/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ | ||
$dropdown_args = apply_filters( 'widget_archives_dropdown_args', array( | ||
'type' => 'monthly', | ||
'format' => 'option', | ||
'show_post_count' => $show_post_count, | ||
) ); | ||
|
||
$dropdown_args['echo'] = 0; | ||
|
||
$archives = wp_get_archives( $dropdown_args ); | ||
|
||
switch ( $dropdown_args['type'] ) { | ||
case 'yearly': | ||
$label = __( 'Select Year', 'gutenberg' ); | ||
break; | ||
case 'monthly': | ||
$label = __( 'Select Month', 'gutenberg' ); | ||
break; | ||
case 'daily': | ||
$label = __( 'Select Day', 'gutenberg' ); | ||
break; | ||
case 'weekly': | ||
$label = __( 'Select Week', 'gutenberg' ); | ||
break; | ||
default: | ||
$label = __( 'Select Post', 'gutenberg' ); | ||
break; | ||
} | ||
|
||
$label = esc_attr( $label ); | ||
|
||
$block_content = '<label class="screen-reader-text" for="' . $dropdown_id . '">' . $title . '</label> | ||
<select id="' . $dropdown_id . '" name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"> | ||
<option value="">' . $label . '</option>' . $archives . '</select>'; | ||
|
||
$block_content = sprintf( | ||
'<div class="%1$s">%2$s</div>', | ||
esc_attr( $class ), | ||
$block_content | ||
); | ||
} else { | ||
|
||
/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ | ||
$archives_args = apply_filters( 'widget_archives_args', array( | ||
'type' => 'monthly', | ||
'show_post_count' => $show_post_count, | ||
) ); | ||
|
||
$archives_args['echo'] = 0; | ||
|
||
$block_content = wp_get_archives( $archives_args ); | ||
|
||
$block_content = sprintf( | ||
'<ul class="%1$s">%2$s</ul>', | ||
esc_attr( $class ), | ||
$block_content | ||
); | ||
} | ||
|
||
return $block_content; | ||
} | ||
|
||
/** | ||
* Register archives block. | ||
*/ | ||
function register_block_core_archives() { | ||
register_block_type( 'core/archives', array( | ||
'attributes' => array( | ||
'showPostCounts' => array( | ||
'type' => 'boolean', | ||
'default' => false, | ||
), | ||
'displayAsDropdown' => array( | ||
'type' => 'boolean', | ||
'default' => false, | ||
), | ||
'align' => array( | ||
'type' => 'string', | ||
'default' => 'none', | ||
), | ||
), | ||
'render_callback' => 'render_block_core_archives', | ||
) ); | ||
} | ||
|
||
add_action( 'init', 'register_block_core_archives' ); |
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 @@ | ||
<!-- wp:archives {"showPostCounts":false,"displayAsDropdown":false} /--> |
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,10 @@ | ||
[ | ||
{ | ||
"uid": "_uid_0", | ||
"name": "core/archives", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
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,11 @@ | ||
[ | ||
{ | ||
"blockName": "core/archives", | ||
"attrs": { | ||
"showPostCounts": false, | ||
"displayAsDropdown": false | ||
}, | ||
"innerBlocks": [], | ||
"innerHTML": "" | ||
} | ||
] |
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 @@ | ||
<!-- wp:archives /--> |
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 @@ | ||
<!-- wp:archives {"showPostCounts":true,"displayAsDropdown":false} /--> |
10 changes: 10 additions & 0 deletions
10
core-blocks/test/fixtures/core__archives__showPostCounts.json
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,10 @@ | ||
[ | ||
{ | ||
"uid": "_uid_0", | ||
"name": "core/archives", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
11 changes: 11 additions & 0 deletions
11
core-blocks/test/fixtures/core__archives__showPostCounts.parsed.json
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,11 @@ | ||
[ | ||
{ | ||
"blockName": "core/archives", | ||
"attrs": { | ||
"showPostCounts": true, | ||
"displayAsDropdown": false | ||
}, | ||
"innerBlocks": [], | ||
"innerHTML": "" | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
core-blocks/test/fixtures/core__archives__showPostCounts.serialized.html
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 @@ | ||
<!-- wp:archives /--> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, filter
widget_archives_dropdown_args
can change the periodic interval, meaning "monthly" may become inaccurate. However, I could see filtering as a more advanced feature, and sticking with "monthly" offers the benefit of conveying the basic idea of the block more clearly than e.g. "Display a periodic archive […]". Because of this, I don't mind sticking with the current copy.