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-forums.php

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

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