BP_FB_like_notification

The FB like notification for buddypress BP FB like notification class.

Defined (2)

The class is defined in the following location(s).

/bp-fb-like-notification.php  
  1. class BP_FB_like_notification { 
  2. private static $instance; 
  3.  
  4. public static function get_instance() { 
  5. if(!isset(self::$instance)) { 
  6. self::$instance=new self(); 
  7. return self::$instance; 
  8.  
  9. function __construct() { 
  10. $load_plugin = false; 
  11. if( is_user_logged_in() ) { 
  12. $load_plugin = true; 
  13.  
  14. /** The plugin loads another javascript file(one more HTTP request), and causes multiple ajax requests to server. 
  15. * You can use the below filter to conditionaly load the plugin. 
  16. E.g: load the plugin based on user roles!! 
  17. just return false, from your hooked function and the plugin wouldn't be loaded 
  18. */ 
  19. $load_plugin = apply_filters( 'bfln_load_plugin', $load_plugin ); 
  20.  
  21. if( $load_plugin ) { 
  22. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  23. add_action( 'wp_ajax_bfln_get_notification', array( $this, 'ajax_get_notifications' ) );  
  24.  
  25. function enqueue_scripts() { 
  26. wp_enqueue_script( 
  27. "bfln_main",  
  28. path_join( WP_PLUGIN_URL, basename( dirname( __FILE__ ) )."/bp-fb-like-notification_v4.js" ),  
  29. array( 'jquery' ) 
  30. ); 
  31.  
  32. /** 
  33. The time interval : after each interval an ajax request to check for new notification is triggered. 
  34. Default is 2 minutes : 2*60*1000: in milliseconds 
  35. If you want change 2 minutes to something else, use the filter 
  36. */ 
  37. $time = apply_filters( 'bfln_ajax_interval', 2*60*1000 ); 
  38.  
  39. $arguments = array( 
  40. "action" => "bfln_get_notification",  
  41. "time" => (int)$time,  
  42. "nonce" => wp_create_nonce( "bfln_main" ),  
  43. "doctitle" => "",  
  44. "newdoctitle" => "",  
  45. "newnotification" => 0  
  46. ); 
  47.  
  48. wp_localize_script( "bfln_main", "BFLN_", $arguments ); 
  49.  
  50. function ajax_get_notifications() { 
  51. check_ajax_referer( 'bfln_main', 'nonce' ); 
  52. $retVal = array( 
  53. "count" => 0,  
  54. "notifications" => "" 
  55. ); 
  56.  
  57. if ( !is_user_logged_in() ) { 
  58. echo json_encode($retVal); 
  59. exit(); 
  60.  
  61. $user_id = bp_loggedin_user_id(); 
  62. $notifictions = bp_core_get_notifications_for_user( $user_id , 'object'); 
  63. if( $notifictions && !empty( $notifictions ) ) { 
  64. $retVal["count"] = count($notifictions); 
  65. $retVal["notifications"] = $notifictions; 
  66. echo json_encode($retVal); 
  67. exit(); 
  1. class BP_FB_like_notification { 
  2. private static $instance; 
  3.  
  4. public static function get_instance() { 
  5. if(!isset(self::$instance)) { 
  6. self::$instance=new self(); 
  7. return self::$instance; 
  8.  
  9. function __construct() { 
  10. $load_plugin = false; 
  11. if( is_user_logged_in() ) { 
  12. $load_plugin = true; 
  13.  
  14. /** The plugin loads another javascript file(one more HTTP request), and causes multiple ajax requests to server. 
  15. * You can use the below filter to conditionaly load the plugin. 
  16. E.g: load the plugin based on user roles!! 
  17. just return false, from your hooked function and the plugin wouldn't be loaded 
  18. */ 
  19. $load_plugin = apply_filters( 'bfln_load_plugin', $load_plugin ); 
  20.  
  21. if( $load_plugin ) { 
  22. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  23. add_action( 'wp_ajax_bfln_get_notification', array( $this, 'ajax_get_notifications' ) );  
  24.  
  25. function enqueue_scripts() { 
  26. wp_enqueue_script( 
  27. "bfln_main",  
  28. path_join( WP_PLUGIN_URL, basename( dirname( __FILE__ ) )."/bp-fb-like-notification_v4.js" ),  
  29. array( 'jquery' ) 
  30. ); 
  31.  
  32. /** 
  33. The time interval : after each interval an ajax request to check for new notification is triggered. 
  34. Default is 2 minutes : 2*60*1000: in milliseconds 
  35. If you want change 2 minutes to something else, use the filter 
  36. */ 
  37. $time = apply_filters( 'bfln_ajax_interval', 2*60*1000 ); 
  38.  
  39. $arguments = array( 
  40. "action" => "bfln_get_notification",  
  41. "time" => (int)$time,  
  42. "nonce" => wp_create_nonce( "bfln_main" ),  
  43. "doctitle" => "",  
  44. "newdoctitle" => "",  
  45. "newnotification" => 0  
  46. ); 
  47.  
  48. wp_localize_script( "bfln_main", "BFLN_", $arguments ); 
  49.  
  50. function ajax_get_notifications() { 
  51. check_ajax_referer( 'bfln_main', 'nonce' ); 
  52. $retVal = array( 
  53. "count" => 0,  
  54. "notifications" => "" 
  55. ); 
  56.  
  57. if ( !is_user_logged_in() ) { 
  58. echo json_encode($retVal); 
  59. exit(); 
  60.  
  61. $user_id = bp_loggedin_user_id(); 
  62. $notifictions = bp_core_get_notifications_for_user( $user_id , 'object'); 
  63. if( $notifictions && !empty( $notifictions ) ) { 
  64. $retVal["count"] = count($notifictions); 
  65. $retVal["notifications"] = $notifictions; 
  66. echo json_encode($retVal); 
  67. exit();