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 / model / class-upstream-model-object.php

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

376 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UpStream_Model_Object
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 if ( ! defined( 'UPSTREAM_ITEM_TYPE_PROJECT' ) ) {
18 define( 'UPSTREAM_ITEM_TYPE_PROJECT', 'project' );
19 define( 'UPSTREAM_ITEM_TYPE_MILESTONE', 'milestone' );
20 define( 'UPSTREAM_ITEM_TYPE_CLIENT', 'client' );
21 define( 'UPSTREAM_ITEM_TYPE_TASK', 'task' );
22 define( 'UPSTREAM_ITEM_TYPE_BUG', 'bug' );
23 define( 'UPSTREAM_ITEM_TYPE_FILE', 'file' );
24 define( 'UPSTREAM_ITEM_TYPE_DISCUSSION', 'discussion' );
25 }
26
27 /**
28 * Class UpStream_Model_Object
29 */
30 class UpStream_Model_Object {
31
32 /**
33 * Id
34 *
35 * @var int
36 */
37 protected $id = 0;
38
39 /**
40 * Type
41 *
42 * @var undefined
43 */
44 protected $type = null;
45
46 /**
47 * Title
48 *
49 * @var undefined
50 */
51 protected $title = null;
52
53 /**
54 * AssignedTo
55 *
56 * @var array
57 */
58 protected $assignedTo = array(); // phpcs:ignore
59
60 /**
61 * Created_by
62 *
63 * @var int
64 */
65 protected $createdBy = 0; // phpcs:ignore
66
67 /**
68 * Description
69 *
70 * @var undefined
71 */
72 protected $description = null;
73
74 /**
75 * AdditionalFields
76 *
77 * @var array
78 */
79 protected $additionalFields = array(); // phpcs:ignore
80
81 /**
82 * UpStream_Model_Object constructor.
83 *
84 * @param int $id Id.
85 * @throws UpStream_Model_ArgumentException Exception.
86 */
87 public function __construct( $id = 0 ) {
88 if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $id ) ) {
89 throw new UpStream_Model_ArgumentException(
90 sprintf(
91 // translators: %s: ID.
92 __( 'ID %s must be a valid alphanumeric.', 'upstream' ),
93 $id
94 )
95 );
96 }
97
98 $this->id = $id;
99 }
100
101 /**
102 * IsAssignedTo
103 *
104 * @param int $user_id The user_id of the user to check.
105 * @return bool True if this object is assigned to user_id, or false otherwise.
106 */
107 public function isAssignedTo( $user_id ) { // phpcs:ignore
108 foreach ( $this->assignedTo as $a ) { // phpcs:ignore
109 if ( $a === $user_id ) {
110 return true;
111 }
112 }
113 return false;
114 }
115
116 /**
117 * Fields
118 */
119 public static function fields() {
120 return array(
121 'id' => array(
122 'type' => 'id',
123 'title' => __( 'ID' ),
124 'search' => false,
125 'display' => true,
126 ),
127 'title' => array(
128 'type' => 'string',
129 'title' => __( 'Title' ),
130 'search' => true,
131 'display' => true,
132 ),
133 'description' => array(
134 'type' => 'text',
135 'title' => __( 'Description' ),
136 'search' => true,
137 'display' => true,
138 ),
139 'createdBy' => array(
140 'type' => 'user_id',
141 'title' => __( 'Created By' ),
142 'search' => true,
143 'display' => true,
144 ),
145 'assignedTo' => array(
146 'type' => 'user_id',
147 'is_array' => true,
148 'title' => __( 'Assigned To' ),
149 'search' => true,
150 'display' => true,
151 ),
152 );
153 }
154
155 /**
156 * CustomFields
157 *
158 * @param mixed $fields fields.
159 * @param mixed $type type.
160 * @param mixed $id id.
161 */
162 public static function customFields( $fields, $type, $id = 0 ) { // phpcs:ignore
163 $list = apply_filters( 'upstream_model_list_properties', array(), $type, $id );
164 return array_merge( $fields, $list );
165 }
166
167 /**
168 * Get
169 *
170 * @param mixed $property property.
171 * @throws UpStream_Model_ArgumentException Exception.
172 */
173 public function __get( $property ) {
174 $property = apply_filters( 'upstream_wcs_model_variable', $property );
175
176 switch ( $property ) {
177
178 case 'id':
179 case 'title':
180 case 'assignedTo':
181 case 'createdBy':
182 case 'type':
183 case 'description':
184 return $this->{$property};
185
186 default:
187 if ( array_key_exists( $property, $this->additionalFields ) ) { // phpcs:ignore
188
189 $value = apply_filters( 'upstream_model_get_property_value', $this->additionalFields[ $property ], $this->type, $this->id, $property ); // phpcs:ignore
190
191 } else {
192
193 $property_exists = apply_filters( 'upstream_model_property_exists', false, $this->type, $this->id, $property );
194 if ( ! $property_exists ) {
195 throw new UpStream_Model_ArgumentException(
196 sprintf(
197 // translators: %s: property.
198 __( 'This (%s) is not a valid property.', 'upstream' ),
199 $property
200 )
201 );
202 }
203
204 $this->additionalFields[ $property ] = null; // phpcs:ignore
205
206 return null;
207 }
208
209 return $value;
210 }
211 }
212
213 /**
214 * Set
215 *
216 * @param mixed $property property.
217 * @param mixed $value value.
218 * @throws UpStream_Model_ArgumentException Exception.
219 * @return void
220 */
221 public function __set( $property, $value ) {
222 $property = apply_filters( 'upstream_wcs_model_variable', $property );
223
224 switch ( $property ) {
225
226 case 'id':
227 if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $value ) ) {
228 throw new UpStream_Model_ArgumentException(
229 sprintf(
230 // translators: %s: ID.
231 __( 'ID %s must be a valid alphanumeric.', 'upstream' ),
232 $value
233 )
234 );
235 }
236 $this->{$property} = $value;
237 break;
238
239 case 'title':
240 if ( trim( sanitize_text_field( $value ) ) === '' ) {
241 throw new UpStream_Model_ArgumentException( __( 'You must enter a title.', 'upstream' ) );
242 }
243
244 $this->{$property} = trim( sanitize_text_field( $value ) );
245 break;
246
247 case 'description':
248 $this->{$property} = wp_kses_post( $value );
249 break;
250
251 case 'assignedTo':
252 case 'assignedTo:byUsername':
253 case 'assignedTo:byEmail':
254 if ( ! is_array( $value ) ) {
255 $value = explode( ',', $value );
256 }
257
258 $new_value = array();
259
260 foreach ( $value as $uid ) {
261 $user = false;
262 if ( 'assignedTo' === $property ) {
263 $user = get_user_by( 'id', $uid );
264 }
265 if ( 'assignedTo:byUsername' === $property ) {
266 $user = get_user_by( 'login', trim( $uid ) );
267 }
268 if ( 'assignedTo:byEmail' === $property ) {
269 $user = get_user_by( 'email', trim( $uid ) );
270 }
271
272 if ( false === $user ) {
273 throw new UpStream_Model_ArgumentException(
274 sprintf(
275 // translators: %1$s: user name.
276 // translators: %2$s: user field.
277 __( 'User "%1$s" (for field %2$s) does not exist.', 'upstream' ),
278 $uid,
279 $property
280 )
281 );
282 }
283
284 $new_value[] = $user->ID;
285 }
286
287 $this->assignedTo = $new_value; // phpcs:ignore
288 break;
289
290 case 'createdBy':
291 if ( get_userdata( $value ) === false ) {
292 throw new UpStream_Model_ArgumentException(
293 sprintf(
294 // translators: %s: ID.
295 __( 'User ID %s does not exist.', 'upstream' ),
296 $value
297 )
298 );
299 }
300
301 $this->{$property} = $value;
302 break;
303
304 default:
305 $orig_value = ( array_key_exists( $property, $this->additionalFields ) ) ? $this->additionalFields[ $property ] : null; // phpcs:ignore
306
307 $property_exists = apply_filters( 'upstream_model_property_exists', false, $this->type, $this->id, $property );
308 if ( ! $property_exists ) {
309 throw new UpStream_Model_ArgumentException(
310 sprintf(
311 // translators: %s: property.
312 __( 'This (%s) is not a valid property.', 'upstream' ),
313 $property
314 )
315 );
316 }
317
318 $new_value = apply_filters( 'upstream_model_set_property_value', $orig_value, $this->type, $this->id, $property, $value );
319 $this->additionalFields[ $property ] = $new_value; // phpcs:ignore
320 }
321 }
322
323 /**
324 * LoadDate
325 *
326 * @param mixed $data data.
327 * @param mixed $field field.
328 */
329 public static function loadDate( $data, $field ) { // phpcs:ignore
330 if ( ! empty( $data[ $field . '.YMD' ] ) && self::isValidDate( $data[ $field . '.YMD' ] ) ) {
331 return $data[ $field . '.YMD' ];
332 } elseif ( ! empty( $data[ $field ] ) ) {
333 return self::timestampToYMD( $data[ $field ] );
334 }
335 return null;
336 }
337
338 /**
339 * TimestampToYMD
340 *
341 * @param mixed $timestamp timestamp.
342 */
343 public static function timestampToYMD( $timestamp ) { // phpcs:ignore
344 $offset = get_option( 'gmt_offset' );
345 $sign = $offset < 0 ? '-' : '+';
346 $hours = (int) $offset;
347 $minutes = abs( ( $offset - (int) $offset ) * 60 );
348 $offset = (int) sprintf( '%s%d%02d', $sign, abs( $hours ), $minutes );
349 $calc_offset_seconds = $offset < 0 ? $offset * -1 * 60 : $offset * 60;
350
351 $date = date_i18n( 'Y-m-d', $timestamp + $calc_offset_seconds );
352 return $date;
353 }
354
355 /**
356 * YmdToTimestamp
357 *
358 * @param mixed $ymd ymd.
359 */
360 public static function ymdToTimestamp( $ymd ) { // phpcs:ignore
361 // TODO: check timezones with this.
362 return date_create_from_format( 'Y-m-d', $ymd )->getTimestamp();
363 }
364
365 /**
366 * IsValidDate
367 *
368 * @param mixed $ymd ymd.
369 */
370 public static function isValidDate( $ymd ) { // phpcs:ignore
371 $d = DateTime::createFromFormat( 'Y-m-d', $ymd );
372 return $d && $d->format( 'Y-m-d' ) === $ymd;
373 }
374
375 }
376