| 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( $args['forumid'], 'ct') ){ |
| 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($topic['forumid'], 'et') || ($this->wpforo->current_userid == $topic['userid'] && $this->wpforo->perm->forum_can($topic['forumid'], 'eot') && $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($post['forumid'], 'dt') || ($this->wpforo->current_userid == $topic['userid'] && $this->wpforo->perm->forum_can($post['forumid'], 'dot') && $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 |
$posts_count = $this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_posts', array( 'topicid' => $topicid)); |
| 289 |
if($this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_topics', array( 'topicid' => $topicid))){ |
| 290 |
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))){ |
| 291 |
$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']) ); |
| 292 |
$this->wpforo->member->reset($topic['userid']); |
| 293 |
$this->wpforo->forum->rebuild_last_infos($forumid); |
| 294 |
$this->wpforo->notice->add('This topic successfully deleted', 'success'); |
| 295 |
do_action( 'wpforo_after_delete_topic', $topic ); |
| 296 |
return TRUE; |
| 297 |
}else{ |
| 298 |
$this->wpforo->notice->add('Topic delete error', 'error'); |
| 299 |
return FALSE; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
}elseif( isset($_REQUEST['ids']) && !empty($_REQUEST['ids']) ){ |
| 305 |
$topicids = explode(',',$_REQUEST['ids']); |
| 306 |
if(is_array($topicids)){ |
| 307 |
foreach($topicids as $topicid){ |
| 308 |
$topic = $this->get_topic($topicid); |
| 309 |
if( $forumid = $topic['forumid'] ){ |
| 310 |
|
| 311 |
$questions = ''; |
| 312 |
$forum = $this->wpforo->forum->get_forum($forumid); |
| 313 |
if( isset($forum['cat_layout']) && $forum['cat_layout'] == 3 ){ |
| 314 |
$questions = ', `questions` = `questions` - 1 '; |
| 315 |
$this->users_stats_incr_minus($topicid); |
| 316 |
} |
| 317 |
// It's a same code (deleting one topic) |
| 318 |
$posts_count = $this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_posts', array( 'topicid' => $topicid)); |
| 319 |
if($this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_topics', array( 'topicid' => $topicid))){ |
| 320 |
$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)); |
| 321 |
$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']) ); |
| 322 |
$this->wpforo->forum->rebuild_last_infos($forumid); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
$this->wpforo->notice->add('All Checked topics successfully deleted', 'success'); |
| 327 |
do_action( 'wpforo_after_bulk_delete_topics', $topicids ); |
| 328 |
return TRUE; |
| 329 |
} |
| 330 |
|
| 331 |
$this->wpforo->notice->add('Topics delete error', 'error'); |
| 332 |
return FALSE; |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
$this->wpforo->notice->add('Topics delete error', 'error'); |
| 337 |
return FALSE; |
| 338 |
} |
| 339 |
|
| 340 |
################################################################################# |
| 341 |
/** |
| 342 |
* array get_topic(array or id(num)) |
| 343 |
* |
| 344 |
* Returns array from defined and default arguments. |
| 345 |
* |
| 346 |
* @since 1.0.0 |
| 347 |
* |
| 348 |
* @param mixed defined arguments array for returning |
| 349 |
* |
| 350 |
* @return array |
| 351 |
*/ |
| 352 |
|
| 353 |
function get_topic( $args = array(), $cache = false){ |
| 354 |
if( !$args ) return; |
| 355 |
|
| 356 |
if(is_array($args)){ |
| 357 |
$default = array( |
| 358 |
'topicid' => NULL, |
| 359 |
'slug' => '', |
| 360 |
); |
| 361 |
}elseif(is_numeric($args)){ |
| 362 |
$default = array( |
| 363 |
'topicid' => $args, |
| 364 |
'slug' => '', |
| 365 |
); |
| 366 |
}elseif(is_string($args)){ |
| 367 |
$default = array( |
| 368 |
'topicid' => NULL, |
| 369 |
'slug' => $args, |
| 370 |
); |
| 371 |
} |
| 372 |
|
| 373 |
$args = wpforo_parse_args( $args, $default ); |
| 374 |
|
| 375 |
if(isset($args['topicid'])){ |
| 376 |
if( isset(self::$cache['topic'][$args['topicid']]) ){ |
| 377 |
return self::$cache['topic'][$args['topicid']]; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
if(!empty($args)){ |
| 382 |
extract($args, EXTR_OVERWRITE); |
| 383 |
|
| 384 |
$sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_topics`"; |
| 385 |
$wheres = array(); |
| 386 |
if($topicid != NULL) $wheres[] = "`topicid` = " . intval($topicid); |
| 387 |
if($slug != '') $wheres[] = "`slug` = '" . esc_sql(sanitize_title($slug)) . "'"; |
| 388 |
|
| 389 |
if(!empty($wheres)){ |
| 390 |
$sql .= " WHERE " . implode($wheres, " AND "); |
| 391 |
} |
| 392 |
|
| 393 |
$topic = $this->wpforo->db->get_row($sql, ARRAY_A); |
| 394 |
|
| 395 |
if($cache){ |
| 396 |
return self::$cache['topic'][$topicid] = $topic; |
| 397 |
} |
| 398 |
else{ |
| 399 |
return $topic; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
/** |
| 405 |
* array get_topic(array or id(num)) |
| 406 |
* Returns merged arguments array from defined and default arguments. |
| 407 |
* |
| 408 |
* @since 1.0.0 |
| 409 |
* |
| 410 |
* @param array defined arguments array for returning |
| 411 |
* |
| 412 |
* @return associative array where count is topic count and other numeric arrays with topic |
| 413 |
*/ |
| 414 |
function get_topics($args = array(), &$items_count = 0){ |
| 415 |
$default = array( |
| 416 |
'include' => array(), // array( 2, 10, 25 ) |
| 417 |
'exclude' => array(), // array( 2, 10, 25 ) |
| 418 |
'forumids' => array(), |
| 419 |
'forumid' => NULL, |
| 420 |
'userid' => NULL, // user id in DB |
| 421 |
'type' => 0, //sticki, etc . . . |
| 422 |
'orderby' => 'type, topicid', // type, topicid, modified, created |
| 423 |
'order' => 'DESC', // ASC DESC |
| 424 |
'offset' => NULL, // this use when you give row_count |
| 425 |
'row_count' => NULL // 4 or 1 ... |
| 426 |
); |
| 427 |
|
| 428 |
$args = wpforo_parse_args( $args, $default ); |
| 429 |
if(is_array($args) && !empty($args)){ |
| 430 |
extract($args, EXTR_OVERWRITE); |
| 431 |
|
| 432 |
if( $row_count === 0 ) return array(); |
| 433 |
|
| 434 |
$include = wpforo_parse_args( $include ); |
| 435 |
$exclude = wpforo_parse_args( $exclude ); |
| 436 |
$forumids = wpforo_parse_args( $forumids ); |
| 437 |
|
| 438 |
$wheres = array(); |
| 439 |
|
| 440 |
if(!empty($include)) $wheres[] = "`topicid` IN(" . implode(', ', array_map('intval', $include)) . ")"; |
| 441 |
if(!empty($exclude)) $wheres[] = "`topicid` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")"; |
| 442 |
if(!empty($forumids)) $wheres[] = "`forumid` IN(" . implode(', ', array_map('intval', $forumids)) . ")"; |
| 443 |
if(!is_null($forumid)) $wheres[] = "`forumid` = " . intval($forumid); |
| 444 |
if(!is_null($userid)) $wheres[] = "`userid` = " . intval($userid); |
| 445 |
if($type != 0) $wheres[] = " `type` = " . intval($type); |
| 446 |
|
| 447 |
$sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_topics`"; |
| 448 |
if(!empty($wheres)){ |
| 449 |
$sql .= " WHERE " . implode($wheres, " AND "); |
| 450 |
} |
| 451 |
|
| 452 |
$items_count = $this->wpforo->db->get_var(preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql)); |
| 453 |
|
| 454 |
$sql .= " ORDER BY " . str_replace(',', ' ' . esc_sql($order) . ',', esc_sql($orderby)) . " " . esc_sql($order); |
| 455 |
|
| 456 |
if(!is_null($row_count)){ |
| 457 |
if(!is_null($offset)){ |
| 458 |
$sql .= esc_sql(" LIMIT $offset,$row_count"); |
| 459 |
}else{ |
| 460 |
$sql .= esc_sql(" LIMIT $row_count"); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
return $this->wpforo->db->get_results($sql, ARRAY_A); |
| 465 |
|
| 466 |
} |
| 467 |
} |
| 468 |
/** |
| 469 |
* |
| 470 |
* Search in your chosen column and return array with needles |
| 471 |
* |
| 472 |
* @since 1.0.0 |
| 473 |
* |
| 474 |
* @param string needle |
| 475 |
* |
| 476 |
* @param column name in db ( slug, title, body ) |
| 477 |
* |
| 478 |
* @param $additional if it's true' return multi-dimensional arrays, if false it return simple array |
| 479 |
* |
| 480 |
* @return array with matches |
| 481 |
*/ |
| 482 |
|
| 483 |
function search( $needle = '', $fields = array( 'title', 'body' )){ |
| 484 |
if($needle != ''){ |
| 485 |
|
| 486 |
$needle = stripslashes($needle); |
| 487 |
|
| 488 |
if(!is_array($fields)){ |
| 489 |
$fields = array($fields); // if is it string it will be convert to array |
| 490 |
} |
| 491 |
|
| 492 |
$topicids = array(); |
| 493 |
foreach($fields as $field){ |
| 494 |
if($field == 'body'){ |
| 495 |
$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)) ."%'" ); |
| 496 |
}else{ |
| 497 |
$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)) ."%'" ); |
| 498 |
} |
| 499 |
$topicids = array_merge( $topicids, $matches ); |
| 500 |
} |
| 501 |
return array_unique($topicids); |
| 502 |
} |
| 503 |
else{ |
| 504 |
return $matches = array(); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
function get_sum_answer($forumids){ |
| 509 |
$sum = $this->wpforo->db->get_var("SELECT SUM(`answers`) FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `forumid` IN(". implode(', ', array_map('intval', $forumids)) .")"); |
| 510 |
if($sum) return $sum; |
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
function get_forumslug($forumid){ |
| 515 |
$slug = $this->wpforo->db->get_var("SELECT `slug` FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `forumid` = " . intval($forumid)); |
| 516 |
if($slug) return $slug; |
| 517 |
return 0; |
| 518 |
} |
| 519 |
|
| 520 |
function get_forumslug_byid($topicid){ |
| 521 |
$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).")"); |
| 522 |
if($slug) return $slug; |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
function is_sticky( $topicid ){ |
| 527 |
$type = $this->wpforo->db->get_var( "SELECT `type` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) ); |
| 528 |
if( $type == 1 ) return TRUE; |
| 529 |
return FALSE; |
| 530 |
} |
| 531 |
|
| 532 |
function is_closed( $topicid ){ |
| 533 |
$type = $this->wpforo->db->get_var( "SELECT `closed` FROM " . $this->wpforo->db->prefix."wpforo_topics WHERE `topicid` = " . intval($topicid) ); |
| 534 |
if( $type == 1 ) return TRUE; |
| 535 |
return FALSE; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* move topic to another forum |
| 540 |
* |
| 541 |
* @since 1.0.0 |
| 542 |
* |
| 543 |
* @param topicid and new forumid |
| 544 |
* |
| 545 |
* @return false and true |
| 546 |
*/ |
| 547 |
function move($topicid, $forumid){ |
| 548 |
$topic = $this->get_topic( $topicid ); |
| 549 |
if( $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_topics` SET `forumid` = ". intval($forumid) ." WHERE `topicid` = ". intval($topicid) ) ){ |
| 550 |
$this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_posts` SET `forumid` = ". intval($forumid) ." WHERE `topicid` = ". intval($topicid) ); |
| 551 |
$post = $this->wpforo->post->get_post($topic['last_post']); |
| 552 |
|
| 553 |
$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']) ); |
| 554 |
$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) ); |
| 555 |
|
| 556 |
$this->wpforo->forum->rebuild_last_infos($topic['forumid']); |
| 557 |
|
| 558 |
$this->wpforo->notice->add('Topic successfully moved', 'success'); |
| 559 |
return $topicid; |
| 560 |
} |
| 561 |
|
| 562 |
$this->wpforo->notice->add('Topic Move Error', 'error'); |
| 563 |
return FALSE; |
| 564 |
} |
| 565 |
|
| 566 |
function get_posts_count($topicid){ |
| 567 |
if($topicid){ |
| 568 |
return $this->wpforo->db->get_var("SELECT `posts` FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `topicid` = " . intval($topicid)); |
| 569 |
} |
| 570 |
else{ |
| 571 |
return $this->wpforo->db->get_var("SELECT `posts` FROM `".$this->wpforo->db->prefix."wpforo_topics`"); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
function get_topic_url($topic, $forum = array()){ |
| 576 |
|
| 577 |
if( !is_array($topic) ) $topic = $this->get_topic( intval($topic) ); |
| 578 |
|
| 579 |
if( is_array($topic) && !empty($topic) ){ |
| 580 |
|
| 581 |
if( is_array($forum) && !empty($forum)){ |
| 582 |
$forum_slug = $forum['slug']; |
| 583 |
} |
| 584 |
else{ |
| 585 |
|
| 586 |
if( isset($topic['forumid']) && !$topic['forumid'] ){ |
| 587 |
if( isset(self::$cache['forum_slug'][$topic['forumid']]) ){ |
| 588 |
$forum_slug = self::$cache['forum_slug'][$topic['forumid']]; |
| 589 |
} |
| 590 |
else{ |
| 591 |
$forum_slug = $this->get_forumslug($topic['forumid']); |
| 592 |
} |
| 593 |
self::$cache['forum_slug'][$topic['forumid']] = $forum_slug; |
| 594 |
} |
| 595 |
else{ |
| 596 |
$forum_slug = $this->get_forumslug_byid($topic['topicid']); |
| 597 |
} |
| 598 |
|
| 599 |
} |
| 600 |
|
| 601 |
return WPFORO_BASE_URL . $forum_slug . '/' . $topic['slug']; |
| 602 |
|
| 603 |
}else{ |
| 604 |
return WPFORO_BASE_URL; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
function get_count(){ |
| 610 |
return $this->wpforo->db->get_var( "SELECT COUNT(`topicid`) FROM `".$this->wpforo->db->prefix."wpforo_topics`" ); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
} |
| 615 |
?> |