Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. function pwuf_get_follow_unfollow_links( $follow_id = null ) {
  2.  
  3. global $user_ID;
  4.  
  5. if( empty( $follow_id ) )
  6. return;
  7.  
  8. if( ! is_user_logged_in() )
  9. return;
  10.  
  11. if ( $follow_id == $user_ID )
  12. return;
  13.  
  14. ob_start(); ?>
  15. <div class="follow-links">
  16. <?php
  17. if ( pwuf_is_following( $user_ID, $follow_id ) ) {
  18. $classes = "unfollow";
  19. $text = "Following";
  20. } else {
  21. $classes = "follow";
  22. $text = "Follow";
  23. }
  24. ?>
  25. <span><a href="#" class="<?php echo $classes; ?>" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span><?php echo $text; ?></span></a></span>
  26. <img src="<?php echo PWUF_FOLLOW_URL; ?>/images/loading.svg" class="pwuf-ajax" style="display:none;"/>
  27. </div>
  28. <?php
  29. return ob_get_clean();
  30. }
  31.  
  32. /**
  33. * Processes the ajax request to follow a user
  34. *
  35. * @access private
  36. * @since 1.0
  37. * @return void
  38. */
  39.  
  40. function pwuf_process_new_follow() {
  41. if ( isset( $_POST['user_id'] ) && isset( $_POST['follow_id'] ) ) {
  42. if( pwuf_follow_user( absint( $_POST['user_id'] ), absint( $_POST['follow_id'] ) ) ) {
  43. echo 'success';
  44. } else {
  45. echo 'failed';
  46. }
  47. }
  48. die();
  49. }
  50. add_action('wp_ajax_follow', 'pwuf_process_new_follow');
  51.  
  52.  
  53. /**
  54. * Processes the ajax request to unfollow a user
  55. *
  56. * @access private
  57. * @since 1.0
  58. * @return void
  59. */
  60.  
  61. function pwuf_process_unfollow() {
  62. if ( isset( $_POST['user_id'] ) && isset( $_POST['follow_id'] ) ) {
  63. if( pwuf_unfollow_user( absint( $_POST['user_id'] ), absint( $_POST['follow_id'] ) ) ) {
  64. echo 'success';
  65. } else {
  66. echo 'failed';
  67. }
  68. }
  69. die();
  70. }
  71. add_action('wp_ajax_unfollow', 'pwuf_process_unfollow');
  72.  
  73. /**
  74. * Loads plugin scripts
  75. *
  76. * @access private
  77. * @since 1.0
  78. * @return void
  79. */
  80.  
  81. function pwuf_load_scripts() {
  82. wp_enqueue_script( 'pwuf-follow', PWUF_FOLLOW_URL . 'js/follow.js', array( 'jquery' ) );
  83. wp_localize_script( 'pwuf-follow', 'pwuf_vars', array(
  84. 'processing_error' => __( 'There was a problem processing your request.', 'pwuf' ),
  85. 'login_required' => __( 'Oops, you must be logged-in to follow users.', 'pwuf' ),
  86. 'logged_in' => is_user_logged_in() ? 'true' : 'false',
  87. 'ajaxurl' => admin_url( 'admin-ajax.php' ),
  88. 'nonce' => wp_create_nonce( 'follow_nonce' )
  89. ) );
  90. }
  91. add_action( 'wp_enqueue_scripts', 'pwuf_load_scripts' );
  92.  
  93. jQuery(document).ready(function($) {
  94. /*******************************
  95. follow / unfollow a user
  96. *******************************/
  97. $( '.follow-links a' ).on('click', function(e) {
  98. e.preventDefault();
  99.  
  100. var $this = $(this);
  101.  
  102. if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
  103. alert( pwuf_vars.login_required );
  104. return;
  105. }
  106.  
  107. var data = {
  108. action: $this.hasClass('follow') ? 'follow' : 'unfollow',
  109. user_id: $this.data('user-id'),
  110. follow_id: $this.data('follow-id'),
  111. nonce: pwuf_vars.nonce
  112. };
  113.  
  114. $this.closest('.follow-links').find('img.pwuf-ajax').show();
  115.  
  116. $.post( pwuf_vars.ajaxurl, data, function ( response ) {
  117. if ( response == 'success' ) {
  118. if ( $this.hasClass( 'follow' ) ) {;
  119. $this.removeClass( 'follow' ).addClass( 'unfollow' );
  120. $this.find( 'span' ).text( 'Unfollow' );
  121. } else {;
  122. $this.removeClass( 'unfollow' ).addClass( 'follow' );
  123. $this.find( 'span' ).text( 'Follow' );
  124. }
  125. } else {
  126. alert( pwuf_vars.processing_error );
  127. }
  128. $this.closest('.follow-links').find('img.pwuf-ajax').hide();
  129. });
  130. });
  131. });
  132.  
  133. add_action('wp_ajax_user_following_by_ajax', 'user_following_by_ajax_callback');
  134. add_action('wp_ajax_nopriv_user_following_by_ajax', 'user_following_by_ajax_callback');
  135.  
  136. function user_following_by_ajax_callback() {
  137.  
  138. check_ajax_referer('user_more_following', 'security');
  139. $paged = $_POST['page'];
  140.  
  141. /// detect the author data with the name or with the author id
  142. $curauth = NULL;
  143.  
  144. if( isset($_POST['author_name']) AND trim($_POST['author_name']) != '' ) {
  145.  
  146. $curauth = get_user_by('slug', trim($_POST['author_name']) );
  147.  
  148. } elseif( isset($_POST['author']) AND intval($_POST['author']) > 0 ) {
  149.  
  150. $curauth = get_userdata( intval($_POST['author']) );
  151.  
  152. }
  153.  
  154. if( !is_null($curauth) ) {
  155.  
  156. $include = get_user_meta($curauth->ID, '_pwuf_following', true);
  157.  
  158. if ( empty( $include ) ) {
  159.  
  160. echo '<div class="no-follower">Not found followers yet <i class="fa fa-slack fa-1x" aria-hidden="true"></i></div>';
  161.  
  162. } else {
  163.  
  164. $args = array (
  165. 'order' => 'DESC',
  166. 'include' => $include,
  167. 'number' => '20',
  168. 'paged' => $paged
  169. );
  170.  
  171. $wp_user_query = new WP_User_Query( $args );
  172.  
  173. $users = $wp_user_query->get_results();
  174.  
  175. foreach ( $users as $user ) {
  176.  
  177. $avatar = get_avatar($user->user_email, 100);
  178. $author_profile_url = get_author_posts_url($user->ID);
  179. $profile = get_userdata($user->ID);
  180.  
  181. echo '<div class="members-name-9"><a href="', $author_profile_url, '">' . $profile->first_name .'</a>';
  182.  
  183. echo '<a href="', $author_profile_url, '">', $avatar , '</a>';
  184.  
  185. echo pwuf_get_follow_unfollow_links( $user->ID );
  186.  
  187. }
  188.  
  189. echo '</div>';
  190.  
  191. }
  192.  
  193. } else {
  194.  
  195. echo '<div class="no-follower">Not found followers yet <i class="fa fa-slack fa-1x" aria-hidden="true"></i></div>';
  196. }
  197.  
  198. wp_die();
  199. }
  200.  
  201. <script type="text/javascript">
  202. var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
  203. var page = 2;
  204. jQuery(function($) {
  205. $('body').on('click', '.loadmorefollowing', function(e) {
  206. e.preventDefault();
  207.  
  208. var data =
  209. {
  210. 'action': 'user_following_by_ajax',
  211. 'page': page,
  212. 'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
  213. 'author': '<?php echo esc_html($curauth->ID); ?>'
  214.  
  215. };
  216.  
  217. $.post(ajaxurl, data, function(response) {
  218.  
  219. if ( '' === response ) {
  220. $('.loadmorefollowing').hide();
  221. $('.nomorefollowing').show();
  222. return;
  223. }
  224.  
  225. $('#following-artists-cont').append(response);
  226. page++;
  227. });
  228. });
  229. });
  230. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement