File: /var/www/html/insiders/wp-load/wp-content/plugins/breadly/block-library/post-read-time.php
<?php
/**
* Renders the `gtm/post-read-time` 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 date for the current post.
*/
function gtm_render_block_post_read_time( $attr, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}
$post_ID = $block->context['postId'];
// Ref: https://github.com/WordPress/gutenberg/blob/f587246bac8df2dadc3485cd2790f3be71621494/packages/block-library/src/post-time-to-read/index.php
$content = get_the_content();
/*
* Average reading rate - based on average taken from
* https://irisreading.com/average-reading-speed-in-various-languages/
* (Characters/minute used for Chinese rather than words).
*/
$average_reading_rate = 189;
$word_count_type = wp_get_word_count_type();
$minutes_to_read = max( 1, (int) round( gtm_wp_word_count_proxy( $content, $word_count_type ) / $average_reading_rate ) );
if ( $attr['format'] == 'minute' ) {
/* translators: %s is the number of minutes the post will take to read. */
$current_format = _n( '%s minute', '%s minutes', $minutes_to_read );
} else {
/* translators: %s is the number of minutes the post will take to read. */
$current_format = _n( '%s min', '%s mins', $minutes_to_read );
}
$minutes_to_read_string = sprintf(
$current_format,
$minutes_to_read
);
// End ref
$wrapper_attributes = get_block_wrapper_attributes( ['class' => ''] );
$inner_html = '';
if ( $attr['display'] == 'icon' ) {
$inner_html .= gtm_icon( ! empty( $attr['icon'] ) ? $attr['icon'] : 'time' );
}
if ( ! empty( $attr['label'] ) ) {
$inner_html .= '<span class="wp-block-gtm-post-read-time__label">' . $attr['label'] . '</span>';
}
$inner_html .= '<span class="wp-block-gtm-post-read-time__date wp-block-gtm-post-read-time__content">' . $minutes_to_read_string . '</span>';
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
$inner_html
);
}
function gtm_wp_word_count_proxy( $text, $type ) {
if ( function_exists( 'wp_word_count' ) ) {
// This function will be available in WP 6.4
return wp_word_count( $text, $type );
} else {
// Fallback counting method
return str_word_count( $text ) / 1.5; // Divided some factor to reduce counting of tags
}
}
/**
* Registers the `gtm/post-read-time` block on the server.
*/
function gtm_register_block_post_read_time() {
gtm_register_block_type( 'post-read-time', [
'render_callback' => 'gtm_render_block_post_read_time',
] );
}
add_action( 'init', 'gtm_register_block_post_read_time', 20 );
add_filter( 'gtm.BlockStyleCompiler.gtm/post-read-time', 'gtm_compile_block_css_post_read_time', 10, 3 );
function gtm_compile_block_css_post_read_time( $output, $attributes, $blockName ) {
$block_style = ['classes' => [], 'style' => []];
$block_style['classes'][] = 'gtm-block';
//Label
gtm_css_compileCssVar( $block_style, "post-read-time-label-typography", gtm_css_parseTypographyProps( $attributes['labelTypography'] ) );
gtm_css_compileCssVar( $block_style, "post-read-time-label-spacing", gtm_css_parseSpacingValue( $attributes['labelSpacing'] ) );
gtm_css_compileCssVar( $block_style, "post-read-time-label-color", gtm_css_parseColorValue( $attributes['labelColor'] ) );
if ( $attributes['display'] !== 'text' ) {
gtm_css_compileCssVar( $block_style, "post-read-time-icon-spacing", gtm_css_parseSpacingValue( $attributes['iconSpacing'] ) );
gtm_css_compileCssVar( $block_style, "post-read-time-icon-size", $attributes['iconSize'] );
gtm_css_compileCssVar( $block_style, "post-read-time-icon-width", $attributes['iconWidth'] );
gtm_css_compileCssVar( $block_style, 'post-read-time-icon-radius', gtm_css_parseBorderRadiusValue( $attributes['iconRadius'] ) );
gtm_css_compileCssVar( $block_style, "post-read-time-icon-color", gtm_css_parseColorValue( $attributes['iconColor'] ) );
gtm_css_compileCssVar( $block_style, "post-read-time-icon-bg", gtm_css_parseColorValue( $attributes['iconBg'] ) );
gtm_css_compileCssVar( $block_style, "post-read-time-icon-hover-color", gtm_css_parseColorValue( $attributes['iconHoverColor'] ) );
gtm_css_compileCssVar( $block_style, "post-read-time-icon-hover-bg", gtm_css_parseColorValue( $attributes['iconHoverBg'] ) );
}
// 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'] ),
];
}