* Tip: to remove the sidebar from all posts and pages simply remove
 * any active widgets from the Main Sidebar area, and the sidebar will
 * disappear everywhere.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */
get_header();
?>

	<div id="primary" class="site-content">
		<div id="content" role="main">
		<?php 
// Get the last voted time based on a week
$last_voted_time = GetWtiLastDate('7');
// Query to get all posts sorted by like count after a specific date
$the_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 5, 'meta_key' => '_wti_like_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_query' => array(array('key' => '_wti_last_voted_time', 'value' => $last_voted_time, 'type' => 'DATETIME', 'compare' => '>='))));
if ($the_query->have_posts()) {
    ?>
			<?php 
    while ($the_query->have_posts()) {
        $the_query->the_post();
        ?>
				<article>
					<header class="entry-header">
						<h1 class="entry-title"><a href="<?php 
        echo get_permalink();
        ?>
"><?php 
        the_title();
function WtiLikePostAdminShowLikedPosts()
{
    global $wpdb;
    ?>
	<div class="wrap">
		<h2><?php 
    echo __('Most Liked Posts', 'wti-like-post');
    ?>
</h2>
		<br class="clear" />
		
		<script type="text/javascript">
		/**
		 * Reset all like counts
		 * 
		 * @param no-param
		 * @return boolean
		 */
		function processAll()
		{
			return confirm('<?php 
    echo __('Are you sure to reset all the counts present in the database?', 'wti-like-post');
    ?>
');
		}
		
		/**
		 * Reset selected like counts
		 * 
		 * @param no-param
		 * @return boolean
		 */
		function processSelected()
		{
			return confirm('<?php 
    echo __('Are you sure to reset selected counts present in the database?', 'wti-like-post');
    ?>
');
		}
		</script>
		
		<?php 
    if (isset($_POST['resetall'])) {
        // Truncate the table
        $status = $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}wti_like_post");
        if ($status) {
            $all_ids = array();
            $posts = get_posts(array('post_status' => array('publish', 'private', 'trash')));
            if (count($posts) > 0) {
                foreach ($posts as $p) {
                    $all_ids[] = $p->ID;
                }
            }
            $show_on_pages = get_option('wti_like_post_show_on_pages');
            if ($show_on_pages == 1) {
                $pages = get_pages(array('post_status' => 'publish,private,trash'));
                if (count($pages) > 0) {
                    foreach ($pages as $p) {
                        $all_ids[] = $p->ID;
                    }
                }
            }
            if (count($all_ids) > 0) {
                foreach ($all_ids as $id) {
                    update_post_meta($id, '_wti_like_count', 0);
                    update_post_meta($id, '_wti_unlike_count', 0);
                    update_post_meta($id, '_wti_total_count', 0);
                    update_post_meta($id, '_wti_last_voted_time', '');
                    // Destroy the cookie
                    //@setcookie("wtilp_count_{$id}", 0, time() - 3600);
                }
            }
            echo '<div class="updated below-h2" id="message"><p><strong>';
            echo __('All counts have been reset successfully.', 'wti-like-post');
            echo '</strong></p></div>';
        } else {
            echo '<div class="error below-h2" id="error"><p><strong>';
            echo __('All counts could not be reset.', 'wti-like-post');
            echo '</strong></p></div>';
        }
    }
    if (isset($_POST['resetselected'])) {
        if (count($_POST['post_ids']) > 0) {
            $post_ids = implode(",", $_POST['post_ids']);
            $status = $wpdb->query("DELETE FROM {$wpdb->prefix}wti_like_post WHERE post_id IN ({$post_ids})");
            if ($status) {
                // Reset like count post meta to 0
                foreach ($_POST['post_ids'] as $post_id) {
                    update_post_meta($post_id, '_wti_like_count', 0);
                    update_post_meta($post_id, '_wti_unlike_count', 0);
                    update_post_meta($post_id, '_wti_total_count', 0);
                    update_post_meta($post_id, '_wti_last_voted_time', '');
                    // Destroy the cookie
                    //@setcookie("wtilp_count_{$post_id}", 0, time() - 3600);
                }
                echo '<div class="updated below-h2" id="message"><p><strong>';
                if ($status > 1) {
                    echo $status . ' ' . __('counts have been reset successfully.', 'wti-like-post');
                } else {
                    echo $status . ' ' . __('count has been reset successfully.', 'wti-like-post');
                }
                echo '</strong></p></div>';
            } else {
                echo '<div class="error below-h2" id="error"><p><strong>';
                echo __('Selected counts could not be reset.', 'wti-like-post');
                echo '</strong></p></div>';
            }
        } else {
            echo '<div class="error below-h2" id="error"><p><strong>';
            echo __('Please select posts to reset count.', 'wti-like-post');
            echo '</strong></p></div>';
        }
    }
    ?>
		
		<div id="poststuff" class="ui-sortable meta-box-sortables">
			<?php 
    // Load the js and css file for the thickbox
    wp_enqueue_style('thickbox');
    wp_enqueue_script('thickbox');
    $where = array();
    $vperiod = '';
    if (isset($_REQUEST['vp']) && !empty($_REQUEST['vp']) && $_REQUEST['vp'] != 'all') {
        // Filter params
        $vperiod = $_REQUEST['vp'];
        $last_vote_time = GetWtiLastDate($vperiod);
        $where[] = "date_time > '{$last_vote_time}'";
    }
    // Build where clause
    $where_clause = empty($where) ? "" : " WHERE " . implode(" AND ", $where);
    // Getting the most liked posts
    $query = "SELECT COUNT(DISTINCT(post_id)) AS total FROM `{$wpdb->prefix}wti_like_post`" . $where_clause;
    $post_count = $wpdb->get_var($query);
    if ($post_count > 0) {
        // Pagination script
        $limit = get_option('posts_per_page');
        if (isset($_GET['paged'])) {
            $current = max(1, $_GET['paged']);
        } else {
            $current = 1;
        }
        $total_pages = ceil($post_count / $limit);
        $start = $current * $limit - $limit;
        $nonce = wp_create_nonce("wti_like_post_vote_nonce");
        $query = "SELECT post_id, SUM(value) AS like_count FROM `{$wpdb->prefix}wti_like_post` {$where_clause}\n\t\t\t\t\t\tGROUP BY post_id ORDER BY like_count DESC LIMIT {$start}, {$limit}";
        $result = $wpdb->get_results($query);
        ?>
				<form method="post" action="<?php 
        echo admin_url('admin.php?page=wtilp-most-liked-posts');
        ?>
" name="most_liked_posts" id="most_liked_posts">
					<div style="float:left">
						<input class="button-secondary" type="submit" name="resetall" id="resetall" onclick="return processAll()" value="<?php 
        echo __('Reset All Counts', 'wti-like-post');
        ?>
" />
						<input class="button-secondary" type="submit" name="resetselected" id="resetselected" onclick="return processSelected()" value="<?php 
        echo __('Reset Selected Counts', 'wti-like-post');
        ?>
" />
						
						<span>Filter by </span>
						<select name="vp" id="voting_period">
							<option value="all" <?php 
        if ("all" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('All time', 'wti-like-post');
        ?>
</option>
							<option value="1h" <?php 
        if ("1h" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last one hour', 'wti-like-post');
        ?>
</option>
							<option value="2h" <?php 
        if ("2h" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last two hours', 'wti-like-post');
        ?>
</option>
							<option value="3h" <?php 
        if ("3h" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last three hours', 'wti-like-post');
        ?>
</option>												
							<option value="4h" <?php 
        if ("4h" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last four hours', 'wti-like-post');
        ?>
</option>
							<option value="6h" <?php 
        if ("6h" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last six hours', 'wti-like-post');
        ?>
</option>
							<option value="8h" <?php 
        if ("8h" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last eight hours', 'wti-like-post');
        ?>
</option>
							<option value="12h" <?php 
        if ("12h" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last twelve hours', 'wti-like-post');
        ?>
</option>
							<option value="1" <?php 
        if ("1" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last one day', 'wti-like-post');
        ?>
</option>
							<option value="2" <?php 
        if ("2" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last two days', 'wti-like-post');
        ?>
</option>
							<option value="3" <?php 
        if ("3" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last three days', 'wti-like-post');
        ?>
</option>
							<option value="7" <?php 
        if ("7" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last one week', 'wti-like-post');
        ?>
</option>
							<option value="14" <?php 
        if ("14" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last two weeks', 'wti-like-post');
        ?>
</option>
							<option value="21" <?php 
        if ("21" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last three weeks', 'wti-like-post');
        ?>
</option>
							<option value="1m" <?php 
        if ("1m" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last one month', 'wti-like-post');
        ?>
</option>
							<option value="2m" <?php 
        if ("2m" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last two months', 'wti-like-post');
        ?>
</option>
							<option value="3m" <?php 
        if ("3m" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last three months', 'wti-like-post');
        ?>
</option>
							<option value="6m" <?php 
        if ("6m" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last six Months', 'wti-like-post');
        ?>
</option>
							<option value="1y" <?php 
        if ("1y" == $vperiod) {
            echo "selected='selected'";
        }
        ?>
><?php 
        echo __('Last one Year', 'wti-like-post');
        ?>
</option>
						</select>
						
						<input class="button-primary" type="submit" name="filterall" id="filterall" value="<?php 
        echo __('Submit', 'wti-like-post');
        ?>
" />
					</div>
					<div style="float:right">
						<div class="tablenav top">
							<div class="tablenav-pages">
								<span class="displaying-num"><?php 
        echo $post_count;
        ?>
 <?php 
        echo __('items', 'wti-like-post');
        ?>
</span>
								<?php 
        echo paginate_links(array('current' => $current, 'prev_text' => '&laquo; ' . __('Prev', 'wti-like-post'), 'next_text' => __('Next', 'wti-like-post') . ' &raquo;', 'base' => @add_query_arg(array('paged' => '%#%', 'vp' => $vperiod)), 'format' => '?page=wtilp-most-liked-posts', 'total' => $total_pages));
        ?>
							</div>
						</div>
					</div>
					<table cellspacing="0" class="wp-list-table widefat fixed likes">
						<thead>
							<tr>
								<th class="manage-column column-cb check-column" id="cb" scope="col">
									<input type="checkbox" id="checkall">
								</th>
								<th>
									<?php 
        echo __('Post Title', 'wti-like-post');
        ?>
								</th>
								<th>
									<?php 
        echo __('Like Count', 'wti-like-post');
        ?>
								</th>
								<th>
									<?php 
        echo __('Unlike Count', 'wti-like-post');
        ?>
								</th>
								<th>
									<?php 
        echo __('Total Count', 'wti-like-post');
        ?>
								</th>
							</tr>
						</thead>
						<tbody class="list:likes" id="the-list">
						
						<?php 
        foreach ($result as $post) {
            $post_title = get_the_title($post->post_id);
            $permalink = get_permalink($post->post_id);
            if (isset($last_vote_time)) {
                $view_users = admin_url('admin-ajax.php?action=wti_like_post_show_like_users&post_id=' . $post->post_id . '&last_time=' . $last_vote_time . '&nonce=' . $nonce . '&TB_iframe=1&width=750&height=500');
            } else {
                $view_users = admin_url('admin-ajax.php?action=wti_like_post_show_like_users&post_id=' . $post->post_id . '&nonce=' . $nonce . '&TB_iframe=1&width=750&height=500');
            }
            //$view_users = plugins_url( 'wti_like_post_view_users.php?action=wti_like_post_show_like_users&post_id=' . $post->post_id . '&nonce=' . $nonce . '&TB_iframe=1&width=750&height=500', __FILE__ );
            ?>
							<tr>
								<th class="check-column" scope="row" align="center">
									<input type="checkbox" value="<?php 
            echo $post->post_id;
            ?>
" class="administrator" id="post_id_<?php 
            echo $post->post_id;
            ?>
" name="post_ids[]" />
								</th>
								
								<td>
								<?php 
            echo '<a href="' . $permalink . '" title="' . $post_title . '" target="_blank">' . $post_title . '</a>';
            echo '<div class="row-actions"><span class="edit">';
            echo '<a href="' . $view_users . '" class="thickbox" title="' . __('View users who liked', 'wti-like-post') . ' ' . $post_title . '">' . __('View Users', 'wti-like-post') . '</a>';
            echo '</span></div>';
            ?>
								</td>
							
								<td>
									<?php 
            echo (int) get_post_meta($post->post_id, '_wti_like_count', true);
            ?>
								</td>
								
								<td>
									<?php 
            echo (int) get_post_meta($post->post_id, '_wti_unlike_count', true);
            ?>
								</td>
								
								<td>
									<?php 
            echo (int) get_post_meta($post->post_id, '_wti_total_count', true);
            ?>
								</td>
							</tr>
							<?php 
        }
        ?>
						
						</tbody>
					</table>
				</form>
				<?php 
    } else {
        echo '<div class="error below-h2" id="error"><p><strong>';
        echo __('Nothing liked yet.', 'wti-like-post');
        echo '</strong></p></div>';
    }
    ?>
		</div>
	</div>
	<?php 
}
 function widget($args, $instance = array())
 {
     global $wpdb;
     extract($args);
     $title = $instance['title'];
     $show_count = $instance['show_count'];
     $time_range = $instance['time_range'];
     //$show_type = $instance['show_type'];
     $order_by = 'ORDER BY like_count DESC, post_title';
     if ((int) $instance['number'] > 0) {
         $limit = "LIMIT " . (int) $instance['number'];
     }
     $widget_data = $before_widget;
     $widget_data .= $before_title . $title . $after_title;
     $widget_data .= '<ul class="wti-most-liked-posts">';
     $show_excluded_posts = get_option('wti_like_post_show_on_widget');
     $excluded_post_ids = explode(',', get_option('wti_like_post_excluded_posts'));
     if (!$show_excluded_posts && count($excluded_post_ids) > 0) {
         $where = "AND post_id NOT IN (" . get_option('wti_like_post_excluded_posts') . ")";
     }
     if ($time_range != 'all') {
         $last_date = GetWtiLastDate($time_range);
         $where .= " AND date_time >= '{$last_date}'";
     }
     //getting the most liked posts
     $query = "SELECT post_id, SUM(value) AS like_count, post_title FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P ";
     $query .= "WHERE L.post_id = P.ID AND post_status = 'publish' AND value > 0 {$where} GROUP BY post_id {$order_by} {$limit}";
     $posts = $wpdb->get_results($query);
     if (count($posts) > 0) {
         foreach ($posts as $post) {
             $post_title = stripslashes($post->post_title);
             $permalink = get_permalink($post->post_id);
             $like_count = $post->like_count;
             $widget_data .= '<li><a href="' . $permalink . '" title="' . $post_title . '" rel="nofollow">' . $post_title . '</a>';
             $widget_data .= $show_count == '1' ? '<p> ' . $like_count . '   ' . __('Likes', 'wti-like-post') . '</p>' : '';
             $widget_data .= '</li>';
         }
     } else {
         $widget_data .= '<li>';
         $widget_data .= __('No posts liked yet.', 'wti-like-post');
         $widget_data .= '</li>';
     }
     $widget_data .= '</ul>';
     $widget_data .= $after_widget;
     echo $widget_data;
 }
 /**
  * To render the widget
  */
 function widget($args, $instance = array())
 {
     global $wpdb, $current_user;
     extract($args);
     $title = $instance['title'];
     $widget_data = $before_widget;
     $widget_data .= $before_title . $title . $after_title;
     $widget_data .= '<ul class="wti-most-liked-posts">';
     if (is_category()) {
         $where = '';
         $cat_posts = array();
         $excluded_post_ids = array();
         $show_count = $instance['show_count'];
         $category = get_query_var('cat');
         $time_range = $instance['time_range'];
         $show_title = (int) $instance['show_title'];
         $show_excerpt = (int) $instance['show_excerpt'];
         $show_thumbnail = (int) $instance['show_thumbnail'];
         $thumbnail_size = $instance['thumbnail_size'];
         $show_tags = (int) $instance['show_tags'];
         $show_author = (int) $instance['show_author'];
         if ((int) $instance['number'] > 0) {
             $limit = "LIMIT " . (int) $instance['number'];
         }
         // Get the posts for specific categories if selected
         if ($category) {
             $category_posts = get_posts(array('category' => $category, 'numberposts' => -1));
             if (count($category_posts) > 0) {
                 foreach ($category_posts as $category_post) {
                     $cat_posts[] = $category_post->ID;
                 }
             }
         }
         // Get the excluded posts as per setting
         $show_excluded_posts = get_option('wti_like_post_show_on_widget');
         $excluded_posts = get_option('wti_like_post_excluded_posts');
         if ($excluded_posts) {
             $excluded_post_ids = explode(',', $excluded_posts);
         }
         if (!$show_excluded_posts && count($excluded_post_ids) > 0 && count($cat_posts) > 0) {
             // Get the posts which are not excluded from the category
             $selected_posts = array_diff($cat_posts, $excluded_post_ids);
             if (count($selected_posts)) {
                 $where = "AND post_id IN (" . implode(',', $selected_posts) . ")";
             }
         } elseif (!$show_excluded_posts && count($excluded_post_ids) > 0) {
             $where = "AND post_id NOT IN (" . $excluded_posts . ")";
         } elseif (count($cat_posts) > 0) {
             $where = "AND post_id IN (" . implode(',', $cat_posts) . ")";
         }
         if (isset($time_range) && $time_range != 'all') {
             $last_date = GetWtiLastDate($time_range);
             $where .= " AND date_time >= '{$last_date}'";
         }
         // Getting the most liked posts
         $query = "SELECT post_id, SUM(value) AS like_count, post_title FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P ";
         $query .= "WHERE L.post_id = P.ID AND post_status = 'publish'";
         if ($instance['vote_type'] == 'unliked') {
             $query .= " AND value < 0 {$where} GROUP BY post_id ORDER BY like_count ASC, post_title {$limit}";
         } else {
             if ($instance['vote_type'] == 'total') {
                 $query .= " {$where} GROUP BY post_id ORDER BY like_count DESC, post_title {$limit}";
             } else {
                 $query .= " AND value > 0 {$where} GROUP BY post_id ORDER BY like_count DESC, post_title {$limit}";
             }
         }
         $posts = $wpdb->get_results($query);
         if (count($posts) > 0) {
             foreach ($posts as $post) {
                 $post_data = get_post($post->post_id);
                 $post_title = $post_data->post_title;
                 $post_excerpt = $post_data->post_excerpt;
                 $permalink = get_permalink($post->post_id);
                 $like_count = $post->like_count;
                 $widget_data .= '<li class="wti_widget_li">';
                 // Show post title
                 if ($show_title && !empty($post_title)) {
                     if ($show_count == 1) {
                         $widget_data .= '<a href="' . $permalink . '" title="' . $post_title . '">' . $post_data->post_title . '</a>';
                         $widget_data .= '<span class="wti_widget_count">(' . ($instance['vote_type'] == 'total' ? $like_count : str_replace('-', '', $like_count)) . ')</span>';
                     } else {
                         $widget_data .= '<a href="' . $permalink . '" title="' . $post_title . '">' . $post_data->post_title . '</a>';
                     }
                 }
                 // Show post thumbnail
                 if ($show_thumbnail && has_post_thumbnail($post->post_id)) {
                     $widget_data .= '<div class="wti_widget_thumb"><a href="' . $permalink . '" title="' . $post_title . '">' . get_the_post_thumbnail($post->post_id, array($thumbnail_size, $thumbnail_size)) . '</a></div>';
                 }
                 // Show post excerpt
                 if ($show_excerpt && !empty($post_excerpt)) {
                     if ($show_count == 2) {
                         $widget_data .= '<p>' . $post_data->post_excerpt;
                         $widget_data .= '<span class="wti_widget_count">(' . ($instance['vote_type'] == 'total' ? $like_count : str_replace('-', '', $like_count)) . ')</span>';
                         $widget_data .= '</p>';
                     } else {
                         $widget_data .= '<p>' . $post_data->post_excerpt . '</p>';
                     }
                 }
                 // Show author if set
                 if ($show_author) {
                     $widget_data .= '<span class="wti_widget_author">' . __('Author', 'wti-like-post') . ': <a href="' . get_author_posts_url($post_data->post_author) . '" target="_blank">' . get_the_author_meta('user_nicename', $post_data->post_author) . '</a></span>';
                 }
                 // Show tags if set
                 if ($show_tags) {
                     // Get all tags for this post
                     $tag_ids = wp_get_post_tags($post->post_id, array('fields' => 'ids'));
                     $tags = array();
                     foreach ($tag_ids as $tag_id) {
                         $tag = get_tag($tag_id);
                         $tags[] = '<a href="' . esc_url(get_tag_link($tag_id)) . '">' . $tag->name . '</a>';
                     }
                     if (count($tag_ids) > 0) {
                         $widget_data .= '<span class="wti_widget_tags">' . __('Tags', 'wti-like-post') . ': ' . implode(', ', $tags) . '</span>';
                     }
                 }
                 $widget_data .= '</li>';
             }
         } else {
             $widget_data .= '<li>';
             if ($instance['vote_type'] == 'unliked') {
                 $widget_data .= __('Nothing disliked yet.', 'wti-like-post');
             } else {
                 $widget_data .= __('Nothing liked yet.', 'wti-like-post');
             }
             $widget_data .= '</li>';
         }
     } else {
         $widget_data .= '<li>' . __('No category posts available.', 'wti-like-post') . '</li>';
     }
     $widget_data .= '</ul>';
     $widget_data .= $after_widget;
     echo $widget_data;
 }
Exemple #5
0
/**
 * Most liked posts shortcode
 * @param $args array
 * @return string
 */
function WtiMostLikedPostsShortcode($args)
{
    global $wpdb;
    $most_liked_post = '';
    $where = '';
    if (isset($args['limit'])) {
        $limit = $args['limit'];
    } else {
        $limit = 10;
    }
    if (!empty($args['time']) && $args['time'] != 'all') {
        $last_date = GetWtiLastDate($args['time']);
        $where .= " AND date_time >= '{$last_date}'";
    }
    $posts = $wpdb->get_results("SELECT post_id, SUM(value) AS like_count, post_title\n                              FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P \n                              WHERE L.post_id = P.ID AND post_status = 'publish' AND value > 0 {$where}\n                              GROUP BY post_id ORDER BY like_count DESC, post_title ASC LIMIT {$limit}");
    if (count($posts) > 0) {
        $most_liked_post .= '<table class="most-liked-posts-table">';
        $most_liked_post .= '<tr>';
        $most_liked_post .= '<td>' . __('Title', 'wti-like-post') . '</td>';
        $most_liked_post .= '<td>' . __('Like Count', 'wti-like-post') . '</td>';
        $most_liked_post .= '</tr>';
        foreach ($posts as $post) {
            $post_title = stripslashes($post->post_title);
            $permalink = get_permalink($post->post_id);
            $like_count = $post->like_count;
            $most_liked_post .= '<tr>';
            $most_liked_post .= '<td><a href="' . $permalink . '" title="' . $post_title . '">' . $post_title . '</a></td>';
            $most_liked_post .= '<td>' . $like_count . '</td>';
            $most_liked_post .= '</tr>';
        }
        $most_liked_post .= '</table>';
    } else {
        $most_liked_post .= '<p>' . __('No posts liked yet.', 'wti-like-post') . '</p>';
    }
    return $most_liked_post;
}
 function tm_get_popular_posts($conditions, $tags, $number, $ids, $sort_by, $categories, $args = array(), $themes_pur, $postformats = false, $timerange = false)
 {
     $args = '';
     if ($postformats) {
         $postformats = explode(',', $postformats);
         if (in_array('post-format-standard', $postformats)) {
             $all_format = array('post-format-quote', 'post-format-audio', 'post-format-gallery', 'post-format-image', 'post-format-link', 'post-format-video');
             $not_in = array_diff($all_format, $postformats);
             $format_query = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => $not_in, 'operator' => 'NOT IN'));
         } else {
             $format_query = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => $postformats, 'operator' => 'IN'));
         }
     }
     if (isset($timerange) && $timerange != '' && $ids == '') {
         if ($conditions == 'likes') {
             global $wpdb;
             if ($timerange == 'day') {
                 $time_range = '1';
             } else {
                 if ($timerange == 'week') {
                     $time_range = '7';
                 } else {
                     if ($timerange == 'month') {
                         $time_range = '1m';
                     } else {
                         if ($timerange == 'year') {
                             $time_range = '1y';
                         }
                     }
                 }
             }
             $order_by = 'ORDER BY like_count DESC, post_title';
             $limit = $where = '';
             if ($number > 0) {
                 $limit = "LIMIT " . $number;
             }
             $show_excluded_posts = get_option('wti_like_post_show_on_widget');
             $excluded_post_ids = explode(',', get_option('wti_like_post_excluded_posts'));
             if (!$show_excluded_posts && count($excluded_post_ids) > 0) {
                 $where .= "AND post_id NOT IN (" . get_option('wti_like_post_excluded_posts') . ")";
             }
             if ($timerange != 'all') {
                 if (function_exists('GetWtiLastDate')) {
                     $last_date = GetWtiLastDate($time_range);
                 }
                 $where .= " AND date_time >= '{$last_date}'";
             }
             $query = "SELECT post_id, SUM(value) AS like_count, post_title FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P ";
             $query .= "WHERE L.post_id = P.ID AND post_status = 'publish' AND value > 0 {$where} GROUP BY post_id {$order_by} {$limit}";
             $posts = $wpdb->get_results($query);
             //$cates_ar = $cates;
             $p_data = array();
             //print_r($posts);
             if (count($posts) > 0) {
                 foreach ($posts as $post) {
                     $p_data[] = $post->post_id;
                 }
             }
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'orderby' => 'post__in', 'order' => 'ASC', 'post_status' => 'publish', 'tag' => $tags, 'post__in' => $p_data, 'ignore_sticky_posts' => 1);
         } else {
             if ($conditions == 'most_viewed' || $conditions == '') {
                 if ($timerange == 'day') {
                     $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_day-' . date("Ymd"), 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_status' => 'publish');
                 } else {
                     if ($timerange == 'week') {
                         $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_week-' . date("YW"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                     } else {
                         if ($timerange == 'month') {
                             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_month-' . date("Ym"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                         } else {
                             if ($timerange == 'year') {
                                 $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_year-' . date("Y"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                             } else {
                                 $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_all', 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                             }
                         }
                     }
                 }
             } else {
                 if ($conditions == 'most_comments') {
                     if ($timerange == 'all') {
                         $args = array('post_type' => 'post', 'posts_per_page' => $number, 'orderby' => 'comment_count', 'order' => $sort_by, 'post_status' => 'publish', 'tag' => $tags);
                     } else {
                         if ($timerange == 'day') {
                             $some_comments = get_comments(array('date_query' => array(array('after' => '1 day ago'))));
                         } else {
                             if ($timerange == 'week') {
                                 $some_comments = get_comments(array('date_query' => array(array('after' => '1 week ago'))));
                             } else {
                                 if ($timerange == 'month') {
                                     $some_comments = get_comments(array('date_query' => array(array('after' => '1 month ago'))));
                                 } else {
                                     if ($timerange == 'year') {
                                         $some_comments = get_comments(array('date_query' => array(array('after' => '1 year ago'))));
                                     }
                                 }
                             }
                         }
                         $arr_id = array();
                         foreach ($some_comments as $comment) {
                             $arr_id[] = $comment->comment_post_ID;
                         }
                         $arr_id = array_unique($arr_id, SORT_REGULAR);
                         //$arr_id = implode(",", $arr_id);
                         $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'post_status' => 'publish', 'post__in' => $arr_id, 'ignore_sticky_posts' => 1);
                     }
                 } else {
                     $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => true);
                     if ($timerange != 'all') {
                         if ($timerange == 'week') {
                             $number_day = '7';
                         } elseif ($timerange == 'day') {
                             $number_day = '1';
                         } elseif ($timerange == 'month') {
                             $number_day = '30';
                         } elseif ($timerange == 'year') {
                             $number_day = '365';
                         }
                         $limit_date = date('Y-m-d', strtotime('-' . $number_day . ' day'));
                         $args['date_query'] = array('after' => $limit_date);
                     }
                 }
             }
         }
         if ($postformats) {
             $args['tax_query'] = $format_query;
         } elseif ($themes_pur != '0') {
             $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
         }
         if (!is_array($categories)) {
             if (isset($categories)) {
                 $cats = explode(",", $categories);
                 if (is_numeric($cats[0])) {
                     //$args += array('category__in' => $cats);
                     $args['category__in'] = $cats;
                 } else {
                     $args['category_name'] = $categories;
                 }
             }
         } else {
             if (count($categories) > 0) {
                 $args += array('category__in' => $categories);
             }
         }
         $query = new WP_Query($args);
         return $query;
     } else {
         if ($conditions == 'most_viewed' && $ids == '') {
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_all', 'orderby' => 'meta_value_num', 'order' => $sort_by, 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => 1, 'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-audio', 'operator' => 'NOT IN')));
             if ($postformats) {
                 $args['tax_query'] = $format_query;
             } elseif ($themes_pur != '0') {
                 $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
             }
         } elseif ($conditions == 'most_comments' && $ids == '') {
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'orderby' => 'comment_count', 'order' => $sort_by, 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => 1, 'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-audio', 'operator' => 'NOT IN')));
             if ($postformats) {
                 $args['tax_query'] = $format_query;
             } elseif ($themes_pur != '0') {
                 $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
             }
         } elseif ($conditions == 'high_rated' && $ids == '') {
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_all', 'orderby' => 'meta_value_num', 'order' => $sort_by, 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => 1);
             if ($postformats) {
                 $args['tax_query'] = $format_query;
             } elseif ($themes_pur != '0') {
                 $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
             }
         } elseif ($conditions == 'playlist') {
             $ids = explode(",", $ids);
             $gc = array();
             $dem = 0;
             foreach ($ids as $grid_cat) {
                 $dem++;
                 array_push($gc, $grid_cat);
             }
             $args = array('post_type' => 'post', 'post__in' => $gc, 'posts_per_page' => $number, 'orderby' => 'post__in', 'order' => $sort_by, 'ignore_sticky_posts' => 1, 'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video')));
         } elseif ($ids != '') {
             $ids = explode(",", $ids);
             $gc = array();
             $dem = 0;
             foreach ($ids as $grid_cat) {
                 $dem++;
                 array_push($gc, $grid_cat);
             }
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'post_status' => 'publish', 'tag' => $tags, 'post__in' => $gc, 'ignore_sticky_posts' => 1);
         } elseif ($ids == '' && $conditions == 'latest') {
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => 1);
             if ($postformats) {
                 $args['tax_query'] = $format_query;
             } elseif ($themes_pur != '0') {
                 $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
             }
         } elseif ($ids == '' && $conditions == 'title') {
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'orderby' => 'title', 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => 1);
             if ($postformats) {
                 $args['tax_query'] = $format_query;
             } elseif ($themes_pur != '0') {
                 $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
             }
         } elseif ($ids == '' && $conditions == 'random') {
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'orderby' => 'rand', 'post_status' => 'publish', 'tag' => $tags, 'ignore_sticky_posts' => 1);
             if ($postformats) {
                 $args['tax_query'] = $format_query;
             }
         } elseif ($ids == '' && $conditions == 'likes') {
             //echo 'ok';
             global $wpdb;
             $show_count = 1;
             $time_range = 'all';
             //$show_type = $instance['show_type'];
             $order_by = 'ORDER BY like_count DESC, post_title';
             $show_excluded_posts = get_option('wti_like_post_show_on_widget');
             $excluded_post_ids = explode(',', get_option('wti_like_post_excluded_posts'));
             if (!$show_excluded_posts && count($excluded_post_ids) > 0) {
                 $where = "AND post_id NOT IN (" . get_option('wti_like_post_excluded_posts') . ")";
             }
             $query = "SELECT post_id, SUM(value) AS like_count, post_title FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P ";
             $query .= "WHERE L.post_id = P.ID AND post_status = 'publish' AND value > -1 {$where} GROUP BY post_id {$order_by}";
             $posts = $wpdb->get_results($query);
             $cates_ar = $cates;
             $p_data = array();
             //print_r($posts);
             if (count($posts) > 0) {
                 foreach ($posts as $post) {
                     $p_data[] = $post->post_id;
                 }
             }
             //$p_data = array_reverse($p_data);
             //print_r($p_data);
             $args = array('post_type' => 'post', 'posts_per_page' => $number, 'orderby' => 'post__in', 'order' => 'ASC', 'post_status' => 'publish', 'tag' => $tags, 'post__in' => $p_data, 'ignore_sticky_posts' => 1);
         }
         if (!is_array($categories)) {
             if (isset($categories)) {
                 $cats = explode(",", $categories);
                 if (is_numeric($cats[0])) {
                     //$args += array('category__in' => $cats);
                     $args['category__in'] = $cats;
                 } else {
                     $args['category_name'] = $categories;
                 }
             }
         } else {
             if (count($categories) > 0) {
                 $args += array('category__in' => $categories);
             }
         }
         $query = new WP_Query($args);
         return $query;
     }
 }
    function widget($args, $instance)
    {
        wp_enqueue_script('jquery-isotope');
        $cache = wp_cache_get('widget_trending_videos', 'widget');
        if (!is_array($cache)) {
            $cache = array();
        }
        if (!isset($argsxx['widget_id'])) {
            $argsxx['widget_id'] = $this->id;
        }
        if (isset($cache[$argsxx['widget_id']])) {
            echo $cache[$argsxx['widget_id']];
            return;
        }
        ob_start();
        extract($args);
        $conditions = empty($instance['conditions']) ? '' : $instance['conditions'];
        $title = empty($instance['title']) ? '' : $instance['title'];
        $title = apply_filters('widget_title', $title);
        $number = empty($instance['number']) ? '' : $instance['number'];
        $timerange = empty($instance['timerange']) ? '' : $instance['timerange'];
        $show_likes = empty($instance['show_likes']) ? '' : $instance['show_likes'];
        $show_com = empty($instance['show_com']) ? '' : $instance['show_com'];
        $link = empty($instance['link']) ? '' : $instance['link'];
        $show_view = empty($instance['show_view']) ? '' : $instance['show_view'];
        $show_rate = empty($instance['show_rate']) ? '' : $instance['show_rate'];
        $show_excerpt = empty($instance['show_excerpt']) ? '' : $instance['show_excerpt'];
        $num_ex = empty($instance['num_ex']) ? '' : $instance['num_ex'];
        if (function_exists('ot_get_option')) {
            $themes_pur = ot_get_option('theme_purpose');
        }
        if ($conditions == 'most_liked' && class_exists('CT_ContentHelper')) {
            global $wpdb;
            $show_count = 1;
            if ($timerange == 'day') {
                $time_range = '1';
            } else {
                if ($timerange == 'week') {
                    $time_range = '7';
                } else {
                    if ($timerange == 'month') {
                        $time_range = '1m';
                    } else {
                        if ($timerange == 'year') {
                            $time_range = '1y';
                        }
                    }
                }
            }
            //$time_range = $instance['time_range'];
            //$show_type = $instance['show_type'];
            $order_by = 'ORDER BY like_count DESC, post_title';
            if ($number > 0) {
                $limit = "LIMIT " . $number;
            }
            $widget_data = $before_widget;
            $widget_data .= $before_title . $title . $after_title;
            $show_excluded_posts = get_option('wti_like_post_show_on_widget');
            $excluded_post_ids = explode(',', get_option('wti_like_post_excluded_posts'));
            if (!$show_excluded_posts && count($excluded_post_ids) > 0) {
                $where = "AND post_id NOT IN (" . get_option('wti_like_post_excluded_posts') . ")";
            }
            if ($time_range != 'all') {
                $last_date = GetWtiLastDate($time_range);
                $where .= " AND date_time >= '{$last_date}'";
            }
            //getting the most liked posts
            $query = "SELECT post_id, SUM(value) AS like_count, post_title FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P ";
            $query .= "WHERE L.post_id = P.ID AND post_status = 'publish' AND value > 0 {$where} GROUP BY post_id {$order_by} {$limit}";
            $posts = $wpdb->get_results($query);
            if (count($posts) > 0) {
                $item_loop_video = new CT_ContentHtml();
                $widget_data .= '
			<div class="widget-content">
					<div class="list-rating-item row">';
                foreach ($posts as $post) {
                    $p_data = $excerpt = '';
                    $post_title = stripslashes($post->post_title);
                    $permalink = get_permalink($post->post_id);
                    $like_count = $post->like_count;
                    $p_data = get_post($post->post_id);
                    $excerpt = strip_tags($p_data->post_content);
                    $excerpt = wp_trim_words($excerpt, $num_ex, $more = '');
                    $widget_data .= $item_loop_video->tm_likes_html($post, $like_count, $themes_pur, $show_likes, $show_com, $show_rate, $show_view, $show_excerpt, $excerpt);
                }
                $widget_data .= '</div>
			</div>';
            }
            $widget_data .= $after_widget;
            echo $widget_data;
        } else {
            if (class_exists('CT_ContentHelper')) {
                if ($conditions == 'most_viewed' || $conditions == '') {
                    if ($timerange == 'day') {
                        $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_day-' . date("Ymd"), 'orderby' => 'meta_value_num', 'order' => 'DESC', 'post_status' => 'publish');
                    } else {
                        if ($timerange == 'week') {
                            $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_week-' . date("YW"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                        } else {
                            if ($timerange == 'month') {
                                $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_month-' . date("Ym"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                            } else {
                                if ($timerange == 'year') {
                                    $args = array('post_type' => 'post', 'posts_per_page' => $number, 'meta_key' => '_count-views_year-' . date("Y"), 'orderby' => 'meta_value_num', 'order' => '', 'post_status' => 'publish');
                                }
                            }
                        }
                    }
                    if ($themes_pur != '0') {
                        $args['tax_query'] = array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-video'));
                    }
                    $the_query = new WP_Query($args);
                    $html = $before_widget;
                    if ($title) {
                        $html .= $before_title . $title . $after_title;
                    }
                    if ($the_query->have_posts()) {
                        $html .= '
						<div class="widget-content">
						<div class="list-rating-item row">
					';
                        $item_video = new CT_ContentHtml();
                        $i = 0;
                        while ($the_query->have_posts()) {
                            $the_query->the_post();
                            $i++;
                            $excerpt = get_the_excerpt();
                            $excerpt = wp_trim_words($excerpt, $num_ex, $more = '');
                            $html .= $item_video->get_item_video_trending($conditions, $themes_pur, $show_likes, $show_com, $show_rate, $show_view, $show_excerpt, $excerpt);
                        }
                        $html .= '</div>
					</div>';
                    }
                    $html .= $after_widget;
                    echo $html;
                } else {
                    if ($conditions == 'most_comments') {
                        wp_reset_postdata();
                        if ($timerange == 'day') {
                            $some_comments = get_comments(array('date_query' => array(array('after' => '1 day ago'))));
                        } else {
                            if ($timerange == 'week') {
                                $some_comments = get_comments(array('date_query' => array(array('after' => '1 week ago'))));
                            } else {
                                if ($timerange == 'month') {
                                    $some_comments = get_comments(array('date_query' => array(array('after' => '1 month ago'))));
                                } else {
                                    if ($timerange == 'year') {
                                        $some_comments = get_comments(array('date_query' => array(array('after' => '1 year ago'))));
                                    }
                                }
                            }
                        }
                        $html = $before_widget;
                        if ($title) {
                            $html .= $before_title . $title . $after_title;
                        }
                        $arr_id = array();
                        foreach ($some_comments as $comment) {
                            $arr_id[] = $comment->comment_post_ID;
                        }
                        $arr_id = array_unique($arr_id, SORT_REGULAR);
                        //$arr_id = implode(",", $arr_id);
                        $args = array('post_type' => 'post', 'posts_per_page' => $number, 'order' => $sort_by, 'post_status' => 'publish', 'post__in' => $arr_id, 'ignore_sticky_posts' => 1);
                        $query = new WP_Query($args);
                        if ($query->have_posts()) {
                            $html .= '
				<div class="widget-content">
					<div class="list-rating-item row">';
                            while ($query->have_posts()) {
                                $query->the_post();
                                $excerpt = get_the_excerpt();
                                $html .= '
						<div class="col-md-12 col-sm-4">
						  <div class="video-item">
							  <div class="videos-row">
									<div class="item-thumbnail">
										<a href="' . get_permalink(get_the_ID()) . '" title="' . get_the_title(get_the_ID()) . '">';
                                if (has_post_thumbnail(get_the_ID())) {
                                    $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'thumb_139x89', true);
                                } else {
                                    $thumbnail[0] = function_exists('tm_get_default_image') ? tm_get_default_image() : '';
                                }
                                $html .= '<img src="' . $thumbnail[0] . '" alt="' . the_title_attribute('echo=0') . '" title="' . the_title_attribute('echo=0') . '">';
                                //'.get_the_post_thumbnail(get_the_ID(), array(139,89) ).'
                                if ($themes_pur != '0') {
                                    $html .= '<div class="link-overlay fa fa-play "></div>';
                                }
                                $html .= '</a>';
                                if ($show_rate != 'hide_r') {
                                    $html .= tm_post_rating(get_the_ID());
                                }
                                $html .= '</div>
								  <div class="item-info">
									<div class="all-info">
									<h2 class="rt-article-title"> <a href="' . get_permalink(get_the_ID()) . '" title="' . get_the_title(get_the_ID()) . '">' . get_the_title(get_the_ID()) . '</a></h2>
									<div class="item-meta">';
                                if ($show_view != 'hide_v') {
                                    $html .= '<span class="pp-icon"><i class="fa fa-eye"></i> ' . get_post_meta(get_the_ID(), '_count-views_all', true) . '</span><br>';
                                }
                                if ($show_likes != 'hide_l' && function_exists('GetWtiLikeCount')) {
                                    $html .= '<span class="pp-icon iclike"><i class="fa fa-thumbs-up"></i> ' . str_replace('+', '', GetWtiLikeCount(get_the_ID())) . '</span><br>';
                                }
                                if ($show_com != 'hide_c') {
                                    $html .= '<span class="pp-icon"><i class="fa fa-comment"></i> ' . get_comments_number(get_the_ID()) . '</span><br>';
                                }
                                $html .= '
										</div>
										</div>
										</div>';
                                if ($show_excerpt != 'hide_ex') {
                                    $html .= '<div class="pp-exceprt">' . $excerpt . '</div>';
                                }
                                $html .= '
							   </div>
						  </div>
						</div>
						';
                            }
                            $html .= '</div>
				</div>';
                        }
                        $html .= $after_widget;
                        echo $html;
                    }
                }
                wp_reset_postdata();
                $cache[$argsxx['widget_id']] = ob_get_flush();
                wp_cache_set('widget_trending_videos', $cache, 'widget');
            }
        }
    }
/**
 * Check whether user has already voted or not
 * @param $post_id integer
 * @return integer
 */
function HasWtiAlreadyVoted($post_id, $check_option = 0, $voting_period = '', $get_voted_id = false)
{
    global $wpdb, $wti_ip_address;
    $where = '';
    $wti_has_voted = 0;
    $voted_id = 0;
    $voted_details = array();
    if ($voting_period != 0 && $voting_period != 'once') {
        // If there is restriction on revoting with voting period, check with voting time
        $last_voted_date = GetWtiLastDate($voting_period);
        $where = "AND date_time >= '{$last_voted_date}'";
    }
    if ($check_option == 2) {
        // 0 never voted, 1 this user has voted, 2 others have voted
        if (isset($_COOKIE["wtilp_count_{$post_id}"]) && !empty($_COOKIE["wtilp_count_{$post_id}"])) {
            // In case cookie already set
            $wti_has_voted = 1;
        } else {
            // In case cookie deleted
            $votes_key = $wpdb->get_col("SELECT cookie_value AS has_voted FROM {$wpdb->prefix}wti_like_post\n\t\t\t\t\t\t\t\t   WHERE post_id = '{$post_id}' {$where}", 0);
            if (empty($votes_key)) {
                $wti_has_voted = 0;
            } else {
                $wti_has_voted = 2;
            }
        }
    } else {
        if ($check_option == 1) {
            // Check with user id
            $current_user = wp_get_current_user();
            $user_id = (int) $current_user->ID;
            $result = $wpdb->get_row("SELECT COUNT(id) AS has_voted, SUM(value) AS vote_count\n\t\t\t\t\t\t\tFROM {$wpdb->prefix}wti_like_post\n\t\t\t\t\t\t\tWHERE post_id = '{$post_id}' AND user_id = {$user_id} {$where}");
            $wti_has_voted = $result->has_voted > 0 ? 1 : 0;
            $wti_voted_count = $result->vote_count;
            if ($get_voted_id) {
                $voted = $wpdb->get_var("SELECT MAX(id) FROM {$wpdb->prefix}wti_like_post\n\t\t\t\t\t\t\t\tWHERE post_id = '{$post_id}' AND user_id = {$user_id}");
                $voted_id = (int) $voted;
            }
        } else {
            $result = $wpdb->get_row("SELECT COUNT(id) AS has_voted, SUM(value) AS vote_count\n\t\t\t\t\t\t\tFROM {$wpdb->prefix}wti_like_post\n\t\t\t\t\t\t\tWHERE post_id = '{$post_id}' AND ip = '{$wti_ip_address}' {$where}");
            $wti_has_voted = $result->has_voted > 0 ? 1 : 0;
            $wti_voted_count = $result->vote_count;
            if ($get_voted_id) {
                $voted = $wpdb->get_var("SELECT MAX(id) FROM {$wpdb->prefix}wti_like_post\n\t\t\t\t\t\t\t\tWHERE post_id = '{$post_id}' AND ip = '{$wti_ip_address}'");
                $voted_id = (int) $voted;
            }
        }
    }
    return array('has_voted' => $wti_has_voted, 'voted_id' => $voted_id, 'voted_count' => $wti_voted_count);
}
/**
 * Most liked posts shortcode
 * @param $args array
 * @return string
 */
function WtiMyMostLikedPostsShortcode($args)
{
    global $wpdb, $current_user;
    $most_liked_post = '';
    if ($current_user->ID == 0) {
        $most_liked_post .= '<p>' . __('Please login to see the posts you like.', 'wti-like-post') . '</p>';
    } else {
        // Getting the most liked posts
        $query = "SELECT post_id, SUM(value) AS like_count, post_title, post_author FROM `{$wpdb->prefix}wti_like_post` L, {$wpdb->prefix}posts P ";
        $query .= "WHERE L.post_id = P.ID AND post_status = 'publish' AND user_id = {$current_user->ID}";
        // Get the posts for specific post type
        if (isset($args['post_type']) && !empty($args['post_type'])) {
            $post_type = trim($args['post_type']);
            $query .= " AND P.post_type IN ('" . str_replace(",", "','", $post_type) . "')";
        }
        if (isset($args['vote_type'])) {
            // Check to see whether like or unlike posts need to be shown
            if ($args['vote_type'] == 'unliked') {
                $query .= " AND value < 0";
                $order_by = "ORDER BY like_count ASC, post_title ASC";
            } elseif ($args['vote_type'] == 'total') {
                $order_by = " ORDER BY like_count DESC, post_title ASC";
            } else {
                $query .= " AND value > 0";
                $order_by = "ORDER BY like_count DESC, post_title ASC";
            }
        } else {
            $query .= " AND value > 0";
            $order_by = "ORDER BY like_count DESC, post_title ASC";
        }
        if (isset($args['category']) && !empty($args['category'])) {
            // Get the posts for specific category if selected
            $cat_posts = array();
            $category_posts = get_posts(array('category' => $args['category'], 'numberposts' => -1));
            if (count($category_posts) > 0) {
                foreach ($category_posts as $category_post) {
                    $cat_posts[] = $category_post->ID;
                }
            }
            if (count($cat_posts) > 0) {
                $query .= " AND P.ID IN (" . implode(',', $cat_posts) . ")";
            }
        }
        // Gets posts liked for selected time range
        if (isset($args['time']) && $args['time'] != 'all') {
            $last_date = GetWtiLastDate($args['time']);
            $query .= " AND date_time >= '{$last_date}'";
        }
        $limit = isset($args['limit']) ? $args['limit'] : 10;
        $query .= " GROUP BY post_id {$order_by} LIMIT {$limit}";
        $posts = $wpdb->get_results($query);
        if (count($posts) > 0) {
            $most_liked_post .= '<table class="my-most-liked-posts-table">';
            $most_liked_post .= '<tr>';
            $most_liked_post .= '<th>' . __('Title', 'wti-like-post') . '</th>';
            $most_liked_post .= '<th>' . __('Like Count', 'wti-like-post') . '</th>';
            if (isset($args['author']) && $args['author'] == 'yes') {
                $most_liked_post .= '<th>' . __('Author', 'wti-like-post') . '</th>';
            }
            $most_liked_post .= '</tr>';
            foreach ($posts as $post) {
                $post_title = stripslashes($post->post_title);
                $permalink = get_permalink($post->post_id);
                $like_count = $post->like_count;
                $most_liked_post .= '<tr>';
                $most_liked_post .= '<td><a href="' . $permalink . '" title="' . $post_title . '">' . $post_title . '</td>';
                $most_liked_post .= '<td>' . (isset($args['vote_type']) && $args['vote_type'] == 'total' ? $like_count : str_replace('-', '', $like_count)) . '</td>';
                if (isset($args['author']) && $args['author'] == 'yes') {
                    $most_liked_post .= '<td><a href="' . get_author_posts_url($post->post_author) . '" target="_blank">' . get_the_author_meta('user_nicename', $post->post_author) . '</a></td>';
                }
                $most_liked_post .= '</tr>';
            }
            $most_liked_post .= '</table>';
        } else {
            if (isset($args['vote_type']) && $args['vote_type'] == 'unliked') {
                $most_liked_post .= '<p>' . __('Nothing disliked yet.', 'wti-like-post') . '</p>';
            } else {
                $most_liked_post .= '<p>' . __('Nothing liked yet.', 'wti-like-post') . '</p>';
            }
        }
    }
    return $most_liked_post;
}