| 1 |
<?php |
| 2 |
// Creating the most liked posts widget |
| 3 |
class wp_ulike_widget extends WP_Widget { |
| 4 |
|
| 5 |
/** |
| 6 |
* Constructor |
| 7 |
*/ |
| 8 |
function __construct() { |
| 9 |
parent::__construct( |
| 10 |
'wp_ulike', |
| 11 |
__('WP Ulike Widget', 'alimir'), |
| 12 |
array( 'description' => __( 'An advanced widget that gives you all most liked records with different types', 'alimir' )) |
| 13 |
); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Most Liked Posts Function |
| 18 |
* |
| 19 |
* @author Alimir |
| 20 |
* @since 1.1 |
| 21 |
* @return String |
| 22 |
*/ |
| 23 |
public function most_liked_posts($numberOf, $before, $after, $show_count) { |
| 24 |
global $wpdb; |
| 25 |
|
| 26 |
$request = "SELECT ID, post_title, meta_value FROM ".$wpdb->prefix."posts, ".$wpdb->prefix."postmeta"; |
| 27 |
$request .= " WHERE ".$wpdb->prefix."posts.ID = ".$wpdb->prefix."postmeta.post_id"; |
| 28 |
$request .= " AND post_status='publish' AND meta_key='_liked'"; |
| 29 |
$request .= " ORDER BY ".$wpdb->prefix."postmeta.meta_value+0 DESC LIMIT $numberOf"; |
| 30 |
$posts = $wpdb->get_results($request); |
| 31 |
|
| 32 |
foreach ($posts as $post) { |
| 33 |
$post_title = stripslashes($post->post_title); |
| 34 |
$permalink = get_permalink($post->ID); |
| 35 |
$post_count = $post->meta_value; |
| 36 |
|
| 37 |
echo $before.'<a href="' . $permalink . '" title="' . $post_title.'" rel="nofollow">' . $post_title . '</a>'; |
| 38 |
echo $show_count == '1' ? ' ('.wp_ulike_format_number($post_count).')' : ''; |
| 39 |
echo $after; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Last Posts Liked By Current User |
| 45 |
* |
| 46 |
* @author Alimir |
| 47 |
* @since 2.0 |
| 48 |
* @updated 2.1 |
| 49 |
* @return String |
| 50 |
*/ |
| 51 |
public function last_posts_liked_by_current_user($numberOf, $before, $after, $show_count) { |
| 52 |
global $wpdb,$user_ID,$wp_user_IP; |
| 53 |
|
| 54 |
$request = "SELECT U.post_id, P.meta_value AS counter |
| 55 |
FROM ".$wpdb->prefix."ulike AS U, ".$wpdb->prefix."postmeta AS P |
| 56 |
WHERE (U.ip LIKE '$wp_user_IP' OR U.user_id = $user_ID) AND U.post_id = P.post_id AND meta_key='_liked' |
| 57 |
GROUP BY U.post_id |
| 58 |
ORDER BY MAX(U.date_time) DESC LIMIT $numberOf |
| 59 |
"; |
| 60 |
$likes = $wpdb->get_results($request); |
| 61 |
|
| 62 |
if($likes != 0){ |
| 63 |
foreach ($likes as $like) { |
| 64 |
$permalink = get_permalink($like->post_id); |
| 65 |
$post_title = get_the_title($like->post_id); |
| 66 |
$post_count = $like->counter; |
| 67 |
echo $before.'<a href="' . $permalink . '" title="' . $post_title.'" rel="nofollow">' . $post_title . '</a>'; |
| 68 |
echo $show_count == '1' ? ' ('.wp_ulike_format_number($post_count).')' : ''; |
| 69 |
echo $after; |
| 70 |
} |
| 71 |
} |
| 72 |
else{ |
| 73 |
echo $before; |
| 74 |
echo __('you haven\'t liked any post yet!','alimir'); |
| 75 |
echo $after; |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Most Liked Comments Function |
| 82 |
* |
| 83 |
* @author Alimir |
| 84 |
* @since 1.9 |
| 85 |
* @return String |
| 86 |
*/ |
| 87 |
public function most_liked_comments($numberOf, $before, $after, $show_count) { |
| 88 |
global $wpdb; |
| 89 |
|
| 90 |
$request = "SELECT * FROM ".$wpdb->prefix."comments, ".$wpdb->prefix."commentmeta"; |
| 91 |
$request .= " WHERE ".$wpdb->prefix."comments.comment_ID = ".$wpdb->prefix."commentmeta.comment_id"; |
| 92 |
$request .= " AND comment_approved='1' AND meta_key='_commentliked'"; |
| 93 |
$request .= " ORDER BY ".$wpdb->prefix."commentmeta.meta_value+0 DESC LIMIT $numberOf"; |
| 94 |
$comments = $wpdb->get_results($request); |
| 95 |
|
| 96 |
foreach ($comments as $comment) { |
| 97 |
$comment_author = stripslashes($comment->comment_author); |
| 98 |
$post_permalink = get_permalink($comment->comment_post_ID); |
| 99 |
$post_title = get_the_title($comment->comment_post_ID); |
| 100 |
$comment_permalink = get_permalink($comment->comment_ID); |
| 101 |
$comment_likes_count = $comment->meta_value; |
| 102 |
|
| 103 |
echo $before.'<span class="comment-author-link">' . $comment_author . '</span> ' . __('on','alimir'); |
| 104 |
echo ' <a href="' . $post_permalink . '#comment-' . $comment->comment_ID . '" title="' . $post_title.'" rel="nofollow">' . $post_title . '</a>'; |
| 105 |
echo $show_count == '1' ? ' ('.wp_ulike_format_number($comment_likes_count).')' : ''; |
| 106 |
echo $after; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Most Liked Activities Function |
| 112 |
* |
| 113 |
* @author Alimir |
| 114 |
* @since 2.0 |
| 115 |
* @return String |
| 116 |
*/ |
| 117 |
public function most_liked_activities($numberOf, $before, $after, $show_count) { |
| 118 |
global $wpdb; |
| 119 |
|
| 120 |
$request = "SELECT * FROM ".$wpdb->prefix."bp_activity, ".$wpdb->prefix."bp_activity_meta"; |
| 121 |
$request .= " WHERE ".$wpdb->prefix."bp_activity.id = ".$wpdb->prefix."bp_activity_meta.activity_id"; |
| 122 |
$request .= " AND meta_key='_activityliked'"; |
| 123 |
$request .= " ORDER BY ".$wpdb->prefix."bp_activity_meta.meta_value+0 DESC LIMIT $numberOf"; |
| 124 |
$activities = $wpdb->get_results($request); |
| 125 |
|
| 126 |
foreach ($activities as $activity) { |
| 127 |
$activity_permalink = bp_activity_get_permalink( $activity->activity_id ); |
| 128 |
$activity_action = $activity->action; |
| 129 |
|
| 130 |
echo $before; |
| 131 |
echo $activity_action; |
| 132 |
echo $after; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Most Liked Activities Function |
| 138 |
* |
| 139 |
* @author Alimir |
| 140 |
* @since 1.2 |
| 141 |
* @updated 2.0 |
| 142 |
* @return String |
| 143 |
*/ |
| 144 |
public function most_liked_users($numberOf, $before, $after, $show_count, $sizeOf) { |
| 145 |
global $wpdb; |
| 146 |
|
| 147 |
$request = "SELECT T.user_id, SUM(T.CountUser) AS SumUser |
| 148 |
FROM( |
| 149 |
SELECT user_id, count(user_id) AS CountUser |
| 150 |
FROM ".$wpdb->prefix."ulike |
| 151 |
WHERE user_id BETWEEN 1 AND 999999 |
| 152 |
GROUP BY user_id |
| 153 |
UNION ALL |
| 154 |
SELECT user_id, count(user_id) AS CountUser |
| 155 |
FROM ".$wpdb->prefix."ulike_activities |
| 156 |
WHERE user_id BETWEEN 1 AND 999999 |
| 157 |
GROUP BY user_id |
| 158 |
UNION ALL |
| 159 |
SELECT user_id, count(user_id) AS CountUser |
| 160 |
FROM ".$wpdb->prefix."ulike_comments |
| 161 |
WHERE user_id BETWEEN 1 AND 999999 |
| 162 |
GROUP BY user_id |
| 163 |
) AS T |
| 164 |
GROUP BY T.user_id |
| 165 |
ORDER BY SumUser DESC LIMIT $numberOf |
| 166 |
"; |
| 167 |
$likes = $wpdb->get_results($request); |
| 168 |
|
| 169 |
foreach ($likes as $like) { |
| 170 |
$get_user_id = stripslashes($like->user_id); |
| 171 |
$get_user_info = get_userdata($get_user_id); |
| 172 |
$get_likes_count = $like->SumUser; |
| 173 |
$echo_likes_count = $show_count == '1' ? ' ('.$get_likes_count . ' ' . __('Like','alimir').')' : ''; |
| 174 |
if($get_user_info != ''){ |
| 175 |
echo $before . '<a class="user-tooltip" title="'.$get_user_info->display_name . $echo_likes_count.'">'.get_avatar( $get_user_info->user_email, $sizeOf, '' , 'avatar').'</a>'; |
| 176 |
echo $after; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Display Widget |
| 183 |
* |
| 184 |
* @author Alimir |
| 185 |
* @since 1.1 |
| 186 |
* @updated 2.0 |
| 187 |
* @return String |
| 188 |
*/ |
| 189 |
public function widget( $args, $instance ) { |
| 190 |
$title = apply_filters('widget_title', $instance['title'] ); |
| 191 |
$numberOf = $instance['count']; |
| 192 |
$type = $instance['type']; |
| 193 |
$sizeOf = $instance['size']; |
| 194 |
$show_count = (isset($instance['show_count']) == true ) ? 1 : 0; |
| 195 |
|
| 196 |
echo $args['before_widget']; |
| 197 |
if ( ! empty( $title ) ) |
| 198 |
echo $args['before_title'] . $title . $args['after_title']; |
| 199 |
echo '<ul class="most_liked_'.$type.'">'; |
| 200 |
if($type == "post") |
| 201 |
echo $this->most_liked_posts($numberOf, '<li>', '</li>', $show_count); |
| 202 |
else if($type == "comment") |
| 203 |
echo $this->most_liked_comments($numberOf, '<li>', '</li>', $show_count); |
| 204 |
else if($type == "activity") |
| 205 |
echo $this->most_liked_activities($numberOf, '<li>', '</li>', $show_count); |
| 206 |
else if($type == "users") |
| 207 |
echo $this->most_liked_users($numberOf, '<li>', '</li>', $show_count, $sizeOf); |
| 208 |
else if($type == "last_posts_liked") |
| 209 |
echo $this->last_posts_liked_by_current_user($numberOf, '<li>', '</li>', $show_count); |
| 210 |
echo '</ul>'; |
| 211 |
echo $args['after_widget']; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Widget Options |
| 216 |
* |
| 217 |
* @author Alimir |
| 218 |
* @since 1.1 |
| 219 |
* @updated 2.0 |
| 220 |
* @return String |
| 221 |
*/ |
| 222 |
public function form( $instance ) { |
| 223 |
//Set up some default widget settings. |
| 224 |
$defaults = array( 'title' => __('Most Liked', 'alimir'), 'count' => 10, 'size' => 32, 'show_count' => false, 'type' => 'post' ); |
| 225 |
$instance = wp_parse_args( (array) $instance, $defaults ); |
| 226 |
?> |
| 227 |
<p> |
| 228 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'alimir'); ?></label> |
| 229 |
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" /> |
| 230 |
</p> |
| 231 |
|
| 232 |
<p> |
| 233 |
<label for="<?php echo $this->get_field_id( 'type' ); ?>"><?php _e('Type:', 'alimir'); ?></label> |
| 234 |
<select name="<?php echo $this->get_field_name( 'type' ); ?>" style="width:100%;"> |
| 235 |
<option value="post" <?php selected( $instance['type'], "post" ); ?>><?php echo _e('Most Liked Posts', 'alimir'); ?></option> |
| 236 |
<option value="comment" <?php selected( $instance['type'], "comment" ); ?>><?php echo _e('Most Liked Comments', 'alimir'); ?></option> |
| 237 |
<option value="activity" <?php selected( $instance['type'], "activity" ); ?>><?php echo _e('Most Liked Activities', 'alimir'); ?></option> |
| 238 |
<option value="users" <?php selected( $instance['type'], "users" ); ?>><?php echo _e('Most Liked Users', 'alimir'); ?></option> |
| 239 |
<option value="last_posts_liked" <?php selected( $instance['type'], "last_posts_liked" ); ?>><?php echo _e('Last Posts Liked By User', 'alimir'); ?></option> |
| 240 |
</select> |
| 241 |
</p> |
| 242 |
|
| 243 |
<p> |
| 244 |
<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e('Number of items to show:', 'alimir'); ?><small> (max. 15)</small></label> |
| 245 |
<input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" value="<?php echo $instance['count']; ?>" style="width:100%;" /> |
| 246 |
</p> |
| 247 |
|
| 248 |
<p> |
| 249 |
<label for="<?php echo $this->get_field_id( 'size' ); ?>"><?php _e('User avatar size:', 'alimir'); ?><small> (min. 32)</small></label> |
| 250 |
<input id="<?php echo $this->get_field_id( 'size' ); ?>" name="<?php echo $this->get_field_name( 'size' ); ?>" value="<?php echo $instance['size']; ?>" style="width:100%;" /><em><small style="color:#f00;">Just In Most Liked Users Type</small></em> |
| 251 |
</p> |
| 252 |
|
| 253 |
<p> |
| 254 |
<input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id( 'show_count' ); ?>" name="<?php echo $this->get_field_name( 'show_count' ); ?>" <?php if($instance['show_count'] == true) echo 'checked="checked"'; ?> /> |
| 255 |
<label for="<?php echo $this->get_field_id( 'show_count' ); ?>"><?php _e('Activate Like Counter', 'alimir'); ?></label> |
| 256 |
</p> |
| 257 |
<?php |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Widget Options Update |
| 262 |
* |
| 263 |
* @author Alimir |
| 264 |
* @since 1.1 |
| 265 |
* @updated 2.0 |
| 266 |
* @return String |
| 267 |
*/ |
| 268 |
public function update( $new_instance, $old_instance ) { |
| 269 |
$instance = $old_instance; |
| 270 |
|
| 271 |
$instance['title'] = strip_tags( $new_instance['title'] ); |
| 272 |
$instance['count'] = strip_tags( $new_instance['count'] ); |
| 273 |
$instance['type'] = strip_tags( $new_instance['type'] ); |
| 274 |
$instance['size'] = strip_tags( $new_instance['size'] ); |
| 275 |
$instance['show_count'] = $new_instance['show_count']; |
| 276 |
|
| 277 |
return $instance; |
| 278 |
} |
| 279 |
} |