1 | <?php |
---|
2 | |
---|
3 | Class BP_Blogs_Blog { |
---|
4 | var $id; |
---|
5 | var $user_id; |
---|
6 | var $blog_id; |
---|
7 | |
---|
8 | function bp_blogs_blog( $id = null ) { |
---|
9 | global $bp, $wpdb; |
---|
10 | |
---|
11 | if ( !$user_id ) |
---|
12 | $user_id = $bp['current_userid']; |
---|
13 | |
---|
14 | if ( $id ) { |
---|
15 | $this->id = $id; |
---|
16 | $this->populate(); |
---|
17 | } |
---|
18 | } |
---|
19 | |
---|
20 | function populate() { |
---|
21 | global $wpdb, $bp; |
---|
22 | |
---|
23 | $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp['blogs']['table_name'] . " WHERE id = %d", $this->id ) ); |
---|
24 | |
---|
25 | $this->user_id = $blog->user_id; |
---|
26 | $this->blog_id = $blog->blog_id; |
---|
27 | } |
---|
28 | |
---|
29 | function save() { |
---|
30 | global $wpdb, $bp; |
---|
31 | |
---|
32 | // Don't try and save if there is no user ID or blog ID set. |
---|
33 | if ( !$this->user_id || !$this->blog_id ) |
---|
34 | return false; |
---|
35 | |
---|
36 | // Don't save if this blog has already been recorded for the user. |
---|
37 | if ( !$this->id && $this->exists() ) |
---|
38 | return false; |
---|
39 | |
---|
40 | if ( $this->id ) { |
---|
41 | // Update |
---|
42 | $sql = $wpdb->prepare( "UPDATE " . $bp['blogs']['table_name'] . " SET user_id = %d, blog_id = %d WHERE id = %d", $this->user_id, $this->blog_id, $this->id ); |
---|
43 | } else { |
---|
44 | // Save |
---|
45 | $sql = $wpdb->prepare( "INSERT INTO " . $bp['blogs']['table_name'] . " ( user_id, blog_id ) VALUES ( %d, %d )", $this->user_id, $this->blog_id ); |
---|
46 | } |
---|
47 | |
---|
48 | if ( !$wpdb->query($sql) ) |
---|
49 | return false; |
---|
50 | |
---|
51 | if ( $this->id ) |
---|
52 | return $this->id; |
---|
53 | else |
---|
54 | return $wpdb->insert_id; |
---|
55 | } |
---|
56 | |
---|
57 | function exists() { |
---|
58 | global $bp, $wpdb; |
---|
59 | |
---|
60 | return $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM " . $bp['blogs']['table_name'] . " WHERE user_id = %d AND blog_id = %d", $this->user_id, $this->blog_id ) ); |
---|
61 | } |
---|
62 | |
---|
63 | /* Static Functions */ |
---|
64 | |
---|
65 | function delete_blog_for_all( $blog_id ) { |
---|
66 | global $wpdb, $bp; |
---|
67 | |
---|
68 | bp_blogs_delete_blogmeta( $blog_id ); |
---|
69 | |
---|
70 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name'] . " WHERE blog_id = %d", $blog_id ) ); |
---|
71 | } |
---|
72 | |
---|
73 | function delete_blog_for_user( $blog_id, $user_id = null ) { |
---|
74 | global $wpdb, $bp; |
---|
75 | |
---|
76 | if ( !$user_id ) |
---|
77 | $user_id = $bp['loggedin_userid']; |
---|
78 | |
---|
79 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name'] . " WHERE user_id = %d AND blog_id = %d", $user_id, $blog_id ) ); |
---|
80 | } |
---|
81 | |
---|
82 | function delete_blogs_for_user( $user_id = null ) { |
---|
83 | global $wpdb, $bp; |
---|
84 | |
---|
85 | if ( !$user_id ) |
---|
86 | $user_id = $bp['loggedin_userid']; |
---|
87 | |
---|
88 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name'] . " WHERE user_id = %d", $user_id ) ); |
---|
89 | } |
---|
90 | |
---|
91 | function get_blogs_for_user( $user_id = null ) { |
---|
92 | global $bp, $wpdb; |
---|
93 | |
---|
94 | if ( !$user_id ) |
---|
95 | $user_id = $bp['current_userid']; |
---|
96 | |
---|
97 | // Show logged in users their hidden blogs. |
---|
98 | if ( !bp_is_home() ) |
---|
99 | $blog_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ", $user_id) ); |
---|
100 | else |
---|
101 | $blog_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ", $user_id) ); |
---|
102 | |
---|
103 | $total_blog_count = BP_Blogs_Blog::total_blog_count( $user_id ); |
---|
104 | |
---|
105 | for ( $i = 0; $i < count($blog_ids); $i++ ) { |
---|
106 | $blogs[] = array( |
---|
107 | 'id' => $blog_ids[$i], |
---|
108 | 'siteurl' => get_blog_option($blog_ids[$i], 'siteurl'), |
---|
109 | 'title' => get_blog_option($blog_ids[$i], 'blogname'), |
---|
110 | 'description' => get_blog_option($blog_ids[$i], 'blogdescription') |
---|
111 | ); |
---|
112 | } |
---|
113 | |
---|
114 | return array( 'blogs' => $blogs, 'count' => $total_blog_count ); |
---|
115 | } |
---|
116 | |
---|
117 | function total_blog_count( $user_id = null ) { |
---|
118 | global $bp, $wpdb; |
---|
119 | |
---|
120 | if ( !$user_id ) |
---|
121 | $user_id = $bp['current_userid']; |
---|
122 | |
---|
123 | // If the user is logged in return the blog count including their hidden blogs. |
---|
124 | if ( !bp_is_home() ) |
---|
125 | return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(b.blog_id) FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id) ); |
---|
126 | else |
---|
127 | return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(b.blog_id) FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id) ); |
---|
128 | } |
---|
129 | |
---|
130 | function get_all( $limit = null, $page = null ) { |
---|
131 | global $bp, $wpdb; |
---|
132 | |
---|
133 | if ( $limit && $page ) { |
---|
134 | $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); |
---|
135 | $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(b.blog_id) FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN " . $bp['blogs']['table_name_blogmeta'] . " bm ON b.blog_id = bm.blog_id LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' ORDER BY CONVERT(bm.meta_value, SIGNED) DESC" ) ); |
---|
136 | } |
---|
137 | |
---|
138 | $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN " . $bp['blogs']['table_name_blogmeta'] . " bm ON b.blog_id = bm.blog_id LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm.meta_key = 'last_activity' ORDER BY CONVERT(bm.meta_value, SIGNED) DESC {$pag_sql}" ) ); |
---|
139 | |
---|
140 | return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); |
---|
141 | } |
---|
142 | |
---|
143 | function get_by_letter( $letter, $limit = null, $page = null ) { |
---|
144 | global $bp, $wpdb; |
---|
145 | |
---|
146 | like_escape($letter); |
---|
147 | |
---|
148 | if ( $limit && $page ) { |
---|
149 | $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); |
---|
150 | $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(blog_id) FROM {$bp['blogs']['table_name_blogmeta']} WHERE meta_key = 'name' AND meta_value LIKE '$letter%%' ORDER BY meta_value ASC" ) ); |
---|
151 | } |
---|
152 | |
---|
153 | $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT blog_id FROM {$bp['blogs']['table_name_blogmeta']} WHERE meta_key = 'name' AND meta_value LIKE '$letter%%' ORDER BY meta_value ASC{$pag_sql}" ) ); |
---|
154 | |
---|
155 | return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); |
---|
156 | } |
---|
157 | |
---|
158 | function search_blogs( $filter, $limit = null, $page = null ) { |
---|
159 | global $wpdb, $bp; |
---|
160 | |
---|
161 | like_escape($filter); |
---|
162 | |
---|
163 | if ( $limit && $page ) { |
---|
164 | $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); |
---|
165 | $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(blog_id) FROM {$bp['blogs']['table_name_blogmeta']} WHERE ( meta_key = 'name' OR meta_key = 'description' ) AND meta_value LIKE '%%$filter%%' ORDER BY meta_value ASC" ) ); |
---|
166 | } |
---|
167 | |
---|
168 | $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT blog_id FROM {$bp['blogs']['table_name_blogmeta']} WHERE ( meta_key = 'name' OR meta_key = 'description' ) AND meta_value LIKE '%%$filter%%' ORDER BY meta_value ASC{$pag_sql}" ) ); |
---|
169 | |
---|
170 | return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); |
---|
171 | } |
---|
172 | |
---|
173 | function get_random( $limit = null, $page = null ) { |
---|
174 | global $bp, $wpdb; |
---|
175 | |
---|
176 | if ( $limit && $page ) { |
---|
177 | $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); |
---|
178 | $total_blogs = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(b.blog_id) FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY rand()" ) ); |
---|
179 | } |
---|
180 | |
---|
181 | $paged_blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id FROM " . $bp['blogs']['table_name'] . " b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$pag_sql}" ) ); |
---|
182 | |
---|
183 | return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); |
---|
184 | } |
---|
185 | |
---|
186 | function is_hidden( $blog_id ) { |
---|
187 | global $wpdb; |
---|
188 | |
---|
189 | if ( $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT blog_id FROM {$wpdb->base_prefix}blogs WHERE blog_id = %d AND public = 1 AND archived = '0' AND spam = 0 AND mature = 0 AND deleted = 0", $blog_id ) ) ) |
---|
190 | return false; |
---|
191 | |
---|
192 | return true; |
---|
193 | } |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | Class BP_Blogs_Post { |
---|
198 | var $id; |
---|
199 | var $user_id; |
---|
200 | var $blog_id; |
---|
201 | var $post_id; |
---|
202 | var $date_created; |
---|
203 | |
---|
204 | function bp_blogs_post( $id = null ) { |
---|
205 | global $bp, $wpdb; |
---|
206 | |
---|
207 | if ( !$user_id ) |
---|
208 | $user_id = $bp['current_userid']; |
---|
209 | |
---|
210 | if ( $id ) { |
---|
211 | $this->id = $id; |
---|
212 | $this->populate(); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | function populate() { |
---|
217 | global $wpdb, $bp; |
---|
218 | |
---|
219 | $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE id = %d", $this->id ) ); |
---|
220 | |
---|
221 | $this->user_id = $post->user_id; |
---|
222 | $this->blog_id = $post->blog_id; |
---|
223 | $this->post_id = $post->post_id; |
---|
224 | $this->date_created = $post->date_created; |
---|
225 | } |
---|
226 | |
---|
227 | function save() { |
---|
228 | global $wpdb, $bp; |
---|
229 | |
---|
230 | if ( $this->id ) { |
---|
231 | // Update |
---|
232 | $sql = $wpdb->prepare( "UPDATE " . $bp['blogs']['table_name_blog_posts'] . " SET post_id = %d, blog_id = %d, user_id = %d, date_created = FROM_UNIXTIME(%d) WHERE id = %d", $this->post_id, $this->blog_id, $this->user_id, $this->date_created, $this->id ); |
---|
233 | } else { |
---|
234 | // Save |
---|
235 | $sql = $wpdb->prepare( "INSERT INTO " . $bp['blogs']['table_name_blog_posts'] . " ( post_id, blog_id, user_id, date_created ) VALUES ( %d, %d, %d, FROM_UNIXTIME(%d) )", $this->post_id, $this->blog_id, $this->user_id, $this->date_created ); |
---|
236 | } |
---|
237 | |
---|
238 | if ( !$wpdb->query($sql) ) |
---|
239 | return false; |
---|
240 | |
---|
241 | if ( $this->id ) |
---|
242 | return $this->id; |
---|
243 | else |
---|
244 | return $wpdb->insert_id; |
---|
245 | } |
---|
246 | |
---|
247 | /* Static Functions */ |
---|
248 | |
---|
249 | function delete( $post_id, $blog_id, $user_id = null ) { |
---|
250 | global $wpdb, $bp, $current_user; |
---|
251 | |
---|
252 | if ( !$user_id ) |
---|
253 | $user_id = $current_user->ID; |
---|
254 | |
---|
255 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE user_id = %d AND blog_id = %d AND post_id = %d", $user_id, $blog_id, $post_id ) ); |
---|
256 | } |
---|
257 | |
---|
258 | function delete_oldest( $user_id = null ) { |
---|
259 | global $wpdb, $bp; |
---|
260 | |
---|
261 | if ( !$user_id ) |
---|
262 | $user_id = $current_user->ID; |
---|
263 | |
---|
264 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) ); |
---|
265 | } |
---|
266 | |
---|
267 | function delete_posts_for_user( $user_id = null ) { |
---|
268 | global $wpdb, $bp; |
---|
269 | |
---|
270 | if ( !$user_id ) |
---|
271 | $user_id = $bp['loggedin_userid']; |
---|
272 | |
---|
273 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE user_id = %d", $user_id ) ); |
---|
274 | } |
---|
275 | |
---|
276 | function delete_posts_for_blog( $blog_id ) { |
---|
277 | global $wpdb, $bp; |
---|
278 | |
---|
279 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE blog_id = %d", $blog_id ) ); |
---|
280 | } |
---|
281 | |
---|
282 | function get_latest_posts( $blog_id = null, $limit = 5 ) { |
---|
283 | global $wpdb, $bp; |
---|
284 | |
---|
285 | if ( $blog_id ) |
---|
286 | $blog_sql = $wpdb->prepare( " AND p.blog_id = %d", $blog_id ); |
---|
287 | |
---|
288 | $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_id, p.blog_id FROM " . $bp['blogs']['table_name_blog_posts'] . " p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 $blog_sql ORDER BY p.date_created DESC LIMIT $limit" ) ); |
---|
289 | |
---|
290 | for ( $i = 0; $i < count($post_ids); $i++ ) { |
---|
291 | $posts[$i] = BP_Blogs_Post::fetch_post_content($post_ids[$i]); |
---|
292 | } |
---|
293 | |
---|
294 | return $posts; |
---|
295 | } |
---|
296 | |
---|
297 | function get_posts_for_user( $user_id = null ) { |
---|
298 | global $bp, $wpdb; |
---|
299 | |
---|
300 | if ( !$user_id ) |
---|
301 | $user_id = $bp['current_userid']; |
---|
302 | |
---|
303 | // Show a logged in user their posts on private blogs, but not anyone else. |
---|
304 | if ( !bp_is_home() ) { |
---|
305 | $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_id, p.blog_id FROM " . $bp['blogs']['table_name_blog_posts'] . " p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.user_id = %d ORDER BY p.date_created DESC", $user_id) ); |
---|
306 | $total_post_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(p.post_id) FROM " . $bp['blogs']['table_name_blog_posts'] . " p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.user_id = %d", $user_id) ); |
---|
307 | } else { |
---|
308 | $post_ids = $wpdb->get_results( $wpdb->prepare( "SELECT p.post_id, p.blog_id FROM " . $bp['blogs']['table_name_blog_posts'] . " p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND p.user_id = %d ORDER BY p.date_created DESC", $user_id) ); |
---|
309 | $total_post_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(p.post_id) FROM " . $bp['blogs']['table_name_blog_posts'] . " p LEFT JOIN {$wpdb->base_prefix}blogs b ON p.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 p.user_id = %d", $user_id) ); |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | for ( $i = 0; $i < count($post_ids); $i++ ) { |
---|
314 | $post = BP_Blogs_Post::fetch_post_content($post_ids[$i]); |
---|
315 | if($post->post_author == $user_id) |
---|
316 | $posts[] = $post; |
---|
317 | else |
---|
318 | $total_post_count--; |
---|
319 | } |
---|
320 | return array( 'posts' => $posts, 'count' => $total_post_count ); |
---|
321 | } |
---|
322 | |
---|
323 | function fetch_post_content( $post_object ) { |
---|
324 | global $current_blog; |
---|
325 | |
---|
326 | // TODO: switch_to_blog() calls are expensive and this needs to be changed. |
---|
327 | switch_to_blog( $post_object->blog_id ); |
---|
328 | $post = get_post($post_object->post_id); |
---|
329 | $post->blog_id = $post_object->blog_id; |
---|
330 | switch_to_blog( $current_blog->blog_id ); |
---|
331 | |
---|
332 | return $post; |
---|
333 | } |
---|
334 | |
---|
335 | function get_total_recorded_for_user( $user_id = null ) { |
---|
336 | global $bp, $wpdb; |
---|
337 | |
---|
338 | if ( !$user_id ) |
---|
339 | $user_id = $current_user->ID; |
---|
340 | |
---|
341 | return $wpdb->get_var( $wpdb->prepare( "SELECT count(post_id) FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE user_id = %d", $user_id ) ); |
---|
342 | } |
---|
343 | |
---|
344 | function is_recorded( $post_id, $blog_id, $user_id = null ) { |
---|
345 | global $bp, $wpdb, $current_user; |
---|
346 | |
---|
347 | if ( !$user_id ) |
---|
348 | $user_id = $current_user->ID; |
---|
349 | |
---|
350 | return $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE post_id = %d AND blog_id = %d AND user_id = %d", $post_id, $blog_id, $user_id ) ); |
---|
351 | } |
---|
352 | |
---|
353 | function total_post_count( $blog_id ) { |
---|
354 | global $bp, $wpdb; |
---|
355 | |
---|
356 | if ( !$blog_id ) |
---|
357 | return false; |
---|
358 | |
---|
359 | return $wpdb->get_var( $wpdb->prepare( "SELECT count(post_id) FROM " . $bp['blogs']['table_name_blog_posts'] . " WHERE blog_id = %d", $blog_id ) ); |
---|
360 | } |
---|
361 | |
---|
362 | function get_all() { |
---|
363 | global $bp, $wpdb; |
---|
364 | |
---|
365 | return $wpdb->get_col( $wpdb->prepare( "SELECT post_id, blog_id FROM " . $bp['blogs']['table_name_blog_posts'] ) ); |
---|
366 | } |
---|
367 | |
---|
368 | } |
---|
369 | |
---|
370 | Class BP_Blogs_Comment { |
---|
371 | var $id; |
---|
372 | var $user_id; |
---|
373 | var $blog_id; |
---|
374 | var $comment_id; |
---|
375 | var $comment_post_id; |
---|
376 | var $date_created; |
---|
377 | |
---|
378 | function bp_blogs_comment( $id = null ) { |
---|
379 | global $bp, $wpdb; |
---|
380 | |
---|
381 | if ( !$user_id ) |
---|
382 | $user_id = $bp['current_userid']; |
---|
383 | |
---|
384 | if ( $id ) { |
---|
385 | $this->id = $id; |
---|
386 | $this->populate( $id ); |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | function populate( $id ) { |
---|
391 | global $wpdb, $bp; |
---|
392 | |
---|
393 | $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp['blogs']['table_name_blog_comments'] . " WHERE id = %d", $this->id ) ); |
---|
394 | |
---|
395 | $this->comment_id = $comment->comment_id; |
---|
396 | $this->user_id = $comment->user_id; |
---|
397 | $this->blog_id = $comment->blog_id; |
---|
398 | $this->comment_post_id = $comment->comment_post_id; |
---|
399 | $this->date_created = $comment->date_created; |
---|
400 | } |
---|
401 | |
---|
402 | function save() { |
---|
403 | global $wpdb, $bp; |
---|
404 | |
---|
405 | if ( $this->id ) { |
---|
406 | // Update |
---|
407 | $sql = $wpdb->prepare( "UPDATE " . $bp['blogs']['table_name_blog_comments'] . " SET comment_id = %d, comment_post_id = %d, blog_id = %d, user_id = %d, date_created = FROM_UNIXTIME(%d) WHERE id = %d", $this->comment_id, $this->comment_post_id, $this->blog_id, $this->user_id, $this->date_created, $this->id ); |
---|
408 | } else { |
---|
409 | // Save |
---|
410 | $sql = $wpdb->prepare( "INSERT INTO " . $bp['blogs']['table_name_blog_comments'] . " ( comment_id, comment_post_id, blog_id, user_id, date_created ) VALUES ( %d, %d, %d, %d, FROM_UNIXTIME(%d) )", $this->comment_id, $this->comment_post_id, $this->blog_id, $this->user_id, $this->date_created ); |
---|
411 | } |
---|
412 | |
---|
413 | if ( !$wpdb->query($sql) ) |
---|
414 | return false; |
---|
415 | |
---|
416 | if ( $this->id ) |
---|
417 | return $this->id; |
---|
418 | else |
---|
419 | return $wpdb->insert_id; |
---|
420 | } |
---|
421 | |
---|
422 | /* Static Functions */ |
---|
423 | |
---|
424 | function delete( $comment_id, $blog_id, $user_id = null ) { |
---|
425 | global $wpdb, $bp, $current_user; |
---|
426 | |
---|
427 | if ( !$user_id ) |
---|
428 | $user_id = $current_user->ID; |
---|
429 | |
---|
430 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_comments'] . " WHERE comment_id = %d AND blog_id = %d AND user_id = %d", $comment_id, $blog_id, $user_id ) ); |
---|
431 | } |
---|
432 | |
---|
433 | function delete_oldest( $user_id = null ) { |
---|
434 | global $wpdb, $bp, $current_user; |
---|
435 | |
---|
436 | if ( !$user_id ) |
---|
437 | $user_id = $current_user->ID; |
---|
438 | |
---|
439 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_comments'] . " WHERE user_id = %d ORDER BY date_created ASC LIMIT 1", $user_id ) ); |
---|
440 | } |
---|
441 | |
---|
442 | function delete_comments_for_user( $user_id = null ) { |
---|
443 | global $wpdb, $bp; |
---|
444 | |
---|
445 | if ( !$user_id ) |
---|
446 | $user_id = $bp['loggedin_userid']; |
---|
447 | |
---|
448 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_comments'] . " WHERE user_id = %d", $user_id ) ); |
---|
449 | } |
---|
450 | |
---|
451 | function delete_comments_for_blog( $blog_id ) { |
---|
452 | global $wpdb, $bp; |
---|
453 | |
---|
454 | return $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blog_comments'] . " WHERE blog_id = %d", $blog_id ) ); |
---|
455 | } |
---|
456 | |
---|
457 | function get_comments_for_user( $user_id = null ) { |
---|
458 | global $bp, $wpdb; |
---|
459 | |
---|
460 | if ( !$user_id ) |
---|
461 | $user_id = $bp['current_userid']; |
---|
462 | |
---|
463 | // Show the logged in user their comments on hidden blogs, but not to anyone else. |
---|
464 | if ( !bp_is_home() ) { |
---|
465 | $comment_ids = $wpdb->get_results( $wpdb->prepare( "SELECT c.comment_id, c.blog_id FROM " . $bp['blogs']['table_name_blog_comments'] . " c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d ORDER BY c.date_created ASC", $user_id) ); |
---|
466 | $total_comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(c.comment_id) FROM " . $bp['blogs']['table_name_blog_comments'] . " c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.public = 1 AND b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d", $user_id) ); |
---|
467 | } else { |
---|
468 | $comment_ids = $wpdb->get_results( $wpdb->prepare( "SELECT c.comment_id, c.blog_id FROM " . $bp['blogs']['table_name_blog_comments'] . " c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d ORDER BY c.date_created ASC", $user_id) ); |
---|
469 | $total_comment_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(c.comment_id) FROM " . $bp['blogs']['table_name_blog_comments'] . " c LEFT JOIN {$wpdb->base_prefix}blogs b ON c.blog_id = b.blog_id WHERE b.deleted = 0 AND b.archived = '0' AND b.spam = 0 AND b.mature = 0 AND c.user_id = %d", $user_id) ); |
---|
470 | } |
---|
471 | |
---|
472 | for ( $i = 0; $i < count($comment_ids); $i++ ) { |
---|
473 | $comments[$i] = BP_Blogs_Comment::fetch_comment_content($comment_ids[$i]); |
---|
474 | } |
---|
475 | |
---|
476 | return array( 'comments' => $comments, 'count' => $total_comment_count ); |
---|
477 | } |
---|
478 | |
---|
479 | function fetch_comment_content( $comment_object ) { |
---|
480 | global $current_blog; |
---|
481 | |
---|
482 | switch_to_blog($comment_object->blog_id); |
---|
483 | $comment = get_comment($comment_object->comment_id); |
---|
484 | $comment->blog_id = $comment_object->blog_id; |
---|
485 | $comment->post = &get_post( $comment->comment_post_ID ); |
---|
486 | switch_to_blog($current_blog->blog_id); |
---|
487 | |
---|
488 | return $comment; |
---|
489 | } |
---|
490 | |
---|
491 | function get_total_recorded_for_user( $user_id = null ) { |
---|
492 | global $bp, $wpdb, $current_user; |
---|
493 | |
---|
494 | if ( !$user_id ) |
---|
495 | $user_id = $current_user->ID; |
---|
496 | |
---|
497 | return $wpdb->get_var( $wpdb->prepare( "SELECT count(comment_id) FROM " . $bp['blogs']['table_name_blog_comments'] . " WHERE user_id = %d", $user_id ) ); |
---|
498 | } |
---|
499 | |
---|
500 | function total_comment_count( $blog_id, $post_id ) { |
---|
501 | global $bp, $wpdb; |
---|
502 | |
---|
503 | if ( $post_id ) |
---|
504 | $post_sql = $wpdb->prepare( " AND comment_post_id = %d", $post_id ); |
---|
505 | |
---|
506 | return $wpdb->get_var( $wpdb->prepare( "SELECT count(comment_id) WHERE blog_id = %d{$post_sql}", $blog_id ) ); |
---|
507 | } |
---|
508 | |
---|
509 | |
---|
510 | function is_recorded( $comment_id, $comment_post_id, $blog_id, $user_id = null ) { |
---|
511 | global $bp, $wpdb, $current_user; |
---|
512 | |
---|
513 | if ( !$user_id ) |
---|
514 | $user_id = $current_user->ID; |
---|
515 | |
---|
516 | return $wpdb->get_var( $wpdb->prepare( "SELECT comment_id FROM " . $bp['blogs']['table_name_blog_comments'] . " WHERE comment_id = %d AND blog_id = %d AND comment_post_id = %d AND user_id = %d", $comment_id, $blog_id, $comment_post_id, $user_id ) ); |
---|
517 | } |
---|
518 | |
---|
519 | } |
---|
520 | |
---|
521 | ?> |
---|