diff --git a/core-blocks/archives/block.js b/core-blocks/archives/block.js new file mode 100644 index 0000000000000..c4524562b103e --- /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 0000000000000..bcfcf25d91e53 --- /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 0000000000000..785c8c4ae637a --- /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 0000000000000..b6e6d06ab4e44 --- /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 eb19acfe8b10b..b8b9f77c118c4 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'; @@ -54,6 +55,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 0000000000000..68bda3aaef3fd --- /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 0000000000000..27fd284777b07 --- /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 0000000000000..f883870399030 --- /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 0000000000000..b8928752375cd --- /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 0000000000000..bd1c93d1e1d33 --- /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 0000000000000..27fd284777b07 --- /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 0000000000000..b016ecf02288d --- /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 0000000000000..b8928752375cd --- /dev/null +++ b/core-blocks/test/fixtures/core__archives__showPostCounts.serialized.html @@ -0,0 +1 @@ +