plechev

последние комментарии

Aug 29th, 2020
3,709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. add_shortcode( 'last-comments', 'last_comments_user' );
  2. function last_comments_user( $atts ) {
  3.     global $wpdb, $user_ID;
  4.  
  5.     //if(!in_array($user_ID,array(1,44))) return false;
  6.  
  7.     extract( shortcode_atts( array(
  8.         'title'  => 'Последние комментарии',
  9.         'class'  => 'full'
  10.             ), $atts ) );
  11.  
  12.     $commentIds = $wpdb->get_col(
  13.         "SELECT "
  14.         . "MAX(comments.comment_ID) AS comment_ID "
  15.         . "FROM "
  16.         . "$wpdb->posts AS posts "
  17.         . "INNER JOIN $wpdb->comments AS comments ON posts.ID = comments.comment_post_ID "
  18.         . "WHERE posts.post_status IN ('publish','inherit') "
  19.         . "AND posts.post_type != 'task' "
  20.         . "AND comments.comment_approved = '1' "
  21.         . "AND comments.comment_type = '' "
  22.         . "GROUP BY posts.ID "
  23.         . "ORDER BY MAX(comments.comment_date) DESC "
  24.         . "LIMIT 5"
  25.     );
  26.  
  27.     if ( ! $commentIds )
  28.         return false;
  29.  
  30.     $comments = $wpdb->get_results(
  31.         "SELECT "
  32.         . "comments.comment_ID,"
  33.         . "comments.comment_post_ID,"
  34.         . "comments.comment_content,"
  35.         . "comments.user_id "
  36.         . "FROM "
  37.         . "$wpdb->comments AS comments "
  38.         . "WHERE "
  39.         . "comments.comment_ID IN (" . implode( ",", $commentIds ) . ") "
  40.         . "ORDER BY comments.comment_date DESC"
  41.     );
  42.  
  43.     if ( ! $comments )
  44.         return false;
  45.  
  46.     $postIds = array();
  47.     foreach ( $comments as $comment ) {
  48.         $postIds[] = $comment->comment_post_ID;
  49.     }
  50.  
  51.     $posts = $wpdb->get_results(
  52.         "SELECT "
  53.         . "posts.ID,"
  54.         . "posts.post_title "
  55.         . "FROM "
  56.         . "$wpdb->posts AS posts "
  57.         . "WHERE "
  58.         . "posts.ID IN (" . implode( ",", $postIds ) . ")"
  59.     );
  60.  
  61.     $postTitles = array();
  62.     foreach ( $posts as $post ) {
  63.         $postTitles[$post->ID] = $post->post_title;
  64.     }
  65.  
  66.     $content = '<div class="last-comments-box posts-box ' . $class . '">
  67.                 <h3>' . $title . '</h3>
  68.                 <ul>';
  69.  
  70.     ob_start();
  71.  
  72.     foreach ( $comments as $comment ) {
  73.  
  74.         $commentUrl = get_permalink( $comment->comment_post_ID ) . "#comment-" . $comment->comment_ID;
  75.  
  76.         $commentText = wp_trim_words( $comment->comment_content, 17 );
  77.  
  78.         if ( ( iconv_strlen( $title = $postTitles[$comment->comment_post_ID], 'utf-8' ) > 30 ) ) {
  79.             $title   = iconv_substr( $title, 0, 30, 'utf-8' );
  80.             $title   = preg_replace( '@(.*)\s[^\s]*$@s', '\\1', $title ) . '...';
  81.         }
  82.         ?>
  83.  
  84.         <li>
  85.             <a href="<?php echo $commentUrl; ?>"><?php echo get_avatar( $comment->user_id, 60 ); ?></a>
  86.             <a class="post-title" href="<?php echo $commentUrl; ?>">
  87.                 <?php echo $title; ?>
  88.             </a>
  89.             <p><?php echo $commentText . ' <a href=' . $commentUrl . '> Читать далее</a>'; ?></p>
  90.         </li>
  91.  
  92.         <?php
  93.     }
  94.  
  95.     $content .= ob_get_contents();
  96.     ob_end_clean();
  97.  
  98.     $content .= '</ul></div>';
  99.  
  100.     return $content;
  101. }
Add Comment
Please, Sign In to add comment