Skip to content

Commit

Permalink
Add server-side rendering
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lamosty committed May 24, 2017
1 parent e88a06f commit 497cee8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { registerBlock } from '../../api';
import { getLatestPosts } from './data.js';


registerBlock( 'core/latest-posts', {
registerBlock( 'core/latestposts', {
title: wp.i18n.__( 'Latest Posts' ),

icon: 'list-view',
Expand Down
37 changes: 37 additions & 0 deletions blocks/library/latest-posts/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

function gutenberg_block_core_latest_posts( $attributes ) {
$postsToShow = 5;

if ( array_key_exists( 'poststoshow', $attributes ) ) {
$postsToShow = $attributes['poststoshow'];
}

$recent_posts = wp_get_recent_posts( array(
'numberposts' => $postsToShow,
'post_status' => 'publish'
) );

$posts_content = '';

foreach( $recent_posts as $post ) {
$post_permalink = get_permalink( $post['ID'] );

$posts_content .= "<li><a href='{$post_permalink}'>{$post['post_title']}</a></li>\n";
}

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

return $block_content;
}

register_block( 'core/latestposts', array(
'render' => 'gutenberg_block_core_latest_posts'
) );
8 changes: 8 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @package gutenberg
*/

define( 'GUTENBERG__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'GUTENBERG__BLOCKS_LIBRARY_DIR', GUTENBERG__PLUGIN_DIR . 'blocks/library' );

/**
* Gutenberg's Menu.
*
Expand Down Expand Up @@ -473,3 +476,8 @@ function the_gutenberg_project() {
</div>
<?php
}

function gutenberg_load_blocks_server_side_redering() {
require_once GUTENBERG__BLOCKS_LIBRARY_DIR . '/latest-posts/index.php';
}
add_action( 'init', 'gutenberg_load_blocks_server_side_redering' );

0 comments on commit 497cee8

Please sign in to comment.