| @@ -5,8 +5,97 @@ | ||
| 5 | 5 | * @package WordPress |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | + * Build an array with CSS classes and inline styles defining the colors | |
| 10 | + * which will be applied to the navigation markup in the front-end. | |
| 11 | + * | |
| 12 | + * @param array $attributes Navigation block attributes. | |
| 13 | + * @return array Colors CSS classes and inline styles. | |
| 14 | + */ | |
| 15 | +function gutenberg_post_author_build_css_colors( $attributes ) { | |
| 16 | + $text_colors = array( | |
| 17 | + 'css_classes' => array(), | |
| 18 | + 'inline_styles' => '', | |
| 19 | + ); | |
| 20 | + $background_colors = array( | |
| 21 | + 'css_classes' => array(), | |
| 22 | + 'inline_styles' => '', | |
| 23 | + ); | |
| 24 | + | |
| 25 | + // Text color. | |
| 26 | + $has_named_text_color = array_key_exists( 'textColor', $attributes ); | |
| 27 | + $has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); | |
| 28 | + | |
| 29 | + // If has text color. | |
| 30 | + if ( $has_custom_text_color || $has_named_text_color ) { | |
| 31 | + // Add has-text-color class. | |
| 32 | + $text_colors['css_classes'][] = 'has-text-color'; | |
| 33 | + } | |
| 34 | + | |
| 35 | + if ( $has_named_text_color ) { | |
| 36 | + // Add the color class. | |
| 37 | + $text_colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] ); | |
| 38 | + } elseif ( $has_custom_text_color ) { | |
| 39 | + // Add the custom color inline style. | |
| 40 | + $text_colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + // Background color. | |
| 44 | + $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); | |
| 45 | + $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); | |
| 46 | + | |
| 47 | + // If has background color. | |
| 48 | + if ( $has_custom_background_color || $has_named_background_color ) { | |
| 49 | + // Add has-background-color class. | |
| 50 | + $background_colors['css_classes'][] = 'has-background-color'; | |
| 51 | + } | |
| 52 | + | |
| 53 | + if ( $has_named_background_color ) { | |
| 54 | + // Add the background-color class. | |
| 55 | + $background_colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); | |
| 56 | + } elseif ( $has_custom_background_color ) { | |
| 57 | + // Add the custom background-color inline style. | |
| 58 | + $background_colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + return array( | |
| 62 | + 'text' => $text_colors, | |
| 63 | + 'background' => $background_colors, | |
| 64 | + ); | |
| 65 | +} | |
| 66 | + | |
| 67 | +/** | |
| 68 | + * Build an array with CSS classes and inline styles defining the font sizes | |
| 69 | + * which will be applied to the navigation markup in the front-end. | |
| 70 | + * | |
| 71 | + * @param array $attributes Navigation block attributes. | |
| 72 | + * @return array Font size CSS classes and inline styles. | |
| 73 | + */ | |
| 74 | +function gutenberg_post_author_build_css_font_sizes( $attributes ) { | |
| 75 | + // CSS classes. | |
| 76 | + $font_sizes = array( | |
| 77 | + 'css_classes' => array(), | |
| 78 | + 'inline_styles' => '', | |
| 79 | + ); | |
| 80 | + | |
| 81 | + $has_named_font_size = array_key_exists( 'fontSize', $attributes ); | |
| 82 | + $has_custom_font_size = array_key_exists( 'style', $attributes ) | |
| 83 | + && array_key_exists( 'typography', $attributes['style'] ) | |
| 84 | + && array_key_exists( 'fontSize', $attributes['style']['typography'] ); | |
| 85 | + | |
| 86 | + if ( $has_named_font_size ) { | |
| 87 | + // Add the font size class. | |
| 88 | + $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] ); | |
| 89 | + } elseif ( $has_custom_font_size ) { | |
| 90 | + // Add the custom font size inline style. | |
| 91 | + $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['style']['typography']['fontSize'] ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + return $font_sizes; | |
| 95 | +} | |
| 96 | + | |
| 97 | +/** | |
| 9 | 98 | * Renders the `core/post-author` block on the server. |
| 10 | 99 | * |
| 11 | 100 | * @param array $attributes Block attributes. |
| 12 | 101 | * @param string $content Block default content. |
| @@ -27,21 +116,35 @@ | ||
| 27 | 116 | $author_id, |
| 28 | 117 | $attributes['avatarSize'] |
| 29 | 118 | ) : null; |
| 30 | 119 | |
| 31 | - $byline = ! empty( $attributes['byline'] ) ? $attributes['byline'] : false; | |
| 32 | - $classes = array_merge( | |
| 120 | + $byline = ! empty( $attributes['byline'] ) ? $attributes['byline'] : false; | |
| 121 | + $colors = gutenberg_post_author_build_css_colors( $attributes ); | |
| 122 | + $font_sizes = gutenberg_post_author_build_css_font_sizes( $attributes ); | |
| 123 | + $classes = array_merge( | |
| 124 | + $colors['background']['css_classes'], | |
| 125 | + $font_sizes['css_classes'], | |
| 126 | + array( 'wp-block-post-author' ), | |
| 33 | 127 | isset( $attributes['className'] ) ? array( $attributes['className'] ) : array(), |
| 34 | 128 | isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(), |
| 35 | - isset( $attributes['textAlign'] ) ? array( 'has-text-align-' . $attributes['textAlign'] ) : array() | |
| 129 | + isset( $attributes['align'] ) ? array( 'has-text-align-' . $attributes['align'] ) : array() | |
| 36 | 130 | ); |
| 37 | 131 | |
| 38 | - $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); | |
| 132 | + $class_attribute = sprintf( ' class="%s"', esc_attr( implode( ' ', $classes ) ) ); | |
| 133 | + $style_attribute = ( $colors['background']['inline_styles'] || $font_sizes['inline_styles'] ) | |
| 134 | + ? sprintf( ' style="%s"', esc_attr( $colors['background']['inline_styles'] ) . esc_attr( $font_sizes['inline_styles'] ) ) | |
| 135 | + : ''; | |
| 39 | 136 | |
| 40 | - return sprintf( '<div %1$s>', $wrapper_attributes ) . | |
| 137 | + // Add text colors on content for higher specificty. Prevents theme override for has-background-color. | |
| 138 | + $content_class_attribute = sprintf( ' class="wp-block-post-author__content %s"', esc_attr( implode( ' ', $colors['text']['css_classes'] ) ) ); | |
| 139 | + $content_style_attribute = ( $colors['text']['inline_styles'] ) | |
| 140 | + ? sprintf( ' style="%s"', esc_attr( $colors['text']['inline_styles'] ) ) | |
| 141 | + : ''; | |
| 142 | + | |
| 143 | + return sprintf( '<div %1$s %2$s>', $class_attribute, $style_attribute ) . | |
| 41 | 144 | ( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) . |
| 42 | - '<div class="wp-block-post-author__content">' . | |
| 43 | - ( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . esc_html( $byline ) . '</p>' : '' ) . | |
| 145 | + sprintf( '<div %1$s %2$s>', $content_class_attribute, $content_style_attribute ) . | |
| 146 | + ( ! empty( $byline ) ? '<p class="wp-block-post-author__byline">' . $byline . '</p>' : '' ) . | |
| 44 | 147 | '<p class="wp-block-post-author__name">' . get_the_author_meta( 'display_name', $author_id ) . '</p>' . |
| 45 | 148 | ( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) . |
| 46 | 149 | '</div>' . |
| 47 | 150 | '</div>'; |