PluginProbe
wpForo Forum / 1.3.1
wpForo Forum v1.3.1
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.3.1, at wpf-includes/class-topics.php

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