| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Model_Manager |
| 4 |
* |
| 5 |
* WordPress Coding Standart (WCS) note: |
| 6 |
* All camelCase methods and object properties on this file are not converted to snake_case, |
| 7 |
* because it being used (heavily) on another add-on plugins. |
| 8 |
* |
| 9 |
* @package UpStream |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class UpStream_Model_Manager |
| 19 |
*/ |
| 20 |
class UpStream_Model_Manager { |
| 21 |
|
| 22 |
/** |
| 23 |
* Instance |
| 24 |
* |
| 25 |
* @var undefined |
| 26 |
*/ |
| 27 |
protected static $instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Objects |
| 31 |
* |
| 32 |
* @var array |
| 33 |
*/ |
| 34 |
protected $objects = array(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Reset |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function reset() { |
| 42 |
$this->objects = array(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* GetByID |
| 47 |
* |
| 48 |
* @param mixed $object_type object_type. |
| 49 |
* @param mixed $object_id object_id. |
| 50 |
* @param mixed $parent_type parent_type. |
| 51 |
* @param mixed $parent_id parent_id. |
| 52 |
* @throws \UpStream_Model_ArgumentException Exception. |
| 53 |
* @throws UpStream_Model_ArgumentException Exception. |
| 54 |
*/ |
| 55 |
public function getByID( $object_type, $object_id, $parent_type = null, $parent_id = 0 ) { // phpcs:ignore |
| 56 |
if ( ! in_array( $object_type, array( UPSTREAM_ITEM_TYPE_CLIENT, UPSTREAM_ITEM_TYPE_PROJECT, UPSTREAM_ITEM_TYPE_MILESTONE, UPSTREAM_ITEM_TYPE_TASK, UPSTREAM_ITEM_TYPE_BUG, UPSTREAM_ITEM_TYPE_FILE ), true ) ) { |
| 57 |
throw new \UpStream_Model_ArgumentException( |
| 58 |
sprintf( |
| 59 |
// translators: %s: type name. |
| 60 |
__( 'Item type %s is not valid.', 'upstream' ), |
| 61 |
$object_type |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
if ( empty( $this->objects[ $object_type ] ) || empty( $this->objects[ $object_type ][ $object_id ] ) ) { |
| 67 |
$this->loadObject( $object_type, $object_id, $parent_type, $parent_id ); |
| 68 |
} |
| 69 |
|
| 70 |
if ( empty( $this->objects[ $object_type ][ $object_id ] ) ) { |
| 71 |
throw new UpStream_Model_ArgumentException( |
| 72 |
sprintf( |
| 73 |
// translators: %1$s: ID. |
| 74 |
// translators: %2$s: TYPE. |
| 75 |
// translators: %3$s: PARENT. |
| 76 |
// translators: %4$s: PARENT TYPE. |
| 77 |
__( 'This (ID = %1$s, TYPE = %2$s, PARENT ID = %3$s, PARENT TYPE = %4$s) is not a valid object', 'upstream' ), |
| 78 |
$object_id, |
| 79 |
$object_type, |
| 80 |
$parent_id, |
| 81 |
$parent_type |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
return $this->objects[ $object_type ][ $object_id ]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* LoadObject |
| 91 |
* |
| 92 |
* @param mixed $object_type object_type. |
| 93 |
* @param mixed $object_id object_id. |
| 94 |
* @param mixed $parent_type parent_type. |
| 95 |
* @param mixed $parent_id parent_id. |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
protected function loadObject( $object_type, $object_id, $parent_type, $parent_id ) { // phpcs:ignore |
| 99 |
// TODO: add exceptions. |
| 100 |
if ( UPSTREAM_ITEM_TYPE_PROJECT === $object_type ) { |
| 101 |
|
| 102 |
$project = new UpStream_Model_Project( $object_id ); |
| 103 |
$this->objects[ $object_type ][ $object_id ] = $project; |
| 104 |
|
| 105 |
foreach ( $project->tasks() as $item ) { |
| 106 |
$this->objects[ UPSTREAM_ITEM_TYPE_TASK ][ $item->id ] = $item; |
| 107 |
} |
| 108 |
|
| 109 |
foreach ( $project->bugs() as $item ) { |
| 110 |
$this->objects[ UPSTREAM_ITEM_TYPE_BUG ][ $item->id ] = $item; |
| 111 |
} |
| 112 |
|
| 113 |
foreach ( $project->files() as $item ) { |
| 114 |
$this->objects[ UPSTREAM_ITEM_TYPE_FILE ][ $item->id ] = $item; |
| 115 |
} |
| 116 |
} elseif ( UPSTREAM_ITEM_TYPE_MILESTONE === $object_type ) { |
| 117 |
$this->objects[ $object_type ][ $object_id ] = new UpStream_Model_Milestone( $object_id ); |
| 118 |
} elseif ( UPSTREAM_ITEM_TYPE_CLIENT === $object_type ) { |
| 119 |
$this->objects[ $object_type ][ $object_id ] = new UpStream_Model_Client( $object_id ); |
| 120 |
} elseif ( UPSTREAM_ITEM_TYPE_TASK === $object_type ) { |
| 121 |
$this->loadObject( $parent_type, $parent_id, null, null ); |
| 122 |
} elseif ( UPSTREAM_ITEM_TYPE_BUG === $object_type ) { |
| 123 |
$this->loadObject( $parent_type, $parent_id, null, null ); |
| 124 |
} elseif ( UPSTREAM_ITEM_TYPE_FILE === $object_type ) { |
| 125 |
$this->loadObject( $parent_type, $parent_id, null, null ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* LoadAll |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function loadAll() { // phpcs:ignore |
| 135 |
$posts = get_posts( |
| 136 |
array( |
| 137 |
'post_type' => 'project', |
| 138 |
'post_status' => 'publish', |
| 139 |
'numberposts' => -1, |
| 140 |
) |
| 141 |
); |
| 142 |
|
| 143 |
foreach ( $posts as $post ) { |
| 144 |
$this->getByID( UPSTREAM_ITEM_TYPE_PROJECT, $post->ID ); |
| 145 |
} |
| 146 |
|
| 147 |
$posts = get_posts( |
| 148 |
array( |
| 149 |
'post_type' => 'upst_milestone', |
| 150 |
'post_status' => 'publish', |
| 151 |
'numberposts' => -1, |
| 152 |
) |
| 153 |
); |
| 154 |
|
| 155 |
foreach ( $posts as $post ) { |
| 156 |
$this->getByID( UPSTREAM_ITEM_TYPE_MILESTONE, $post->ID ); |
| 157 |
} |
| 158 |
|
| 159 |
$posts = get_posts( |
| 160 |
array( |
| 161 |
'post_type' => 'client', |
| 162 |
'post_status' => 'publish', |
| 163 |
'numberposts' => -1, |
| 164 |
) |
| 165 |
); |
| 166 |
|
| 167 |
foreach ( $posts as $post ) { |
| 168 |
$this->getByID( UPSTREAM_ITEM_TYPE_CLIENT, $post->ID ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* FindAllByCallback |
| 174 |
* |
| 175 |
* @param mixed $callback callback. |
| 176 |
*/ |
| 177 |
public function findAllByCallback( $callback ) { // phpcs:ignore |
| 178 |
$return_items = array(); |
| 179 |
|
| 180 |
if ( count( $this->objects ) === 0 ) { |
| 181 |
$this->loadAll(); |
| 182 |
} |
| 183 |
|
| 184 |
foreach ( $this->objects as $type => $items ) { |
| 185 |
foreach ( $items as $id => $item ) { |
| 186 |
$r = $callback( $item ); |
| 187 |
if ( $r ) { |
| 188 |
$return_items[] = $item; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return $return_items; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* FindAccessibleProjects |
| 198 |
*/ |
| 199 |
public function findAccessibleProjects() { // phpcs:ignore |
| 200 |
$return_items = array(); |
| 201 |
|
| 202 |
if ( count( $this->objects ) === 0 ) { |
| 203 |
$this->loadAll(); |
| 204 |
} |
| 205 |
|
| 206 |
foreach ( $this->objects[ UPSTREAM_ITEM_TYPE_PROJECT ] as $id => $item ) { |
| 207 |
$access = upstream_can_access_object( |
| 208 |
'view_project', |
| 209 |
UPSTREAM_ITEM_TYPE_PROJECT, |
| 210 |
$id, |
| 211 |
null, |
| 212 |
0, |
| 213 |
UPSTREAM_PERMISSIONS_ACTION_VIEW |
| 214 |
); |
| 215 |
if ( $access ) { |
| 216 |
$return_items[] = $item; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
usort( |
| 221 |
$return_items, |
| 222 |
function( $item1, $item2 ) { |
| 223 |
return strcasecmp( $item1->title, $item2->title ); |
| 224 |
} |
| 225 |
); |
| 226 |
|
| 227 |
return $return_items; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* CreateObject |
| 233 |
* |
| 234 |
* @param mixed $object_type object_type. |
| 235 |
* @param mixed $title title. |
| 236 |
* @param mixed $created_by created_by. |
| 237 |
* @param mixed $parent_id parent_id. |
| 238 |
* @throws \UpStream_Model_ArgumentException Exception. |
| 239 |
*/ |
| 240 |
public function createObject( $object_type, $title, $created_by, $parent_id = 0 ) { // phpcs:ignore |
| 241 |
switch ( $object_type ) { |
| 242 |
|
| 243 |
case UPSTREAM_ITEM_TYPE_PROJECT: |
| 244 |
return \UpStream_Model_Project::create( $title, $created_by ); |
| 245 |
|
| 246 |
case UPSTREAM_ITEM_TYPE_MILESTONE: |
| 247 |
return \UpStream_Model_Milestone::create( $title, $created_by, $parent_id ); |
| 248 |
|
| 249 |
case UPSTREAM_ITEM_TYPE_CLIENT: |
| 250 |
return \UpStream_Model_Client::create( $title, $created_by ); |
| 251 |
|
| 252 |
case UPSTREAM_ITEM_TYPE_TASK: |
| 253 |
$parent = $this->getByID( UPSTREAM_ITEM_TYPE_PROJECT, $parent_id ); |
| 254 |
return \UpStream_Model_Task::create( $parent, $title, $created_by ); |
| 255 |
|
| 256 |
case UPSTREAM_ITEM_TYPE_FILE: |
| 257 |
$parent = $this->getByID( UPSTREAM_ITEM_TYPE_PROJECT, $parent_id ); |
| 258 |
return \UpStream_Model_File::create( $parent, $title, $created_by ); |
| 259 |
|
| 260 |
case UPSTREAM_ITEM_TYPE_BUG: |
| 261 |
$parent = $this->getByID( UPSTREAM_ITEM_TYPE_PROJECT, $parent_id ); |
| 262 |
return \UpStream_Model_Bug::create( $parent, $title, $created_by ); |
| 263 |
|
| 264 |
default: |
| 265 |
throw new \UpStream_Model_ArgumentException( |
| 266 |
sprintf( |
| 267 |
// translators: %s: type name. |
| 268 |
__( 'Item type %s is not valid.', 'upstream' ), |
| 269 |
$object_type |
| 270 |
) |
| 271 |
); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* DeleteObject |
| 277 |
* |
| 278 |
* @param mixed $object_type object_type. |
| 279 |
* @param mixed $object_id object_id. |
| 280 |
* @param mixed $parent_type parent_type. |
| 281 |
* @param mixed $parent_id parent_id. |
| 282 |
* @throws \UpStream_Model_ArgumentException Exception. |
| 283 |
* @return void |
| 284 |
*/ |
| 285 |
public function deleteObject( $object_type, $object_id, $parent_type, $parent_id ) { // phpcs:ignore |
| 286 |
// throws exception if the object doesn't exist... |
| 287 |
$obj = $this->getByID( $object_type, $object_id, $parent_type, $parent_id ); |
| 288 |
|
| 289 |
switch ( $object_type ) { |
| 290 |
|
| 291 |
case UPSTREAM_ITEM_TYPE_PROJECT: |
| 292 |
wp_delete_post( $object_id ); |
| 293 |
break; |
| 294 |
|
| 295 |
case UPSTREAM_ITEM_TYPE_MILESTONE: |
| 296 |
if ( class_exists( '\UpStream\Factory' ) ) { |
| 297 |
$milestone = \UpStream\Factory::get_milestone( $object_id ); |
| 298 |
|
| 299 |
if ( ! empty( $milestone ) ) { |
| 300 |
$milestone->delete(); |
| 301 |
} |
| 302 |
} |
| 303 |
break; |
| 304 |
|
| 305 |
case UPSTREAM_ITEM_TYPE_CLIENT: |
| 306 |
case UPSTREAM_ITEM_TYPE_TASK: |
| 307 |
case UPSTREAM_ITEM_TYPE_FILE: |
| 308 |
case UPSTREAM_ITEM_TYPE_BUG: |
| 309 |
default: |
| 310 |
throw new \UpStream_Model_ArgumentException( |
| 311 |
sprintf( |
| 312 |
// translators: %s: type name. |
| 313 |
__( 'Item type %s is not valid.', 'upstream' ), |
| 314 |
$object_type |
| 315 |
) |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
unset( $this->objects[ $object_type ][ $object_id ] ); |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Get_instance |
| 325 |
*/ |
| 326 |
public static function get_instance() { |
| 327 |
if ( empty( static::$instance ) ) { |
| 328 |
$instance = new self(); |
| 329 |
static::$instance = $instance; |
| 330 |
} |
| 331 |
|
| 332 |
return static::$instance; |
| 333 |
} |
| 334 |
|
| 335 |
} |
| 336 |
|