| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if( !defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
class wpForoPost{ |
| 6 |
|
| 7 |
private $wpforo; |
| 8 |
public static $cache = array( 'posts' => array(), 'post' => array(), 'item' => array(), 'topic_slug' => array(), 'forum_slug' => array(), 'post_url' => 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 |
public function add( $args = array() ){ |
| 19 |
|
| 20 |
if( empty($args) && empty($_REQUEST['post']) ){ $this->wpforo->notice->add('Reply request error', 'error'); return FALSE; } |
| 21 |
if( empty($args) && !empty($_REQUEST['post']) ){ $args = $_REQUEST['post']; $args['body'] = $_REQUEST['postbody']; } |
| 22 |
if( !isset($args['body']) || !$args['body'] ){ $this->wpforo->notice->add('Post is empty', 'error'); return FALSE; } |
| 23 |
|
| 24 |
extract($args, EXTR_OVERWRITE); |
| 25 |
|
| 26 |
if( !isset($topicid) || !$topicid ){ $this->wpforo->notice->add('Error: No topic selected', 'error'); return FALSE; } |
| 27 |
if( !$topic = $this->wpforo->topic->get_topic(intval($topicid)) ){ $this->wpforo->notice->add('Error: Topic is not found', 'error'); return FALSE; } |
| 28 |
if( !$forum = $this->wpforo->forum->get_forum(intval($topic['forumid'])) ){ $this->wpforo->notice->add('Error: Forum is not found', 'error'); return FALSE; } |
| 29 |
|
| 30 |
if( $topic['closed'] ){ |
| 31 |
$this->wpforo->notice->add('Can\'t write a post: This topic is closed', 'error'); |
| 32 |
return FALSE; |
| 33 |
} |
| 34 |
|
| 35 |
if( !$this->wpforo->perm->forum_can('cr', $topic['forumid']) ){ |
| 36 |
$this->wpforo->notice->add('You haven\'t permission to create post into this forum', 'error'); |
| 37 |
return FALSE; |
| 38 |
} |
| 39 |
|
| 40 |
do_action( 'wpforo_start_add_post', $args ); |
| 41 |
|
| 42 |
$post = $args; |
| 43 |
$post['forumid'] = $forumid = (isset($topic['forumid']) ? intval($topic['forumid']) : 0); |
| 44 |
$post['parentid'] = $parentid = (isset($parentid) ? intval($parentid) : 0); |
| 45 |
$post['title'] = $title = (isset($title) ? wpforo_text( trim($title), 250, false ) : ''); |
| 46 |
$post['body'] = $body = ( isset($body) ? preg_replace('#</pre>[\r\n\t\s\0]*<pre>#isu', "\r\n", $body) : '' ); |
| 47 |
$post['created'] = $created = ( isset($created) ? $created : current_time( 'mysql', 1 ) ); |
| 48 |
$post['userid'] = $userid = ( isset($userid) ? intval($userid) : $this->wpforo->current_userid ); |
| 49 |
|
| 50 |
$post = apply_filters('wpforo_add_post_data_filter', $post); |
| 51 |
|
| 52 |
if(empty($post)) return FALSE; |
| 53 |
|
| 54 |
extract($post, EXTR_OVERWRITE); |
| 55 |
|
| 56 |
if(isset($forumid)) $forumid = intval($forumid); |
| 57 |
if(isset($topicid)) $topicid = intval($topicid); |
| 58 |
if(isset($parentid)) $parentid = intval($parentid); |
| 59 |
if(isset($title)) $title = sanitize_text_field(trim($title)); |
| 60 |
if(isset($created)) $created = sanitize_text_field($created); |
| 61 |
if(isset($userid)) $userid = intval($userid); |
| 62 |
if(isset($body)) $body = wpforo_kses(trim($body), 'post'); |
| 63 |
if(isset($status)) $status = intval($status); |
| 64 |
|
| 65 |
do_action( 'wpforo_before_add_post', $post ); |
| 66 |
|
| 67 |
if( |
| 68 |
$this->wpforo->db->insert( |
| 69 |
$this->wpforo->db->prefix . 'wpforo_posts', |
| 70 |
array( |
| 71 |
'forumid' => $forumid, |
| 72 |
'topicid' => $topicid, |
| 73 |
'parentid' => $parentid, |
| 74 |
'userid' => $userid, |
| 75 |
'title' => stripslashes($title), |
| 76 |
'body' => stripslashes($body), |
| 77 |
'created' => $created, |
| 78 |
'modified' => $created, |
| 79 |
'status' => (isset($status) ? $status : 0), |
| 80 |
), |
| 81 |
array('%d','%d','%d','%d','%s','%s','%s','%s', '%d') |
| 82 |
) |
| 83 |
){ |
| 84 |
$postid = $this->wpforo->db->insert_id; |
| 85 |
|
| 86 |
$answ_incr = ''; |
| 87 |
$comm_incr = ''; |
| 88 |
if( isset($forum['cat_layout']) && $forum['cat_layout'] == 3 ){ |
| 89 |
if($parentid){ |
| 90 |
$comm_incr = ', `comments` = `comments` + 1 '; |
| 91 |
}else{ |
| 92 |
$answ_incr = ', `answers` = `answers` + 1 '; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
$this->wpforo->db->query( "UPDATE `" . $this->wpforo->db->prefix . "wpforo_forums` SET `last_topicid` = ". intval($topicid) .", `last_postid` = ". intval($postid) .", `last_post_date` = '".esc_sql($created)."', `last_userid` = " . intval($userid) . ", `posts` = `posts` + 1 WHERE `forumid` = " . intval($topic['forumid']) ); |
| 97 |
$this->wpforo->db->query( "UPDATE `" . $this->wpforo->db->prefix . "wpforo_topics` SET `modified` = '" . esc_sql($created) . "', `last_post` = ". intval($postid) .", `posts` = `posts` + 1 $answ_incr WHERE `topicid` = " . intval($topicid) ); |
| 98 |
$this->wpforo->db->query( "UPDATE `" . $this->wpforo->db->prefix . "wpforo_profiles` SET `posts` = `posts` + 1 $answ_incr $comm_incr WHERE `userid` = " . intval($userid) ); |
| 99 |
|
| 100 |
$post['postid'] = $postid; |
| 101 |
$post['posturl'] = $this->get_post_url($postid); |
| 102 |
|
| 103 |
do_action( 'wpforo_after_add_post', $post, $topic ); |
| 104 |
|
| 105 |
$this->wpforo->member->reset($userid); |
| 106 |
$this->wpforo->notice->add('You successfully replied', 'success'); |
| 107 |
return $postid; |
| 108 |
} |
| 109 |
|
| 110 |
$this->wpforo->notice->add('Reply request error', 'error'); |
| 111 |
return FALSE; |
| 112 |
} |
| 113 |
|
| 114 |
public function edit( $args = array() ){ |
| 115 |
|
| 116 |
if( empty($args) && (!isset($_REQUEST['post']) || empty($_REQUEST['post'])) ) return FALSE; |
| 117 |
if( empty($args) && !empty($_REQUEST['post']) ){ $args = $_REQUEST['post']; $args['body'] = $_REQUEST['postbody']; } |
| 118 |
|
| 119 |
do_action( 'wpforo_start_edit_post', $args ); |
| 120 |
|
| 121 |
if( !isset($args['postid']) || !$args['postid'] || !is_numeric($args['postid']) ){ |
| 122 |
$this->wpforo->notice->add('Cannot update post data', 'error'); |
| 123 |
return FALSE; |
| 124 |
} |
| 125 |
$args['postid'] = intval($args['postid']); |
| 126 |
if( !$post = $this->get_post($args['postid']) ){ $this->wpforo->notice->add('No Posts found for update', 'error'); return FALSE; } |
| 127 |
|
| 128 |
$args['userid'] = $post['userid']; |
| 129 |
$args['status'] = $post['status']; |
| 130 |
|
| 131 |
$args = apply_filters('wpforo_edit_post_data_filter', $args); |
| 132 |
if(empty($args)) return FALSE; |
| 133 |
|
| 134 |
extract($args, EXTR_OVERWRITE); |
| 135 |
|
| 136 |
$diff = current_time( 'timestamp', 1 ) - strtotime($post['created']); |
| 137 |
if( !($this->wpforo->perm->forum_can('er', $post['forumid']) || ($this->wpforo->current_userid == $post['userid'] && $this->wpforo->perm->forum_can('eor', $post['forumid']) && $diff < $this->wpforo->post_options['eor_durr'])) ){ |
| 138 |
$this->wpforo->notice->add('You haven\'t permission to edit post from this forum', 'error'); |
| 139 |
return FALSE; |
| 140 |
} |
| 141 |
|
| 142 |
$title = (isset($title) ? wpforo_text( trim($title), 250, false ) : ''); |
| 143 |
$body = ( isset($body) ? preg_replace('#</pre>[\r\n\t\s\0]*<pre>#isu', "\r\n", $body) : '' ); |
| 144 |
|
| 145 |
if(isset($forumid)) $forumid = intval($forumid); |
| 146 |
if(isset($topicid)) $topicid = intval($topicid); |
| 147 |
if(isset($parentid)) $parentid = intval($parentid); |
| 148 |
if(isset($title)) $title = sanitize_text_field(trim($title)); |
| 149 |
if(isset($slug)) $slug = sanitize_title($slug); |
| 150 |
if(isset($created)) $created = sanitize_text_field($created); |
| 151 |
if(isset($userid)) $userid = intval($userid); |
| 152 |
if(isset($body)) $body = wpforo_kses(trim($body), 'post'); |
| 153 |
if(isset($status)) $status = intval($status); |
| 154 |
|
| 155 |
if( FALSE !== $this->wpforo->db->update( |
| 156 |
$this->wpforo->db->prefix."wpforo_posts", |
| 157 |
array( |
| 158 |
'title' => ( isset($title) ? stripslashes($title) : stripslashes($post['title']) ), |
| 159 |
'body' => ( (isset($body) && $body) ? stripslashes($body) : stripslashes($post['body']) ), |
| 160 |
'modified' => current_time( 'mysql', 1 ), |
| 161 |
'status' => ( isset($status) ? $status : intval($post['status']) ), |
| 162 |
), |
| 163 |
array('postid' => $postid), |
| 164 |
array('%s','%s','%s','%d'), |
| 165 |
array('%d') |
| 166 |
) |
| 167 |
){ |
| 168 |
do_action( 'wpforo_after_edit_post', array( 'postid' => $postid, 'topicid' => $topicid, 'title' => $title, 'body' => $body, 'status' => $status ) ); |
| 169 |
|
| 170 |
$this->wpforo->notice->add('This post successfully edited', 'success'); |
| 171 |
return $postid; |
| 172 |
} |
| 173 |
|
| 174 |
$this->wpforo->notice->add('Reply request error', 'error'); |
| 175 |
return FALSE; |
| 176 |
} |
| 177 |
|
| 178 |
################################################################################# |
| 179 |
/** |
| 180 |
* Delete post from DB |
| 181 |
* |
| 182 |
* Returns true if successfully deleted or false. |
| 183 |
* |
| 184 |
* @since 1.0.0 |
| 185 |
* |
| 186 |
* @return bool |
| 187 |
*/ |
| 188 |
|
| 189 |
function delete($postid){ |
| 190 |
$postid = intval($postid); |
| 191 |
|
| 192 |
if( !$post = $this->get_post($postid) ) return true; |
| 193 |
|
| 194 |
do_action('wpforo_before_delete_post', $post); |
| 195 |
|
| 196 |
$diff = current_time( 'timestamp', 1 ) - strtotime($post['created']); |
| 197 |
if( !($this->wpforo->perm->forum_can('dr', $post['forumid']) || ($this->wpforo->current_userid == $post['userid'] && $this->wpforo->perm->forum_can('dor', $post['forumid']) && $diff < $this->wpforo->post_options['dor_durr'])) ){ |
| 198 |
$this->wpforo->notice->add('You haven\'t permission to delete post from this forum', 'error'); |
| 199 |
return FALSE; |
| 200 |
} |
| 201 |
|
| 202 |
//Find and delete default atatchments before deleting post |
| 203 |
$this->delete_attachments( $postid ); |
| 204 |
|
| 205 |
//Delete post |
| 206 |
if( $this->wpforo->db->delete($this->wpforo->db->prefix . 'wpforo_posts', array( 'postid' => intval($postid) ), array( '%d' )) ){ |
| 207 |
$last_post = $this->get_posts( array('topicid' => intval($post['topicid']), 'order' => 'DESC', 'row_count' => 1) ); |
| 208 |
if(is_array($last_post) && !empty($last_post)){ |
| 209 |
$last_post = $last_post[0]; |
| 210 |
}else{ |
| 211 |
$last_post = array( 'created' => '0000-00-00 00:00:00', 'userid' => 0, 'postid' => 0 ); |
| 212 |
} |
| 213 |
|
| 214 |
$this->wpforo->db->delete( |
| 215 |
$this->wpforo->db->prefix.'wpforo_likes', array( 'postid' => $postid ), array( '%d' ) |
| 216 |
); |
| 217 |
$this->wpforo->db->delete( |
| 218 |
$this->wpforo->db->prefix.'wpforo_votes', array( 'postid' => $postid ), array( '%d' ) |
| 219 |
); |
| 220 |
|
| 221 |
$answ_incr = ''; |
| 222 |
$comm_incr = ''; |
| 223 |
$forum = $this->wpforo->forum->get_forum($post['forumid']); |
| 224 |
if( isset($forum['cat_layout']) && $forum['cat_layout'] == 3 ){ |
| 225 |
if($post['parentid']){ |
| 226 |
$comm_incr = ', `comments` = IF( (`comments` - 1) < 0, 0, `comments` - 1 ) '; |
| 227 |
}else{ |
| 228 |
$answ_incr = ', `answers` = IF( (`answers` - 1) < 0, 0, `answers` - 1 ) '; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
if($this->wpforo->db->query( "UPDATE IGNORE " . $this->wpforo->db->prefix . "wpforo_topics SET `last_post` = " . intval($last_post['postid']) . ", `posts` = IF( (`posts` - 1) < 0, 0, `posts` - 1 ) $answ_incr WHERE `topicid` = " . intval( $post['topicid'] ))){ |
| 233 |
if( $this->wpforo->db->query( "UPDATE IGNORE `" . $this->wpforo->db->prefix . "wpforo_forums` SET `last_post_date` = '" . esc_sql($last_post['created']) . "', `last_userid` = " . intval($last_post['userid']) . ", `last_postid` = " . intval($last_post['postid']) . ", `posts` = IF( (`posts` - 1) < 0, 0, `posts` - 1 ) WHERE `forumid` = " . intval( $post['forumid'] ))){ |
| 234 |
if( $this->wpforo->db->query( "UPDATE IGNORE `" . $this->wpforo->db->prefix . "wpforo_profiles` SET `posts` = IF( (`posts` - 1) < 0, 0, `posts` - 1 ) $answ_incr $comm_incr WHERE `userid` = " . intval($post['userid']) ) ){ |
| 235 |
$this->wpforo->member->reset($post['userid']); |
| 236 |
$this->wpforo->notice->add('This post successfully deleted', 'success'); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
do_action('wpforo_after_delete_post', $post); |
| 242 |
|
| 243 |
if( $post['is_first_post'] ) return $this->wpforo->topic->delete($post['topicid']); |
| 244 |
return TRUE; |
| 245 |
} |
| 246 |
|
| 247 |
$this->wpforo->notice->add('Post delete error', 'error'); |
| 248 |
return FALSE; |
| 249 |
} |
| 250 |
|
| 251 |
################################################################################# |
| 252 |
/** |
| 253 |
* array get_post(id(num)) |
| 254 |
* |
| 255 |
* Returns array from defined and default arguments. |
| 256 |
* |
| 257 |
* @since 1.0.0 |
| 258 |
* |
| 259 |
* @return array |
| 260 |
*/ |
| 261 |
function get_post($postid){ |
| 262 |
|
| 263 |
$post = array(); |
| 264 |
$cache = $this->wpforo->cache->on('memory_cashe'); |
| 265 |
|
| 266 |
if( $cache && isset(self::$cache['post'][$postid]) ){ |
| 267 |
return self::$cache['post'][$postid]; |
| 268 |
} |
| 269 |
|
| 270 |
$sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `postid` = " . intval($postid); |
| 271 |
$post = $this->wpforo->db->get_row($sql, ARRAY_A); |
| 272 |
if(!empty($post)) $post['userid'] = intval($post['userid']); |
| 273 |
|
| 274 |
if( isset($post['forumid']) && $post['forumid'] && !$this->wpforo->perm->forum_can('vf', $post['forumid']) ){ |
| 275 |
return array(); |
| 276 |
} |
| 277 |
|
| 278 |
if( isset($post['status']) && $post['status'] && !wpforo_is_owner($post['userid'])){ |
| 279 |
if( isset($post['forumid']) && $post['forumid'] && !$this->wpforo->perm->forum_can('au', $post['forumid']) ){ |
| 280 |
return array(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
if($cache && isset($postid)){ |
| 285 |
self::$cache['post'][$postid] = $post; |
| 286 |
} |
| 287 |
|
| 288 |
$post = apply_filters('wpforo_get_post', $post); |
| 289 |
return $post; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Returns merged arguments array from defined and default arguments. |
| 294 |
* |
| 295 |
* @since 1.0.0 |
| 296 |
* |
| 297 |
* @param array |
| 298 |
* |
| 299 |
* @return array |
| 300 |
*/ |
| 301 |
function get_posts($args = array(), &$items_count = 0){ |
| 302 |
|
| 303 |
$cache = $this->wpforo->cache->on('object_cashe'); |
| 304 |
|
| 305 |
$default = array( |
| 306 |
'include' => array(), // array( 2, 10, 25 ) |
| 307 |
'exclude' => array(), // array( 2, 10, 25 ) |
| 308 |
|
| 309 |
'topicid' => NULL, // topic id in DB |
| 310 |
'forumid' => NULL, // forum id in DB |
| 311 |
'parentid' => -1, // parent post id |
| 312 |
'userid' => NULL, // user id in DB |
| 313 |
'orderby' => 'postid', // forumid, order, parentid |
| 314 |
'order' => 'ASC', // ASC DESC |
| 315 |
'offset' => NULL, // this use when you give row_count |
| 316 |
'row_count' => NULL, // 4 or 1 ... |
| 317 |
'status' => NULL, // 0 or 1 ... |
| 318 |
|
| 319 |
'check_private' => FALSE |
| 320 |
); |
| 321 |
|
| 322 |
$args = wpforo_parse_args( $args, $default ); |
| 323 |
|
| 324 |
if(is_array($args) && !empty($args)){ |
| 325 |
|
| 326 |
if( $cache ){ |
| 327 |
$object_key = md5( serialize($args) . $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'];} |
| 328 |
} |
| 329 |
|
| 330 |
extract($args, EXTR_OVERWRITE); |
| 331 |
|
| 332 |
if( $row_count === 0 ) return array(); |
| 333 |
|
| 334 |
$include = wpforo_parse_args( $include ); |
| 335 |
$exclude = wpforo_parse_args( $exclude ); |
| 336 |
|
| 337 |
$wheres = array(); |
| 338 |
$table_as_prefix = '`'.$this->wpforo->db->prefix.'wpforo_posts`.'; |
| 339 |
|
| 340 |
if(!empty($include)) $wheres[] = $table_as_prefix . "`postid` IN(" . implode(', ', array_map('intval', $include)) . ")"; |
| 341 |
if(!empty($exclude)) $wheres[] = $table_as_prefix . "`postid` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")"; |
| 342 |
|
| 343 |
if(!is_null($topicid)) $wheres[] = $table_as_prefix . "`topicid` = " . intval($topicid); |
| 344 |
if($parentid != -1) $wheres[] = $table_as_prefix . "`parentid` = " . intval($parentid); |
| 345 |
if(!is_null($userid)) $wheres[] = $table_as_prefix . "`userid` = " . intval($userid); |
| 346 |
if(!is_null($status)) $wheres[] = $table_as_prefix . "`status` = " . intval($status); |
| 347 |
|
| 348 |
if( isset($forumid) && $forumid ){ |
| 349 |
if( $this->wpforo->perm->forum_can('au', $forumid) ){ |
| 350 |
if(!is_null($status)) $wheres[] = $table_as_prefix . " `status` = " . intval($status); |
| 351 |
} |
| 352 |
elseif( isset($this->wpforo->current_userid) && $this->wpforo->current_userid ){ |
| 353 |
$wheres[] = " ( " . $table_as_prefix . "`status` = 0 OR (" . $table_as_prefix . "`status` = 1 AND " . $table_as_prefix . "`userid` = " .intval($this->wpforo->current_userid). ") )"; |
| 354 |
} |
| 355 |
else{ |
| 356 |
$wheres[] = " " . $table_as_prefix . "`status` = 0"; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if( $check_private ){ |
| 361 |
$sql = "SELECT DISTINCT `".$this->wpforo->db->prefix."wpforo_posts`.*, `".$this->wpforo->db->prefix."wpforo_topics`.`private` FROM `".$this->wpforo->db->prefix."wpforo_posts`, `".$this->wpforo->db->prefix."wpforo_topics`"; |
| 362 |
if(!empty($wheres)){ |
| 363 |
$sql .= " WHERE `".$this->wpforo->db->prefix."wpforo_posts`.`topicid` = `".$this->wpforo->db->prefix."wpforo_topics`.`topicid` " . ((!empty($wheres)) ? 'AND' : '') . " " . implode(" AND ", $wheres); |
| 364 |
} |
| 365 |
else{ |
| 366 |
$sql .= " WHERE `".$this->wpforo->db->prefix."wpforo_posts`.`topicid` = `".$this->wpforo->db->prefix."wpforo_topics`.`topicid` "; |
| 367 |
} |
| 368 |
$sql .= " ORDER BY " .$table_as_prefix . "`$orderby` " . $order; |
| 369 |
$item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql); |
| 370 |
} |
| 371 |
else{ |
| 372 |
$sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_posts`"; |
| 373 |
if(!empty($wheres)){ |
| 374 |
$sql .= " WHERE " . implode(" AND ", $wheres); |
| 375 |
} |
| 376 |
$sql .= " ORDER BY `$orderby` " . $order; |
| 377 |
$item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql); |
| 378 |
} |
| 379 |
|
| 380 |
if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql); |
| 381 |
|
| 382 |
if($row_count != NULL){ |
| 383 |
if($offset != NULL){ |
| 384 |
$sql .= esc_sql(" LIMIT $offset,$row_count"); |
| 385 |
}else{ |
| 386 |
$sql .= esc_sql(" LIMIT $row_count"); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
$posts = $this->wpforo->db->get_results($sql, ARRAY_A); |
| 391 |
$posts = apply_filters('wpforo_get_posts', $posts); |
| 392 |
|
| 393 |
if( $check_private ){ |
| 394 |
foreach($posts as $key => $post){ |
| 395 |
if( isset($post['forumid']) && !$this->wpforo->perm->forum_can('vf', $post['forumid']) ){ |
| 396 |
unset($posts[$key]); |
| 397 |
} |
| 398 |
if( isset($posts[$key]) && isset($post['forumid']) && isset($post['private']) && $post['private'] && !wpforo_is_owner($post['userid']) ){ |
| 399 |
if( !$this->wpforo->perm->forum_can('vp', $post['forumid']) ){ |
| 400 |
unset($posts[$key]); |
| 401 |
} |
| 402 |
} |
| 403 |
if( isset($posts[$key]) && isset($post['forumid']) && isset($post['status']) && $post['status'] && !wpforo_is_owner($post['userid']) ){ |
| 404 |
if( !$this->wpforo->perm->forum_can('au', $post['forumid']) ){ |
| 405 |
unset($posts[$key]); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
if($cache && isset($object_key) && !empty($posts)){ |
| 412 |
self::$cache['posts'][$object_key]['items'] = $posts; |
| 413 |
self::$cache['posts'][$object_key]['items_count'] = $items_count; |
| 414 |
} |
| 415 |
return $posts; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
function search( $args = array(), &$items_count = 0 ){ |
| 421 |
if(!is_array($args)) $args = array('needle' => $args); |
| 422 |
|
| 423 |
$default = array( |
| 424 |
'needle' => '', // search needle |
| 425 |
'forumids' => array(), // array( 2, 10, 25 ) |
| 426 |
'date_period' => 0, // topic id in DB |
| 427 |
'type' => 'entire-posts', // search type ( entire-posts | titles-only | user-posts | user-topics ) |
| 428 |
'orderby' => 'relevancy', // Sort Search Results by ( relevancy | date | user | forum ) |
| 429 |
'order' => 'DESC', // Sort Search Results ( ASC | DESC ) |
| 430 |
'offset' => NULL, // this use when you give row_count |
| 431 |
'row_count' => NULL // 4 or 1 ... |
| 432 |
); |
| 433 |
|
| 434 |
$args = wpforo_parse_args( $args, $default ); |
| 435 |
|
| 436 |
if( !empty($args) ){ |
| 437 |
extract($args, EXTR_OVERWRITE); |
| 438 |
|
| 439 |
$date_period = intval($date_period); |
| 440 |
|
| 441 |
$selects = array('p.`postid`', 't.`topicid`', 't.`private`', 't.`status`', 't.`forumid`', 'p.`userid`', 't.`title`', 'p.`created`', 'p.`body`' ); |
| 442 |
$innerjoins = array('INNER JOIN `'.$this->wpforo->db->prefix.'wpforo_topics` t ON t.`topicid` = p.`topicid`'); |
| 443 |
$wheres = array(); |
| 444 |
$orders = array(); |
| 445 |
|
| 446 |
if(!empty($forumids)) $wheres[] = "t.`forumid` IN(" . implode(', ', array_map('intval', $forumids)) . ")"; |
| 447 |
if( $date_period != 0 ){ |
| 448 |
$date = date( 'Y-m-d H:i:s', current_time( 'timestamp', 1 ) - ($date_period * 24 * 60 * 60) ); |
| 449 |
if($date) $wheres[] = "p.`created` > '".esc_sql($date)."'"; |
| 450 |
} |
| 451 |
|
| 452 |
if($needle){ |
| 453 |
|
| 454 |
$needle = trim( trim( str_replace(' ', '* ', $needle) ), '*' ) . "*"; |
| 455 |
$needle = esc_sql(substr(sanitize_text_field($needle), 0, 60)); |
| 456 |
|
| 457 |
if($type == 'entire-posts'){ |
| 458 |
$selects[] = "MATCH(t.`title`) AGAINST('$needle' IN BOOLEAN MODE) + MATCH(p.`title`) AGAINST('$needle' IN BOOLEAN MODE) + MATCH(p.`body`) AGAINST('$needle' IN BOOLEAN MODE) AS matches"; |
| 459 |
$wheres[] = "( MATCH(t.`title`) AGAINST('$needle' IN BOOLEAN MODE) OR MATCH(p.`title`, p.`body`) AGAINST('$needle' IN BOOLEAN MODE) )"; |
| 460 |
$orders[] = "MATCH(t.`title`) AGAINST('$needle') + MATCH(p.`title`) AGAINST('$needle') + MATCH(p.`body`) AGAINST('$needle')"; |
| 461 |
$orders[] = "MATCH(t.`title`) AGAINST('$needle' IN BOOLEAN MODE) + MATCH(p.`title`) AGAINST('$needle' IN BOOLEAN MODE) + MATCH(p.`body`) AGAINST('$needle' IN BOOLEAN MODE)"; |
| 462 |
}elseif($type == 'titles-only'){ |
| 463 |
$selects[] = "MATCH(t.`title`) AGAINST('$needle' IN BOOLEAN MODE) + MATCH(p.`title`) AGAINST('$needle' IN BOOLEAN MODE) AS matches"; |
| 464 |
$wheres[] = "( MATCH(t.`title`) AGAINST('$needle' IN BOOLEAN MODE) OR MATCH(p.`title`) AGAINST('$needle' IN BOOLEAN MODE) )"; |
| 465 |
$orders[] = "MATCH(t.`title`) AGAINST('$needle') + MATCH(p.`title`) AGAINST('$needle')"; |
| 466 |
$orders[] = "MATCH(t.`title`) AGAINST('$needle' IN BOOLEAN MODE) + MATCH(p.`title`) AGAINST('$needle' IN BOOLEAN MODE)"; |
| 467 |
}elseif($type == 'user-posts' || $type == 'user-topics'){ |
| 468 |
$innerjoins[] = "INNER JOIN `".$this->wpforo->db->base_prefix."users` u ON u.`ID` = p.`userid`"; |
| 469 |
$wheres[] = "( u.`user_login` LIKE '$needle' OR u.`user_email` LIKE '$needle' OR u.`display_name` LIKE '$needle' )"; |
| 470 |
if($type == 'user-topics') $wheres[] = "`is_first_post` = 1"; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
if($orderby == 'date'){ |
| 475 |
$orders = array('p.`created`'); |
| 476 |
}elseif($orderby == 'user'){ |
| 477 |
$orders = array('p.`userid`'); |
| 478 |
}elseif($orderby == 'forum'){ |
| 479 |
$orders = array('t.`forumid`'); |
| 480 |
} |
| 481 |
|
| 482 |
$sql = "SELECT COUNT(p.`postid`) FROM `".$this->wpforo->db->prefix."wpforo_posts` p ".implode(' ', $innerjoins); |
| 483 |
if(!empty($wheres)) $sql .= " WHERE " . implode( " AND ", $wheres ); |
| 484 |
$items_count = $this->wpforo->db->get_var($sql); |
| 485 |
|
| 486 |
$sql = "SELECT ".implode(', ', $selects)." FROM `".$this->wpforo->db->prefix."wpforo_posts` p ".implode(' ', $innerjoins); |
| 487 |
if(!empty($wheres)) $sql .= " WHERE " . implode( " AND ", $wheres ); |
| 488 |
if(!empty($orders)) $sql .= " ORDER BY ".implode(' '.strtoupper($order).', ', $orders)." ".strtoupper($order); |
| 489 |
|
| 490 |
if($row_count != NULL){ |
| 491 |
if($offset != NULL){ |
| 492 |
$sql .= esc_sql(" LIMIT $offset,$row_count"); |
| 493 |
}else{ |
| 494 |
$sql .= esc_sql(" LIMIT $row_count"); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
$posts = $this->wpforo->db->get_results($sql, ARRAY_A); |
| 499 |
foreach($posts as $key => $post){ |
| 500 |
if( !$this->wpforo->perm->forum_can( 'vf', $post['forumid'] ) ) unset($posts[$key]); |
| 501 |
if( $post['private'] && !$this->wpforo->perm->forum_can( 'vp', $post['forumid'] ) ) unset($posts[$key]); |
| 502 |
if( $post['status'] && !$this->wpforo->perm->forum_can( 'au', $post['forumid'] ) ) unset($posts[$key]); |
| 503 |
} |
| 504 |
return $posts; |
| 505 |
}else{ |
| 506 |
return array(); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* return likes count by post id |
| 512 |
* |
| 513 |
* Return likes count |
| 514 |
* |
| 515 |
* @since 1.0.0 |
| 516 |
* |
| 517 |
* @param int |
| 518 |
* |
| 519 |
* @return int |
| 520 |
*/ |
| 521 |
function get_post_likes_count($postid){ |
| 522 |
return $this->wpforo->db->get_var("SELECT COUNT(l.`likeid`) FROM `".$this->wpforo->db->prefix."wpforo_likes` l, `".$this->wpforo->db->base_prefix."users` u WHERE `l`.`userid` = `u`.ID AND `l`.`postid` = ".intval($postid) ); |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* return usernames who likes this post |
| 527 |
* |
| 528 |
* Return array with username |
| 529 |
* |
| 530 |
* @since 1.0.0 |
| 531 |
* |
| 532 |
* @param int |
| 533 |
* |
| 534 |
* @return array |
| 535 |
*/ |
| 536 |
function get_likers_usernames($postid){ |
| 537 |
return $this->wpforo->db->get_results("SELECT u.ID, u.display_name FROM `".$this->wpforo->db->prefix."wpforo_likes` l, `".$this->wpforo->db->base_prefix."users` u WHERE `l`.`userid` = `u`.ID AND `l`.`postid` = ".intval($postid)." ORDER BY l.`userid` = " . intval($this->wpforo->current_userid) . " DESC, l.`likeid` DESC LIMIT 3", ARRAY_A); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* return like ID or null |
| 542 |
* |
| 543 |
* @since 1.0.0 |
| 544 |
* |
| 545 |
* @param int int |
| 546 |
* |
| 547 |
* @return null or like id |
| 548 |
*/ |
| 549 |
function is_liked($postid, $userid){ |
| 550 |
$returned_value = $this->wpforo->db->get_var("SELECT likeid FROM `".$this->wpforo->db->prefix."wpforo_likes` WHERE `postid` = ".intval($postid)." AND `userid` = ".intval($userid) ); |
| 551 |
if(is_null($returned_value)){ |
| 552 |
return FALSE; |
| 553 |
}else{ |
| 554 |
return $returned_value; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* return votes sum by post id |
| 560 |
* |
| 561 |
* Return votes count |
| 562 |
* |
| 563 |
* @since 1.0.0 |
| 564 |
* |
| 565 |
* @param int |
| 566 |
* |
| 567 |
* @return int |
| 568 |
*/ |
| 569 |
function get_post_votes_sum($postid){ |
| 570 |
$sum = $this->wpforo->db->get_var("SELECT sum(`reaction`) FROM `".$this->wpforo->db->prefix."wpforo_votes` WHERE `postid` = ".intval($postid) ); |
| 571 |
if($sum == null){ |
| 572 |
$sum = 0; |
| 573 |
} |
| 574 |
return $sum; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
/** |
| 579 |
* return forum slug |
| 580 |
* |
| 581 |
* string (slug) |
| 582 |
* |
| 583 |
* @since 1.0.0 |
| 584 |
* |
| 585 |
* @param int |
| 586 |
* |
| 587 |
* @return string or false |
| 588 |
*/ |
| 589 |
|
| 590 |
function get_forumslug_byid($postid){ |
| 591 |
|
| 592 |
$cache = $this->wpforo->cache->on('memory_cashe'); |
| 593 |
|
| 594 |
if( $cache && isset(self::$cache['forum_slug'][$postid]) ){ |
| 595 |
return self::$cache['forum_slug'][$postid]; |
| 596 |
} |
| 597 |
|
| 598 |
$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` =(SELECT `topicid` FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE postid = ".intval($postid)."))"); |
| 599 |
|
| 600 |
if($cache && isset($postid)){ |
| 601 |
self::$cache['forum_slug'][$postid] = $slug; |
| 602 |
} |
| 603 |
|
| 604 |
if($slug){ |
| 605 |
return $slug; |
| 606 |
}else{ |
| 607 |
return FALSE; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
/** |
| 613 |
* return topic slug |
| 614 |
* |
| 615 |
* string (slug) |
| 616 |
* |
| 617 |
* @since 1.0.0 |
| 618 |
* |
| 619 |
* @param int |
| 620 |
* |
| 621 |
* @return string or false |
| 622 |
*/ |
| 623 |
|
| 624 |
function get_topicslug_byid( $postid ){ |
| 625 |
|
| 626 |
$cache = $this->wpforo->cache->on('memory_cashe'); |
| 627 |
|
| 628 |
if( $cache && isset(self::$cache['topic_slug'][$postid]) ){ |
| 629 |
return self::$cache['topic_slug'][$postid]; |
| 630 |
} |
| 631 |
|
| 632 |
$slug = $this->wpforo->db->get_var("SELECT `slug` FROM ".$this->wpforo->db->prefix."wpforo_topics WHERE `topicid` =(SELECT `topicid` FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE postid = ".intval($postid).")"); |
| 633 |
|
| 634 |
if($cache && isset($postid)){ |
| 635 |
self::$cache['topic_slug'][$postid] = $slug; |
| 636 |
} |
| 637 |
|
| 638 |
if($slug){ |
| 639 |
return $slug; |
| 640 |
}else{ |
| 641 |
return FALSE; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
/** |
| 646 |
* return post full url by id |
| 647 |
* |
| 648 |
* @since 1.0.0 |
| 649 |
* |
| 650 |
* @param int $postid |
| 651 |
* |
| 652 |
* @return string $url |
| 653 |
*/ |
| 654 |
function get_post_url( $arg ){ |
| 655 |
|
| 656 |
$position = array(); |
| 657 |
|
| 658 |
if( isset($arg) && !is_array($arg) ){ |
| 659 |
$postid = intval($arg); |
| 660 |
$post = $this->get_post($postid); |
| 661 |
} |
| 662 |
else{ |
| 663 |
$post = $arg; |
| 664 |
$postid = $post['postid']; |
| 665 |
} |
| 666 |
if( is_array($post) && !empty($post) ){ |
| 667 |
$url = $this->get_forumslug_byid($postid) . '/' . $this->get_topicslug_byid($postid); |
| 668 |
if( $post['topicid'] ){ |
| 669 |
if( !$position ) $position = $this->wpforo->db->get_var("SELECT COUNT(`postid`) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `topicid` = ".intval($post['topicid'])." AND `postid` <= " . ($post['parentid'] ? intval($post['parentid']) : intval($postid) ) . " ORDER BY `postid`"); |
| 670 |
if( $position <= $this->wpforo->post_options['posts_per_page'] ) return wpforo_home_url($url) . "#post-" . intval($postid); |
| 671 |
$paged = ceil( $position/$this->wpforo->post_options['posts_per_page'] ); |
| 672 |
return wpforo_home_url( $url . "/paged/".$paged ) ."#post-" . intval($postid); |
| 673 |
} |
| 674 |
} |
| 675 |
return wpforo_home_url(); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
/** |
| 680 |
* return 0 or 1 |
| 681 |
* |
| 682 |
* @since 1.0.0 |
| 683 |
* |
| 684 |
* @param int $postid |
| 685 |
*/ |
| 686 |
function is_answered( $postid ){ |
| 687 |
$is_answered = $this->wpforo->db->get_var( $this->wpforo->db->prepare( |
| 688 |
" SELECT is_answer |
| 689 |
FROM `".$this->wpforo->db->prefix."wpforo_posts` |
| 690 |
WHERE postid = %d |
| 691 |
", |
| 692 |
intval($postid) |
| 693 |
) ); |
| 694 |
return $is_answered; |
| 695 |
} |
| 696 |
|
| 697 |
function get_count( $args = array() ){ |
| 698 |
$sql = "SELECT COUNT(`postid`) FROM `".$this->wpforo->db->prefix."wpforo_posts`"; |
| 699 |
if( !empty($args) ){ |
| 700 |
$wheres = array(); |
| 701 |
foreach ($args as $key => $value) $wheres[] = "`$key` = " . intval($value); |
| 702 |
if($wheres) $sql .= " WHERE " . implode(' AND ', $wheres); |
| 703 |
} |
| 704 |
return $this->wpforo->db->get_var($sql); |
| 705 |
} |
| 706 |
|
| 707 |
function unapproved_count(){ |
| 708 |
return $this->wpforo->db->get_var( "SELECT COUNT(*) FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `status` = 1" ); |
| 709 |
} |
| 710 |
|
| 711 |
function get_attachment_id( $filename ){ |
| 712 |
$attach_id = $this->wpforo->db->get_var( "SELECT `post_id` FROM `".$this->wpforo->db->prefix."postmeta` WHERE `meta_key` = '_wp_attached_file' AND `meta_value` LIKE '%" . esc_sql($filename) . "' LIMIT 1"); |
| 713 |
return $attach_id; |
| 714 |
} |
| 715 |
|
| 716 |
function delete_attachments( $postid ){ |
| 717 |
$post = $this->get_post($postid); |
| 718 |
if( isset($post['body']) && $post['body'] ){ |
| 719 |
if( preg_match_all('|\/wpforo\/default_attachments\/([^\s\"\]]+)|is', $post['body'], $attachments, PREG_SET_ORDER) ){ |
| 720 |
$upload_dir = wp_upload_dir(); |
| 721 |
$default_attachments_dir = $upload_dir['basedir'] . '/wpforo/default_attachments/'; |
| 722 |
foreach( $attachments as $attachment ){ |
| 723 |
$filename = trim($attachment[1]); |
| 724 |
$file = $default_attachments_dir . $filename; |
| 725 |
if( file_exists($file) ){ |
| 726 |
$posts = $this->wpforo->db->get_var( "SELECT COUNT(*) as posts FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `body` LIKE '%" . esc_sql( $attachment[0] ) . "%'" ); |
| 727 |
if( is_numeric($posts) && $posts == 1 ){ |
| 728 |
$attachmentid = $this->get_attachment_id( '/' . $filename ); |
| 729 |
if ( !wp_delete_attachment( $attachmentid ) ){ |
| 730 |
@unlink($file); |
| 731 |
} |
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
public function status( $postid, $status ){ |
| 740 |
if( !$postid = wpforo_bigintval($postid) ) return false; |
| 741 |
if( !$post = $this->get_post($postid) ) return false; |
| 742 |
|
| 743 |
if( $post['is_first_post'] ) return $this->wpforo->topic->status($post['topicid'], $status); |
| 744 |
|
| 745 |
if( false !== $this->wpforo->db->update( |
| 746 |
$this->wpforo->db->prefix."wpforo_posts", |
| 747 |
array( 'status' => intval($status) ), |
| 748 |
array( 'postid' => $postid ), |
| 749 |
array( '%d' ), |
| 750 |
array( '%d' ) |
| 751 |
)){ |
| 752 |
$this->wpforo->notice->add('Done!', 'success'); |
| 753 |
return true; |
| 754 |
} |
| 755 |
|
| 756 |
$this->wpforo->notice->add('error: Change Status action', 'error'); |
| 757 |
return false; |
| 758 |
} |
| 759 |
} |
| 760 |
?> |