Skip to content

Commit

Permalink
Latest Posts: Move attributes definition server-side
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Sep 1, 2017
1 parent 7b2fd62 commit 15ac75c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
22 changes: 0 additions & 22 deletions blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ registerBlockType( 'core/latest-posts', {

keywords: [ __( 'recent posts' ) ],

attributes: {
postsToShow: {
type: 'number',
default: 5,
},
displayPostDate: {
type: 'boolean',
default: false,
},
layout: {
type: 'string',
default: 'list',
},
columns: {
type: 'number',
default: 3,
},
align: {
type: 'string',
},
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
Expand Down
46 changes: 25 additions & 21 deletions blocks/library/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,8 @@
* @return string Returns the post content with latest posts added.
*/
function gutenberg_render_block_core_latest_posts( $attributes ) {
$posts_to_show = 5;

if ( array_key_exists( 'postsToShow', $attributes ) ) {

// Basic attribute validation.
if (
is_numeric( $attributes['postsToShow'] ) &&
$attributes['postsToShow'] > 0 &&
$attributes['postsToShow'] < 100
) {
$posts_to_show = intval( $attributes['postsToShow'] );
}
}

$align = 'center';
if ( isset( $attributes['align'] ) && in_array( $attributes['align'], array( 'left', 'right', 'wide', 'full' ), true ) ) {
$align = $attributes['align'];
}

$recent_posts = wp_get_recent_posts( array(
'numberposts' => $posts_to_show,
'numberposts' => $attributes['postsToShow'],
'post_status' => 'publish',
) );

Expand Down Expand Up @@ -63,7 +44,7 @@ function gutenberg_render_block_core_latest_posts( $attributes ) {
$list_items_markup .= "</li>\n";
}

$class = "wp-block-latest-posts align{$align}";
$class = "wp-block-latest-posts align{$attributes['align']}";
if ( isset( $attributes['layout'] ) && 'grid' === $attributes['layout'] ) {
$class .= ' is-grid';
}
Expand All @@ -82,5 +63,28 @@ function gutenberg_render_block_core_latest_posts( $attributes ) {
}

register_block_type( 'core/latest-posts', array(
'attributes' => array(
'postsToShow' => array(
'type' => 'number',
'default' => 5,
),
'displayPostDate' => array(
'type' => 'boolean',
'default' => false,
),
'layout' => array(
'type' => 'string',
'default' => 'list',
),
'columns' => array(
'type' => 'number',
'default' => 3,
),
'align' => array(
'type' => 'string',
'default' => 'center',
),
),

'render_callback' => 'gutenberg_render_block_core_latest_posts',
) );
3 changes: 2 additions & 1 deletion blocks/test/fixtures/core__latest-posts.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"postsToShow": 5,
"displayPostDate": false,
"layout": "list",
"columns": 3
"columns": 3,
"align": "center"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"postsToShow": 5,
"displayPostDate": true,
"layout": "list",
"columns": 3
"columns": 3,
"align": "center"
}
}
]
25 changes: 24 additions & 1 deletion blocks/test/server-attributes.json
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
{}
{
"core\/latest-posts": {
"postsToShow": {
"type": "number",
"default": 5
},
"displayPostDate": {
"type": "boolean",
"default": false
},
"layout": {
"type": "string",
"default": "list"
},
"columns": {
"type": "number",
"default": 3
},
"align": {
"type": "string",
"default": "center"
}
}
}

0 comments on commit 15ac75c

Please sign in to comment.