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