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

Add calendar block v1 #13772

Merged
merged 4 commits into from
Feb 14, 2019
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
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
}
// Currently merged in core as `gutenberg_render_block_core_latest_comments`,
// expected to change soon.
if ( ! function_exists( 'render_block_core_calendar' ) ) {
require dirname( __FILE__ ) . '/../packages/block-library/src/calendar/index.php';
}
if ( ! function_exists( 'render_block_core_latest_comments' )
&& ! function_exists( 'gutenberg_render_block_core_latest_comments' ) ) {
require dirname( __FILE__ ) . '/../packages/block-library/src/latest-comments/index.php';
Expand Down
67 changes: 67 additions & 0 deletions packages/block-library/src/calendar/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* External dependencies
*/
import moment from 'moment';
import memoize from 'memize';

/**
* WordPress dependencies
*/
import {
Disabled,
ServerSideRender,
} from '@wordpress/components';
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';

class CalendarEdit extends Component {
constructor() {
super( ...arguments );
this.getYearMonth = memoize(
this.getYearMonth.bind( this ),
{ maxSize: 1 }
);
this.getServerSideAttributes = memoize(
this.getServerSideAttributes.bind( this ),
{ maxSize: 1 }
);
}

getYearMonth( date ) {
if ( ! date ) {
return {};
}
const momentDate = moment( date );
return {
year: momentDate.year(),
month: momentDate.month() + 1,
};
}

getServerSideAttributes( attributes, date ) {
return {
...attributes,
...this.getYearMonth( date ),
Copy link
Member

Choose a reason for hiding this comment

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

Why not set it on the server using WP functions?

Copy link
Member Author

Choose a reason for hiding this comment

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

When the user changes the publish date, even if the post is not yet saved we want the user to see the calendar of that publish date so we need a way to pass that information to the server, that exactly what we are doing here.
The attributes month and year are never saved, they are just transient attributes used by the server side render mechanism.

Copy link
Member

Choose a reason for hiding this comment

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

I still think it isn't a good approach. Let me explain on the post from my personal website:
https://gziolo.pl/2017/11/07/starter-kit-and-reusable-scripts/
If I visit it as of today you will see that the calendar in the sidebar renders the same month as when this post was published:
screen shot 2019-02-14 at 11 29 11

it behaves differently for the pages as it simply renders the current month:
screen shot 2019-02-14 at 11 31 31

There are also archive pages which follow the URL path:

screen shot 2019-02-14 at 11 33 46

This should also be the case when I want to edit my post in Gutenberg. That's why I suggested that we build logic on PHP side which tries the following:

  • if it is a post type get the publish date from the post metadata
  • if it is a page or custom CPT get the current month

@melchoyce does it make sense?

Copy link
Member

Choose a reason for hiding this comment

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

Thinking a bit more about it. I'm coming to the conclusion that it might be nice to explore the following UI improvement around month and date. What if we allow the author to pick what calendar would display, some options to consider:

  • inherit the post's date (this is what we have at the moment)
  • use the current date
  • let the author pick any date

/cc @mapk and @youknowriad for feedback

};
}

render() {
return (
<Disabled>
<ServerSideRender
block="core/calendar"
attributes={ this.getServerSideAttributes(
this.props.attributes,
this.props.date
) }
/>
</Disabled>
);
}
}

export default withSelect( ( select ) => {
return {
date: select( 'core/editor' ).getEditedPostAttribute( 'date' ),
};
} )( CalendarEdit );
33 changes: 33 additions & 0 deletions packages/block-library/src/calendar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import edit from './edit';

export const name = 'core/calendar';

export const settings = {
title: __( 'Calendar' ),

description: __( 'A calendar of your site’s posts.' ),

icon: 'calendar',

category: 'widgets',

keywords: [ __( 'posts' ), __( 'archive' ) ],

supports: {
align: true,
},

edit,

save() {
return null;
},
};
71 changes: 71 additions & 0 deletions packages/block-library/src/calendar/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Server-side rendering of the `core/calendar` block.
*
* @package WordPress
*/

/**
* Renders the `core/calendar` block on server.
*
* @param array $attributes The block attributes.
*
* @return string Returns the block content.
*/
function render_block_core_calendar( $attributes ) {
global $monthnum, $year, $post;
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, $post is never used. Any reason why phpcs doesn't scream?

$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'];
}

$custom_class_name = empty( $attributes['className'] ) ? '' : ' ' . $attributes['className'];
$align_class_name = empty( $attributes['align'] ) ? '' : ' ' . "align{$attributes['align']}";

return sprintf(
'<div class="%1$s">%2$s</div>',
esc_attr( 'wp-block-calendar' . $custom_class_name . $align_class_name ),
noisysocks marked this conversation as resolved.
Show resolved Hide resolved
get_calendar( true, false )
);

// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$monthnum = $previous_monthnum;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$year = $previous_year;
}

/**
* Registers the `core/calendar` block on server.
*/
function register_block_core_calendar() {
register_block_type(
'core/calendar',
array(
'attributes' => array(
'align' => array(
'type' => 'string',
),
'className' => array(
'type' => 'string',
),
'month' => array(
'type' => 'integer',
),
'year' => array(
'type' => 'integer',
),
),
'render_callback' => 'render_block_core_calendar',
)
);
}

add_action( 'init', 'register_block_core_calendar' );
37 changes: 37 additions & 0 deletions packages/block-library/src/calendar/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.wp-block-calendar {
text-align: center;

th,
tbody td {
padding: 4px;
border: 1px solid $light-gray-500;
}
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved

tfoot td {
border: none;
}

table {
width: 100%;
border-collapse: collapse;
font-family: $default-font;
}

table th {
font-weight: 440;
background: $light-gray-300;
}

a {
text-decoration: underline;
}

tfoot a {
color: $blue-medium-800;
}

table tbody,
table caption {
color: $dark-gray-600;
}
}
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as gallery from './gallery';
import * as archives from './archives';
import * as audio from './audio';
import * as button from './button';
import * as calendar from './calendar';
import * as categories from './categories';
import * as code from './code';
import * as columns from './columns';
Expand Down Expand Up @@ -68,6 +69,7 @@ export const registerCoreBlocks = () => {
archives,
audio,
button,
calendar,
categories,
code,
columns,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@import "./block/edit-panel/style.scss";
@import "./block/indicator/style.scss";
@import "./button/style.scss";
@import "./calendar/style.scss";
@import "./categories/style.scss";
@import "./columns/style.scss";
@import "./cover/style.scss";
Expand Down
1 change: 1 addition & 0 deletions test/integration/full-content/fixtures/core__calendar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:calendar /-->
10 changes: 10 additions & 0 deletions test/integration/full-content/fixtures/core__calendar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/calendar",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
18 changes: 18 additions & 0 deletions test/integration/full-content/fixtures/core__calendar.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"blockName": "core/calendar",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:calendar /-->