Repository navigation
Expand file tree
/
Copy pathplugin.php
More file actions
136 lines (122 loc) · 4.58 KB
/
Copy pathplugin.php
File metadata and controls
136 lines (122 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
Plugin Name: bbPress Unread Posts
Description: Displays an icon next to each thread if there are unread posts for the current user in it.
Version: 1.0
Author: destroflyer
*/
add_action("init", "bbp_unread_posts_Initialize");
function bbp_unread_posts_Initialize(){
wp_enqueue_style("bbpress_unread_posts_Style", plugins_url("style.css", __FILE__));
if(is_user_logged_in()){
add_action("bbp_theme_before_topic_title", "bbp_unread_posts_PerformTopicSpecificActions");
add_action("bbp_theme_before_topic_title", "bbp_unread_posts_IconWrapperBegin");
add_action("bbp_theme_before_forum_title", "bbp_unreadforum_posts_IconWrapperBegin");
add_action("bbp_theme_after_topic_meta", "bbp_unread_posts_IconWrapperEnd");
add_action("bbp_theme_after_forum_title", "bbp_unreadforum_posts_IconWrapperEnd");
add_action("bbp_template_after_single_topic", "bbp_unread_posts_OnTopicVisit");
add_filter("bbp_get_topic_pagination_count", "bbp_unread_posts_MarkAllTopicsAsReadButtonFilter");
}
}
function bbp_unreadforum_posts_IconWrapperEnd(){
echo '
</div>
';
}
function bbp_unreadforum_posts_IconWrapperBegin(){
$isUnreadTopic = false;
$forum_id = bbp_unread_posts_GetCurrentLoopedForumID();
$params["post_parent"] = $forum_id;
$params["posts_per_page"] = 150;
bbp_has_topics( $params );
while ( bbp_topics() ) : bbp_the_topic();
$topicID = bbp_unread_posts_GetCurrentLoopedTopicID();
$topicLastActiveTime = bbp_convert_date(get_post_meta($topicID, '_bbp_last_active_time', true));
$lastVisitTime = get_post_meta($topicID, bbp_unread_posts_getLastVisitMetaKey(), true);
$isUnreadTopic = ($topicLastActiveTime > $lastVisitTime);
if ($isUnreadTopic) {
break;
}
endwhile;
echo '
<div class="bbpresss_unread_posts_icon">
<img src="' . plugins_url("images/" . ($isUnreadTopic?"folder_new.gif":"folder.gif"), __FILE__) . '">
</div>
<div style="display:table-cell;">
';
}
function bbp_unread_posts_PerformTopicSpecificActions(){
if(bbp_unread_posts_IsMarkAllTopicsAsReadRequested()){
$topicID = bbp_unread_posts_GetCurrentLoopedTopicID();
bbp_unread_posts_UpdateLastTopicVisit($topicID);
}
}
function bbp_unread_posts_IconWrapperBegin(){
$topicID = bbp_unread_posts_GetCurrentLoopedTopicID();
$topicLastActiveTime = bbp_convert_date(get_post_meta($topicID, '_bbp_last_active_time', true));
$lastVisitTime = get_post_meta($topicID, bbp_unread_posts_getLastVisitMetaKey(), true);
$isUnreadTopic = ($topicLastActiveTime > $lastVisitTime);
echo '
<div class="bbpresss_unread_posts_icon">
<a href="' . bbp_get_topic_last_reply_url($topicID) . '">
<img src="' . plugins_url("images/" . ($isUnreadTopic?"folder_new.gif":"folder.gif"), __FILE__) . '">
</a>
</div>
<div style="display:table-cell;">
';
}
function bbp_unread_posts_GetCurrentLoopedTopicID(){
return bbpress()->topic_query->post->ID;
}
function bbp_unread_posts_IconWrapperEnd(){
echo '
</div>
';
}
function bbp_unread_posts_OnTopicVisit(){
$topicID = bbpress()->reply_query->query["post_parent"];
bbp_unread_posts_UpdateLastTopicVisit($topicID);
}
function bbp_unread_posts_UpdateLastTopicVisit($topicID){
update_post_meta($topicID, bbp_unread_posts_getLastVisitMetaKey(), time());
}
function bbp_unread_posts_getLastVisitMetaKey(){
return "bbpress_unread_posts_last_visit_" . bbpress()->current_user->id;
}
function bbp_unread_posts_MarkAllTopicsAsReadButtonFilter($content){
global $bbp_unread_posts_paginationIndex;
if(bbp_unread_posts_IsForumPage()){
if(!isset($bbp_unread_posts_paginationIndex)){
$bbp_unread_posts_paginationIndex = -1;
}
$bbp_unread_posts_paginationIndex++;
if($bbp_unread_posts_paginationIndex == 1){
$html = '
<form action="" method="post" class="bbpress_unread_posts_mark_as_read_button_container">
<input type="hidden" name="bbp_unread_posts_markAllTopicAsRead" value="1"/>
';
if(bbp_unread_posts_IsMarkAllTopicsAsReadRequested()){
$html .= '
<span style="font-weight:bold;">Marked all topics as read</span>
';
}
else{
$html .= '
<input type="submit" value="Mark all topics as read"/>
';
}
$html .= '
</form>
';
$content = $html . $content;
}
}
return $content;
}
function bbp_unread_posts_IsForumPage(){
return isset(bbpress()->topic_query->posts);
}
function bbp_unread_posts_IsMarkAllTopicsAsReadRequested(){
return isset($_POST["bbp_unread_posts_markAllTopicAsRead"]);
}
?>