1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\REST_API; |
4
|
|
|
|
5
|
|
|
use \Carbon_Fields\Helper\Helper; |
6
|
|
|
use \Carbon_Fields\Container\Repository as ContainerRepository; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Decorate default REST routes with extra information provided by Carbon Fields |
10
|
|
|
*/ |
11
|
|
|
class Decorator { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ContainerRepository instance |
15
|
|
|
* |
16
|
|
|
* @var ContainerRepository |
17
|
|
|
*/ |
18
|
|
|
protected $container_repository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ContainerRepository $container_repository |
22
|
|
|
*/ |
23
|
|
|
public function __construct( ContainerRepository $container_repository ) { |
24
|
|
|
$this->container_repository = $container_repository; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Boot up functionality |
29
|
|
|
*/ |
30
|
|
|
public function boot() { |
31
|
|
|
add_action( 'rest_api_init', array( $this, 'register_fields' ) ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Register Carbon Fields using the register_rest_field() function |
36
|
|
|
*/ |
37
|
|
|
public function register_fields() { |
38
|
|
|
$containers = $this->container_repository->get_containers(); |
39
|
|
|
$containers = array_filter( $containers, function( $container ) { |
40
|
|
|
return ( $container->type !== 'Theme_Options' ); |
41
|
|
|
} ); |
42
|
|
|
|
43
|
|
|
foreach ( $containers as $container ) { |
44
|
|
|
$fields = $container->get_fields(); |
45
|
|
|
$context = strtolower( $container->type ); |
46
|
|
|
$type_callable = array( __CLASS__, "get_{$context}_container_settings" ); |
47
|
|
|
if ( !is_callable( $type_callable ) ) { |
|
|
|
|
48
|
|
|
continue; // unsupported container type |
49
|
|
|
} |
50
|
|
|
$types = call_user_func( $type_callable, $container ); |
51
|
|
|
|
52
|
|
|
foreach ( $fields as $field ) { |
53
|
|
|
// TODO remove debug statement |
54
|
|
|
if ( false && ! $field->get_visible_in_rest_api() ) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$getter = function( $object, $field_name, $request ) use ( $container ) { |
|
|
|
|
59
|
|
|
$object_id = self::get_object_id( $object, $container->type ); |
60
|
|
|
return Helper::get_value( $object_id, $container->type, $field_name ); |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
$setter = function( $value, $object, $field_name ) use ( $container ) { |
64
|
|
|
$object_id = self::get_object_id( $object, $container->type ); |
65
|
|
|
$success = Helper::set_value( $object_id, $container->type, $field_name, $value ); |
66
|
|
|
if ( ! $success ) { |
67
|
|
|
echo 'Failed to find or update field "' . $key . '".'; |
|
|
|
|
68
|
|
|
exit; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
}; |
71
|
|
|
|
72
|
|
|
register_rest_field( $types, |
73
|
|
|
$field->get_base_name(), array( |
74
|
|
|
'get_callback' => $getter, |
75
|
|
|
'update_callback' => $setter, |
76
|
|
|
'schema' => null, |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get Post Meta Container visibility settings |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public static function get_post_meta_container_settings( $container ) { |
89
|
|
|
return $container->settings['post_type']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get Term Meta Container visibility settings |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public static function get_term_meta_container_settings( $container ) { |
98
|
|
|
return $container->settings['taxonomy']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get User Meta Container visibility settings |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public static function get_user_meta_container_settings( $container ) { |
|
|
|
|
107
|
|
|
return 'user'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get Comment Meta Container visibility settings |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public static function get_comment_meta_container_settings( $container ) { |
|
|
|
|
116
|
|
|
return 'comment'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieve ID from object based on $context |
121
|
|
|
* |
122
|
|
|
* @param object $object |
123
|
|
|
* @param string $container_type |
124
|
|
|
* @return null|int |
125
|
|
|
*/ |
126
|
|
|
public static function get_object_id( $object, $container_type ) { |
127
|
|
|
$object = is_array( $object ) ? (object) $object : $object; |
128
|
|
|
switch ( $container_type ) { |
129
|
|
|
case 'Post_Meta': // fallthrough intended |
130
|
|
|
case 'User_Meta': |
131
|
|
|
return isset( $object->ID ) ? $object->ID : $object->id; |
132
|
|
|
break; |
|
|
|
|
133
|
|
|
case 'Term_Meta': |
134
|
|
|
return $object->term_id; |
135
|
|
|
break; |
|
|
|
|
136
|
|
|
case 'Comment_Meta' : |
|
|
|
|
137
|
|
|
return $object->comment_ID; |
138
|
|
|
break; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|