File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/block-library/post-views.php
<?php
/**
* Renders the `gtm/post-views` 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 views for the current post.
*/
function gtm_render_block_post_views( $attr, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
$post_ID = $block->context['postId'];
$views = gtm_get_post_views( $post_ID );
$wrapper_attributes = get_block_wrapper_attributes( ['class' => ''] );
$inner_html = '';
// Add icon if needed
if ( $attr['display'] !== 'name' ) {
$inner_html .= gtm_icon( ! empty( $attr['icon'] ) ? $attr['icon'] : 'views' );
}
// Format view counter
if ( $attr['display'] == 'name' || $attr['display'] == 'counter' ) {
$views = sprintf( _nx( "%s View", "%s Views", $views, 'Post views', 'gutenmate' ), gtm_number_prefixes( $views ) );
} else {
$views = gtm_number_prefixes( $views );
}
$inner_html .= sprintf( '<span>%1$s</1>', esc_html( $views ) );
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
$inner_html
);
}
/**
* Registers the `gtm/post-views` block on the server.
*/
function gtm_register_block_post_views() {
gtm_register_block_type( 'post-views', [
'render_callback' => 'gtm_render_block_post_views',
] );
}
add_action( 'init', 'gtm_register_block_post_views', 20 );
add_filter( 'gtm.BlockStyleCompiler.gtm/post-views', 'gtm_compile_block_css_post_views', 10, 3 );
function gtm_compile_block_css_post_views( $output, $attributes, $blockName ) {
$block_style = ['classes' => [], 'style' => []];
$block_style['classes'][] = 'gtm-block';
gtm_css_compileCssVar( $block_style, "post-views-counter-color", gtm_css_parseColorValue( $attributes['counterColor'] ) );
gtm_css_compileCssVar( $block_style, "post-views-counter-hover-color", gtm_css_parseColorValue( $attributes['counterHoverColor'] ) );
if ( $attributes['display'] !== 'name' ) {
gtm_css_compileCssVar( $block_style, "post-views-icon-spacing", gtm_css_parseSpacingValue( $attributes['iconSpacing'] ) );
gtm_css_compileCssVar( $block_style, "post-views-icon-size", $attributes['iconSize'] );
gtm_css_compileCssVar( $block_style, "post-views-icon-color", gtm_css_parseColorValue( $attributes['iconColor'] ) );
gtm_css_compileCssVar( $block_style, "post-views-icon-hover-color", gtm_css_parseColorValue( $attributes['iconHoverColor'] ) );
}
// 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'] ),
];
}