PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.3
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.3
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / assets / lib / class_notices.php

class_notices.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.3, at assets/lib/class_notices.php

343 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Store first activation time. Existing installs are backdated past the 7-day marketing delay.
4 *
5 * Runs on init priority 1 so it sees a missing wppb_version before upgrades.php writes one.
6 */
7 function wppb_maybe_set_first_activation_time() {
8 if ( get_option( 'wppb_first_activation' ) ) {
9 return;
10 }
11
12 $stamp = get_option( 'wppb_version' ) ? time() - WEEK_IN_SECONDS : time();
13 add_option( 'wppb_first_activation', $stamp );
14 }
15 add_action( 'init', 'wppb_maybe_set_first_activation_time', 1 );
16
17 /**
18 * True when 7 days have passed since first activation.
19 */
20 function wppb_should_show_marketing_notice() {
21 $first_activation = (int) get_option( 'wppb_first_activation', 0 );
22
23 if ( $first_activation <= 0 ) {
24 return false;
25 }
26
27 return ( time() - $first_activation ) >= WEEK_IN_SECONDS;
28 }
29
30 /**
31 * Class that adds a misc notice
32 *
33 * @since v.2.0
34 *
35 * @return void
36 */
37 class WPPB_Add_General_Notices{
38 public $notificationId = '';
39 public $notificationMessage = '';
40 public $notificationClass = '';
41 public $startDate = '';
42 public $endDate = '';
43
44 function __construct( $notificationId, $notificationMessage, $notificationClass = 'updated' , $startDate = '', $endDate = '' ){
45 $this->notificationId = $notificationId;
46 $this->notificationMessage = $notificationMessage;
47 $this->notificationClass = $notificationClass;
48
49 if( !empty( $startDate ) && time() < strtotime( $startDate ) )
50 return;
51
52 if( !empty( $endDate ) && time() > strtotime( $endDate ) )
53 return;
54
55 add_action( 'admin_notices', array( $this, 'add_admin_notice' ) );
56 add_action( 'admin_init', array( $this, 'dismiss_notification' ) );
57 }
58
59
60 // Display a notice that can be dismissed in case the serial number is inactive
61 function add_admin_notice() {
62 global $current_user ;
63 global $pagenow;
64
65 $user_id = $current_user->ID;
66 do_action( $this->notificationId.'_before_notification_displayed', $current_user, $pagenow );
67
68 if ( current_user_can( 'manage_options' ) ){
69 // Check that the user hasn't already clicked to ignore the message
70 if ( ! get_user_meta($user_id, $this->notificationId.'_dismiss_notification' ) ) {
71 echo wp_kses_post( apply_filters($this->notificationId.'_notification_message','<div class="'. $this->notificationClass .'" style="position:relative">'.$this->notificationMessage.'</div>', $this->notificationMessage) );
72 }
73 do_action( $this->notificationId.'_notification_displayed', $current_user, $pagenow );
74 }
75 do_action( $this->notificationId.'_after_notification_displayed', $current_user, $pagenow );
76 }
77
78 function dismiss_notification() {
79
80 if( empty( $_GET['_wpnonce'] ) || !wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), 'wppb_general_notice_dismiss' ) )
81 return;
82
83 global $current_user;
84
85 $user_id = $current_user->ID;
86
87 do_action( $this->notificationId.'_before_notification_dismissed', $current_user );
88
89 // If user clicks to ignore the notice, add that to their user meta
90 if ( isset( $_GET[$this->notificationId.'_dismiss_notification']) && '0' == $_GET[$this->notificationId.'_dismiss_notification'] )
91 add_user_meta( $user_id, $this->notificationId.'_dismiss_notification', 'true', true );
92
93 do_action( $this->notificationId.'_after_notification_dismissed', $current_user );
94
95 }
96 }
97
98 Class WPPB_Plugin_Notifications {
99
100 public $notifications = array();
101 private static $_instance = null;
102 private $prefix = 'wppb';
103 private $menu_slug = 'profile-builder';
104 public $pluginPages = array( 'profile-builder-', 'manage-fields', 'wppb-', 'admin-email-customizer', 'user-email-customizer', 'pb-labels-edit', 'profile-user-profile-picture', 'pbie-import-export' );
105
106 protected function __construct() {
107 add_action( 'admin_init', array( $this, 'dismiss_admin_notifications' ), 200 );
108 add_action( 'admin_init', array( $this, 'add_admin_menu_notification_counts' ), 1000 );
109 add_action( 'admin_init', array( $this, 'remove_other_plugin_notices' ), 1001 );
110 }
111
112
113 function dismiss_admin_notifications() {
114 if( ! empty( $_GET[$this->prefix.'_dismiss_admin_notification'] ) ) {
115
116 if( empty( $_GET['_wpnonce'] ) || !wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), 'wppb_plugin_notice_dismiss' ) )
117 return;
118
119 $notifications = self::get_instance();
120 $notifications->dismiss_notification( sanitize_text_field( $_GET[$this->prefix.'_dismiss_admin_notification'] ) );
121
122 }
123
124 }
125
126 function add_admin_menu_notification_counts() {
127
128 global $menu, $submenu;
129
130 $notifications = WPPB_Plugin_Notifications::get_instance();
131
132 if( ! empty( $menu ) ) {
133 foreach( $menu as $menu_position => $menu_data ) {
134 if( ! empty( $menu_data[2] ) && $menu_data[2] == $this->menu_slug ) {
135 $menu_count = $notifications->get_count_in_menu();
136 if( ! empty( $menu_count ) )
137 $menu[$menu_position][0] .= '<span class="update-plugins '.$this->prefix.'-update-plugins"><span class="plugin-count">' . $menu_count . '</span></span>';
138 }
139 }
140 }
141
142 if( ! empty( $submenu[$this->menu_slug] ) ) {
143 foreach( $submenu[$this->menu_slug] as $menu_position => $menu_data ) {
144 $menu_count = $notifications->get_count_in_submenu( $menu_data[2] );
145
146 if( ! empty( $menu_count ) )
147 $submenu[$this->menu_slug][$menu_position][0] .= '<span class="update-plugins '.$this->prefix.'-update-plugins"><span class="plugin-count">' . $menu_count . '</span></span>';
148 }
149 }
150 }
151
152 /* handle other plugin notifications on our plugin pages */
153 function remove_other_plugin_notices(){
154
155 /* remove all other plugin notifications except our own from the rest of the PB pages */
156 if( $this->is_plugin_page() ) {
157 global $wp_filter;
158 if (!empty($wp_filter['admin_notices'])) {
159 if (!empty($wp_filter['admin_notices']->callbacks)) {
160 foreach ($wp_filter['admin_notices']->callbacks as $priority => $callbacks_level) {
161 if (!empty($callbacks_level)) {
162 foreach ($callbacks_level as $key => $callback) {
163 if( is_array( $callback['function'] ) ){
164 if( is_object($callback['function'][0])) {//object here
165 if (strpos(get_class($callback['function'][0]), 'PMS_') !== 0 && strpos(get_class($callback['function'][0]), 'WPPB_') !== 0 && strpos(get_class($callback['function'][0]), 'TRP_') !== 0 && strpos(get_class($callback['function'][0]), 'WCK_') !== 0) {
166 unset($wp_filter['admin_notices']->callbacks[$priority][$key]);//unset everything that doesn't come from our plugins
167 }
168 }
169 } else if( is_string( $callback['function'] ) ){//it should be a function name
170 if (strpos($callback['function'], 'pms_') !== 0 && strpos($callback['function'], 'wppb_') !== 0 && strpos($callback['function'], 'trp_') !== 0 && strpos($callback['function'], 'wck_') !== 0) {
171 unset($wp_filter['admin_notices']->callbacks[$priority][$key]);//unset everything that doesn't come from our plugins
172 }
173 }
174 }
175 }
176 }
177 }
178 }
179 }
180
181 }
182
183 /**
184 *
185 *
186 */
187 public static function get_instance() {
188 if( is_null( self::$_instance ) )
189 self::$_instance = new WPPB_Plugin_Notifications();
190
191 return self::$_instance;
192 }
193
194
195 /**
196 *
197 *
198 */
199 public function add_notification( $notification_id = '', $notification_message = '', $notification_class = 'update-nag', $count_in_menu = true, $count_in_submenu = array(), $show_in_all_backend = false ) {
200
201 if( empty( $notification_id ) )
202 return;
203
204 if( empty( $notification_message ) )
205 return;
206
207 global $current_user;
208
209 if( get_user_meta( $current_user->ID, $notification_id . '_dismiss_notification' ) )
210 return;
211
212 $this->notifications[$notification_id] = array(
213 'id' => $notification_id,
214 'message' => $notification_message,
215 'class' => $notification_class,
216 'count_in_menu' => $count_in_menu,
217 'count_in_submenu' => $count_in_submenu
218 );
219
220 if( $this->is_plugin_page() || $show_in_all_backend == true ) {
221 new WPPB_Add_General_Notices( $notification_id, $notification_message, $notification_class );
222 }
223
224 }
225
226
227 /**
228 * Feature / promo notices. Hidden for 7 days after a new install.
229 */
230 public function add_marketing_notification( $notification_id = '', $notification_message = '', $notification_class = 'update-nag', $count_in_menu = true, $count_in_submenu = array(), $show_in_all_backend = false ) {
231 if ( ! wppb_should_show_marketing_notice() ) {
232 return;
233 }
234
235 $this->add_notification( $notification_id, $notification_message, $notification_class, $count_in_menu, $count_in_submenu, $show_in_all_backend );
236 }
237
238
239 /**
240 *
241 *
242 */
243 public function get_notifications() {
244 return $this->notifications;
245 }
246
247
248 /**
249 *
250 *
251 */
252 public function get_notification( $notification_id = '' ) {
253
254 if( empty( $notification_id ) )
255 return null;
256
257 $notifications = $this->get_notifications();
258
259 if( ! empty( $notifications[$notification_id] ) )
260 return $notifications[$notification_id];
261 else
262 return null;
263
264 }
265
266
267 /**
268 *
269 *
270 */
271 public function dismiss_notification( $notification_id = '' ) {
272 global $current_user;
273 add_user_meta( $current_user->ID, $notification_id . '_dismiss_notification', 'true', true );
274 }
275
276
277 /**
278 *
279 *
280 */
281 public function get_count_in_menu() {
282 $count = 0;
283
284 foreach( $this->notifications as $notification ) {
285 if( ! empty( $notification['count_in_menu'] ) )
286 $count++;
287 }
288
289 return $count;
290 }
291
292
293 /**
294 *
295 *
296 */
297 public function get_count_in_submenu( $submenu = '' ) {
298
299 if( empty( $submenu ) )
300 return 0;
301
302 $count = 0;
303
304 foreach( $this->notifications as $notification ) {
305 if( empty( $notification['count_in_submenu'] ) )
306 continue;
307
308 if( ! is_array( $notification['count_in_submenu'] ) )
309 continue;
310
311 if( ! in_array( $submenu, $notification['count_in_submenu'] ) )
312 continue;
313
314 $count++;
315 }
316
317 return $count;
318
319 }
320
321
322 /**
323 *
324 *
325 */
326 public function is_plugin_page() {
327 if( !empty( $this->pluginPages ) ){
328 foreach ( $this->pluginPages as $pluginPage ){
329 if( ! empty( $_GET['page'] ) && false !== strpos( sanitize_text_field( $_GET['page'] ), $pluginPage ) )
330 return true;
331
332 if( ! empty( $_GET['post_type'] ) && false !== strpos( sanitize_text_field( $_GET['post_type'] ), $pluginPage ) )
333 return true;
334
335 if( ! empty( $_GET['post'] ) && false !== strpos( get_post_type( (int)$_GET['post'] ), $pluginPage ) )
336 return true;
337 }
338 }
339
340 return false;
341 }
342
343 }