PluginProbe
wpForo Forum / 1.3.0
wpForo Forum v1.3.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-forums.php

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

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