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