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

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