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

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