| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\classes; |
| 4 |
|
| 5 |
use stdClass; |
| 6 |
|
| 7 |
// Exit if accessed directly |
| 8 |
if( ! defined( 'ABSPATH' ) ) exit; |
| 9 |
|
| 10 |
class UserGroups { |
| 11 |
public $default; |
| 12 |
public $default_groupid; |
| 13 |
public $cans; |
| 14 |
public $current; |
| 15 |
private $post_flood_intervals; |
| 16 |
|
| 17 |
function __construct() { |
| 18 |
$this->init_defaults(); |
| 19 |
$this->cans = apply_filters( 'wpforo_usergroup_cans', $this->default->cans ); |
| 20 |
$this->init_hooks(); |
| 21 |
} |
| 22 |
|
| 23 |
private function init_defaults() { |
| 24 |
$this->default = new stdClass; |
| 25 |
|
| 26 |
$this->default->default_groupid = 3; |
| 27 |
|
| 28 |
$this->default->group = [ |
| 29 |
'groupid' => 0, |
| 30 |
'name' => '', |
| 31 |
'cans' => '', |
| 32 |
'description' => '', |
| 33 |
'utitle' => '', |
| 34 |
'role' => '', |
| 35 |
'access' => '', |
| 36 |
'color' => '', |
| 37 |
'visible' => 0, |
| 38 |
'secondary' => 0, |
| 39 |
'is_default' => 0, |
| 40 |
]; |
| 41 |
|
| 42 |
$this->default->post_flood_intervals = [ |
| 43 |
0 => 0, |
| 44 |
1 => 0, |
| 45 |
2 => 0, |
| 46 |
3 => 0, |
| 47 |
4 => 0, |
| 48 |
5 => 0, |
| 49 |
]; |
| 50 |
|
| 51 |
$this->default->cans = [ |
| 52 |
'mf' => __( 'Dashboard - Manage Boards & Forums', 'wpforo' ), |
| 53 |
'ms' => __( 'Dashboard - Manage Settings', 'wpforo' ), |
| 54 |
'mt' => __( 'Dashboard - Manage Tools', 'wpforo' ), |
| 55 |
'vm' => __( 'Dashboard - Manage Members', 'wpforo' ), |
| 56 |
'aum' => __( 'Dashboard - Moderate Topics & Posts', 'wpforo' ), |
| 57 |
'vmg' => __( 'Dashboard - Manage Usergroups', 'wpforo' ), |
| 58 |
'mp' => __( 'Dashboard - Manage Phrases', 'wpforo' ), |
| 59 |
'mth' => __( 'Dashboard - Manage Themes', 'wpforo' ), |
| 60 |
|
| 61 |
'em' => __( 'Dashboard - Can edit member', 'wpforo' ), |
| 62 |
'bm' => __( 'Dashboard - Can ban member', 'wpforo' ), |
| 63 |
'dm' => __( 'Dashboard - Can delete member', 'wpforo' ), |
| 64 |
|
| 65 |
'aup' => __( 'Front - Can pass moderation', 'wpforo' ), |
| 66 |
'view_stat' => __( 'Front - Can view statistic', 'wpforo' ), |
| 67 |
'vmem' => __( 'Front - Can view members', 'wpforo' ), |
| 68 |
'vprf' => __( 'Front - Can view profiles', 'wpforo' ), |
| 69 |
'vpra' => __( 'Front - Can view member activity', 'wpforo' ), |
| 70 |
'vprs' => __( 'Front - Can view member subscriptions', 'wpforo' ), |
| 71 |
|
| 72 |
'upc' => __( 'Front - Can upload cover', 'wpforo' ), |
| 73 |
'upa' => __( 'Front - Can upload avatar', 'wpforo' ), |
| 74 |
'ups' => __( 'Front - Can have signature', 'wpforo' ), |
| 75 |
'va' => __( 'Front - Can view avatars', 'wpforo' ), |
| 76 |
|
| 77 |
'vmu' => __( 'Front - Can view member username', 'wpforo' ), |
| 78 |
'vmm' => __( 'Front - Can view member email', 'wpforo' ), |
| 79 |
'vmt' => __( 'Front - Can view member title', 'wpforo' ), |
| 80 |
'vmct' => __( 'Front - Can view member custom title', 'wpforo' ), |
| 81 |
'vmr' => __( 'Front - Can view member reputation', 'wpforo' ), |
| 82 |
'vmw' => __( 'Front - Can view member website', 'wpforo' ), |
| 83 |
'vmsn' => __( 'Front - Can view member social networks', 'wpforo' ), |
| 84 |
'vmrd' => __( 'Front - Can view member reg. date', 'wpforo' ), |
| 85 |
'vml' => __( 'Front - Can view member location', 'wpforo' ), |
| 86 |
'vmo' => __( 'Front - Can view member occupation', 'wpforo' ), |
| 87 |
'vms' => __( 'Front - Can view member signature', 'wpforo' ), |
| 88 |
'vmam' => __( 'Front - Can view member about me', 'wpforo' ), |
| 89 |
'vwpm' => __( 'Front - Can write PM', 'wpforo' ), |
| 90 |
'caa' => __( 'Front - Can access to attachments', 'wpforo' ), |
| 91 |
'vt_add_topic' => __( 'Front - Can access to add topic page', 'wpforo' ), |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
private function init_options() { |
| 96 |
$this->default_groupid = $this->get_default_groupid( $this->default->default_groupid ); |
| 97 |
$this->post_flood_intervals = wpforo_get_option( 'wpforo_post_flood_intervals', $this->default->post_flood_intervals ); |
| 98 |
} |
| 99 |
|
| 100 |
public function init_current() { |
| 101 |
if( ! $this->current = $this->get_usergroup( WPF()->current_user_groupid ) ) { |
| 102 |
$this->current = $this->get_usergroup(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private function init_hooks() { |
| 107 |
// add_action('wpforo_after_add_usergroup', array($this, 'after_add_edit_usergroup')); |
| 108 |
// add_action('wpforo_after_edit_usergroup', array($this, 'after_add_edit_usergroup')); |
| 109 |
if( WPF()->is_installed() ) $this->init_options(); |
| 110 |
} |
| 111 |
|
| 112 |
public function get_flood_interval( $groupid, $obj = 'post' ) { |
| 113 |
$flood_interval = ( wpfkey( $this->post_flood_intervals, $groupid ) ? $this->post_flood_intervals[ $groupid ] : 3 ); |
| 114 |
|
| 115 |
return apply_filters( 'wpforo_usergroup_get_flood_interval', intval( $flood_interval ), $groupid, $obj ); |
| 116 |
} |
| 117 |
|
| 118 |
public function fix_group( $group ) { |
| 119 |
$group = wpforo_array_args_cast_and_merge( (array) $group, $this->default->group ); |
| 120 |
$cans = array_map( '__return_zero', $this->cans ); |
| 121 |
$group['cans'] = maybe_unserialize( $group['cans'] ); |
| 122 |
if( is_array( $group['cans'] ) ) { |
| 123 |
$group['cans'] = wpforo_array_args_cast_and_merge( $group['cans'], $cans ); |
| 124 |
} else { |
| 125 |
$group['cans'] = $cans; |
| 126 |
} |
| 127 |
|
| 128 |
return $group; |
| 129 |
} |
| 130 |
|
| 131 |
function usergroup_list_data() { |
| 132 |
$ugdata = []; |
| 133 |
$groups = WPF()->db->get_results( 'SELECT * FROM ' . WPF()->tables->usergroups . ' ORDER BY `name` ', ARRAY_A ); |
| 134 |
foreach( $groups as $group ) { |
| 135 |
$user_count = WPF()->db->get_var( "SELECT COUNT(*) FROM " . WPF()->tables->profiles . " WHERE `groupid` = " . intval( $group['groupid'] ) . " OR FIND_IN_SET(" . intval( $group['groupid'] ) . ", `secondary_groupids`)" ); |
| 136 |
$ugdata[ $group['groupid'] ]['groupid'] = intval( $group['groupid'] ); |
| 137 |
$ugdata[ $group['groupid'] ]['name'] = wpforo_phrase( $group['name'], false ); |
| 138 |
$ugdata[ $group['groupid'] ]['role'] = $group['role']; |
| 139 |
$ugdata[ $group['groupid'] ]['count'] = intval( $user_count ); |
| 140 |
$ugdata[ $group['groupid'] ]['access'] = $group['access']; |
| 141 |
$ugdata[ $group['groupid'] ]['color'] = $group['color']; |
| 142 |
$ugdata[ $group['groupid'] ]['secondary'] = $group['secondary']; |
| 143 |
} |
| 144 |
|
| 145 |
return $ugdata; |
| 146 |
} |
| 147 |
|
| 148 |
function add( $title, $cans = [], $description = '', $role = 'subscriber', $access = 'standard', $color = '', $visible = 1, $secondary = 0 ) { |
| 149 |
$i = 2; |
| 150 |
$real_title = $title; |
| 151 |
while( WPF()->db->get_var( |
| 152 |
WPF()->db->prepare( |
| 153 |
"SELECT `groupid` FROM `" . WPF()->tables->usergroups . "` WHERE `name` = %s", |
| 154 |
sanitize_text_field( $title ) |
| 155 |
) |
| 156 |
) ) { |
| 157 |
$title = $real_title . '-' . $i; |
| 158 |
$i ++; |
| 159 |
} |
| 160 |
|
| 161 |
$group = [ |
| 162 |
'name' => sanitize_text_field( $title ), |
| 163 |
'cans' => serialize( wpforo_parse_args( $cans, array_map( '__return_zero', $this->cans ) ) ), |
| 164 |
'description' => $description, |
| 165 |
'utitle' => sanitize_text_field( $real_title ), |
| 166 |
'role' => $role, |
| 167 |
'access' => $access, |
| 168 |
'color' => $color, |
| 169 |
'visible' => $visible, |
| 170 |
'secondary' => $secondary, |
| 171 |
]; |
| 172 |
|
| 173 |
if( WPF()->db->insert( WPF()->tables->usergroups, $group, [ '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d' ] ) ) { |
| 174 |
$group['groupid'] = WPF()->db->insert_id; |
| 175 |
|
| 176 |
do_action( 'wpforo_after_add_usergroup', $group ); |
| 177 |
|
| 178 |
WPF()->notice->add( 'User group successfully added', 'success' ); |
| 179 |
|
| 180 |
return $group['groupid']; |
| 181 |
} |
| 182 |
|
| 183 |
WPF()->notice->add( 'User group add error', 'error' ); |
| 184 |
|
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
function edit( $groupid, $title, $cans, $description = '', $role = null, $access = null, $color = '', $visible = 1, $secondary = 0 ) { |
| 189 |
if( ! $this->can( 'vmg' ) ) { |
| 190 |
WPF()->notice->add( 'Permission denied', 'error' ); |
| 191 |
|
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
if( $groupid = intval( $groupid ) ) { |
| 196 |
$old_group = $this->get_usergroup( $groupid ); |
| 197 |
$group = [ |
| 198 |
'name' => sanitize_text_field( $title ), |
| 199 |
'cans' => serialize( wpforo_parse_args( $cans, array_map( '__return_zero', $this->cans ) ) ), |
| 200 |
'description' => $description, |
| 201 |
'utitle' => $old_group['utitle'], |
| 202 |
'role' => is_null( $role ) ? $old_group['role'] : $role, |
| 203 |
'access' => is_null( $access ) ? $old_group['access'] : $access, |
| 204 |
'color' => $color, |
| 205 |
'visible' => $visible, |
| 206 |
'secondary' => $secondary, |
| 207 |
]; |
| 208 |
|
| 209 |
if( false !== WPF()->db->update( WPF()->tables->usergroups, $group, [ 'groupid' => $groupid ], [ '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d' ], [ '%d' ] ) ) { |
| 210 |
$group['groupid'] = $groupid; |
| 211 |
|
| 212 |
do_action( 'wpforo_after_edit_usergroup', $group ); |
| 213 |
|
| 214 |
WPF()->notice->add( 'User group successfully edited', 'success' ); |
| 215 |
|
| 216 |
return $groupid; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
WPF()->notice->add( 'User group edit error', 'error' ); |
| 221 |
|
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
function delete( $groupid, $mergeid ) { |
| 226 |
if( ! $this->can( 'vmg' ) ) { |
| 227 |
WPF()->notice->add( 'Permission denied', 'error' ); |
| 228 |
|
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
if( ( $groupid = intval( $groupid ) ) && ! in_array( $groupid, [ 1, 4 ] ) ) { |
| 233 |
if( $mergeid = intval( $mergeid ) ) { |
| 234 |
$sql = "UPDATE `" . WPF()->tables->profiles . "` SET `groupid` = %d WHERE `groupid` = %d"; |
| 235 |
WPF()->db->query( WPF()->db->prepare( $sql, $mergeid, $groupid ) ); |
| 236 |
} |
| 237 |
|
| 238 |
if( false !== WPF()->db->delete( WPF()->tables->usergroups, [ 'groupid' => $groupid ], [ '%d' ] ) ) { |
| 239 |
WPF()->notice->add( wpforo_phrase( 'Usergroup has been successfully deleted. All users of this usergroup have been moved to the usergroup you\'ve chosen', false ), 'success' ); |
| 240 |
|
| 241 |
return $groupid; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
WPF()->notice->add( 'Can\'t delete this Usergroup', 'error' ); |
| 246 |
|
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
public function _get_usergroup( $groupid = 4 ) { |
| 251 |
return WPF()->db->get_row( |
| 252 |
WPF()->db->prepare( |
| 253 |
"SELECT * FROM `" . WPF()->tables->usergroups . "` WHERE `groupid` = %d", |
| 254 |
$groupid |
| 255 |
), |
| 256 |
ARRAY_A |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
public function get_usergroup( $groupid = 4 ) { |
| 261 |
return wpforo_ram_get( [ $this, '_get_usergroup' ], $groupid ); |
| 262 |
} |
| 263 |
|
| 264 |
public function _get_usergroups( $field = 'full' ) { |
| 265 |
if( $field === 'full' ) { |
| 266 |
$groups = (array) WPF()->db->get_results( "SELECT * FROM `" . WPF()->tables->usergroups . "`", ARRAY_A ); |
| 267 |
} else { |
| 268 |
$groups = WPF()->db->get_col( "SELECT `$field` FROM `" . WPF()->tables->usergroups . "`" ); |
| 269 |
} |
| 270 |
|
| 271 |
return $groups; |
| 272 |
} |
| 273 |
|
| 274 |
public function get_usergroups( $field = 'full' ) { |
| 275 |
return wpforo_ram_get( [ $this, '_get_usergroups' ], $field ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* @param array|int $selected |
| 280 |
* @param array|int $exclude |
| 281 |
* |
| 282 |
* @return string |
| 283 |
*/ |
| 284 |
public function get_selectbox( $selected = [], $exclude = [] ) { |
| 285 |
$selected = array_map( 'intval', (array) $selected ); |
| 286 |
$exclude = array_map( 'intval', (array) $exclude ); |
| 287 |
$html = ''; |
| 288 |
foreach( $this->usergroup_list_data() as $group ) { |
| 289 |
if( in_array( $group['groupid'], $exclude ) ) continue; |
| 290 |
$html .= sprintf( |
| 291 |
'<option value="%1$s" %2$s>%3$s</option>', |
| 292 |
intval( $group['groupid'] ), |
| 293 |
in_array( $group['groupid'], $selected ) ? ' selected ' : '', |
| 294 |
esc_html( $group['name'] ) . "\t(" . $group['count'] . ")" |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
return $html; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* @param array|int $selected |
| 303 |
* @param array|int $exclude |
| 304 |
*/ |
| 305 |
public function show_selectbox( $selected = [], $exclude = [] ) { |
| 306 |
echo $this->get_selectbox( $selected, $exclude ); |
| 307 |
} |
| 308 |
|
| 309 |
function get_visible_usergroup_ids() { |
| 310 |
$key = [ 'usergroup', 'get_visible_usergroup_ids' ]; |
| 311 |
if( WPF()->ram_cache->exists( $key ) ) return WPF()->ram_cache->get( $key ); |
| 312 |
$col = WPF()->db->get_col( "SELECT `groupid` FROM `" . WPF()->tables->usergroups . "` WHERE `visible` = 1" ); |
| 313 |
WPF()->ram_cache->set( $key, $col ); |
| 314 |
|
| 315 |
return $col; |
| 316 |
} |
| 317 |
|
| 318 |
public function _get_secondary_groupids() { |
| 319 |
return WPF()->db->get_col( "SELECT `groupid` FROM `" . WPF()->tables->usergroups . "` WHERE `groupid` NOT IN(1,2,4) AND `secondary` = 1" ); |
| 320 |
} |
| 321 |
|
| 322 |
public function get_secondary_groupids() { |
| 323 |
return wpforo_ram_get( [$this, '_get_secondary_groupids'] ); |
| 324 |
} |
| 325 |
|
| 326 |
function get_secondary_group_names( $ids ) { |
| 327 |
if( ! is_array( $ids ) ) $ids = explode( ',', $ids ); |
| 328 |
$ids = array_map( 'intval', $ids ); |
| 329 |
$ids = array_diff( $ids, [ 1, 2, 4 ] ); |
| 330 |
if( $ids ) { |
| 331 |
$ids = implode( ',', $ids ); |
| 332 |
|
| 333 |
return WPF()->db->get_col( "SELECT `name` FROM `" . WPF()->tables->usergroups . "` WHERE `secondary` = 1 AND `groupid` IN (" . esc_sql( $ids ) . ")" ); |
| 334 |
} |
| 335 |
|
| 336 |
return []; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* @deprecated since 2.1.6, instead of this method you can use $this->get_secondary_groups() |
| 341 |
*/ |
| 342 |
function get_secondary_usergroups() { |
| 343 |
return $this->get_secondary_groups(); |
| 344 |
} |
| 345 |
|
| 346 |
function get_secondary_groups() { |
| 347 |
return wpforo_ram_get( [ $this, '_get_secondary_groups' ] ); |
| 348 |
} |
| 349 |
|
| 350 |
function _get_secondary_groups() { |
| 351 |
$order_by = apply_filters('wpforo_get_secondary_groups_order_by', 'ORDER BY `groupid` ASC'); |
| 352 |
return (array) WPF()->db->get_results( "SELECT * FROM `" . WPF()->tables->usergroups . "` WHERE `groupid` NOT IN(1,2,4) AND `secondary` = 1 " . esc_sql( $order_by ), ARRAY_A ); |
| 353 |
} |
| 354 |
|
| 355 |
function get_groupids_by_role( $role ) { |
| 356 |
if( $role ) { |
| 357 |
$ugids = WPF()->db->get_col( "SELECT `groupid` FROM `" . WPF()->tables->usergroups . "` WHERE `role` = '" . esc_sql( $role ) . "' ORDER BY `groupid` ASC" ); |
| 358 |
if( ! empty( $ugids ) ) { |
| 359 |
return $ugids; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return null; |
| 364 |
} |
| 365 |
|
| 366 |
function get_group_by_role( $role ) { |
| 367 |
if( $role ) { |
| 368 |
$usergroups = WPF()->db->get_results( "SELECT * FROM `" . WPF()->tables->usergroups . "` WHERE `role` = '" . esc_sql( $role ) . "' ORDER BY `groupid` ASC", ARRAY_A ); |
| 369 |
if( ! empty( $usergroups ) ) { |
| 370 |
return $usergroups; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
return null; |
| 375 |
} |
| 376 |
|
| 377 |
function get_roles() { |
| 378 |
$roles = wp_roles(); |
| 379 |
$roles = $roles->get_names(); |
| 380 |
|
| 381 |
return $roles; |
| 382 |
} |
| 383 |
|
| 384 |
function get_roles_ug() { |
| 385 |
$roles_ug = WPF()->db->get_results( "SELECT `name`, `role` FROM `" . WPF()->tables->usergroups . "`", ARRAY_A ); |
| 386 |
$roles = wp_roles(); |
| 387 |
$roles = $roles->get_names(); |
| 388 |
if( ! empty( $roles ) ) { |
| 389 |
foreach( $roles as $role => $name ) { |
| 390 |
foreach( $roles_ug as $ug ) { |
| 391 |
if( wpfval( $ug, 'role' ) && $role == $ug['role'] ) { |
| 392 |
$roles_ug[ $role ][] = $ug['name']; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
return $roles_ug; |
| 399 |
} |
| 400 |
|
| 401 |
function get_roles_woug() { |
| 402 |
$roles_woug = []; |
| 403 |
$roles_ug = WPF()->db->get_col( "SELECT `role` FROM `" . WPF()->tables->usergroups . "` GROUP BY `role`" ); |
| 404 |
$roles = wp_roles(); |
| 405 |
$roles = $roles->get_names(); |
| 406 |
if( ! empty( $roles ) ) { |
| 407 |
foreach( $roles as $role => $name ) { |
| 408 |
if( ! in_array( $role, $roles_ug ) ) { |
| 409 |
$roles_woug[ $role ] = $name; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
return $roles_woug; |
| 415 |
} |
| 416 |
|
| 417 |
function get_role_usergroup_relation() { |
| 418 |
$roles = []; |
| 419 |
$data = WPF()->db->get_results( "SELECT `groupid`, `role` FROM `" . WPF()->tables->usergroups . "` ORDER BY `groupid` DESC", ARRAY_A ); |
| 420 |
if( ! empty( $data ) ) { |
| 421 |
$data = array_map( function($d){ $d['groupid'] = intval( $d['groupid'] ); return $d; }, $data ); |
| 422 |
foreach( $data as $rel ) { |
| 423 |
if( $rel['groupid'] === 1 && in_array( $rel['role'], [ 'subscriber', 'contributor' ], true ) ) { |
| 424 |
$roles['administrator'] = $rel['groupid']; |
| 425 |
} elseif( $rel['groupid'] === 2 && $rel['role'] === 'subscriber' ) { |
| 426 |
$roles['editor'] = $rel['groupid']; |
| 427 |
} elseif( $rel['role'] ) { |
| 428 |
$roles[ $rel['role'] ] = $rel['groupid']; |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return $roles; |
| 434 |
} |
| 435 |
|
| 436 |
function get_usergroup_role_relation() { |
| 437 |
$usergroups = []; |
| 438 |
$data = WPF()->db->get_results( "SELECT `groupid`, `role` FROM `" . WPF()->tables->usergroups . "`", ARRAY_A ); |
| 439 |
if( ! empty( $data ) ) { |
| 440 |
foreach( $data as $rel ) { |
| 441 |
$usergroups[ $rel['groupid'] ] = $rel['role']; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
return $usergroups; |
| 446 |
} |
| 447 |
|
| 448 |
function get_usergroup_access_relation() { |
| 449 |
$usergroups = []; |
| 450 |
$data = WPF()->db->get_results( "SELECT `groupid`, `access` FROM `" . WPF()->tables->usergroups . "`", ARRAY_A ); |
| 451 |
if( ! empty( $data ) ) { |
| 452 |
foreach( $data as $rel ) { |
| 453 |
$usergroups[ (int) $rel['groupid'] ] = $rel['access']; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
return $usergroups; |
| 458 |
} |
| 459 |
|
| 460 |
function set_ug_roles( $ug_role ) { |
| 461 |
if( ! empty( $ug_role ) ) { |
| 462 |
foreach( $ug_role as $usergroupid => $role ) { |
| 463 |
$role = sanitize_text_field( $role ); |
| 464 |
WPF()->db->query( "UPDATE " . WPF()->tables->usergroups . " SET `role` = '" . esc_sql( $role ) . "' WHERE `groupid` = " . intval( $usergroupid ) ); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
function set_users_groupid( $groupid_userids ) { |
| 470 |
$status = [ 'error' => 0, 'success' => false ]; |
| 471 |
if( ! empty( $groupid_userids ) ) { |
| 472 |
foreach( $groupid_userids as $group_id => $user_ids ) { |
| 473 |
if( $group_id && ! empty( $user_ids ) ) { |
| 474 |
$userids = implode( ',', $user_ids ); |
| 475 |
$sql = "UPDATE " . WPF()->tables->profiles . " SET `groupid` = " . intval( $group_id ) . " WHERE `userid` IN(" . esc_sql( $userids ) . ")"; |
| 476 |
if( false === WPF()->db->query( $sql ) ) { |
| 477 |
$status['error'] = WPF()->db->last_error; |
| 478 |
$status['success'] = false; |
| 479 |
break; |
| 480 |
} else { |
| 481 |
$status['success'] = true; |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
do_action( 'wpforo_set_users_groupid', $groupid_userids, $status ); |
| 487 |
|
| 488 |
return $status; |
| 489 |
} |
| 490 |
|
| 491 |
function build_users_groupid_array( $groupid_role_or_role_groupid, $users, $is_groupid_role = false ) { |
| 492 |
$array = []; |
| 493 |
$group_users = []; |
| 494 |
$user_prime_group = []; |
| 495 |
$user_second_groups = []; |
| 496 |
if( ! empty( $users ) ) { |
| 497 |
foreach( $users as $user ) { |
| 498 |
if( ! empty( $user->roles ) ) { |
| 499 |
foreach( $user->roles as $role ) { |
| 500 |
//$ugids = wpforo_key( $usergroupid_role, $role, 'sort' ); |
| 501 |
$ugids = []; |
| 502 |
if( !empty( $groupid_role_or_role_groupid ) ){ |
| 503 |
if( ! $is_groupid_role ){ |
| 504 |
foreach ( $groupid_role_or_role_groupid as $_role => $_groupid ){ |
| 505 |
if( $_role === $role ){ |
| 506 |
$ugids[] = $_groupid; |
| 507 |
} |
| 508 |
} |
| 509 |
} else { |
| 510 |
foreach ( $groupid_role_or_role_groupid as $_groupid => $_role ){ |
| 511 |
if( $_role === $role ){ |
| 512 |
$ugids[] = $_groupid; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
sort( $ugids ); |
| 518 |
|
| 519 |
$ug_count = count( $ugids ); |
| 520 |
if( ! empty( $ugids ) ) { |
| 521 |
foreach( $ugids as $ugid ) { |
| 522 |
// The count of $ug_count === 1 means the user role is synced, |
| 523 |
// it has one unique usergroup. |
| 524 |
if( $ug_count === 1 ) { |
| 525 |
if( ! isset( $user_prime_group[ $user->ID ] ) ) { |
| 526 |
$user_prime_group[ $user->ID ][] = $ugid; |
| 527 |
$group_users[ $ugid ][] = intval( $user->ID ); |
| 528 |
} else { |
| 529 |
$user_second_groups[ $user->ID ][] = $ugid; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
$array['group_users'] = $group_users; |
| 539 |
$array['user_prime_group'] = $user_prime_group; |
| 540 |
$array['user_second_groups'] = $user_second_groups; |
| 541 |
|
| 542 |
return $array; |
| 543 |
} |
| 544 |
|
| 545 |
public function after_add_edit_usergroup( $group ) { |
| 546 |
if( wpforo_setting( 'authorization', 'role_synch' ) ) { |
| 547 |
$limit = apply_filters( 'wpforo_synch_roles_users_limit', 5000 ); |
| 548 |
$users = get_users( [ 'role' => $group['role'], 'number' => $limit ] ); |
| 549 |
if( ! empty( $users ) ) { |
| 550 |
if( count( $users ) <= $limit ) { |
| 551 |
$status = wpforo_synch_role( [ $group['groupid'] => $group['role'] ], $users ); |
| 552 |
wpforo_clean_cache( 'user' ); |
| 553 |
if( $error = wpfval( $status, 'error' ) ) { |
| 554 |
WPF()->notice->add( $error, 'error' ); |
| 555 |
} |
| 556 |
} else { |
| 557 |
WPF()->notice->add( 'Please make sure you don\'t have not-synched Roles in the "User Roles" table below, then click on the [Synchronize] button to update users Usergroup IDs.', 'error' ); |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
public function can( $do, $groupids = null ) { |
| 564 |
if( is_null( $groupids ) ) { |
| 565 |
if( current_user_can( 'administrator' ) ) return 1; |
| 566 |
$groupids = WPF()->current_user_groupids; |
| 567 |
} |
| 568 |
|
| 569 |
if( $groupids = array_map( 'intval', (array) $groupids ) ) { |
| 570 |
foreach( $groupids as $groupid ) { |
| 571 |
if( $groupid ) { |
| 572 |
$group = $this->get_usergroup( $groupid ); |
| 573 |
if( wpfval( $group, 'cans' ) ){ |
| 574 |
$group_cans = unserialize( $group['cans'] ); |
| 575 |
if( (int) wpfval( $group_cans, $do ) ) return 1; |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
public function get_groupids_by_can( $do ) { |
| 585 |
$usergroupids = []; |
| 586 |
$usergroups = $this->get_usergroups(); |
| 587 |
foreach( $usergroups as $usergroup ) { |
| 588 |
$cans = unserialize( $usergroup['cans'] ); |
| 589 |
if( isset( $cans[ $do ] ) && $cans[ $do ] ) { |
| 590 |
$usergroupids[] = $usergroup['groupid']; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
return $usergroupids; |
| 595 |
} |
| 596 |
|
| 597 |
public function can_manage_forum( $groupids = null ) { |
| 598 |
return ( $this->can( 'cf', $groupids ) && $this->can( 'ef', $groupids ) && $this->can( 'df', $groupids ) ) || $this->can( 'mf', $groupids ); |
| 599 |
} |
| 600 |
|
| 601 |
public function set_default( $groupid ) { |
| 602 |
if( in_array( intval($groupid), [1,4], true ) ) return false; |
| 603 |
if( WPF()->db->update( |
| 604 |
WPF()->tables->usergroups, |
| 605 |
['is_default' => 1], |
| 606 |
['groupid' => $groupid], |
| 607 |
['%d'], |
| 608 |
['%d'] |
| 609 |
)){ |
| 610 |
return false !== WPF()->db->query( WPF()->db->prepare( |
| 611 |
"UPDATE `" . WPF()->tables->usergroups . "` SET `is_default` = 0 WHERE `groupid` <> %d", |
| 612 |
$groupid |
| 613 |
)); |
| 614 |
} |
| 615 |
|
| 616 |
return false; |
| 617 |
} |
| 618 |
|
| 619 |
public function get_default_groupid( $default_groupid ){ |
| 620 |
$groupid = (int) WPF()->db->get_var( |
| 621 |
"SELECT `groupid` FROM `" . WPF()->tables->usergroups . "` WHERE `is_default` = 1 ORDER BY `groupid` DESC" |
| 622 |
); |
| 623 |
if( !$groupid ) $groupid = $default_groupid; |
| 624 |
|
| 625 |
return $groupid; |
| 626 |
} |
| 627 |
} |
| 628 |
|