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

Fix: Calendar block: Always show current month for non post types on the editor #13873

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
11 changes: 10 additions & 1 deletion packages/block-library/src/calendar/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ class CalendarEdit extends Component {
}

export default withSelect( ( select ) => {
const {
getEditedPostAttribute,
} = select( 'core/editor' );
const postType = getEditedPostAttribute( 'type' );
// Dates are used to overwrite year and month used on the calendar.
// This overwrite should only happen for 'post' post types.
// For other post types the calendar always displays the current month.
return {
date: select( 'core/editor' ).getEditedPostAttribute( 'date' ),
date: postType === 'post' ?
getEditedPostAttribute( 'date' ) :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Why is the block dependent on the current post being edited? Is the widget dependent on posts too? Doesn't seem like it since the widget can be used in pages without any post?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's complex, see my investigation summarized in this comment: #13772 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @youknowriad, In fact, the widget depends on the post and shows the calendar of the post publish date the widget is being shown in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a summary of the research I did while implementing the block:
The calendar widget renders the calendar of the month the current post was published, if the widget is an archives page it renders the calendar of that month, and if it is a neutral non-date specific page (homepage, taxonomy, author, and as @gziolo referred page and other cpts) it renders the calendar of the current month.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it :) I guess we'll have to revisit this once we start the widgets page as there won't be any getEditedPostAttribute selector there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is going to be an interesting challenge.

undefined,
};
} )( CalendarEdit );
22 changes: 13 additions & 9 deletions packages/block-library/src/calendar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@
* @return string Returns the block content.
*/
function render_block_core_calendar( $attributes ) {
global $monthnum, $year, $post;
global $monthnum, $year;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there lint options available to make it so this sort of error is impossible to introduce?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there are some tools created by the community that allows us to do that https://github.com/sirbrillig/phpcs-variable-analysis.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


$previous_monthnum = $monthnum;
$previous_year = $year;

if ( isset( $attributes['month'] ) ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$monthnum = $attributes['month'];
}

if ( isset( $attributes['year'] ) ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$year = $attributes['year'];
if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) {
$permalink_structure = get_option( 'permalink_structure' );
if (
strpos( $permalink_structure, '%monthnum%' ) !== false &&
strpos( $permalink_structure, '%year%' ) !== false
) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$monthnum = $attributes['month'];
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$year = $attributes['year'];
}
}

$custom_class_name = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
Expand Down