PluginProbe
wpForo Forum / 1.2.0
wpForo Forum v1.2.0
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.2.0, at wpf-includes/class-posts.php

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