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

Created author block. #3250

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
108 changes: 108 additions & 0 deletions blocks/library/author/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* External Dependencies
*/
import { connect } from 'react-redux';

/**
* WordPress depensdencies
*/
import { Component, compose } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { PanelBody, withAPIData, Placeholder, Spinner } from '@wordpress/components';

/**
* Internal dependencies
*/
import InspectorControls from '../../inspector-controls';
import ToggleControl from '../../inspector-controls/toggle-control';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import BlockDescription from '../../block-description';
import SingleAuthor from '../../single-author';
import {
getEditedPostAttribute,
} from '../../../editor/selectors';

class AuthorBlock extends Component {
constructor() {
super( ...arguments );
this.toggleHideBio = this.toggleHideBio.bind( this );
this.toggleHideAvatar = this.toggleHideAvatar.bind( this );
this.toggleHideName = this.toggleHideName.bind( this );
}
toggleHideBio() {
const { attributes, setAttributes } = this.props;
setAttributes( { hideBio: ! attributes.hideBio } );
}

toggleHideAvatar() {
const { attributes, setAttributes } = this.props;
setAttributes( { hideAvatar: ! attributes.hideAvatar } );
}

toggleHideName() {
const { attributes, setAttributes } = this.props;
setAttributes( { hideName: ! attributes.hideName } );
}

render() {
const { focus, attributes, setAttributes, author } = this.props;
const { align, hideBio, hideAvatar, hideName } = attributes;
return [
! author.data && (
<Placeholder key="placeholder"
icon="admin-post"
label={ __( 'Author' ) }
>
<Spinner />
</Placeholder>
),
author.data && (
<SingleAuthor key="content"
avatar={ hideAvatar ? null : author.data.avatar_urls[ '96' ] }
bio={ hideBio ? null : author.data.description }
className={ align && `align${ align }` }
name={ hideName ? null : author.data.name }
/>
),
focus && (
<InspectorControls key="inspector">
<BlockDescription>
<p>{ __( 'Shows the post author' ) }</p>
</BlockDescription>
<PanelBody title={ __( 'Alignment' ) }>
<BlockAlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => setAttributes( { align: nextAlign } ) }
/>
</PanelBody>
<ToggleControl
label={ __( 'Hide bio' ) }
checked={ hideBio }
onChange={ this.toggleHideBio }
/>
<ToggleControl
label={ __( 'Hide avatar' ) }
checked={ hideAvatar }
onChange={ this.toggleHideAvatar }
/>
<ToggleControl
label={ __( 'Hide name' ) }
checked={ hideName }
onChange={ this.toggleHideName }
/>
</InspectorControls>
),
];
}
}

export default compose(
connect(
( state ) => ( {
postAuthorId: getEditedPostAttribute( state, 'author' ),
Copy link
Member

Choose a reason for hiding this comment

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

See also https://github.com/WordPress/gutenberg/pull/5602/files#r175522235 for a proposal where I think we could formalize the dependence on this post context via a new attribute source type.

} )
),
withAPIData( ( props ) => ( {
author: `/wp/v2/users/${ props.postAuthorId }`,
} ) )
)( AuthorBlock );
22 changes: 22 additions & 0 deletions blocks/library/author/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.editor-block-list__block[data-type="core/author"] {
&[data-align="center"] {
text-align: center;
}

&[data-align="right"] {
text-align: right;
}

&[data-align="left"],
&[data-align="right"] {
max-width: 350px;
.blocks-single-author{
max-width: 100%;
}
}
}
.blocks-single-author.with-bio {
img {
margin-right: 10px;
}
}
36 changes: 36 additions & 0 deletions blocks/library/author/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import './editor.scss';
import AuthorBlock from './block';
import { registerBlockType } from '../../api';

registerBlockType( 'core/author', {
title: __( 'Author' ),

icon: 'admin-users',

category: 'widgets',

supports: {
html: false,
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( [ 'wide', 'full', 'left', 'right' ].indexOf( align ) !== -1 ) {
return { 'data-align': align };
}
},

edit: AuthorBlock,

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

/**
* Renders the `core/author` block on server.
*
* @param array $attributes The block attributes.
*
* @return string Returns the core author block rendered.
*/
function gutenberg_render_block_core_author( $attributes ) {
$align = 'center';
if ( in_array( $attributes['align'], array( 'left', 'right', 'full' ), true ) ) {
$align = $attributes['align'];
}

$title_markup = $attributes['hideName'] ? '' : sprintf(
'<h2>%1$s</h2>',
esc_html( get_the_author_meta( 'display_name' ) )
);

$content_markup = '';
if ( ! $attributes['hideAvatar'] || ! $attributes['hideBio'] ) {
$img_markup = '';
if ( ! $attributes['hideAvatar'] ) {
$img_markup = sprintf(
'<img src="%1$s" class="%2$s" />',
esc_attr( get_avatar_url( get_the_author_meta( 'ID' ) ) ),
$attributes['hideBio'] ? 'aligncenter' : 'alignleft'
);
}
$content_markup .= sprintf(
'<p>%1$s%2$s</p>',
$img_markup,
$attributes['hideBio'] ? '' : esc_html( strip_tags( get_the_author_meta( 'description' ) ) )
);
}
$bio_class = $attributes['hideBio'] ? 'no-bio' : 'with-bio';
$class = "blocks-single-author align{$align} {$align} {$bio_class}";

return sprintf(
'<section class="%1$s">%2$s%3$s</section>',
esc_attr( $class ),
$title_markup,
$content_markup
);
}

register_block_type( 'core/author', array(
'render_callback' => 'gutenberg_render_block_core_author',
'attributes' => array(
'hideBio' =>
array(
'type' => 'boolean',
'default' => false,
),
'hideAvatar' =>
array(
'type' => 'boolean',
'default' => false,
),
'hideName' =>
array(
'type' => 'boolean',
'default' => false,
),
'align' =>
array(
'type' => 'string',
'default' => 'center',
),
),

) );
2 changes: 2 additions & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ import './video';
import './audio';
import './block';
import './paragraph';
import './author';

39 changes: 39 additions & 0 deletions blocks/single-author/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

/**
* External Depenedencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import './style.scss';

function SingleAuthor( { avatar, bio, className, name } ) {
const classes = classnames(
className,
'blocks-single-author',
{
'with-bio': bio,
'no-bio': ! bio,
}
);
return (
<section className={ classes }>
{ name && <h2> { name }</h2> }
{ ( avatar || bio ) && (
<p>
{ avatar && <img src={ avatar } alt={ __( 'avatar' ) } className={ bio ? 'alignleft' : 'aligncenter' } /> }
{ bio }
</p>
) }
</section>
);
}

export default SingleAuthor;
16 changes: 16 additions & 0 deletions blocks/single-author/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.blocks-single-author {
&.alignright, &.alignleft {
max-width: 50%;
}
&.with-bio {
text-align: left;
}
&.no-bio {
text-align: center;
}
p:after {
content: "";
clear: both;
display: block;
}
}
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__author.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:author {"hideBio":false,"align":"center"} /-->
14 changes: 14 additions & 0 deletions blocks/test/fixtures/core__author.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"uid": "_uid_0",
"name": "core/author",
"isValid": true,
"attributes": {
"hideBio": false,
"hideAvatar": false,
"hideName": false,
"align": "center"
},
"originalContent": ""
}
]
15 changes: 15 additions & 0 deletions blocks/test/fixtures/core__author.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"blockName": "core/author",
"attrs": {
"hideBio": false,
"align": "center"
},
"innerBlocks": [],
"innerHTML": ""
},
{
"attrs": {},
"innerHTML": "\n"
}
]
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__author.serialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:author /-->
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__author___hide.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:author {"hideBio":true,"hideAvatar":true,"hideName":true} /-->
14 changes: 14 additions & 0 deletions blocks/test/fixtures/core__author___hide.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"uid": "_uid_0",
"name": "core/author",
"isValid": true,
"attributes": {
"hideBio": true,
"hideAvatar": true,
"hideName": true,
"align": "center"
},
"originalContent": ""
}
]
16 changes: 16 additions & 0 deletions blocks/test/fixtures/core__author___hide.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"blockName": "core/author",
"attrs": {
"hideBio": true,
"hideAvatar": true,
"hideName": true
},
"innerBlocks": [],
"innerHTML": ""
},
{
"attrs": {},
"innerHTML": "\n"
}
]
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__author___hide.serialized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:author {"hideBio":true,"hideAvatar":true,"hideName":true} /-->
1 change: 1 addition & 0 deletions blocks/test/fixtures/core__author__right.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:author {"align":"right"} /-->
14 changes: 14 additions & 0 deletions blocks/test/fixtures/core__author__right.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"uid": "_uid_0",
"name": "core/author",
"isValid": true,
"attributes": {
"hideBio": false,
"hideAvatar": false,
"hideName": false,
"align": "right"
},
"originalContent": ""
}
]
Loading