带有计数器的帖子上的喜欢和不喜欢按钮-每个用户会话只允许每个帖子点击一次

时间:2020-12-27 作者:Dominic Herold

在“a”上工作;Like"E;vs“;Dislike"E;用于WordPress中的博客文章。虽然一切正常,但这里的目的是让它为来宾和登录用户服务,但更重要的是;只允许同一用户对每篇文章单击一次(喜欢或不喜欢)(试图了解我是否可以在这里使用WordPress会话?)。

Idea:

来宾/注册和登录用户单击“喜欢”或“不喜欢”=该特定帖子的两个按钮均已停用(无法再次单击)。如果同一用户访问同一网站上的不同帖子,而该用户尚未单击这两个按钮中的任何一个;他们可以单击“喜欢”或“不喜欢”。

我希望我说的很清楚,希望你们都能理解我。我正在寻找建议和代码示例,以及如何实现这一点。

As of now; 用户(无论是否登录)可以单击LikeDislike 他们想多少次就多少次,而计数却一直在滴答作响——这并不理想。

This is the code I\'m working on:

add_filter( \'the_content\', \'post_likes\' );
function post_likes( $content ) {

    // only on posts
    if ( is_singular( \'post\' ) ) {
        
        ob_start();

        ?>
            <ul class="likes">

                <li class="likes__item likes__item--like">
                    <a href="<?php echo add_query_arg( \'post_action\', \'like\'); ?>">
                        Useful (<?php echo get_post_like_count(\'likes\') ?>)
                    </a>
                </li>

                <li class="likes__item likes__item--dislike">
                    <a href="<?php echo add_query_arg(\'post_action\', \'dislike\'); ?>">
                        Dislike (<?php echo get_post_like_count(\'dislikes\') ?>)
                    </a>
                </li>

            </ul>
        
        <?php

        $output = ob_get_clean();

        return $output . $content;
    
    } else {
    
        return $content;
    }
}

function get_post_like_count( $type = \'likes\' ) {

    // like or dislike count
    $current_count = get_post_meta( get_the_id(), $type, true );

    return ( $current_count ? $current_count : 0 );
}


add_action( \'template_redirect\', \'process_post_like_click\' );
function process_post_like_click() {

    $processed_like = false;
    $redirect = false;

    // Check if like or dislike
    if ( is_singular( \'post\' ) ) {
        
        if ( isset( $_GET[\'post_action\'] ) ) {
            
            if ( $_GET[\'post_action\'] == \'like\' ) {
            
                // Like
                $like_count = get_post_meta( get_the_id(), \'likes\', true );

                if ( $like_count ) {
                    $like_count = $like_count + 1;
            
                } else {
                    $like_count = 1;
                }

                $processed_like = update_post_meta( get_the_id(), \'likes\', $like_count );
            
            } elseif ( $_GET[\'post_action\'] == \'dislike\' ) {

                // Dislike
                $dislike_count = get_post_meta( get_the_id(), \'dislikes\', true );

                if ( $dislike_count ) {
                    $dislike_count = $dislike_count + 1;
            
                } else {
                    $dislike_count = 1;
                }

                $processed_like = update_post_meta( get_the_id(), \'dislikes\', $dislike_count );
            }

            if ( $processed_like ) {
                $redirect = get_the_permalink();
                
                // how do I deactivate both buttons on this post while keping them intact on other posts?

            }
        }
    }

    // redirect (refresh)
    if ( $redirect ) {
        wp_redirect( $redirect );
        die;
    }
}
谢谢大家!

1 个回复
最合适的回答,由SO网友:Q Studio 整理而成

未经测试的伪代码示例,您可以扩展这些示例来解决这些问题:

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 );

}
然后,可以将所有数据存储在一个post\\u元字段中,只需正确构造数组即可。

array(
    \'like\' => array(
        \'ip1\', \'ip2\', \'ip3\', \'etc\'
    ),
    \'dislike\' => array(
        \'ip4\', \'ip5\', \'ip6\', \'etc\'
    )
)
通过这种方式,您可以创建一些助手函数(或者最好是一个具有一些getter和setter方法的类),以便在加载UI和保存数据时引用。

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 );

}