User Following System

时间:2018-06-10 作者:Matt H

我有一个自定义插件,允许用户相互跟踪。ajax提供的follow和unfollow功能很好,但仍有一些bug需要解决,我不确定如何修复它们。

当你第一次访问一个新用户的页面时,会出现一个按钮,上面写着“订阅”,当你单击该按钮时,ajax会启动,成功后,按钮会切换到一个隐藏的按钮,上面写着“取消订阅”,但问题是,当你第一次访问一个新用户的页面时,“取消订阅”按钮会首先出现,而这并不是要发生的。

当您订阅一个新用户,然后取消订阅时,如果您再次尝试单击“取消订阅”按钮,ajax会通知您一个错误,因为您不能取消订阅两次,这很好,但问题在于您尝试多次订阅一个用户时,由于某种原因不会返回错误,这是允许的。

这些是函数,请告诉我错误在哪里,以及如何修复错误。任何帮助都将不胜感激。

function tb_get_following( $user_id ) {

if ( empty( $user_id ) ) {
    $user_id = get_current_user_id();
}

$following = get_user_meta( $user_id, \'_tb_following\', true );

return (array) apply_filters( \'tb_get_following\', $following, $user_id );
}

function tb_get_followers( $user_id ) {

if ( empty( $user_id ) ) {
    $user_id = get_current_user_id();
}

$followers = get_user_meta( $user_id, \'_tb_followers\', true );

return (array) apply_filters( \'tb_get_followers\', $followers, $user_id );

}



function tb_follow_user( $user_id, $user_to_follow ) {

$following = tb_get_following( $user_id );

if ( $following && is_array( $following ) ) {
    $following[] = $user_to_follow;
} else {
    $following = array();
    $following[] = $user_to_follow;
}

// retrieve the IDs of all users who are following $user_to_follow
$followers = tb_get_followers( $user_to_follow );

if ( $followers && is_array( $followers ) ) {
    $followers[] = $user_id;
} else {
    $followers = array();
    $followers[] = $user_id;
}

do_action( \'tb_pre_follow_user\', $user_id, $user_to_follow );

// update the IDs that this user is following
$followed = update_user_meta( $user_id, \'_tb_following\', $following );

// update the IDs that follow $user_id
$followers = update_user_meta( $user_to_follow, \'_tb_followers\', $followers );

// increase the followers count
$followed_count = tb_increase_followed_by_count( $user_to_follow ) ;

if ( $followed ) {

    do_action( \'tb_post_follow_user\', $user_id, $user_to_follow );

    return true;
}
return false;
}



function tb_unfollow_user( $user_id, $unfollow_user ) {

do_action( \'tb_pre_unfollow_user\', $user_id, $unfollow_user );

// get all IDs that $user_id follows
$following = tb_get_following( $user_id );

if ( is_array( $following ) && in_array( $unfollow_user, $following ) ) {

    $modified = false;

    foreach ( $following as $key => $follow ) {
        if ( $follow == $unfollow_user ) {
            unset( $following[$key] );
            $modified = true;
        }
    }

    if ( $modified ) {
        if ( update_user_meta( $user_id, \'_tb_following\', $following ) ) {
            tb_decrease_followed_by_count( $unfollow_user );
        }
    }

}

// get all IDs that follow the user we have just unfollowed so that we can remove $user_id
$followers = tb_get_followers( $unfollow_user );

if ( is_array( $followers ) && in_array( $user_id, $followers ) ) {

    $modified = false;

    foreach ( $followers as $key => $follower ) {
        if ( $follower == $user_id ) {
            unset( $followers[$key] );
            $modified = true;
        }
    }

    if ( $modified ) {
        update_user_meta( $unfollow_user, \'_tb_followers\', $followers );
    }

}

if ( $modified ) {
    do_action( \'tb_post_unfollow_user\', $user_id, $unfollow_user );
    return true;
}

return false;
}


function tb_get_following_count( $user_id ) {

if ( empty( $user_id ) ) {
    $user_id = get_current_user_id();
}

$following = tb_get_following( $user_id );

$count = 0;

if ( $following ) {
    $count = count( $following );
}

return (int) apply_filters( \'tb_get_following_count\', $count, $user_id );
}



function tb_get_follower_count( $user_id ) {

if ( empty( $user_id ) ) {
    $user_id = get_current_user_id();
}

$followed_count = get_user_meta( $user_id, \'_tb_followed_by_count\', true );

$count = 0;

if ( $followed_count ) {
    $count = $followed_count;
}

return (int) apply_filters( \'tb_get_follower_count\', $count, $user_id );
}

function tb_increase_followed_by_count( $user_id ) {

do_action( \'tb_pre_increase_followed_count\', $user_id );

$followed_count = tb_get_follower_count( $user_id );

if ( $followed_count !== false ) {

    $new_followed_count = update_user_meta( $user_id, \'_tb_followed_by_count\', $followed_count + 1 );

} else {

    $new_followed_count = update_user_meta( $user_id, \'_tb_followed_by_count\', 1 );

}

do_action( \'tb_post_increase_followed_count\', $user_id );

return $new_followed_count;
}

function tb_decrease_followed_by_count( $user_id ) {

do_action( \'tb_pre_decrease_followed_count\', $user_id );

$followed_count = tb_get_follower_count( $user_id );

if ( $followed_count ) {

    $count = update_user_meta( $user_id, \'_tb_followed_by_count\', ( $followed_count - 1 ) );

    do_action( \'tb_post_increase_followed_count\', $user_id );

}
return $count;
}



function tb_is_following( $user_id, $followed_user ) {

$following = tb_get_following( $user_id );

$ret = false; // is not following by default

if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
    $ret = true; // is following
}

return (int) apply_filters( \'tb_is_following\', $user_id, $followed_user );

}
以下是html按钮的功能:

function tb_get_follow_unfollow_links( $follow_id = null ) {

global $user_ID;

if( empty( $follow_id ) )
    return;


if ( $follow_id == $user_ID )
    return;

ob_start(); ?>
    <?php if(is_user_logged_in()): ?>

        <?php if ( pwuf_is_following( $user_ID, $follow_id ) ) { ?>
        <a href="#" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>" class="button unfollow followed"><i></i> Unsubscribe</a>
        <a href="#" class="button follow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><i></i> Subscribe</a>
        <?php } else { ?>
        <a href="#" class="button follow" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><i></i> Subscribe</a>
        <a href="#" class="button followed unfollow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><i></i> Unsubscribe</a>
        <?php } ?>
        <?php else: ?>
        <a class="not-logged-inbutton"><i></i> Subscribe</a>
        <?php endif; ?>
<?php
return ob_get_clean();
}
以下是ajax操作和ajax功能:

function tb_process_new_follow() {
if ( isset( $_POST[\'user_id\'] ) && isset( $_POST[\'follow_id\'] ) ) {
    if( tb_follow_user( absint( $_POST[\'user_id\'] ), absint( $_POST[\'follow_id\'] ) ) ) {
        echo \'success\';
    } else {
        echo \'failed\';
    }
}
die();
}
add_action(\'wp_ajax_follow\', \'tb_process_new_follow\');

function tb_process_unfollow() {
if ( isset( $_POST[\'user_id\'] ) && isset( $_POST[\'follow_id\'] ) ) {
    if( tb_unfollow_user( absint( $_POST[\'user_id\'] ), absint( $_POST[\'follow_id\'] ) ) ) {
        echo \'success\';
    } else {
        echo \'failed\';
    }
}
die();
}
add_action(\'wp_ajax_unfollow\', \'tb_process_unfollow\');
jQuery:

jQuery(document).ready(function($) {
/*******************************
follow / unfollow a user
*******************************/
$( \'.follow-links a\' ).on(\'click\', function(e) {
    e.preventDefault();

    var $this = $(this);

    var data      = {
        action:    $this.hasClass(\'follow\') ? \'follow\' : \'unfollow\',
        user_id:   $this.data(\'user-id\'),
        follow_id: $this.data(\'follow-id\'),
        nonce:     pwuf_vars.nonce
    };

    $.post( pwuf_vars.ajaxurl, data, function(response) {
        console.log(data);
        if( response == \'success\' ) {
            $(\'.follow-links a\').toggle();
        } else {
            console.log( pwuf_vars.processing_error );
        }
    } );
});
});

1 个回复
SO网友:Bjorn

以下是一些提示:

为什么在中使用输出缓冲tb_get_follow_unfollow_links()? 我看没有必要这样做。

在里面tb_get_follow_unfollow_links() 仅回显1按钮。在页面加载时,使用php决定必须设置哪些类和按钮文本。

使用jQuery,在获得成功的ajax返回时更改按钮。更改按钮类,以便可以在单击时触发不同的ajax调用。

使用.on 要从动态(从ajax加载)元素获取事件,请执行以下操作:$( "#container" ).on( "click", ".element", function() {});.

第二个问题很容易通过一些调试发现。要检查用户是否已订阅,可以执行以下操作:

$following = get_user_meta( $user_id, \'_tb_following\', true );
if( ! in_array( $id_to_check, $following )) { // user not subscribed, do it now }
你好,比约恩

结束