-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add initial scaffolding for REST API Latest Posts block * Load 5 latest posts into unordered list * Refactor to `latest-posts` instead of `rest-api-latest-posts` * Move data fetchers to data.js file Props @mtias for the suggestion * Refactor edit() to render a React component Fixes the issue of having to use `attributes` to store the latest posts and is also nicer. * Add poststoshow attribute with a default value 5 * Add server-side rendering I also had to rename the block from core/latest-posts to core/latestposts since I think there's a bug in the server-side matcher -- it ignores block comments with hyphens: #882 * Use defaultProps to initialize postsToShow * Add various fixes according to the review * Fix linting errors * Rebase and fix eslint/WP coding standards * Add fn which loads server-side rendering of blocks This was lost in last rebase * Avoid small render* functions and put the whole rendering in render * Add full post content fixture * Abort the latest posts query if component is unmounted before response * Import __ from i18n instead of using wp.i18n.__ * Use class prop instead of React state to store posts request * Add basic posts to show attribute validation
- Loading branch information
Showing
10 changed files
with
181 additions
and
0 deletions.
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
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 |
---|---|---|
|
@@ -11,3 +11,4 @@ import './pullquote'; | |
import './table'; | ||
import './preformatted'; | ||
import './code'; | ||
import './latest-posts'; |
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,19 @@ | ||
/** | ||
* Returns a Promise with the latest posts or an error on failure. | ||
* | ||
* @param {Number} postsToShow Number of posts to display. | ||
* | ||
* @returns {wp.api.collections.Posts} Returns a Promise with the latest posts. | ||
*/ | ||
export function getLatestPosts( postsToShow = 5 ) { | ||
const postsCollection = new wp.api.collections.Posts(); | ||
|
||
const posts = postsCollection.fetch( { | ||
data: { | ||
per_page: postsToShow, | ||
}, | ||
} ); | ||
|
||
return posts; | ||
} | ||
|
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,74 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Placeholder } from 'components'; | ||
import { __ } from 'i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { registerBlockType } from '../../api'; | ||
import { getLatestPosts } from './data.js'; | ||
|
||
registerBlockType( 'core/latestposts', { | ||
title: __( 'Latest Posts' ), | ||
|
||
icon: 'list-view', | ||
|
||
category: 'rest-api', | ||
|
||
defaultAttributes: { | ||
poststoshow: 5, | ||
}, | ||
|
||
edit: class extends wp.element.Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
const { poststoshow } = this.props.attributes; | ||
|
||
this.state = { | ||
latestPosts: [], | ||
}; | ||
|
||
this.latestPostsRequest = getLatestPosts( poststoshow ); | ||
|
||
this.latestPostsRequest | ||
.then( latestPosts => this.setState( { latestPosts } ) ); | ||
} | ||
|
||
render() { | ||
const { latestPosts } = this.state; | ||
|
||
if ( ! latestPosts.length ) { | ||
return ( | ||
<Placeholder | ||
icon="update" | ||
label={ __( 'Loading latest posts, please wait' ) } | ||
> | ||
</Placeholder> | ||
); | ||
} | ||
|
||
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> | ||
); | ||
} | ||
}, | ||
|
||
componentWillUnmount() { | ||
if ( this.latestPostsRequest.state() === 'pending' ) { | ||
this.latestPostsRequest.abort(); | ||
} | ||
}, | ||
|
||
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,60 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/latest-posts` block. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Renders the `core/latest-posts` block on server. | ||
* | ||
* @param array $attributes The block attributes. | ||
* | ||
* @return string Returns the post content with latest posts added. | ||
*/ | ||
function gutenberg_block_core_latest_posts( $attributes ) { | ||
$posts_to_show = 5; | ||
|
||
if ( array_key_exists( 'poststoshow', $attributes ) ) { | ||
$posts_to_show_attr = $attributes['poststoshow']; | ||
|
||
// Basic attribute validation. | ||
if ( | ||
is_numeric( $posts_to_show_attr ) && | ||
$posts_to_show_attr > 0 && | ||
$posts_to_show_attr < 100 | ||
) { | ||
$posts_to_show = $attributes['poststoshow']; | ||
} | ||
} | ||
|
||
$recent_posts = wp_get_recent_posts( array( | ||
'numberposts' => $posts_to_show, | ||
'post_status' => 'publish', | ||
) ); | ||
|
||
$posts_content = ''; | ||
|
||
foreach ( $recent_posts as $post ) { | ||
$post_id = $post['ID']; | ||
$post_permalink = get_permalink( $post_id ); | ||
$post_title = get_the_title( $post_id ); | ||
|
||
$posts_content .= "<li><a href='{$post_permalink}'>{$post_title}</a></li>\n"; | ||
} | ||
|
||
$block_content = <<<CONTENT | ||
<div class="blocks-latest-posts"> | ||
<ul> | ||
{$posts_content} | ||
</ul> | ||
</div> | ||
CONTENT; | ||
|
||
return $block_content; | ||
} | ||
|
||
register_block_type( 'core/latestposts', array( | ||
'render' => 'gutenberg_block_core_latest_posts', | ||
) ); |
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,2 @@ | ||
<!-- wp:core/latestposts poststoshow="5" --><!-- /wp:core/latestposts --> | ||
|
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,9 @@ | ||
[ | ||
{ | ||
"uid": "_uid_0", | ||
"name": "core/latestposts", | ||
"attributes": { | ||
"poststoshow": 5 | ||
} | ||
} | ||
] |
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,2 @@ | ||
<!-- wp:core/latestposts poststoshow="5" --><!-- /wp:core/latestposts --> | ||
|
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
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