PluginProbe
UpStream: a Project Management Plugin for WordPress / 2.1.0
UpStream: a Project Management Plugin for WordPress v2.1.0
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / class-upstream-counts.php

class-upstream-counts.php in UpStream: a Project Management Plugin for WordPress 2.1.0, at includes/class-upstream-counts.php

258 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Upstream_Counts.
4 *
5 * @package UpStream
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Class Upstream_Counts
15 *
16 * @deprecated User Upstream_Counter instead
17 */
18 class Upstream_Counts {
19
20 /**
21 * Project data
22 *
23 * @var array
24 */
25 public $projects = null;
26
27 /**
28 * User data
29 *
30 * @var array
31 */
32 public $user = null;
33
34
35 /**
36 * Class constructor
37 *
38 * @param int $id Projects id.
39 */
40 public function __construct( $id ) {
41 $this->projects = (array) $this->get_projects( $id );
42 $this->user = upstream_user_data();
43 }
44
45 /**
46 * Retrieve all tasks from projects.
47 *
48 * @param int $id Projects id.
49 */
50 public function get_projects( $id ) {
51 return $this->get_projects_cached( $id );
52 }
53
54 /**
55 * Retrieve all tasks from cached projects.
56 *
57 * @param int $id Projects id.
58 */
59 public function get_projects_cached( $id ) {
60 $allprojects = Upstream_Cache::get_instance()->get( 'allprojectsbyid' );
61 if ( false === $allprojects ) {
62 $args = array(
63 'post_type' => 'project',
64 'post_status' => 'any',
65 'posts_per_page' => -1,
66 );
67 $projects = (array) get_posts( $args );
68 $allprojects = array();
69
70 foreach ( $projects as $p ) {
71 $allprojects[ $p->ID ] = $p;
72 }
73
74 Upstream_Cache::get_instance()->set( 'allprojectsbyid', $allprojects );
75 }
76
77 $rv = array();
78
79 if ( 0 === $id ) {
80 return $allprojects;
81 } elseif ( isset( $allprojects[ $id ] ) ) {
82 $rv[] = $allprojects[ $id ];
83 }
84
85 return $rv;
86 }
87
88 /**
89 * Returns the total count of open items
90 *
91 * @param string $type The item type to be searched. I.e.: tasks, bugs, etc.
92 */
93 public function total_open( $type ) {
94 $items_open_count = 0;
95
96 $items = $this->get_items( $type );
97 if ( count( $items ) > 0 ) {
98 $option = (array) get_option( "upstream_{$type}" );
99 $statuses = isset( $option['statuses'] ) ? $option['statuses'] : '';
100
101 if ( ! empty( $statuses ) ) {
102 if ( 'milestones' === $type ) {
103 return $this->total( $type );
104 }
105
106 return null;
107 }
108
109 $types = wp_list_pluck( $statuses, 'type', 'name' );
110
111 foreach ( $items as $item ) {
112 if ( ! isset( $item['status'] ) ) {
113 continue;
114 }
115 }
116 }
117
118 return $items_open_count;
119 }
120
121 /**
122 * Retrieve all items from projects.
123 *
124 * @param string $type The item type to be searched. I.e.: tasks, bugs, etc.
125 */
126 public function get_items( $type ) {
127 $items = array();
128
129 if ( count( $this->projects ) > 0 ) {
130 foreach ( $this->projects as $i => $project ) {
131 // Check if the items are disabled.
132 $meta = get_post_meta( $project->ID, '_upstream_project_disable_' . $type, true );
133 if ( 'on' === $meta ) {
134 continue;
135 }
136
137 // If milestones, don't use the metadata, but the milestone classes instead.
138 if ( 'milestones' === $type ) {
139 $milestones_util = \UpStream\Milestones::getInstance();
140 $data_set = $milestones_util->get_milestones_from_project( $project->ID, true );
141 } else {
142 $data_set = get_post_meta( $project->ID, '_upstream_project_' . $type, true );
143 }
144
145 if ( ! empty( $data_set ) && is_array( $data_set ) ) {
146 foreach ( $data_set as $value ) {
147 $items[] = $value;
148 }
149 }
150 };
151 }
152
153 return $items;
154 }
155
156 /**
157 * Get the count of items.
158 *
159 * @param string $type The item type to be searched. I.e.: tasks, bugs, etc.
160 */
161 public function total( $type ) {
162 $items = (array) $this->get_items( $type );
163 $items_count = count( $items );
164
165 return $items_count;
166 }
167
168 /**
169 * Get the count of items assigned to the current user.
170 *
171 * @param string $item_type The item type to be searched. I.e.: tasks, bugs, etc.
172 *
173 * @return integer
174 * @since 1.0.0
175 */
176 public function assigned_to( $item_type ) {
177 $rowset = $this->get_items( $item_type );
178 if ( count( $rowset ) === 0 ) {
179 return 0;
180 }
181
182 $current_user_id = (int) $this->user['id'];
183 $assigned_items_count = 0;
184
185 foreach ( $rowset as $row ) {
186 $assignees = isset( $row['assigned_to'] ) ? array_unique(
187 array_filter(
188 array_map(
189 'intval',
190 (array) $row['assigned_to']
191 )
192 )
193 ) : array();
194 if ( in_array( $current_user_id, $assignees ) ) {
195 $assigned_items_count++;
196 }
197 }
198
199 return $assigned_items_count;
200 }
201
202 /**
203 * Returns the count of OPEN tasks for the current user
204 *
205 * @param string $type The item type to be searched. I.e.: tasks, bugs, etc.
206 */
207 public function assigned_to_open( $type ) {
208 $items = $this->get_items( $type );
209 if ( ! $items ) {
210 return '0';
211 }
212
213 $option = get_option( 'upstream_' . $type );
214 $statuses = isset( $option['statuses'] ) ? $option['statuses'] : '';
215
216 if ( ! $statuses ) {
217 if ( 'milestones' === $type ) {
218 return $this->total( $type );
219 } else {
220 return null;
221 }
222 }
223
224 // TODOPERM: should this be ID or name.
225 $types = array();
226 foreach ( $statuses as $s ) {
227 if ( isset( $s['id'] ) && isset( $s['type'] ) ) {
228 $types[ $s['id'] ] = $s['type'];
229 }
230 }
231
232 $count = 0;
233 foreach ( $items as $key => $item ) {
234 $item = (array) $item;
235 if ( ! isset( $item['assigned_to'] ) ) {
236 continue;
237 }
238
239 if ( ! is_array( $item['assigned_to'] ) ) {
240 $item['assigned_to'] = array( (int) $item['assigned_to'] );
241 }
242
243 if ( ! in_array( $this->user['id'], $item['assigned_to'] ) ) {
244 continue;
245 }
246
247 $item_status = isset( $item['status'] ) ? $item['status'] : '';
248
249 if ( ( isset( $types[ $item_status ] ) && 'open' === $types[ $item_status ] ) || '' === $item_status ) {
250 $count++;
251 }
252 }
253
254 return $count;
255 }
256 }
257
258