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

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