File: /var/www/html/insiders/wp-load/wp-content/plugins/breadly/block-library/post-author-date.php
<?php
/**
* Renders the `gtm/post-author-date` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Returns the post author and date for the current post.
*/
function gtm_render_block_post_author_date( $attr, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
$post_ID = $block->context['postId'];
$author_id = get_post_field( 'post_author', $block->context['postId'] );
$author_name = get_the_author_meta( 'display_name', $author_id );
$author_url = get_author_posts_url( $author_id );
$avatar_html = get_avatar( $author_id, intval( $attr['avatarSize'] ) );
$tooltip = gtm_tooltip( sprintf( esc_html__( "View posts from %s", 'gutenmate' ), $author_name ) );
$wrapper_attributes = get_block_wrapper_attributes( ['class' => ''] );
$the_date = ( $attr['dateType'] ?? '' ) == 'modified' ? get_the_modified_date( 'U' ) : get_the_date( 'U' );
$date = '';
if ( $attr['dateFormat'] == 'time_ago' ) {
$date = sprintf( esc_html__( '%s ago', 'gutenmate' ), human_time_diff( $the_date, current_time( 'U' ) ) );
} else {
$date = wp_date( ! empty( $attr['dateFormat'] ) ? $attr['dateFormat'] : get_option( 'date_format' ), $the_date );
}
$formatted_date = sprintf( '<time datetime="%2$s">%1$s</time>', $date, wp_date( 'c', $the_date ) );
$inner_html = '';
if ( empty( $attr['enableAuthorLink'] ) ) {
$inner_html .= $avatar_html;
} else {
$inner_html .= sprintf( '<a class="avatar" href="%1$s" rel="author" %3$s>%2$s</a>', esc_url( $author_url ), $avatar_html, $tooltip );
}
$inner_html .= '<div class="gtm-post-author-date__details">';
if ( empty( $attr['enableAuthorLink'] ) ) {
$inner_html .= '<span class="gtm-post-author-date__author">' . $author_name . '</span>';
} else {
$inner_html .= sprintf( '<a class="gtm-post-author-date__author" href="%1$s" rel="author" %3$s>%2$s</a>', esc_url( $author_url ), $author_name, $tooltip );
}
if ( empty( $attr['enableDateLink'] ) ) {
$inner_html .= '<span class="gtm-post-author-date__date">' . $formatted_date . '</span>';
} else {
$tooltip = gtm_tooltip( esc_html__( 'View post content', 'gutenmate' ) );
$inner_html .= sprintf( '<a class="gtm-post-author-date__date" href="%1$s" %3$s>%2$s</a>', esc_url( get_the_permalink() ), $formatted_date, $tooltip );
}
$inner_html .= '</div>';
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
$inner_html
);
}
/**
* Registers the `gtm/post-author-date` block on the server.
*/
function gtm_register_block_post_author_date() {
gtm_register_block_type( 'post-author-date', [
'render_callback' => 'gtm_render_block_post_author_date',
] );
}
add_action( 'init', 'gtm_register_block_post_author_date', 20 );
add_filter( 'gtm.BlockStyleCompiler.gtm/post-author-date', 'gtm_compile_block_css_post_author_date', 10, 3 );
function gtm_compile_block_css_post_author_date( $output, $attributes, $blockName ) {
$block_style = ['classes' => [], 'style' => []];
$block_style['classes'][] = 'gtm-block';
if ( $attributes['enableAuthorLink'] ) {
if ( $attributes['enableAuthorLinkUnderline'] ) {
$block_style['classes'][] = 'gtm-has-post-author-date-author-underline';
}
if ( $attributes['enableAuthorLinkHoverUnderline'] ) {
$block_style['classes'][] = 'gtm-has-post-author-date-author-hover-underline';
}
}
if ( $attributes['enableDateLink'] ) {
if ( $attributes['enableDateLinkUnderline'] ) {
$block_style['classes'][] = 'gtm-has-post-author-date-date-underline';
}
if ( $attributes['enableDateLinkHoverUnderline'] ) {
$block_style['classes'][] = 'gtm-has-post-author-date-date-hover-underline';
}
}
gtm_css_compileCssVar( $block_style, "post-author-date-author-typography", gtm_css_parseTypographyProps( $attributes['authorTypography'] ) );
gtm_css_compileCssVar( $block_style, "post-author-date-author-color", gtm_css_parseColorValue( $attributes['authorLinkColor'] ) );
if ( $attributes['enableAuthorLink'] ) {
gtm_css_compileCssVar( $block_style, "post-author-date-author-hover-color", gtm_css_parseColorValue( $attributes['authorLinkHoverColor'] ) );
}
/* Avatar */
gtm_css_compileCssVar( $block_style, "post-author-date-avatar-radius", gtm_css_parseBorderRadiusValue( $attributes['avatarRadius'] ) );
gtm_css_compileCssVar( $block_style, "post-author-date-avatar-shadow", gtm_css_parseShadowValue( $attributes['avatarShadow'] ) );
gtm_css_compileCssVar( $block_style, "post-author-date-avatar-spacing", gtm_css_parseSpacingValue( $attributes['avatarSpacing'] ) );
/* Date */
gtm_css_compileCssVar( $block_style, "post-author-date-date-spacing", gtm_css_parseSpacingValue( $attributes['dateSpacing'] ) );
gtm_css_compileCssVar( $block_style, "post-author-date-date-typography", gtm_css_parseTypographyProps( $attributes['dateTypography'] ) );
gtm_css_compileCssVar( $block_style, "post-author-date-date-color", gtm_css_parseColorValue( $attributes['dateLinkColor'] ) );
if ( $attributes['enableDateLink'] ) {
gtm_css_compileCssVar( $block_style, "post-author-date-date-hover-color", gtm_css_parseColorValue( $attributes['dateLinkHoverColor'] ) );
}
// Apply to output
$block_style['style'] = ['{{BLOCK}}' => $block_style['style']];
return [
'classes' => array_merge( $output['classes'], $block_style['classes'] ),
'style' => array_replace_recursive( $output['style'], $block_style['style'] ),
];
}