wtyczki – System śledzenia użytkownika
Mam niestandardową wtyczkę, która pozwala użytkownikom śledzić się nawzajem. Funkcjonalność follow i unfollow działa przez ajax, co jest w porządku, ale jest tylko kilka błędów, które wymagają uporządkowania i nie jestem pewien, jak je naprawić.
-
Kiedy po raz pierwszy wchodzisz na stronę nowego użytkownika, pojawia się przycisk, który ma się pojawić z napisem „Subskrybuj”, a kiedy klikniesz ten przycisk, ajax uruchamia się, a następnie po sukcesie przycisk jest przełączany na ukryty przycisk z napisem „Anuluj subskrypcję ”, ale problem polega na tym, że kiedy po raz pierwszy odwiedzasz stronę nowego użytkownika, przycisk „Anuluj subskrypcję” pojawia się jako pierwszy, co nie powinno mieć miejsca.
-
Kiedy subskrybujesz nowego użytkownika, a następnie anulujesz subskrypcję, jeśli spróbujesz ponownie kliknąć przycisk anulowania subskrypcji, ajax powiadomi Cię o błędzie, ponieważ nie możesz dwukrotnie anulować subskrypcji, co jest w porządku, ale problem polega na próbie subskrybowania użytkownika więcej niż jeden raz, z jakiegoś powodu nie jest zwracany żaden błąd i jest to dozwolone.
Oto funkcje, daj mi znać, gdzie są błędy i jak mogę je naprawić. Każda pomoc byłaby bardzo mile widziana.
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 );
}
Oto funkcje przycisków 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();
}
Oto akcje ajax i funkcja 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 );
}
} );
});
});