-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestFieldsRegistry.php
More file actions
156 lines (122 loc) · 3.37 KB
/
RestFieldsRegistry.php
File metadata and controls
156 lines (122 loc) · 3.37 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
<?php
namespace csrui\WPConstruct\Plugin\ContentType;
use csrui\WPConstruct\Plugin\ContentType\ContentTypeRegistry;
use csrui\WPConstruct\Plugin\ContentType\RestFields;
/**
* Handles registering Rest custom fields. Still in BETA.
*
*
* @since 0.0.1
* @package csrui\WPConstruct\Plugin\ContentType
* @author Rui Sardinha <mail@ruisardinha.com>
*/
class RestFieldsRegistry {
/**
* Get data from a WP post
*
* @since 0.0.1
* @access protected
* @param array $post A WP Post in array format
* @param array $objects List ACF Group fields
* @return array
*/
static protected function get_rest_from_post( array $post, array $objects ) : array {
$data = [];
if ( isset( $post['id'] ) !== true || is_numeric( $post['id'] ) !== true ) {
return $data;
}
if ( empty( $objects ) ) {
return $data;
}
foreach ( $objects as $obj ) {
// If object doesnt implement RestFields just ignore it
if ( ( $obj instanceof RestFields ) === false ) {
continue;
}
// Get list of rest fields
$fields = $obj->get_rest_fields();
foreach ( $fields as $field ) {
// Check first if a custom getter was defined for a given field.
if ( is_callable( [ $obj, "get_rest_{$field}" ] ) ) {
$data[ $field ] = call_user_func( [ $obj, "get_rest_{$field}" ], $post['id'] );
continue;
}
// If ACF detected, use it to retrieve metadata.
// It would be nice if this was chosen and not auto-detected
if ( function_exists( 'get_field' ) ) {
$data[ $field ] = get_field( $field, $post['id'] );
continue;
}
// Fallback to WordPress default function.
$data[ $field ] = get_post_meta( $post['id'], $field, true );
}
} // End foreach().
return $data;
}
/**
* Sort objects into specified groups.
*
* @since 0.0.0
* @param array $objects
* @return array
*/
static protected function sorter( array $objects = [] ) : array {
$data = [];
if ( empty( $objects ) ) {
return $data;
}
foreach ( $objects as $obj ) {
$locations = $obj->get_locations();
foreach ( $locations as $location ) {
if ( ! isset( $location['post_type'] ) ) {
continue;
}
$data[ $location['post_type'] ][] = $obj;
}
}
return $data;
}
/**
* Get a list of RestFields to register.
*
* @since 0.0.0
* @param array $plugin_components
* @return array
*/
static protected function get_objects_to_register( array $plugin_components = [] ) : array {
$data = [];
$objects = [];
foreach ( $plugin_components as $component ) {
if ( $component instanceof RestFields ) {
$objects[] = $component;
}
}
$data = static::sorter( $objects );
return $data;
}
/**
* Register each RestFields component.
*
* Example: RestFieldsRegistry::register( [
* Fields\Event::class,
* ] );
*
* @since 0.0.0
* @param array $plugin_components
* @return array
*/
static public function register( array $plugin_components = [] ) {
$objects_to_register = static::get_objects_to_register( $plugin_components );
add_action( 'rest_api_init', function() use ( $objects_to_register ) {
foreach ( $objects_to_register as $post_type => $objects ) {
register_rest_field( $post_type, 'custom_fields',
[
'get_callback' => function( $post ) use ( $objects ) {
return static::get_rest_from_post( $post, $objects );
}
]
);
}
} );
}
}