From 15ac75c23c161a4f71dfc5b9ddd59ac1daaacf3a Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 24 Aug 2017 11:25:47 -0400 Subject: [PATCH] Latest Posts: Move attributes definition server-side --- blocks/library/latest-posts/index.js | 22 --------- blocks/library/latest-posts/index.php | 46 ++++++++++--------- blocks/test/fixtures/core__latest-posts.json | 3 +- .../core__latest-posts__displayPostDate.json | 3 +- blocks/test/server-attributes.json | 25 +++++++++- 5 files changed, 53 insertions(+), 46 deletions(-) diff --git a/blocks/library/latest-posts/index.js b/blocks/library/latest-posts/index.js index 6e4e865ea5b27..7ccdc7a2ee2aa 100644 --- a/blocks/library/latest-posts/index.js +++ b/blocks/library/latest-posts/index.js @@ -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 ) { diff --git a/blocks/library/latest-posts/index.php b/blocks/library/latest-posts/index.php index 978f1374da40e..9335cdebdae7d 100644 --- a/blocks/library/latest-posts/index.php +++ b/blocks/library/latest-posts/index.php @@ -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', ) ); @@ -63,7 +44,7 @@ function gutenberg_render_block_core_latest_posts( $attributes ) { $list_items_markup .= "\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'; } @@ -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', ) ); diff --git a/blocks/test/fixtures/core__latest-posts.json b/blocks/test/fixtures/core__latest-posts.json index c1e9e6f8d4433..8f17313f15f96 100644 --- a/blocks/test/fixtures/core__latest-posts.json +++ b/blocks/test/fixtures/core__latest-posts.json @@ -7,7 +7,8 @@ "postsToShow": 5, "displayPostDate": false, "layout": "list", - "columns": 3 + "columns": 3, + "align": "center" } } ] diff --git a/blocks/test/fixtures/core__latest-posts__displayPostDate.json b/blocks/test/fixtures/core__latest-posts__displayPostDate.json index 238718c80ea86..58ed17cb9dc05 100644 --- a/blocks/test/fixtures/core__latest-posts__displayPostDate.json +++ b/blocks/test/fixtures/core__latest-posts__displayPostDate.json @@ -7,7 +7,8 @@ "postsToShow": 5, "displayPostDate": true, "layout": "list", - "columns": 3 + "columns": 3, + "align": "center" } } ] diff --git a/blocks/test/server-attributes.json b/blocks/test/server-attributes.json index 0967ef424bce6..ffbc5ddd6aad9 100644 --- a/blocks/test/server-attributes.json +++ b/blocks/test/server-attributes.json @@ -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" + } + } +} \ No newline at end of file