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

[WIP] Add alignment for latest posts block #1183

Closed
wants to merge 9 commits into from
102 changes: 87 additions & 15 deletions blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
*/
import { Placeholder } from 'components';
import { __ } from 'i18n';
import classNames from 'classnames';

/**
* Internal dependencies
*/
import './style.scss';
import { registerBlockType } from '../../api';
import { getLatestPosts } from './data.js';
import InspectorControls from '../../inspector-controls';
import IconButton from '../../../components/icon-button';

registerBlockType( 'core/latestposts', {
title: __( 'Latest Posts' ),
Expand All @@ -21,6 +25,13 @@ registerBlockType( 'core/latestposts', {
poststoshow: 5,
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
return { 'data-align': align };
}
},

edit: class extends wp.element.Component {
constructor() {
super( ...arguments );
Expand All @@ -39,24 +50,85 @@ registerBlockType( 'core/latestposts', {

render() {
const { latestPosts } = this.state;
const { focus } = this.props;

if ( ! latestPosts.length ) {
return (
<Placeholder
icon="update"
label={ __( 'Loading latest posts, please wait' ) }
>
</Placeholder>
);
}
const alignments = [
{
icon: 'align-left',
title: wp.i18n.__( 'Align left' ),
isActive: ( { align } ) => 'left' === align,
value: 'left',
},
{
icon: 'align-center',
title: wp.i18n.__( 'Align center' ),
isActive: ( { align } ) => ! align || 'center' === align,
value: 'center',
},
{
icon: 'align-right',
title: wp.i18n.__( 'Align right' ),
isActive: ( { align } ) => 'right' === align,
value: 'right',
},
{
icon: 'align-wide',
title: __( 'Wide width' ),
isActive: ( { align } ) => 'wide' === align,
value: 'wide',
},
{
icon: 'align-full-width',
title: __( 'Full width' ),
isActive: ( { align } ) => 'full' === align,
value: 'full',
},
];

return (
<div className="blocks-latest-posts">
<ul>
{ latestPosts.map( ( post, i ) =>
<li key={ i }><a href={ post.link }>{ post.title.rendered }</a></li>
) }
</ul>
<div>
{ 0 === latestPosts.length
? (
<Placeholder
icon="update"
label={ __( 'Loading latest posts, please wait' ) }
>
</Placeholder>
)
: (
<div className="blocks-latest-posts">
<ul>
{ latestPosts.map( ( post, i ) =>
<li key={ i } target="_blank"><a href={ post.link }>{ post.title.rendered }</a></li>
) }
</ul>
</div>
)
}
{ focus &&
<InspectorControls>

<h4>{ wp.i18n.__( 'Alignment' ) }</h4>
<div>
{ alignments.map( ( alignment, index ) => (
<IconButton
key={ index }
icon={ alignment.icon }
label={ alignment.title }
onClick={ ( event ) => {
event.stopPropagation();
this.props.setAttributes( { align: alignment.value } );
} }
className={ classNames( 'components-toolbar__control', {
'is-active': alignment.isActive( this.props.attributes ),
} ) }
aria-pressed={ alignment.isActive( this.props.attributes ) }
focus={ focus && ! index }
/>
) ) }
</div>
</InspectorControls>
}
</div>
);
}
Expand Down
3 changes: 3 additions & 0 deletions blocks/library/latest-posts/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.blocks-latest-posts {
margin-left: 1em;
}
9 changes: 8 additions & 1 deletion lib/blocks/latest-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ function gutenberg_render_block_core_latest_posts( $attributes ) {
}
}

$align = 'center';
if ( isset( $attributes['align'] ) && in_array( $attributes['align'], array( 'left', 'right', 'wide', 'full' ), true ) ) {
$align = $attributes['align'];
}

$recent_posts = wp_get_recent_posts( array(
'numberposts' => $posts_to_show,
'post_status' => 'publish',
Expand All @@ -43,8 +48,10 @@ function gutenberg_render_block_core_latest_posts( $attributes ) {
$posts_content .= "<li><a href='{$post_permalink}'>{$post_title}</a></li>\n";
}

$class = 'blocks-latest-posts ' . esc_attr( 'align' . $align );

$block_content = <<<CONTENT
<div class="blocks-latest-posts">
<div class="{$class}">
<ul>
{$posts_content}
</ul>
Expand Down