From 96991fda64ff060305f19f2af5133f78144b596a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 10:45:05 -0400 Subject: [PATCH 01/12] added default fallbacks --- lib/blocks.php | 112 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 85 insertions(+), 27 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index a618da90590db2..3b88d358ad3279 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -242,9 +242,9 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl $supports = gutenberg_experimental_global_styles_get_supported_styles( $block_type->supports ); $attributes = array(); - $attributes = gutenberg_experimental_build_css_colors( $attributes, $block['attrs'], $supports ); - $attributes = gutenberg_experimental_build_css_typography( $attributes, $block['attrs'], $supports ); - $attributes = gutenberg_build_css_block_alignment( $attributes, $block['attrs'], $supports ); + $attributes = gutenberg_experimental_build_css_colors( $attributes, $block['attrs'], $supports, $block_type ); + $attributes = gutenberg_experimental_build_css_typography( $attributes, $block['attrs'], $supports, $block_type ); + $attributes = gutenberg_build_css_block_alignment( $attributes, $block['attrs'], $supports, $block_type ); if ( ! count( $attributes ) ) { return $block_content; @@ -295,20 +295,23 @@ function gutenberg_experimental_apply_classnames_and_styles( $block_content, $bl * Add CSS classes and inline styles for colors to the incoming attributes array. * This will be applied to the block markup in the front-end. * - * @param array $attributes comprehensive list of attributes to be applied. - * @param array $block_attributes block attributes. - * @param array $supports style features the 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. + * @param object $block_type registered block type class. * @return array Colors CSS classes and inline styles. */ -function gutenberg_experimental_build_css_colors( $attributes, $block_attributes, $supports ) { +function gutenberg_experimental_build_css_colors( $attributes, $block_attributes, $supports, $block_type ) { // Text Colors. // Check support for text colors. if ( in_array( 'color', $supports, true ) ) { - $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); - $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); + $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); + $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); + $has_named_text_color_default = isset( $block_type->attributes['textColor']['default'] ); + $has_custom_text_color_default = isset( $block_type->attributes['style']['color']['text']['default'] ); // Apply required generic class. - if ( $has_custom_text_color || $has_named_text_color ) { + if ( $has_custom_text_color || $has_named_text_color || $has_named_text_color_default || $has_custom_text_color_default ) { $attributes['css_classes'][] = 'has-text-color'; } // Apply color class or inline style. @@ -317,14 +320,25 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } elseif ( $has_custom_text_color ) { $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); } + // Fallback to default values if defined. + elseif ( $has_named_text_color_default ) { + $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_type->attributes['textColor']['default'] ); + } elseif ( $has_custom_text_color_default ) { + $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_type->attributes['style']['color']['text']['default'] ); + } } // Link Colors. if ( in_array( 'link-color', $supports, true ) ) { - $has_link_color = isset( $block_attributes['style']['color']['link'] ); + $has_link_color = isset( $block_attributes['style']['color']['link'] ); + $has_link_color_default = isset( $block_type->attributes['style']['color']['link']['default'] ); + // Apply required class and style. - if ( $has_link_color ) { + if ( $has_link_color || $has_link_color_default ) { $attributes['css_classes'][] = 'has-link-color'; + } + + if ( $has_link_color ) { // If link is a named color. if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. @@ -335,15 +349,29 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); } } + // Fallback to default value if defined. + elseif ( $has_link_color_default ) { + // If link is a named color. + if ( strpos( $block_type->attributes['style']['color']['link']['default'], 'var:preset|color|' ) !== false ) { + // Get the name from the string and add proper styles. + $index_to_splice = strrpos( $block_type->attributes['style']['color']['link']['default'], '|' ) + 1; + $link_color_name = substr( $block_type->attributes['style']['color']['link']['default'], $index_to_splice ); + $attributes['inline_styles'][] = sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + } else { + $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_type->attributes['style']['color']['link']['default'] ); + } + } } // Background Colors. if ( in_array( 'background-color', $supports, true ) ) { - $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); - $has_custom_background_color = isset( $block_attributes['style']['color']['background'] ); + $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); + $has_custom_background_color = isset( $block_attributes['style']['color']['background'] ); + $has_named_background_color_default = isset( $block_type->attributes['backgroundColor']['default'] ); + $has_custom_background_color_default = isset( $block_type->attributes['style']['color']['background']['default'] ); // Apply required background class. - if ( $has_custom_background_color || $has_named_background_color ) { + if ( $has_custom_background_color || $has_named_background_color || $has_named_background_color_default || $has_custom_background_color_default ) { $attributes['css_classes'][] = 'has-background'; } // Apply background color classes or styles. @@ -352,14 +380,22 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } elseif ( $has_custom_background_color ) { $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] ); } + // Fallback to default values if defined. + elseif ( $has_named_background_color_default ) { + $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); + } elseif ( $has_custom_background_color_default ) { + $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['color']['background']['default'] ); + } } // Gradients. if ( in_array( 'background', $supports, true ) ) { - $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); - $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] ); + $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); + $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] ); + $has_named_gradient_default = isset( $block_type->attributes['gradient']['default'] ); + $has_custom_gradient_default = isset( $block_type->attributes['style']['color']['gradient']['default'] ); - if ( $has_named_gradient || $has_custom_gradient ) { + if ( $has_named_gradient || $has_custom_gradient || $has_named_gradient_default || $has_custom_gradient_default ) { $attributes['css_classes'][] = 'has-background'; } // Apply required background class. @@ -368,6 +404,12 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } elseif ( $has_custom_gradient ) { $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] ); } + // Fallback to default values if defined. + elseif ( $has_named_gradient_default ) { + $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); + } elseif ( $has_custom_gradient_default ) { + $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_type->attributes['style']['color']['gradient']['default'] ); + } } return $attributes; @@ -377,12 +419,13 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes * Add CSS classes and inline styles for font sizes to the incoming attributes array. * This will be applied to the block markup in the front-end. * - * @param array $attributes comprehensive list of attributes to be applied. - * @param array $block_attributes block attributes. - * @param array $supports style features the 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. + * @param object $block_type registered block type class. * @return array Font size CSS classes and inline styles. */ -function gutenberg_experimental_build_css_typography( $attributes, $block_attributes, $supports ) { +function gutenberg_experimental_build_css_typography( $attributes, $block_attributes, $supports, $block_type ) { // Font Size. if ( in_array( 'font-size', $supports, true ) ) { $has_named_font_size = array_key_exists( 'fontSize', $block_attributes ); @@ -394,6 +437,12 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib } elseif ( $has_custom_font_size ) { $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] ); } + // Fallback to default values if defined. + elseif ( isset( $block_type->attributes['fontSize']['default'] ) ) { + $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_type->attributes['fontSize']['default'] ); + } elseif ( isset( $block_type->attributes['style']['typography']['fontSize']['default'] ) ) { + $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_type->attributes['style']['typography']['fontSize']['default'] ); + } } // Line Height. @@ -403,6 +452,10 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib if ( $has_line_height ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); } + // Fallback to default value if defined. + elseif ( isset( $block_type->attributes['style']['typography']['lineHeight']['default'] ) ) { + $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_type->attributes['style']['typography']['lineHeight']['default'] ); + } } return $attributes; @@ -412,18 +465,23 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib * Add CSS classes for block alignment to the incoming attributes array. * This will be applied to the block markup in the front-end. * - * @param array $attributes comprehensive list of attributes to be applied. - * @param array $block_attributes block attributes. - * @param array $supports style features the 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. + * @param object $block_type registered block type class. * @return array Block alignment CSS classes and inline styles. */ -function gutenberg_build_css_block_alignment( $attributes, $block_attributes, $supports ) { +function gutenberg_build_css_block_alignment( $attributes, $block_attributes, $supports, $block_type ) { if ( in_array( 'block-align', $supports, true ) ) { $has_block_alignment = array_key_exists( 'align', $block_attributes ); - if ( $has_block_alignment ) { + if ( $has_block_alignment && $block_attributes['align'] !== '' ) { $attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] ); } + // Fallback to default value if defined. + elseif ( isset( $block_type->attributes['align']['default'] ) ) { + $attributes['css_classes'][] = sprintf( 'align%s', $block_type->attributes['align']['default'] ); + } } return $attributes; From 228273c56e1f99aa3a799d72fc736b0700be393d Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 14:26:56 -0400 Subject: [PATCH 02/12] handle default background and gradient more gracefully --- lib/blocks.php | 73 +++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 52 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 3b88d358ad3279..7b0176293b98e8 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -305,13 +305,12 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes // Text Colors. // Check support for text colors. if ( in_array( 'color', $supports, true ) ) { - $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); - $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); - $has_named_text_color_default = isset( $block_type->attributes['textColor']['default'] ); - $has_custom_text_color_default = isset( $block_type->attributes['style']['color']['text']['default'] ); + $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); + $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); + $has_named_text_color_default = isset( $block_type->attributes['textColor']['default'] ); // Apply required generic class. - if ( $has_custom_text_color || $has_named_text_color || $has_named_text_color_default || $has_custom_text_color_default ) { + if ( $has_custom_text_color || $has_named_text_color || $has_named_text_color_default ) { $attributes['css_classes'][] = 'has-text-color'; } // Apply color class or inline style. @@ -323,22 +322,16 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes // Fallback to default values if defined. elseif ( $has_named_text_color_default ) { $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_type->attributes['textColor']['default'] ); - } elseif ( $has_custom_text_color_default ) { - $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_type->attributes['style']['color']['text']['default'] ); } } // Link Colors. if ( in_array( 'link-color', $supports, true ) ) { - $has_link_color = isset( $block_attributes['style']['color']['link'] ); - $has_link_color_default = isset( $block_type->attributes['style']['color']['link']['default'] ); - - // Apply required class and style. - if ( $has_link_color || $has_link_color_default ) { - $attributes['css_classes'][] = 'has-link-color'; - } + $has_link_color = isset( $block_attributes['style']['color']['link'] ); if ( $has_link_color ) { + // Apply required class and style. + $attributes['css_classes'][] = 'has-link-color'; // If link is a named color. if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. @@ -349,29 +342,15 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); } } - // Fallback to default value if defined. - elseif ( $has_link_color_default ) { - // If link is a named color. - if ( strpos( $block_type->attributes['style']['color']['link']['default'], 'var:preset|color|' ) !== false ) { - // Get the name from the string and add proper styles. - $index_to_splice = strrpos( $block_type->attributes['style']['color']['link']['default'], '|' ) + 1; - $link_color_name = substr( $block_type->attributes['style']['color']['link']['default'], $index_to_splice ); - $attributes['inline_styles'][] = sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); - } else { - $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_type->attributes['style']['color']['link']['default'] ); - } - } } // Background Colors. if ( in_array( 'background-color', $supports, true ) ) { - $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); - $has_custom_background_color = isset( $block_attributes['style']['color']['background'] ); - $has_named_background_color_default = isset( $block_type->attributes['backgroundColor']['default'] ); - $has_custom_background_color_default = isset( $block_type->attributes['style']['color']['background']['default'] ); + $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 || $has_named_background_color_default || $has_custom_background_color_default ) { + if ( $has_custom_background_color || $has_named_background_color ) { $attributes['css_classes'][] = 'has-background'; } // Apply background color classes or styles. @@ -380,22 +359,14 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } elseif ( $has_custom_background_color ) { $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] ); } - // Fallback to default values if defined. - elseif ( $has_named_background_color_default ) { - $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); - } elseif ( $has_custom_background_color_default ) { - $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['color']['background']['default'] ); - } } // Gradients. if ( in_array( 'background', $supports, true ) ) { - $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); - $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] ); - $has_named_gradient_default = isset( $block_type->attributes['gradient']['default'] ); - $has_custom_gradient_default = isset( $block_type->attributes['style']['color']['gradient']['default'] ); + $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 || $has_named_gradient_default || $has_custom_gradient_default ) { + if ( $has_named_gradient || $has_custom_gradient ) { $attributes['css_classes'][] = 'has-background'; } // Apply required background class. @@ -404,11 +375,15 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } elseif ( $has_custom_gradient ) { $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] ); } - // Fallback to default values if defined. - elseif ( $has_named_gradient_default ) { + } + + if ( ! in_array( 'has-background', $attributes['css_classes'], true ) ) { + if ( in_array( 'background-color', $supports, true ) && isset( $block_type->attributes['backgroundColor']['default'] ) ) { + $attributes['css_classes'][] = 'has-background'; + $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); + } elseif ( in_array( 'background', $supports, true ) && isset( $block_type->attributes['gradient']['default'] ) ) { + $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); - } elseif ( $has_custom_gradient_default ) { - $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_type->attributes['style']['color']['gradient']['default'] ); } } @@ -440,8 +415,6 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib // Fallback to default values if defined. elseif ( isset( $block_type->attributes['fontSize']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_type->attributes['fontSize']['default'] ); - } elseif ( isset( $block_type->attributes['style']['typography']['fontSize']['default'] ) ) { - $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_type->attributes['style']['typography']['fontSize']['default'] ); } } @@ -452,10 +425,6 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib if ( $has_line_height ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); } - // Fallback to default value if defined. - elseif ( isset( $block_type->attributes['style']['typography']['lineHeight']['default'] ) ) { - $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_type->attributes['style']['typography']['lineHeight']['default'] ); - } } return $attributes; From 5e7ae24d65e90cde4bc2764e9b7e3764c74f746b Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 15:39:28 -0400 Subject: [PATCH 03/12] check for explicit no alignment value --- lib/blocks.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 7b0176293b98e8..4db684f65b4f83 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -444,12 +444,16 @@ function gutenberg_build_css_block_alignment( $attributes, $block_attributes, $s if ( in_array( 'block-align', $supports, true ) ) { $has_block_alignment = array_key_exists( 'align', $block_attributes ); - if ( $has_block_alignment && $block_attributes['align'] !== '' ) { - $attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] ); - } - // Fallback to default value if defined. - elseif ( isset( $block_type->attributes['align']['default'] ) ) { - $attributes['css_classes'][] = sprintf( 'align%s', $block_type->attributes['align']['default'] ); + // Don't apply style or default if attribute is saved to be ''. + // This is used as the none option when a default value is defined. + if ( $block_attributes['align'] !== '' ) { + if ( $has_block_alignment ) { + $attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] ); + } + // Fallback to default value if defined. + elseif ( isset( $block_type->attributes['align']['default'] ) ) { + $attributes['css_classes'][] = sprintf( 'align%s', $block_type->attributes['align']['default'] ); + } } } From 8e62e67e6a510e28ce19f01da23839458946d020 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 16:43:05 -0400 Subject: [PATCH 04/12] support custom defaults --- lib/blocks.php | 59 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 4db684f65b4f83..f3d592fdd5b240 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -308,9 +308,10 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); $has_named_text_color_default = isset( $block_type->attributes['textColor']['default'] ); + $has_custom_text_color_default = isset( $block_type->attributes['style']['default']['color']['text'] ); // Apply required generic class. - if ( $has_custom_text_color || $has_named_text_color || $has_named_text_color_default ) { + if ( $has_custom_text_color || $has_named_text_color || $has_named_text_color_default || $has_custom_text_color_default ) { $attributes['css_classes'][] = 'has-text-color'; } // Apply color class or inline style. @@ -322,16 +323,22 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes // Fallback to default values if defined. elseif ( $has_named_text_color_default ) { $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_type->attributes['textColor']['default'] ); + } elseif ( $has_custom_text_color_default ) { + $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_type->attributes['style']['default']['color']['text'] ); } } // Link Colors. if ( in_array( 'link-color', $supports, true ) ) { $has_link_color = isset( $block_attributes['style']['color']['link'] ); + $has_link_color_default = isset( $block_type->attributes['style']['default']['color']['link'] ); - if ( $has_link_color ) { + if ( $has_link_color || $has_link_color_default ) { // Apply required class and style. $attributes['css_classes'][] = 'has-link-color'; + } + + if( $has_link_color ) { // If link is a named color. if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. @@ -342,6 +349,20 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); } } + // Fallback to default value if defined. + elseif ( $has_link_color_default ) { + // If link is a named color. + if ( strpos( $block_type->attributes['style']['default']['color']['link'], 'var:preset|color|' ) !== false ) { + // Get the name from the string and add proper styles. + $index_to_splice = strrpos( $block_type->attributes['style']['default']['color']['link'], '|' ) + 1; + $link_color_name = substr( $block_type->attributes['style']['default']['color']['link'], $index_to_splice ); + $attributes['inline_styles'][] = sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name ); + } else { + $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_type->attributes['style']['default']['color']['link'] ); + } + } + + } // Background Colors. @@ -377,13 +398,27 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } } - if ( ! in_array( 'has-background', $attributes['css_classes'], true ) ) { - if ( in_array( 'background-color', $supports, true ) && isset( $block_type->attributes['backgroundColor']['default'] ) ) { - $attributes['css_classes'][] = 'has-background'; - $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); - } elseif ( in_array( 'background', $supports, true ) && isset( $block_type->attributes['gradient']['default'] ) ) { - $attributes['css_classes'][] = 'has-background'; - $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); + // Fallback to default values for background and gradient if neither have been set. + if ( ! isset( $attributes['css_classes'] ) || ! in_array( 'has-background', $attributes['css_classes'], true ) ) { + // Check backrgound support and apply default background if exists. + if ( in_array( 'background-color', $supports, true ) ) { + if ( isset( $block_type->attributes['backgroundColor']['default'] ) ) { + $attributes['css_classes'][] = 'has-background'; + $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); + } elseif ( isset( $block_type->attributes['style']['default']['color']['background'] ) ) { + $attributes['css_classes'][] = 'has-background'; + $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['default']['color']['background'] ); + } + } + // Check gradient support and apply default background if exists. + elseif ( in_array( 'background', $supports, true ) ) { + if ( isset( $block_type->attributes['gradient']['default'] ) ) { + $attributes['css_classes'][] = 'has-background'; + $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); + } elseif ( isset( $block_type->attributes['style']['default']['color']['gradient'] ) ) { + $attributes['css_classes'][] = 'has-background'; + $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_type->attributes['style']['default']['color']['gradient'] ); + } } } @@ -415,6 +450,8 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib // Fallback to default values if defined. elseif ( isset( $block_type->attributes['fontSize']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_type->attributes['fontSize']['default'] ); + } elseif ( isset( $block_type->attributes['style']['default']['typography']['fontSize'] ) ) { + $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_type->attributes['style']['default']['typography']['fontSize'] ); } } @@ -425,6 +462,10 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib if ( $has_line_height ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); } + // Fallback to default value if defined. + elseif ( isset( $block_type->attributes['style']['default']['typography']['lineHeight'] ) ) { + $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_type->attributes['style']['default']['typography']['lineHeight'] ); + } } return $attributes; From 71009467e43895f1bc5fc54cc4c2f2690a952011 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 16:46:23 -0400 Subject: [PATCH 05/12] php format --- lib/blocks.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index f3d592fdd5b240..39e231f27c425f 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -305,9 +305,9 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes // Text Colors. // Check support for text colors. if ( in_array( 'color', $supports, true ) ) { - $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); - $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); - $has_named_text_color_default = isset( $block_type->attributes['textColor']['default'] ); + $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); + $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); + $has_named_text_color_default = isset( $block_type->attributes['textColor']['default'] ); $has_custom_text_color_default = isset( $block_type->attributes['style']['default']['color']['text'] ); // Apply required generic class. @@ -330,15 +330,15 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes // Link Colors. if ( in_array( 'link-color', $supports, true ) ) { - $has_link_color = isset( $block_attributes['style']['color']['link'] ); + $has_link_color = isset( $block_attributes['style']['color']['link'] ); $has_link_color_default = isset( $block_type->attributes['style']['default']['color']['link'] ); + // Apply required class and style. if ( $has_link_color || $has_link_color_default ) { - // Apply required class and style. $attributes['css_classes'][] = 'has-link-color'; } - if( $has_link_color ) { + if ( $has_link_color ) { // If link is a named color. if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. @@ -361,14 +361,12 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_type->attributes['style']['default']['color']['link'] ); } } - - } // Background Colors. if ( in_array( 'background-color', $supports, true ) ) { - $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); - $has_custom_background_color = isset( $block_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 ) { @@ -384,8 +382,8 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes // Gradients. if ( in_array( 'background', $supports, true ) ) { - $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); - $has_custom_gradient = isset( $block_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 ) { $attributes['css_classes'][] = 'has-background'; @@ -406,7 +404,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); } elseif ( isset( $block_type->attributes['style']['default']['color']['background'] ) ) { - $attributes['css_classes'][] = 'has-background'; + $attributes['css_classes'][] = 'has-background'; $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['default']['color']['background'] ); } } @@ -416,7 +414,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); } elseif ( isset( $block_type->attributes['style']['default']['color']['gradient'] ) ) { - $attributes['css_classes'][] = 'has-background'; + $attributes['css_classes'][] = 'has-background'; $attributes['inline_styles'][] = sprintf( 'background: %s;', $block_type->attributes['style']['default']['color']['gradient'] ); } } From fb579230d55e0f57ea7c5dd8da7e7b7b388cc0e1 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 17:04:52 -0400 Subject: [PATCH 06/12] fix gradient default --- lib/blocks.php | 12 +++++++++--- packages/block-library/src/post-author/block.json | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 39e231f27c425f..369c65fb0b8bb2 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -398,18 +398,24 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes // Fallback to default values for background and gradient if neither have been set. if ( ! isset( $attributes['css_classes'] ) || ! in_array( 'has-background', $attributes['css_classes'], true ) ) { + $has_named_background_default = isset( $block_type->attributes['backgroundColor']['default'] ); + $has_custom_background_default = isset( $block_type->attributes['style']['default']['color']['background'] ); + + echo ''; // Check backrgound support and apply default background if exists. - if ( in_array( 'background-color', $supports, true ) ) { - if ( isset( $block_type->attributes['backgroundColor']['default'] ) ) { + if ( in_array( 'background-color', $supports, true ) && ( $has_named_background_default || $has_custom_background_default ) ) { + echo ''; + if ( $has_named_background_default ) { $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); - } elseif ( isset( $block_type->attributes['style']['default']['color']['background'] ) ) { + } elseif ( $has_custom_background_default ) { $attributes['css_classes'][] = 'has-background'; $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['default']['color']['background'] ); } } // Check gradient support and apply default background if exists. elseif ( in_array( 'background', $supports, true ) ) { + echo ''; if ( isset( $block_type->attributes['gradient']['default'] ) ) { $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); diff --git a/packages/block-library/src/post-author/block.json b/packages/block-library/src/post-author/block.json index 3e788777dec81d..4235a933573781 100644 --- a/packages/block-library/src/post-author/block.json +++ b/packages/block-library/src/post-author/block.json @@ -18,6 +18,20 @@ }, "byline": { "type": "string" + }, + "style": { + "type": "object", + "default": { + "color": { + "gradient": "linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(0,212,255,1) 100%)", + "text": "#0F0", + "link": "#F00" + }, + "typography": { + "fontSize": "33", + "lineHeight": "2.0" + } + } } }, "usesContext": [ From ba6ce07f89cc1f4b2bc8a70d86c7c551d4c1074a Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 17:21:33 -0400 Subject: [PATCH 07/12] whoopsie, thats just for testing --- packages/block-library/src/post-author/block.json | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/packages/block-library/src/post-author/block.json b/packages/block-library/src/post-author/block.json index 4235a933573781..3e788777dec81d 100644 --- a/packages/block-library/src/post-author/block.json +++ b/packages/block-library/src/post-author/block.json @@ -18,20 +18,6 @@ }, "byline": { "type": "string" - }, - "style": { - "type": "object", - "default": { - "color": { - "gradient": "linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(0,212,255,1) 100%)", - "text": "#0F0", - "link": "#F00" - }, - "typography": { - "fontSize": "33", - "lineHeight": "2.0" - } - } } }, "usesContext": [ From 881c137556ac7683aec2006fc8799567451bb876 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 17:27:08 -0400 Subject: [PATCH 08/12] thought i got rid of you... --- lib/blocks.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 369c65fb0b8bb2..3eb229f1620fac 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -401,10 +401,8 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $has_named_background_default = isset( $block_type->attributes['backgroundColor']['default'] ); $has_custom_background_default = isset( $block_type->attributes['style']['default']['color']['background'] ); - echo ''; // Check backrgound support and apply default background if exists. if ( in_array( 'background-color', $supports, true ) && ( $has_named_background_default || $has_custom_background_default ) ) { - echo ''; if ( $has_named_background_default ) { $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_type->attributes['backgroundColor']['default'] ); @@ -415,7 +413,6 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } // Check gradient support and apply default background if exists. elseif ( in_array( 'background', $supports, true ) ) { - echo ''; if ( isset( $block_type->attributes['gradient']['default'] ) ) { $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); From d101ae0563ff2134423b7ecb6030e50265dba833 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 18:07:03 -0400 Subject: [PATCH 09/12] update tests --- phpunit/class-block-supported-styles-test.php | 386 +++++++++++++++++- 1 file changed, 380 insertions(+), 6 deletions(-) diff --git a/phpunit/class-block-supported-styles-test.php b/phpunit/class-block-supported-styles-test.php index c4596be98e9ca1..d96a686e966aad 100644 --- a/phpunit/class-block-supported-styles-test.php +++ b/phpunit/class-block-supported-styles-test.php @@ -89,7 +89,7 @@ private function assert_styles_and_classes_match( $block, $expected_classes, $ex private $block_content = '
So say we all.
'; /** - * Tests color support for named color support for named colors. + * Tests color support for named colors. */ function test_named_color_support() { $block_type_settings = array( @@ -120,6 +120,38 @@ function test_named_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests default color support for named color default values. + */ + function test_named_color_support_default() { + $block_type_settings = array( + 'attributes' => array( + 'textColor' => array( 'default' => 'red' ), + 'backgroundColor' => array( 'default' => 'black' ), + // The following should not be applied (subcatagories of color support). + 'gradient' => array( 'default' => 'blue' ), + ), + 'supports' => array( + '__experimentalColor' => true, + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $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->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + /** * Tests color support for custom colors. */ @@ -142,7 +174,7 @@ function test_custom_color_support() { 'background' => '#fff', // The following should not be applied (subcatagories of color support). 'gradient' => 'some-gradient', - 'style' => array( 'color' => array( 'link' => '#fff' ) ), + 'link' => '#fff', ), ), ), @@ -157,6 +189,45 @@ function test_custom_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests color support for custom color default values. + */ + function test_custom_color_support_default() { + $block_type_settings = array( + 'attributes' => array( + 'style' => array( + 'default' => array( + 'color' => array( + 'text' => '#000', + 'background' => '#fff', + // The following should not be applied (subcatagories of color support). + 'gradient' => 'some-gradient', + 'link' => '#fff', + ), + ), + ), + ), + 'supports' => array( + '__experimentalColor' => true, + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $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 ); + } + /** * Tests link color support for named colors. */ @@ -188,6 +259,43 @@ function test_named_link_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests link color support for named color default value. + */ + function test_named_link_color_support_default() { + $block_type_settings = array( + 'attributes' => array( + 'style' => array( + 'default' => array( + 'color' => array( + 'link' => 'var:preset|color|red', + ), + ), + ), + ), + 'supports' => array( + '__experimentalColor' => array( + 'linkColor' => true, + ), + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $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->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + /** * Tests link color support for custom colors. */ @@ -219,6 +327,43 @@ function test_custom_link_color_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests link color support for custom colors default value. + */ + function test_custom_link_color_support_default() { + $block_type_settings = array( + 'attributes' => array( + 'style' => array( + 'default' => array( + 'color' => array( + 'link' => '#fff', + ), + ), + ), + ), + 'supports' => array( + '__experimentalColor' => array( + 'linkColor' => true, + ), + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class has-link-color'; + $expected_styles = 'test:style; --wp--style--color--link: #fff;'; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + /** * Tests gradient color support for named gradients. */ @@ -250,6 +395,37 @@ function test_named_gradient_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests gradient color support for named gradients default values. + */ + function test_named_gradient_support_default() { + $block_type_settings = array( + 'attributes' => array( + 'gradient' => array( 'default' => 'red' ), + ), + 'supports' => array( + '__experimentalColor' => array( + 'gradients' => true, + ), + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class has-background has-red-gradient-background'; + $expected_styles = 'test:style; '; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + /** * Tests gradient color support for custom gradients. */ @@ -281,12 +457,63 @@ function test_custom_gradient_support() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests gradient color support for custom gradients default values. + */ + function test_custom_gradient_support_default() { + $block_type_settings = array( + 'attributes' => array( + 'style' => array( + 'default' => array( + 'color' => array( + 'gradient' => 'some-gradient-style', + ), + ), + ), + ), + 'supports' => array( + '__experimentalColor' => array( + 'gradients' => true, + ), + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class has-background'; + $expected_styles = 'test:style; background: some-gradient-style;'; + + $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(), + 'attributes' => array( + 'textColor' => array( 'default' => 'red' ), + 'backgroundColor' => array( 'default' => 'black' ), + 'gradient' => array( 'default' => 'blue' ), + 'style' => array( + 'default' => array( + 'color' => array( + 'text' => '#000', + 'background' => '#fff', + 'link' => '#ggg', + 'gradient' => 'some-gradient', + ), + ), + ), + ), 'supports' => array(), 'render_callback' => true, ); @@ -346,6 +573,35 @@ function test_named_font_size() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests support for named font sizes default values. + */ + function test_named_font_size_default() { + $block_type_settings = array( + 'attributes' => array( + 'fontSize' => array( 'default' => 'large' ), + ), + 'supports' => array( + '__experimentalFontSize' => true, + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + '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 ); + } + /** * Tests support for custom font sizes. */ @@ -375,12 +631,56 @@ function test_custom_font_size() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests support for custom font size default values. + */ + function test_custom_font_size_default() { + $block_type_settings = array( + 'attributes' => array( + 'style' => array( + 'default' => array( + 'typography' => array( + 'fontSize' => '10', + ), + ), + ), + ), + 'supports' => array( + '__experimentalFontSize' => true, + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + '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 ); + } + /** * Tests that font size attributes are not applied without support flag. */ function test_font_size_unsupported() { $block_type_settings = array( - 'attributes' => array(), + 'attributes' => array( + 'fontSize' => array( 'default' => 'large' ), + 'style' => array( + 'default' => array( + 'typography' => array( + 'fontSize' => '10', + ), + ), + ), + ), 'supports' => array(), 'render_callback' => true, ); @@ -432,12 +732,55 @@ function test_line_height() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests line height support default values. + */ + function test_line_height_default() { + $block_type_settings = array( + 'attributes' => array( + 'style' => array( + 'default' => array( + 'typography' => array( + 'lineHeight' => '10', + ), + ), + ), + ), + 'supports' => array( + '__experimentalLineHeight' => true, + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + '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 ); + } + /** * Tests line height not applied without support flag. */ function test_line_height_unsupported() { $block_type_settings = array( - 'attributes' => array(), + 'attributes' => array( + 'style' => array( + 'default' => array( + 'typography' => array( + 'lineHeight' => '10', + ), + ), + ), + ), 'supports' => array(), 'render_callback' => true, ); @@ -488,12 +831,43 @@ function test_block_alignment() { $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); } + /** + * Tests support for block alignment default values. + */ + function test_block_alignment_default() { + $block_type_settings = array( + 'attributes' => array( + 'align' => array( 'default' => 'wide' ), + ), + 'supports' => array( + 'align' => true, + ), + 'render_callback' => true, + ); + $this->register_block_type( 'core/example', $block_type_settings ); + + $block = array( + 'blockName' => 'core/example', + 'attrs' => array(), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + $expected_classes = 'wp-block-example foo-bar-class alignwide'; + $expected_styles = 'test:style; '; + + $this->assert_styles_and_classes_match( $block, $expected_classes, $expected_styles ); + } + /** * Tests block alignment requires support to be added. */ function test_block_alignment_unsupported() { $block_type_settings = array( - 'attributes' => array(), + 'attributes' => array( + 'align' => array( 'default' => 'wide' ), + ), 'supports' => array(), 'render_callback' => true, ); From 1e8963a1bbba1d9a317bee2f08b7b09cfcec1b79 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 18:54:52 -0400 Subject: [PATCH 10/12] fix linter errors --- lib/blocks.php | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 3eb229f1620fac..4280543278c261 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -319,8 +319,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_attributes['textColor'] ); } elseif ( $has_custom_text_color ) { $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); - } - // Fallback to default values if defined. + } // Fallback to default values if defined. elseif ( $has_named_text_color_default ) { $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_type->attributes['textColor']['default'] ); } elseif ( $has_custom_text_color_default ) { @@ -348,8 +347,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } else { $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); } - } - // Fallback to default value if defined. + } // Fallback to default value if defined. elseif ( $has_link_color_default ) { // If link is a named color. if ( strpos( $block_type->attributes['style']['default']['color']['link'], 'var:preset|color|' ) !== false ) { @@ -410,8 +408,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = 'has-background'; $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['default']['color']['background'] ); } - } - // Check gradient support and apply default background if exists. + } // Check gradient support and apply default background if exists. elseif ( in_array( 'background', $supports, true ) ) { if ( isset( $block_type->attributes['gradient']['default'] ) ) { $attributes['css_classes'][] = 'has-background'; @@ -447,8 +444,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] ); } elseif ( $has_custom_font_size ) { $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] ); - } - // Fallback to default values if defined. + } // Fallback to default values if defined. elseif ( isset( $block_type->attributes['fontSize']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_type->attributes['fontSize']['default'] ); } elseif ( isset( $block_type->attributes['style']['default']['typography']['fontSize'] ) ) { @@ -462,8 +458,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib // Add the style (no classes for line-height). if ( $has_line_height ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); - } - // Fallback to default value if defined. + } // Fallback to default value if defined. elseif ( isset( $block_type->attributes['style']['default']['typography']['lineHeight'] ) ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_type->attributes['style']['default']['typography']['lineHeight'] ); } @@ -488,11 +483,10 @@ function gutenberg_build_css_block_alignment( $attributes, $block_attributes, $s // Don't apply style or default if attribute is saved to be ''. // This is used as the none option when a default value is defined. - if ( $block_attributes['align'] !== '' ) { + if ( '' !== $block_attributes['align'] ) { if ( $has_block_alignment ) { $attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] ); - } - // Fallback to default value if defined. + } // Fallback to default value if defined. elseif ( isset( $block_type->attributes['align']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'align%s', $block_type->attributes['align']['default'] ); } From 1c332c78d791700aaa2e6dda5db386d7d284b5b8 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 19:18:04 -0400 Subject: [PATCH 11/12] darn php unit linter is a bully --- lib/blocks.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 4280543278c261..92d88f5c9b3d84 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -319,8 +319,8 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_attributes['textColor'] ); } elseif ( $has_custom_text_color ) { $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); - } // Fallback to default values if defined. - elseif ( $has_named_text_color_default ) { + // Fallback to default value if defined. + } elseif ( $has_named_text_color_default ) { $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_type->attributes['textColor']['default'] ); } elseif ( $has_custom_text_color_default ) { $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_type->attributes['style']['default']['color']['text'] ); @@ -347,8 +347,8 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } else { $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); } - } // Fallback to default value if defined. - elseif ( $has_link_color_default ) { + // Fallback to default value if defined. + } elseif ( $has_link_color_default ) { // If link is a named color. if ( strpos( $block_type->attributes['style']['default']['color']['link'], 'var:preset|color|' ) !== false ) { // Get the name from the string and add proper styles. @@ -408,8 +408,8 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = 'has-background'; $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['default']['color']['background'] ); } - } // Check gradient support and apply default background if exists. - elseif ( in_array( 'background', $supports, true ) ) { + // Check gradient support and apply default background if exists. + } elseif ( in_array( 'background', $supports, true ) ) { if ( isset( $block_type->attributes['gradient']['default'] ) ) { $attributes['css_classes'][] = 'has-background'; $attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_type->attributes['gradient']['default'] ); @@ -444,8 +444,8 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] ); } elseif ( $has_custom_font_size ) { $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] ); - } // Fallback to default values if defined. - elseif ( isset( $block_type->attributes['fontSize']['default'] ) ) { + // Fallback to default value if defined. + } elseif ( isset( $block_type->attributes['fontSize']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_type->attributes['fontSize']['default'] ); } elseif ( isset( $block_type->attributes['style']['default']['typography']['fontSize'] ) ) { $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_type->attributes['style']['default']['typography']['fontSize'] ); @@ -458,8 +458,8 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib // Add the style (no classes for line-height). if ( $has_line_height ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); - } // Fallback to default value if defined. - elseif ( isset( $block_type->attributes['style']['default']['typography']['lineHeight'] ) ) { + // Fallback to default value if defined. + } elseif ( isset( $block_type->attributes['style']['default']['typography']['lineHeight'] ) ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_type->attributes['style']['default']['typography']['lineHeight'] ); } } @@ -486,8 +486,8 @@ function gutenberg_build_css_block_alignment( $attributes, $block_attributes, $s if ( '' !== $block_attributes['align'] ) { if ( $has_block_alignment ) { $attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] ); - } // Fallback to default value if defined. - elseif ( isset( $block_type->attributes['align']['default'] ) ) { + // Fallback to default value if defined. + } elseif ( isset( $block_type->attributes['align']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'align%s', $block_type->attributes['align']['default'] ); } } From d1341eacca0287badcb40072e83c54ff35abd574 Mon Sep 17 00:00:00 2001 From: Addison-Stavlo Date: Thu, 30 Jul 2020 19:37:55 -0400 Subject: [PATCH 12/12] srsly go home evil linter --- lib/blocks.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 92d88f5c9b3d84..b60e7465848c3b 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -319,7 +319,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_attributes['textColor'] ); } elseif ( $has_custom_text_color ) { $attributes['inline_styles'][] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] ); - // Fallback to default value if defined. + // Fallback to default value if defined. } elseif ( $has_named_text_color_default ) { $attributes['css_classes'][] = sprintf( 'has-%s-color', $block_type->attributes['textColor']['default'] ); } elseif ( $has_custom_text_color_default ) { @@ -347,7 +347,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes } else { $attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] ); } - // Fallback to default value if defined. + // Fallback to default value if defined. } elseif ( $has_link_color_default ) { // If link is a named color. if ( strpos( $block_type->attributes['style']['default']['color']['link'], 'var:preset|color|' ) !== false ) { @@ -408,7 +408,7 @@ function gutenberg_experimental_build_css_colors( $attributes, $block_attributes $attributes['css_classes'][] = 'has-background'; $attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_type->attributes['style']['default']['color']['background'] ); } - // Check gradient support and apply default background if exists. + // Check gradient support and apply default background if exists. } elseif ( in_array( 'background', $supports, true ) ) { if ( isset( $block_type->attributes['gradient']['default'] ) ) { $attributes['css_classes'][] = 'has-background'; @@ -444,7 +444,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] ); } elseif ( $has_custom_font_size ) { $attributes['inline_styles'][] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] ); - // Fallback to default value if defined. + // Fallback to default value if defined. } elseif ( isset( $block_type->attributes['fontSize']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'has-%s-font-size', $block_type->attributes['fontSize']['default'] ); } elseif ( isset( $block_type->attributes['style']['default']['typography']['fontSize'] ) ) { @@ -458,7 +458,7 @@ function gutenberg_experimental_build_css_typography( $attributes, $block_attrib // Add the style (no classes for line-height). if ( $has_line_height ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] ); - // Fallback to default value if defined. + // Fallback to default value if defined. } elseif ( isset( $block_type->attributes['style']['default']['typography']['lineHeight'] ) ) { $attributes['inline_styles'][] = sprintf( 'line-height: %s;', $block_type->attributes['style']['default']['typography']['lineHeight'] ); } @@ -486,7 +486,7 @@ function gutenberg_build_css_block_alignment( $attributes, $block_attributes, $s if ( '' !== $block_attributes['align'] ) { if ( $has_block_alignment ) { $attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] ); - // Fallback to default value if defined. + // Fallback to default value if defined. } elseif ( isset( $block_type->attributes['align']['default'] ) ) { $attributes['css_classes'][] = sprintf( 'align%s', $block_type->attributes['align']['default'] ); }