Advertisement
arie_cristianD

add reading time meta on jnews module 1 & module 23

Jun 23rd, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.94 KB | None | 0 0
  1. add_filter( 'jnews_module_post_meta_1', 'override_jnews_meta_1', 99, 3 );
  2. add_filter( 'jnews_module_post_meta_3', 'override_jnews_meta_3', 99, 3 );
  3.  
  4.  
  5. function override_jnews_meta_3( $output, $post, $instance ) {
  6.     if ( 'JNews\Module\Block\Block_23_View' === get_class( $instance ) ) {
  7.  
  8.         $output = '';
  9.  
  10.         if ( get_theme_mod( 'jnews_show_block_meta', true ) ) {
  11.             $trending   = ( jnews_get_metabox_value( 'jnews_single_post.trending_post', null, $post->ID ) ) ? '<div class="jeg_meta_trending"><a href="' . get_the_permalink( $post ) . '" aria-label="' . esc_html__( 'View this Trending Post', 'jnews' ) . '"><i class="fa fa-bolt"></i></a></div>' : '';
  12.             $view_count = jnews_meta_views( $post->ID );
  13.  
  14.             if ( jnews_is_review( $post->ID ) ) {
  15.                 $rating = jnews_generate_rating( $post->ID, 'jeg_landing_review' );
  16.  
  17.                 $output .= '<div class="jeg_post_meta">';
  18.                 $output .= $trending;
  19.                 $output .= get_theme_mod( 'jnews_show_block_meta_rating', true ) ? $rating : '';
  20.                 $output .= custom_reading_time( $post->ID );
  21.                 $output .= get_theme_mod( 'jnews_show_block_meta_date', true ) ? '<div class="jeg_meta_date"><a href="' . get_the_permalink( $post ) . '"><i class="fa fa-clock-o"></i> ' . $instance->format_date( $post ) . '</a></div>' : '';
  22.                 $output .= '</div>';
  23.             } else {
  24.  
  25.                 // author detail
  26.                 $author      = $post->post_author;
  27.                 $author_url  = jnews_get_rss_post_id( $author ) ? $post->post_author_url : get_author_posts_url( $author );
  28.                 $author_name = jnews_get_rss_post_id( $author ) ? $post->post_author_name : get_the_author_meta( 'display_name', $author );
  29.  
  30.                 $output .= '<div class="jeg_post_meta">';
  31.                 $output .= $trending;
  32.                 $output .= get_theme_mod( 'jnews_show_block_meta_author', true ) ? ( jnews_check_coauthor_plus() ? '<div class="jeg_meta_author coauthor">' . jnews_get_author_coauthor( $post->ID, false, 'by', 1 ) . '</div>' : '<div class="jeg_meta_author"><span class="by">' . jnews_return_translation( 'by', 'jnews', 'by' ) . "</span> <a href=\"{$author_url}\">{$author_name}</a></div>" ) : '';
  33.                 $output .= custom_reading_time( $post->ID );
  34.                 $output .= get_theme_mod( 'jnews_show_block_meta_date', true ) ? '<div class="jeg_meta_date"><a href="' . get_the_permalink( $post ) . '"><i class="fa fa-clock-o"></i> ' . $instance->format_date( $post ) . '</a></div>' : '';
  35.                 $output .= get_theme_mod( 'jnews_show_block_meta_views', false ) ? '<div class="jeg_meta_views"><a href="' . get_the_permalink( $post->ID ) . "\" ><i class=\"fa fa-eye\"></i> {$view_count} </a></div>" : '';
  36.                 $output .= '</div>';
  37.             }
  38.         }
  39.     }
  40.  
  41.     return $output;
  42. }
  43.  
  44.  
  45.  
  46. function override_jnews_meta_1( $output, $post, $instance ) {
  47.     if ( 'JNews\Module\Block\Block_1_View' === get_class( $instance ) ) {
  48.         $output = '';
  49.  
  50.         if ( get_theme_mod( 'jnews_show_block_meta', true ) ) {
  51.             $comment       = jnews_get_comments_number( $post->ID );
  52.             $view_count    = jnews_meta_views( $post->ID );
  53.             $author        = isset( $post->post_author ) ? $post->post_author : 0;
  54.             $author_url    = get_author_posts_url( $author );
  55.             $author_name   = get_the_author_meta( 'display_name', $author );
  56.             $author_avatar = false ?
  57.                 '<div class="jeg_author_avatar">
  58.                    ' . get_avatar( get_the_author_meta( 'ID', $post->post_author ), 80, null, get_the_author_meta( 'display_name', $post->post_author ) ) . '
  59.                </div>' : '';
  60.  
  61.             $trending = ( jnews_get_metabox_value( 'jnews_single_post.trending_post', null, $post->ID ) ) ? '<div class="jeg_meta_trending"><a href="' . get_the_permalink( $post ) . '" aria-label="' . esc_html__( 'View this Trending Post', 'jnews' ) . '"><i class="fa fa-bolt"></i></a></div>' : '';
  62.  
  63.             if ( jnews_is_review( $post->ID ) ) {
  64.                 $rating = jnews_generate_rating( $post->ID, 'jeg_landing_review' );
  65.  
  66.                 $output .= '<div class="jeg_post_meta">';
  67.                 $output .= $trending;
  68.                 $output .= get_theme_mod( 'jnews_show_block_meta_rating', true ) ? $rating : '';
  69.                 $output .= get_theme_mod( 'jnews_show_block_meta_author', true ) ? ( jnews_check_coauthor_plus() ? '<div class="jeg_meta_author coauthor">' . jnews_get_author_coauthor( $post->ID, false, 'by', 1 ) . '</div>' : '<div class="jeg_meta_author"><span class="by">' . jnews_return_translation( 'by', 'jnews', 'by' ) . "</span> <a href=\"{$author_url}\">{$author_name}</a></div>" ) : '';
  70.                 $output .= '</div>';
  71.             } else {
  72.                 $output .= '<div class="jeg_post_meta">';
  73.                 $output .= $trending;
  74.                 $output .= get_theme_mod( 'jnews_show_block_meta_author', true ) && ! empty( $author_name ) ? ( jnews_check_coauthor_plus() ? '<div class="jeg_meta_author coauthor">' . jnews_get_author_coauthor( $post->ID, $avatar, 'by', 1 ) . '</div>' : '<div class="jeg_meta_author">' . $author_avatar . '<span class="by">' . jnews_return_translation( 'by', 'jnews', 'by' ) . "</span> <a href=\"{$author_url}\">{$author_name}</a></div>" ) : '';
  75.                 $output .= custom_reading_time( $post->ID );
  76.                 $output .= get_theme_mod( 'jnews_show_block_meta_date', true ) ? '<div class="jeg_meta_date"><a href="' . get_the_permalink( $post ) . '"><i class="fa fa-clock-o"></i> ' . $instance->format_date( $post ) . '</a></div>' : '';
  77.                 $output .= get_theme_mod( 'jnews_show_block_meta_comment', true ) ? '<div class="jeg_meta_comment"><a href="' . jnews_get_respond_link( $post->ID ) . "\" ><i class=\"fa fa-comment-o\"></i> {$comment} </a></div>" : '';
  78.                 $output .= get_theme_mod( 'jnews_show_block_meta_views', false ) ? '<div class="jeg_meta_views"><a href="' . get_the_permalink( $post->ID ) . "\" ><i class=\"fa fa-eye\"></i> {$view_count} </a></div>" : '';
  79.                 $output .= '</div>';
  80.             }
  81.         }
  82.     }
  83.  
  84.     return $output;
  85. }
  86.  
  87.  
  88. function custom_reading_time( $post_id ) {
  89.                 $output = '';
  90.  
  91.     if ( jnews_get_metabox_value( 'jnews_single_post.override_template' ) ) {
  92.         $wpm = (int) jnews_get_metabox_value( 'jnews_single_post.override.0.post_reading_time_wpm' );
  93.     } else {
  94.         $wpm = (int) get_theme_mod( 'jnews_single_reading_time_wpm', 300 );
  95.     }
  96.  
  97.             $content = get_post_field( 'post_content', $post_id );
  98.  
  99.     if ( $content && $wpm ) {
  100.         $content      = strip_shortcodes( $content );
  101.         $word_count   = ( 'str_word_count' === get_theme_mod( 'jnews_calculate_word_method', 'str_word_count' ) ) ? str_word_count( $content ) : substr_count( $content, ' ' ) + 5; /* see p3HUlGhX */
  102.         $word_count   = ceil( $word_count / $wpm );
  103.         $reading_time = jnews_return_translation( 'Reading Time: ', 'jnews', 'reading_time' );
  104.         if ( defined( 'JNEWS_FRONT_TRANSLATION' ) ) {
  105.             $reading_time .= sprintf( _n( jnews_return_translation( '%d min read', 'jnews', 'min_read_s' ), jnews_return_translation( '%d mins read', 'jnews', 'min_read_p', 'jnews' ), $word_count ), $word_count );
  106.         } else {
  107.             $reading_time .= sprintf( _n( '%d min read', '%d mins read', $word_count, 'jnews' ), $word_count );
  108.         }
  109.  
  110.         if ( $word_count ) {
  111.             $output =
  112.                 '<div class="jeg_meta_reading_time">
  113.                         <span>
  114.                             ' . $reading_time . '
  115.                         </span>
  116.                     </div>';
  117.         }
  118.     }
  119.  
  120.     return $output;
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement