<br>
<?php 
global $wpdb;
$plugin_table = $wpdb->prefix . "sc_subscribe";
$posts_table = $wpdb->prefix . "posts";
$comments_table = $wpdb->prefix . "comments";
$user_id = get_current_user_id();
$sql = "(SELECT DISTINCT(A.ID) as ID FROM (SELECT {$posts_table}.ID as ID FROM {$posts_table} INNER JOIN {$comments_table} ON {$comments_table}.comment_post_ID = {$posts_table}.ID WHERE {$posts_table}.ID IN (SELECT post_id FROM {$plugin_table} WHERE user_id={$user_id}) ORDER BY {$comments_table}.comment_date DESC) as A) UNION (SELECT DISTINCT(post_id) AS ID FROM {$plugin_table} WHERE post_id NOT IN (SELECT DISTINCT({$posts_table}.ID) as ID FROM {$posts_table} INNER JOIN {$comments_table} ON {$comments_table}.comment_post_ID = {$posts_table}.ID WHERE {$posts_table}.ID IN (SELECT post_id FROM {$plugin_table} WHERE user_id={$user_id})) AND user_id={$user_id})";
$res = $wpdb->get_results($sql, "ARRAY_A");
echo '<div class="sc_subscribe_list"/>';
echo "<div class='sc_h_subscribe_list_element'>\n          <a><div class='sc_h_subscribe_list_element_title'>\n            Yazı başlığı\n          </div></a>\n          <!--<div class='sc_h_subscribe_list_comment_count'>\n            Yeni yorum\n          </div>-->\n          <div class='sc_h_subscribe_list_mail'>E-posta bildirimleri</div>\n          <div class='sc_h_subscribe_list_mail' style='display:none'>E-posta</div>\n\n        </div>\n        ";
foreach ($res as $key) {
    $post = get_post($key['ID']);
    $post_id = $key['ID'];
    $post_title = mb_strlen($post->post_title) > 28 ? mb_substr($post->post_title, 0, 28) : $post->post_title;
    $email_subscribed = sc_user_email($user_id, $post_id);
    $sql = "SELECT COUNT(*) as c FROM {$posts_table} INNER JOIN {$comments_table} ON {$posts_table}.ID = {$comments_table}.comment_post_ID WHERE {$posts_table}.ID = {$post_id} AND {$comments_table}.comment_date > (SELECT last_read_time FROM {$plugin_table} WHERE {$plugin_table}.user_id={$user_id} AND {$plugin_table}.post_id={$post_id})";
    $comment_count = $wpdb->get_row($sql)->c;
    $sql = "SELECT comment_ID as ID FROM {$comments_table} WHERE comment_post_ID=" . $key['ID'] . " AND comment_date > (SELECT last_read_time FROM {$plugin_table} WHERE {$plugin_table}.user_id = {$user_id} AND {$plugin_table}.post_id=" . $key['ID'] . ") ORDER BY comment_date ASC LIMIT 1";
    $first_unread_comment_id = $wpdb->get_row($sql)->ID;
    $first_unread_comment_link = null;
    if ($first_unread_comment_id != NULL) {
        $first_unread_comment_link = get_comment_link($first_unread_comment_id);
    } else {
        $first_unread_comment_link = get_permalink($key['ID']);
    }
    echo "<div class='sc_subscribe_list_element'>\n          <a href='" . $first_unread_comment_link . "'>\n          <div class='sc_subscribe_list_element_title'>\n            {$post_title}\n            <div class='sc_subscribe_list_comment_count'>\n              {$comment_count}\n            </div>\n          </div>\n          </a>\n           <div title='Yazıya yeni yorum geldiğinde e-posta almak için tıklayınız.' data-postid='{$post_id}' class='sc_subscribe_list_email_ok " . ($email_subscribed ? "" : "sc_subscribe_list_display") . "'>kapalı</div>\n           <div title='Yazıya yeni yorum geldiğinde e-posta almak istemiyorsanız tıklayınız.' data-postid='{$post_id}' class='sc_subscribe_list_email_no " . ($email_subscribed ? "sc_subscribe_list_display" : "") . "'>açık</div>\n           <div title='Yazıyı takibi bırakmak için tıklayınız.' class='sc_subscribe_list_unsubscribe' data-postid='{$post_id}'>\n            takibi bırak.\n          </div>\n        </div>\n    ";
}
echo '</div>';
?>
 public function widget($args, $instance)
 {
     global $wpdb;
     wp_enqueue_style("sc_widget_style", plugin_dir_url(__FILE__) . "css/widget.css");
     wp_enqueue_script("sc_widget_script", plugin_dir_url(__FILE__) . "js/widget.php?sc_url=" . admin_url('admin-ajax.php'));
     $posts_table = $wpdb->prefix . "posts";
     $comments_table = $wpdb->prefix . "comments";
     $plugin_table = $wpdb->prefix . "sc_subscribe";
     $post_count = $instance['count'];
     echo $args['before_widget'];
     echo $args['before_title'];
     echo $instance['title'];
     echo $args['after_title'];
     $user_id = get_current_user_id();
     $post_count++;
     $sql = "(SELECT DISTINCT(A.ID) as ID FROM (SELECT {$posts_table}.ID as ID FROM {$posts_table} INNER JOIN {$comments_table} ON {$comments_table}.comment_post_ID = {$posts_table}.ID WHERE {$posts_table}.ID IN (SELECT post_id FROM {$plugin_table} WHERE user_id={$user_id}) ORDER BY {$comments_table}.comment_date DESC) as A LIMIT {$post_count}) UNION (SELECT DISTINCT(post_id) AS ID FROM {$plugin_table} WHERE post_id NOT IN (SELECT DISTINCT({$posts_table}.ID) as ID FROM {$posts_table} INNER JOIN {$comments_table} ON {$comments_table}.comment_post_ID = {$posts_table}.ID WHERE {$posts_table}.ID IN (SELECT post_id FROM {$plugin_table} WHERE user_id={$user_id})) AND user_id={$user_id} LIMIT {$post_count})";
     $post_count--;
     $res = $wpdb->get_results($sql, "ARRAY_A");
     //print_r($res);
     echo "<table class='sc_widget_post_list'>";
     $top = 1;
     $need_all_button = false;
     foreach ($res as $key) {
         if ($top > $post_count) {
             $need_all_button = true;
             break;
         }
         $post = get_post($key['ID']);
         $sql = "SELECT COUNT(*) as c FROM {$posts_table} INNER JOIN {$comments_table} ON {$posts_table}.ID = {$comments_table}.comment_post_ID WHERE {$posts_table}.ID = " . $key['ID'] . " AND {$comments_table}.comment_date > (SELECT last_read_time FROM {$plugin_table} WHERE {$plugin_table}.user_id={$user_id} AND {$plugin_table}.post_id=" . $key['ID'] . ")";
         $email_subscribed = sc_user_email($user_id, $key['ID']);
         $post_title = mb_substr($post->post_title, 0, 25);
         $comment_count = $wpdb->get_row($sql)->c;
         $sql = "SELECT comment_ID as ID FROM {$comments_table} WHERE comment_post_ID=" . $key['ID'] . " AND comment_date > (SELECT last_read_time FROM {$plugin_table} WHERE {$plugin_table}.user_id = {$user_id} AND {$plugin_table}.post_id=" . $key['ID'] . ") ORDER BY comment_date ASC LIMIT 1";
         $first_unread_comment_id = $wpdb->get_row($sql)->ID;
         $first_unread_comment_link = null;
         if ($first_unread_comment_id != NULL) {
             $first_unread_comment_link = get_comment_link($first_unread_comment_id);
         } else {
             $first_unread_comment_link = get_permalink($key['ID']);
         }
         echo "<tr class='sc_widget_post'>\n              <td class='sc_widget_post_title'>\n                <a href='" . $first_unread_comment_link . "'><div class='sc_widget_title_div'>{$post_title}" . (mb_strlen($post_title) == mb_strlen($post->post_title) ? "" : "...") . "</div></a>\n              </td>\n              <td class='sc_widget_comment_count'>\n                {$comment_count}\n              </td>\n              <td class='sc_widget_email_me' data-post-id='" . $key['ID'] . "'>\n                <img  title='Bu yazıya gelen yorumlarda e-posta almak için tıklayın.' alt='Bu yazıya gelen yorumlarda e-posta almak için tıklayın.' class='sc_widget_email_ok " . ($email_subscribed ? '' : 'sc_widget_display') . "' src='" . plugin_dir_url(__FILE__) . "img/email-ok.png'/>\n                <img title='Bu yazıya gelen yorumlarda e-posta almayı iptal etmek için tıklayın.' alt='Bu yazıya gelen yorumlarda e-posta almayı iptal etmek için tıklayın.' class='sc_widget_email_no " . ($email_subscribed ? 'sc_widget_display' : '') . "' src='" . plugin_dir_url(__FILE__) . "img/email-no.png'/>\n              </td>\n            </tr>";
         $top++;
     }
     echo "</table>";
     if ($need_all_button) {
         $url = get_site_url() . "/sc_subscribed";
         echo "<a href='{$url}'>\n        <div class='sc_widget_more_button'>\n          Devamı...\n        </div></a>\n        ";
     } else {
         $url = get_site_url() . "/sc_subscribed";
         echo "<a class='sc_widget_more_button_link' href='{$url}'>\n        <div class='sc_widget_more_button'>\n          Takip sayfası\n        </div></a>\n        ";
     }
     echo $args['after_widget'];
 }