PluginProbe
wpForo Forum / 1.2.0
wpForo Forum v1.2.0
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / class-topics.php

class-topics.php in wpForo Forum 1.2.0, at wpf-includes/class-topics.php

803 lines 30.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if( !defined( 'ABSPATH' ) ) exit;
4
5 class wpForoTopic{
6
7 private $wpforo;
8 private static $cache = array();
9
10 function __construct( $wpForo ){
11 if(!isset($this->wpforo)) $this->wpforo = $wpForo;
12 }
13
14 private function unique_slug($slug){
15 $new_slug = wpforo_text($slug, 250, false);
16 $i = 2;
17 while( $this->wpforo->db->get_var("SELECT `topicid` FROM ".$this->wpforo->db->prefix."wpforo_topics WHERE `slug` = '" . esc_sql($new_slug) . "'") ){
18 $new_slug = wpforo_text($slug, 250, false) . '-' . $i;
19 $i++;
20 }
21 return $new_slug;
22 }
23
24 public function add( $args = array() ){
25
26 if( empty($args) && empty($_REQUEST['topic']) ) return FALSE;
27 if( empty($args) && !empty($_REQUEST['topic']) ){
28 $args = $_REQUEST['topic'];
29 $args['body'] = $_REQUEST['postbody'];
30 }
31
32 if( !isset($args['body']) || !$args['body'] ){ $this->wpforo->notice->add('Post is empty', 'error'); return FALSE; }
33
34 if( !isset($args['forumid']) || !$args['forumid'] = intval($args['forumid']) ){
35 $this->wpforo->notice->add('Add Topic error: No forum selected', 'error');
36 return FALSE;
37 }
38
39 if( !$this->wpforo->perm->forum_can( 'ct', $args['forumid']) ){
40 $this->wpforo->notice->add('You haven\'t permission to create topic into this forum', 'error');
41 return FALSE;
42 }
43
44 if( !isset($args['title']) || !$args['title'] = trim(strip_tags($args['title'])) ){
45 $this->wpforo->notice->add('Please insert required fields!', 'error');
46 return FALSE;
47 }
48
49 do_action( 'wpforo_start_add_topic', $args );
50
51 $args['title'] = wpforo_text($args['title'], 250, false);
52 $args['body'] = (isset($args['body']) ? preg_replace('#</pre>[\r\n\t\s\0]*<pre>#isu', "\r\n", $args['body']) : '' );
53 $args['slug'] = (isset($args['slug']) && $args['slug']) ? sanitize_title($args['slug']) : ((isset($args['title'])) ? sanitize_title($args['title']) : md5(time()));
54 $args['slug'] = $this->unique_slug($args['slug']);
55 $args['created'] = (isset($args['created']) ? sanitize_text_field($args['created']) : current_time( 'mysql', 1 ) );
56 $args['userid'] = (isset($args['userid']) ? intval($args['userid']) : $this->wpforo->current_userid );
57
58 $args = apply_filters('wpforo_add_topic_data_filter', $args);
59
60 if(empty($args)) return FALSE;
61
62 extract($args, EXTR_OVERWRITE);
63
64 if(isset($forumid)) $forumid = intval($forumid);
65 if(isset($title)) $title = sanitize_text_field(trim($title));
66 if(isset($slug)) $slug = sanitize_title($slug);
67 if(isset($created)) $created = sanitize_text_field($created);
68 if(isset($userid)) $userid = intval($userid);
69 if(isset($type)) $type = intval($type);
70 if(isset($status)) $status = intval($status);
71 if(isset($private)) $private = intval($private);
72 if(isset($meta_key)) $meta_key = sanitize_text_field($meta_key);
73 if(isset($meta_desc)) $meta_desc = sanitize_text_field($meta_desc);
74 if(isset($body)) $body = wpforo_kses(trim($body), 'post');
75 $meta_key = (isset($meta_key) ? $meta_key : '');
76 $meta_desc = (isset($meta_desc) ? $meta_desc : '');
77 $has_attach = ( isset($has_attach) && $has_attach ) ? 1 : ((strpos($body, '[attach]') !== FALSE) ? 1 : 0);
78
79 do_action( 'wpforo_before_add_topic', $args );
80
81 if(
82 $this->wpforo->db->insert(
83 $this->wpforo->db->prefix . 'wpforo_topics',
84 array(
85 'title' => stripslashes($title),
86 'slug' => $slug,
87 'forumid' => $forumid,
88 'userid' => $userid,
89 'type' => (isset($type) ? 1 : 0),
90 'status' => (isset($status) ? $status : 0),
91 'private' => (isset($private) ? 1 : 0),
92 'created' => $created,
93 'modified' => $created,
94 'last_post' => 0,
95 'views' => 0,
96 'posts' => 1,
97 'meta_key' => $meta_key,
98 'meta_desc' => $meta_desc,
99 'has_attach'=> $has_attach
100 ),
101 array('%s','%s','%d','%d','%d','%d','%d','%s','%s','%d','%d','%d','%s','%s','%d')
102 )
103 ){
104 $topicid = $this->wpforo->db->insert_id;
105 if(
106 $this->wpforo->db->insert(
107 $this->wpforo->db->prefix . 'wpforo_posts',
108 array(
109 'forumid' => $forumid,
110 'topicid' => $topicid,
111 'userid' => $userid,
112 'title' => stripslashes($title),
113 'body' => stripslashes($body),
114 'created' => $created,
115 'modified' => $created,
116 'is_first_post' => 1,
117 'status' => (isset($status) ? $status : 0),
118 ),
119 array('%d','%d','%d','%s','%s','%s','%s','%d','%d')
120 )
121 ){
122 $first_postid = $this->wpforo->db->insert_id;
123 if( FALSE !== $this->wpforo->db->update(
124 $this->wpforo->db->prefix . 'wpforo_topics',
125 array( 'first_postid' => $first_postid, 'last_post' => $first_postid ),
126 array( 'topicid' => $topicid ),
127 array( '%d', '%d' ),
128 array( '%d' )
129 )
130 ){
131 $questions = '';
132 $forum = $this->wpforo->forum->get_forum($forumid);
133 if( isset($forum['cat_layout']) && $forum['cat_layout'] == 3 ) $questions = ', `questions` = `questions` + 1 ';
134
135 $this->wpforo->db->query( "UPDATE " . $this->wpforo->db->prefix . "wpforo_forums SET `last_post_date` = '" . esc_sql($created). "', `last_userid` = " . intval($userid). ", `last_topicid` = " . intval($topicid) . ", `last_postid` = " . intval($first_postid) . ", `topics` = `topics` + 1 , `posts` = `posts` + 1 WHERE `forumid` = " . intval($forumid) );
136 $this->wpforo->db->query( "UPDATE " . $this->wpforo->db->prefix . "wpforo_profiles SET `posts` = `posts` + 1 $questions WHERE `userid` = " . intval($userid) );
137
138 $args['topicid'] = $topicid;
139 $args['topicurl'] = $this->get_topic_url($topicid);
140
141 do_action( 'wpforo_after_add_topic', $args );
142
143 $this->wpforo->member->reset($userid);
144 $this->wpforo->notice->add('Your topic successfully added', 'success');
145 return $topicid;
146 }
147 }
148
149 }
150
151 $this->wpforo->notice->add('Topic add error', 'error');
152 return FALSE;
153 }
154
155 public function edit( $args = array() ){
156
157 if( empty($args) && empty($_REQUEST['topic']) ) return FALSE;
158 if( !isset($args['topicid']) && isset($_GET['id']) ) $args['topicid'] = intval($_GET['id']);
159 if( empty($args) && !empty($_REQUEST['topic']) ){ $args = $_REQUEST['topic']; $args['body'] = $_REQUEST['postbody']; }
160
161 do_action( 'wpforo_start_edit_topic', $args );
162
163 if( !$topic = $this->get_topic( $args['topicid'] ) ){
164 $this->wpforo->notice->add('Topic not found.', 'error');
165 return FALSE;
166 }
167
168 $args['status'] = $topic['status'];
169 $args['userid'] = $topic['userid'];
170
171 $args = apply_filters('wpforo_edit_topic_data_filter', $args);
172 if(empty($args)) return FALSE;
173
174 extract($args, EXTR_OVERWRITE);
175
176 if(isset($topicid)) $topicid = intval($topicid);
177 if(isset($forumid)) $forumid = intval($forumid);
178 if(isset($title)) $title = sanitize_text_field(trim($title));
179 if(isset($slug)) $slug = sanitize_title($slug);
180 if(isset($created)) $created = sanitize_text_field($created);
181 if(isset($userid)) $userid = intval($userid);
182 if(isset($type)) $type = intval($type);
183 if(isset($status)) $status = intval($status);
184 if(isset($private)) $private = intval($private);
185 if(isset($meta_key)) $meta_key = sanitize_text_field($meta_key);
186 if(isset($meta_desc)) $meta_desc = sanitize_text_field($meta_desc);
187 if(isset($has_attach)) $has_attach = intval($has_attach);
188 if(isset($body)) $body = wpforo_kses(trim($body), 'post');
189
190
191 if( !isset($topicid) ){
192 $this->wpforo->notice->add('Topic edit error', 'error');
193 return FALSE;
194 }
195 if( !isset($title) || !$title = trim(strip_tags($title)) ){
196 $this->wpforo->notice->add('Please insert required fields!', 'error');
197 return FALSE;
198 }
199
200 $title = wpforo_text($title, 250, false);
201 if(isset($body)) $body = preg_replace('#</pre>[\r\n\t\s\0]*<pre>#isu', "\r\n", $body);
202
203 $diff = current_time( 'timestamp', 1 ) - strtotime($topic['created']);
204 if( !($this->wpforo->perm->forum_can('et', $topic['forumid']) || ($this->wpforo->current_userid == $topic['userid'] && $this->wpforo->perm->forum_can('eot', $topic['forumid']) && $diff < $this->wpforo->post_options['eot_durr'])) ){
205 $this->wpforo->notice->add('You have no permission to edit this topic', 'error');
206 return FALSE;
207 }
208
209 $t_update = $this->wpforo->db->update(
210 $this->wpforo->db->prefix."wpforo_topics",
211 array(
212 'title' => ( isset($title) ? stripslashes($title) : stripslashes($topic['title']) ),
213 'type' => ( isset($type) ? $type : intval($topic['type']) ),
214 'status' => ( isset($status) ? $status : intval($topic['status']) ),
215 'private' => ( isset($private) ? $private : intval($topic['private']) ),
216 'has_attach'=> ( isset($body) ? (strpos($body, '[attach]') !== FALSE ? 1 : 0) : $topic['has_attach'] )
217 ),
218 array( 'topicid' => intval($topicid) ),
219 array( '%s','%d','%d','%d','%d' ),
220 array( '%d' )
221 );
222
223 if( isset($topic['first_postid']) ){
224 if( !$post = $this->wpforo->post->get_post( $topic['first_postid'] ) ){
225 $this->wpforo->notice->add('Topic first post data not found.', 'error');
226 return FALSE;
227 }
228 }
229 else{
230 $this->wpforo->notice->add('Topic first post not found.', 'error');
231 return FALSE;
232 }
233
234 $p_update = $this->wpforo->db->update(
235 $this->wpforo->db->prefix."wpforo_posts",
236 array(
237 'title' => ( isset($title) ? stripslashes($title) : stripslashes($post['title']) ),
238 'body' => ( (isset($body) && $body) ? stripslashes($body) : stripslashes($post['body']) ),
239 'modified' => current_time( 'mysql', 1 ),
240 'status' => ( isset($status) ? $status : intval($topic['status']) ),
241 ),
242 array( 'postid' => intval($topic['first_postid']) ),
243 array( '%s', '%s', '%s', '%d' ),
244 array( '%d' )
245 );
246
247 if($t_update !== FALSE && $p_update !== FALSE){
248 $this->wpforo->notice->add('Topic successfully updated', 'success');
249 return $topicid;
250 }
251
252 $this->wpforo->notice->add('Topic edit error', 'error');
253 return FALSE;
254 }
255
256 private function users_stats_incr_minus($topicid){
257 $topicid = intval($topicid);
258 $sql = "SELECT `userid`, IF(`parentid` = 0, 'answers', 'comments') AS `type`, COUNT(*) AS `quantity`
259 FROM `".$this->wpforo->db->prefix."wpforo_posts`
260 WHERE `is_first_post` != 1 AND `topicid` IN( $topicid )
261 GROUP BY `userid`, `parentid` = 0
262 ORDER BY `userid`, `type`";
263 if( $users_incr_stats = $this->wpforo->db->get_results($sql, ARRAY_A) ){
264 $prev_userid = 0;
265 $sets = array();
266 foreach( $users_incr_stats as $users_incr_stat ){
267 if( $prev_userid == 0 ) $prev_userid = $users_incr_stat['userid'];
268
269 if( $prev_userid != $users_incr_stat['userid'] && $prev_userid != 0 ){
270 if( !empty($sets) ){
271 $sql = "UPDATE IGNORE `".$this->wpforo->db->prefix."wpforo_profiles` SET ".implode(', ', $sets)." WHERE `userid` = " . intval($prev_userid);
272 $this->wpforo->db->query($sql);
273 }
274 $prev_userid = $users_incr_stat['userid'];
275 $sets = array();
276 }
277
278 if( $users_incr_stat['type'] == 'answers' ) $sets[] = "`answers` = IF( (`answers` - " . esc_sql($users_incr_stat['quantity']) . ") < 0, 0, `answers` - " . esc_sql($users_incr_stat['quantity']) . " )";
279 if( $users_incr_stat['type'] == 'comments' ) $sets[] = "`comments` = IF( (`comments` - " . esc_sql($users_incr_stat['quantity']) . ") < 0, 0, `comments` - " . esc_sql($users_incr_stat['quantity']) . " )";
280
281 }
282
283 if( !empty($sets) ){
284 $sql = "UPDATE IGNORE `".$this->wpforo->db->prefix."wpforo_profiles` SET ".implode(', ', $sets)." WHERE `userid` = " . intval($users_incr_stat['userid']);
285 $this->wpforo->db->query($sql);
286 }
287 }
288 }
289
290 #################################################################################
291 /**
292 * Delete topic from DB
293 *
294 * Returns true if successfully deleted or false.
295 *
296 * @since 1.0.0
297 *
298 * @return bool
299 */
300
301 function delete($topicid = 0){
302 if(!$topicid && isset( $_REQUEST['id'] ) ) $topicid = intval($_REQUEST['id']);
303
304 if( !$topic = $this->get_topic($topicid) ) return true;
305 $diff = current_time( 'timestamp', 1 ) - strtotime($topic['created']);
306 if( !($this->wpforo->perm->forum_can('dt', $topic['forumid']) || ($this->wpforo->current_userid == $topic['userid'] && $this->wpforo->perm->forum_can('dot', $topic['forumid']) && $diff < $this->wpforo->post_options['dot_durr'])) ){
307 $this->wpforo->notice->add('You haven\'t permission to delete topic from this forum', 'error');
308 return FALSE;
309 }
310
311 if( $topicid = intval($topicid) ){
312 if( $forumid = $topic['forumid'] ){
313
314 $questions = '';
315 $forum = $this->wpforo->forum->get_forum($forumid);
316 if( isset($forum['cat_layout']) && $forum['cat_layout'] == 3 ){
317 $questions = ', `questions` = `questions` - 1 ';
318 $this->users_stats_incr_minus($topicid);
319 }
320
321 if( $post_ids = $this->wpforo->db->get_col(
322 $this->wpforo->db->prepare( "SELECT postid FROM {$this->wpforo->db->prefix}wpforo_posts WHERE topicid = %d", $topicid )
323 )){
324 $this->wpforo->db->query(
325 "DELETE FROM {$this->wpforo->db->prefix}wpforo_likes WHERE postid IN(".implode(',', array_map('intval', $post_ids) ).")"
326 );
327 $this->wpforo->db->query(
328 "DELETE FROM {$this->wpforo->db->prefix}wpforo_votes WHERE postid IN(".implode(',', array_map('intval', $post_ids) ).")"
329 );
330 }
331
332 //Find and delete default atatchments before deleting post
333 $this->delete_attachments( $topicid );
334
335 //Delete post
336 $posts_count = $this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_posts', array( 'topicid' => $topicid));
337 if($this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_topics', array( 'topicid' => $topicid))){
338 $this->wpforo->db->delete(
339 $this->wpforo->db->prefix.'wpforo_views', array( 'topicid' => $topicid ), array( '%d' )
340 );
341 if($this->wpforo->db->query( "UPDATE IGNORE " . $this->wpforo->db->prefix . "wpforo_forums SET `topics` = IF( (`topics` - 1) < 0, 0, `topics` - 1 ), `posts` = IF( (`posts` - ".intval($posts_count).") < 0, 0, `posts` - ".intval($posts_count)." ) WHERE `forumid` = " . intval($forumid))){
342 $this->wpforo->db->query( "UPDATE IGNORE `" . $this->wpforo->db->prefix . "wpforo_profiles` SET `posts` = IF( (`posts` - " . intval($posts_count) .") < 0, 0, `posts` - ".intval($posts_count)." ) $questions WHERE `userid` = " . intval($topic['userid']) );
343 $this->wpforo->member->reset($topic['userid']);
344 $this->wpforo->forum->rebuild_last_infos($forumid);
345 $this->wpforo->notice->add('This topic successfully deleted', 'success');
346 do_action( 'wpforo_after_delete_topic', $topic );
347 return TRUE;
348 }else{
349 $this->wpforo->notice->add('Topic delete error', 'error');
350 return FALSE;
351 }
352 }
353 }
354
355 }elseif( isset($_REQUEST['ids']) && !empty($_REQUEST['ids']) ){
356 $topicids = explode(',',$_REQUEST['ids']);
357 if(is_array($topicids)){
358 foreach($topicids as $topicid){
359 $topic = $this->get_topic($topicid);
360 if( $forumid = $topic['forumid'] ){
361
362 if( $post_ids = $this->wpforo->db->get_col(
363 $this->wpforo->db->prepare( "SELECT postid FROM {$this->wpforo->db->prefix}wpforo_posts WHERE topicid = %d", $topicid )
364 )){
365 $this->wpforo->db->query(
366 "DELETE FROM {$this->wpforo->db->prefix}wpforo_likes WHERE postid IN(".implode(',', array_map('intval', $post_ids) ).")"
367 );
368 $this->wpforo->db->query(
369 "DELETE FROM {$this->wpforo->db->prefix}wpforo_votes WHERE postid IN(".implode(',', array_map('intval', $post_ids) ).")"
370 );
371 }
372
373 $questions = '';
374 $forum = $this->wpforo->forum->get_forum($forumid);
375 if( isset($forum['cat_layout']) && $forum['cat_layout'] == 3 ){
376 $questions = ', `questions` = `questions` - 1 ';
377 $this->users_stats_incr_minus($topicid);
378 }
379 // It's a same code (deleting one topic)
380 $posts_count = $this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_posts', array( 'topicid' => $topicid));
381 if($this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_topics', array( 'topicid' => $topicid))){
382 $this->wpforo->db->delete(
383 $this->wpforo->db->prefix.'wpforo_views', array( 'topicid' => $topicid ), array( '%d' )
384 );
385 $this->wpforo->db->query( "UPDATE IGNORE " . $this->wpforo->db->prefix . "wpforo_forums SET `topics` = IF( (`topics` - 1) < 0, 0, `topics` - 1 ), `posts` = IF( (`posts` - ".intval($posts_count).") < 0, 0, `posts` - ".intval($posts_count)." ) WHERE `forumid` = " . intval($forumid));
386 $this->wpforo->db->query( "UPDATE IGNORE `" . $this->wpforo->db->prefix . "wpforo_profiles` SET `posts` = IF( (`posts` - ".intval($posts_count).") < 0, 0, `posts` - ".intval($posts_count)." ) $questions WHERE `userid` = " . intval($topic['userid']) );
387 $this->wpforo->forum->rebuild_last_infos($forumid);
388 do_action( 'wpforo_after_delete_topic', $topic );
389 }
390 }
391 }
392 $this->wpforo->notice->add('All Checked topics successfully deleted', 'success');
393 do_action( 'wpforo_after_bulk_delete_topics', $topicids );
394 return TRUE;
395 }
396
397 $this->wpforo->notice->add('Topics delete error', 'error');
398 return FALSE;
399
400 }
401
402 $this->wpforo->notice->add('Topics delete error', 'error');
403 return FALSE;
404 }
405
406 #################################################################################
407 /**
408 * array get_topic(array or id(num))
409 *
410 * Returns array from defined and default arguments.
411 *
412 * @since 1.0.0
413 *
414 * @param mixed defined arguments array for returning
415 *
416 * @return array
417 */
418
419 function get_topic( $args = array(), $cache = false){
420 if( !$args ) return;
421
422 if(is_array($args)){
423 $default = array(
424 'topicid' => NULL,
425 'slug' => '',
426 );
427 }elseif(is_numeric($args)){
428 $default = array(
429 'topicid' => $args,
430 'slug' => '',
431 );
432 }elseif(!is_numeric($args)){
433 $default = array(
434 'topicid' => NULL,
435 'slug' => $args,
436 );
437 }
438
439 $args = wpforo_parse_args( $args, $default );
440
441 if(!empty($args['topicid'])){
442 if( !empty(self::$cache['topic'][$args['topicid']]) ){
443 return self::$cache['topic'][$args['topicid']];
444 }
445 }
446
447 if(!empty($args['slug'])){
448 if( !empty(self::$cache['topic'][addslashes($args['slug'])]) ){
449 return self::$cache['topic'][addslashes($args['slug'])];
450 }
451 }
452
453 if(!empty($args)){
454 extract($args, EXTR_OVERWRITE);
455
456 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_topics`";
457 $wheres = array();
458 if($topicid != NULL) $wheres[] = "`topicid` = " . intval($topicid);
459 if($slug != '') $wheres[] = "`slug` = '" . esc_sql($slug) . "'";
460
461 if(!empty($wheres)){
462 $sql .= " WHERE " . implode($wheres, " AND ");
463 }
464
465 $topic = $this->wpforo->db->get_row($sql, ARRAY_A);
466
467 if( isset($topic['private']) && $topic['private'] && !wpforo_is_owner($topic['userid']) ){
468 if( isset($topic['forumid']) && $topic['forumid'] && !$this->wpforo->perm->forum_can('vp', $topic['forumid']) ){
469 return array();
470 }
471 }
472
473 if( isset($topic['status']) && $topic['status'] && !wpforo_is_owner($topic['userid'])){
474 if( isset($topic['forumid']) && $topic['forumid'] && !$this->wpforo->perm->forum_can('au', $topic['forumid']) ){
475 return array();
476 }
477 }
478
479 if($cache){
480 self::$cache['topic'][addslashes($topic['slug'])] = $topic;
481 return self::$cache['topic'][$topic['topicid']] = $topic;
482 }
483 else{
484 return $topic;
485 }
486 }
487
488 }
489 /**
490 * array get_topic(array or id(num))
491 * Returns merged arguments array from defined and default arguments.
492 *
493 * @since 1.0.0
494 *
495 * @param array defined arguments array for returning
496 *
497 * @return associative array where count is topic count and other numeric arrays with topic
498 */
499 function get_topics($args = array(), &$items_count = 0){
500 $default = array(
501 'include' => array(), // array( 2, 10, 25 )
502 'exclude' => array(), // array( 2, 10, 25 )
503 'forumids' => array(),
504 'forumid' => NULL,
505 'userid' => NULL, // user id in DB
506 'type' => 0, //0, 1, etc . . .
507 'status' => NULL, //0, 1, etc . . .
508 'private' => NULL, //0, 1, etc . . .
509 'orderby' => 'type, topicid', // type, topicid, modified, created
510 'order' => 'DESC', // ASC DESC
511 'offset' => NULL, // this use when you give row_count
512 'row_count' => NULL // 4 or 1 ...
513 );
514
515 $args = wpforo_parse_args( $args, $default );
516 if(is_array($args) && !empty($args)){
517 extract($args, EXTR_OVERWRITE);
518
519 if( $row_count === 0 ) return array();
520
521 $include = wpforo_parse_args( $include );
522 $exclude = wpforo_parse_args( $exclude );
523 $forumids = wpforo_parse_args( $forumids );
524
525 $wheres = array();
526
527 if(!empty($include)) $wheres[] = "`topicid` IN(" . implode(', ', array_map('intval', $include)) . ")";
528 if(!empty($exclude)) $wheres[] = "`topicid` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
529 if(!empty($forumids)) $wheres[] = "`forumid` IN(" . implode(', ', array_map('intval', $forumids)) . ")";
530 if(!is_null($forumid)) $wheres[] = "`forumid` = " . intval($forumid);
531 if(!is_null($userid)) $wheres[] = "`userid` = " . intval($userid);
532 if($type != 0) $wheres[] = " `type` = " . intval($type);
533
534 if(empty($forumids)){
535 if( isset($forumid) && !$this->wpforo->perm->forum_can('vf', $forumid) ){
536 return array();
537 }
538 }
539
540 if( isset($forumid) && $forumid ){
541 if( $this->wpforo->perm->forum_can('vp', $forumid) ){
542 if(!is_null($private)) $wheres[] = " `private` = " . intval($private);
543 }
544 elseif( isset($this->wpforo->current_userid) && $this->wpforo->current_userid ){
545 $wheres[] = " ( `private` = 0 OR (`private` = 1 AND `userid` = " .intval($this->wpforo->current_userid). ") )";
546 }
547 else{
548 $wheres[] = " `private` = 0";
549 }
550 }
551
552 if( isset($forumid) && $forumid ){
553 if( $this->wpforo->perm->forum_can('au', $forumid) ){
554 if(!is_null($status)) $wheres[] = " `status` = " . intval($status);
555 }
556 elseif( isset($this->wpforo->current_userid) && $this->wpforo->current_userid ){
557 $wheres[] = " ( `status` = 0 OR (`status` = 1 AND `userid` = " .intval($this->wpforo->current_userid). ") )";
558 }
559 else{
560 $wheres[] = " `status` = 0";
561 }
562 }
563
564 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_topics`";
565 if(!empty($wheres)){
566 $sql .= " WHERE " . implode($wheres, " AND ");
567 }
568
569 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
570 if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql);
571
572 $sql .= " ORDER BY " . str_replace(',', ' ' . esc_sql($order) . ',', esc_sql($orderby)) . " " . esc_sql($order);
573
574 if(!is_null($row_count)){
575 if(!is_null($offset)){
576 $sql .= esc_sql(" LIMIT $offset,$row_count");
577 }else{
578 $sql .= esc_sql(" LIMIT $row_count");
579 }
580 }
581
582 if(!empty($forumids) || !$forumid){
583 $topics = $this->wpforo->db->get_results($sql, ARRAY_A);
584 foreach($topics as $key => $topic){
585 if( isset($topic['private']) && $topic['private'] && !wpforo_is_owner($topic['userid']) ){
586 if( !$this->wpforo->perm->forum_can('vp', $topic['forumid']) ){
587 unset($topics[$key]);
588 }
589 }
590 if( isset($topic['status']) && $topic['status'] && !wpforo_is_owner($topic['userid']) ){
591 if( !$this->wpforo->perm->forum_can('au', $topic['forumid']) ){
592 unset($topics[$key]);
593 }
594 }
595 }
596 return $topics;
597 }
598 else{
599 return $this->wpforo->db->get_results($sql, ARRAY_A);
600 }
601 }
602 }
603 /**
604 *
605 * Search in your chosen column and return array with needles
606 *
607 * @since 1.0.0
608 *
609 * @param string needle
610 *
611 * @param column name in db ( slug, title, body )
612 *
613 * @param $additional if it's true' return multi-dimensional arrays, if false it return simple array
614 *
615 * @return array with matches
616 */
617
618 function search( $needle = '', $fields = array( 'title', 'body' )){
619 if($needle != ''){
620
621 $needle = stripslashes($needle);
622
623 if(!is_array($fields)){
624 $fields = array($fields); // if is it string it will be convert to array
625 }
626
627 $topicids = array();
628 foreach($fields as $field){
629 if($field == 'body'){
630 $matches = $this->wpforo->db->get_col( "SELECT `topicid` FROM ".$this->wpforo->db->prefix."wpforo_posts WHERE `".esc_sql($field)."` LIKE '%". esc_sql(sanitize_text_field($needle)) ."%'" );
631 }else{
632 $matches = $this->wpforo->db->get_col( "SELECT `topicid` FROM ".$this->wpforo->db->prefix."wpforo_topics WHERE `".esc_sql($field)."`LIKE '%". esc_sql(sanitize_text_field($needle)) ."%'" );
633 }
634 $topicids = array_merge( $topicids, $matches );
635 }
636 return array_unique($topicids);
637 }
638 else{
639 return $matches = array();
640 }
641 }
642
643 function get_sum_answer($forumids){
644 $sum = $this->wpforo->db->get_var("SELECT SUM(`answers`) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `forumid` IN(". implode(', ', array_map('intval', $forumids)) .")");
645 if($sum) return $sum;
646 return 0;
647 }
648
649 function get_forumslug($forumid){
650 $slug = $this->wpforo->db->get_var("SELECT `slug` FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `forumid` = " . intval($forumid));
651 if($slug) return $slug;
652 return 0;
653 }
654
655 function get_forumslug_byid($topicid){
656 $slug = $this->wpforo->db->get_var("SELECT `slug` FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `forumid` =(SELECT forumid FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `topicid` =".intval($topicid).")");
657 if($slug) return $slug;
658 return 0;
659 }
660
661 function is_sticky( $topicid ){
662 $type = $this->wpforo->db->get_var( "SELECT `type` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
663 if( $type == 1 ) return TRUE;
664 return FALSE;
665 }
666
667 function is_private( $topicid ){
668 $private = $this->wpforo->db->get_var( "SELECT `private` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
669 if( $private == 1 ) return TRUE;
670 return FALSE;
671 }
672
673 function is_unapproved( $topicid ){
674 $status = $this->wpforo->db->get_var( "SELECT `status` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
675 if( $status == 1 ) return TRUE;
676 return FALSE;
677 }
678
679 function is_closed( $topicid ){
680 $type = $this->wpforo->db->get_var( "SELECT `closed` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
681 if( $type == 1 ) return TRUE;
682 return FALSE;
683 }
684
685 function is_solved( $topicid ){
686 $post = $this->wpforo->db->get_var( "SELECT `postid` FROM " . $this->wpforo->db->prefix."wpforo_posts WHERE `is_answer` = 1 AND `topicid` = " . intval($topicid) . " LIMIT 1" );
687 if( $post ) return TRUE;
688 return FALSE;
689 }
690
691 /**
692 * move topic to another forum
693 *
694 * @since 1.0.0
695 *
696 * @param topicid and new forumid
697 *
698 * @return false and true
699 */
700 function move($topicid, $forumid){
701 $topic = $this->get_topic( $topicid );
702 if( $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_topics` SET `forumid` = ". intval($forumid) ." WHERE `topicid` = ". intval($topicid) ) ){
703 $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_posts` SET `forumid` = ". intval($forumid) ." WHERE `topicid` = ". intval($topicid) );
704 $post = $this->wpforo->post->get_post($topic['last_post']);
705
706 $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_forums` SET `topics` = `topics` - 1, `posts` = `posts` - ".intval($topic['posts'])." WHERE `forumid` = ".intval($topic['forumid']) );
707 $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_forums` SET `topics` = `topics` + 1, `posts` = `posts` + ".intval($topic['posts']).", `last_topicid` = ".intval($topicid).", `last_postid` = ".intval($topic['last_post']).", `last_userid` = ".intval($post['userid']).", `last_post_date` = '". esc_sql($post['created']) ."' WHERE `forumid` = ". intval($forumid) );
708
709 $this->wpforo->forum->rebuild_last_infos($topic['forumid']);
710
711 $this->wpforo->notice->add('Topic successfully moved', 'success');
712 return $topicid;
713 }
714
715 $this->wpforo->notice->add('Topic Move Error', 'error');
716 return FALSE;
717 }
718
719 function get_posts_count($topicid){
720 if($topicid){
721 return $this->wpforo->db->get_var("SELECT `posts` FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `topicid` = " . intval($topicid));
722 }
723 else{
724 return $this->wpforo->db->get_var("SELECT `posts` FROM `".$this->wpforo->db->prefix."wpforo_topics`");
725 }
726 }
727
728 function get_topic_url($topic, $forum = array()){
729
730 if( !is_array($topic) ) $topic = $this->get_topic( $topic );
731
732 if( is_array($topic) && !empty($topic) ){
733
734 if( is_array($forum) && !empty($forum)){
735 $forum_slug = $forum['slug'];
736 }
737 else{
738
739 if( isset($topic['forumid']) && !$topic['forumid'] ){
740 if( isset(self::$cache['forum_slug'][$topic['forumid']]) ){
741 $forum_slug = self::$cache['forum_slug'][$topic['forumid']];
742 }
743 else{
744 $forum_slug = $this->get_forumslug($topic['forumid']);
745 }
746 self::$cache['forum_slug'][$topic['forumid']] = $forum_slug;
747 }
748 else{
749 $forum_slug = $this->get_forumslug_byid($topic['topicid']);
750 }
751
752 }
753
754 return WPFORO_BASE_URL . $forum_slug . '/' . $topic['slug'];
755
756 }else{
757 return WPFORO_BASE_URL;
758 }
759 }
760
761
762 function get_count(){
763 return $this->wpforo->db->get_var( "SELECT COUNT(`topicid`) FROM `".$this->wpforo->db->prefix."wpforo_topics`" );
764 }
765
766 public function status( $topicid, $status ){
767 if( !$topicid = wpforo_bigintval($topicid) ) return false;
768
769 if( false !== $this->wpforo->db->update(
770 $this->wpforo->db->prefix."wpforo_topics",
771 array( 'status' => intval($status) ),
772 array( 'topicid' => $topicid ),
773 array( '%d' ),
774 array( '%d' )
775 )){
776 if( false !== $this->wpforo->db->update(
777 $this->wpforo->db->prefix."wpforo_posts",
778 array( 'status' => intval($status) ),
779 array( 'topicid' => $topicid ),
780 array( '%d' ),
781 array( '%d' )
782 )){
783 $this->wpforo->notice->add('Done!', 'success');
784 return true;
785 }
786 }
787
788 $this->wpforo->notice->add('error: Change Status action', 'error');
789 return false;
790 }
791
792 public function delete_attachments( $topicid ){
793 $args = array( 'topicid' => $topicid );
794 $posts = $this->wpforo->post->get_posts( $args );
795 if(!empty($posts)){
796 foreach( $posts as $post ){
797 $this->wpforo->post->delete_attachments( $post['postid'] );
798 }
799 }
800 }
801
802 }
803 ?>