-
Notifications
You must be signed in to change notification settings - Fork 1
/
post-meta-manager.class.php
143 lines (99 loc) · 3.32 KB
/
post-meta-manager.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
<?php
namespace PostTypeBuilder;
use \ReflectionProperty;
class PostMetaManager{
private $keys = array();
private $registered_property_types = null;
function __construct($qualified_class_name){
global $wpdb;
$class_meta = PostTypeBuilder::$registered_class_names[$qualified_class_name];
$qualified_class_name = $class_meta->qualified_class_name;
$instance = new $qualified_class_name();
foreach($class_meta->class_reflector->getProperties(ReflectionProperty::IS_PUBLIC) as $property){
if(!$property->hasAnnotation("Addendum\\Property")){
continue;
}
$key = $property->getName();
$this->keys[] = $key;
}
//
$this->registered_property_types = array();
foreach($class_meta->class_reflector->getProperties(ReflectionProperty::IS_PUBLIC) as $property){
if(!$property->hasAnnotation("Addendum\\Property")){
continue;
}
$property_annotation = $property->getAnnotation("Addendum\\Property");
$this->registered_property_types[$property->getName()] = $property_annotation->type;
}
}
public function save($instance){
foreach($this->keys as $key){
$value = $instance->$key;
delete_post_meta($instance->ID, $key);
add_post_meta($instance->ID, $key, $value);
}
}
public function load($instance){
foreach($this->keys as $key){
$values = get_post_meta($instance->ID, $key);
foreach($values as $value){
if(is_string($value)){
$value = stripslashes_deep($value);
}
// handle legacy data stored as separate meta entries - otherwise $value is an unserialized array
if(is_string($value) && is_array($instance->$key)){
$property = &$instance->$key;
$property[] = $value;
continue;
}
$instance->$key = $value;
}
}
}
public function lazy_load_single($instance, $property_name){
$id = $instance->$property_name;
if(is_numeric($id) == false){
return null;
}
$type_name = $this->registered_property_types[$property_name];
if($type_name == "Image"){
return get_post($id);
}
if($type_name == "User"){
return get_userdata($id);
}
if(array_key_exists($type_name, PostTypeBuilder::$registered_class_names)){
$class_meta = PostTypeBuilder::$registered_class_names[$type_name];
$qualified_class_name = $class_meta->qualified_class_name;
return new $qualified_class_name($id);
}
return null;
}
public function lazy_load_multiple($instance, $property_name){
$type_name = $this->registered_property_types[$property_name];
$ids = $instance->$property_name;
if($type_name == "Image"){
return get_posts(array("post__in" => $ids, "post_type" => "attachment", "post_status" => "any")); // working ?
}
if($type_name == "User"){
$users = array();
foreach($ids as $id){
$users[] = get_userdata($id);
}
return $users;
}
if(array_key_exists($type_name, PostTypeBuilder::$registered_class_names)){
$class_meta = PostTypeBuilder::$registered_class_names[$type_name];
$qualified_class_name = $class_meta->qualified_class_name;
$instances = array();
foreach($ids as $id){
$instances[] = new $qualified_class_name($id);
}
return $instances;
}
return null;
}
public function has_property($property_name){
return array_key_exists($property_name, $this->registered_property_types);
}
}