PluginProbe
UpStream: a Project Management Plugin for WordPress / 1.39.0
UpStream: a Project Management Plugin for WordPress v1.39.0
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / model / UpStream_Model_Meta_Object.php

UpStream_Model_Meta_Object.php in UpStream: a Project Management Plugin for WordPress 1.39.0, at includes/model/UpStream_Model_Meta_Object.php

133 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined('ABSPATH')) {
5 exit;
6 }
7
8
9 class UpStream_Model_Meta_Object extends UpStream_Model_Object
10 {
11 protected $parent = null;
12
13 protected $metadataKey = null;
14
15 /**
16 * UpStream_Model_Meta_Object constructor.
17 */
18 public function __construct($parent, $item_metadata)
19 {
20 parent::__construct();
21
22 $this->parent = $parent;
23 $this->loadFromArray($item_metadata);
24 }
25
26 protected function loadFromArray($item_metadata)
27 {
28
29 $this->id = !empty($item_metadata['id']) ? $item_metadata['id'] : 0;
30 $this->title = !empty($item_metadata['title']) ? $item_metadata['title'] : null;
31 $this->assignedTo = !empty($item_metadata['assigned_to']) ? $item_metadata['assigned_to'] : [];
32 $this->createdBy = !empty($item_metadata['created_by']) ? $item_metadata['created_by'] : [];
33 $this->description = !empty($item_metadata['description']) ? $item_metadata['description'] : null;
34
35 $this->additionalFields = apply_filters('upstream_model_load_fields', $this->additionalFields, $item_metadata,
36 $this->type, $this->id);
37 }
38
39 public function storeToArray(&$item_metadata)
40 {
41
42 if (!($this->parent instanceof UpStream_Model_Post_Object)) {
43 // TODO: throw error
44 }
45
46 if ($this->id == 0) {
47 $this->id = uniqid(get_current_user_id());
48 }
49 $item_metadata['id'] = $this->id;
50
51 if ($this->title != null) $item_metadata['title'] = $this->title;
52 if (count($this->assignedTo) > 0) $item_metadata['assigned_to'] = $this->assignedTo;
53 if ($this->createdBy > 0) $item_metadata['created_by'] = $this->createdBy;
54 if ($this->description != null) $item_metadata['description'] = $this->description;
55
56 $dataToStore = [];
57 $dataToStore = apply_filters('upstream_model_store_fields', $dataToStore, $this->additionalFields,
58 $this->type, $this->id);
59
60 foreach ($dataToStore as $key => $value) {
61 $item_metadata[$key] = $value;
62 }
63
64 }
65
66 public function store()
67 {
68 if (!($this->parent instanceof UpStream_Model_Post_Object)) {
69 throw new UpStream_Model_ArgumentException(__('Parent is of the wrong type.', 'upstream'));
70 }
71
72 $added = false;
73
74 $new_item = [];
75 $this->storeToArray($new_item);
76
77 $itemset = get_post_meta($this->parent->id, $this->metadataKey);
78 if ($itemset && count($itemset) == 1 && is_array($itemset[0])) {
79 // it's ok
80 } else {
81 $itemset = [[]];
82 }
83
84 for ($i = 0; $i < count($itemset[0]); $i++) {
85 if ($itemset[0][$i]['id'] == $this->id) {
86
87 $itemset[0][$i] = $new_item;
88 $added = true;
89 break;
90
91 }
92 }
93
94 if (!$added) {
95 $itemset[0][] = $new_item;
96 }
97
98 $r = update_post_meta($this->parent->id, $this->metadataKey, $itemset[0]);
99
100 $projectObject = new UpStream_Project($this->parent->id);
101 $projectObject->update_project_meta();
102
103 }
104
105 public function __get($property)
106 {
107 switch ($property) {
108
109 case 'parentId':
110 if ($this->parent != null)
111 return $this->parent->id;
112 return 0;
113
114 case 'parent':
115 return $this->parent;
116
117 default:
118 return parent::__get($property);
119 }
120 }
121
122 public function __set($property, $value)
123 {
124 switch ($property) {
125
126 default:
127 parent::__set($property, $value);
128 break;
129
130 }
131 }
132
133 }