Guest User

Untitled

a guest
Jun 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. function tb_is_following( $user_id, $follow_id ) {
  2.  
  3. $following = tb_get_following( $user_id, $follow_id );
  4.  
  5. $ret = false; // is not following by default
  6.  
  7. if ( is_array( $following ) && in_array( $follow_id, $following ) ) {
  8. $ret = true; // is following
  9. }
  10.  
  11. return $ret;
  12. }
  13.  
  14. function tb_process_new_follow() {
  15. if ( isset( $_POST['user_id'] ) && isset( $_POST['follow_id'] ) ) {
  16. if( tb_follow_user( absint( $_POST['user_id'] ), absint( $_POST['follow_id'] ) ) ) {
  17. echo 'success';
  18.  
  19. } else {
  20. echo 'failed';
  21. }
  22. }
  23. die();
  24. }
  25. add_action('wp_ajax_follow', 'tb_process_new_follow');
  26.  
  27. function tb_process_unfollow() {
  28. if ( isset( $_POST['user_id'] ) && isset( $_POST['follow_id'] ) ) {
  29. if( tb_unfollow_user( absint( $_POST['user_id'] ), absint( $_POST['follow_id'] ) ) ) {
  30. echo 'success';
  31. } else {
  32. echo 'failed';
  33. }
  34. }
  35. die();
  36. }
  37. add_action('wp_ajax_unfollow', 'tb_process_unfollow');
  38.  
  39. jQuery(document).ready(function($) {
  40. /*******************************
  41. follow / unfollow a user
  42. *******************************/
  43. $( '.follow-links a' ).on('click', function(e) {
  44. e.preventDefault();
  45.  
  46. var $this = $(this);
  47.  
  48. var data = {
  49. action: $this.hasClass('follow') ? 'follow' : 'unfollow',
  50. user_id: $this.data('user-id'),
  51. follow_id: $this.data('follow-id'),
  52. nonce: tb_vars.nonce
  53. };
  54.  
  55. $.post( tb_vars.ajaxurl, data, function(response) {
  56. console.log(data);
  57. if( response == 'success' ) {
  58. $('.follow-links a').toggle();
  59. } else {
  60. console.log( tb_vars.processing_error );
  61. }
  62. } );
  63.  
  64.  
  65. });
  66. });
Add Comment
Please, Sign In to add comment