Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rest-api endpoint #90

Open
christophe-bazin opened this issue Feb 20, 2017 · 6 comments
Open

Rest-api endpoint #90

christophe-bazin opened this issue Feb 20, 2017 · 6 comments

Comments

@christophe-bazin
Copy link

Hi !
I begin to use this nice plugin, but I need to have a API endpoint.
It's planned ?

Thank you !

@lesterchan
Copy link
Owner

Nope, I have no time to add that in. Feel free to fork it and add it in =)

@christophe-bazin
Copy link
Author

Ok !
Thank you :)

@drzraf
Copy link
Contributor

drzraf commented Sep 14, 2017

FYI, you will find that here in the -dev branch I split to code to permit the use of a REST API

@drzraf
Copy link
Contributor

drzraf commented Sep 15, 2017

@lesterchan any hope/plan to see -dev merged?

@lesterchan
Copy link
Owner

@drzraf nope. It is still too huge a change and I have not look through the code yet. We also need more testers. If you really need it out in the public urgently, why not fork the plugin release the plugin as your own? Maybe like WP-PostRatings Plus or WP-PostRatings Adv.

@drzraf
Copy link
Contributor

drzraf commented Sep 18, 2017

For the record, this the code of my API endpoint (where comment are submitted through REST and can have an optional wp-postrating vote value)

/*
  Copyright (C) 2017, Raphaël Droz
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
*/

class WP_REST_Vote_Comments_Field {

    static function rest_api_init_additional_fields() {
        register_rest_field( 'comment',
                             'vote',
                             array(
                                 'get_callback'    => [__CLASS__, 'comment_get_vote'],
                                 'update_callback' => [__CLASS__, 'comment_set_vote'],
                                 'schema'          => array(
                                     'description' => __( 'Rate associated with the comment.' ),
                                     'type'        => 'integer',
                                     'minimum'     => 0,
                                     'maximum'     => intval(get_option('postratings_max'))
                                 ),
                             )
        );
    }

    static function comment_get_vote( $object, $field_name, $request ) {
        if ( ( $rate_id = get_comment_meta( $object['id'], 'postratings_id', true ) ) ) {
            if (intval($rate_id)) {
                global $wpdb;
                $rate = $wpdb->get_var( $wpdb->prepare( "SELECT rating_rating FROM {$wpdb->ratings} WHERE rating_id = %d", intval($rate_id)) );
                return $rate ? intval($rate) : false;
            }
        }
        return false;
    }

    static function comment_set_vote( $value, $object, $field_name ) {
        if ( ! $value || ! is_numeric( $value ) || $field_name == 'vote' ) {
            return;
        }

        if ( ! is_plugin_active('wp-postratings/wp-postratings.php') ||
             ! function_exists('process_ratings_from_rest_API')) {
            return ;
        }

        $vote_from_API = process_ratings_from_rest_API($object, intval($value));
        if ($vote_from_API === true ) {
            return $rate;
        }

        // WP_Error || false
        return $vote_from_API;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants