-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Closed
Created author block. #3250
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ), | ||
} ) | ||
), | ||
withAPIData( ( props ) => ( { | ||
author: `/wp/v2/users/${ props.postAuthorId }`, | ||
} ) ) | ||
)( AuthorBlock ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
), | ||
|
||
) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ import './video'; | |
import './audio'; | ||
import './block'; | ||
import './paragraph'; | ||
import './author'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:author {"hideBio":false,"align":"center"} /--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:author /--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:author {"hideBio":true,"hideAvatar":true,"hideName":true} /--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:author {"hideBio":true,"hideAvatar":true,"hideName":true} /--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:author {"align":"right"} /--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.