Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b5b25e9
Try pass get_block_wrapper_attributes the data it needs
oandregal Oct 20, 2020
c5a7c89
Update get_block_wrapper_attributes to get the data it needs as params
oandregal Oct 20, 2020
93f5244
Social Link: update get_block_wrapper_attributes
oandregal Oct 20, 2020
3f36a02
Archives: update get_block_wrapper_attributes
oandregal Oct 20, 2020
cb00551
Calendar: update get_block_wrapper_attributes
oandregal Oct 20, 2020
5d4e745
Categories: update get_block_wrapper_attributes
oandregal Oct 20, 2020
071c525
Latest comments: update get_block_wrapper_attributes
oandregal Oct 20, 2020
822dfae
Archives: fix phpdoc
oandregal Oct 20, 2020
304b717
Calendar: fix phpdoc
oandregal Oct 20, 2020
47a9c19
Categories: fix phpdoc
oandregal Oct 20, 2020
ec44737
Latest Posts: update get_block_wrapper_attributes
oandregal Oct 20, 2020
9dd7e5a
Navigation: update get_block_wrapper_attributes
oandregal Oct 20, 2020
c973dc1
NavigationLink: update get_block_wrapper_attributes
oandregal Oct 20, 2020
53811ab
PostAuthor: update get_block_wrapper_attributes
oandregal Oct 20, 2020
150f570
PostCommentAuthor: update get_block_wrapper_attributes
oandregal Oct 20, 2020
b39488b
PostCommentContent: update get_block_wrapper_attributes
oandregal Oct 20, 2020
24ec6be
PostCommentDate: update get_block_wrapper_attributes
oandregal Oct 20, 2020
2384492
PostComments: update get_block_wrapper_attributes
oandregal Oct 20, 2020
a85117d
PostCommentsCount: update get_block_wrapper_attributes
oandregal Oct 20, 2020
7cab217
PostCommentsForm: update get_block_wrapper_attributes
oandregal Oct 20, 2020
51661af
PostContent: update get_block_wrapper_attributes
oandregal Oct 20, 2020
3d3b33d
PostDate: update get_block_wrapper_attributes
oandregal Oct 20, 2020
300acd5
PostExcerpt: update get_block_wrapper_attributes
oandregal Oct 20, 2020
263e819
PostFeaturedImage: update get_block_wrapper_attributes
oandregal Oct 20, 2020
859a883
PostHierarchicalTerms: update get_block_wrapper_attributes
oandregal Oct 20, 2020
b4b010c
PostTags: update get_block_wrapper_attributes
oandregal Oct 20, 2020
f57c461
PostTitle: update get_block_wrapper_attributes
oandregal Oct 20, 2020
7cb972c
RSS: update get_block_wrapper_attributes
oandregal Oct 20, 2020
2a2723a
Search: update get_block_wrapper_attributes
oandregal Oct 20, 2020
c6bee53
SiteLogo: update get_block_wrapper_attributes
oandregal Oct 20, 2020
ae31f08
SiteTagline: update get_block_wrapper_attributes
oandregal Oct 20, 2020
9fcea12
SiteTitle: update get_block_wrapper_attributes
oandregal Oct 20, 2020
fe490b2
TagClound: update get_block_wrapper_attributes
oandregal Oct 20, 2020
d088f1b
TemplatePart: update get_block_wrapper_attributes
oandregal Oct 20, 2020
ae1e288
Update test
oandregal Oct 20, 2020
f11475b
Remove current_parsed_block global
oandregal Oct 20, 2020
bc31f22
Make linter happy
oandregal Oct 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions lib/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ public function register( $block_support_name, $block_support_config ) {
* Generates an array of HTML attributes, such as classes, by applying to
* the given block all of the features that the block supports.
*
* @param array $parsed_block Block as parsed from content.
* @param string $block_name The name of the block to be processed.
* @param array $block_attributes The attributes of the block to be processed.
* @return array Array of HTML attributes.
*/
public function apply_block_supports( $parsed_block ) {
$block_attributes = $parsed_block['attrs'];
$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
$parsed_block['blockName']
);
public function apply_block_supports( $block_name, $block_attributes ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );

// If no render_callback, assume styles have been previously handled.
if ( ! $block_type || empty( $block_type ) ) {
Expand Down Expand Up @@ -139,13 +137,14 @@ private function register_attributes() {
* Generates a string of attributes by applying to the current block being
* rendered all of the features that the block supports.
*
* @param array $extra_attributes Optional. Extra attributes to render on the block wrapper.
* @param string $block_name The name of the block to be processed.
* @param array $block_attributes The attributes of the block to be processed.
* @param array $extra_attributes Optional. Extra attributes to render on the block wrapper.
*
* @return string String of HTML classes.
*/
function get_block_wrapper_attributes( $extra_attributes = array() ) {
global $current_parsed_block;
$new_attributes = WP_Block_Supports::get_instance()->apply_block_supports( $current_parsed_block );
function get_block_wrapper_attributes( $block_name, $block_attributes, $extra_attributes = array() ) {
$new_attributes = WP_Block_Supports::get_instance()->apply_block_supports( $block_name, $block_attributes );

if ( empty( $new_attributes ) && empty( $extra_attributes ) ) {
return '';
Expand Down
12 changes: 3 additions & 9 deletions lib/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ public function __get( $name ) {
*/
public function render( $options = array() ) {
global $post;
global $current_parsed_block;
$options = array_replace(
array(
'dynamic' => true,
Expand All @@ -217,14 +216,9 @@ public function render( $options = array() ) {
if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
$index = 0;
foreach ( $this->inner_content as $chunk ) {
if ( is_string( $chunk ) ) {
$block_content .= $chunk;
} else {
$parent_parsed_block = $current_parsed_block;
$current_parsed_block = $this->inner_blocks[ $index ]->parsed_block;
$block_content .= $this->inner_blocks[ $index++ ]->render();
$current_parsed_block = $parent_parsed_block;
}
$block_content .= is_string( $chunk ) ?
$chunk :
$this->inner_blocks[ $index++ ]->render();
}
}

Expand Down
10 changes: 0 additions & 10 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,11 @@ function gutenberg_replace_default_block_categories( $default_categories ) {
}
add_filter( 'block_categories', 'gutenberg_replace_default_block_categories' );

global $current_parsed_block;
$current_parsed_block = array(
'blockName' => null,
'attributes' => null,
);

/**
* Shim that hooks into `pre_render_block` so as to override `render_block` with
* a function that assigns block context.
*
* The context handling can be removed when plugin support requires WordPress 5.5.0+.
* The global current_parsed_block assignment can be removed when plugin support requires WordPress 5.6.0+.
*
* @see https://core.trac.wordpress.org/ticket/49927
* @see https://core.trac.wordpress.org/changeset/48243
Expand All @@ -389,7 +382,6 @@ function gutenberg_replace_default_block_categories( $default_categories ) {
*/
function gutenberg_render_block_with_assigned_block_context( $pre_render, $parsed_block ) {
global $post, $wp_query;
global $current_parsed_block;

/*
* If a non-null value is provided, a filter has run at an earlier priority
Expand All @@ -399,8 +391,6 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse
return $pre_render;
}

$current_parsed_block = $parsed_block;

$source_block = $parsed_block;

/** This filter is documented in src/wp-includes/blocks.php */
Expand Down
12 changes: 9 additions & 3 deletions packages/block-library/src/archives/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
*
* @see WP_Widget_Archives
*
* @param array $attributes The block attributes.
* @param Array $attributes The block attributes.
* @param String $content InnerBlocks content of the Block.
* @param WPBlock $block Block object.
*
* @return string Returns the post content with archives added.
*/
function render_block_core_archives( $attributes ) {
function render_block_core_archives( $attributes, $content, $block ) {
$show_post_count = ! empty( $attributes['showPostCounts'] );

$class = '';
Expand Down Expand Up @@ -97,7 +99,11 @@ function render_block_core_archives( $attributes ) {
);
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => $classnames )
);

return sprintf(
'<ul %1$s>%2$s</ul>',
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/calendar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
/**
* Renders the `core/calendar` block on server.
*
* @param array $attributes The block attributes.
* @param Array $attributes The block attributes.
* @param String $content InnerBlocks content of the Block.
* @param WPBlock $block Block object.
*
* @return string Returns the block content.
*/
function render_block_core_calendar( $attributes ) {
function render_block_core_calendar( $attributes, $content, $block ) {
global $monthnum, $year;

$previous_monthnum = $monthnum;
Expand All @@ -31,7 +33,10 @@ function render_block_core_calendar( $attributes ) {
}
}

$wrapper_attributes = get_block_wrapper_attributes();
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes
);
$output = sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
Expand Down
12 changes: 9 additions & 3 deletions packages/block-library/src/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
/**
* Renders the `core/categories` block on server.
*
* @param array $attributes The block attributes.
* @param Array $attributes The block attributes.
* @param String $content InnerBlocks content of the Block.
* @param WPBlock $block Block object.
*
* @return string Returns the categories list/dropdown markup.
*/
function render_block_core_categories( $attributes ) {
function render_block_core_categories( $attributes, $content, $block ) {
static $block_id = 0;
$block_id++;

Expand Down Expand Up @@ -47,7 +49,11 @@ function render_block_core_categories( $attributes ) {
$type = 'list';
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => "wp-block-categories-{$type}" ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => "wp-block-categories-{$type}" )
);

return sprintf(
$wrapper_markup,
Expand Down
12 changes: 9 additions & 3 deletions packages/block-library/src/latest-comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ function wp_latest_comments_draft_or_post_title( $post = 0 ) {
/**
* Renders the `core/latest-comments` block on server.
*
* @param array $attributes The block attributes.
* @param Array $attributes The block attributes.
* @param String $content InnerBlocks content of the Block.
* @param WPBlock $block Block object.
*
* @return string Returns the post content with latest comments added.
*/
function render_block_core_latest_comments( $attributes = array() ) {
function render_block_core_latest_comments( $attributes = array(), $content, $block ) {
// This filter is documented in wp-includes/widgets/class-wp-widget-recent-comments.php.
$comments = get_comments(
apply_filters(
Expand Down Expand Up @@ -129,7 +131,11 @@ function render_block_core_latest_comments( $attributes = array() ) {
if ( empty( $comments ) ) {
$classnames[] = 'no-comments';
}
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classnames ) ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => implode( ' ', $classnames ) )
);

return ! empty( $comments ) ? sprintf(
'<ol %1$s>%2$s</ol>',
Expand Down
12 changes: 9 additions & 3 deletions packages/block-library/src/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ function block_core_latest_posts_get_excerpt_length() {
/**
* Renders the `core/latest-posts` block on server.
*
* @param array $attributes The block attributes.
* @param Array $attributes The block attributes.
* @param String $content InnerBlocks content of the Block.
* @param WPBlock $block Block object.
*
* @return string Returns the post content with latest posts added.
*/
function render_block_core_latest_posts( $attributes ) {
function render_block_core_latest_posts( $attributes, $content, $block ) {
global $post, $block_core_latest_posts_excerpt_length;

$args = array(
Expand Down Expand Up @@ -171,7 +173,11 @@ function render_block_core_latest_posts( $attributes ) {
$class .= ' has-author';
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $class ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => $class )
);

return sprintf(
'<ul %1$s>%2$s</ul>',
Expand Down
8 changes: 5 additions & 3 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ function block_core_navigation_link_render_submenu_icon() {
/**
* Renders the `core/navigation-link` block.
*
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param array $block The parsed block.
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param WPBlock $block The parsed block.
*
* @return string Returns the post content with the legacy widget added.
*/
Expand Down Expand Up @@ -128,6 +128,8 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
};

$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array(
'class' => $css_classes . ( $has_submenu ? ' has-child' : '' ) .
( $is_active ? ' current-menu-item' : '' ),
Expand Down
8 changes: 5 additions & 3 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ function block_core_navigation_render_submenu_icon() {
/**
* Renders the `core/navigation` block on server.
*
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param array $block The parsed block.
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param WPBlock $block The parsed block.
*
* @return string Returns the post content with the legacy widget added.
*/
Expand Down Expand Up @@ -140,6 +140,8 @@ function render_block_core_navigation( $attributes, $content, $block ) {
}

$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array(
'class' => implode( ' ', $classes ),
'style' => $colors['inline_styles'] . $font_sizes['inline_styles'],
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/post-author/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ function render_block_core_post_author( $attributes, $content, $block ) {
isset( $attributes['textAlign'] ) ? array( 'has-text-align-' . $attributes['textAlign'] ) : array()
);

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => implode( ' ', $classes ) )
);

return sprintf( '<div %1$s>', $wrapper_attributes ) .
( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) .
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/post-comment-author/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ function render_block_core_post_comment_author( $attributes, $content, $block )
return '';
}

$wrapper_attributes = get_block_wrapper_attributes();
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes
);

return sprintf(
'<div %1$s>%2$s</div>',
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/post-comment-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ function render_block_core_post_comment_content( $attributes, $content, $block )
if ( ! isset( $block->context['commentId'] ) ) {
return '';
}
$wrapper_attributes = get_block_wrapper_attributes();
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes
);
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/post-comment-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ function render_block_core_post_comment_date( $attributes, $content, $block ) {
return '';
}

$wrapper_attributes = get_block_wrapper_attributes();
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes
);
return sprintf(
'<div %1$s><time datetime="%2$s">%3$s</time></div>',
$wrapper_attributes,
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/post-comments-count/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ function render_block_core_post_comments_count( $attributes, $content, $block )
$classes .= 'has-text-align-' . $attributes['textAlign'];
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => $classes )
);
return sprintf(
'<div class="%1$s">%2$s</div>',
$wrapper_attributes,
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/post-comments-form/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ function render_block_core_post_comments_form( $attributes, $content, $block ) {
ob_start();
comment_form( array(), $block->context['postId'] );
$form = ob_get_clean();
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => $classes )
);

return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $form );
}
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/post-comments/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function render_block_core_post_comments( $attributes, $content, $block ) {
$classes .= 'has-text-align-' . $attributes['textAlign'];
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => $classes )
);
$output = sprintf( '<div %1$s>', $wrapper_attributes ) . ob_get_clean() . '</div>';
return $output;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/block-library/src/post-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ function render_block_core_post_content( $attributes, $content, $block ) {
return '';
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'entry-content' ) );
$wrapper_attributes = get_block_wrapper_attributes(
$block->name,
$attributes,
array( 'class' => 'entry-content' )
);

return (
'<div ' . $wrapper_attributes . '>' .
Expand Down
Loading