kylephillips / favorites

Simple and flexible favorite buttons for any WordPress post type.
https://favoriteposts.com
224 stars 85 forks source link

API for settings favorites #91

Open a-v-ebrahimi opened 6 years ago

a-v-ebrahimi commented 6 years ago

Hi

To use your plugin in mobile app, I need an API for setting or removing favorites, is this API available?

Thanks

gfargo commented 6 years ago

If I'm understanding this question correctly, I too could benefit from a wrapper function for add_favorite / remove_favorite. I'd be happy to create a PR to add them as well 👍

I was messing around with the Entity classes a bunch recently, however, it wasn't saving the post to the user_id I provided.

If there isn't a wrapper function at the moment would you be able to provide some insight into the workflow required to favorite a post from PHP / within an action hook?

Thanks again!

Update: _realized the plugin checks both $_POST['logged_in'] and $_POST['user_id'] before saving a post to users favorite. In my case, I was automatically logging in new users and trying to favorite the article however because the original $_POST didn't have those default WP args it was silently failing before saving the post to the user's meta._

// Adding post to user's favorites via PHP

// Setup new favorite class
$favorite = new \Favorites\Entities\Favorite\Favorite();
// Trigger an update with the desired `post_id`
$favorite->update( $post_id, 'active', get_current_blog_id() );

As noted above, Favorites will then save this meta to the user_id found inside $_POST.

aloveric commented 6 years ago

@GFargo Thanks for your help. I will try these two add_favorite / remove_favorite functions.