-
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
Add "Latest Posts" Block #870
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d9ab526
Add initial scaffolding for REST API Latest Posts block
lamosty b939cce
Load 5 latest posts into unordered list
lamosty a120720
Refactor to `latest-posts` instead of `rest-api-latest-posts`
lamosty 4426fe9
Move data fetchers to data.js file
lamosty 6facd28
Refactor edit() to render a React component
lamosty 484506a
Add poststoshow attribute with a default value 5
lamosty ec72c14
Add server-side rendering
lamosty 219c2ff
Use defaultProps to initialize postsToShow
lamosty ca5cc8e
Add various fixes according to the review
lamosty 6e5fe1e
Fix linting errors
lamosty 1ab4e32
Rebase and fix eslint/WP coding standards
lamosty 74cc4aa
Add fn which loads server-side rendering of blocks
lamosty 3927d22
Avoid small render* functions and put the whole rendering in render
lamosty c9c7146
Add full post content fixture
lamosty 64b30f1
Abort the latest posts query if component is unmounted before response
lamosty efb8746
Import __ from i18n instead of using wp.i18n.__
lamosty 3168983
Use class prop instead of React state to store posts request
lamosty d312525
Add basic posts to show attribute validation
lamosty 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
die( 'Silence is golden.' ); | ||
} | ||
|
||
define( 'GUTENBERG__BLOCKS_LIBRARY_DIR', GUTENBERG__PLUGIN_DIR . 'blocks/library' ); | ||
|
||
$wp_registered_blocks = array(); | ||
|
||
/** | ||
|
@@ -128,3 +130,12 @@ function do_blocks( $content ) { | |
return $new_content; | ||
} | ||
add_filter( 'the_content', 'do_blocks', 10 ); // BEFORE do_shortcode(). | ||
|
||
/** | ||
* Loads the server-side rendering of blocks. If your block supports | ||
* server-side rendering, add it here. | ||
*/ | ||
function gutenberg_load_blocks_server_side_rendering() { | ||
require_once GUTENBERG__BLOCKS_LIBRARY_DIR . '/latest-posts/index.php'; | ||
} | ||
add_action( 'init', 'gutenberg_load_blocks_server_side_rendering' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there are a few other issues here, not least that |
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.
These
define
s are duplicating code already in place elsewhere:gutenberg/lib/client-assets.php
Lines 13 to 22 in 3f727da
The
GUTENBERG__
(double underscore) convention is unique to the constants introduced here; others in the codebase do not use this convention. It seems unnecessary to me.I don't have a preference on whether this is done as a function or constants (the function is pretty fast), but it needs to be consistent in approach and naming.
Also,
gutenberg.php
should contain the bare minimum necessary to initialize the plugin, because it is going to be rewritten during the build process. These defines should probably happen in a newlib/constants.php
or similar.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.
Ah, I didn't notice that, my bad.
In a new PR, I'll fix those things.
Thanks!