PluginProbe
wpForo Forum / 1.3.1
wpForo Forum v1.3.1
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / class-posts.php

class-posts.php in wpForo Forum 1.3.1, at wpf-includes/class-posts.php

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