From 07da41ea49bd64d4526b845c38060ecde908e734 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Fri, 13 Jul 2018 18:06:48 +0800 Subject: [PATCH 01/11] Changes from PR #5495 squashed into single commit Co-authored-by: Pascal Birchler Co-authored-by: Miina Sikk Co-authored-by: Weston Ruter --- core-blocks/archives/block.js | 84 ++++++++++++++ core-blocks/archives/editor.scss | 4 + core-blocks/archives/index.js | 40 +++++++ core-blocks/archives/index.php | 105 ++++++++++++++++++ core-blocks/index.js | 2 + core-blocks/test/fixtures/core__archives.html | 1 + core-blocks/test/fixtures/core__archives.json | 10 ++ .../test/fixtures/core__archives.parsed.json | 11 ++ .../fixtures/core__archives.serialized.html | 1 + .../core__archives__showPostCounts.html | 1 + .../core__archives__showPostCounts.json | 10 ++ ...core__archives__showPostCounts.parsed.json | 11 ++ ...__archives__showPostCounts.serialized.html | 1 + 13 files changed, 281 insertions(+) create mode 100644 core-blocks/archives/block.js create mode 100644 core-blocks/archives/editor.scss create mode 100644 core-blocks/archives/index.js create mode 100644 core-blocks/archives/index.php create mode 100644 core-blocks/test/fixtures/core__archives.html create mode 100644 core-blocks/test/fixtures/core__archives.json create mode 100644 core-blocks/test/fixtures/core__archives.parsed.json create mode 100644 core-blocks/test/fixtures/core__archives.serialized.html create mode 100644 core-blocks/test/fixtures/core__archives__showPostCounts.html create mode 100644 core-blocks/test/fixtures/core__archives__showPostCounts.json create mode 100644 core-blocks/test/fixtures/core__archives__showPostCounts.parsed.json create mode 100644 core-blocks/test/fixtures/core__archives__showPostCounts.serialized.html diff --git a/core-blocks/archives/block.js b/core-blocks/archives/block.js new file mode 100644 index 00000000000000..c4524562b103eb --- /dev/null +++ b/core-blocks/archives/block.js @@ -0,0 +1,84 @@ +/** + * WordPress dependencies + */ +import { Component, Fragment } from '@wordpress/element'; +import { + PanelBody, + ServerSideRender, + ToggleControl, +} from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import { + InspectorControls, + BlockAlignmentToolbar, + BlockControls, +} from '@wordpress/editor'; + +import './editor.scss'; + +class ArchivesBlock extends Component { + constructor() { + super( ...arguments ); + + this.toggleShowPostCounts = this.toggleShowPostCounts.bind( this ); + this.toggleDisplayAsDropdown = this.toggleDisplayAsDropdown.bind( this ); + } + + toggleShowPostCounts() { + const { attributes, setAttributes } = this.props; + const { showPostCounts } = attributes; + + setAttributes( { showPostCounts: ! showPostCounts } ); + } + + toggleDisplayAsDropdown() { + const { attributes, setAttributes } = this.props; + const { displayAsDropdown } = attributes; + + setAttributes( { displayAsDropdown: ! displayAsDropdown } ); + } + + render() { + const { attributes, isSelected, setAttributes } = this.props; + const { align, showPostCounts, displayAsDropdown } = attributes; + + const inspectorControls = isSelected && ( + + + + + + + ); + + return ( + + { inspectorControls } + + { + setAttributes( { align: nextAlign } ); + } } + controls={ [ 'left', 'center', 'right' ] } + /> + + + + ); + } +} + +export default ArchivesBlock; diff --git a/core-blocks/archives/editor.scss b/core-blocks/archives/editor.scss new file mode 100644 index 00000000000000..bcfcf25d91e532 --- /dev/null +++ b/core-blocks/archives/editor.scss @@ -0,0 +1,4 @@ +.gutenberg .wp-block-archives a { + pointer-events: none; + cursor: default; +} diff --git a/core-blocks/archives/index.js b/core-blocks/archives/index.js new file mode 100644 index 00000000000000..785c8c4ae637a8 --- /dev/null +++ b/core-blocks/archives/index.js @@ -0,0 +1,40 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import ArchivesBlock from './block'; +export const name = 'core/archives'; + +export const settings = { + title: __( 'Archives' ), + + description: __( 'This block displays a monthly archive of your site’s Posts.' ), + + icon: 'calendar-alt', + + category: 'widgets', + + keywords: [ __( 'archives' ) ], + + supports: { + html: false, + }, + + getEditWrapperProps( attributes ) { + const { align } = attributes; + if ( 'left' === align || 'right' === align || 'center' === align ) { + return { 'data-align': align }; + } + }, + + edit: ArchivesBlock, + + save() { + // Handled by PHP. + return null; + }, +}; diff --git a/core-blocks/archives/index.php b/core-blocks/archives/index.php new file mode 100644 index 00000000000000..b6e6d06ab4e44d --- /dev/null +++ b/core-blocks/archives/index.php @@ -0,0 +1,105 @@ + '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 = ' + '; + } 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( + '
%2$s
', + 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' ); diff --git a/core-blocks/index.js b/core-blocks/index.js index 6782e3c5b6ef69..89366bdc8a071f 100644 --- a/core-blocks/index.js +++ b/core-blocks/index.js @@ -16,6 +16,7 @@ import * as image from './image'; import * as heading from './heading'; import * as quote from './quote'; import * as gallery from './gallery'; +import * as archives from './archives'; import * as audio from './audio'; import * as button from './button'; import * as categories from './categories'; @@ -56,6 +57,7 @@ export const registerCoreBlocks = () => { // Register all remaining core blocks. shortcode, + archives, audio, button, categories, diff --git a/core-blocks/test/fixtures/core__archives.html b/core-blocks/test/fixtures/core__archives.html new file mode 100644 index 00000000000000..68bda3aaef3fdf --- /dev/null +++ b/core-blocks/test/fixtures/core__archives.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/core-blocks/test/fixtures/core__archives.json b/core-blocks/test/fixtures/core__archives.json new file mode 100644 index 00000000000000..27fd284777b07b --- /dev/null +++ b/core-blocks/test/fixtures/core__archives.json @@ -0,0 +1,10 @@ +[ + { + "uid": "_uid_0", + "name": "core/archives", + "isValid": true, + "attributes": {}, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/core-blocks/test/fixtures/core__archives.parsed.json b/core-blocks/test/fixtures/core__archives.parsed.json new file mode 100644 index 00000000000000..f883870399030b --- /dev/null +++ b/core-blocks/test/fixtures/core__archives.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/archives", + "attrs": { + "showPostCounts": false, + "displayAsDropdown": false + }, + "innerBlocks": [], + "innerHTML": "" + } +] diff --git a/core-blocks/test/fixtures/core__archives.serialized.html b/core-blocks/test/fixtures/core__archives.serialized.html new file mode 100644 index 00000000000000..b8928752375cd9 --- /dev/null +++ b/core-blocks/test/fixtures/core__archives.serialized.html @@ -0,0 +1 @@ + diff --git a/core-blocks/test/fixtures/core__archives__showPostCounts.html b/core-blocks/test/fixtures/core__archives__showPostCounts.html new file mode 100644 index 00000000000000..bd1c93d1e1d336 --- /dev/null +++ b/core-blocks/test/fixtures/core__archives__showPostCounts.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/core-blocks/test/fixtures/core__archives__showPostCounts.json b/core-blocks/test/fixtures/core__archives__showPostCounts.json new file mode 100644 index 00000000000000..27fd284777b07b --- /dev/null +++ b/core-blocks/test/fixtures/core__archives__showPostCounts.json @@ -0,0 +1,10 @@ +[ + { + "uid": "_uid_0", + "name": "core/archives", + "isValid": true, + "attributes": {}, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/core-blocks/test/fixtures/core__archives__showPostCounts.parsed.json b/core-blocks/test/fixtures/core__archives__showPostCounts.parsed.json new file mode 100644 index 00000000000000..b016ecf02288de --- /dev/null +++ b/core-blocks/test/fixtures/core__archives__showPostCounts.parsed.json @@ -0,0 +1,11 @@ +[ + { + "blockName": "core/archives", + "attrs": { + "showPostCounts": true, + "displayAsDropdown": false + }, + "innerBlocks": [], + "innerHTML": "" + } +] diff --git a/core-blocks/test/fixtures/core__archives__showPostCounts.serialized.html b/core-blocks/test/fixtures/core__archives__showPostCounts.serialized.html new file mode 100644 index 00000000000000..b8928752375cd9 --- /dev/null +++ b/core-blocks/test/fixtures/core__archives__showPostCounts.serialized.html @@ -0,0 +1 @@ + From a15f0e0bcc75c0d1f13b496f3c2d5b9c0f07008e Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 16 Jul 2018 15:17:33 +0800 Subject: [PATCH 02/11] Remove archived keywords, since the title is implicitly a keyword already --- core-blocks/archives/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/core-blocks/archives/index.js b/core-blocks/archives/index.js index 785c8c4ae637a8..19036bff7fe80f 100644 --- a/core-blocks/archives/index.js +++ b/core-blocks/archives/index.js @@ -18,8 +18,6 @@ export const settings = { category: 'widgets', - keywords: [ __( 'archives' ) ], - supports: { html: false, }, From eca4224d6c93aedaf109647d01a87ff2908aeb12 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 16 Jul 2018 15:23:25 +0800 Subject: [PATCH 03/11] Use already destructured property `attributes` over `this.props.attributes` --- core-blocks/archives/block.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-blocks/archives/block.js b/core-blocks/archives/block.js index c4524562b103eb..6ffdf4f1ed5868 100644 --- a/core-blocks/archives/block.js +++ b/core-blocks/archives/block.js @@ -75,7 +75,7 @@ class ArchivesBlock extends Component { controls={ [ 'left', 'center', 'right' ] } /> - + ); } From 3677a65bb7c65322ac3639a901004cd5d7aaf197 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 16 Jul 2018 15:25:19 +0800 Subject: [PATCH 04/11] Use title case for copy --- core-blocks/archives/block.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-blocks/archives/block.js b/core-blocks/archives/block.js index 6ffdf4f1ed5868..fa433cd38b3ead 100644 --- a/core-blocks/archives/block.js +++ b/core-blocks/archives/block.js @@ -50,12 +50,12 @@ class ArchivesBlock extends Component { From 480338c9b1c415191c3eee386703ccb22759322f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 16 Jul 2018 15:56:41 +0800 Subject: [PATCH 05/11] Use `Disabled` element to prevent interaction with Archives Block editor view Use of `pointer-events` to disable interaction was problematic. It didn't disabled the dropdown version of the block and it doesn't prevent keyboard interaction. The Disabled component handles these situations. --- core-blocks/archives/block.js | 7 ++++--- core-blocks/archives/editor.scss | 4 ---- 2 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 core-blocks/archives/editor.scss diff --git a/core-blocks/archives/block.js b/core-blocks/archives/block.js index fa433cd38b3ead..c18fbd010e98ab 100644 --- a/core-blocks/archives/block.js +++ b/core-blocks/archives/block.js @@ -6,6 +6,7 @@ import { PanelBody, ServerSideRender, ToggleControl, + Disabled, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; @@ -18,8 +19,6 @@ import { BlockControls, } from '@wordpress/editor'; -import './editor.scss'; - class ArchivesBlock extends Component { constructor() { super( ...arguments ); @@ -75,7 +74,9 @@ class ArchivesBlock extends Component { controls={ [ 'left', 'center', 'right' ] } /> - + + + ); } diff --git a/core-blocks/archives/editor.scss b/core-blocks/archives/editor.scss deleted file mode 100644 index bcfcf25d91e532..00000000000000 --- a/core-blocks/archives/editor.scss +++ /dev/null @@ -1,4 +0,0 @@ -.gutenberg .wp-block-archives a { - pointer-events: none; - cursor: default; -} From c785d48311492a342a6abe95f6db69dfdb398f10 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 16 Jul 2018 16:07:48 +0800 Subject: [PATCH 06/11] Rename block.js to edit.js for consistency with other blocks --- core-blocks/archives/{block.js => edit.js} | 4 ++-- core-blocks/archives/index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename core-blocks/archives/{block.js => edit.js} (96%) diff --git a/core-blocks/archives/block.js b/core-blocks/archives/edit.js similarity index 96% rename from core-blocks/archives/block.js rename to core-blocks/archives/edit.js index c18fbd010e98ab..9f396dd39686d8 100644 --- a/core-blocks/archives/block.js +++ b/core-blocks/archives/edit.js @@ -19,7 +19,7 @@ import { BlockControls, } from '@wordpress/editor'; -class ArchivesBlock extends Component { +class ArchivesEdit extends Component { constructor() { super( ...arguments ); @@ -82,4 +82,4 @@ class ArchivesBlock extends Component { } } -export default ArchivesBlock; +export default ArchivesEdit; diff --git a/core-blocks/archives/index.js b/core-blocks/archives/index.js index 19036bff7fe80f..98a6be6067984d 100644 --- a/core-blocks/archives/index.js +++ b/core-blocks/archives/index.js @@ -6,7 +6,7 @@ import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ -import ArchivesBlock from './block'; +import edit from './edit'; export const name = 'core/archives'; export const settings = { @@ -29,7 +29,7 @@ export const settings = { } }, - edit: ArchivesBlock, + edit, save() { // Handled by PHP. From ad5aa1d291f957e56f1fbafdd374854859fe082d Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 16 Jul 2018 16:13:53 +0800 Subject: [PATCH 07/11] Convert ArchivesEdit to a function component --- core-blocks/archives/edit.js | 102 +++++++++++++---------------------- 1 file changed, 38 insertions(+), 64 deletions(-) diff --git a/core-blocks/archives/edit.js b/core-blocks/archives/edit.js index 9f396dd39686d8..262d7532bd083f 100644 --- a/core-blocks/archives/edit.js +++ b/core-blocks/archives/edit.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { Component, Fragment } from '@wordpress/element'; +import { Fragment } from '@wordpress/element'; import { PanelBody, ServerSideRender, @@ -19,67 +19,41 @@ import { BlockControls, } from '@wordpress/editor'; -class ArchivesEdit extends Component { - constructor() { - super( ...arguments ); - - this.toggleShowPostCounts = this.toggleShowPostCounts.bind( this ); - this.toggleDisplayAsDropdown = this.toggleDisplayAsDropdown.bind( this ); - } - - toggleShowPostCounts() { - const { attributes, setAttributes } = this.props; - const { showPostCounts } = attributes; - - setAttributes( { showPostCounts: ! showPostCounts } ); - } - - toggleDisplayAsDropdown() { - const { attributes, setAttributes } = this.props; - const { displayAsDropdown } = attributes; - - setAttributes( { displayAsDropdown: ! displayAsDropdown } ); - } - - render() { - const { attributes, isSelected, setAttributes } = this.props; - const { align, showPostCounts, displayAsDropdown } = attributes; - - const inspectorControls = isSelected && ( - - - - - - - ); - - return ( - - { inspectorControls } - - { - setAttributes( { align: nextAlign } ); - } } - controls={ [ 'left', 'center', 'right' ] } - /> - - - - - - ); - } +export default function ArchivesEdit( { attributes, isSelected, setAttributes } ) { + const { align, showPostCounts, displayAsDropdown } = attributes; + + const inspectorControls = isSelected && ( + + + setAttributes( { showPostCounts: ! showPostCounts } ) } + /> + setAttributes( { displayAsDropdown: ! displayAsDropdown } ) } + /> + + + ); + + return ( + + { inspectorControls } + + { + setAttributes( { align: nextAlign } ); + } } + controls={ [ 'left', 'center', 'right' ] } + /> + + + + + + ); } - -export default ArchivesEdit; From 0696d4cc2cf8cd1bca14fd9d4a06431915000102 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 16 Jul 2018 16:19:44 +0800 Subject: [PATCH 08/11] Remove `isSelected` boolean and inline InspectorControls --- core-blocks/archives/edit.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/core-blocks/archives/edit.js b/core-blocks/archives/edit.js index 262d7532bd083f..c39c81170adb60 100644 --- a/core-blocks/archives/edit.js +++ b/core-blocks/archives/edit.js @@ -19,29 +19,25 @@ import { BlockControls, } from '@wordpress/editor'; -export default function ArchivesEdit( { attributes, isSelected, setAttributes } ) { +export default function ArchivesEdit( { attributes, setAttributes } ) { const { align, showPostCounts, displayAsDropdown } = attributes; - const inspectorControls = isSelected && ( - - - setAttributes( { showPostCounts: ! showPostCounts } ) } - /> - setAttributes( { displayAsDropdown: ! displayAsDropdown } ) } - /> - - - ); - return ( - { inspectorControls } + + + setAttributes( { showPostCounts: ! showPostCounts } ) } + /> + setAttributes( { displayAsDropdown: ! displayAsDropdown } ) } + /> + + Date: Mon, 16 Jul 2018 16:29:22 +0800 Subject: [PATCH 09/11] Render archives list container as `ul` when a list and `div` when a dropdown --- core-blocks/archives/index.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/core-blocks/archives/index.php b/core-blocks/archives/index.php index b6e6d06ab4e44d..9c221590e5c31d 100644 --- a/core-blocks/archives/index.php +++ b/core-blocks/archives/index.php @@ -57,6 +57,12 @@ function render_block_core_archives( $attributes ) { $block_content = ' '; + + $block_content = sprintf( + '
%2$s
', + esc_attr( $class ), + $block_content + ); } else { /** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ @@ -68,13 +74,13 @@ function render_block_core_archives( $attributes ) { $archives_args['echo'] = 0; $block_content = wp_get_archives( $archives_args ); - } - $block_content = sprintf( - '
%2$s
', - esc_attr( $class ), - $block_content - ); + $block_content = sprintf( + '
    %2$s
', + esc_attr( $class ), + $block_content + ); + } return $block_content; } From 3c964d348a3246b3a5f863ee6d5d44213fd33edd Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 16 Jul 2018 13:42:28 +0100 Subject: [PATCH 10/11] Remove unnecessary React keys --- core-blocks/archives/edit.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-blocks/archives/edit.js b/core-blocks/archives/edit.js index c39c81170adb60..0a53c463a47d14 100644 --- a/core-blocks/archives/edit.js +++ b/core-blocks/archives/edit.js @@ -24,7 +24,7 @@ export default function ArchivesEdit( { attributes, setAttributes } ) { return ( - + - + { @@ -48,7 +48,7 @@ export default function ArchivesEdit( { attributes, setAttributes } ) { /> - + ); From 5c3ba3635cf9b37bd4d12a9728c836c7d1048132 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Mon, 16 Jul 2018 13:43:26 +0100 Subject: [PATCH 11/11] Improve description copy --- core-blocks/archives/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-blocks/archives/index.js b/core-blocks/archives/index.js index 98a6be6067984d..8a4e5e6531e656 100644 --- a/core-blocks/archives/index.js +++ b/core-blocks/archives/index.js @@ -7,12 +7,13 @@ import { __ } from '@wordpress/i18n'; * Internal dependencies */ import edit from './edit'; + export const name = 'core/archives'; export const settings = { title: __( 'Archives' ), - description: __( 'This block displays a monthly archive of your site’s Posts.' ), + description: __( 'Display a monthly archive of your site’s Posts.' ), icon: 'calendar-alt',