| 1 |
<?php |
| 2 |
/** |
| 3 |
* UamUserGroup.class.php |
| 4 |
* |
| 5 |
* The UamUserGroup class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @category UserAccessManager |
| 10 |
* @package UserAccessManager |
| 11 |
* @author Alexander Schneider <alexanderschneider85@googlemail.com> |
| 12 |
* @copyright 2008-2010 Alexander Schneider |
| 13 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 14 |
* @version SVN: $Id$ |
| 15 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* The user group class. |
| 20 |
* |
| 21 |
* @category UserAccessManager |
| 22 |
* @package UserAccessManager |
| 23 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 24 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 25 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 26 |
*/ |
| 27 |
|
| 28 |
class UamUserGroup |
| 29 |
{ |
| 30 |
protected $accessHandler = null; |
| 31 |
protected $id = null; |
| 32 |
protected $groupName = null; |
| 33 |
protected $groupDesc = null; |
| 34 |
protected $readAccess = null; |
| 35 |
protected $writeAccess = null; |
| 36 |
protected $ipRange = null; |
| 37 |
protected $roles = array(); |
| 38 |
protected $objects = null; |
| 39 |
protected $singleObjects = null; |
| 40 |
private $_assignedObjects = null; |
| 41 |
protected $plObjects = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Consturtor |
| 45 |
* |
| 46 |
* @param object $uamAccessHandler The access handler object. |
| 47 |
* @param integer $id The id of the user group. |
| 48 |
* |
| 49 |
* @return null |
| 50 |
*/ |
| 51 |
public function __construct($uamAccessHandler, $id = null) |
| 52 |
{ |
| 53 |
$this->accessHandler = $uamAccessHandler; |
| 54 |
|
| 55 |
//Create default values for the objects. |
| 56 |
foreach ($this->getAllObjectTypes() as $objectType) { |
| 57 |
$this->objects[$objectType] = array( |
| 58 |
'real' => -1, |
| 59 |
'full' => -1 |
| 60 |
); |
| 61 |
$this->_assignedObjects[$objectType] = null; |
| 62 |
} |
| 63 |
|
| 64 |
if ($id !== null) { |
| 65 |
global $wpdb; |
| 66 |
|
| 67 |
$this->id = $id; |
| 68 |
|
| 69 |
$dbUsergroup = $wpdb->get_row( |
| 70 |
"SELECT * |
| 71 |
FROM ".DB_ACCESSGROUP." |
| 72 |
WHERE ID = ".$this->getId()." |
| 73 |
LIMIT 1", |
| 74 |
ARRAY_A |
| 75 |
); |
| 76 |
|
| 77 |
$this->groupName = $dbUsergroup['groupname']; |
| 78 |
$this->groupDesc = $dbUsergroup['groupdesc']; |
| 79 |
$this->readAccess = $dbUsergroup['read_access']; |
| 80 |
$this->writeAccess = $dbUsergroup['write_access']; |
| 81 |
$this->ipRange = $dbUsergroup['ip_range']; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Returns the user access handler object. |
| 87 |
* |
| 88 |
* @return object |
| 89 |
*/ |
| 90 |
public function &getAccessHandler() |
| 91 |
{ |
| 92 |
return $this->accessHandler; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Deletes the user group. |
| 97 |
* |
| 98 |
* @return null |
| 99 |
*/ |
| 100 |
public function delete() |
| 101 |
{ |
| 102 |
if ($this->id == null) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
global $wpdb; |
| 107 |
|
| 108 |
$wpdb->query( |
| 109 |
"DELETE FROM " . DB_ACCESSGROUP . " |
| 110 |
WHERE ID = $this->id LIMIT 1" |
| 111 |
); |
| 112 |
|
| 113 |
foreach ($this->getAllObjectTypes() as $objectType) { |
| 114 |
$this->_deleteObjectsFromDb($objectType); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Saves the user group. |
| 120 |
* |
| 121 |
* @return null; |
| 122 |
*/ |
| 123 |
public function save() |
| 124 |
{ |
| 125 |
global $wpdb; |
| 126 |
|
| 127 |
if ($this->id == null) { |
| 128 |
$wpdb->query( |
| 129 |
"INSERT INTO " . DB_ACCESSGROUP . " ( |
| 130 |
ID, |
| 131 |
groupname, |
| 132 |
groupdesc, |
| 133 |
read_access, |
| 134 |
write_access, |
| 135 |
ip_range |
| 136 |
) |
| 137 |
VALUES ( |
| 138 |
NULL, |
| 139 |
'" . $this->groupName . "', |
| 140 |
'" . $this->groupDesc . "', |
| 141 |
'" . $this->readAccess . "', |
| 142 |
'" . $this->writeAccess . "', |
| 143 |
'" . $this->ipRange . "' |
| 144 |
)" |
| 145 |
); |
| 146 |
|
| 147 |
$this->id = $wpdb->insert_id; |
| 148 |
} else { |
| 149 |
$wpdb->query( |
| 150 |
"UPDATE " . DB_ACCESSGROUP . " |
| 151 |
SET groupname = '" . $this->groupName . "', |
| 152 |
groupdesc = '" . $this->groupDesc . "', |
| 153 |
read_access = '" . $this->readAccess . "', |
| 154 |
write_access = '" . $this->writeAccess . "', |
| 155 |
ip_range = '" . $this->ipRange . "' |
| 156 |
WHERE ID = " . $this->id |
| 157 |
); |
| 158 |
|
| 159 |
foreach ($this->getAllObjectTypes() as $objectType) { |
| 160 |
//Load to object |
| 161 |
$this->getObjectsFromType($objectType); |
| 162 |
//Delete form database |
| 163 |
$this->_deleteObjectsFromDb($objectType); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
foreach ($this->getAllObjectTypes() as $objectType) { |
| 168 |
foreach ($this->getObjectsFromType($objectType) as $key => $object) { |
| 169 |
$sql = sprintf( |
| 170 |
$this->_getSqlQuery($objectType, 'insert'), |
| 171 |
trim($key) |
| 172 |
); |
| 173 |
$wpdb->query($sql); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/* |
| 180 |
* Primary values. |
| 181 |
*/ |
| 182 |
|
| 183 |
/** |
| 184 |
* Returns the group id. |
| 185 |
* |
| 186 |
* @return integer |
| 187 |
*/ |
| 188 |
public function getId() |
| 189 |
{ |
| 190 |
return $this->id; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Returns the group name. |
| 195 |
* |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public function getGroupName() |
| 199 |
{ |
| 200 |
return $this->groupName; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Sets the group name. |
| 205 |
* |
| 206 |
* @param string $groupName The new group name. |
| 207 |
* |
| 208 |
* @return null |
| 209 |
*/ |
| 210 |
public function setGroupName($groupName) |
| 211 |
{ |
| 212 |
$this->groupName = $groupName; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the group description. |
| 217 |
* |
| 218 |
* @return string |
| 219 |
*/ |
| 220 |
public function getGroupDesc() |
| 221 |
{ |
| 222 |
return $this->groupDesc; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Sets the group description. |
| 227 |
* |
| 228 |
* @param string $groupDesc The new group description. |
| 229 |
* |
| 230 |
* @return null |
| 231 |
*/ |
| 232 |
public function setGroupDesc($groupDesc) |
| 233 |
{ |
| 234 |
$this->groupDesc = $groupDesc; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Returns the read access. |
| 239 |
* |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
public function getReadAccess() |
| 243 |
{ |
| 244 |
return $this->readAccess; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Sets the read access. |
| 249 |
* |
| 250 |
* @param string $readAccess The read access. |
| 251 |
* |
| 252 |
* @return null |
| 253 |
*/ |
| 254 |
public function setReadAccess($readAccess) |
| 255 |
{ |
| 256 |
$this->readAccess = $readAccess; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Returns the write access. |
| 261 |
* |
| 262 |
* @return string |
| 263 |
*/ |
| 264 |
public function getWriteAccess() |
| 265 |
{ |
| 266 |
return $this->writeAccess; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Sets the write access. |
| 271 |
* |
| 272 |
* @param string $writeAccess The wirte access. |
| 273 |
* |
| 274 |
* @return null |
| 275 |
*/ |
| 276 |
public function setWriteAccess($writeAccess) |
| 277 |
{ |
| 278 |
$this->writeAccess = $writeAccess; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Returns the ip range. |
| 283 |
* |
| 284 |
* @param string $type The return type. |
| 285 |
* |
| 286 |
* @return array |
| 287 |
*/ |
| 288 |
public function getIpRange($type = null) |
| 289 |
{ |
| 290 |
if ($type == 'string') { |
| 291 |
return $this->ipRange; |
| 292 |
} |
| 293 |
|
| 294 |
$ipRange = explode(';', $this->ipRange); |
| 295 |
|
| 296 |
if ($ipRange[0] == null) { |
| 297 |
return null; |
| 298 |
} |
| 299 |
|
| 300 |
return $ipRange; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Sets the ip range. |
| 305 |
* |
| 306 |
* @param string $ipRange The new ip range. |
| 307 |
* |
| 308 |
* @return null |
| 309 |
*/ |
| 310 |
public function setIpRange($ipRange) |
| 311 |
{ |
| 312 |
if (is_array($ipRange)) { |
| 313 |
$ipRange = implode(';', $ipRange); |
| 314 |
} |
| 315 |
|
| 316 |
$this->ipRange = $ipRange; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Returns all objects types. |
| 321 |
* |
| 322 |
* @return array |
| 323 |
*/ |
| 324 |
public function getAllObjectTypes() |
| 325 |
{ |
| 326 |
return $this->getAccessHandler()->getAllObjectTypes(); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
/* |
| 331 |
* Meta functions. |
| 332 |
*/ |
| 333 |
|
| 334 |
/** |
| 335 |
* Magic method getter. |
| 336 |
* |
| 337 |
* @param string $name The name of the function |
| 338 |
* @param array $arguments The arguments for the function |
| 339 |
* |
| 340 |
* @return mixed |
| 341 |
*/ |
| 342 |
public function __call($name, $arguments) |
| 343 |
{ |
| 344 |
$uam = $this->getAccessHandler()->getUserAccessManager(); |
| 345 |
|
| 346 |
$action = ''; |
| 347 |
|
| 348 |
if ($uam->startsWith($name, 'add')) { |
| 349 |
$prefix = 'add'; |
| 350 |
} elseif ($uam->startsWith($name, 'remove')) { |
| 351 |
$prefix = 'remove'; |
| 352 |
} elseif ($uam->startsWith($name, 'isMember')) { |
| 353 |
$prefix = 'isMember'; |
| 354 |
} |
| 355 |
|
| 356 |
$objectType = str_replace($prefix, '', $name); |
| 357 |
$objectType = strtolower($objectType); |
| 358 |
|
| 359 |
$objectId = $arguments[0]; |
| 360 |
|
| 361 |
if ($prefix == 'add') { |
| 362 |
return $this->addObject( |
| 363 |
$objectType, |
| 364 |
$objectId |
| 365 |
); |
| 366 |
} elseif ($prefix == 'remove') { |
| 367 |
return $this->removeObject( |
| 368 |
$objectType, |
| 369 |
$objectId |
| 370 |
); |
| 371 |
} elseif ($prefix == 'ismember') { |
| 372 |
$withInfo = $arguments[1]; |
| 373 |
|
| 374 |
return $this->objectIsMember( |
| 375 |
$objectType, |
| 376 |
$objectId, |
| 377 |
$withInfo |
| 378 |
); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Returns the sql query. |
| 384 |
* |
| 385 |
* @param string $objectType The object type. |
| 386 |
* @param string $action The sql action. |
| 387 |
* |
| 388 |
* @return string |
| 389 |
*/ |
| 390 |
private function _getSqlQuery($objectType, $action) |
| 391 |
{ |
| 392 |
$sql = ''; |
| 393 |
|
| 394 |
if ($action == 'select') { |
| 395 |
$sql = "SELECT object_id as id |
| 396 |
FROM ".DB_ACCESSGROUP_TO_OBJECT." |
| 397 |
WHERE group_id = ".$this->getId()." |
| 398 |
AND object_type = '".$objectType ."'"; |
| 399 |
} elseif ($action == 'delete') { |
| 400 |
$sql = "DELETE FROM ".DB_ACCESSGROUP_TO_OBJECT." |
| 401 |
WHERE group_id = ".$this->getId()." |
| 402 |
AND object_type = '".$objectType ."'"; |
| 403 |
} elseif ($action == 'insert') { |
| 404 |
$sql = "INSERT INTO ".DB_ACCESSGROUP_TO_OBJECT." ( |
| 405 |
group_id, |
| 406 |
object_id, |
| 407 |
object_type |
| 408 |
) VALUES ( |
| 409 |
'".$this->getId()."', |
| 410 |
'%s', |
| 411 |
'".$objectType."' |
| 412 |
)"; |
| 413 |
} |
| 414 |
|
| 415 |
return $sql; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* Adds a object of the given type. |
| 420 |
* |
| 421 |
* @param string $objectType The object type. |
| 422 |
* @param integer $objectId The object id. |
| 423 |
* |
| 424 |
* @return null |
| 425 |
*/ |
| 426 |
public function addObject($objectType, $objectId) |
| 427 |
{ |
| 428 |
if (!in_array($objectType, $this->getAllObjectTypes())) { |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
$this->getAccessHandler()->unsetUserGroupsForObject(); |
| 433 |
$this->getObjectsFromType($objectType); |
| 434 |
|
| 435 |
$object = new stdClass; |
| 436 |
$object->id = $objectId; |
| 437 |
|
| 438 |
$this->objects[$objectType]['real'][$objectId] = $object; |
| 439 |
$this->objects[$objectType]['full'] = -1; |
| 440 |
|
| 441 |
$this->_assignedObjects[$objectType][$objectId] = $objectId; |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Removes a object of the given type. |
| 446 |
* |
| 447 |
* @param string $objectType The object type. |
| 448 |
* @param integer $objectId The object id. |
| 449 |
* |
| 450 |
* @return null |
| 451 |
*/ |
| 452 |
public function removeObject($objectType, $objectId) |
| 453 |
{ |
| 454 |
if (!in_array($objectType, $this->getAllObjectTypes())) { |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
$this->getAccessHandler()->unsetUserGroupsForObject(); |
| 459 |
$this->getObjectsFromType($objectType); |
| 460 |
|
| 461 |
unset($this->objects[$objectType]['real'][$objectId]); |
| 462 |
$this->objects[$objectType]['full'] = -1; |
| 463 |
|
| 464 |
unset($this->singleObjects[$objectType][$objectId]); |
| 465 |
unset($this->_assignedObjects[$objectType][$objectId]); |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* Returns the assigned objects. |
| 470 |
* |
| 471 |
* @param string $objectType The object type. |
| 472 |
* |
| 473 |
* @return array |
| 474 |
*/ |
| 475 |
private function _getAssignedObjects($objectType) |
| 476 |
{ |
| 477 |
if ($this->_assignedObjects[$objectType] !== null) { |
| 478 |
return $this->_assignedObjects[$objectType]; |
| 479 |
} |
| 480 |
|
| 481 |
global $wpdb; |
| 482 |
|
| 483 |
$dbObjects = $wpdb->get_results( |
| 484 |
$this->_getSqlQuery($objectType, 'select') |
| 485 |
); |
| 486 |
|
| 487 |
$this->_assignedObjects[$objectType] = array(); |
| 488 |
|
| 489 |
foreach ($dbObjects as $dbObject) { |
| 490 |
$this->_assignedObjects[$objectType][$dbObject->id] |
| 491 |
= $dbObject->id; |
| 492 |
} |
| 493 |
|
| 494 |
return $this->_assignedObjects[$objectType]; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Checks if the object is assigned to the group. |
| 499 |
* |
| 500 |
* @param string $objectType The object type. |
| 501 |
* @param integer $objectId The object id. |
| 502 |
* |
| 503 |
* @return boolean |
| 504 |
*/ |
| 505 |
private function _isObjectAssignedToGroup($objectType, $objectId) |
| 506 |
{ |
| 507 |
return array_key_exists( |
| 508 |
$objectId, |
| 509 |
$this->_getAssignedObjects($objectType) |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* Unsets the ojects. |
| 515 |
* |
| 516 |
* @param string $objectType The object type. |
| 517 |
* @param boolean $plusRemove If true also database entrys will remove. |
| 518 |
* |
| 519 |
* @return null; |
| 520 |
*/ |
| 521 |
public function unsetObjects($objectType, $plusRemove = false) |
| 522 |
{ |
| 523 |
if (!in_array($objectType, $this->getAllObjectTypes())) { |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
if ($plusRemove) { |
| 528 |
$this->_deleteObjectsFromDb($objectType); |
| 529 |
} |
| 530 |
|
| 531 |
$this->objects[$objectType] = array( |
| 532 |
'real' => array(), |
| 533 |
'full' => array(), |
| 534 |
); |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* Removes all objects from the user group. |
| 539 |
* |
| 540 |
* @param string $objectType The object type. |
| 541 |
* |
| 542 |
* @return null |
| 543 |
*/ |
| 544 |
private function _deleteObjectsFromDb($objectType) |
| 545 |
{ |
| 546 |
if (isset($this->id)) { |
| 547 |
global $wpdb; |
| 548 |
|
| 549 |
$wpdb->query( |
| 550 |
$this->_getSqlQuery($objectType, 'delete') |
| 551 |
); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Checks if the given object is a member of the group. |
| 557 |
* |
| 558 |
* @param string $objectType The object type. |
| 559 |
* @param interger $objectId The id of the object which should be checked. |
| 560 |
* @param boolean $withInfo If true then we return additional infos. |
| 561 |
* |
| 562 |
* @return boolean |
| 563 |
*/ |
| 564 |
public function objectIsMember($objectType, $objectId, $withInfo = false) |
| 565 |
{ |
| 566 |
if (!in_array($objectType, $this->getAllObjectTypes())) { |
| 567 |
return; |
| 568 |
} |
| 569 |
|
| 570 |
$object = $this->_getSingleObject($objectType, $objectId, 'full'); |
| 571 |
|
| 572 |
if ($object !== null) { |
| 573 |
if (isset($object->recursiveMember) |
| 574 |
&& $withInfo |
| 575 |
) { |
| 576 |
return $object->recursiveMember; |
| 577 |
} |
| 578 |
|
| 579 |
return true; |
| 580 |
} |
| 581 |
|
| 582 |
return false; |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Returns all objects of the given type. |
| 587 |
* |
| 588 |
* @param string $objectType The object type. |
| 589 |
* @param string $type The return type, could be real or full. |
| 590 |
* |
| 591 |
* @return array |
| 592 |
*/ |
| 593 |
function getObjectsFromType($objectType, $type = 'real') |
| 594 |
{ |
| 595 |
if (!in_array($objectType, $this->getAllObjectTypes())) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
if ($this->id == null) { |
| 600 |
return array(); |
| 601 |
} |
| 602 |
|
| 603 |
if ($type != 'real' |
| 604 |
&& $type != 'full' |
| 605 |
) { |
| 606 |
return array(); |
| 607 |
} |
| 608 |
|
| 609 |
if ($this->objects[$objectType][$type] != -1) { |
| 610 |
return $this->objects[$objectType][$type]; |
| 611 |
} else { |
| 612 |
$this->objects[$objectType][$type] = array(); |
| 613 |
} |
| 614 |
|
| 615 |
global $wpdb; |
| 616 |
|
| 617 |
$dbObjects = $wpdb->get_results( |
| 618 |
$this->_getSqlQuery($objectType, 'select') |
| 619 |
); |
| 620 |
|
| 621 |
foreach ($dbObjects as $dbObject) { |
| 622 |
$object = $this->_getSingleObject($objectType, $dbObject->id, $type); |
| 623 |
|
| 624 |
if ($object !== null) { |
| 625 |
$this->objects[$objectType][$type][$object->id] = $object; |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
$postableTypes = $this->getAccessHandler()->getPostableTypes(); |
| 630 |
|
| 631 |
if ($type == 'full' |
| 632 |
&& !in_array($objectType, $postableTypes) |
| 633 |
&& $objectType != 'role' |
| 634 |
) { |
| 635 |
if ($objectType == 'category') { |
| 636 |
$this->objects[$objectType][$type] |
| 637 |
= $this->getFullCategories($this->objects[$objectType][$type]); |
| 638 |
} elseif ($objectType == 'user') { |
| 639 |
$this->objects[$objectType][$type] = $this->getFullUsers(); |
| 640 |
} else { |
| 641 |
$plObject = $this->getAccessHandler()->getPlObject($objectType); |
| 642 |
$this->objects[$objectType][$type] |
| 643 |
= $plObject['reference']->{$plObject['getFullObjects']}( |
| 644 |
$this->objects[$objectType][$type], |
| 645 |
&$this |
| 646 |
); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
return $this->objects[$objectType][$type]; |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Returns a single object. |
| 655 |
* |
| 656 |
* @param string $objectType The object type. |
| 657 |
* @param interger $objectId The id of the object which should be checked. |
| 658 |
* @param string $type The return type. Can be real or full. |
| 659 |
* |
| 660 |
* @return object |
| 661 |
*/ |
| 662 |
private function _getSingleObject($objectType, $objectId, $type) |
| 663 |
{ |
| 664 |
if (isset($this->singleObjects[$objectType][$objectId])) { |
| 665 |
return $this->singleObjects[$objectType][$objectId]; |
| 666 |
} |
| 667 |
|
| 668 |
$isRecursiveMember = array(); |
| 669 |
|
| 670 |
if ($type == 'full' |
| 671 |
&& $objectType != 'role' |
| 672 |
) { |
| 673 |
$postableTypes = $this->getAccessHandler()->getPostableTypes(); |
| 674 |
|
| 675 |
if (in_array($objectType, $postableTypes)) { |
| 676 |
$isRecursiveMember = $this->_getFullPost($objectType, $objectId); |
| 677 |
} else if ($objectType == 'category') { |
| 678 |
$isRecursiveMember = $this->_getFullCategory($objectId); |
| 679 |
} else if ($objectType == 'user') { |
| 680 |
$isRecursiveMember = $this->_getFullUser($objectId); |
| 681 |
} else { |
| 682 |
$isRecursiveMember = $this->_getFullPlObject($objectType, $objectId); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
if ($this->_isObjectAssignedToGroup($objectType, $objectId) |
| 687 |
|| $isRecursiveMember != array() |
| 688 |
) { |
| 689 |
$object = new stdClass; |
| 690 |
$object->id = $objectId; |
| 691 |
|
| 692 |
if ($isRecursiveMember != array()) { |
| 693 |
$object->recursiveMember = $isRecursiveMember; |
| 694 |
} |
| 695 |
|
| 696 |
$this->singleObjects[$objectType][$objectId] = $object; |
| 697 |
} else { |
| 698 |
$this->singleObjects[$objectType][$objectId] = null; |
| 699 |
} |
| 700 |
|
| 701 |
return $this->singleObjects[$objectType][$objectId]; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
/* |
| 706 |
* Group users functions. |
| 707 |
*/ |
| 708 |
|
| 709 |
/** |
| 710 |
* Returns a single user. |
| 711 |
* |
| 712 |
* @param integer $objectId The object id. |
| 713 |
* |
| 714 |
* @return object |
| 715 |
*/ |
| 716 |
private function _getFullUser($objectId) |
| 717 |
{ |
| 718 |
$isRecursiveMember = array(); |
| 719 |
|
| 720 |
global $wpdb; |
| 721 |
|
| 722 |
$curUserdata = get_userdata($objectId); |
| 723 |
|
| 724 |
if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) { |
| 725 |
$capabilities = $curUserdata->{$wpdb->prefix . "capabilities"}; |
| 726 |
} else { |
| 727 |
$capabilities = null; |
| 728 |
} |
| 729 |
|
| 730 |
$role = (is_array($capabilities) && count($capabilities) > 0) ? array_keys($capabilities) : array('norole'); |
| 731 |
|
| 732 |
if (array_key_exists($role[0], $this->getObjectsFromType('role'))) { |
| 733 |
$roleObject->name = $role[0]; |
| 734 |
|
| 735 |
$isRecursiveMember = array('role' => array()); |
| 736 |
$isRecursiveMember['role'][] = $roleObject; |
| 737 |
} |
| 738 |
|
| 739 |
return $isRecursiveMember; |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Returns the users in the group |
| 744 |
* |
| 745 |
* @return array |
| 746 |
*/ |
| 747 |
public function getFullUsers() |
| 748 |
{ |
| 749 |
global $wpdb; |
| 750 |
|
| 751 |
$dbUsers = $wpdb->get_results( |
| 752 |
"SELECT ID, user_nicename |
| 753 |
FROM ".$wpdb->users |
| 754 |
); |
| 755 |
|
| 756 |
$fullUsers = array(); |
| 757 |
|
| 758 |
if (isset($dbUsers)) { |
| 759 |
foreach ($dbUsers as $dbUser) { |
| 760 |
$user = $this->_getSingleObject('user', $dbUser->ID, 'full'); |
| 761 |
|
| 762 |
if ($user !== null) { |
| 763 |
$fullUsers[$user->id] = $user; |
| 764 |
} |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
return $fullUsers; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
/* |
| 773 |
* Group categories functions. |
| 774 |
*/ |
| 775 |
|
| 776 |
/** |
| 777 |
* Returns a single category. |
| 778 |
* |
| 779 |
* @param integer $objectId The object id. |
| 780 |
* |
| 781 |
* @return object |
| 782 |
*/ |
| 783 |
private function _getFullCategory($objectId) |
| 784 |
{ |
| 785 |
$category = get_category($objectId); |
| 786 |
|
| 787 |
$isRecursiveMember = array(); |
| 788 |
|
| 789 |
$userAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 790 |
$uamOptions = $userAccessManager->getAdminOptions(); |
| 791 |
|
| 792 |
if ($uamOptions['lock_recursive'] == 'true') { |
| 793 |
if (isset($category->parent) |
| 794 |
&& !is_null($category->parent) |
| 795 |
) { |
| 796 |
$parentCategory = $this->_getSingleObject( |
| 797 |
'category', |
| 798 |
$category->parent, |
| 799 |
'full' |
| 800 |
); |
| 801 |
|
| 802 |
if ($parentCategory !== null) { |
| 803 |
$curCategory = get_category($objectId); |
| 804 |
$parentCategory->name = $curCategory->name; |
| 805 |
|
| 806 |
if (isset($parentCategory->recursiveMember)) { |
| 807 |
$isRecursiveMember['category'][] |
| 808 |
= $parentCategory; |
| 809 |
} else { |
| 810 |
$isRecursiveMember['category'][] |
| 811 |
= $parentCategory; |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
return $isRecursiveMember; |
| 818 |
} |
| 819 |
|
| 820 |
/** |
| 821 |
* Returns the categories in the group |
| 822 |
* |
| 823 |
* @param string $categories The real categories. |
| 824 |
* |
| 825 |
* @return array |
| 826 |
*/ |
| 827 |
public function getFullCategories($categories) |
| 828 |
{ |
| 829 |
$userAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 830 |
$uamOptions = $userAccessManager->getAdminOptions(); |
| 831 |
|
| 832 |
foreach ($categories as $category) { |
| 833 |
if ($category != null) { |
| 834 |
if ($uamOptions['lock_recursive'] == 'true') { |
| 835 |
//We have to remove the filter to get all categories |
| 836 |
$removeSucc = remove_filter( |
| 837 |
'get_terms', |
| 838 |
array( |
| 839 |
$this->getAccessHandler()->getUserAccessManager(), |
| 840 |
'showCategory' |
| 841 |
) |
| 842 |
); |
| 843 |
|
| 844 |
if ($removeSucc) { |
| 845 |
$args = array( |
| 846 |
'child_of' => $category->id, |
| 847 |
'hide_empty' => false |
| 848 |
); |
| 849 |
|
| 850 |
$categoryChilds = get_categories($args); |
| 851 |
|
| 852 |
add_filter( |
| 853 |
'get_terms', |
| 854 |
array(&$userAccessManager, 'showCategory') |
| 855 |
); |
| 856 |
|
| 857 |
|
| 858 |
foreach ($categoryChilds as $categoryChild) { |
| 859 |
$curCategoryChild = new stdClass(); |
| 860 |
$curCategoryChild->id = $categoryChild->term_id; |
| 861 |
$curCategoryChild->name = $categoryChild->name; |
| 862 |
|
| 863 |
$curCategoryChild->recursiveMember |
| 864 |
= array('category' => array()); |
| 865 |
$curCategoryChild->recursiveMember['category'][] |
| 866 |
= $curCategoryChild; |
| 867 |
$categories[$curCategoryChild->id] |
| 868 |
= $curCategoryChild; |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
$categories[$category->id] = $category; |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
return $categories; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
/* |
| 882 |
* Group posts functions. |
| 883 |
*/ |
| 884 |
|
| 885 |
/** |
| 886 |
* Returns the membership of a single post. |
| 887 |
* |
| 888 |
* @param string $postType The post type needed for the intern representation. |
| 889 |
* @param integer $objectId The object id. |
| 890 |
* |
| 891 |
* @return object |
| 892 |
*/ |
| 893 |
private function _getFullPost($postType, $objectId) |
| 894 |
{ |
| 895 |
$post = get_post($objectId); |
| 896 |
|
| 897 |
$isRecursiveMember = array(); |
| 898 |
|
| 899 |
$userAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 900 |
$uamOptions = $userAccessManager->getAdminOptions(); |
| 901 |
|
| 902 |
foreach ($this->getObjectsFromType('category', 'full') as $category) { |
| 903 |
if (in_category($category->id, $post->ID)) { |
| 904 |
$categoryObject = get_category($category->id); |
| 905 |
$category->name = $categoryObject->name; |
| 906 |
|
| 907 |
$isRecursiveMember['category'][] = $category; |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
if (get_option('show_on_front') == 'page' |
| 912 |
&& $post->post_parent == 0 |
| 913 |
&& $post->post_type == 'post' |
| 914 |
&& get_option('page_for_posts') != $objectId |
| 915 |
) { |
| 916 |
$parentId = get_option('page_for_posts'); |
| 917 |
} else { |
| 918 |
$parentId = $post->post_parent; |
| 919 |
} |
| 920 |
|
| 921 |
if ($uamOptions['lock_recursive'] == 'true' |
| 922 |
&& $parentId != 0 |
| 923 |
) { |
| 924 |
$parent = get_post($parentId); |
| 925 |
|
| 926 |
$parentPost = $this->_getSingleObject( |
| 927 |
$parent->post_type, |
| 928 |
$parentId, |
| 929 |
'full' |
| 930 |
); |
| 931 |
|
| 932 |
if ($parentPost !== null) { |
| 933 |
$postObject = get_post($parentPost->id); |
| 934 |
$parentPost->name = $postObject->post_title; |
| 935 |
|
| 936 |
$isRecursiveMember[$parent->post_type][] = $parentPost; |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
return $isRecursiveMember; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
/* |
| 945 |
* Group pluggable objects functions. |
| 946 |
*/ |
| 947 |
|
| 948 |
/** |
| 949 |
* Returns a the recursive membership for a pluggable object. |
| 950 |
* |
| 951 |
* @param string $objectType The pluggable object type. |
| 952 |
* @param integer $objectId The object id. |
| 953 |
* |
| 954 |
* @return array |
| 955 |
*/ |
| 956 |
private function _getFullPlObject($objectType, $objectId) |
| 957 |
{ |
| 958 |
$isRecursiveMember = array(); |
| 959 |
|
| 960 |
$plObject = $this->getAccessHandler()->getPlObject($objectType); |
| 961 |
|
| 962 |
if (isset($plObject['reference']) |
| 963 |
&& isset($plObject['getFull']) |
| 964 |
) { |
| 965 |
$plRecMember = $plObject['reference']->{$plObject['getFull']}( |
| 966 |
$objectId, |
| 967 |
&$this |
| 968 |
); |
| 969 |
|
| 970 |
if (is_array($plRecMember)) { |
| 971 |
$isRecursiveMember = $plRecMember; |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
return $isRecursiveMember; |
| 976 |
} |
| 977 |
} |