Skip to content

Commit

Permalink
Fix undefined index warnings in Latest Comments & Latest Posts (#12149)
Browse files Browse the repository at this point in the history
In 53c975b, `prepare_attributes_for_render` was changed so that
the `$attributes` array that is passed to `render_callback` contains the
same keys as what is passed to the block on the front-end.

This caused some 'Undefined index' notices in the Latest Comments and
Latest Posts blocks. We need to be using `isset()` when using attributes
that do not have a default value defined.
  • Loading branch information
noisysocks authored Nov 21, 2018
1 parent eccb423 commit 114c443
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/src/latest-comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function gutenberg_render_block_core_latest_comments( $attributes = array() ) {
}

$class = 'wp-block-latest-comments';
if ( $attributes['align'] ) {
if ( isset( $attributes['align'] ) ) {
$class .= " align{$attributes['align']}";
}
if ( $attributes['displayAvatar'] ) {
Expand Down
19 changes: 11 additions & 8 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
* @return string Returns the post content with latest posts added.
*/
function render_block_core_latest_posts( $attributes ) {
$recent_posts = wp_get_recent_posts(
array(
'numberposts' => $attributes['postsToShow'],
'post_status' => 'publish',
'order' => $attributes['order'],
'orderby' => $attributes['orderBy'],
'category' => $attributes['categories'],
)
$args = array(
'numberposts' => $attributes['postsToShow'],
'post_status' => 'publish',
'order' => $attributes['order'],
'orderby' => $attributes['orderBy'],
);

if ( isset( $attributes['categories'] ) ) {
$args['categories'] = $attributes['categories'];
}

$recent_posts = wp_get_recent_posts( $args );

$list_items_markup = '';

foreach ( $recent_posts as $post ) {
Expand Down

0 comments on commit 114c443

Please sign in to comment.