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

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

855 lines 33.5 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
6 class wpForoForum{
7
8 private $wpforo;
9 static $cache = array( 'forums' => array(), 'forum' => array(), 'item' => array() );
10
11 public function __construct( $wpForo ){
12 if(!isset($this->wpforo)) $this->wpforo = $wpForo;
13 }
14
15 public function get_cache( $var ){
16 if( isset(self::$cache[$var]) ) return self::$cache[$var];
17 }
18
19 private function unique_slug($slug, $parentid = 0, $forumid = 0){
20 $new_slug = wpforo_text($slug, 250, false);
21 $forumid = intval($forumid);
22 $i = 2;
23 while( $this->wpforo->db->get_var("SELECT `forumid` FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `slug` = '" . esc_sql($new_slug) . "'" . ($forumid ? ' AND `forumid` != '. intval($forumid) : '')) ){
24 if( !isset($parent_slug) && $parentid = intval($parentid) ){
25 $parent_slug = $this->wpforo->db->get_var("SELECT `slug` FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `forumid` = " . intval($parentid) );
26 $new_slug = $parent_slug . "-" . wpforo_text($slug, 250, false);
27 }else{
28 $new_slug = wpforo_text($slug, 250, false) . '-' . $i;
29 }
30 $i++;
31 }
32 return $new_slug;
33 }
34
35 public function add( $args = array(), $checkperm = TRUE ){
36 if( $checkperm && !$this->wpforo->perm->usergroup_can('cf') ){
37 $this->wpforo->notice->add('Permission denied for add forum', 'error');
38 return FALSE;
39 }
40
41 if( empty($args) && empty($_REQUEST['forum']) ) return FALSE;
42 if( empty($args) && !empty($_REQUEST['forum']) ) $args = $_REQUEST['forum'];
43
44 extract($args, EXTR_OVERWRITE);
45
46 if( !isset($title) || !$title ){
47 $this->wpforo->notice->add('Please insert required fields!', 'error');
48 return FALSE;
49 }
50
51 $title = sanitize_text_field($title);
52 $title = wpforo_text($title, 250, false);
53 $description = (isset($description) ? wpforo_kses($description, 'post') : '');
54 $permission = (isset($permission) && is_array($permission)) ? serialize(array_map('sanitize_text_field', $permission)) : 'a:5:{i:1;s:4:"full";i:2;s:9:"moderator";i:3;s:8:"standard";i:4;s:9:"read_only";i:5;s:8:"standard";}';
55 $meta_key = (isset($meta_key)) ? sanitize_text_field($meta_key) : '';
56 $meta_desc = (isset($meta_desc)) ? sanitize_text_field($meta_desc) : '';
57 $parentid = (isset($parentid)) ? intval($parentid) : 0;
58 $slug = (isset($slug) && $slug) ? sanitize_title($slug) : ((isset($title)) ? sanitize_title($title) : md5(time()));
59 $slug = $this->unique_slug($slug, $parentid);
60 $icon = (isset($icon)) ? sanitize_text_field($icon) : '';
61 $topics = (isset($topics)) ? intval($topics) : 0;
62 $posts = (isset($posts)) ? intval($posts) : 0;
63 $order = (isset($order)) ? intval($order) : 0;
64 $cat_layout = (isset($cat_layout)) ? intval($cat_layout) : 1;
65 $status = (isset($status)) ? intval($status) : 1;
66 $is_cat = (isset($is_cat)) ? intval($is_cat) : 0;
67 if(!$parentid) $is_cat = 1;
68
69 if($parentid) {
70 $cat_layout = $this->wpforo->db->get_var("SELECT `cat_layout` FROM `".$this->wpforo->db->prefix ."wpforo_forums` WHERE `forumid` = " . intval($parentid) );
71 $cat_layout = intval($cat_layout);
72 }
73
74 if( $this->wpforo->db->insert(
75 $this->wpforo->db->prefix . 'wpforo_forums',
76 array(
77 'title' => stripslashes($title),
78 'slug' => $slug,
79 'description' => stripslashes($description),
80 'parentid' => $parentid,
81 'icon' => $icon,
82 'topics' => $topics,
83 'posts' => $posts,
84 'permissions' => $permission,
85 'meta_key' => $meta_key,
86 'meta_desc' => $meta_desc,
87 'status' => $status,
88 'is_cat' => $is_cat,
89 'cat_layout' => $cat_layout,
90 'order' => $order
91 ),
92 array('%s','%s','%s','%d','%s','%d','%d','%s','%s','%s','%d','%d','%d','%d')
93 )
94 ){
95 $forumid = $this->wpforo->db->insert_id;
96 $this->delete_tree_cache();
97 wpforo_clean_cache($forumid, 'forum');
98 $this->wpforo->notice->add('Your forum successfully added', 'success');
99 return $forumid;
100 }
101
102 $this->wpforo->notice->add('Can\'t add forum', 'error');
103 return FALSE;
104 }
105
106 public function edit( $args = array() ){
107 if( !$this->wpforo->perm->usergroup_can('ef') ){
108 $this->wpforo->notice->add('Permission denied for edit forum', 'error');
109 return FALSE;
110 }
111
112 if( empty($args) && empty($_REQUEST['forum']) ) return FALSE;
113 if( empty($args) && !empty($_REQUEST['forum']) ) $args = $_REQUEST['forum'];
114 if( !isset($args['forumid']) && isset($_GET['id']) ) $args['forumid'] = $_GET['id'];
115
116 extract($args, EXTR_OVERWRITE);
117
118 if( !isset($forumid) || !$forumid ){
119 $this->wpforo->notice->add('Forum update error', 'error');
120 return FALSE;
121 }
122
123 if( !isset($title) || !$title ){
124 $this->wpforo->notice->add('Please insert required fields!', 'error');
125 return FALSE;
126 }
127
128 $forumid = intval($forumid);
129 $title = sanitize_text_field($title);
130 $title = wpforo_text($title, 250, false);
131 $description = wpforo_kses($description, 'post');
132 $permission = (isset($permission) && is_array($permission)) ? serialize(array_map('sanitize_text_field', $permission)) : 'a:5:{i:1;s:4:"full";i:2;s:9:"moderator";i:3;s:8:"standard";i:4;s:9:"read_only";i:5;s:8:"standard";}';
133 $meta_key = (isset($meta_key)) ? sanitize_text_field($meta_key) : '';
134 $meta_desc = (isset($meta_desc)) ? sanitize_text_field($meta_desc) : '';
135 $parentid = (isset($parentid)) ? intval($parentid) : 0;
136 $slug = (isset($slug)) ? sanitize_title($slug) : ((isset($title)) ? sanitize_title($title) : md5(time()));
137 $slug = $this->unique_slug($slug, $parentid, $forumid);
138 $icon = (isset($icon)) ? sanitize_text_field($icon) : '';
139 $topics = (isset($topics)) ? intval($topics) : 0;
140 $posts = (isset($posts)) ? intval($posts) : 0;
141 $order = (isset($order)) ? intval($order) : 0;
142 $cat_layout = (isset($cat_layout)) ? intval($cat_layout) : 1;
143 $status = (isset($status)) ? intval($status) : 1;
144 $is_cat = (isset($is_cat)) ? intval($is_cat) : 0;
145 if(!$parentid) $is_cat = 1;
146
147 if($parentid) {
148 $cat_layout = $this->wpforo->db->get_var("SELECT `cat_layout` FROM `".$this->wpforo->db->prefix ."wpforo_forums` WHERE `forumid` = " . intval($parentid) );
149 $cat_layout = intval($cat_layout);
150 }
151
152 if( FALSE !== $this->wpforo->db->update(
153 $this->wpforo->db->prefix . 'wpforo_forums',
154 array(
155 'title' => stripslashes($title),
156 'slug' => $slug,
157 'description' => stripslashes($description),
158 'parentid' => $parentid,
159 'icon' => $icon,
160 'permissions' => $permission,
161 'meta_key' => $meta_key,
162 'meta_desc' => $meta_desc,
163 'status' => $status,
164 'is_cat' => $is_cat,
165 'cat_layout' => $cat_layout
166 ),
167 array('forumid' => $forumid),
168 array('%s','%s','%s','%d','%s','%s','%s','%s','%d','%d','%d'),
169 array('%d')
170 )
171 ){
172 if( isset($cat_layout) ){
173 $childs = array();
174 $this->get_childs($forumid, $childs);
175 $sql = "UPDATE `".$this->wpforo->db->prefix . "wpforo_forums` SET `cat_layout` = ".intval($cat_layout)." WHERE `forumid` IN(". implode(',', array_map('intval', $childs)).")";
176 $this->wpforo->db->query($sql);
177 }
178 $this->delete_tree_cache();
179 wpforo_clean_cache($forumid, 'forum');
180 $this->wpforo->notice->add('Forum successfully updated', 'success');
181 return $forumid;
182 }
183
184 $this->wpforo->notice->add('Forum update error', 'error');
185 return FALSE;
186 }
187
188 public function delete($forumid = 0){
189 $forumid = intval($forumid);
190 if(!$forumid && isset( $_REQUEST['id'] ) ) $forumid = intval($_REQUEST['id']);
191
192 if( !$this->wpforo->perm->usergroup_can('df') ){
193 $this->wpforo->notice->add('Permission denied for delete forum', 'error');
194 return FALSE;
195 }
196
197 $childs = array();
198 $this->get_childs($forumid, $childs);
199 $forumids = implode(',', array_map('intval', $childs));
200
201 // START delete topic posts include first post
202 if( $topicids = $this->wpforo->db->get_col( "SELECT `topicid` FROM ".$this->wpforo->db->prefix."wpforo_topics WHERE `forumid` IN(". esc_sql($forumids) .")" ) ){
203 foreach($topicids as $topicid){
204 $this->wpforo->topic->delete($topicid, false);
205 }
206 }
207 // END delete topic posts include first post
208
209 if($this->wpforo->db->query( "DELETE FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `forumid` IN(". esc_sql($forumids) .")" )){
210 $this->delete_tree_cache();
211 wpforo_clean_cache();
212 $this->wpforo->notice->add('Your forum successfully deleted', 'success');
213 return TRUE;
214 }
215
216 $this->wpforo->notice->add('Forum deleting error', 'error');
217 return FALSE;
218 }
219
220 public function merge($forumid = 0, $mergeid = 0){
221 $forumid = intval($forumid);
222 $mergeid = intval($mergeid);
223
224 if(!$forumid && isset( $_REQUEST['id'] ) ) $forumid = intval($_REQUEST['id']);
225 if(!$mergeid && isset( $_REQUEST['forum']['mergeid'] ) ) $mergeid = intval($_REQUEST['forum']['mergeid']);
226
227 if( !$forumid || !$mergeid ) return false;
228
229 if( $child_forumids = $this->get_child_forums( $forumid ) ){
230 $forumids = trim( implode(',', array_map('intval', $child_forumids)) );
231 if( $forumids ){
232 $merge_layout = $this->wpforo->db->get_var("SELECT `cat_layout` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = " . intval($mergeid) );
233
234 if(!$this->wpforo->db->query( "UPDATE ".$this->wpforo->db->prefix."wpforo_forums SET `parentid` = " . intval($mergeid) . ", `cat_layout` = " . intval($merge_layout) . " WHERE `forumid` IN(". esc_sql($forumids) .")" )){
235 $this->wpforo->notice->add('Forum merging error', 'error');
236 return FALSE;
237 }
238 }
239 }
240
241 $this->wpforo->db->update(
242 $this->wpforo->db->prefix . 'wpforo_topics',
243 array( 'forumid' => $mergeid ),
244 array( 'forumid' => $forumid ),
245 array( '%d' ),
246 array( '%d' )
247 );
248 $this->wpforo->db->update(
249 $this->wpforo->db->prefix . 'wpforo_posts',
250 array( 'forumid' => $mergeid ),
251 array( 'forumid' => $forumid ),
252 array( '%d' ),
253 array( '%d' )
254 );
255
256 $this->rebuild_last_infos($mergeid);
257 $this->rebuild_stats($mergeid);
258
259 if($this->wpforo->db->delete( $this->wpforo->db->prefix.'wpforo_forums', array( 'forumid' => $forumid ), array( '%d' ) )){
260 $this->delete_tree_cache();
261 wpforo_clean_cache(0, 'forum');
262 $this->wpforo->notice->add('Forum is successfully merged', 'success');
263 return TRUE;
264 }
265
266 $this->wpforo->notice->add('Forum merging error', 'error');
267 return FALSE;
268 }
269
270 public function rebuild_last_infos($forumid){
271
272 $forumid = intval($forumid);
273
274 $last_topicid = 0;
275 $last_postid = 0;
276 $last_userid = 0;
277 $last_post_date = '0000-00-00 00:00:00';
278
279 $last_topics = $this->wpforo->topic->get_topics( array('forumid' => $forumid, 'orderby' => 'topicid', 'order' => 'DESC', 'row_count' => 1) );
280 if(!empty($last_topics)){
281 $last_topic = $last_topics[0];
282 $last_topicid = $last_topic['topicid'];
283 }
284 $last_posts = $this->wpforo->topic->get_topics( array('forumid' => $forumid, 'orderby' => 'modified', 'order' => 'DESC', 'row_count' => 1) );
285 if(!empty($last_posts)){
286 $last_post = $last_posts[0];
287 $last_post_data = $this->wpforo->post->get_post($last_post['last_post']);
288 if(!empty($last_post_data)){
289 $last_postid = $last_post_data['postid'];
290 $last_userid = $last_post_data['userid'];
291 $last_post_date = $last_post_data['created'];
292 }
293 }
294
295 $this->wpforo->db->query( "UPDATE `".$this->wpforo->db->prefix."wpforo_forums`
296 SET `last_topicid` = ".intval($last_topicid).", `last_postid` = ".intval($last_postid).",
297 `last_userid` = ".intval($last_userid).", `last_post_date` = '".esc_sql($last_post_date)."'
298 WHERE `forumid` = ".intval($forumid) );
299 wpforo_clean_cache(0, 'forum');
300 }
301
302 public function rebuild_stats($forumid){
303 if( !$forumid = intval($forumid) ) return false;
304 $topics = $this->wpforo->topic->get_count( array('forumid' => $forumid) );
305 $posts = $this->wpforo->post->get_count( array('forumid' => $forumid) );
306
307 if( false !== $this->wpforo->db->update(
308 $this->wpforo->db->prefix . "wpforo_forums",
309 array('topics' => $topics, 'posts' => $posts ),
310 array('forumid' => $forumid),
311 array('%d', '%d'),
312 array('%d')
313 ) ) {
314 wpforo_clean_cache(0, 'forum');
315 return true;
316 }
317 return false;
318 }
319
320 function get_forum( $args){
321
322 $cache = $this->wpforo->cache->on('memory_cashe');
323
324 if(is_array($args)){
325 $default = array(
326 'forumid' => NULL, // forumid
327 'slug' => '', // slug
328 'status' => NULL, // status forum 1 OR 0
329 'cat_layout' => 1, // forum layout
330 'type' => 'all' // category, forum
331 );
332 }else{
333 $default = array(
334 'forumid' => ( is_numeric($args) ? intval($args) : NULL ), // forumid
335 'slug' => ( !is_numeric($args) ? $args : '' ), // slug
336 'cat_layout' => 1, // forum layout
337 'status' => NULL, // status forum 1 OR 0
338 'type' => 'all' // category, forum
339 );
340 }
341 $args = wpforo_parse_args( $args, $default );
342
343 if( $cache && !empty($args['forumid']) ){
344 if( !empty(self::$cache['forum'][$args['forumid']]) ){
345 return self::$cache['forum'][$args['forumid']];
346 }
347 }
348
349 if( $cache && !empty($args['slug']) ){
350 if( !empty(self::$cache['forum'][addslashes($args['slug'])]) ){
351 return self::$cache['forum'][addslashes($args['slug'])];
352 }
353 }
354 if(!empty($args)){
355 extract($args, EXTR_OVERWRITE);
356 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_forums`";
357 $wheres = array();
358 if($forumid != NULL) $wheres[] = "`forumid` = " . intval($forumid);
359 if($status != NULL) $wheres[] = "`status` = " . intval($status);
360 if($type == 'category'){
361 $wheres[] = "`is_cat` = 1";
362 }elseif($type == 'forum'){
363 $wheres[] = "`is_cat` = 0";
364 }
365 if($slug != '') $wheres[] = "`slug` = '" . esc_sql($slug) . "'";
366
367 if(!empty($wheres)){
368 $sql .= " WHERE " . implode( " AND ", $wheres );
369 }
370 $forum = $this->wpforo->db->get_row($sql, ARRAY_A);
371 if(!empty($forum)) {
372 $forum['url'] = $this->get_forum_url( $forum );
373 }
374 $forum = apply_filters('wpforo_get_forum', $forum);
375 if($cache && isset($forumid)){
376 self::$cache['forum'][addslashes($forum['slug'])] = $forum;
377 return self::$cache['forum'][$forum['forumid']] = $forum;
378 }
379 else{
380 return $forum;
381 }
382 }
383 }
384
385 function get_forums($args = array(), &$items_count = 0 ){
386
387 $cache = $this->wpforo->cache->on('object_cashe');
388
389 $default = array(
390 'include' => array(), // array( 2, 10, 25 )
391 'exclude' => array(), // array( 2, 10, 25 )
392 'parent_include' => array(), // array( 2, 10, 25 )
393 'parent_exclude' => array(), // array( 2, 10, 25 )
394 'parentid' => NULL,
395 'parent_slug' => '',
396 'status' => NULL,
397 'type' => 'all', // category, forum
398 'orderby' => 'order', // order by `field`
399 'order' => 'ASC', // ASC DESC
400 'offset' => NULL, // OFFSET
401 'row_count' => NULL, // ROW COUNT
402 );
403
404 $args = wpforo_parse_args( $args, $default );
405 if(is_array($args) && !empty($args)){
406
407 extract($args, EXTR_OVERWRITE);
408
409 $include = wpforo_parse_args( $include );
410 $exclude = wpforo_parse_args( $exclude );
411 $parent_include = wpforo_parse_args( $parent_include );
412 $parent_exclude = wpforo_parse_args( $parent_exclude );
413
414 $sql = "SELECT * FROM `".$this->wpforo->db->prefix."wpforo_forums`";
415 $wheres = array();
416
417 if(!empty($include)) $wheres[] = "`forumid` IN(" . implode(', ', array_map('intval', $include)) . ")";
418 if(!empty($exclude)) $wheres[] = "`forumid` NOT IN(" . implode(', ', array_map('intval', $exclude)) . ")";
419 if(!empty($parent_include)) $wheres[] = "`parentid` IN(" . implode(', ', array_map('intval', $parent_include)) . ")";
420 if(!empty($parent_exclude)) $wheres[] = "`parentid` NOT IN(" . implode(', ', array_map('intval', $parent_exclude)) . ")";
421 if($parentid != NULL) $wheres[] = " `parentid` = " . intval($parentid);
422 if($status != NULL) $wheres[] = " `status` = " . intval($status);
423
424 if($type == 'category'){
425 $wheres[] = " `is_cat` = 1";
426 }elseif($type == 'forum'){
427 $wheres[] = " `is_cat` = 0";
428 }
429
430 if($parent_slug != '') $wheres[] = "`slug` = '" . esc_sql($parent_slug) . "'";
431
432 if(!empty($wheres)) $sql .= " WHERE " . implode( " AND ", $wheres );
433
434 $item_count_sql = preg_replace('#SELECT.+?FROM#isu', 'SELECT count(*) FROM', $sql);
435 if( $item_count_sql ) $items_count = $this->wpforo->db->get_var($item_count_sql);
436
437 $sql .= esc_sql(" ORDER BY `$orderby` " . $order);
438
439 if($row_count != NULL){
440 if($offset != NULL){
441 $sql .= esc_sql(" LIMIT $offset,$row_count");
442 }else{
443 $sql .= esc_sql(" LIMIT $row_count");
444 }
445 }
446
447 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'];}}
448
449 $forums = $this->wpforo->db->get_results($sql, ARRAY_A);
450 $forums = apply_filters('wpforo_get_topics', $forums);
451
452 if($cache && isset($object_key) && !empty($forums)){
453 self::$cache['forums'][$object_key]['items'] = $forums;
454 self::$cache['forums'][$object_key]['items_count'] = $items_count;
455 }
456 return $forums;
457 }
458 }
459
460 function search($needle, $fields = array()){
461
462 if($needle){
463 $needle = sanitize_text_field($needle);
464 if(empty($fields)){
465 $fields = array(
466 'title',
467 'description',
468 'meta_key',
469 'meta_desc'
470 );
471 }
472
473 $sql = "SELECT `forumid` FROM `".$this->wpforo->db->prefix."wpforo_forums`";
474 $wheres = array();
475
476 foreach($fields as $field){
477 $wheres[] = "`" . esc_sql($field) . "` LIKE '%" . esc_sql($needle) . "%'";
478 }
479
480 $sql .= " WHERE " . implode(" OR ", $wheres);
481 return $this->wpforo->db->get_col($sql);
482 }
483
484 return array();
485 }
486
487 function update_hierarchy(){
488
489 $this->delete_tree_cache();
490
491 if(is_array($_REQUEST['forum']) && !empty($_REQUEST['forum'])){
492 $i = 0;
493 foreach($_REQUEST['forum'] as $hierarchy){
494
495 extract($hierarchy, EXTR_OVERWRITE);
496
497 if(!isset($forumid) || !$forumid = intval($forumid) ) continue;
498
499 if(FALSE !== $this->wpforo->db->update(
500 $this->wpforo->db->prefix . 'wpforo_forums',
501 array(
502 'parentid' => (isset($parentid) ? intval($parentid) : 0),
503 'order' => (isset($order) ? intval($order) : 0),
504 ),
505 array( 'forumid' => intval($forumid) ),
506 array(
507 '%d',
508 '%d'
509 ),
510 array( '%d' )
511 )) $i++;
512
513 if(isset($parentid) && $parentid = intval($parentid) ){
514 $cat_layout = $this->wpforo->db->get_var("SELECT `cat_layout` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = " . intval($parentid));
515 $this->wpforo->db->query("UPDATE `".$this->wpforo->db->prefix."wpforo_forums` SET `cat_layout` = " . intval($cat_layout) . " WHERE `forumid` = " . intval($forumid));
516 }
517
518 }
519
520 $this->wpforo->db->query("UPDATE `".$this->wpforo->db->prefix."wpforo_forums` SET `is_cat` = 0");
521 $this->wpforo->db->query("UPDATE `".$this->wpforo->db->prefix."wpforo_forums` SET `is_cat` = 1 WHERE `parentid` = 0");
522
523 if($i){
524 $this->wpforo->notice->add('Forum hierarchy successfully updated', 'success');
525 }else{
526 $this->wpforo->notice->add('Cannot update forum hierarchy', 'error');
527 }
528
529 }
530 }
531
532 function get_childs($forumid, &$data){
533 if(empty($data)) $data[] = $forumid;
534 $sub_forums = $this->wpforo->db->get_results("SELECT `forumid` FROM ".$this->wpforo->db->prefix."wpforo_forums WHERE `parentid` = ".intval($forumid), ARRAY_A);
535 if(!empty($sub_forums)){
536 foreach($sub_forums as $sub_forum){
537 $data[] = $sub_forum['forumid'];
538 $this->get_childs($sub_forum['forumid'], $data);
539 }
540 }
541 }
542
543
544 // get forums tree for drop down menu
545
546 /**
547 * Returns depth for this item.
548 *
549 * @since 1.0.0
550 *
551 * @param int item id
552 *
553 * @param int before calling the function $depth = 0
554 *
555 * @return int
556 */
557 function count_depth($forumid, &$depth){
558 $parentid = $this->wpforo->db->get_var("SELECT `parentid` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = ".intval($forumid));
559
560 if($parentid != 0){
561 $depth++;
562 $this->count_depth($parentid, $depth);
563 }
564 }
565
566 function get_child_forums($parent){
567 $children = $this->wpforo->db->get_results("SELECT `forumid` AS childid FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `parentid` = ".intval($parent)." ORDER BY `order`", ARRAY_A);
568 if(!empty($children)){
569 foreach( $children as $child ){
570 $data[] = $child['childid'];
571 }
572 return $data;
573 }else{
574 return array();
575 }
576 }
577
578 function forum_list( $parent, $type , $cats = TRUE, $topicid = 0 ){
579 static $old_depth;
580 global $wpforo;
581
582 foreach ( $parent as $forumid ) {
583
584 if ($forumid == 0) continue;
585 $depth = 0;
586 $this->count_depth($forumid, $depth);
587 $name = $this->wpforo->db->get_var("SELECT `title` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = ".intval($forumid));
588 if($type == 'select_box'){
589 if( isset($_GET['page']) && $_GET['page'] == 'wpforo-ads' && isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id']) && $_GET['id'] ){
590 $ad = $wpforo->ad->get_ad($_GET['id']);
591 $ad_forumids = explode(',', $ad['forumids']);
592 }
593 ?><option value="<?php echo intval($forumid) ?>"<?php echo( !$cats && $depth == 0 ? ' disabled ': ''); echo ( $forumid == $this->parentid($topicid) || (isset( $_GET['forumid'] ) && $forumid == $_GET['forumid']) || ( !empty($_GET['wpff']) && in_array($forumid, $_GET['wpff']) ) || ( !empty($ad_forumids) && in_array($forumid, $ad_forumids) ) || ( isset($_GET['parentid']) && $_GET['parentid'] == $forumid ) ? ' selected ' : '' ) ?> > <?php echo esc_html(str_repeat( '— ', $depth ) . trim($name)) ?></option><?php
594 }elseif($type == 'drag_menu'){
595 $cur_forum = $this->wpforo->db->get_row("SELECT `cat_layout`, `topics`, `posts` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = ".intval($forumid), ARRAY_A);
596 $cat_layout_name = ( $cur_forum['cat_layout'] == 2 ? 'Simplified Layout' : ( $cur_forum['cat_layout'] == 3 ? 'QA Layout' : 'Extended Layout' ) ); ?>
597
598 <li id="menu-item-<?php echo intval($forumid) ?>" class="menu-item menu-item-depth-<?php echo esc_attr($depth) ?>">
599 <input id="forumid-<?php echo intval($forumid) ?>" type="hidden" name="forum[<?php echo intval($forumid) ?>][forumid]"/>
600 <input id="parentid-<?php echo intval($forumid) ?>" type="hidden" name="forum[<?php echo intval($forumid) ?>][parentid]"/>
601 <input id="order-<?php echo intval($forumid) ?>" type="hidden" name="forum[<?php echo intval($forumid) ?>][order]"/>
602 <dl class="menu-item-bar">
603 <dt class="menu-item-handle forum_width">
604 <span class="item-title forumtitle"><span style="font-weight:400; cursor:help;" title="Forum ID"><?php echo $forumid; ?> &nbsp;|&nbsp;</span> <?php echo esc_html($name) ?></span>
605 <span class="item-controls">
606 <span class="wpforo-cat-layout"><?php echo ( $depth != 0 ? __('Topics', 'wpforo') . '&nbsp;(' . intval($cur_forum['topics']) . ')&nbsp;,&nbsp;' . __('Posts', 'wpforo') . '&nbsp;(' . intval($cur_forum['posts']) . ')&nbsp; | &nbsp;' : '' ) ?><?php echo ( $depth == 0 ? '(&nbsp;<i>' . esc_html($cat_layout_name) . '</i>&nbsp;)&nbsp; | &nbsp;' : '' ); ?></span>
607 <span class="menu_add"><a href="<?php echo admin_url( 'admin.php?page=wpforo-forums&action=add&parentid=' . intval($forumid) ) ?>" > <img src="<?php echo WPFORO_URL ?>/wpf-assets/images/icons/plus<?php echo ((!$depth) ? '-dark' : ''); ?>.png" title="<?php if( $depth ) : _e('Add a new SubForum', 'wpforo'); else: _e('Add a new Forum in this Category', 'wpforo'); endif; ?>"/></a></span> &nbsp;|&nbsp;
608 <span class="menu_edit"><a href="<?php echo admin_url( 'admin.php?page=wpforo-forums&id=' . intval($forumid) . '&action=edit' ) ?>"><img src="<?php echo WPFORO_URL ?>/wpf-assets/images/icons/pencil<?php echo ((!$depth) ? '-dark' : ''); ?>.png" title="<?php _e('edit', 'wpforo') ?>"/></a></span>&nbsp;|&nbsp;
609 <?php if( $this->wpforo->perm->usergroup_can('df') ): ?>
610 <span class="menu_delete"><a href="<?php echo admin_url( 'admin.php?page=wpforo-forums&id=' . intval($forumid) . '&action=del' ) ?>"><img src="<?php echo WPFORO_URL ?>/wpf-assets/images/icons/trash<?php echo ((!$depth) ? '-dark' : ''); ?>.png" title="<?php _e('delete', 'wpforo') ?>"/></a></span>&nbsp;|&nbsp;
611 <?php endif; ?>
612 <span class="menu_view"><a href="<?php echo esc_url(wpforo_forum($forumid, 'url')); ?>" > <img src="<?php echo WPFORO_URL ?>/wpf-assets/images/icons/eye<?php echo ((!$depth) ? '-dark' : ''); ?>.png" title="<?php _e('View', 'wpforo') ?>"/> </a> </span>
613
614 </span>
615 </dt>
616 </dl>
617 <ul class="menu-item-transport"></ul>
618 </li>
619
620 <?php
621 }elseif($type == 'front_list'){
622 $slug = $this->wpforo->db->get_var("SELECT `slug` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = ".intval($forumid));
623 if(isset($old_depth) && $old_depth == $depth) echo '</dd><dd>';
624 if(isset($old_depth) && $old_depth < $depth) echo '<dl><dd>';
625 if(isset($old_depth) && $old_depth > $depth) echo '</dd></dl>';
626 $old_depth = $depth;
627 if(isset($wpforo->current_object) && isset($wpforo->current_object['forumid'])){
628 if( $forumid == $wpforo->current_object['forumid'] ){
629 echo'<span class="wpf-dl-item wpf-dl-current"><i class="fa fa-comments-o"></i><strong>'.esc_html($name).'</strong><a href="' . esc_url( wpforo_forum($forumid, 'url') ) . '" >&nbsp;&raquo;</a></span>';
630 }
631 else{
632 echo'<span class="wpf-dl-item"><a href="'.esc_url( wpforo_forum($forumid, 'url') ).'" ><i class="fa fa-comments-o"></i>'.esc_html($name).'</a></span>';
633 }
634 }
635 else{
636 echo'<span class="wpf-dl-item"><a href="'.esc_url( wpforo_forum($forumid, 'url') ).'" ><i class="fa fa-comments-o"></i>'.esc_html($name).'</a></span>';
637 }
638 }
639 $subforums = $this->get_child_forums($forumid);
640 if( !empty($subforums) ){
641 $this->forum_list($subforums, $type);
642 }
643 }
644 }
645
646 function tree( $type = 'front_list', $cats = TRUE, $topicid = 0 ){
647 $parentids = $this->wpforo->db->get_results("SELECT `forumid` AS parentid FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `parentid` = 0 ORDER BY `order`", ARRAY_A);
648 if(!empty($parentids)){
649 foreach( $parentids as $parentid ){
650 $data[] = $parentid['parentid'];
651 }
652 if( !wpforo_is_admin() ){
653 $key = md5( serialize($data) . $type . (int)$cats . $this->wpforo->current_user_groupid );
654 $html = get_option( 'wpforo_forum_tree_' . $key );
655 if( $html ){
656 echo $html;
657 }
658 elseif( function_exists('ob_start') ){
659 ob_start();
660 $this->forum_list($data, $type, $cats);
661 $html = ob_get_clean();
662 if($type != 'drag_menu'){
663 update_option( 'wpforo_forum_tree_' . $key, $html );
664 }
665 echo $html;
666 }
667 }
668 else{
669 $this->forum_list($data, $type, $cats);
670 }
671 }
672 }
673 // end forums tree for drop down menu
674
675 public function delete_tree_cache() {
676 $this->wpforo->db->query("DELETE FROM " . $this->wpforo->db->options . " WHERE `option_name` LIKE 'wpforo_forum_tree_%'");
677 }
678
679 function parentid( $topicid = 0 ){
680 if(isset($_GET['page']) && $_GET['page'] == 'wpforo-forums'){
681 if( isset($_GET['id'])) return $this->wpforo->db->get_var("SELECT `parentid` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = ".intval($_GET['id']));
682 }
683 elseif( isset($_GET['page']) && $_GET['page'] == 'wpforo-topics' ){
684 if( isset($_GET['id'])) return $this->wpforo->db->get_var( "SELECT `forumid` FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `topicid` = ".intval($_GET['id']));
685 }else{
686 if( $topicid ) return $this->wpforo->db->get_var( "SELECT `forumid` FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `topicid` = ".intval($topicid));
687 }
688 }
689
690 function permissions(){
691 $access_arr = $this->wpforo->perm->get_accesses();
692 if(!empty( $access_arr )){
693
694 if(isset($_GET['id'])){
695 if($permissions_srlz = $this->wpforo->db->get_var("SELECT `permissions` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = ".intval($_GET['id']))){
696 $permissions_arr = unserialize($permissions_srlz);
697 }
698 }
699
700 if($usergroups = $this->wpforo->db->get_results("SELECT `groupid`, `name` FROM `".$this->wpforo->db->prefix."wpforo_usergroups`", ARRAY_A)){
701 foreach($usergroups as $usergroup){
702 extract($usergroup, EXTR_OVERWRITE);
703 echo '
704 <tr>
705 <td>'.esc_html($name).'</td>
706 <td>
707 <select name="forum[permission]['.intval($groupid).']">';
708 foreach($access_arr as $value){
709
710 echo '<option value="'.esc_attr($value['access']).'" '.
711 ((isset($permissions_arr[$groupid]) && $value['access'] == $permissions_arr[$groupid])
712 || (!isset($permissions_arr[$groupid])
713 && (($name == 'Guest' && $value['access'] == 'read_only')
714 || ($name == 'Registered' && $value['access'] == 'standard')
715 || ($name == 'Customer' && $value['access'] == 'standard')
716 || ($name == 'Moderator' && $value['access'] == 'moderator')
717 || ($name == 'Admin' && $value['access'] == 'full')
718 || ($name != 'Guest' && $name != 'Registered' && $name != 'Customer' && $name != 'Moderator' && $name != 'Admin' && $value['access'] == 'standard') )) ? ' selected ' : '').'>'.esc_html( __( $value['title'], 'wpforo') ).'</option>';
719 }
720 echo'
721 </select>
722 </td>
723 </tr>
724 ';
725 }
726
727
728
729 }
730 }
731 }
732
733 /**
734 * array get_counts(array or id(num))
735 *
736 * @since 1.0.0
737 *
738 * @param array defined arguments array for returning
739 *
740 * @return int count topics
741 */
742 function get_counts($forumids){
743
744 if( !empty($forumids) ){
745
746 $wheres = '';
747
748 if(!is_array($forumids)){
749 $wheres = "`forumid` = " . intval($forumids);
750 }else{
751 $wheres = "`forumid` IN(" . implode(', ', array_map('intval', $forumids)) . ")";
752 }
753
754 $sql = "SELECT SUM(`topics`) as topics, SUM(`posts`) as posts FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE " . $wheres;
755 return $this->wpforo->db->get_row($sql, ARRAY_A);
756
757 }
758
759 return 0;
760 }
761
762 /**
763 * array get_layout(array)
764 *
765 * @since 1.0.0
766 *
767 * @param array defined arguments array for returning
768 *
769 * @return int layout id
770 */
771 function get_layout($args){
772 if(is_array($args)){
773 $default = array(
774 'forumid' => NULL, // forum id
775 'topicid' => NULL, // topic id
776 'postid' => NULL, // post id
777 );
778 }else{
779 $default = array(
780 'forumid' => $args, // forumid
781 'topicid' => NULL, // topic id
782 'postid' => NULL, // post id
783 );
784 }
785 $args = wpforo_parse_args( $args, $default );
786 if(!empty($args)){
787 extract($args, EXTR_OVERWRITE);
788
789 if( $args['forumid'] ){
790 $sql = "SELECT `cat_layout` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = " . intval($args['forumid']);
791 $cat_layout = $this->wpforo->db->get_var($sql);
792 return ( $cat_layout ? $cat_layout : 1 );
793 }elseif( $args['topicid'] ){
794 $sql = "SELECT `forumid` FROM `".$this->wpforo->db->prefix."wpforo_topics` WHERE `topicid` = " . intval($args['topicid']);
795 $forumid = $this->wpforo->db->get_var($sql);
796 return $this->get_layout(array( 'forumid' => $forumid ));
797 }elseif( $args['postid'] ){
798 $sql = "SELECT `forumid` FROM `".$this->wpforo->db->prefix."wpforo_posts` WHERE `postid` = " . intval($args['postid']);
799 $forumid = $this->wpforo->db->get_var($sql);
800 return $this->get_layout(array( 'forumid' => $forumid ));
801 }
802 }
803
804 }
805
806 function get_forum_url($forum){
807
808 if( !is_array($forum) ){
809 if(is_numeric($forum)){
810 $forum = $this->get_forum($forum);
811 }else{
812 $forum = array('slug' => $forum);
813 }
814 }
815
816 if( is_array($forum) && !empty($forum) ){
817 return wpforo_home_url( utf8_uri_encode($forum['slug']) );
818 }else{
819 return wpforo_home_url();
820 }
821 }
822
823 function get_all_relative_ids($forumid, &$relative_ids){
824 $forum = $this->wpforo->db->get_row("SELECT `parentid`, `forumid` FROM `".$this->wpforo->db->prefix."wpforo_forums` WHERE `forumid` = ".intval($forumid), ARRAY_A);
825
826 if($forum['parentid'] != 0){
827 $relative_ids[] = $forum['forumid'];
828 $this->get_all_relative_ids($forum['parentid'], $relative_ids);
829 }else{
830 $relative_ids[] = $forum['forumid'];
831 $relative_ids = array_reverse($relative_ids);
832 }
833 }
834
835 function get_count( $args = array() ){
836 $sql = "SELECT COUNT(`forumid`) FROM `".$this->wpforo->db->prefix."wpforo_forums`";
837 if( !empty($args) ){
838 $wheres = array();
839 foreach ($args as $key => $value) $wheres[] = "`$key` = " . intval($value);
840 if($wheres) $sql .= " WHERE " . implode(' AND ', $wheres);
841 }
842 return $this->wpforo->db->get_var($sql);
843 }
844
845 function get_lastinfo( $ids = array() ){
846 $lastinfo = array();
847 if(!empty($ids)){
848 $ids = implode(',', array_map('intval', $ids));
849 $lastinfo = $this->wpforo->db->get_row( "SELECT `userid` as last_userid, `topicid` as last_topicid, `postid` as last_postid, `created` as last_post_date FROM `" . $this->wpforo->db->prefix . "wpforo_posts` WHERE forumid IN(" . $ids .") ORDER BY `created` DESC LIMIT 1", ARRAY_A);
850 }
851 return $lastinfo;
852 }
853 }
854
855 ?>