Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Pixelbart

    (@pixelbart)

    @sonnh

    Unfortunately, this is currently not possible. However, you can generate your own endpoints for the REST api. Depends on what you need.

    /**
     * @return void
     */
    add_action('rest_api_init', function () {
    
        if (!function_exists('helpful_get_pro')) {
            return;
        }
    
        $options = [
            'methods' => 'GET',
            'callback' => function() {
    
                if (isset($_GET['post_id']) && is_numeric($_GET['post_id'])) {
                    $post_id = intval(sanitize_text_field($_GET['post_id']));
    
                    return [
                        'pro' => helpful_get_pro($post_id),
                        'contra' => helpful_get_contra($post_id),
                    ];
                }
    
                return [
                    'pro' => helpful_get_pro_all(),
                    'contra' => helpful_get_contra_all(),
                ];
            },
            'permission_callback' => '__return_empty_string',
        ];
    
        /**
         * domain.com/wp-json/helpful/votes/
         * OR
         * domain.com/wp-json/helpful/votes/?post_id=12345
         */
        register_rest_route('helpful', '/votes/', $options);
    });

    If you put this in your functions.php you can get the following URLs with the REST API:

    All votes: domain.com/wp-json/helpful/votes/
    Single post: domain.com/wp-json/helpful/votes/?post_id=12345

    You can find more functions here:

    https://github.com/pixelbart/helpful/blob/master/core/functions/values.php

    Note that sometimes it is necessary to go to the permalinks page in your settings before the endpoint is available. So settings > call permalinks in your wp-admin.

    I may include something similar in the future. Unfortunately, I haven’t gotten around to it yet.

    Please stay healthy and have a nice day!

    Greetings Kevin

    Maybe you do not name the endpoint helpful, so that there are no conflicts in the future!

    • This reply was modified 3 years, 4 months ago by Pixelbart.
    Thread Starter sonnh

    (@sonnh)

    @pixelbart Thank you so much your example is really helpful.
    One more question, how about voting via REST api. I want to basic authen as admin then update vote count.

    Plugin Author Pixelbart

    (@pixelbart)

    @sonnh

    It is important that you generate a unique ID for the user here. This is necessary so that a user cannot vote more than once. You have to take care of this yourself, since this is not possible with the API.

    The code for the REST endpoint should look something like this:

    /**
     * @return void
     */
    add_action('rest_api_init', function () {
    
        if (!function_exists('helpful_get_pro')) {
            return;
        }
    
        $options = [
            'methods' => 'POST',
            'permission_callback' => '__return_empty_string',
        ];
    
        $options['callback'] = function($data) {
    
            if (!isset($data['user_id']) || '' === $data['user_id']) {
                return 0;
            }
    
            if (!isset($data['post_id']) || ! is_numeric($data['post_id'])) {
                return 0;
            }
    
            $user_id = sanitize_text_field($data['user_id']);
            $post_id = intval(sanitize_text_field($data['post_id']));
    
            Helpful\Core\Helpers::insert_vote($user_id, $post_id, 'pro', null);
    
            return 1;
        };
    
        /**
         * domain.com/wp-json/helpful/pro/?post_id=1234&user_id=1234
         */
        register_rest_route('helpful', '/pro/', $options);
    
        $options['callback'] = function($data) {
    
            if (!isset($data['user_id']) || '' === $data['user_id']) {
                return 0;
            }
    
            if (!isset($data['post_id']) || ! is_numeric($data['post_id'])) {
                return 0;
            }
    
            $user_id = sanitize_text_field($data['user_id']);
            $post_id = intval(sanitize_text_field($data['post_id']));
    
            Helpful\Core\Helpers::insert_vote($user_id, $post_id, 'contra', null);
    
            return 1;
        };
    
        /**
         * domain.com/wp-json/helpful/contra/?post_id=1234&user_id=1234
         */
        register_rest_route('helpful', '/contra/', $options);
    });

    After that you can try the following things to execute your request:

    /* Request URL */
    $url = 'domain.com/wp-json/helpful/pro/';
    $url = 'domain.com/wp-json/helpful/contra/';
    
    /**
     * Here we now enter the user who should publish the post.
     * As password we use the generated password by applications passwords from admin user profile.
     */
    $user = '<benutzername>';
    $pass = '<applications passwords>';
    
    /* Request header */
    $headers = [
        'Authorization' => 'Basic ' . base64_encode($user . ':' . $pass),
        'cache-control' => 'no-cache',
    ];
    
    /* Request body */
    $body = [
        'post_id' => 1234, // post_id for the vote
        'user_id' => 1234, // unique user_id
    ];
    
    /* Request arguments */
    $args = [
        'headers' => $headers,
        'body' => $body,
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'cookies' => [],
    ];
    
    /* Our request */
    $response = wp_remote_post($url, $args);
    
    /* If the request returns an error, the error is output */
    if (is_wp_error($response)) {
        print_r($response->get_error_message());
    }
    
    $response = wp_remote_retrieve_body($response);
    $response = wp_json_decode($response);
    $response = intval($response);
    
    if (1 === $response) {
        echo 'Vote was saved';
    }
    
    if (0 === $response) {
        echo 'Vote was not saved';
    }

    How this works with the REST API and what you have to consider, you have to read yourself in the codex. But this is how it should work.

    • This reply was modified 3 years, 4 months ago by Pixelbart.
    Thread Starter sonnh

    (@sonnh)

    Great !!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Can i access helpful plugin from WordPress Restfull API’ is closed to new replies.