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

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