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 "Archives" block #1518

Closed
wants to merge 1 commit into from
Closed
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
58 changes: 58 additions & 0 deletions blocks/library/archives/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* WordPress dependencies
*/
import { Component } from 'element';
Copy link
Member

Choose a reason for hiding this comment

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

Note with #2172, these need to be updated to prefix the dependencies with @wordpress/. You will need to perform a rebase against the latest version of master and apply your changes:

git fetch origin
git rebase origin/master

import { Placeholder } from 'components';
import { __ } from 'i18n';

/**
* Internal dependencies
*/
import { registerBlockType } from '../../api';
import InspectorControls from '../../inspector-controls';
import ToggleControl from '../../inspector-controls/toggle-control';

registerBlockType( 'core/archives', {
title: __( 'Archives' ),

icon: 'calendar-alt',

category: 'widgets',

defaultAttributes: {
count: true,
dropdown: false,
},

edit( { attributes, setAttributes, focus } ) {
const { count, dropdown } = attributes;
const toggleCount = () => setAttributes( { count: ! count } );
const toggleDropdown = () => setAttributes( { dropdown: ! dropdown } );
return [
focus && (
<InspectorControls key="inspector">
<ToggleControl
label={ __( 'Show post counts' ) }
checked={ !! count }
onChange={ toggleCount }
/>
<ToggleControl
label={ __( 'Display as dropdown' ) }
checked={ !! dropdown }
onChange={ toggleDropdown }
/>
</InspectorControls>
),
<Placeholder
icon="update"
key="placeholder"
label={ __( 'Loading archives, please wait' ) }
>
</Placeholder>,
];
},

save() {
return null;
},
} );
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import './html';
import './freeform';
import './latest-posts';
import './cover-image';
import './archives';
1 change: 1 addition & 0 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@

// Register server-side code for individual blocks.
require_once dirname( __FILE__ ) . '/lib/blocks/latest-posts.php';
require_once dirname( __FILE__ ) . '/lib/blocks/archives.php';
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be changed in light of #2014.

}
88 changes: 88 additions & 0 deletions lib/blocks/archives.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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 gutenberg_render_block_core_archives( $attributes ) {
$count = ! empty( $attributes['count'] ) ? '1' : '0';
$dropdown = ! empty( $attributes['dropdown'] ) ? '1' : '0';

if ( $dropdown ) {
// Todo: 123 should be a unique number, see WP_Widget_Archives class.
$dropdown_id = esc_attr( 'archives-dropdown-123' );
$title = __( 'Archives' );

/** 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' => $count,
) );

$dropdown_args['echo'] = 0;

$archives = wp_get_archives( $dropdown_args );

switch ( $dropdown_args['type'] ) {
case 'yearly':
$label = __( 'Select Year' );
break;
case 'monthly':
$label = __( 'Select Month' );
break;
case 'daily':
$label = __( 'Select Day' );
break;
case 'weekly':
$label = __( 'Select Week' );
break;
default:
$label = __( 'Select Post' );
break;
}

$label = esc_attr( $label );

$block_content = <<<CONTENT
<div class="blocks-archives">
<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}
</div>
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' => $count,
) );

$archives_args['echo'] = 0;

$archives = wp_get_archives( $archives_args );

$block_content = <<<CONTENT
<div class="blocks-archives">
{$archives}
</div>
CONTENT;
}

return $block_content;
}

register_block_type( 'core/archives', array(
'render' => 'gutenberg_render_block_core_archives',
) );