-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest-meta-fields.php
More file actions
160 lines (143 loc) · 4.54 KB
/
rest-meta-fields.php
File metadata and controls
160 lines (143 loc) · 4.54 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
160
<?php defined( 'ABSPATH' ) || exit;
/**
* Rest Meta Fields class
*
* Based on WP_REST_Post_Meta_Fields class
*/
/**
* Core class used to manage meta values for posts via the REST API.
*
* @since 1.0.0
*
* @see WP_REST_Meta_Fields
*/
class TDB_REST_Meta_Fields extends WP_REST_Meta_Fields {
/**
* Table name.
*
* @since 1.0.0
* @var string
*/
protected $name;
/**
* Table Meta table object.
*
* @since 1.0.0
* @access public
* @var TDB_Table $table
*/
public $table;
/**
* Constructor.
*
* @since 1.0.0
*
* @param string $name Table name to register fields for.
*/
public function __construct( $name ) {
$this->name = $name;
$this->table = tdb_get_table_object( $name );
}
/**
* Retrieves the object meta type.
*
* @since 1.0.0
*
* @return string The meta type.
*/
protected function get_meta_type() {
return $this->table->meta->name;
}
/**
* Retrieves the object meta subtype.
*
* @since 1.0.0
*
* @return string Subtype for the meta type, or empty string if no specific subtype.
*/
protected function get_meta_subtype() {
return $this->name;
}
/**
* Retrieves the type for register_rest_field().
*
* @since 1.0.0
*
* @see register_rest_field()
*
* @return string The REST field type.
*/
public function get_rest_field_type() {
return $this->name;
}
/**
* Retrieves the meta field value.
*
* @since 1.0.0
*
* @param int $object_id Object ID to fetch meta for.
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|object Object containing the meta values by name, otherwise WP_Error object.
*/
public function get_value( $object_id, $request ) {
$fields = $this->get_registered_fields();
$response = array();
foreach ( $fields as $meta_key => $args ) {
$name = $args['name'];
$all_values = tdb_get_object_meta( $object_id, $meta_key, false );
if ( $args['single'] ) {
if ( empty( $all_values ) ) {
$value = $args['schema']['default'];
} else {
$value = $all_values[0];
}
$value = $this->prepare_value_for_response( $value, $request, $args );
} else {
$value = array();
foreach ( $all_values as $row ) {
$value[] = $this->prepare_value_for_response( $row, $request, $args );
}
}
$response[ $name ] = $value;
}
return $response;
}
/**
* Updates a meta value for an object.
*
* @since 4.7.0
*
* @param int $object_id Object ID to update.
* @param string $meta_key Key for the custom field.
* @param string $name Name for the field that is exposed in the REST API.
* @param mixed $value Updated value.
* @return bool|WP_Error True if the meta field was updated, WP_Error otherwise.
*/
protected function update_meta_value( $object_id, $meta_key, $name, $value ) {
$meta_type = $this->get_meta_type();
if ( ! current_user_can( $this->table->cap->edit_post_meta, $object_id, $meta_key ) ) {
return new WP_Error(
'rest_cannot_update',
/* translators: %s: custom field key */
sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.' ), $name ),
array( 'key' => $name, 'status' => rest_authorization_required_code() )
);
}
// Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
$old_value = tdb_get_object_meta( $object_id, $meta_key );
$subtype = get_object_subtype( $meta_type, $object_id );
if ( 1 === count( $old_value ) ) {
if ( (string) sanitize_meta( $meta_key, $value, $meta_type, $subtype ) === $old_value[0] ) {
return true;
}
}
if ( ! tdb_update_object_meta( $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
return new WP_Error(
'rest_meta_database_error',
__( 'Could not update meta value in database.' ),
array( 'key' => $name, 'status' => WP_Http::INTERNAL_SERVER_ERROR )
);
}
return true;
}
}