-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathDecorator.php
More file actions
159 lines (137 loc) · 4.48 KB
/
Copy pathDecorator.php
File metadata and controls
159 lines (137 loc) · 4.48 KB
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
<?php
namespace Carbon_Fields\REST_API;
use Carbon_Fields\Helper\Helper;
use Carbon_Fields\Container\Repository as ContainerRepository;
/**
* Decorate default REST routes with extra information provided by Carbon Fields
*/
class Decorator {
/**
* ContainerRepository instance
*
* @var ContainerRepository
*/
protected $container_repository;
/**
* @param ContainerRepository $container_repository
*/
public function __construct( ContainerRepository $container_repository ) {
$this->container_repository = $container_repository;
}
/**
* Boot up functionality
*/
public function boot() {
add_action( 'rest_api_init', array( $this, 'register_fields' ) );
}
/**
* Register Carbon Fields using the register_rest_field() function
*/
public function register_fields() {
$containers = $this->container_repository->get_containers();
$containers = array_filter( $containers, function( $container ) {
return ( $container->type !== 'theme_options' );
} );
foreach ( $containers as $container ) {
$fields = $container->get_fields();
$context = $container->type;
$type_callable = array( __CLASS__, "get_{$context}_container_settings" );
if ( ! is_callable( $type_callable ) ) {
continue; // unsupported container type
}
$types = call_user_func( $type_callable, $container );
foreach ( $fields as $field ) {
if ( ! $field->get_visible_in_rest_api() ) {
continue;
}
$getter = function( $object, $field_name ) use ( $container ) {
$object_id = self::get_object_id( $object, $container->type );
$value = Helper::get_value( $object_id, $container->type, '', $field_name );
$field = Helper::get_field( $container->type, $container->id, $field_name );
if ( apply_filters( 'carbon_fields_rest_api_return_attachments_as_urls', false, $value, $field, $object_id ) ) {
$attachments_class = [
"Carbon_Fields\Field\Media_Gallery_Field",
"Carbon_Fields\Field\File_Field",
"Carbon_Fields\Field\Image_Field"
];
if ( in_array( get_class( $field ), $attachments_class, true ) ) {
$value = Helper::get_attachments_urls($value);
}
}
return $value;
};
$setter = function( $value, $object, $field_name ) use ( $container ) {
$object_id = self::get_object_id( $object, $container->type );
Helper::set_value( $object_id, $container->type, '', $field_name, $value );
};
register_rest_field( $types,
$field->get_base_name(), array(
'get_callback' => $getter,
'update_callback' => $setter,
'schema' => null,
)
);
}
}
}
/**
* Get Post Meta Container visibility settings
*
* @param \Carbon_Fields\Container\Post_Meta_Container $container
* @return array
*/
public static function get_post_meta_container_settings( $container ) {
return $container->get_post_type_visibility();
}
/**
* Get Term Meta Container visibility settings
*
* @param \Carbon_Fields\Container\Term_Meta_Container $container
* @return array
*/
public static function get_term_meta_container_settings( $container ) {
return $container->get_taxonomy_visibility();
}
/**
* Get User Meta Container visibility settings
*
* @param \Carbon_Fields\Container\User_Meta_Container $container
* @return string
*/
public static function get_user_meta_container_settings( $container ) {
return 'user';
}
/**
* Get Comment Meta Container visibility settings
*
* @param \Carbon_Fields\Container\Comment_Meta_Container $container
* @return string
*/
public static function get_comment_meta_container_settings( $container ) {
return 'comment';
}
/**
* Retrieve ID from object based on $context
*
* @param object $object
* @param string $container_type
* @return null|int
*/
public static function get_object_id( $object, $container_type ) {
$object = is_array( $object ) ? (object) $object : $object;
$container_type = Helper::normalize_type( $container_type );
switch ( $container_type ) {
case 'post_meta': // fallthrough intended
case 'user_meta':
return isset( $object->id ) ? $object->id : $object->ID;
break;
case 'term_meta':
return isset( $object->id ) ? $object->id : $object->term_id;
break;
case 'comment_meta':
return isset( $object->id ) ? $object->id : $object->comment_ID;
break;
}
return null;
}
}