-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbp-live-notification.php
executable file
·184 lines (147 loc) · 3.7 KB
/
bp-live-notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Plugin Name: BuddyPress Live Notification
* Plugin URI: https://buddydev.com/plugins/buddypress-live-notification/
* Version: 2.1.1
* Description: Adds a Facebook Like realtime notification for user on a BuddyPress based social network
* Author: BuddyDev
* Author URI: https://buddydev.com
* License: GPL
*/
// No direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit( 0 );
}
/**
* Main Class.
*/
class BP_Live_Notification_Helper {
/**
* Singleton instance.
*
* @var BP_Live_Notification_Helper
*/
private static $instance;
/**
* Plugin Directory url.
*
* @var string
*/
private $url;
/**
* Plugin director path.
*
* @var string
*/
private $path;
/**
* Constructor.
*/
private function __construct() {
$this->url = plugin_dir_url( __FILE__ );
$this->path = plugin_dir_path( __FILE__ );
add_action( 'bp_include', array( $this, 'load' ) );
add_action( 'bp_loaded', array( $this, 'load_translations' ) );
add_action( 'bp_enqueue_scripts', array( $this, 'load_css' ) );
add_action( 'bp_enqueue_scripts', array( $this, 'load_js' ) );
add_action( 'wp_head', array( $this, 'add_js_global' ) );
}
/**
* Get the singleton.
*
* @return BP_Live_Notification_Helper
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Load core files
*/
public function load() {
if ( ! $this->is_active() ) {
return;
}
$files = array(
'core/bp-live-notifications-functions.php',
'core/bp-live-notifications-ajax-handler.php',
);
foreach ( $files as $file ) {
require_once $this->path . $file;
}
}
/**
* Load translation file
*/
public function load_translations() {
load_plugin_textdomain( 'bp-live-notification', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Get js variables.
*
* @return array
*/
public function get_js_settings() {
return apply_filters( 'bpln_get_js_settings', array(
// timeout in 10 seconds.
'timeout' => 10,
// please do not change last_notified as we use it to filter the new notifications.
'last_notified' => bpln_get_latest_notification_id(),
) );
}
/**
* Load required js
*/
public function load_js() {
if ( ! $this->is_active() ) {
return;
}
if ( ! is_user_logged_in() || is_admin() && bpln_disable_in_dashboard() ) {
return;
}
wp_register_script( 'achtung_js', $this->url . 'assets/vendors/achtung/ui.achtung.min.js', array( 'jquery' ) );
wp_register_script( 'bpln_js', $this->url . 'assets/js/bpln.js', array( 'jquery', 'json2', 'heartbeat' ) );
wp_enqueue_script( 'achtung_js' );
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.
}
/**
* Load CSS file
*/
public function load_css() {
if ( ! $this->is_active() ) {
return;
}
if ( ! is_user_logged_in() || is_admin() && bpln_disable_in_dashboard() ) {
return;
}
wp_register_style( 'achtung_css', $this->url . 'assets/vendors/achtung/ui.achtung.css' );
wp_enqueue_style( 'achtung_css' );
}
/**
* Add global bpln object
*/
public function add_js_global() {
if ( ! $this->is_active() ) {
return;
}
?>
<script type="text/javascript">
var bpln = <?php echo json_encode( $this->get_js_settings() );?>;
</script>
<?php
}
/**
* Is BuddyPress Notifications active.
*
* @return bool
*/
public function is_active() {
if ( function_exists( 'bp_is_active' ) && bp_is_active( 'notifications' ) ) {
return true;
}
return false;
}
}
BP_Live_Notification_Helper::get_instance();