From 805d34b50db79b6bd3c5c0a5a572979c1daffd7d Mon Sep 17 00:00:00 2001 From: apeatling Date: Mon, 8 Jun 2020 10:58:22 -0700 Subject: [PATCH 01/47] Add experimental support settings for colors, fonts, and line height. --- packages/block-library/src/site-title/block.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/site-title/block.json b/packages/block-library/src/site-title/block.json index 4e4050aacc3a41..dabc71f1f00900 100644 --- a/packages/block-library/src/site-title/block.json +++ b/packages/block-library/src/site-title/block.json @@ -12,6 +12,9 @@ }, "supports": { "html": false, - "lightBlockWrapper": true + "lightBlockWrapper": true, + "__experimentalColor": true, + "__experimentalFontSize": true, + "__experimentalLineHeight": true } } From 05d22e835a4f5c6421c5eb68f06507df3dc1724b Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 11 Jun 2020 11:18:39 -0700 Subject: [PATCH 02/47] Add initial support for rendering of classes and styles for blocks rendered on the server side. --- lib/blocks.php | 187 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) diff --git a/lib/blocks.php b/lib/blocks.php index 80522bda953672..66dd6e33a6da05 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -296,3 +296,190 @@ function gutenberg_enqueue_editor_block_styles_assets() { } add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_editor_block_styles_assets' ); } + +/** + * Renders the classNames and styles for blocks + * + * @param string $block_content Output of the current block. + * @param array $block Block Object. + * @return string New block output. + */ +function gutenberg_experimental_apply_classnames_and_styles( $block_content, $block ) { + // Don't filter template part blocks since we filter the blocks in each template + // part individually. + if ( 'core/template-part' == $block[ 'blockName' ] ) { + return $block_content; + } + + // TODO: Check for supports: _experimental* before adding any of this to a block. + + if ( isset( $block['attrs'] ) ) { + $colors = gutenberg_experimental_build_css_colors( $block['attrs'] ); + $font_sizes = gutenberg_experimental_build_css_font_sizes( $block['attrs'] ); + + $extra_classes = array_merge( + $colors['background']['css_classes'], + $colors['text']['css_classes'], + $font_sizes['css_classes'], + isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), + isset( $block['attrs']['itemsJustification'] ) ? array( 'items-justified-' . $block['attrs']['itemsJustification'] ) : array(), + isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() + ); + $extra_styles = ( + $colors['text']['inline_styles'] || + $colors['background']['inline_styles'] || + $font_sizes['inline_styles'] + ) ? esc_attr( $colors['text']['inline_styles'] ) . + esc_attr( $colors['background']['inline_styles'] ) . + esc_attr( $font_sizes['inline_styles'] ) + : ''; + + $dom = new DOMDocument( '1.0', 'utf-8' ); + @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); + $xpath = new DOMXPath( $dom ); + $block_root = $xpath->query( "/*" )[0]; + + if ( empty( $block_root ) ) { + return $block_content; + } + + // Merge and dedupe new and existing classes and styles + $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'style' ) . ' ' ) . $extra_styles ) ) ); + + // Apply new styles and classes + if ( ! empty( $new_classes ) ) { + $block_root->setAttribute( 'class', $new_classes ); + } + + if ( ! empty( $new_styles ) ) { + $block_root->setAttribute( 'style', $new_styles ); + } + + return $dom->saveHtml(); + } + + return $block_content; +} +add_filter( 'render_block', 'gutenberg_experimental_apply_classnames_and_styles', 10, 2 ); + +/** + * Build an array with CSS classes and inline styles defining the colors + * which will be applied to the navigation markup in the front-end. + * + * @param array $attributes Navigation block attributes. + * @return array Colors CSS classes and inline styles. + */ +function gutenberg_experimental_build_css_colors( $attributes ) { + $text_colors = array( + 'css_classes' => array(), + 'inline_styles' => '', + ); + $background_colors = array( + 'css_classes' => array(), + 'inline_styles' => '', + ); + + // Text color. + $has_named_text_color = array_key_exists( 'textColor', $attributes ); + $has_custom_text_color = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'text', $attributes['style']['color'] ); + + // If has text color. + if ( $has_custom_text_color || $has_named_text_color ) { + // Add has-text-color class. + $text_colors['css_classes'][] = 'has-text-color'; + } + + if ( $has_named_text_color ) { + // Add the color class. + $text_colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); + } elseif ( $has_custom_text_color ) { + // Add the custom color inline style. + $text_colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); + } + + // Link colors. + $has_link_color = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'link', $attributes['style']['color'] ); + + if ( $has_link_color ) { + $text_colors['css_classes'][] = 'has-link-color'; + // If link is a named color. + if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { + // Get the name from the string and add proper styles. + $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; + $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); + $text_colors['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + } else { + $text_colors['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + } + } + // Background color. + $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); + $has_custom_background_color = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'background', $attributes['style']['color'] ); + + // Gradient color. + $has_named_gradient = array_key_exists( 'gradient', $attributes ); + $has_custom_gradient = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'gradient', $attributes['style']['color'] ); + + // If has background color. + if ( $has_custom_background_color || $has_named_background_color || $has_named_gradient || $has_custom_gradient ) { + // Add has-background-color class. + $background_colors['css_classes'][] = 'has-background-color'; + } + + if ( $has_named_background_color ) { + // Add the background-color class. + $background_colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); + } elseif ( $has_custom_background_color ) { + // Add the custom background-color inline style. + $background_colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); + } elseif ( $has_named_gradient ) { + $background_colors['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); + } elseif ( $has_custom_gradient ) { + $background_colors['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); + } + + return array( + 'text' => $text_colors, + 'background' => $background_colors, + ); +} + +/** + * Build an array with CSS classes and inline styles defining the font sizes + * which will be applied to the navigation markup in the front-end. + * + * @param array $attributes Navigation block attributes. + * @return array Font size CSS classes and inline styles. + */ +function gutenberg_experimental_build_css_font_sizes( $attributes ) { + // CSS classes. + $font_sizes = array( + 'css_classes' => array(), + 'inline_styles' => '', + ); + + $has_named_font_size = array_key_exists( 'fontSize', $attributes ); + $has_custom_font_size = array_key_exists( 'style', $attributes ) + && array_key_exists( 'typography', $attributes['style'] ) + && array_key_exists( 'fontSize', $attributes['style']['typography'] ); + + if ( $has_named_font_size ) { + // Add the font size class. + $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); + } elseif ( $has_custom_font_size ) { + // Add the custom font size inline style. + $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); + } + + return $font_sizes; +} + From d652a9f3ac232c1025c5c577de12602c0fc19d04 Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 11 Jun 2020 11:23:07 -0700 Subject: [PATCH 03/47] Remove custom item justification attribute. --- lib/blocks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index 66dd6e33a6da05..38ec9ea1d2f411 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -322,7 +322,6 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $colors['text']['css_classes'], $font_sizes['css_classes'], isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), - isset( $block['attrs']['itemsJustification'] ) ? array( 'items-justified-' . $block['attrs']['itemsJustification'] ) : array(), isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() ); $extra_styles = ( From 357942c0b861587f9f3154744c026086620a5ce4 Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 11 Jun 2020 11:30:02 -0700 Subject: [PATCH 04/47] Remove nav block references --- lib/blocks.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 38ec9ea1d2f411..3323dc6d601b35 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -364,9 +364,9 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl /** * Build an array with CSS classes and inline styles defining the colors - * which will be applied to the navigation markup in the front-end. + * which will be applied to the block markup in the front-end. * - * @param array $attributes Navigation block attributes. + * @param array $attributes block attributes. * @return array Colors CSS classes and inline styles. */ function gutenberg_experimental_build_css_colors( $attributes ) { @@ -454,9 +454,9 @@ function gutenberg_experimental_build_css_colors( $attributes ) { /** * Build an array with CSS classes and inline styles defining the font sizes - * which will be applied to the navigation markup in the front-end. + * which will be applied to the block markup in the front-end. * - * @param array $attributes Navigation block attributes. + * @param array $attributes block attributes. * @return array Font size CSS classes and inline styles. */ function gutenberg_experimental_build_css_font_sizes( $attributes ) { From a2db6d00f8f144afc3de48b1bbecbb464c59dc62 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 11 Jun 2020 15:21:26 -0400 Subject: [PATCH 05/47] add line height support --- lib/blocks.php | 30 ++++++++++++------- .../block-library/src/site-title/block.json | 4 ++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 3323dc6d601b35..27c0bc0e860426 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -315,22 +315,22 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl if ( isset( $block['attrs'] ) ) { $colors = gutenberg_experimental_build_css_colors( $block['attrs'] ); - $font_sizes = gutenberg_experimental_build_css_font_sizes( $block['attrs'] ); + $typography = gutenberg_experimental_build_css_typography( $block['attrs'] ); $extra_classes = array_merge( $colors['background']['css_classes'], $colors['text']['css_classes'], - $font_sizes['css_classes'], + $typography['css_classes'], isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() ); $extra_styles = ( $colors['text']['inline_styles'] || $colors['background']['inline_styles'] || - $font_sizes['inline_styles'] + $typography['inline_styles'] ) ? esc_attr( $colors['text']['inline_styles'] ) . esc_attr( $colors['background']['inline_styles'] ) . - esc_attr( $font_sizes['inline_styles'] ) + esc_attr( $typography['inline_styles'] ) : ''; $dom = new DOMDocument( '1.0', 'utf-8' ); @@ -430,8 +430,8 @@ function gutenberg_experimental_build_css_colors( $attributes ) { // If has background color. if ( $has_custom_background_color || $has_named_background_color || $has_named_gradient || $has_custom_gradient ) { - // Add has-background-color class. - $background_colors['css_classes'][] = 'has-background-color'; + // Add has-background class. + $background_colors['css_classes'][] = 'has-background'; } if ( $has_named_background_color ) { @@ -459,9 +459,9 @@ function gutenberg_experimental_build_css_colors( $attributes ) { * @param array $attributes block attributes. * @return array Font size CSS classes and inline styles. */ -function gutenberg_experimental_build_css_font_sizes( $attributes ) { +function gutenberg_experimental_build_css_typography( $attributes ) { // CSS classes. - $font_sizes = array( + $typography = array( 'css_classes' => array(), 'inline_styles' => '', ); @@ -473,12 +473,20 @@ function gutenberg_experimental_build_css_font_sizes( $attributes ) { if ( $has_named_font_size ) { // Add the font size class. - $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); + $typography['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); } elseif ( $has_custom_font_size ) { // Add the custom font size inline style. - $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); + $typography['inline_styles'] .= sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); } - return $font_sizes; + $has_line_height = array_key_exists( 'style', $attributes ) + && array_key_exists( 'typography', $attributes['style'] ) + && array_key_exists( 'lineHeight', $attributes['style']['typography'] ); + + if ( $has_line_height ) { + $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); + } + + return $typography; } diff --git a/packages/block-library/src/site-title/block.json b/packages/block-library/src/site-title/block.json index dabc71f1f00900..bc172dd36d90e2 100644 --- a/packages/block-library/src/site-title/block.json +++ b/packages/block-library/src/site-title/block.json @@ -13,7 +13,9 @@ "supports": { "html": false, "lightBlockWrapper": true, - "__experimentalColor": true, + "__experimentalColor": { + "gradients": true + }, "__experimentalFontSize": true, "__experimentalLineHeight": true } From 3a3095ba37fc766ac2e7b224f048bad94e59a724 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 12:55:38 -0400 Subject: [PATCH 06/47] consolodate colors arrays --- lib/blocks.php | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 27c0bc0e860426..281b27a898ec2e 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -318,18 +318,15 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $typography = gutenberg_experimental_build_css_typography( $block['attrs'] ); $extra_classes = array_merge( - $colors['background']['css_classes'], - $colors['text']['css_classes'], + $colors['css_classes'], $typography['css_classes'], isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() ); $extra_styles = ( - $colors['text']['inline_styles'] || - $colors['background']['inline_styles'] || + $colors['inline_styles'] || $typography['inline_styles'] - ) ? esc_attr( $colors['text']['inline_styles'] ) . - esc_attr( $colors['background']['inline_styles'] ) . + ) ? esc_attr( $colors['inline_styles'] ) . esc_attr( $typography['inline_styles'] ) : ''; @@ -370,11 +367,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl * @return array Colors CSS classes and inline styles. */ function gutenberg_experimental_build_css_colors( $attributes ) { - $text_colors = array( - 'css_classes' => array(), - 'inline_styles' => '', - ); - $background_colors = array( + $color_settings = array( 'css_classes' => array(), 'inline_styles' => '', ); @@ -388,15 +381,15 @@ function gutenberg_experimental_build_css_colors( $attributes ) { // If has text color. if ( $has_custom_text_color || $has_named_text_color ) { // Add has-text-color class. - $text_colors['css_classes'][] = 'has-text-color'; + $color_settings['css_classes'][] = 'has-text-color'; } if ( $has_named_text_color ) { // Add the color class. - $text_colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); + $color_settings['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); } elseif ( $has_custom_text_color ) { // Add the custom color inline style. - $text_colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); + $color_settings['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); } // Link colors. @@ -405,15 +398,15 @@ function gutenberg_experimental_build_css_colors( $attributes ) { && array_key_exists( 'link', $attributes['style']['color'] ); if ( $has_link_color ) { - $text_colors['css_classes'][] = 'has-link-color'; + $color_settings['css_classes'][] = 'has-link-color'; // If link is a named color. if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); - $text_colors['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); } else { - $text_colors['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); } } // Background color. @@ -431,25 +424,22 @@ function gutenberg_experimental_build_css_colors( $attributes ) { // If has background color. if ( $has_custom_background_color || $has_named_background_color || $has_named_gradient || $has_custom_gradient ) { // Add has-background class. - $background_colors['css_classes'][] = 'has-background'; + $color_settings['css_classes'][] = 'has-background'; } if ( $has_named_background_color ) { // Add the background-color class. - $background_colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); + $color_settings['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); } elseif ( $has_custom_background_color ) { // Add the custom background-color inline style. - $background_colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); + $color_settings['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); } elseif ( $has_named_gradient ) { - $background_colors['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); + $color_settings['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); } elseif ( $has_custom_gradient ) { - $background_colors['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); + $color_settings['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); } - return array( - 'text' => $text_colors, - 'background' => $background_colors, - ); + return $color_settings; } /** From 3eb8e4de695dd8a159d951e5c4986eb47efa0b8e Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 13:32:15 -0400 Subject: [PATCH 07/47] check support, return early if none --- lib/blocks.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/blocks.php b/lib/blocks.php index 281b27a898ec2e..e17f3dfd3ba58f 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -313,7 +313,15 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl // TODO: Check for supports: _experimental* before adding any of this to a block. + if ( isset( $block['attrs'] ) ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); + + if ( sizeof( $supports ) === 0 ) { + return $block_content; + } + $colors = gutenberg_experimental_build_css_colors( $block['attrs'] ); $typography = gutenberg_experimental_build_css_typography( $block['attrs'] ); From 871a2724e5fe99071aac63f88fc0fa4a6ef62e70 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 13:53:45 -0400 Subject: [PATCH 08/47] add support checks for colors --- lib/blocks.php | 81 ++++++++++++++++++++++--------------------- lib/global-styles.php | 1 + 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index e17f3dfd3ba58f..cf85b63648c4a2 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -322,8 +322,8 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl return $block_content; } - $colors = gutenberg_experimental_build_css_colors( $block['attrs'] ); - $typography = gutenberg_experimental_build_css_typography( $block['attrs'] ); + $colors = gutenberg_experimental_build_css_colors( $block['attrs'], $supports ); + $typography = gutenberg_experimental_build_css_typography( $block['attrs'], $supports ); $extra_classes = array_merge( $colors['css_classes'], @@ -374,24 +374,23 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl * @param array $attributes block attributes. * @return array Colors CSS classes and inline styles. */ -function gutenberg_experimental_build_css_colors( $attributes ) { +function gutenberg_experimental_build_css_colors( $attributes, $supports ) { $color_settings = array( 'css_classes' => array(), 'inline_styles' => '', ); - // Text color. - $has_named_text_color = array_key_exists( 'textColor', $attributes ); - $has_custom_text_color = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'text', $attributes['style']['color'] ); - - // If has text color. + // Text colors. + if( in_array( 'color', $supports ) ) { + $has_named_text_color = array_key_exists( 'textColor', $attributes ); + $has_custom_text_color = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'text', $attributes['style']['color'] ); + } if ( $has_custom_text_color || $has_named_text_color ) { // Add has-text-color class. $color_settings['css_classes'][] = 'has-text-color'; } - if ( $has_named_text_color ) { // Add the color class. $color_settings['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); @@ -401,47 +400,49 @@ function gutenberg_experimental_build_css_colors( $attributes ) { } // Link colors. - $has_link_color = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'link', $attributes['style']['color'] ); - + if( in_array( 'link-color', $supports ) ) { + $has_link_color = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'link', $attributes['style']['color'] ); + } if ( $has_link_color ) { $color_settings['css_classes'][] = 'has-link-color'; - // If link is a named color. - if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { - // Get the name from the string and add proper styles. - $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; - $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); - } else { - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); - } } - // Background color. - $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); - $has_custom_background_color = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'background', $attributes['style']['color'] ); - - // Gradient color. - $has_named_gradient = array_key_exists( 'gradient', $attributes ); - $has_custom_gradient = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'gradient', $attributes['style']['color'] ); - - // If has background color. + // If link is a named color. + if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { + // Get the name from the string and add proper styles. + $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; + $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + } else { + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + } + + // Background color & gradient. + if( in_array( 'background-color', $supports ) ) { + $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); + $has_custom_background_color = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'background', $attributes['style']['color'] ); + } + if ( in_array( 'background', $supports ) ) { + $has_named_gradient = array_key_exists( 'gradient', $attributes ); + $has_custom_gradient = array_key_exists( 'style', $attributes ) + && array_key_exists( 'color', $attributes['style'] ) + && array_key_exists( 'gradient', $attributes['style']['color'] ); + } if ( $has_custom_background_color || $has_named_background_color || $has_named_gradient || $has_custom_gradient ) { // Add has-background class. $color_settings['css_classes'][] = 'has-background'; } - if ( $has_named_background_color ) { // Add the background-color class. $color_settings['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); } elseif ( $has_custom_background_color ) { // Add the custom background-color inline style. $color_settings['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); - } elseif ( $has_named_gradient ) { + } + if ( $has_named_gradient ) { $color_settings['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); } elseif ( $has_custom_gradient ) { $color_settings['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); @@ -457,7 +458,7 @@ function gutenberg_experimental_build_css_colors( $attributes ) { * @param array $attributes block attributes. * @return array Font size CSS classes and inline styles. */ -function gutenberg_experimental_build_css_typography( $attributes ) { +function gutenberg_experimental_build_css_typography( $attributes, $supports ) { // CSS classes. $typography = array( 'css_classes' => array(), diff --git a/lib/global-styles.php b/lib/global-styles.php index 9e23f31eaba109..0971f3d8538687 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -273,6 +273,7 @@ function gutenberg_experimental_global_styles_get_supported_styles( $supports ) 'color' => array( '__experimentalColor' ), 'background-color' => array( '__experimentalColor' ), 'background' => array( '__experimentalColor', 'gradients' ), + 'link-color' => array( '__experimentalColor', 'linkColor' ), 'line-height' => array( '__experimentalLineHeight' ), 'font-size' => array( '__experimentalFontSize' ), ); From fa6567b851e120c72ac9e84649c7b4a04dd62c26 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 14:00:38 -0400 Subject: [PATCH 09/47] support checks for typography --- lib/blocks.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index cf85b63648c4a2..567df672408462 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -465,11 +465,12 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { 'inline_styles' => '', ); - $has_named_font_size = array_key_exists( 'fontSize', $attributes ); - $has_custom_font_size = array_key_exists( 'style', $attributes ) - && array_key_exists( 'typography', $attributes['style'] ) - && array_key_exists( 'fontSize', $attributes['style']['typography'] ); - + if( in_array( 'font-size', $supports ) ) { + $has_named_font_size = array_key_exists( 'fontSize', $attributes ); + $has_custom_font_size = array_key_exists( 'style', $attributes ) + && array_key_exists( 'typography', $attributes['style'] ) + && array_key_exists( 'fontSize', $attributes['style']['typography'] ); + } if ( $has_named_font_size ) { // Add the font size class. $typography['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); @@ -478,10 +479,11 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { $typography['inline_styles'] .= sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); } - $has_line_height = array_key_exists( 'style', $attributes ) - && array_key_exists( 'typography', $attributes['style'] ) - && array_key_exists( 'lineHeight', $attributes['style']['typography'] ); - + if ( in_array( 'line-height', $supports ) ) { + $has_line_height = array_key_exists( 'style', $attributes ) + && array_key_exists( 'typography', $attributes['style'] ) + && array_key_exists( 'lineHeight', $attributes['style']['typography'] ); + } if ( $has_line_height ) { $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); } From 76bc4f84a7a1ce4b0f253d25d135f0621abf1a5d Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 14:14:17 -0400 Subject: [PATCH 10/47] update comments --- lib/blocks.php | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 567df672408462..0e2eaeee47cd69 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -311,9 +311,6 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl return $block_content; } - // TODO: Check for supports: _experimental* before adding any of this to a block. - - if ( isset( $block['attrs'] ) ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); @@ -380,68 +377,72 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { 'inline_styles' => '', ); - // Text colors. + // Text Colors. + // Check support and attributes. if( in_array( 'color', $supports ) ) { $has_named_text_color = array_key_exists( 'textColor', $attributes ); $has_custom_text_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'text', $attributes['style']['color'] ); } + // Apply required generic class. if ( $has_custom_text_color || $has_named_text_color ) { - // Add has-text-color class. $color_settings['css_classes'][] = 'has-text-color'; } + // Apply color class or inline style. if ( $has_named_text_color ) { - // Add the color class. $color_settings['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); } elseif ( $has_custom_text_color ) { - // Add the custom color inline style. $color_settings['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); } - // Link colors. + // Link Colors. + // Check support and attributes. if( in_array( 'link-color', $supports ) ) { $has_link_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'link', $attributes['style']['color'] ); } + // Apply required class and style. if ( $has_link_color ) { $color_settings['css_classes'][] = 'has-link-color'; - } - // If link is a named color. - if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { - // Get the name from the string and add proper styles. - $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; - $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); - } else { - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + // If link is a named color. + if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { + // Get the name from the string and add proper styles. + $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; + $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + } else { + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + } } - // Background color & gradient. + // Background Color & Gradients. + // Check background color support and attributes. if( in_array( 'background-color', $supports ) ) { $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); $has_custom_background_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'background', $attributes['style']['color'] ); } + // Check gradient support and attributes. if ( in_array( 'background', $supports ) ) { $has_named_gradient = array_key_exists( 'gradient', $attributes ); $has_custom_gradient = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'gradient', $attributes['style']['color'] ); } + // Apply shared classes and styles. if ( $has_custom_background_color || $has_named_background_color || $has_named_gradient || $has_custom_gradient ) { - // Add has-background class. $color_settings['css_classes'][] = 'has-background'; } + // Apply background color classes or styles. if ( $has_named_background_color ) { - // Add the background-color class. $color_settings['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); } elseif ( $has_custom_background_color ) { - // Add the custom background-color inline style. $color_settings['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); } + // Apply gradient classes or styles. if ( $has_named_gradient ) { $color_settings['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); } elseif ( $has_custom_gradient ) { @@ -465,25 +466,27 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { 'inline_styles' => '', ); + // Font Size if( in_array( 'font-size', $supports ) ) { $has_named_font_size = array_key_exists( 'fontSize', $attributes ); $has_custom_font_size = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ) && array_key_exists( 'fontSize', $attributes['style']['typography'] ); } + // Apply required class or style. if ( $has_named_font_size ) { - // Add the font size class. $typography['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); } elseif ( $has_custom_font_size ) { - // Add the custom font size inline style. $typography['inline_styles'] .= sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); } + // Line Height. if ( in_array( 'line-height', $supports ) ) { $has_line_height = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ) && array_key_exists( 'lineHeight', $attributes['style']['typography'] ); } + // Add the style (no classes for line-height). if ( $has_line_height ) { $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); } From efc7c7b04cdeeb64f82b47886f3482d8c9a808e3 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 14:16:19 -0400 Subject: [PATCH 11/47] update function docs --- lib/blocks.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index 0e2eaeee47cd69..57ad5193c7048b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -312,9 +312,10 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl } if ( isset( $block['attrs'] ) ) { + // Check what style features the block supports. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); - + // Return early if nothing is supported. if ( sizeof( $supports ) === 0 ) { return $block_content; } @@ -369,6 +370,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl * which will be applied to the block markup in the front-end. * * @param array $attributes block attributes. + * @param array $supports style features the block attributes. * @return array Colors CSS classes and inline styles. */ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { @@ -457,6 +459,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { * which will be applied to the block markup in the front-end. * * @param array $attributes block attributes. + * @param array $supports style features the block attributes. * @return array Font size CSS classes and inline styles. */ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { From 7e659a99259d971298e0745eaf6531ee2d5fdd12 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 14:34:22 -0400 Subject: [PATCH 12/47] fix errors/warnings --- lib/blocks.php | 119 +++++++++++++++++++++++++++---------------------- 1 file changed, 65 insertions(+), 54 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 57ad5193c7048b..523a496c2bd8b4 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -314,7 +314,11 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl if ( isset( $block['attrs'] ) ) { // Check what style features the block supports. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); + if ( $block_type ) { + $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); + } else { + $supports = array(); + } // Return early if nothing is supported. if ( sizeof( $supports ) === 0 ) { return $block_content; @@ -380,75 +384,80 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { ); // Text Colors. - // Check support and attributes. + // Check support for text colors. if( in_array( 'color', $supports ) ) { $has_named_text_color = array_key_exists( 'textColor', $attributes ); $has_custom_text_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'text', $attributes['style']['color'] ); - } - // Apply required generic class. - if ( $has_custom_text_color || $has_named_text_color ) { - $color_settings['css_classes'][] = 'has-text-color'; - } - // Apply color class or inline style. - if ( $has_named_text_color ) { - $color_settings['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); - } elseif ( $has_custom_text_color ) { - $color_settings['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); + + // Apply required generic class. + if ( $has_custom_text_color || $has_named_text_color ) { + $color_settings['css_classes'][] = 'has-text-color'; + } + // Apply color class or inline style. + if ( $has_named_text_color ) { + $color_settings['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); + } elseif ( $has_custom_text_color ) { + $color_settings['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); + } } // Link Colors. - // Check support and attributes. if( in_array( 'link-color', $supports ) ) { $has_link_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'link', $attributes['style']['color'] ); - } - // Apply required class and style. - if ( $has_link_color ) { - $color_settings['css_classes'][] = 'has-link-color'; - // If link is a named color. - if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { - // Get the name from the string and add proper styles. - $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; - $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); - } else { - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + // Apply required class and style. + if ( $has_link_color ) { + $color_settings['css_classes'][] = 'has-link-color'; + // If link is a named color. + if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { + // Get the name from the string and add proper styles. + $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; + $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + } else { + $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + } } } - // Background Color & Gradients. - // Check background color support and attributes. + // Background Colors. if( in_array( 'background-color', $supports ) ) { $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); $has_custom_background_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'background', $attributes['style']['color'] ); + + // Apply required background class. + if ( $has_custom_background_color || $has_named_background_color ) { + $color_settings['css_classes'][] = 'has-background'; + } + // Apply background color classes or styles. + if ( $has_named_background_color ) { + $color_settings['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); + } elseif ( $has_custom_background_color ) { + $color_settings['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); + } } - // Check gradient support and attributes. + + // Gradients. if ( in_array( 'background', $supports ) ) { $has_named_gradient = array_key_exists( 'gradient', $attributes ); $has_custom_gradient = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'gradient', $attributes['style']['color'] ); - } - // Apply shared classes and styles. - if ( $has_custom_background_color || $has_named_background_color || $has_named_gradient || $has_custom_gradient ) { - $color_settings['css_classes'][] = 'has-background'; - } - // Apply background color classes or styles. - if ( $has_named_background_color ) { - $color_settings['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); - } elseif ( $has_custom_background_color ) { - $color_settings['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); - } - // Apply gradient classes or styles. - if ( $has_named_gradient ) { - $color_settings['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); - } elseif ( $has_custom_gradient ) { - $color_settings['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); + + if ( $has_named_gradient || $has_custom_gradient ) { + $color_settings['css_classes'][] = 'has-background'; + } + // Apply required background class. + if ( $has_named_gradient ) { + $color_settings['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); + } elseif ( $has_custom_gradient ) { + $color_settings['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); + } } return $color_settings; @@ -475,12 +484,13 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { $has_custom_font_size = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ) && array_key_exists( 'fontSize', $attributes['style']['typography'] ); - } - // Apply required class or style. - if ( $has_named_font_size ) { - $typography['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); - } elseif ( $has_custom_font_size ) { - $typography['inline_styles'] .= sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); + + // Apply required class or style. + if ( $has_named_font_size ) { + $typography['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); + } elseif ( $has_custom_font_size ) { + $typography['inline_styles'] .= sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); + } } // Line Height. @@ -488,10 +498,11 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { $has_line_height = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ) && array_key_exists( 'lineHeight', $attributes['style']['typography'] ); - } - // Add the style (no classes for line-height). - if ( $has_line_height ) { - $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); + + // Add the style (no classes for line-height). + if ( $has_line_height ) { + $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); + } } return $typography; From c215ae99ecb43fb5239f592124589f3bfce74417 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 14:34:56 -0400 Subject: [PATCH 13/47] formatting --- lib/blocks.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 523a496c2bd8b4..1308ef7d7c1981 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -301,13 +301,13 @@ function gutenberg_enqueue_editor_block_styles_assets() { * Renders the classNames and styles for blocks * * @param string $block_content Output of the current block. - * @param array $block Block Object. + * @param array $block Block Object. * @return string New block output. */ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $block ) { // Don't filter template part blocks since we filter the blocks in each template // part individually. - if ( 'core/template-part' == $block[ 'blockName' ] ) { + if ( 'core/template-part' == $block['blockName'] ) { return $block_content; } @@ -327,13 +327,13 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $colors = gutenberg_experimental_build_css_colors( $block['attrs'], $supports ); $typography = gutenberg_experimental_build_css_typography( $block['attrs'], $supports ); - $extra_classes = array_merge( + $extra_classes = array_merge( $colors['css_classes'], $typography['css_classes'], isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() ); - $extra_styles = ( + $extra_styles = ( $colors['inline_styles'] || $typography['inline_styles'] ) ? esc_attr( $colors['inline_styles'] ) . @@ -342,8 +342,8 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $dom = new DOMDocument( '1.0', 'utf-8' ); @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); - $xpath = new DOMXPath( $dom ); - $block_root = $xpath->query( "/*" )[0]; + $xpath = new DOMXPath( $dom ); + $block_root = $xpath->query( '/*' )[0]; if ( empty( $block_root ) ) { return $block_content; @@ -351,7 +351,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl // Merge and dedupe new and existing classes and styles $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); - $new_styles = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'style' ) . ' ' ) . $extra_styles ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'style' ) . ' ' ) . $extra_styles ) ) ); // Apply new styles and classes if ( ! empty( $new_classes ) ) { @@ -378,14 +378,14 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl * @return array Colors CSS classes and inline styles. */ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { - $color_settings = array( + $color_settings = array( 'css_classes' => array(), 'inline_styles' => '', ); // Text Colors. // Check support for text colors. - if( in_array( 'color', $supports ) ) { + if ( in_array( 'color', $supports ) ) { $has_named_text_color = array_key_exists( 'textColor', $attributes ); $has_custom_text_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) @@ -404,7 +404,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { } // Link Colors. - if( in_array( 'link-color', $supports ) ) { + if ( in_array( 'link-color', $supports ) ) { $has_link_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'link', $attributes['style']['color'] ); @@ -414,8 +414,8 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // If link is a named color. if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. - $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; - $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); + $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; + $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); } else { $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); @@ -424,7 +424,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { } // Background Colors. - if( in_array( 'background-color', $supports ) ) { + if ( in_array( 'background-color', $supports ) ) { $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); $has_custom_background_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) @@ -479,7 +479,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { ); // Font Size - if( in_array( 'font-size', $supports ) ) { + if ( in_array( 'font-size', $supports ) ) { $has_named_font_size = array_key_exists( 'fontSize', $attributes ); $has_custom_font_size = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ) From 0d40bad2418deb2ad62f97c6acd63a9b05dcbb3a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 12 Jun 2020 21:11:18 -0400 Subject: [PATCH 14/47] started a test --- phpunit/class-block-context-test.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/phpunit/class-block-context-test.php b/phpunit/class-block-context-test.php index f3a7fc1011a757..33c657a91357d1 100644 --- a/phpunit/class-block-context-test.php +++ b/phpunit/class-block-context-test.php @@ -197,4 +197,32 @@ function test_default_context_is_filterable() { $this->assertEquals( array( 'example' => 'ok' ), $provided_context[0] ); } + function test_supported_styles_applied_on_render() { + $block_type_settings = array( + 'attributes' => array( + 'textColor' => array( + 'type' => 'string' + ), + ), + 'supports' => array( + '__experimentalColor' => true + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( 'textColor' => 'red' ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + $block_content = '
So say we all.
'; + + $styled_block = apply_filters( 'render_block', $block_content, $block ); + + $expected_output = '
So say we all.
'; + + $this->assertEquals( $expected_output, $styled_block ); + } } From 229dcd68572631b50a674fb6a8dc333dc5fb85ab Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 14:05:33 -0400 Subject: [PATCH 15/47] moved to own file --- phpunit/class-block-context-test.php | 29 ------ phpunit/class-supported-block-styles-test.php | 93 +++++++++++++++++++ 2 files changed, 93 insertions(+), 29 deletions(-) create mode 100644 phpunit/class-supported-block-styles-test.php diff --git a/phpunit/class-block-context-test.php b/phpunit/class-block-context-test.php index 33c657a91357d1..98b48bb76778d7 100644 --- a/phpunit/class-block-context-test.php +++ b/phpunit/class-block-context-test.php @@ -196,33 +196,4 @@ function test_default_context_is_filterable() { $this->assertEquals( array( 'example' => 'ok' ), $provided_context[0] ); } - - function test_supported_styles_applied_on_render() { - $block_type_settings = array( - 'attributes' => array( - 'textColor' => array( - 'type' => 'string' - ), - ), - 'supports' => array( - '__experimentalColor' => true - ), - ); - $this->register_block_type( 'core/example', $block_type_settings ); - - $block = array( - 'blockName' => 'core/example', - 'attrs' => array( 'textColor' => 'red' ), - 'innerBlock' => array(), - 'innerContent' => array(), - 'innerHTML' => array(), - ); - $block_content = '
So say we all.
'; - - $styled_block = apply_filters( 'render_block', $block_content, $block ); - - $expected_output = '
So say we all.
'; - - $this->assertEquals( $expected_output, $styled_block ); - } } diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php new file mode 100644 index 00000000000000..8e3faccf590709 --- /dev/null +++ b/phpunit/class-supported-block-styles-test.php @@ -0,0 +1,93 @@ + 'example', + 'post_excerpt' => '', + ); + + $post = $this->factory()->post->create_and_get( $args ); + setup_postdata( $post ); + } + + /** + * Tear down each test method. + */ + public function tearDown() { + parent::tearDown(); + + while ( ! empty( $this->registered_block_names ) ) { + $block_name = array_pop( $this->registered_block_names ); + unregister_block_type( $block_name ); + } + } + + /** + * Registers a block type. + * + * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a + * complete WP_Block_Type instance. In case a WP_Block_Type + * is provided, the $args parameter will be ignored. + * @param array $args { + * Optional. Array of block type arguments. Any arguments may be defined, however the + * ones described below are supported by default. Default empty array. + * + * @type callable $render_callback Callback used to render blocks of this block type. + * } + */ + protected function register_block_type( $name, $args ) { + register_block_type( $name, $args ); + + $this->registered_block_names[] = $name; + } + + function test_supported_styles_applied_on_render() { + $block_type_settings = array( + 'attributes' => array( + 'textColor' => array( + 'type' => 'string' + ), + ), + 'supports' => array( + '__experimentalColor' => true + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( 'textColor' => 'red' ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + $block_content = '
So say we all.
'; + + $styled_block = apply_filters( 'render_block', $block_content, $block ); + + $expected_output = '
So say we all.
'; + + $this->assertEquals( $expected_output, $styled_block ); + } +} From e45898d945503a443c062b59ba5d922a8cebfafe Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 14:37:55 -0400 Subject: [PATCH 16/47] first test passes --- phpunit/class-supported-block-styles-test.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 8e3faccf590709..83629b5c04163c 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -62,6 +62,13 @@ protected function register_block_type( $name, $args ) { $this->registered_block_names[] = $name; } + private function get_attribute_from_block( $attribute, $block ) { + $pos = strpos( $block, $attribute . '="' ); + $split_arr = substr( $block, $pos + strlen( $attribute ) + 2 ); + $pos2 = strpos( $split_arr, '"' ); + return substr( $split_arr, 0, $pos2 ); + } + function test_supported_styles_applied_on_render() { $block_type_settings = array( 'attributes' => array( @@ -85,9 +92,10 @@ function test_supported_styles_applied_on_render() { $block_content = '
So say we all.
'; $styled_block = apply_filters( 'render_block', $block_content, $block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_output = '
So say we all.
'; + $expected_classes = 'has-text-color has-red-color'; - $this->assertEquals( $expected_output, $styled_block ); + $this->assertEquals( $expected_classes, $class_list ); } } From 977384bda7cbf63212fb31be37c4d21c53669ce4 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 14:51:17 -0400 Subject: [PATCH 17/47] styles test --- phpunit/class-supported-block-styles-test.php | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 83629b5c04163c..053b1c0dc50b67 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -63,13 +63,13 @@ protected function register_block_type( $name, $args ) { } private function get_attribute_from_block( $attribute, $block ) { - $pos = strpos( $block, $attribute . '="' ); - $split_arr = substr( $block, $pos + strlen( $attribute ) + 2 ); - $pos2 = strpos( $split_arr, '"' ); - return substr( $split_arr, 0, $pos2 ); + $start_index = strpos( $block, $attribute . '="' ) + strlen( $attribute ) + 2; + $split_arr = substr( $block, $start_index ); + $end_index = strpos( $split_arr, '"' ); + return substr( $split_arr, 0, $end_index ); } - function test_supported_styles_applied_on_render() { + function test_supported_color_classes_applied_on_render() { $block_type_settings = array( 'attributes' => array( 'textColor' => array( @@ -84,7 +84,7 @@ function test_supported_styles_applied_on_render() { $block = array( 'blockName' => 'core/example', - 'attrs' => array( 'textColor' => 'red' ), + 'attrs' => array( 'textColor' => 'red', 'backgroundColor' => 'black' ), 'innerBlock' => array(), 'innerContent' => array(), 'innerHTML' => array(), @@ -94,8 +94,38 @@ function test_supported_styles_applied_on_render() { $styled_block = apply_filters( 'render_block', $block_content, $block ); $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_classes = 'has-text-color has-red-color'; + $expected_classes = 'has-text-color has-red-color has-background has-black-background-color'; $this->assertEquals( $expected_classes, $class_list ); } + + function test_supported_color_styles_applied_on_render() { + $block_type_settings = array( + 'attributes' => array( + 'textColor' => array( + 'type' => 'string' + ), + ), + 'supports' => array( + '__experimentalColor' => true + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( 'style' => array( 'color' => array( 'text' => '#000', 'background' => '#fff' ) ) ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + $block_content = '
So say we all.
'; + + $styled_block = apply_filters( 'render_block', $block_content, $block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + + $expected_styles = 'color: #000;background-color: #fff;'; + + $this->assertEquals( $expected_styles, $style_list ); + } } From e1cfeaa7cca831084842b0f04b736e6c76664b67 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 14:55:39 -0400 Subject: [PATCH 18/47] unsupported colors test --- phpunit/class-supported-block-styles-test.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 053b1c0dc50b67..1233720e1a8f02 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -128,4 +128,34 @@ function test_supported_color_styles_applied_on_render() { $this->assertEquals( $expected_styles, $style_list ); } + + function test_unsupported_color_attrs_not_applied_on_render() { + $block_type_settings = array( + 'attributes' => array( + 'textColor' => array( + 'type' => 'string' + ), + ) + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( 'textColor' => 'red', 'backgroundColor' => 'black', 'style' => array( 'color' => array( 'text' => '#000', 'background' => '#fff' ) ) ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + $block_content = '
So say we all.
'; + + $styled_block = apply_filters( 'render_block', $block_content, $block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + + $expected_classes = ''; + $expected_styles = ''; + + $this->assertEquals( $expected_styles, $style_list ); + $this->assertEquals( $expected_classes, $class_list ); + } } From 680a69485c3ee3874e226b911a5a501d1d7c6189 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 15:03:03 -0400 Subject: [PATCH 19/47] php format --- phpunit/class-supported-block-styles-test.php | 67 +++++++++++++------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 1233720e1a8f02..836bebcd2b87dc 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -62,10 +62,16 @@ protected function register_block_type( $name, $args ) { $this->registered_block_names[] = $name; } + /** + * Retrieves attribute such as 'class' or 'style' from the rendered block string. + * + * @param string $attribute Name of attribute to get. + * @param string $block String of rendered block to check. + */ private function get_attribute_from_block( $attribute, $block ) { $start_index = strpos( $block, $attribute . '="' ) + strlen( $attribute ) + 2; - $split_arr = substr( $block, $start_index ); - $end_index = strpos( $split_arr, '"' ); + $split_arr = substr( $block, $start_index ); + $end_index = strpos( $split_arr, '"' ); return substr( $split_arr, 0, $end_index ); } @@ -73,18 +79,21 @@ function test_supported_color_classes_applied_on_render() { $block_type_settings = array( 'attributes' => array( 'textColor' => array( - 'type' => 'string' + 'type' => 'string', ), ), - 'supports' => array( - '__experimentalColor' => true + 'supports' => array( + '__experimentalColor' => true, ), ); $this->register_block_type( 'core/example', $block_type_settings ); - $block = array( + $block = array( 'blockName' => 'core/example', - 'attrs' => array( 'textColor' => 'red', 'backgroundColor' => 'black' ), + 'attrs' => array( + 'textColor' => 'red', + 'backgroundColor' => 'black', + ), 'innerBlock' => array(), 'innerContent' => array(), 'innerHTML' => array(), @@ -92,7 +101,7 @@ function test_supported_color_classes_applied_on_render() { $block_content = '
So say we all.
'; $styled_block = apply_filters( 'render_block', $block_content, $block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); $expected_classes = 'has-text-color has-red-color has-background has-black-background-color'; @@ -103,18 +112,25 @@ function test_supported_color_styles_applied_on_render() { $block_type_settings = array( 'attributes' => array( 'textColor' => array( - 'type' => 'string' + 'type' => 'string', ), ), - 'supports' => array( - '__experimentalColor' => true + 'supports' => array( + '__experimentalColor' => true, ), ); $this->register_block_type( 'core/example', $block_type_settings ); - $block = array( + $block = array( 'blockName' => 'core/example', - 'attrs' => array( 'style' => array( 'color' => array( 'text' => '#000', 'background' => '#fff' ) ) ), + 'attrs' => array( + 'style' => array( + 'color' => array( + 'text' => '#000', + 'background' => '#fff', + ), + ), + ), 'innerBlock' => array(), 'innerContent' => array(), 'innerHTML' => array(), @@ -122,7 +138,7 @@ function test_supported_color_styles_applied_on_render() { $block_content = '
So say we all.
'; $styled_block = apply_filters( 'render_block', $block_content, $block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); $expected_styles = 'color: #000;background-color: #fff;'; @@ -133,15 +149,24 @@ function test_unsupported_color_attrs_not_applied_on_render() { $block_type_settings = array( 'attributes' => array( 'textColor' => array( - 'type' => 'string' + 'type' => 'string', ), - ) + ), ); $this->register_block_type( 'core/example', $block_type_settings ); - $block = array( + $block = array( 'blockName' => 'core/example', - 'attrs' => array( 'textColor' => 'red', 'backgroundColor' => 'black', 'style' => array( 'color' => array( 'text' => '#000', 'background' => '#fff' ) ) ), + 'attrs' => array( + 'textColor' => 'red', + 'backgroundColor' => 'black', + 'style' => array( + 'color' => array( + 'text' => '#000', + 'background' => '#fff', + ), + ), + ), 'innerBlock' => array(), 'innerContent' => array(), 'innerHTML' => array(), @@ -149,11 +174,11 @@ function test_unsupported_color_attrs_not_applied_on_render() { $block_content = '
So say we all.
'; $styled_block = apply_filters( 'render_block', $block_content, $block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); $expected_classes = ''; - $expected_styles = ''; + $expected_styles = ''; $this->assertEquals( $expected_styles, $style_list ); $this->assertEquals( $expected_classes, $class_list ); From 65c9763b8fa8e296ca618ba02c1d7a85c6b11d83 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 15:11:16 -0400 Subject: [PATCH 20/47] add existing classes/styles --- phpunit/class-supported-block-styles-test.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 836bebcd2b87dc..ccb25012d3e7f0 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -98,12 +98,12 @@ function test_supported_color_classes_applied_on_render() { 'innerContent' => array(), 'innerHTML' => array(), ); - $block_content = '
So say we all.
'; + $block_content = '
So say we all.
'; $styled_block = apply_filters( 'render_block', $block_content, $block ); $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_classes = 'has-text-color has-red-color has-background has-black-background-color'; + $expected_classes = 'my-block foo-bar-class has-text-color has-red-color has-background has-black-background-color'; $this->assertEquals( $expected_classes, $class_list ); } @@ -135,12 +135,12 @@ function test_supported_color_styles_applied_on_render() { 'innerContent' => array(), 'innerHTML' => array(), ); - $block_content = '
So say we all.
'; + $block_content = '
So say we all.
'; $styled_block = apply_filters( 'render_block', $block_content, $block ); $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $expected_styles = 'color: #000;background-color: #fff;'; + $expected_styles = 'test:style; color: #000;background-color: #fff;'; $this->assertEquals( $expected_styles, $style_list ); } @@ -171,14 +171,14 @@ function test_unsupported_color_attrs_not_applied_on_render() { 'innerContent' => array(), 'innerHTML' => array(), ); - $block_content = '
So say we all.
'; + $block_content = '
So say we all.
'; $styled_block = apply_filters( 'render_block', $block_content, $block ); $style_list = $this->get_attribute_from_block( 'style', $styled_block ); $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_classes = ''; - $expected_styles = ''; + $expected_classes = 'my-block foo-bar-class'; + $expected_styles = 'test:style;'; $this->assertEquals( $expected_styles, $style_list ); $this->assertEquals( $expected_classes, $class_list ); From 9ec8584ef6ee5fef62ec334ec86e675d95d08086 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 16:32:33 -0400 Subject: [PATCH 21/47] add class check for style test --- phpunit/class-supported-block-styles-test.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index ccb25012d3e7f0..a7db6907a75cb1 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -18,17 +18,7 @@ class Block_Supported_Styles_Test extends WP_UnitTestCase { * Sets up each test method. */ public function setUp() { - global $post; - parent::setUp(); - - $args = array( - 'post_content' => 'example', - 'post_excerpt' => '', - ); - - $post = $this->factory()->post->create_and_get( $args ); - setup_postdata( $post ); } /** @@ -139,10 +129,13 @@ function test_supported_color_styles_applied_on_render() { $styled_block = apply_filters( 'render_block', $block_content, $block ); $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); $expected_styles = 'test:style; color: #000;background-color: #fff;'; + $expected_classes = 'my-block foo-bar-class has-text-color has-background'; $this->assertEquals( $expected_styles, $style_list ); + $this->assertEquals( $expected_classes, $class_list ); } function test_unsupported_color_attrs_not_applied_on_render() { From 5ca9b4732c342ad1906e9a41a6004c5ee7f18b67 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 16:57:54 -0400 Subject: [PATCH 22/47] link color tests --- phpunit/class-supported-block-styles-test.php | 108 +++++++++++++----- 1 file changed, 82 insertions(+), 26 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index a7db6907a75cb1..6b2f7311c521b0 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -65,20 +65,19 @@ private function get_attribute_from_block( $attribute, $block ) { return substr( $split_arr, 0, $end_index ); } - function test_supported_color_classes_applied_on_render() { + private $block_content = '
So say we all.
'; + + + function test_named_color_support() { $block_type_settings = array( - 'attributes' => array( - 'textColor' => array( - 'type' => 'string', - ), - ), + 'attributes' => array(), 'supports' => array( '__experimentalColor' => true, ), ); $this->register_block_type( 'core/example', $block_type_settings ); - $block = array( + $block = array( 'blockName' => 'core/example', 'attrs' => array( 'textColor' => 'red', @@ -88,30 +87,25 @@ function test_supported_color_classes_applied_on_render() { 'innerContent' => array(), 'innerHTML' => array(), ); - $block_content = '
So say we all.
'; - $styled_block = apply_filters( 'render_block', $block_content, $block ); + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_classes = 'my-block foo-bar-class has-text-color has-red-color has-background has-black-background-color'; + $expected_classes = 'wp-block-example foo-bar-class has-text-color has-red-color has-background has-black-background-color'; $this->assertEquals( $expected_classes, $class_list ); } - function test_supported_color_styles_applied_on_render() { + function test_custom_color_support() { $block_type_settings = array( - 'attributes' => array( - 'textColor' => array( - 'type' => 'string', - ), - ), + 'attributes' => array(), 'supports' => array( '__experimentalColor' => true, ), ); $this->register_block_type( 'core/example', $block_type_settings ); - $block = array( + $block = array( 'blockName' => 'core/example', 'attrs' => array( 'style' => array( @@ -125,20 +119,83 @@ function test_supported_color_styles_applied_on_render() { 'innerContent' => array(), 'innerHTML' => array(), ); - $block_content = '
So say we all.
'; - $styled_block = apply_filters( 'render_block', $block_content, $block ); + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); $style_list = $this->get_attribute_from_block( 'style', $styled_block ); $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_styles = 'test:style; color: #000;background-color: #fff;'; - $expected_classes = 'my-block foo-bar-class has-text-color has-background'; + $expected_styles = 'test:style; color: #000;background-color: #fff;'; + $expected_classes = 'wp-block-example foo-bar-class has-text-color has-background'; + + $this->assertEquals( $expected_styles, $style_list ); + $this->assertEquals( $expected_classes, $class_list ); + } + + function test_named_link_color_support() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalColor' => array( + 'linkColor' => true, + ), + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( 'color' => array( 'link' => 'var:preset|color|red' ) ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + + $expected_classes = 'wp-block-example foo-bar-class has-link-color'; + $expected_styles = 'test:style; --wp--style--color--link:var(--wp--preset--color--red);'; + $this->assertEquals( $expected_classes, $class_list ); $this->assertEquals( $expected_styles, $style_list ); + } + + function test_custom_link_color_support() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalColor' => array( + 'linkColor' => true, + ), + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( 'color' => array( 'link' => '#fff' ) ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + + $expected_classes = 'wp-block-example foo-bar-class has-link-color'; + $expected_styles = 'test:style; --wp--style--color--link: #fff;'; + $this->assertEquals( $expected_classes, $class_list ); + $this->assertEquals( $expected_styles, $style_list ); } - function test_unsupported_color_attrs_not_applied_on_render() { + function test_color_unsupported() { $block_type_settings = array( 'attributes' => array( 'textColor' => array( @@ -148,7 +205,7 @@ function test_unsupported_color_attrs_not_applied_on_render() { ); $this->register_block_type( 'core/example', $block_type_settings ); - $block = array( + $block = array( 'blockName' => 'core/example', 'attrs' => array( 'textColor' => 'red', @@ -164,13 +221,12 @@ function test_unsupported_color_attrs_not_applied_on_render() { 'innerContent' => array(), 'innerHTML' => array(), ); - $block_content = '
So say we all.
'; - $styled_block = apply_filters( 'render_block', $block_content, $block ); + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); $style_list = $this->get_attribute_from_block( 'style', $styled_block ); $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_classes = 'my-block foo-bar-class'; + $expected_classes = 'wp-block-example foo-bar-class'; $expected_styles = 'test:style;'; $this->assertEquals( $expected_styles, $style_list ); From a9b1e3eb02da31f75a88eb366a6e459f26ed867b Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 17:07:54 -0400 Subject: [PATCH 23/47] added gradient tests --- phpunit/class-supported-block-styles-test.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 6b2f7311c521b0..18d34ddcb58cca 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -90,10 +90,13 @@ function test_named_color_support() { $styled_block = apply_filters( 'render_block', $this->block_content, $block ); $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); $expected_classes = 'wp-block-example foo-bar-class has-text-color has-red-color has-background has-black-background-color'; + $expected_styles = 'test:style; '; $this->assertEquals( $expected_classes, $class_list ); + $this->assertEquals( $expected_styles, $style_list ); } function test_custom_color_support() { @@ -195,6 +198,70 @@ function test_custom_link_color_support() { $this->assertEquals( $expected_styles, $style_list ); } + function test_named_gradient_support() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalColor' => array( + 'gradients' => true, + ), + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'gradient' => 'red', + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + + $expected_classes = 'wp-block-example foo-bar-class has-background has-red-gradient-background'; + $expected_styles = 'test:style; '; + + $this->assertEquals( $expected_classes, $class_list ); + $this->assertEquals( $expected_styles, $style_list ); + } + + function test_custom_gradient_support() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalColor' => array( + 'gradients' => true, + ), + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( 'color' => array( 'gradient' => 'some-gradient-style' ) ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + + $expected_classes = 'wp-block-example foo-bar-class has-background'; + $expected_styles = 'test:style; background: some-gradient-style;'; + + $this->assertEquals( $expected_classes, $class_list ); + $this->assertEquals( $expected_styles, $style_list ); + } + function test_color_unsupported() { $block_type_settings = array( 'attributes' => array( From 732e2550b89421c9c578466dbdb982268c95e3ea Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 17:22:53 -0400 Subject: [PATCH 24/47] refactor an assertion function --- phpunit/class-supported-block-styles-test.php | 80 ++++++++----------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 18d34ddcb58cca..6bceec3295ae88 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -65,8 +65,23 @@ private function get_attribute_from_block( $attribute, $block ) { return substr( $split_arr, 0, $end_index ); } - private $block_content = '
So say we all.
'; + /** + * Runs assertions that the rendered output has expected class/style attrs. + * + * @param array $block Block to render. + * @param string $expected_classes Expected output class attr string. + * @param string $expected_styles Expected output styles attr string. + */ + private function assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ) { + $styled_block = apply_filters( 'render_block', $this->block_content, $block ); + $class_list = $this->get_attribute_from_block( 'class', $styled_block ); + $style_list = $this->get_attribute_from_block( 'style', $styled_block ); + $this->assertEquals( $expected_classes, $class_list ); + $this->assertEquals( $expected_styles, $style_list ); + } + + private $block_content = '
So say we all.
'; function test_named_color_support() { $block_type_settings = array( @@ -82,21 +97,18 @@ function test_named_color_support() { 'attrs' => array( 'textColor' => 'red', 'backgroundColor' => 'black', + // The following should not be applied (subcatagories of color support). + 'gradient' => 'some-gradient', ), 'innerBlock' => array(), 'innerContent' => array(), 'innerHTML' => array(), ); - $styled_block = apply_filters( 'render_block', $this->block_content, $block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $expected_classes = 'wp-block-example foo-bar-class has-text-color has-red-color has-background has-black-background-color'; $expected_styles = 'test:style; '; - $this->assertEquals( $expected_classes, $class_list ); - $this->assertEquals( $expected_styles, $style_list ); + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } function test_custom_color_support() { @@ -115,6 +127,9 @@ function test_custom_color_support() { 'color' => array( 'text' => '#000', 'background' => '#fff', + // The following should not be applied (subcatagories of color support). + 'gradient' => 'some-gradient', + 'style' => array( 'color' => array( 'link' => '#fff' ) ), ), ), ), @@ -123,15 +138,10 @@ function test_custom_color_support() { 'innerHTML' => array(), ); - $styled_block = apply_filters( 'render_block', $this->block_content, $block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_styles = 'test:style; color: #000;background-color: #fff;'; $expected_classes = 'wp-block-example foo-bar-class has-text-color has-background'; - $this->assertEquals( $expected_styles, $style_list ); - $this->assertEquals( $expected_classes, $class_list ); + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } function test_named_link_color_support() { @@ -155,15 +165,10 @@ function test_named_link_color_support() { 'innerHTML' => array(), ); - $styled_block = apply_filters( 'render_block', $this->block_content, $block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $expected_classes = 'wp-block-example foo-bar-class has-link-color'; $expected_styles = 'test:style; --wp--style--color--link:var(--wp--preset--color--red);'; - $this->assertEquals( $expected_classes, $class_list ); - $this->assertEquals( $expected_styles, $style_list ); + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } function test_custom_link_color_support() { @@ -187,15 +192,10 @@ function test_custom_link_color_support() { 'innerHTML' => array(), ); - $styled_block = apply_filters( 'render_block', $this->block_content, $block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $expected_classes = 'wp-block-example foo-bar-class has-link-color'; $expected_styles = 'test:style; --wp--style--color--link: #fff;'; - $this->assertEquals( $expected_classes, $class_list ); - $this->assertEquals( $expected_styles, $style_list ); + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } function test_named_gradient_support() { @@ -219,15 +219,10 @@ function test_named_gradient_support() { 'innerHTML' => array(), ); - $styled_block = apply_filters( 'render_block', $this->block_content, $block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $expected_classes = 'wp-block-example foo-bar-class has-background has-red-gradient-background'; $expected_styles = 'test:style; '; - $this->assertEquals( $expected_classes, $class_list ); - $this->assertEquals( $expected_styles, $style_list ); + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } function test_custom_gradient_support() { @@ -251,24 +246,16 @@ function test_custom_gradient_support() { 'innerHTML' => array(), ); - $styled_block = apply_filters( 'render_block', $this->block_content, $block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $expected_classes = 'wp-block-example foo-bar-class has-background'; $expected_styles = 'test:style; background: some-gradient-style;'; - $this->assertEquals( $expected_classes, $class_list ); - $this->assertEquals( $expected_styles, $style_list ); + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } function test_color_unsupported() { $block_type_settings = array( - 'attributes' => array( - 'textColor' => array( - 'type' => 'string', - ), - ), + 'attributes' => array(), + 'supports' => array(), ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -281,6 +268,8 @@ function test_color_unsupported() { 'color' => array( 'text' => '#000', 'background' => '#fff', + 'link' => '#ggg', + 'gradient' => 'some-gradient', ), ), ), @@ -289,14 +278,9 @@ function test_color_unsupported() { 'innerHTML' => array(), ); - $styled_block = apply_filters( 'render_block', $this->block_content, $block ); - $style_list = $this->get_attribute_from_block( 'style', $styled_block ); - $class_list = $this->get_attribute_from_block( 'class', $styled_block ); - $expected_classes = 'wp-block-example foo-bar-class'; $expected_styles = 'test:style;'; - $this->assertEquals( $expected_styles, $style_list ); - $this->assertEquals( $expected_classes, $class_list ); + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } } From 160d86fc1f745807201f6a62cef20ff11fa10285 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 17:32:08 -0400 Subject: [PATCH 25/47] added fontSize tests --- phpunit/class-supported-block-styles-test.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 6bceec3295ae88..9477b95ff63be7 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -283,4 +283,78 @@ function test_color_unsupported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + + function test_named_font_size() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalFontSize' => true, + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'fontSize' => 'large', + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class has-large-font-size'; + $expected_styles = 'test:style; '; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + + function test_custom_font_size() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalFontSize' => true, + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( 'typography' => array( 'fontSize' => '10' ) ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class '; + $expected_styles = 'test:style; font-size: 10px;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + + function test_font_size_unsupported() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array(), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'fontSize' => 'large', + 'style' => array( 'typography' => array( 'fontSize' => '10' ) ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class'; + $expected_styles = 'test:style;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } } From c95854965de30b61d0e39bf6fbdfbd4040fdeb87 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 17:36:25 -0400 Subject: [PATCH 26/47] lineHeight tests --- phpunit/class-supported-block-styles-test.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 9477b95ff63be7..6dd5f79ecc04f5 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -357,4 +357,52 @@ function test_font_size_unsupported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + + function test_line_height() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalLineHeight' => true + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( 'typography' => array( 'lineHeight' => '10' ) ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class '; + $expected_styles = 'test:style; line-height: 10;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + + function test_line_height_unsupported() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array(), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( 'typography' => array( 'lineHeight' => '10' ) ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class'; + $expected_styles = 'test:style;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } } From e089533b5341ad55a7652b12ae2a3333b89e5cf8 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 17:45:54 -0400 Subject: [PATCH 27/47] test all supports together --- phpunit/class-supported-block-styles-test.php | 81 ++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index 6dd5f79ecc04f5..c80c16ed2d6a02 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -345,7 +345,7 @@ function test_font_size_unsupported() { 'blockName' => 'core/example', 'attrs' => array( 'fontSize' => 'large', - 'style' => array( 'typography' => array( 'fontSize' => '10' ) ), + 'style' => array( 'typography' => array( 'fontSize' => '10' ) ), ), 'innerBlock' => array(), 'innerContent' => array(), @@ -362,7 +362,7 @@ function test_line_height() { $block_type_settings = array( 'attributes' => array(), 'supports' => array( - '__experimentalLineHeight' => true + '__experimentalLineHeight' => true, ), ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -405,4 +405,81 @@ function test_line_height_unsupported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + + function test_all_supported() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalColor' => array( + 'gradients' => true, + 'linkColor' => true, + ), + '__experimentalFontSize' => true, + '__experimentalLineHeight' => true, + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( + 'color' => array( + 'text' => '#000', + 'background' => '#fff', + 'gradient' => 'some-gradient', + 'style' => array( 'color' => array( 'link' => '#fff' ) ), + ), + 'typography' => array( + 'lineHeight' => '20', + 'fontSize' => '10', + ), + ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class has-text-color has-background'; + $expected_styles = 'test:style; color: #000;background-color: #fff;background: some-gradient;font-size: 10px;line-height: 20;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + + function test_one_supported() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalFontSize' => true, + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( + 'color' => array( + 'text' => '#000', + 'background' => '#fff', + 'gradient' => 'some-gradient', + 'style' => array( 'color' => array( 'link' => '#fff' ) ), + ), + 'typography' => array( + 'lineHeight' => '20', + 'fontSize' => '10', + ), + ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class '; + $expected_styles = 'test:style; font-size: 10px;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } } From d4931420b54fc773973feb7742b1d9b066b361f5 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 17:58:29 -0400 Subject: [PATCH 28/47] update comments --- phpunit/class-supported-block-styles-test.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-supported-block-styles-test.php index c80c16ed2d6a02..b4997de8abdb26 100644 --- a/phpunit/class-supported-block-styles-test.php +++ b/phpunit/class-supported-block-styles-test.php @@ -81,8 +81,14 @@ private function assert_styles_and_classes_match( $block, $expected_classes, $ex $this->assertEquals( $expected_styles, $style_list ); } + /** + * Example block content to test with. + */ private $block_content = '
So say we all.
'; + /** + * Tests color support for named color support for named colors. + */ function test_named_color_support() { $block_type_settings = array( 'attributes' => array(), @@ -111,6 +117,9 @@ function test_named_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests color support for custom colors. + */ function test_custom_color_support() { $block_type_settings = array( 'attributes' => array(), @@ -144,6 +153,9 @@ function test_custom_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests link color support for named colors. + */ function test_named_link_color_support() { $block_type_settings = array( 'attributes' => array(), @@ -171,6 +183,9 @@ function test_named_link_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests link color support for custom colors. + */ function test_custom_link_color_support() { $block_type_settings = array( 'attributes' => array(), @@ -198,6 +213,9 @@ function test_custom_link_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests gradient color support for named gradients. + */ function test_named_gradient_support() { $block_type_settings = array( 'attributes' => array(), @@ -225,6 +243,9 @@ function test_named_gradient_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests gradient color support for custom gradients. + */ function test_custom_gradient_support() { $block_type_settings = array( 'attributes' => array(), @@ -252,6 +273,9 @@ function test_custom_gradient_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests that style attributes for colors are not applied without the support flag. + */ function test_color_unsupported() { $block_type_settings = array( 'attributes' => array(), @@ -284,6 +308,9 @@ function test_color_unsupported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests support for named font sizes. + */ function test_named_font_size() { $block_type_settings = array( 'attributes' => array(), @@ -309,6 +336,9 @@ function test_named_font_size() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests support for custom font sizes. + */ function test_custom_font_size() { $block_type_settings = array( 'attributes' => array(), @@ -334,6 +364,9 @@ function test_custom_font_size() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests that font size attributes are not applied without support flag. + */ function test_font_size_unsupported() { $block_type_settings = array( 'attributes' => array(), @@ -358,6 +391,9 @@ function test_font_size_unsupported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests line height support. + */ function test_line_height() { $block_type_settings = array( 'attributes' => array(), @@ -383,6 +419,9 @@ function test_line_height() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests line height not applied without support flag. + */ function test_line_height_unsupported() { $block_type_settings = array( 'attributes' => array(), @@ -406,6 +445,9 @@ function test_line_height_unsupported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests all support flags together to ensure they work together as expected. + */ function test_all_supported() { $block_type_settings = array( 'attributes' => array(), @@ -447,6 +489,10 @@ function test_all_supported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests that only styles for the supported flag are added. + * Verify one support enabled does not imply multiple supports enabled. + */ function test_one_supported() { $block_type_settings = array( 'attributes' => array(), From 2395a3edb5935b75809178994454bd4710f85d2f Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 18:03:40 -0400 Subject: [PATCH 29/47] file rename --- ...lock-styles-test.php => class-block-supported-styles-test.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename phpunit/{class-supported-block-styles-test.php => class-block-supported-styles-test.php} (100%) diff --git a/phpunit/class-supported-block-styles-test.php b/phpunit/class-block-supported-styles-test.php similarity index 100% rename from phpunit/class-supported-block-styles-test.php rename to phpunit/class-block-supported-styles-test.php From fec76282f72d709a51bfe2e06f4f6ff581308ec4 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 18:16:05 -0400 Subject: [PATCH 30/47] fix linter errors --- lib/blocks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 1308ef7d7c1981..aee56bbf8b11b7 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -349,11 +349,11 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl return $block_content; } - // Merge and dedupe new and existing classes and styles + // Merge and dedupe new and existing classes and styles. $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); $new_styles = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'style' ) . ' ' ) . $extra_styles ) ) ); - // Apply new styles and classes + // Apply new styles and classes. if ( ! empty( $new_classes ) ) { $block_root->setAttribute( 'class', $new_classes ); } @@ -478,7 +478,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { 'inline_styles' => '', ); - // Font Size + // Font Size. if ( in_array( 'font-size', $supports ) ) { $has_named_font_size = array_key_exists( 'fontSize', $attributes ); $has_custom_font_size = array_key_exists( 'style', $attributes ) From d62406ee35efb580518d4bcf6d1c0206ae654d9d Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 19:46:52 -0400 Subject: [PATCH 31/47] more lint error/warning goo --- lib/blocks.php | 14 +++++++------- phpunit/class-block-supported-styles-test.php | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index aee56bbf8b11b7..b033ff49305851 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -307,7 +307,7 @@ function gutenberg_enqueue_editor_block_styles_assets() { function gutenberg_experimental_apply_classnames_and_styles( $block_content, $block ) { // Don't filter template part blocks since we filter the blocks in each template // part individually. - if ( 'core/template-part' == $block['blockName'] ) { + if ( 'core/template-part' === $block['blockName'] ) { return $block_content; } @@ -385,7 +385,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Text Colors. // Check support for text colors. - if ( in_array( 'color', $supports ) ) { + if ( in_array( 'color', $supports, true ) ) { $has_named_text_color = array_key_exists( 'textColor', $attributes ); $has_custom_text_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) @@ -404,7 +404,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { } // Link Colors. - if ( in_array( 'link-color', $supports ) ) { + if ( in_array( 'link-color', $supports, true ) ) { $has_link_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) && array_key_exists( 'link', $attributes['style']['color'] ); @@ -424,7 +424,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { } // Background Colors. - if ( in_array( 'background-color', $supports ) ) { + if ( in_array( 'background-color', $supports, true ) ) { $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); $has_custom_background_color = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) @@ -443,7 +443,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { } // Gradients. - if ( in_array( 'background', $supports ) ) { + if ( in_array( 'background', $supports, true ) ) { $has_named_gradient = array_key_exists( 'gradient', $attributes ); $has_custom_gradient = array_key_exists( 'style', $attributes ) && array_key_exists( 'color', $attributes['style'] ) @@ -479,7 +479,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { ); // Font Size. - if ( in_array( 'font-size', $supports ) ) { + if ( in_array( 'font-size', $supports, true ) ) { $has_named_font_size = array_key_exists( 'fontSize', $attributes ); $has_custom_font_size = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ) @@ -494,7 +494,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { } // Line Height. - if ( in_array( 'line-height', $supports ) ) { + if ( in_array( 'line-height', $supports, true ) ) { $has_line_height = array_key_exists( 'style', $attributes ) && array_key_exists( 'typography', $attributes['style'] ) && array_key_exists( 'lineHeight', $attributes['style']['typography'] ); diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index b4997de8abdb26..fdff7f381a3dc3 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -83,6 +83,8 @@ private function assert_styles_and_classes_match( $block, $expected_classes, $ex /** * Example block content to test with. + * + * @var string */ private $block_content = '
So say we all.
'; From 32bd8bb707e09ae1262eabb869eb270855834f22 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 15 Jun 2020 20:38:24 -0400 Subject: [PATCH 32/47] lint ignore our secret sauce --- lib/blocks.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/blocks.php b/lib/blocks.php index b033ff49305851..f502c93927cfb0 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -341,7 +341,9 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl : ''; $dom = new DOMDocument( '1.0', 'utf-8' ); + // @codingStandardsIgnoreStart @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); + // @codingStandardsIgnoreEnd $xpath = new DOMXPath( $dom ); $block_root = $xpath->query( '/*' )[0]; From f909ab2dec5befa123ddd4c932e732b03a11a35e Mon Sep 17 00:00:00 2001 From: apeatling Date: Tue, 16 Jun 2020 09:45:42 -0700 Subject: [PATCH 33/47] Remove error suppression and return block content on invalid DOM loadHTML. --- lib/blocks.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index f502c93927cfb0..c8c91d4fec44d5 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -341,9 +341,11 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl : ''; $dom = new DOMDocument( '1.0', 'utf-8' ); - // @codingStandardsIgnoreStart - @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ); - // @codingStandardsIgnoreEnd + + if ( ! $dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { + return $block_content; + } + $xpath = new DOMXPath( $dom ); $block_root = $xpath->query( '/*' )[0]; From 1bf2fc003bc940de6d75c9504d1d1460ab607ca7 Mon Sep 17 00:00:00 2001 From: apeatling Date: Tue, 16 Jun 2020 10:45:09 -0700 Subject: [PATCH 34/47] Update documentation to add site-title block. --- .../developers/themes/theme-json.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/designers-developers/developers/themes/theme-json.md b/docs/designers-developers/developers/themes/theme-json.md index 8a523a264fafa6..3db274fbe70e0c 100644 --- a/docs/designers-developers/developers/themes/theme-json.md +++ b/docs/designers-developers/developers/themes/theme-json.md @@ -167,6 +167,7 @@ Each block will declare which style properties it exposes. This has been coined | Group | Yes | Yes | Yes | Yes | | Columns | Yes | Yes | Yes | Yes | | Media & text | Yes | Yes | Yes | Yes | +| Site Title | Yes | Yes | - | Yes | [1] The heading block represents 6 distinct HTML elements: H1-H6. It comes with selectors to target each individual element (ex: core/heading/h1 for H1, etc). @@ -180,6 +181,7 @@ Each block will declare which style properties it exposes. This has been coined | Group | - | - | | Columns | - | - | | Media & text | - | - | +| Site Title | Yes | Yes | [1] The heading block represents 6 distinct HTML elements: H1-H6. It comes with selectors to target each individual element (ex: core/heading/h1 for H1, etc). @@ -379,6 +381,19 @@ The list of features that are currently supported are: "text": } } + }, + "core/site-title": { + "styles": { + "color": { + "background": , + "gradient": , + "text": + }, + "typography": { + "fontSize": , + "lineHeight": + } + } } } ``` From bf1e768af9ebdb3fd06d1f90e072c45185d6f795 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Tue, 16 Jun 2020 20:15:40 -0400 Subject: [PATCH 35/47] helper fn and lint fix --- lib/blocks.php | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index c8c91d4fec44d5..8c76a722d64a6c 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -320,7 +320,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $supports = array(); } // Return early if nothing is supported. - if ( sizeof( $supports ) === 0 ) { + if ( count( $supports ) === 0 ) { return $block_content; } @@ -342,7 +342,10 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $dom = new DOMDocument( '1.0', 'utf-8' ); - if ( ! $dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { + // Suppress warnings from this method from polluting the font-end. + // @codingStandardsIgnoreStart + if ( ! @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { + // @codingStandardsIgnoreEnd return $block_content; } @@ -391,9 +394,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Check support for text colors. if ( in_array( 'color', $supports, true ) ) { $has_named_text_color = array_key_exists( 'textColor', $attributes ); - $has_custom_text_color = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'text', $attributes['style']['color'] ); + $has_custom_text_color = has_triple_nested_key( $attributes, 'style', 'color', 'text' ); // Apply required generic class. if ( $has_custom_text_color || $has_named_text_color ) { @@ -409,9 +410,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Link Colors. if ( in_array( 'link-color', $supports, true ) ) { - $has_link_color = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'link', $attributes['style']['color'] ); + $has_link_color = has_triple_nested_key( $attributes, 'style', 'color', 'link' ); // Apply required class and style. if ( $has_link_color ) { $color_settings['css_classes'][] = 'has-link-color'; @@ -430,9 +429,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Background Colors. if ( in_array( 'background-color', $supports, true ) ) { $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); - $has_custom_background_color = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'background', $attributes['style']['color'] ); + $has_custom_background_color = has_triple_nested_key( $attributes, 'style', 'color', 'background' ); // Apply required background class. if ( $has_custom_background_color || $has_named_background_color ) { @@ -449,9 +446,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Gradients. if ( in_array( 'background', $supports, true ) ) { $has_named_gradient = array_key_exists( 'gradient', $attributes ); - $has_custom_gradient = array_key_exists( 'style', $attributes ) - && array_key_exists( 'color', $attributes['style'] ) - && array_key_exists( 'gradient', $attributes['style']['color'] ); + $has_custom_gradient = has_triple_nested_key( $attributes, 'style', 'color', 'gradient' ); if ( $has_named_gradient || $has_custom_gradient ) { $color_settings['css_classes'][] = 'has-background'; @@ -485,9 +480,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { // Font Size. if ( in_array( 'font-size', $supports, true ) ) { $has_named_font_size = array_key_exists( 'fontSize', $attributes ); - $has_custom_font_size = array_key_exists( 'style', $attributes ) - && array_key_exists( 'typography', $attributes['style'] ) - && array_key_exists( 'fontSize', $attributes['style']['typography'] ); + $has_custom_font_size = has_triple_nested_key( $attributes, 'style', 'typography', 'fontSize' ); // Apply required class or style. if ( $has_named_font_size ) { @@ -499,10 +492,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { // Line Height. if ( in_array( 'line-height', $supports, true ) ) { - $has_line_height = array_key_exists( 'style', $attributes ) - && array_key_exists( 'typography', $attributes['style'] ) - && array_key_exists( 'lineHeight', $attributes['style']['typography'] ); - + $has_line_height = has_triple_nested_key( $attributes, 'style', 'typography', 'lineHeight' ); // Add the style (no classes for line-height). if ( $has_line_height ) { $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); @@ -512,3 +502,17 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { return $typography; } +/** + * Checks whether or not the given array has the given chain of nested keys. + * ex) $attributes['style']['color']['font'] + * + * @param array $array array to check for keys. + * @param string $key1 1st level key to check. + * @param string $key2 2nd level key to check. + * @param string $key3 3rd level key to check. + */ +function has_triple_nested_key( $array, $key1, $key2, $key3 ) { + return array_key_exists( $key1, $array ) + && array_key_exists( $key2, $array[ $key1 ] ) + && array_key_exists( $key3, $array[ $key1 ][ $key2 ] ); +} From e81946ef56df79a28aaf291fba7dec50a2f069e8 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 17 Jun 2020 00:11:01 -0400 Subject: [PATCH 36/47] use isset for checking the nested style objects --- lib/blocks.php | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 8c76a722d64a6c..531bf182970145 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -394,7 +394,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Check support for text colors. if ( in_array( 'color', $supports, true ) ) { $has_named_text_color = array_key_exists( 'textColor', $attributes ); - $has_custom_text_color = has_triple_nested_key( $attributes, 'style', 'color', 'text' ); + $has_custom_text_color = isset( $attributes['style']['color']['text'] ); // Apply required generic class. if ( $has_custom_text_color || $has_named_text_color ) { @@ -410,7 +410,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Link Colors. if ( in_array( 'link-color', $supports, true ) ) { - $has_link_color = has_triple_nested_key( $attributes, 'style', 'color', 'link' ); + $has_link_color = isset( $attributes['style']['color']['link'] ); // Apply required class and style. if ( $has_link_color ) { $color_settings['css_classes'][] = 'has-link-color'; @@ -429,7 +429,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Background Colors. if ( in_array( 'background-color', $supports, true ) ) { $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); - $has_custom_background_color = has_triple_nested_key( $attributes, 'style', 'color', 'background' ); + $has_custom_background_color = isset( $attributes['style']['color']['background'] ); // Apply required background class. if ( $has_custom_background_color || $has_named_background_color ) { @@ -446,7 +446,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $supports ) { // Gradients. if ( in_array( 'background', $supports, true ) ) { $has_named_gradient = array_key_exists( 'gradient', $attributes ); - $has_custom_gradient = has_triple_nested_key( $attributes, 'style', 'color', 'gradient' ); + $has_custom_gradient = isset( $attributes['style']['color']['gradient'] ); if ( $has_named_gradient || $has_custom_gradient ) { $color_settings['css_classes'][] = 'has-background'; @@ -480,7 +480,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { // Font Size. if ( in_array( 'font-size', $supports, true ) ) { $has_named_font_size = array_key_exists( 'fontSize', $attributes ); - $has_custom_font_size = has_triple_nested_key( $attributes, 'style', 'typography', 'fontSize' ); + $has_custom_font_size = isset( $attributes['style']['typography']['fontSize'] ); // Apply required class or style. if ( $has_named_font_size ) { @@ -492,7 +492,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { // Line Height. if ( in_array( 'line-height', $supports, true ) ) { - $has_line_height = has_triple_nested_key( $attributes, 'style', 'typography', 'lineHeight' ); + $has_line_height = isset( $attributes['style']['typography']['lineHeight'] ); // Add the style (no classes for line-height). if ( $has_line_height ) { $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); @@ -501,18 +501,3 @@ function gutenberg_experimental_build_css_typography( $attributes, $supports ) { return $typography; } - -/** - * Checks whether or not the given array has the given chain of nested keys. - * ex) $attributes['style']['color']['font'] - * - * @param array $array array to check for keys. - * @param string $key1 1st level key to check. - * @param string $key2 2nd level key to check. - * @param string $key3 3rd level key to check. - */ -function has_triple_nested_key( $array, $key1, $key2, $key3 ) { - return array_key_exists( $key1, $array ) - && array_key_exists( $key2, $array[ $key1 ] ) - && array_key_exists( $key3, $array[ $key1 ][ $key2 ] ); -} From 8923bd8e2d7c9d17baa2e8afaebe677e186d7db6 Mon Sep 17 00:00:00 2001 From: Addison Stavlo Date: Wed, 17 Jun 2020 13:29:28 -0400 Subject: [PATCH 37/47] Update lib/blocks.php Co-authored-by: Bernie Reiter --- lib/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index 531bf182970145..fced6121ec1760 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -342,7 +342,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $dom = new DOMDocument( '1.0', 'utf-8' ); - // Suppress warnings from this method from polluting the font-end. + // Suppress warnings from this method from polluting the front-end. // @codingStandardsIgnoreStart if ( ! @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { // @codingStandardsIgnoreEnd From a581770e679e51857566b28b5da65b4e58c68e2d Mon Sep 17 00:00:00 2001 From: Addison Stavlo Date: Wed, 17 Jun 2020 13:30:28 -0400 Subject: [PATCH 38/47] Update lib/blocks.php Co-authored-by: Bernie Reiter --- lib/blocks.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index fced6121ec1760..59b6ebe3c0f3fe 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -314,11 +314,12 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl if ( isset( $block['attrs'] ) ) { // Check what style features the block supports. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - if ( $block_type ) { - $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); - } else { - $supports = array(); + if ( ! $block_type ) { + return $block_content; } + + $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); + // Return early if nothing is supported. if ( count( $supports ) === 0 ) { return $block_content; From 7cb269399eb17743c4a754c5cf4ffe80cce088f0 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Fri, 19 Jun 2020 14:34:56 -0400 Subject: [PATCH 39/47] check for ending semi-colon on existing styles --- lib/blocks.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 59b6ebe3c0f3fe..1c661337c3a4b2 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -317,7 +317,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl if ( ! $block_type ) { return $block_content; } - + $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); // Return early if nothing is supported. @@ -357,9 +357,15 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl return $block_content; } + // Some inline styles may be added without ending ';', add this is necessary. + $current_styles = trim( $block_root->getAttribute( 'style' ), ' ' ); + if ( strlen( $current_styles ) > 0 && substr( $current_styles, -1 ) !== ';' ) { + $current_styles = $current_styles . ';'; + }; + // Merge and dedupe new and existing classes and styles. $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); - $new_styles = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'style' ) . ' ' ) . $extra_styles ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . $extra_styles ) ) ); // Apply new styles and classes. if ( ! empty( $new_classes ) ) { From 543fb7e2f213a20cffe94e467abbad1b2ccf0e24 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Mon, 22 Jun 2020 12:26:58 -0400 Subject: [PATCH 40/47] ensure space for string concat and unique-ify-ing --- lib/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 1c661337c3a4b2..66ed3eb003fb4e 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -357,7 +357,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl return $block_content; } - // Some inline styles may be added without ending ';', add this is necessary. + // Some inline styles may be added without ending ';', add this if necessary. $current_styles = trim( $block_root->getAttribute( 'style' ), ' ' ); if ( strlen( $current_styles ) > 0 && substr( $current_styles, -1 ) !== ';' ) { $current_styles = $current_styles . ';'; @@ -365,7 +365,7 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl // Merge and dedupe new and existing classes and styles. $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); - $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . $extra_styles ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $extra_styles ) ) ); // Apply new styles and classes. if ( ! empty( $new_classes ) ) { From 806a23ff2a20db5add84f4e69b8b099ec4538183 Mon Sep 17 00:00:00 2001 From: apeatling Date: Mon, 22 Jun 2020 09:52:06 -0700 Subject: [PATCH 41/47] Remove template part block check, this is no longer needed because we are testing for experimental feature support on blocks. --- lib/blocks.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 66ed3eb003fb4e..880acdc26b1e9b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -305,12 +305,6 @@ function gutenberg_enqueue_editor_block_styles_assets() { * @return string New block output. */ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $block ) { - // Don't filter template part blocks since we filter the blocks in each template - // part individually. - if ( 'core/template-part' === $block['blockName'] ) { - return $block_content; - } - if ( isset( $block['attrs'] ) ) { // Check what style features the block supports. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); From 998f7f6966f6a05bd7fea7c57750ff2a0ead76a7 Mon Sep 17 00:00:00 2001 From: apeatling Date: Mon, 22 Jun 2020 09:52:39 -0700 Subject: [PATCH 42/47] WordPress coding standards tweaks --- lib/blocks.php | 112 ++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 880acdc26b1e9b..51c114a69e5f47 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -305,75 +305,75 @@ function gutenberg_enqueue_editor_block_styles_assets() { * @return string New block output. */ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $block ) { - if ( isset( $block['attrs'] ) ) { - // Check what style features the block supports. - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - if ( ! $block_type ) { - return $block_content; - } + if ( ! isset( $block['attrs'] ) ) { + return $block_content; + } - $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); + // Check what style features the block supports. + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + if ( ! $block_type ) { + return $block_content; + } - // Return early if nothing is supported. - if ( count( $supports ) === 0 ) { - return $block_content; - } + $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); - $colors = gutenberg_experimental_build_css_colors( $block['attrs'], $supports ); - $typography = gutenberg_experimental_build_css_typography( $block['attrs'], $supports ); + // Return early if nothing is supported. + if ( 0 === count( $supports ) ) { + return $block_content; + } - $extra_classes = array_merge( - $colors['css_classes'], - $typography['css_classes'], - isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), - isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() - ); - $extra_styles = ( - $colors['inline_styles'] || - $typography['inline_styles'] - ) ? esc_attr( $colors['inline_styles'] ) . - esc_attr( $typography['inline_styles'] ) - : ''; - - $dom = new DOMDocument( '1.0', 'utf-8' ); - - // Suppress warnings from this method from polluting the front-end. - // @codingStandardsIgnoreStart - if ( ! @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { - // @codingStandardsIgnoreEnd - return $block_content; - } + $colors = gutenberg_experimental_build_css_colors( $block['attrs'], $supports ); + $typography = gutenberg_experimental_build_css_typography( $block['attrs'], $supports ); - $xpath = new DOMXPath( $dom ); - $block_root = $xpath->query( '/*' )[0]; + $extra_classes = array_merge( + $colors['css_classes'], + $typography['css_classes'], + isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), + isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() + ); + $extra_styles = ( + $colors['inline_styles'] || + $typography['inline_styles'] + ) ? esc_attr( $colors['inline_styles'] ) . + esc_attr( $typography['inline_styles'] ) + : ''; + + $dom = new DOMDocument( '1.0', 'utf-8' ); + + // Suppress warnings from this method from polluting the front-end. + // @codingStandardsIgnoreStart + if ( ! @$dom->loadHTML( $block_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_COMPACT ) ) { + // @codingStandardsIgnoreEnd + return $block_content; + } - if ( empty( $block_root ) ) { - return $block_content; - } + $xpath = new DOMXPath( $dom ); + $block_root = $xpath->query( '/*' )[0]; - // Some inline styles may be added without ending ';', add this if necessary. - $current_styles = trim( $block_root->getAttribute( 'style' ), ' ' ); - if ( strlen( $current_styles ) > 0 && substr( $current_styles, -1 ) !== ';' ) { - $current_styles = $current_styles . ';'; - }; + if ( empty( $block_root ) ) { + return $block_content; + } - // Merge and dedupe new and existing classes and styles. - $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); - $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $extra_styles ) ) ); + // Some inline styles may be added without ending ';', add this if necessary. + $current_styles = trim( $block_root->getAttribute( 'style' ), ' ' ); + if ( strlen( $current_styles ) > 0 && substr( $current_styles, -1 ) !== ';' ) { + $current_styles = $current_styles . ';'; + }; - // Apply new styles and classes. - if ( ! empty( $new_classes ) ) { - $block_root->setAttribute( 'class', $new_classes ); - } + // Merge and dedupe new and existing classes and styles. + $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $extra_styles ) ) ); - if ( ! empty( $new_styles ) ) { - $block_root->setAttribute( 'style', $new_styles ); - } + // Apply new styles and classes. + if ( ! empty( $new_classes ) ) { + $block_root->setAttribute( 'class', $new_classes ); + } - return $dom->saveHtml(); + if ( ! empty( $new_styles ) ) { + $block_root->setAttribute( 'style', $new_styles ); } - return $block_content; + return $dom->saveHtml(); } add_filter( 'render_block', 'gutenberg_experimental_apply_classnames_and_styles', 10, 2 ); From 0559112f7b06e2cc6438a51066697c6bda971978 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 24 Jun 2020 19:29:32 -0400 Subject: [PATCH 43/47] refactor per Riad's interface suggestion --- lib/blocks.php | 108 ++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 65 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 51c114a69e5f47..b3b331077cc7c2 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -317,27 +317,14 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); - // Return early if nothing is supported. - if ( 0 === count( $supports ) ) { + $attributes = array(); + $attributes = gutenberg_experimental_build_css_colors( $attributes, $block['attrs'], $supports ); + $attributes = gutenberg_experimental_build_css_typography( $attributes, $block['attrs'], $supports ); + + if ( ! count( $attributes ) ) { return $block_content; } - $colors = gutenberg_experimental_build_css_colors( $block['attrs'], $supports ); - $typography = gutenberg_experimental_build_css_typography( $block['attrs'], $supports ); - - $extra_classes = array_merge( - $colors['css_classes'], - $typography['css_classes'], - isset( $block['attrs']['className'] ) ? array( $block['attrs']['className'] ) : array(), - isset( $block['attrs']['align'] ) ? array( 'has-text-align-' . $block['attrs']['align'] ) : array() - ); - $extra_styles = ( - $colors['inline_styles'] || - $typography['inline_styles'] - ) ? esc_attr( $colors['inline_styles'] ) . - esc_attr( $typography['inline_styles'] ) - : ''; - $dom = new DOMDocument( '1.0', 'utf-8' ); // Suppress warnings from this method from polluting the front-end. @@ -361,8 +348,8 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl }; // Merge and dedupe new and existing classes and styles. - $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', $extra_classes ) ) ) ); - $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $extra_styles ) ) ); + $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', array_key_exists( 'css_classes', $attributes ) ? $attributes['css_classes'] : array() ) ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . esc_attr( implode( ' ', array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array() ) ) ) ) ); // Apply new styles and classes. if ( ! empty( $new_classes ) ) { @@ -381,124 +368,115 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl * Build an array with CSS classes and inline styles defining the colors * which will be applied to the block markup in the front-end. * - * @param array $attributes block attributes. + * @param array $attributes comprehensive list of attributes to be applied. + * @param array $block_attributes block attributes. * @param array $supports style features the block attributes. * @return array Colors CSS classes and inline styles. */ -function gutenberg_experimental_build_css_colors( $attributes, $supports ) { - $color_settings = array( - 'css_classes' => array(), - 'inline_styles' => '', - ); - +function gutenberg_experimental_build_css_colors( $attributes, $block_attributes, $supports ) { // Text Colors. // Check support for text colors. if ( in_array( 'color', $supports, true ) ) { - $has_named_text_color = array_key_exists( 'textColor', $attributes ); - $has_custom_text_color = isset( $attributes['style']['color']['text'] ); + $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); + $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); // Apply required generic class. if ( $has_custom_text_color || $has_named_text_color ) { - $color_settings['css_classes'][] = 'has-text-color'; + $attributes['css_classes'][] = 'has-text-color'; } // Apply color class or inline style. if ( $has_named_text_color ) { - $color_settings['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); + $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_attributes['textColor'] ); } elseif ( $has_custom_text_color ) { - $color_settings['inline_styles'] .= sprintf( 'color: %s;', $attributes['style']['color']['text'] ); + $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); } } // Link Colors. if ( in_array( 'link-color', $supports, true ) ) { - $has_link_color = isset( $attributes['style']['color']['link'] ); + $has_link_color = isset( $block_attributes['style']['color']['link'] ); // Apply required class and style. if ( $has_link_color ) { - $color_settings['css_classes'][] = 'has-link-color'; + $attributes['css_classes'][] = 'has-link-color'; // If link is a named color. - if ( strpos( $attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { + if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. - $index_to_splice = strrpos( $attributes['style']['color']['link'], '|' ) + 1; - $link_color_name = substr( $attributes['style']['color']['link'], $index_to_splice ); - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + $index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1; + $link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice ); + $attributes['inline_styles'][] = sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); } else { - $color_settings['inline_styles'] .= sprintf( '--wp--style--color--link: %s;', $attributes['style']['color']['link'] ); + $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); } } } // Background Colors. if ( in_array( 'background-color', $supports, true ) ) { - $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); - $has_custom_background_color = isset( $attributes['style']['color']['background'] ); + $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); + $has_custom_background_color = isset( $block_attributes['style']['color']['background'] ); // Apply required background class. if ( $has_custom_background_color || $has_named_background_color ) { - $color_settings['css_classes'][] = 'has-background'; + $attributes['css_classes'][] = 'has-background'; } // Apply background color classes or styles. if ( $has_named_background_color ) { - $color_settings['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); + $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] ); } elseif ( $has_custom_background_color ) { - $color_settings['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['style']['color']['background'] ); + $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] ); } } // Gradients. if ( in_array( 'background', $supports, true ) ) { - $has_named_gradient = array_key_exists( 'gradient', $attributes ); - $has_custom_gradient = isset( $attributes['style']['color']['gradient'] ); + $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); + $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] ); if ( $has_named_gradient || $has_custom_gradient ) { - $color_settings['css_classes'][] = 'has-background'; + $attributes['css_classes'][] = 'has-background'; } // Apply required background class. if ( $has_named_gradient ) { - $color_settings['css_classes'][] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); + $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] ); } elseif ( $has_custom_gradient ) { - $color_settings['inline_styles'] .= sprintf( 'background: %s;', $attributes['style']['color']['gradient'] ); + $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] ); } } - return $color_settings; + return $attributes; } /** * Build an array with CSS classes and inline styles defining the font sizes * which will be applied to the block markup in the front-end. * - * @param array $attributes block attributes. + * @param array $attributes comprehensive list of attributes to be applied. + * @param array $block_attributes block attributes. * @param array $supports style features the block attributes. * @return array Font size CSS classes and inline styles. */ -function gutenberg_experimental_build_css_typography( $attributes, $supports ) { - // CSS classes. - $typography = array( - 'css_classes' => array(), - 'inline_styles' => '', - ); - +function gutenberg_experimental_build_css_typography( $attributes, $block_attributes, $supports ) { // Font Size. if ( in_array( 'font-size', $supports, true ) ) { - $has_named_font_size = array_key_exists( 'fontSize', $attributes ); - $has_custom_font_size = isset( $attributes['style']['typography']['fontSize'] ); + $has_named_font_size = array_key_exists( 'fontSize', $block_attributes ); + $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ); // Apply required class or style. if ( $has_named_font_size ) { - $typography['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); + $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] ); } elseif ( $has_custom_font_size ) { - $typography['inline_styles'] .= sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); + $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] ); } } // Line Height. if ( in_array( 'line-height', $supports, true ) ) { - $has_line_height = isset( $attributes['style']['typography']['lineHeight'] ); + $has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] ); // Add the style (no classes for line-height). if ( $has_line_height ) { - $typography['inline_styles'] .= sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); + $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); } } - return $typography; + return $attributes; } From aa4bbdcf66b057635bd2e71d7979cf687ebcbb83 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 24 Jun 2020 21:13:42 -0400 Subject: [PATCH 44/47] cleanup grossly long 1-liners --- lib/blocks.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index b3b331077cc7c2..37ff4d2e93315d 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -348,8 +348,10 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl }; // Merge and dedupe new and existing classes and styles. - $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . implode( ' ', array_key_exists( 'css_classes', $attributes ) ? $attributes['css_classes'] : array() ) ) ) ); - $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . esc_attr( implode( ' ', array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array() ) ) ) ) ); + $classes_to_add = esc_attr( implode( ' ', array_key_exists( 'css_classes', $attributes ) ? $attributes['css_classes'] : array() ) ); + $styles_to_add = esc_attr( implode( ' ', array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array() ) ); + $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . $classes_to_add ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $styles_to_add ) ) ); // Apply new styles and classes. if ( ! empty( $new_classes ) ) { From 1987286aa7967bda5b850a85d14a14a3dec1f9aa Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 24 Jun 2020 21:14:16 -0400 Subject: [PATCH 45/47] php format --- lib/blocks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 37ff4d2e93315d..648a021a1193a8 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -349,9 +349,9 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl // Merge and dedupe new and existing classes and styles. $classes_to_add = esc_attr( implode( ' ', array_key_exists( 'css_classes', $attributes ) ? $attributes['css_classes'] : array() ) ); - $styles_to_add = esc_attr( implode( ' ', array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array() ) ); - $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . $classes_to_add ) ) ); - $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $styles_to_add ) ) ); + $styles_to_add = esc_attr( implode( ' ', array_key_exists( 'inline_styles', $attributes ) ? $attributes['inline_styles'] : array() ) ); + $new_classes = implode( ' ', array_unique( explode( ' ', ltrim( $block_root->getAttribute( 'class' ) . ' ' ) . $classes_to_add ) ) ); + $new_styles = implode( ' ', array_unique( explode( ' ', $current_styles . ' ' . $styles_to_add ) ) ); // Apply new styles and classes. if ( ! empty( $new_classes ) ) { From ba6a395df2ed15804f3fd99452dd9a77d54b6eea Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Wed, 24 Jun 2020 21:24:26 -0400 Subject: [PATCH 46/47] update tests for better inline style spacing --- phpunit/class-block-supported-styles-test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index fdff7f381a3dc3..7f8d3dfd7c5a43 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -149,7 +149,7 @@ function test_custom_color_support() { 'innerHTML' => array(), ); - $expected_styles = 'test:style; color: #000;background-color: #fff;'; + $expected_styles = 'test:style; color: #000; background-color: #fff;'; $expected_classes = 'wp-block-example foo-bar-class has-text-color has-background'; $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); @@ -486,7 +486,7 @@ function test_all_supported() { ); $expected_classes = 'wp-block-example foo-bar-class has-text-color has-background'; - $expected_styles = 'test:style; color: #000;background-color: #fff;background: some-gradient;font-size: 10px;line-height: 20;'; + $expected_styles = 'test:style; color: #000; background-color: #fff; background: some-gradient; font-size: 10px; line-height: 20;'; $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } From 7c17c7621f8201a9c96f2ccb2c3e13e114715425 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 25 Jun 2020 17:31:40 -0400 Subject: [PATCH 47/47] require render_callback --- lib/blocks.php | 6 +- phpunit/class-block-supported-styles-test.php | 114 +++++++++++++----- 2 files changed, 89 insertions(+), 31 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 648a021a1193a8..c153be18ec9bbb 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -309,12 +309,12 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl return $block_content; } - // Check what style features the block supports. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - if ( ! $block_type ) { + // If no render_callback, assume styles have been previously handled. + if ( ! $block_type || ! $block_type->render_callback ) { return $block_content; } - + // Check what style features the block supports. $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); $attributes = array(); diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index 7f8d3dfd7c5a43..0399944bb84d79 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -93,10 +93,11 @@ private function assert_styles_and_classes_match( $block, $expected_classes, $ex */ function test_named_color_support() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalColor' => true, ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -124,10 +125,11 @@ function test_named_color_support() { */ function test_custom_color_support() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalColor' => true, ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -160,12 +162,13 @@ function test_custom_color_support() { */ function test_named_link_color_support() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalColor' => array( 'linkColor' => true, ), ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -190,12 +193,13 @@ function test_named_link_color_support() { */ function test_custom_link_color_support() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalColor' => array( 'linkColor' => true, ), ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -220,12 +224,13 @@ function test_custom_link_color_support() { */ function test_named_gradient_support() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalColor' => array( 'gradients' => true, ), ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -250,12 +255,13 @@ function test_named_gradient_support() { */ function test_custom_gradient_support() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalColor' => array( 'gradients' => true, ), ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -280,8 +286,9 @@ function test_custom_gradient_support() { */ function test_color_unsupported() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array(), + 'attributes' => array(), + 'supports' => array(), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -315,10 +322,11 @@ function test_color_unsupported() { */ function test_named_font_size() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalFontSize' => true, ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -343,10 +351,11 @@ function test_named_font_size() { */ function test_custom_font_size() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalFontSize' => true, ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -371,8 +380,9 @@ function test_custom_font_size() { */ function test_font_size_unsupported() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array(), + 'attributes' => array(), + 'supports' => array(), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -398,10 +408,11 @@ function test_font_size_unsupported() { */ function test_line_height() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalLineHeight' => true, ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -426,8 +437,9 @@ function test_line_height() { */ function test_line_height_unsupported() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array(), + 'attributes' => array(), + 'supports' => array(), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -452,8 +464,8 @@ function test_line_height_unsupported() { */ function test_all_supported() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalColor' => array( 'gradients' => true, 'linkColor' => true, @@ -461,6 +473,7 @@ function test_all_supported() { '__experimentalFontSize' => true, '__experimentalLineHeight' => true, ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -497,10 +510,11 @@ function test_all_supported() { */ function test_one_supported() { $block_type_settings = array( - 'attributes' => array(), - 'supports' => array( + 'attributes' => array(), + 'supports' => array( '__experimentalFontSize' => true, ), + 'render_callback' => true, ); $this->register_block_type( 'core/example', $block_type_settings ); @@ -530,4 +544,48 @@ function test_one_supported() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + + /** + * Tests there is no change without 'render_callback' in block_type. + */ + function test_render_callback_required() { + $block_type_settings = array( + 'attributes' => array(), + 'supports' => array( + '__experimentalColor' => array( + 'gradients' => true, + 'linkColor' => true, + ), + '__experimentalFontSize' => true, + '__experimentalLineHeight' => true, + ), + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array( + 'style' => array( + 'color' => array( + 'text' => '#000', + 'background' => '#fff', + 'gradient' => 'some-gradient', + 'style' => array( 'color' => array( 'link' => '#fff' ) ), + ), + 'typography' => array( + 'lineHeight' => '20', + 'fontSize' => '10', + ), + ), + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class'; + $expected_styles = 'test:style;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } }