-
Notifications
You must be signed in to change notification settings - Fork 1
/
entity.class.php
204 lines (150 loc) · 5.13 KB
/
entity.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace PostTypeBuilder;
class Entity {
public $post_object;
function __construct($post_object_or_post_id = NULL){
if(is_numeric($post_object_or_post_id)){
$post_id = $post_object_or_post_id;
$this->load_by_post_id($post_id);
} else if(is_object($post_object_or_post_id)) {
$post_object = $post_object_or_post_id;
if($post_object->ID == 0 || is_numeric($post_object->ID) == false){
throw new \Exception("Invalid post object supplied as entity (" . get_called_class() . ") constructor parameter \"{$post_object_or_post_id}\"");
}
$this->load_by_post_object($post_object);
} else if($post_object_or_post_id !== NULL) {
throw new \Exception("Unknown entity (" . get_called_class() . ") constructor parameter: \"{$post_object_or_post_id}\"");
} else {
$this->post_object = new \stdClass();
}
}
private function load_by_post_id($post_id){
$post_object = get_post($post_id);
if(is_object($post_object) == false){
throw new \Exception("Could not find (" . get_called_class() . ") entity by ID \"{$post_id}\".");
}
$this->load_by_post_object($post_object);
}
private function load_by_post_object($post_object){
$this->post_object = $post_object;
$class_meta = static::get_class_meta();
$class_meta->post_meta_manager->load($this);
}
public function save(){
$class_meta = static::get_class_meta();
if($this->ID == 0){
if(!isset($this->post_object->post_title)){
$this->post_object->post_title = "Unnamed " . strtolower($class_meta->labels["singular_name"]) . " (#" . strtoupper(substr(md5(time()), 0, 10)) . ")";
}
if(!isset($this->post_object->post_status)){
$this->post_object->post_status = "publish";
}
$this->post_object->post_type = $class_meta->post_type;
$post_id = wp_insert_post($this->post_object);
if($post_id != 0){
$this->post_object = get_post($post_id);
}
$class_meta->post_meta_manager->save($this);
return $this;
}
$class_meta->post_meta_manager->save($this);
if(defined('POSTTYPEBUILDER_SAVING_' . $this->ID)){
return $this;
} else {
define('POSTTYPEBUILDER_SAVING_' . $this->ID, true);
wp_update_post($this->post_object);
}
return $this;
}
public function delete(){
wp_delete_post($this->ID, true);
return $this;
}
public static function get_class_meta(){
$class_name = get_called_class();
$class_meta = PostTypeBuilder::$registered_class_names[$class_name];
return $class_meta;
}
public function __get($name)
{
if(isset($this->post_object->$name)){
return $this->post_object->$name;
} else if(isset($this->$name)){
return $this->$name;
} else {
$property_name = $name . "_id";
if(isset($this->$property_name)){
$class_meta = static::get_class_meta();
$value = $class_meta->post_meta_manager->lazy_load_single($this, $property_name);
return $value;
}
$property_name = substr($name, 0, strlen($name) - 1) . "_ids";
if(isset($this->$property_name)){
$class_meta = static::get_class_meta();
$value = $class_meta->post_meta_manager->lazy_load_multiple($this, $property_name);
return $value;
}
return null;
}
}
public function __set($name, $value)
{
if(isset($this->post_object->$name)){
$this->post_object->$name = $value;
} else {
$this->$name = $value;
}
}
public function generate_input_field($name_and_id, $value = null, $property = null, $property_annotation = null, $many = false){
global $wpdb;
if($many){
$many = "[]";
} else {
$many = "";
}
echo "<select name=\"{$name_and_id}{$many}\" id=\"{$name_and_id}\">";
if($value == null){
echo "<option value=\"\" selected=\"selected\"></option>";
} else {
echo "<option value=\"\"></option>";
}
$class_meta = static::get_class_meta();
foreach($wpdb->get_results("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_status != 'trash' AND post_status != 'auto-draft' AND post_status != 'inherit' AND post_type = '{$class_meta->post_type}'") as $post){
if($value == $post->ID){
echo "<option value=\"{$post->ID}\" selected=\"selected\">{$post->post_title}</option>";
} else {
echo "<option value=\"{$post->ID}\">{$post->post_title}</option>";
}
}
echo "</select>";
}
public function has_defined_property($property_name){
$class_meta = static::get_class_meta();
return $class_meta->post_meta_manager->has_property($property_name);
}
public function __toString(){
if($this->post_object != null){
if($this->post_object->post_title != ""){
return $this->post_object->post_title;
} else {
return "(entity with ID: {$this->post_object->ID})";
}
} else {
return "(entity without ID or title)";
}
}
public static function find($limit = -1){
$class_name = get_called_class();
$query = new Query(new $class_name());
return $query->limit($limit);
}
public static function find_one(){
$class_name = get_called_class();
$query = new Query(new $class_name());
return $query->limit(1);
}
public function to_serializable(){
require_once "to-serializable.php";
return posttypebuilder_to_serializable($this);
}
}