Repository navigation
Conversation
|
Thanks for the pull request; the request itself looks great :) My only concern is how we handle the post meta. We need to provide some mechanism to delete post meta, so I think this needs to run through the existing meta and do an |
|
(Oh, and my one tip for any future pull requests: consider branching so that you can do multiple pull requests without needing to combine them. Not an issue for this, but could be in the future. :) ) |
I obviously didn't think this one bit through 😬
Are we talking about a brute(ish) delete/recreate of the post_meta? I wasn't too certain either, actually |
For example:
The way we could handle this is:
|
|
May I ask, why should updating a post through the API delete all missing meta, when a save in WP admin does no such thing (that I'm aware of at least)? |
I thought the admin did? Note that this isn't including underscore-prefixed meta, which isn't visible via the API. |
|
Just to be clear, the final result would be what? |
Sorry, to be clear, the result would be I don't know of anyone that uses |
I just had a quick look through |
Hmm. Adding an extra key to contain meta to delete makes it unambiguous, but also breaks the entity-body to resource mapping, which would manifest as weird data in something like Backbone. From a REST standpoint, excluding the entry from the post meta is really the only "correct" way to achieve this, but we might have to make tradeoffs here. |
|
Just an idea..., not sure about "REST correctness": Check for POST/PUT/PATCH Or... As far as I know POST is meant for creating and should occur on It is a way around. This way delete would be possible only by POST and expect full set of keys, missing keys would be removed. Still an extra GET might be always necessary. |
|
You can give it a try. Here's a quick patch for |
Thanks for that! I've only had a quick look so far, but I'll take a more in-depth one ASAP. One thing: you should be able to get all the meta for a post by doing |
|
We need See We are sending post meta as key-value pairs, where key is Even though there would be such one, when deleting many fields, many of DB calls will occur. I did it in one call up-front (performance), and since output was array of arrays, I recreated it to more appropriate array of indexes (meta_key) and values (meta_id) for later use. This way an array can be used with conjunction with FYI: In the meantime I've forked the project, and hacked it a little. Now messing around with the terms, which might require an endpoint to delete a term... breaks post_meta "convention". Will think of it a little bit more. Cheers |
We should be able to do it with |
|
If those checks from |
|
One thing still bugs me: Here are some of my thoughts about how to rethink the feature: One [post] to many post meta [of the same meta_key] If more than 1 value exists in Why do we still treat post_meta as second class citizens? Is there a reason why post meta are under Any thoughts? |
Definitely agree. Multiple post meta is a pain to deal with, especially since it's not that common. I'm going to think over this a bit and see if I can come up with a good solution. If you want to try treating post meta as a first-class citizen, I'd love to see what you'd come up with. :) Thanks for all your work on this @attitude, and for caring about the project! Ditto for @zedejose for the initial patch and kicking off the effort here. 🍰 |
|
First-class they are. Check out the branch where post_meta is part of post attributes. I have rewrote the update post_meta part, also with the repeating arrays of "loop fields". Would require some rewriting this experiment for production. Examples:
PS: I am aware that post meta functionality works only with an extra page refresh to see the fresh data. Some caching is messing around with me probably. |
|
@attitude Thanks for writing all of this up. I'm going to take a look at it this week and read over your notes and patch. :) |
|
I don't see the point in merging post meta and post attributes. I am unsure whether delete post meta should be supported out-of-the-box. I think there should be a hook/filter that one can utilize to delete meta. My take on this is similar to zedejose but offers a few extra advantages. First, it is isolated in it's own method. This is useful for people who end up extending the class and overwriting the insert_post method. Also, my code provides a mechanism for sanitizing meta before inserting them into the DB; this is absolutely critical IMHO. |
|
New to WordPress but not to PHP, I'm trying to get the users endpoint to a point where I can use it for a paid project: https://github.com/tobych/WP-API/compare/users. I'd like to get something like it pulled into WP-API so I don't have to maintain my own fork. Well, things were straightforward until I hit metadata, and of course it's pretty much the same story as metadata for posts. What I did was add a user_meta member to the User entity. I'm certainly not doing everything gorgeously though and reading this thread I realize there are plenty of alternatives. The non-meta part of my User representation looks like this: So far, pretty much the same as the representation used in a Post's author. The metadata part adds a So far, so straightforward. Everything's a string. And every metadata field is representation as an array; so far each has only one value, so they're all single-length arrays. Over-the-top perhaps, but consistent. Then come fields that are serialized using PHP's serialize() method. I'm doing this: That solves the serialization problem for me. But what about single vs multiple values for each key? As with posts, most metadata fields have just one value. Usually, more than that wouldn't make sense. At this point I realized I was up against history versus current usage in WordPress and all sorts of other issues, and don't know what I'm doing. Certainly, if the client POSTs or PUTs a representation using a single value, my server code treats it just as it would a single-length array. But which of these should GET give you? or Should properly RESTful representations have just the one true way, or is some flexibility okay? I imagine it would be best just to have things look like the second. As to deleting metadata values, it seems to me that it'd be best to treat metadata items as first-class entities for this purpose, and perhaps to use the same representation inline in the User representation. I'm wondering whether @rmccue (Ryan) and @attitude (Martin) have the same understanding of "first class", above. I would have understood first class as being a separate resource, so for example |
|
@tobych From my quick glance over your code, I'm liking it. Can you open a pull request for that code, but with the meta handling removed? We can discuss user specifics on a separate ticket. :) My thinking at the moment is that we should handle all of the meta handling at once, separate to the other endpoints, so the user stuff can be handled independently of how we handle this ticket. I will come back and look at the rest of this ticket. We need to work out post meta, and get it handled. We've got a couple of alternative approaches here, and I need to thinking over how we handle it. We need to handle all of the following:
We can't leave any of these out. In addition, we need to work out a system that works for not just post meta, but other types of meta (such as user meta). If we don't, we'll introduce a lot of client complexity to handle all the different types of meta. I'm not against including it both with the entity and also as separate endpoints, but we need to think through whether this is appropriate. Again, I will come back and add more thoughts, these are just some quick ones as I do triaging. |
|
Will do! |
Agreed on this one; let's keep it namespaced under @tlovett1 I like your approach in tlovett1/WP-API@3ef9712 so far, but needs to handle the other cases. |
Indeed; I meant first-class handling to have the meta as separate resources available from e.g.
Right. The issue with this is as follows:
You can easily see why this is a contentious issue. :) Here's what I propose:
We can try this approach with post meta first, and if it works, port it to user meta too. In the future, we can think about serialized meta handling separately, but I want to get something shippable here. Thoughts? I'd like to get some action happening on this ticket and get some momentum, but we should all try and agree on a baseline here. |
…eta. Filter for passing sanitization callbacks; filter for requiring santizition callbacks. This method assumes values are passed as they are intended to be stored (array or single). In response to WP-API#68
|
@rmccue , let me know what you think of this one. Update/add/delete post meta. Assume values are passed as they are intended to be stored. This isn't the prettiest solution, but in my opinion the only way to really handle all the cases you mentioned. The commit needs a little cleaning. Sorry about that. |
Doesn't appear to give an error for serialized values. Also, sanitization should be handled at a lower level; see |
|
@rmccue Removed the sanitization callback stuff. I don't understand the serialization problem you are trying to solve. If you want to store a single value in a meta, you send a string/int/boolean. If you want to store an array in meta, you pass an array. If you want to store meta multiple times with the same key, you set the action to "add". |
Right, but when you're retrieving the data back, what if you get something like... [
"a",
"b"
]Is that a single value that's an array, or multiple values? |
|
@rmccue - Ah I see. I was only thinking about pushing content not pulling it. I think the best way to handle this is to represent the data like it is in the database: |
Right, so then... 'post_meta' : {
'key1' : [ 'a', 'b', 'c' ],
'key2' : [ 'a', 'b', 'c' ]
}If Similarly, if a value is Deserializing means we have to face possible lossy data update/insertion, plus a confusing API, which is why we should just skip handling it for now so that we have something to ship. |
|
@rmccue - I see what you are saying now. Following the convention I have for pushing data: Regarding, not knowing if something is a user or not. When pulling from the API you should be expecting specific data. I'm not looking for users when pulling for posts. I don't like the idea of sending serialized data. It just seems hacky. I think we sacrifice the strange data loss situation for the less difficult-to-use API. I might be inclined to agree with you, if you could provide an example of a situation someone might actually encounter where their data gets serialized and they don't know how to handle it. I agree something should be shipped on this ASAP. I think we are over-complicating. |
Right; that's why I'm thinking we just ignore it completely for now, and don't send/handle it at all. The only real issue we have with doing so is that not all data will be displayed, but we're already hiding data (
Real life example from some of my own code: class Token {
protected $data;
public function __construct( $key, $secret, $expiration ) {
$this->data = array(
'key' => $key,
'secret' => $secret,
'expiration' => new DateTime( $expiration ),
);
}
// ...
}
// ...
$data = new Token( $key, $secret, $expiration );
update_post_meta( $post, 'token', $data );Storing that in serialized form is fine, however the JSON-encoded version is useless: Hence, a GET request would give Worse than that, doing a |
|
Interesting example. Where should we go from here? What is left that is blocking a merge? Certainly some unit tests are needed. |
|
See #168 for the current approach and related feedback in relation to this issue. |
|
Posted for feedback on the o2. |
Add ability to add, update, delete post_meta. Fixes WP-API#68. Closes WP-API#189 and WP-API#168.
Logic to read post_meta from the sent json and update a post with it. Code has been moved to after the post is created or updated, since we need an ID for update_post_meta.
This is my first pull request ever, if there are errors or misconceptions please be so kind as to tell me what I'm doing wrong