/bp-live-notification.php

  1. <?php 
  2.  
  3. /** Plugin Name: BuddyPress Live Notification 
  4. * Plugin URI: https://buddydev.com/plugins/buddypress-live-notification/ 
  5. * Version: 2.0.2 
  6. * Description: Adds a Facebook Like realtime notification for user on a BuddyPress based social network 
  7. * Author: BuddyDev 
  8. * Author URI: https://buddydev.com 
  9. * License: GPL 
  10. * 
  11. * */ 
  12.  
  13.  
  14. class BP_Live_Notification_Helper { 
  15. /** 
  16. * 
  17. * @var BP_Live_Notification_Helper 
  18. */ 
  19. private static $instance; 
  20.  
  21. private $url; 
  22. private $path; 
  23.  
  24. private function __construct() { 
  25.  
  26. $this->url = plugin_dir_url( __FILE__ ); 
  27. $this->path = plugin_dir_path( __FILE__ ); 
  28.  
  29. add_action( 'bp_include', array( $this, 'load' ) ); 
  30.  
  31. add_action( 'bp_loaded', array( $this, 'load_textdomain' ) ); 
  32.  
  33. add_action( 'bp_enqueue_scripts', array( $this, 'load_css' ) ); 
  34. add_action( 'bp_enqueue_scripts', array( $this, 'load_js' ) ); 
  35.  
  36. add_action( 'wp_head', array( $this, 'add_js_global' ) ); 
  37.  
  38.  
  39. /** 
  40. * 
  41. * @return BP_Live_Notification_Helper 
  42. */ 
  43. public static function get_instance() { 
  44.  
  45. if ( ! isset( self::$instance ) ) { 
  46. self::$instance = new self(); 
  47.  
  48. return self::$instance; 
  49.  
  50.  
  51.  
  52. /** 
  53. * Load core files 
  54. * 
  55. */ 
  56. public function load() { 
  57.  
  58. if ( ! $this->is_active() ) { 
  59. return; 
  60.  
  61.  
  62. $files = array( 
  63. 'functions.php',  
  64. 'ajax.php',  
  65. ); 
  66.  
  67.  
  68. foreach ( $files as $file ) { 
  69. require_once $this->path . $file; 
  70.  
  71. /** 
  72. * Load translation file 
  73. * 
  74. */ 
  75. public function load_textdomain() { 
  76. load_plugin_textdomain( 'bp-live-notification', false, dirname( plugin_basename(__FILE__ ) ) .'/languages' ); 
  77.  
  78. public function get_js_settings() { 
  79.  
  80. return apply_filters( 'bpln_get_js_settings', array( 
  81. 'timeout' => 10,  
  82. //timeou in 10 seconds 
  83. 'last_notified' => bpln_get_latest_notification_id(),  
  84. //please do not change last_notified as we use it to filter the new notifications 
  85. ) ); 
  86.  
  87. /** 
  88. * Load required js 
  89. * 
  90. */ 
  91. public function load_js() { 
  92.  
  93. if ( ! $this->is_active() ) { 
  94. return; 
  95.  
  96. if ( ! is_user_logged_in() || is_admin() && bpln_disable_in_dashboard() ) { 
  97. return; 
  98.  
  99. wp_register_script( 'achtung_js', $this->url . 'assets/vendors/achtung/ui.achtung.min.js', array( 'jquery' ) ); 
  100.  
  101. wp_register_script( 'bpln_js', $this->url . 'assets/js/bpln.js', array( 'jquery', 'json2', 'heartbeat' ) ); 
  102.  
  103. wp_enqueue_script( 'achtung_js' ); 
  104. wp_enqueue_script( 'bpln_js' );//I am not adding achtung_js as a dependency to avoid the condition when the achtung_js will be replaced by some other library and bpln_js won't load  
  105.  
  106. /** 
  107. * Load CSS file 
  108. * 
  109. */ 
  110. public function load_css() { 
  111.  
  112. if ( ! $this->is_active() ) { 
  113. return; 
  114.  
  115. if ( ! is_user_logged_in() || is_admin() && bpln_disable_in_dashboard() ) { 
  116. return; 
  117.  
  118. wp_register_style( 'achtung_css', $this->url . 'assets/vendors/achtung/ui.achtung.css' ); 
  119. wp_enqueue_style( 'achtung_css' ); 
  120.  
  121.  
  122. /** 
  123. * Add global bpln object 
  124. */ 
  125. public function add_js_global() { 
  126. ?> 
  127. <script type="text/javascript"> 
  128. var bpln = <?php echo json_encode( $this->get_js_settings() );?>; 
  129. </script> 
  130.  
  131. <?php 
  132.  
  133.  
  134. public function is_active() { 
  135.  
  136. if ( bp_is_active( 'notifications' ) ) { 
  137. return true; 
  138.  
  139. return false; 
  140.  
  141.  
  142.  
  143. BP_Live_Notification_Helper::get_instance(); 
.