Skip to main content
deleted 6 characters in body
Source Link
Q Studio
  • 2.5k
  • 7
  • 25
  • 39
function set_like_count( $type = 'like' ) {

    // get post meta ##
    $array = get_post_meta( get_the_ID(), 'post_meta_field' );

    // @todo -- validate it is an array and the requested key exists ##

    // update count for post ##
    $array[ $type ][] = get_the_user_ip();

    // save ##
    update_post_meta( get_the_ID(), 'post_meta_field', $array );

 )

}
function set_like_count( $type = 'like' ) {

    // get post meta ##
    $array = get_post_meta( get_the_ID(), 'post_meta_field' );

    // @todo -- validate it is an array and the requested key exists ##

    // update count for post ##
    $array[ $type ][] = get_the_user_ip();

    // save ##
    update_post_meta( get_the_ID(), 'post_meta_field', $array );

 )

}
function set_like_count( $type = 'like' ) {

    // get post meta ##
    $array = get_post_meta( get_the_ID(), 'post_meta_field' );

    // @todo -- validate it is an array and the requested key exists ##

    // update count for post ##
    $array[ $type ][] = get_the_user_ip();

    // save ##
    update_post_meta( get_the_ID(), 'post_meta_field', $array );

}
Source Link
Q Studio
  • 2.5k
  • 7
  • 25
  • 39

Untested Pseudo-code examples, which you can expand to resolve the issues:

function get_the_user_ip() {

    if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {

        //check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];

    } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {

        // to check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

    } else {

        $ip = $_SERVER['REMOTE_ADDR'];

    }

    return apply_filters( 'get_the_user_ip', $ip );

}

Then, you can store all data in a single post_meta field, you just need to structure your array correctly.

array(
    'like' => array(
        'ip1', 'ip2', 'ip3', 'etc'
    ),
    'dislike' => array(
        'ip4', 'ip5', 'ip6', 'etc'
    )
)

This way you can create a few helper functions ( or, better a class with some getter and setter methods ) to reference when loading the UI and when saving data.

function get_like_count( $type = 'like' ) {

    // get post meta ##
    $array = get_post_meta( get_the_ID(), 'post_meta_field' );

    // @todo -- validate it is an array and the requested key exists ##

    // return count ##
    return count( $array[ $type ] );

}
function set_like_count( $type = 'like' ) {

    // get post meta ##
    $array = get_post_meta( get_the_ID(), 'post_meta_field' );

    // @todo -- validate it is an array and the requested key exists ##

    // update count for post ##
    $array[ $type ][] = get_the_user_ip();

    // save ##
    update_post_meta( get_the_ID(), 'post_meta_field', $array );

 )

}
/** check if the user IP is already stored for this post */
function has_liked() {

    // get post meta ##
    $array = get_post_meta( get_the_ID(), 'post_meta_field' );

    // @todo -- validate it is an array and the requested key exists ##

    // search the array for the IP value
    // this might need to search each array key if you want to know what action the user took ##
    return in_array( get_the_user_ip(), $array );

}