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

864 lines 31.4 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) && $type) ? 1 : 0),
98 'status' => ((isset($status) && $status) ? $status : 0),
99 'private' => ((isset($private) && $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', $args);
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', $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($topicid, 'topic', $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 $this->wpforo->current_object['status'] = 'unapproved';
482 return array();
483 }
484 }
485
486 if($cache){
487 self::$cache['topic'][addslashes($topic['slug'])] = $topic;
488 return self::$cache['topic'][$topic['topicid']] = $topic;
489 }
490 else{
491 return $topic;
492 }
493 }
494
495 }
496 /**
497 * array get_topic(array or id(num))
498 * Returns merged arguments array from defined and default arguments.
499 *
500 * @since 1.0.0
501 *
502 * @param array defined arguments array for returning
503 *
504 * @return associative array where count is topic count and other numeric arrays with topic
505 */
506 function get_topics($args = array(), &$items_count = 0 ){
507
508 $cache = $this->wpforo->cache->on('object_cashe');
509
510 $default = array(
511 'include' => array(), // array( 2, 10, 25 )
512 'exclude' => array(), // array( 2, 10, 25 )
513 'forumids' => array(),
514 'forumid' => NULL,
515 'userid' => NULL, // user id in DB
516 'type' => 0, //0, 1, etc . . .
517 'status' => NULL, //0, 1, etc . . .
518 'private' => NULL, //0, 1, etc . . .
519 'orderby' => 'type, topicid', // type, topicid, modified, created
520 'order' => 'DESC', // ASC DESC
521 'offset' => NULL, // this use when you give row_count
522 'row_count' => NULL // 4 or 1 ...
523 );
524
525 $args = wpforo_parse_args( $args, $default );
526 if(is_array($args) && !empty($args)){
527
528 extract($args, EXTR_OVERWRITE);
529
530 if( $row_count === 0 ) return array();
531
532 $include = wpforo_parse_args( $include );
533 $exclude = wpforo_parse_args( $exclude );
534 $forumids = wpforo_parse_args( $forumids );
535
536 $wheres = array();
537
538 if(!empty($include)) $wheres[] = "`topicid` IN(" . implode(', ', array_map('intval', $include)) . ")";
539 if(!empty($exclude)) $wheres[] = "`topicid` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
540 if(!empty($forumids)) $wheres[] = "`forumid` IN(" . implode(', ', array_map('intval', $forumids)) . ")";
541 if(!is_null($forumid)) $wheres[] = "`forumid` = " . intval($forumid);
542 if(!is_null($userid)) $wheres[] = "`userid` = " . intval($userid);
543 if($type != 0) $wheres[] = " `type` = " . intval($type);
544
545 if(empty($forumids)){
546 if( isset($forumid) && !$this->wpforo->perm->forum_can('vf', $forumid) ){
547 return array();
548 }
549 }
550
551 if( isset($forumid) && $forumid ){
552 if( $this->wpforo->perm->forum_can('vp', $forumid) ){
553 if(!is_null($private)) $wheres[] = " `private` = " . intval($private);
554 }
555 elseif( isset($this->wpforo->current_userid) && $this->wpforo->current_userid ){
556 $wheres[] = " ( `private` = 0 OR (`private` = 1 AND `userid` = " .intval($this->wpforo->current_userid). ") )";
557 }
558 else{
559 $wheres[] = " `private` = 0";
560 }
561 }
562
563 if( isset($forumid) && $forumid ){
564 if( $this->wpforo->perm->forum_can('au', $forumid) ){
565 if(!is_null($status)) $wheres[] = " `status` = " . intval($status);
566 }
567 elseif( isset($this->wpforo->current_userid) && $this->wpforo->current_userid ){
568 $wheres[] = " ( `status` = 0 OR (`status` = 1 AND `userid` = " .intval($this->wpforo->current_userid). ") )";
569 }
570 else{
571 $wheres[] = " `status` = 0";
572 }
573 }
574
575 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_topics`";
576 if(!empty($wheres)){
577 $sql .= " WHERE " . implode($wheres, " AND ");
578 }
579
580 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
581 if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql);
582
583 $sql .= " ORDER BY " . str_replace(',', ' ' . esc_sql($order) . ',', esc_sql($orderby)) . " " . esc_sql($order);
584
585 if(!is_null($row_count)){
586 if(!is_null($offset)){
587 $sql .= esc_sql(" LIMIT $offset,$row_count");
588 }else{
589 $sql .= esc_sql(" LIMIT $row_count");
590 }
591 }
592
593 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']; }}
594
595 $topics = $this->wpforo->db->get_results($sql, ARRAY_A);
596 $topics = apply_filters('wpforo_get_topics', $topics);
597
598 if(!empty($forumids) || !$forumid){
599 foreach($topics as $key => $topic){
600 if( !$this->wpforo->perm->forum_can('vf', $topic['forumid']) ){
601 unset($topics[$key]);
602 }
603 if( isset($topics[$key]) && isset($topic['private']) && $topic['private'] && !wpforo_is_owner($topic['userid']) ){
604 if( !$this->wpforo->perm->forum_can('vp', $topic['forumid']) ){
605 unset($topics[$key]);
606 }
607 }
608 if( isset($topics[$key]) && isset($topic['status']) && $topic['status'] && !wpforo_is_owner($topic['userid']) ){
609 if( !$this->wpforo->perm->forum_can('au', $topic['forumid']) ){
610 unset($topics[$key]);
611 }
612 }
613 }
614 }
615
616 if($cache && isset($object_key) && !empty($topics)){
617 self::$cache['topics'][$object_key]['items'] = $topics;
618 self::$cache['topics'][$object_key]['items_count'] = $items_count;
619 }
620 return $topics;
621 }
622 }
623
624 function get_topics_filtered( $args = array() ){
625 $topics = array();
626 $topics = $this->get_topics( $args );
627 if( !empty($topics) ){
628 foreach($topics as $key => $topic){
629 if( !$this->wpforo->perm->forum_can('vf', $topic['forumid']) ){
630 unset($topics[$key]);
631 }
632 if( isset($topics[$key]) && isset($topic['private']) && $topic['private'] && !wpforo_is_owner($topic['userid']) ){
633 if( !$this->wpforo->perm->forum_can('vp', $topic['forumid']) ){
634 unset($topics[$key]);
635 }
636 }
637 if( isset($topics[$key]) && isset($topic['status']) && $topic['status'] && !wpforo_is_owner($topic['userid']) ){
638 if( !$this->wpforo->perm->forum_can('au', $topic['forumid']) ){
639 unset($topics[$key]);
640 }
641 }
642 }
643 }
644 return $topics;
645 }
646
647 /**
648 *
649 * Search in your chosen column and return array with needles
650 *
651 * @since 1.0.0
652 *
653 * @param string needle
654 *
655 * @param column name in db ( slug, title, body )
656 *
657 * @param $additional if it's true' return multi-dimensional arrays, if false it return simple array
658 *
659 * @return array with matches
660 */
661
662 function search( $needle = '', $fields = array( 'title', 'body' )){
663 if($needle != ''){
664
665 $needle = stripslashes($needle);
666
667 if(!is_array($fields)){
668 $fields = array($fields); // if is it string it will be convert to array
669 }
670
671 $topicids = array();
672 foreach($fields as $field){
673 if($field == 'body'){
674 $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)) ."%'" );
675 }else{
676 $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)) ."%'" );
677 }
678 $topicids = array_merge( $topicids, $matches );
679 }
680 return array_unique($topicids);
681 }
682 else{
683 return $matches = array();
684 }
685 }
686
687 function get_sum_answer($forumids){
688 $sum = $this->wpforo->db->get_var("SELECT SUM(`answers`) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `forumid` IN(". implode(', ', array_map('intval', $forumids)) .")");
689 if($sum) return $sum;
690 return 0;
691 }
692
693 function get_forumslug($forumid){
694 $slug = $this->wpforo->db->get_var("SELECT `slug` FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `forumid` = " . intval($forumid));
695 if($slug) return $slug;
696 return 0;
697 }
698
699 function get_forumslug_byid($topicid){
700 $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).")");
701 if($slug) return $slug;
702 return 0;
703 }
704
705 function is_sticky( $topicid ){
706 if( $this->wpforo->cache->on('object_cashe') ){
707 $type = wpforo_topic($topicid, 'type');
708 }
709 else{
710 $type = $this->wpforo->db->get_var( "SELECT `type` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
711 }
712 if( $type == 1 ) return TRUE;
713 return FALSE;
714 }
715
716 function is_private( $topicid ){
717 if( $this->wpforo->cache->on('object_cashe') ){
718 $private = wpforo_topic($topicid, 'private');
719 }
720 else{
721 $private = $this->wpforo->db->get_var( "SELECT `private` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
722 }
723 if( $private == 1 ) return TRUE;
724 return FALSE;
725 }
726
727 function is_unapproved( $topicid ){
728 if( $this->wpforo->cache->on('object_cashe') ){
729 $status = wpforo_topic($topicid, 'status');
730 }
731 else{
732 $status = $this->wpforo->db->get_var( "SELECT `status` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
733 }
734 if( $status == 1 ) return TRUE;
735 return FALSE;
736 }
737
738 function is_closed( $topicid ){
739 if( $this->wpforo->cache->on('object_cashe') ){
740 $type = wpforo_topic($topicid, 'closed');
741 }
742 else{
743 $type = $this->wpforo->db->get_var( "SELECT `closed` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) );
744 }
745 if( $type == 1 ) return TRUE;
746 return FALSE;
747 }
748
749 function is_solved( $topicid ){
750 $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" );
751 if( $postid ) return TRUE;
752 return FALSE;
753 }
754
755 /**
756 * move topic to another forum
757 *
758 * @since 1.0.0
759 *
760 * @param topicid and new forumid
761 *
762 * @return false and true
763 */
764 function move($topicid, $forumid){
765 $topic = $this->get_topic( $topicid );
766 if( $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_topics` SET `forumid` = ". intval($forumid) ." WHERE `topicid` = ". intval($topicid) ) ){
767 $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_posts` SET `forumid` = ". intval($forumid) ." WHERE `topicid` = ". intval($topicid) );
768 $post = $this->wpforo->post->get_post($topic['last_post']);
769
770 $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']) );
771 $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) );
772
773 $this->wpforo->forum->rebuild_last_infos($topic['forumid']);
774
775 wpforo_clean_cache( $topicid, 'topic', $topic);
776 $this->wpforo->notice->add('Topic successfully moved', 'success');
777 return $topicid;
778 }
779
780 $this->wpforo->notice->add('Topic Move Error', 'error');
781 return FALSE;
782 }
783
784 function get_topic_url($topic, $forum = array()){
785
786 if( !is_array($topic) ) $topic = $this->get_topic( $topic );
787
788 if( is_array($topic) && !empty($topic) ){
789
790 if( is_array($forum) && !empty($forum)){
791 $forum_slug = $forum['slug'];
792 }
793 else{
794
795 if( isset($topic['forumid']) && !$topic['forumid'] ){
796 if( isset(self::$cache['forum_slug'][$topic['forumid']]) ){
797 $forum_slug = self::$cache['forum_slug'][$topic['forumid']];
798 }
799 else{
800 $forum_slug = $this->get_forumslug($topic['forumid']);
801 }
802 self::$cache['forum_slug'][$topic['forumid']] = $forum_slug;
803 }
804 else{
805 $forum_slug = $this->get_forumslug_byid($topic['topicid']);
806 }
807
808 }
809
810 return wpforo_home_url( $forum_slug . '/' . $topic['slug'] );
811
812 }else{
813 return wpforo_home_url();
814 }
815 }
816
817
818 function get_count( $args = array() ){
819 $sql = "SELECT COUNT(`topicid`) FROM `".$this->wpforo->db->prefix."wpforo_topics`";
820 if( !empty($args) ){
821 $wheres = array();
822 foreach ($args as $key => $value) $wheres[] = "`$key` = " . intval($value);
823 if($wheres) $sql .= " WHERE " . implode(' AND ', $wheres);
824 }
825 return $this->wpforo->db->get_var($sql);
826 }
827
828 public function status( $topicid, $status ){
829 if( !$topicid = wpforo_bigintval($topicid) ) return false;
830
831 if( false !== $this->wpforo->db->update(
832 $this->wpforo->db->prefix."wpforo_topics",
833 array( 'status' => intval($status) ),
834 array( 'topicid' => $topicid ),
835 array( '%d' ),
836 array( '%d' )
837 )){
838 if( false !== $this->wpforo->db->update(
839 $this->wpforo->db->prefix."wpforo_posts",
840 array( 'status' => intval($status) ),
841 array( 'topicid' => $topicid ),
842 array( '%d' ),
843 array( '%d' )
844 )){
845 $this->wpforo->notice->add('Done!', 'success');
846 return true;
847 }
848 }
849
850 $this->wpforo->notice->add('error: Change Status action', 'error');
851 return false;
852 }
853
854 public function delete_attachments( $topicid ){
855 $args = array( 'topicid' => $topicid );
856 $posts = $this->wpforo->post->get_posts( $args );
857 if(!empty($posts)){
858 foreach( $posts as $post ){
859 $this->wpforo->post->delete_attachments( $post['postid'] );
860 }
861 }
862 }
863
864 }