Skip to content

Instantly share code, notes, and snippets.

@neverything
Forked from joehoyle/post-likes.php
Created October 6, 2016 13:02
Show Gist options
  • Select an option

  • Save neverything/12c48e8ca76ad8d7f86b33059fcba194 to your computer and use it in GitHub Desktop.

Select an option

Save neverything/12c48e8ca76ad8d7f86b33059fcba194 to your computer and use it in GitHub Desktop.

Revisions

  1. @joehoyle joehoyle revised this gist Jul 15, 2016. 1 changed file with 30 additions and 17 deletions.
    47 changes: 30 additions & 17 deletions post-likes.php
    Original file line number Diff line number Diff line change
    @@ -8,17 +8,25 @@
    * Author URI: http://joehoyle.co.uk
    */

    add_action( 'rest_api_init', function() {
    namespace PostLikes;

    use WP_Error;
    use WP_REST_Server;
    use WP_REST_Response;

    add_action( 'rest_api_init', __NAMESPACE__ . '\\add_rest_field' );
    add_action( 'rest_api_init', __NAMESPACE__ . '\\add_rest_route' );

    /*
    * Register a new field on the REST API "post" object
    * so clients can display the Post Like count with posts.
    */
    function add_rest_field() {

    // no-op if we don't have the rest api plugin available
    if ( ! function_exists( 'register_rest_field' ) ) {
    return;
    }

    /*
    * Register a new field on the REST API "post" object
    * so clients can display the Post Like count with posts.
    */
    register_rest_field( 'post', 'liveblog_likes', array(
    'get_callback' => function( $request ) {
    return (int) get_post_meta( $request['id'], 'liveblog_likes', true );
    @@ -29,16 +37,18 @@
    'context' => array( 'view', 'edit', 'embed' ),
    ),
    ) );
    }

    /*
    * Register a new REST API route for /liveblog-likes/v1/posts/$id/like
    * to allow clients to "like" any post. Only allow posts that are of
    * type "post" and published to be liked.
    */
    /*
    * Register a new REST API route for /liveblog-likes/v1/posts/$id/like
    * to allow clients to "like" any post. Only allow posts that are of
    * type "post" and published to be liked.
    */
    function add_rest_route() {
    register_rest_route( 'liveblog-likes/v1', '/posts/(?P<id>[\d]+)/like', array(
    'methods' => WP_REST_Server::CREATABLE,
    'args' => array(
    'id' => array(
    'id' => array(
    'sanitize_callback' => 'absint',
    ),
    ),
    @@ -54,10 +64,13 @@

    return true;
    },
    'callback' => function( $request ) {
    'callback' => function( $request ) {
    $likes = absint( get_post_meta( $request['id'], 'liveblog_likes', true ) ) + 1;
    update_post_meta( $request['id'], 'liveblog_likes', $likes );
    return new WP_REST_Response( $likes, 201 );
    }
    ) );
    } );
    $response = array(
    'count' => $likes,
    );
    return new WP_REST_Response( $response, 201 );
    },
    ));
    }
  2. @joehoyle joehoyle created this gist Jul 15, 2016.
    63 changes: 63 additions & 0 deletions post-likes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    <?php

    /*
    * Plugin Name: Post Likes
    * Description: WP REST API Post Likes
    * Author: Joe Hoyle
    * Version: 1.0
    * Author URI: http://joehoyle.co.uk
    */

    add_action( 'rest_api_init', function() {

    // no-op if we don't have the rest api plugin available
    if ( ! function_exists( 'register_rest_field' ) ) {
    return;
    }

    /*
    * Register a new field on the REST API "post" object
    * so clients can display the Post Like count with posts.
    */
    register_rest_field( 'post', 'liveblog_likes', array(
    'get_callback' => function( $request ) {
    return (int) get_post_meta( $request['id'], 'liveblog_likes', true );
    },
    'schema' => array(
    'type' => 'integer',
    'description' => 'The number of Liveblog Likes the post has.',
    'context' => array( 'view', 'edit', 'embed' ),
    ),
    ) );

    /*
    * Register a new REST API route for /liveblog-likes/v1/posts/$id/like
    * to allow clients to "like" any post. Only allow posts that are of
    * type "post" and published to be liked.
    */
    register_rest_route( 'liveblog-likes/v1', '/posts/(?P<id>[\d]+)/like', array(
    'methods' => WP_REST_Server::CREATABLE,
    'args' => array(
    'id' => array(
    'sanitize_callback' => 'absint',
    ),
    ),
    'permission_callback' => function( $request ) {

    if ( 'post' !== get_post_type( $request['id'] ) ) {
    return new WP_Error( 'invalid-post-type', 'You can only like posts.', array( 'status' => 400 ) );
    }

    if ( 'publish' !== get_post_status( $request['id'] ) ) {
    return new WP_Error( 'invalid-post-status', 'You can only like posts that are published.', array( 'status' => 400 ) );
    }

    return true;
    },
    'callback' => function( $request ) {
    $likes = absint( get_post_meta( $request['id'], 'liveblog_likes', true ) ) + 1;
    update_post_meta( $request['id'], 'liveblog_likes', $likes );
    return new WP_REST_Response( $likes, 201 );
    }
    ) );
    } );