| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side rendering of the `core/post-author` block. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 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 |
/** |
| 98 |
* Renders the `core/post-author` block on the server. |
| 99 |
* |
| 100 |
* @param array $attributes Block attributes. |
| 101 |
* @param string $content Block default content. |
| 102 |
* @param WP_Block $block Block instance. |
| 103 |
* @return string Returns the rendered author block. |
| 104 |
*/ |
| 105 |
function gutenberg_render_block_core_post_author( $attributes, $content, $block ) { |
| 106 |
if ( ! isset( $block->context['postId'] ) ) { |
| 107 |
return ''; |
| 108 |
} |
| 109 |
|
| 110 |
$author_id = get_post_field( 'post_author', $block->context['postId'] ); |
| 111 |
if ( empty( $author_id ) ) { |
| 112 |
return ''; |
| 113 |
} |
| 114 |
|
| 115 |
$avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar( |
| 116 |
$author_id, |
| 117 |
$attributes['avatarSize'] |
| 118 |
) : null; |
| 119 |
|
| 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' ), |
| 127 |
isset( $attributes['className'] ) ? array( $attributes['className'] ) : array(), |
| 128 |
isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(), |
| 129 |
isset( $attributes['align'] ) ? array( 'has-text-align-' . $attributes['align'] ) : array() |
| 130 |
); |
| 131 |
|
| 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 |
: ''; |
| 136 |
|
| 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 ) . |
| 144 |
( ! empty( $attributes['showAvatar'] ) ? '<div class="wp-block-post-author__avatar">' . $avatar . '</div>' : '' ) . |
| 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>' : '' ) . |
| 147 |
'<p class="wp-block-post-author__name">' . get_the_author_meta( 'display_name', $author_id ) . '</p>' . |
| 148 |
( ! empty( $attributes['showBio'] ) ? '<p class="wp-block-post-author__bio">' . get_the_author_meta( 'user_description', $author_id ) . '</p>' : '' ) . |
| 149 |
'</div>' . |
| 150 |
'</div>'; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Registers the `core/post-author` block on the server. |
| 155 |
*/ |
| 156 |
function gutenberg_register_block_core_post_author() { |
| 157 |
register_block_type_from_metadata( |
| 158 |
__DIR__ . '/post-author', |
| 159 |
array( |
| 160 |
'render_callback' => 'gutenberg_render_block_core_post_author', |
| 161 |
) |
| 162 |
); |
| 163 |
} |
| 164 |
add_action( 'init', 'gutenberg_register_block_core_post_author', 20 ); |
| 165 |
|