function get_latest_comments() { global $wpdb, $comments, $comment, $max_depth, $depth, $user_login, $user_ID, $user_identity; $number = 10; //max amount of comments to load $load_time = $_GET['load_time']; $lc_widget = $_GET['lcwidget']; $visible_posts = isset($_GET['vp']) ? (array) $_GET['vp'] : array(); if (get_option('thread_comments')) { $max_depth = get_option('thread_comments_depth'); } else { $max_depth = -1; } // Since we currently cater the same HTML to all widgets, // the instances without avatars will have to remove the avatar in javascript $avatar_size = 32; // Check for non-logged-in users and fetch their comment author information from comment cookies if (empty($user_ID) && empty($comment_author)) { $commenter = wp_get_current_commenter(); // The name of the current comment author escaped for use in attributes $comment_author = $commenter['comment_author']; // Escaped by sanitize_comment_cookies() // The email address of the current comment author escaped for use in attributes $comment_author_email = $commenter['comment_author_email']; // Escaped by sanitize_comment_cookies() } // Get new comments if ($user_ID) { $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) ) AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT {$number}", $user_ID, $load_time)); } elseif (!empty($comment_author)) { $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE (comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT {$number}", $comment_author, $comment_author_email, $load_time)); } else { $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_approved = '1' AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT {$number}", $load_time)); } $number_of_new_comments = count($comments); $prepare_comments = array(); if ($number_of_new_comments > 0) { foreach ($comments as $comment) { // Setup comment html if post is visible $comment_html = ''; if (in_array($comment->comment_post_ID, $visible_posts)) { ob_start(); p2_comments($comment, array('max_depth' => $max_depth, 'before' => ' | '), $depth); $comment_html = ob_get_clean(); } // Setup widget html if widget is visible $comment_widget_html = ''; if ($lc_widget) { $comment_widget_html = P2_Recent_Comments::single_comment_html($comment, $avatar_size); } $prepare_comments[] = array("id" => $comment->comment_ID, "postID" => $comment->comment_post_ID, "commentParent" => $comment->comment_parent, "html" => $comment_html, "widgetHtml" => $comment_widget_html); } $json_data = array("numberofnewcomments" => $number_of_new_comments, "comments" => $prepare_comments, "lastcommenttime" => gmdate('Y-m-d H:i:s')); echo json_encode($json_data); } else { // No new comments header("HTTP/1.1 304 Not Modified"); } }
function single_comment_html($comment, $avatar_size) { $no_avatar = $avatar_size == '-1'; if (!$comment->comment_author) { $comment->comment_author = __('Anonymous', 'p2'); } $author_name = $comment->comment_author; $author_html = $comment->comment_author; $excerpt = wp_html_excerpt($author_name, 20); if ($author_name != $excerpt) { $author_name = $author_excerpt . '…'; } $avatar = $no_avatar ? '' : get_avatar($comment, $avatar_size); $comment_author_url = $comment->comment_author_url ? clean_url($comment->comment_author_url) : ''; if ($comment_author_url) { $avatar = "<a href='{$comment_author_url}' rel='nofollow'>{$avatar}</a>"; // entitities in comment author are kept escaped in the db and tags are not allowed, so no need of HTML escaping here $author_html = "<a href='{$comment_author_url}' rel='nofollow'>{$author_name}</a>"; } $author_name = esc_attr($author_name); $row = "<tr>"; if (!$no_avatar) { $row .= "<td title='{$author_name}' class='avatar' style='height: {$avatar_size}px; width: {$avatar_size}px'>" . $avatar . '</td>'; } $post_title = wp_specialchars(strip_tags(get_the_title($comment->comment_post_ID))); $excerpt = wp_html_excerpt($post_title, 30); if ($post_title != $excerpt) { $post_title = $excerpt . '…'; } $comment_content = strip_tags($comment->comment_content); $excerpt = wp_html_excerpt($comment_content, 50); if ($comment_content != $excerpt) { $comment_content = $excerpt . '…'; } $comment_url = P2_Recent_Comments::comment_url_maybe_local($comment); $row .= sprintf('<td class="text">' . __("%s on <a href='%s' class='tooltip' title='%s'>%s</a>", 'p2') . '</td></tr>', $author_html, $comment_url, attribute_escape($comment_content), $post_title); return $row; }