| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Upstream_Counter |
| 9 |
*/ |
| 10 |
class Upstream_Counter |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $currentUserData; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
protected $projects = []; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $projectIds = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $items = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* Upstream_Counter constructor. |
| 34 |
* |
| 35 |
* @param int|array $projectIds |
| 36 |
*/ |
| 37 |
public function __construct($projectIds = null) |
| 38 |
{ |
| 39 |
$this->projectIds = $projectIds; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Return the total of items from specific type. |
| 44 |
* |
| 45 |
* @param string $itemType |
| 46 |
* |
| 47 |
* @return int |
| 48 |
*/ |
| 49 |
public function getTotalItemsOfType($itemType) |
| 50 |
{ |
| 51 |
$items = $this->getItemsOfType($itemType); |
| 52 |
|
| 53 |
return count($items); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Retrieve items from the project. |
| 58 |
* |
| 59 |
* @param string $itemType |
| 60 |
* |
| 61 |
* @return array|mixed |
| 62 |
*/ |
| 63 |
public function getItemsOfType($itemType) |
| 64 |
{ |
| 65 |
$projects = $this->getProjects(); |
| 66 |
|
| 67 |
if (empty($projects)) { |
| 68 |
return []; |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! isset($this->items[$itemType])) { |
| 72 |
$items = []; |
| 73 |
|
| 74 |
foreach ($projects as $project) { |
| 75 |
// Check if the item type is disabled for this project. |
| 76 |
$disabled = get_post_meta($project->ID, '_upstream_project_disable_' . $itemType, true) === 'on'; |
| 77 |
if ($disabled) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
|
| 81 |
// If milestones, don't use the metadata, but the milestone classes instead |
| 82 |
if ('milestones' === $itemType) { |
| 83 |
$milestonesUtil = \UpStream\Milestones::getInstance(); |
| 84 |
$dataSet = $milestonesUtil->getMilestonesFromProject($project->ID, true); |
| 85 |
} else { |
| 86 |
$dataSet = get_post_meta($project->ID, '_upstream_project_' . $itemType, true); |
| 87 |
} |
| 88 |
|
| 89 |
// RSD: added if statement to fix count bug 873, which appears due to merge |
| 90 |
if ($dataSet && count($dataSet) > 0) |
| 91 |
$items = array_merge((array)$items, (array)$dataSet); |
| 92 |
} |
| 93 |
|
| 94 |
$this->items[$itemType] = $items; |
| 95 |
} |
| 96 |
|
| 97 |
return $this->items[$itemType]; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Retrieve all tasks from projects. |
| 102 |
* |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
public function getProjects() |
| 106 |
{ |
| 107 |
return $this->getProjectsCached(); |
| 108 |
|
| 109 |
if (empty($this->projects)) { |
| 110 |
$args = [ |
| 111 |
'post_type' => 'project', |
| 112 |
'post_status' => 'any', |
| 113 |
'posts_per_page' => -1, |
| 114 |
]; |
| 115 |
|
| 116 |
if ( ! empty($this->projectIds)) { |
| 117 |
$args['include'] = $this->projectIds; |
| 118 |
} |
| 119 |
|
| 120 |
$this->projects = (array)get_posts($args); |
| 121 |
} |
| 122 |
|
| 123 |
return $this->projects; |
| 124 |
} |
| 125 |
|
| 126 |
public function getProjectsCached() |
| 127 |
{ |
| 128 |
$allprojects = Upstream_Cache::get_instance()->get('allprojectsbyid'); |
| 129 |
if ($allprojects === false) { |
| 130 |
$args = [ |
| 131 |
'post_type' => 'project', |
| 132 |
'post_status' => 'any', |
| 133 |
'posts_per_page' => -1, |
| 134 |
]; |
| 135 |
$projects = (array)get_posts($args); |
| 136 |
$allprojects = []; |
| 137 |
|
| 138 |
foreach ($projects as $p) { |
| 139 |
$allprojects[$p->ID] = $p; |
| 140 |
} |
| 141 |
|
| 142 |
Upstream_Cache::get_instance()->set('allprojectsbyid', $allprojects); |
| 143 |
} |
| 144 |
|
| 145 |
$this->projects = []; |
| 146 |
if ( ! empty($this->projectIds)) { |
| 147 |
if (is_array($this->projectIds)) { |
| 148 |
foreach ($this->projectIds as $id) { |
| 149 |
$this->projects[] = $allprojects[$id]; |
| 150 |
} |
| 151 |
} |
| 152 |
else { |
| 153 |
$this->projects[] = $allprojects[$this->projectIds]; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $this->projects; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Returns the total count of open items |
| 164 |
* |
| 165 |
* @param string $itemType |
| 166 |
* |
| 167 |
* @return null|int |
| 168 |
*/ |
| 169 |
public function getTotalOpenItemsOfType($itemType) |
| 170 |
{ |
| 171 |
$total = 0; |
| 172 |
$items = $this->getItemsOfType($itemType); |
| 173 |
|
| 174 |
if (count($items) > 0) { |
| 175 |
// Milestones doesn't have state so they are always open. |
| 176 |
if ($itemType === 'milestones') { |
| 177 |
return count($items); |
| 178 |
} |
| 179 |
|
| 180 |
$options = (array)get_option("upstream_{$itemType}"); |
| 181 |
$statuses = isset($options['statuses']) ? $options['statuses'] : ''; |
| 182 |
|
| 183 |
if (empty($statuses)) { |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
// TODOPERM: is this right |
| 188 |
$statuses = wp_list_pluck($statuses, 'type', 'id'); |
| 189 |
|
| 190 |
foreach ($items as $item) { |
| 191 |
if ( ! isset($item['status'])) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
$itemStatus = $item['status']; |
| 196 |
if (isset($statuses[$itemStatus]) && $statuses[$itemStatus] === 'open') { |
| 197 |
$total++; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return $total; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get the count of items assigned to the current user. |
| 207 |
* |
| 208 |
* @param string $itemType The item type to be searched. I.e.: tasks, bugs, etc. |
| 209 |
* |
| 210 |
* @return integer |
| 211 |
* |
| 212 |
*/ |
| 213 |
public function getTotalAssignedToCurrentUserOfType($itemType) |
| 214 |
{ |
| 215 |
$rowset = $this->getItemsOfType($itemType); |
| 216 |
if (count($rowset) === 0) { |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
$userData = $this->getCurrentUserData(); |
| 221 |
$currentUserId = (int)$userData['id']; |
| 222 |
|
| 223 |
$assignedItemsCount = 0; |
| 224 |
|
| 225 |
foreach ($rowset as $row) { |
| 226 |
if ( ! isset($row['assigned_to'])) { |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
$assignees = array_unique(array_filter(array_map('intval', (array)$row['assigned_to']))); |
| 231 |
|
| 232 |
if (in_array($currentUserId, $assignees)) { |
| 233 |
$assignedItemsCount++; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
return $assignedItemsCount; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* @return array|void|null |
| 242 |
*/ |
| 243 |
protected function getCurrentUserData() |
| 244 |
{ |
| 245 |
if (empty($this->currentUserData)) { |
| 246 |
$this->currentUserData = upstream_user_data(); |
| 247 |
} |
| 248 |
|
| 249 |
return $this->currentUserData; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Returns the count of OPEN tasks for the current user |
| 254 |
* |
| 255 |
* @return int |
| 256 |
*/ |
| 257 |
public function getTotalOpenItemsOfTypeForCurrentUser($itemType) |
| 258 |
{ |
| 259 |
$items = $this->getItemsOfType($itemType); |
| 260 |
|
| 261 |
if (empty($items)) { |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
// Milestones doesn't have state so they are always opened. |
| 266 |
if ($itemType == 'milestones') { |
| 267 |
return count($items); |
| 268 |
} |
| 269 |
|
| 270 |
$option = get_option('upstream_' . $itemType); |
| 271 |
$statuses = isset($option['statuses']) ? $option['statuses'] : ''; |
| 272 |
|
| 273 |
if (empty($statuses)) { |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
$itemTypes = wp_list_pluck($statuses, 'type', 'id'); |
| 278 |
$userData = $this->getCurrentUserData(); |
| 279 |
|
| 280 |
$count = 0; |
| 281 |
foreach ($items as $key => $item) { |
| 282 |
$item = (array)$item; |
| 283 |
|
| 284 |
if ( ! isset($item['assigned_to'])) { |
| 285 |
continue; |
| 286 |
} |
| 287 |
|
| 288 |
if ( |
| 289 |
(is_array($item['assigned_to']) && ! in_array($userData['id'], $item['assigned_to'])) |
| 290 |
|| (is_numeric($item['assigned_to'] && $item['assigned_to'] != $userData['id'])) |
| 291 |
) { |
| 292 |
continue; |
| 293 |
} |
| 294 |
|
| 295 |
$item_status = isset($item['status']) ? $item['status'] : ''; |
| 296 |
|
| 297 |
if ((isset($itemTypes[$item_status]) && $itemTypes[$item_status] == 'open') || $item_status === "") { |
| 298 |
$count++; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return $count; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|