PluginProbe
Front End PM / 7.2
Front End PM v7.2
trunk 1.1 1.2 1.3 10.1.1 10.1.2 10.1.3 10.1.4 10.1.5 10.1.6 10.1.7 10.2.1 11.1.1 11.2.1 11.2.2 11.2.3 11.3.1 11.3.3 11.3.4 11.3.5 11.3.6 11.3.7 11.3.8 11.3.9 11.4.1 All 58 releases
front-end-pm / functions.php

functions.php in Front End PM 7.2, at functions.php

1,988 lines 59.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 function fep_plugin_activate(){
8
9 _deprecated_function( __FUNCTION__, '4.4' );
10 //Deprecated in 4.4
11 //Move inside Front_End_Pm class
12
13 }
14
15 function fep_get_option( $option, $default = '', $section = 'FEP_admin_options' ) {
16
17 $options = get_option( $section );
18
19 if( ! is_array( $options ) )
20 $options = array();
21
22 $is_default = false;
23
24 if ( isset( $options[$option] ) ) {
25 $value = $options[$option];
26 } else {
27 $value = $default;
28 $is_default = true;
29 }
30
31 return apply_filters('fep_get_option', $value, $option, $default, $is_default );
32 }
33
34 function fep_update_option( $option, $value = '', $section = 'FEP_admin_options' ) {
35
36 if( empty( $option ) )
37 return false;
38 if( ! is_array( $option ) )
39 $option = array( $option => $value );
40
41 $options = get_option( $section );
42
43 if( ! is_array( $options ) )
44 $options = array();
45
46 return update_option( $section, wp_parse_args( $option, $options ) );
47 }
48
49 function fep_get_user_option( $option, $default = '', $userid = '', $section = 'FEP_user_options' ) {
50
51 $options = get_user_option( $section, $userid ); //if $userid = '' current user option will be return
52
53 $is_default = false;
54
55 if ( isset( $options[$option] ) ) {
56 $value = $options[$option];
57 } else {
58 $value = $default;
59 $is_default = true;
60 }
61
62 return apply_filters('fep_get_user_option', $value, $option, $default, $userid, $is_default );
63 }
64
65 function fep_update_user_option( $option, $value = '', $userid = '', $section = 'FEP_user_options' ) {
66
67 if( empty( $option ) )
68 return false;
69
70 if( ! is_array( $option ) )
71 $option = array( $option => $value );
72
73 if( ! $userid )
74 $userid = get_current_user_id();
75
76 $options = get_user_option( $section, $userid );
77
78 if( ! is_array( $options ) )
79 $options = array();
80
81 return update_user_option( $userid, $section, wp_parse_args( $option, $options ) );
82 }
83
84 if ( !function_exists('fep_get_plugin_caps') ) :
85
86 function fep_get_plugin_caps( $edit_published = false, $for = 'both' ){
87 $message_caps = array(
88 'delete_published_fep_messages' => 1,
89 'delete_private_fep_messages' => 1,
90 'delete_others_fep_messages' => 1,
91 'delete_fep_messages' => 1,
92 'publish_fep_messages' => 1,
93 'read_private_fep_messages' => 1,
94 'edit_private_fep_messages' => 1,
95 'edit_others_fep_messages' => 1,
96 'edit_fep_messages' => 1,
97 'create_fep_messages' => 1,
98 );
99
100 $announcement_caps = array(
101 'delete_published_fep_announcements' => 1,
102 'delete_private_fep_announcements' => 1,
103 'delete_others_fep_announcements' => 1,
104 'delete_fep_announcements' => 1,
105 'publish_fep_announcements' => 1,
106 'read_private_fep_announcements' => 1,
107 'edit_private_fep_announcements' => 1,
108 'edit_others_fep_announcements' => 1,
109 'edit_fep_announcements' => 1,
110 'create_fep_announcements' => 1,
111 );
112
113 if( 'fep_message' == $for ) {
114 $caps = $message_caps;
115 if( $edit_published ) {
116 $caps['edit_published_fep_messages'] = 1;
117 }
118 } elseif( 'fep_announcement' == $for ){
119 $caps = $announcement_caps;
120 if( $edit_published ) {
121 $caps['edit_published_fep_announcements'] = 1;
122 }
123 } else {
124 $caps = array_merge( $message_caps, $announcement_caps );
125 if( $edit_published ) {
126 $caps['edit_published_fep_messages'] = 1;
127 $caps['edit_published_fep_announcements'] = 1;
128 }
129 }
130 return $caps;
131 }
132
133 endif;
134
135 if ( !function_exists('fep_add_caps_to_roles') ) :
136
137 function fep_add_caps_to_roles( $roles = array( 'administrator', 'editor' ), $edit_published = true ) {
138
139 if( ! is_array( $roles ) )
140 $roles = array();
141
142 $caps = fep_get_plugin_caps( $edit_published );
143
144 foreach( $roles as $role ) {
145 $role_obj = get_role( $role );
146 if( !$role_obj )
147 continue;
148
149 foreach( $caps as $cap => $val ) {
150 if( $val )
151 $role_obj->add_cap( $cap );
152 }
153 }
154 }
155
156 endif;
157
158 if ( defined( 'WP_UNINSTALL_PLUGIN' ) ) return;
159
160 add_action('after_setup_theme', 'fep_include_require_files');
161
162 function fep_include_require_files() {
163
164 $fep_files = array(
165 'announcement' => FEP_PLUGIN_DIR. 'includes/class-fep-announcement.php',
166 'attachment' => FEP_PLUGIN_DIR. 'includes/class-fep-attachment.php',
167 'cpt' => FEP_PLUGIN_DIR. 'includes/class-fep-cpt.php',
168 'directory' => FEP_PLUGIN_DIR. 'includes/class-fep-directory.php',
169 'email' => FEP_PLUGIN_DIR. 'includes/class-fep-emails.php',
170 'form' => FEP_PLUGIN_DIR. 'includes/class-fep-form.php',
171 'menu' => FEP_PLUGIN_DIR. 'includes/class-fep-menu.php',
172 'message' => FEP_PLUGIN_DIR. 'includes/class-fep-message.php',
173 'shortcodes' => FEP_PLUGIN_DIR. 'includes/class-fep-shortcodes.php',
174 'user-settings' => FEP_PLUGIN_DIR. 'includes/class-fep-user-settings.php',
175 'main' => FEP_PLUGIN_DIR. 'includes/fep-class.php',
176 'widgets' => FEP_PLUGIN_DIR. 'includes/fep-widgets.php'
177 );
178 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
179 $fep_files['ajax'] = FEP_PLUGIN_DIR. 'includes/class-fep-ajax.php';
180 }
181
182 if( is_admin() ) {
183 $fep_files['settings'] = FEP_PLUGIN_DIR. 'admin/class-fep-admin-settings.php';
184 $fep_files['update'] = FEP_PLUGIN_DIR. 'admin/class-fep-update.php';
185 $fep_files['pro-info'] = FEP_PLUGIN_DIR. 'admin/class-fep-pro-info.php';
186 }
187
188 $fep_files = apply_filters('fep_include_files', $fep_files );
189
190 foreach ( $fep_files as $fep_file ) {
191 require_once( $fep_file );
192 }
193 }
194
195 function fep_plugin_update(){
196
197 _deprecated_function( __FUNCTION__, '4.9', 'Fep_Update class' );
198
199 $prev_ver = fep_get_option( 'plugin_version', '4.1' );
200
201 if( version_compare( $prev_ver, FEP_PLUGIN_VERSION, '!=' ) ) {
202
203 do_action( 'fep_plugin_update', $prev_ver );
204
205 fep_update_option( 'plugin_version', FEP_PLUGIN_VERSION );
206 }
207
208 }
209
210 function fep_plugin_update_from_first( $prev_ver ){
211
212 if( is_admin() && '4.1' == $prev_ver ) { //any previous version of 4.1 also return 4.1
213 fep_plugin_activate();
214 }
215
216 }
217 add_action( 'fep_plugin_update', 'fep_plugin_update_from_first' );
218
219 add_action('after_setup_theme', 'fep_translation');
220
221 function fep_translation()
222 {
223 //SETUP TEXT DOMAIN FOR TRANSLATIONS
224 load_plugin_textdomain('front-end-pm', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
225 }
226
227 add_action('wp_enqueue_scripts', 'fep_enqueue_scripts');
228
229 function fep_enqueue_scripts()
230 {
231
232 wp_register_style( 'fep-common-style', FEP_PLUGIN_URL . 'assets/css/common-style.css', array(), '7.1' );
233 wp_register_style( 'fep-style', FEP_PLUGIN_URL . 'assets/css/style.css', array(), '7.1' );
234 wp_register_style( 'fep-tokeninput-style', FEP_PLUGIN_URL . 'assets/css/token-input-facebook.css' );
235
236 if( fep_page_id() ) {
237 if( is_page( fep_page_id() ) || is_single( fep_page_id() ) ) {
238 wp_enqueue_style( 'fep-style' );
239 }
240 } else {
241 wp_enqueue_style( 'fep-style' );
242 }
243 wp_enqueue_style( 'fep-common-style' );
244
245 $custom_css = '#fep-wrapper{';
246 $custom_css .= 'background-color:' . fep_get_option('bg_color') . ';';
247 $custom_css .= 'color:' . fep_get_option('text_color','#000000') . ';';
248 $custom_css .= '}';
249 $custom_css .= '#fep-wrapper a:not(.fep-button,.fep-button-active){color:' . fep_get_option('link_color','#000080') . ';}';
250 $custom_css .= '.fep-button{';
251 $custom_css .= 'background-color:' . fep_get_option('btn_bg_color', '#F0FCFF') . ';';
252 $custom_css .= 'color:' . fep_get_option('btn_text_color','#000000') . ';';
253 $custom_css .= '}';
254 $custom_css .= '.fep-button:hover,.fep-button-active{';
255 $custom_css .= 'background-color:' . fep_get_option('active_btn_bg_color','#D3EEF5') . ';';
256 $custom_css .= 'color:' . fep_get_option('active_btn_text_color','#000000') . ';';
257 $custom_css .= '}';
258 $custom_css .= '.fep-odd-even > div:nth-child(odd){background-color:' . fep_get_option('odd_color','#F2F7FC') . ';}';
259 $custom_css .= '.fep-odd-even > div:nth-child(even){background-color:' . fep_get_option('even_color','#FAFAFA') . ';}';
260 $custom_css .= '.fep-message .fep-message-title-heading, .fep-per-message .fep-message-title{background-color:' . fep_get_option('mgs_heading_color','#F2F7FC') . ';}';
261 $custom_css .= trim( stripslashes(fep_get_option('custom_css') ) );
262 if( $custom_css ) {
263 wp_add_inline_style( 'fep-common-style', $custom_css );
264 }
265
266 wp_register_script( 'fep-script', FEP_PLUGIN_URL . 'assets/js/script.js', array( 'jquery' ), '3.1', true );
267 wp_localize_script( 'fep-script', 'fep_script',
268 array(
269 'ajaxurl' => admin_url( 'admin-ajax.php' ),
270 'nonce' => wp_create_nonce('fep-autosuggestion')
271 )
272 );
273
274 wp_register_script( 'fep-notification-script', FEP_PLUGIN_URL . 'assets/js/notification.js', array( 'jquery' ), '7.2', true );
275 $call_on_ready = ( isset($_GET['fepaction']) &&
276 ( ( $_GET['fepaction'] == 'viewmessage' && fep_get_new_message_number() ) || ( $_GET['fepaction'] == 'view_announcement' && fep_get_new_announcement_number() ) )
277 ) ? '1' : '0';
278 wp_localize_script( 'fep-notification-script', 'fep_notification_script',
279 apply_filters( 'fep_filter_notification_script_localize', array(
280 'ajaxurl' => admin_url( 'admin-ajax.php' ),
281 'nonce' => wp_create_nonce('fep-notification'),
282 'interval' => apply_filters( 'fep_filter_ajax_notification_interval', MINUTE_IN_SECONDS * 1000 ),
283 'skip' => apply_filters( 'fep_filter_skip_notification_call', 2 ), //How many times notification ajax call will be skipped if browser tab not opened
284 'show_in_title' => fep_get_option( 'show_unread_count_in_title', '1' ),
285 'show_in_desktop' => fep_get_option( 'show_unread_count_in_desktop', '1' ),
286 'call_on_ready' => apply_filters( 'fep_filter_notification_call_on_ready', $call_on_ready ),
287 'play_sound' => fep_get_option( 'play_sound', '1' ),
288 'sound_url' => FEP_PLUGIN_URL . 'assets/audio/plucky.mp3',
289 'icon_url' => FEP_PLUGIN_URL . 'assets/images/desktop-notification-32.png',
290 'mgs_notification_title'=> __('New Message.', 'front-end-pm'),
291 'mgs_notification_body' => __('You have received a new message.', 'front-end-pm'),
292 'mgs_notification_url' => fep_query_url( 'messagebox' ),
293 'ann_notification_title'=> __('New Announcement.', 'front-end-pm'),
294 'ann_notification_body' => __('You have received a new announcement.', 'front-end-pm'),
295 'ann_notification_url' => fep_query_url( 'announcements' ),
296 ))
297 );
298
299
300 wp_register_script( 'fep-replies-show-hide', FEP_PLUGIN_URL . 'assets/js/replies-show-hide.js', array( 'jquery' ), '3.1', true );
301
302 wp_register_script( 'fep-attachment-script', FEP_PLUGIN_URL . 'assets/js/attachment.js', array( 'jquery' ), '6.1', true );
303 wp_localize_script( 'fep-attachment-script', 'fep_attachment_script',
304 array(
305 'remove' => esc_js(__('Remove', 'front-end-pm')),
306 'maximum' => esc_js( fep_get_option('attachment_no', 4 ) ),
307 'max_text' => esc_js( sprintf( __( 'Maximum %s allowed', 'front-end-pm' ), sprintf(_n('%s file', '%s files', fep_get_option('attachment_no', 4 ), 'front-end-pm'), number_format_i18n(fep_get_option('attachment_no', 4 )) ) ) )
308
309 )
310 );
311 wp_register_script( 'fep-shortcode-newmessage', FEP_PLUGIN_URL . 'assets/js/shortcode-newmessage.js', array( 'jquery' ), '6.1', true );
312 wp_localize_script( 'fep-shortcode-newmessage', 'fep_shortcode_newmessage',
313 array(
314 'ajaxurl' => admin_url( 'admin-ajax.php' ),
315 'token' => wp_create_nonce('fep_message'),
316 'refresh_text' => __('Refresh this page and try again.', 'front-end-pm'),
317 )
318 );
319 wp_register_script( 'fep-tokeninput-script', FEP_PLUGIN_URL . 'assets/js/jquery.tokeninput.js', array( 'jquery' ), '6.1', true );
320 wp_register_script( 'fep-block-unblock-script', FEP_PLUGIN_URL . 'assets/js/block-unblock.js', array( 'jquery' ), '6.1', true );
321 wp_localize_script( 'fep-block-unblock-script', 'fep_block_unblock_script',
322 array(
323 'ajaxurl' => admin_url( 'admin-ajax.php' ),
324 'token' => wp_create_nonce('fep-block-unblock-script')
325 )
326 );
327 }
328
329 function fep_page_id() {
330
331 return (int) apply_filters( 'fep_page_id_filter', fep_get_option('page_id', 0 ) );
332 }
333
334 function fep_action_url( $action = '', $arg = array() ) {
335
336 return fep_query_url( $action, $arg );
337 }
338
339 function fep_query_url( $action, $arg = array() ) {
340 $args = array( 'fepaction' => $action );
341 $args = array_merge( $args, $arg );
342 $url = esc_url( fep_query_url_without_esc( $action, $arg ) );
343
344 return apply_filters( 'fep_query_url_filter', $url, $args );
345 }
346
347 function fep_query_url_without_esc( $action, $arg = array() ) {
348
349 $args = array( 'fepaction' => $action );
350 $args = array_merge( $args, $arg );
351
352 if ( fep_page_id() ) {
353 $url = add_query_arg( $args, get_permalink( fep_page_id() ) );
354 } else {
355 $url = add_query_arg( $args );
356 }
357
358 return apply_filters( 'fep_query_url_without_esc_filter', $url, $args );
359 }
360
361 if ( !function_exists('fep_create_nonce') ) :
362 /**
363 * Creates a token usable in a form
364 * return nonce with time
365 * @return string
366 */
367 function fep_create_nonce($action = -1) {
368 $time = time();
369 $nonce = wp_create_nonce($time.$action);
370 return $nonce . '-' . $time;
371 }
372
373 endif;
374
375 if ( !function_exists('fep_verify_nonce') ) :
376 /**
377 * Check if a token is valid. Mark it as used
378 * @param string $_nonce The token
379 * @return bool
380 */
381 function fep_verify_nonce( $_nonce, $action = -1) {
382
383 //Extract timestamp and nonce part of $_nonce
384 $parts = explode( '-', $_nonce );
385
386 // bad formatted onetime-nonce
387 if ( empty( $parts[0] ) || empty( $parts[1] ) )
388 return false;
389
390 $nonce = $parts[0]; // Original nonce generated by WordPress.
391 $generated = $parts[1]; //Time when generated
392
393 $expire = (int) $generated + HOUR_IN_SECONDS; //We want these nonces to have a short lifespan
394 $time = time();
395
396 //Verify the nonce part and check that it has not expired
397 if( ! wp_verify_nonce( $nonce, $generated.$action ) || $time > $expire )
398 return false;
399
400 //Get used nonces
401 $used_nonces = get_option('_fep_used_nonces');
402
403 if(! is_array( $used_nonces ) )
404 $used_nonces = array();
405
406 //Nonce already used.
407 if( isset( $used_nonces[$nonce] ) )
408 return false;
409
410 foreach ($used_nonces as $nonces => $timestamp){
411 if( $timestamp < $time ){
412 //This nonce has expired, so we don't need to keep it any longer
413 unset( $used_nonces[$nonces] );
414 }
415 }
416
417 //Add nonce to used nonces
418 $used_nonces[$nonce] = $expire;
419 update_option( '_fep_used_nonces', $used_nonces, 'no' );
420 return true;
421 }
422 endif;
423
424 function fep_error($wp_error){
425 if(!is_wp_error($wp_error)){
426 return '';
427 }
428 if(count($wp_error->get_error_messages())==0){
429 return '';
430 }
431 $errors = $wp_error->get_error_messages();
432 if (is_admin())
433 $html = '<div id="message" class="error">';
434 else
435 $html = '<div class="fep-wp-error">';
436 foreach($errors as $error){
437 $html .= '<strong>' . __('Error', 'front-end-pm') . ': </strong>'.esc_html($error).'<br />';
438 }
439 $html .= '</div>';
440 return $html;
441 }
442
443 function fep_get_new_message_number()
444 {
445
446 return fep_get_user_message_count( 'unread' );
447 }
448
449 function fep_get_new_message_button( $args = array() ){
450 if ( ! fep_current_user_can( 'access_message' ) )
451 return '';
452
453 $args = wp_parse_args( $args, array(
454 'show_bracket' => '1',
455 'hide_if_zero' => '1',
456 'ajax' => '1',
457 'class' => 'fep-font-red',
458 ) );
459
460 $new = fep_get_new_message_number();
461
462 if( empty( $args['ajax'] ) ){
463 if( ! $new && $args['hide_if_zero'] ){
464 return '';
465 }
466 $ret = '';
467
468 if( $args['show_bracket'] ){
469 $ret .= '(';
470 }
471 $ret .= '<span class="' . $args['class'] . '">' . $new . '</span>';
472 if( $args['show_bracket'] ){
473 $ret .= ')';
474 }
475
476 return $ret;
477 }
478
479 wp_enqueue_script( 'fep-notification-script' );
480
481 $args['class'] = $args['class'] . ' fep_unread_message_count';
482
483 if( $args['hide_if_zero'] ){
484 $args['class'] = $args['class'] . ' fep_unread_message_count_hide_if_zero';
485 }
486
487 $ret = '';
488
489 if( $args['show_bracket'] && $args['hide_if_zero'] && ! $new ){
490 $ret .= '<span class="fep_unread_message_count_hide_if_zero fep-hide">(</span>';
491 } elseif( $args['show_bracket'] && $args['hide_if_zero'] ){
492 $ret .= '<span class="fep_unread_message_count_hide_if_zero">(</span>';
493 } elseif( $args['show_bracket'] ){
494 $ret .= '(';
495 }
496 if( ! $new && $args['hide_if_zero'] ){
497 $args['class'] = $args['class'] . ' fep-hide';
498 }
499 $ret .= '<span class="' . $args['class'] . '">' . $new . '</span>';
500
501 if( $args['show_bracket'] && $args['hide_if_zero'] && ! $new ){
502 $ret .= '<span class="fep_unread_message_count_hide_if_zero fep-hide">)</span>';
503 } elseif( $args['show_bracket'] && $args['hide_if_zero'] ){
504 $ret .= '<span class="fep_unread_message_count_hide_if_zero">)</span>';
505 } elseif( $args['show_bracket'] ){
506 $ret .= ')';
507 }
508
509 return $ret;
510 }
511
512 function fep_get_new_announcement_number()
513 {
514
515 return fep_get_user_announcement_count( 'unread' );
516 }
517
518 function fep_get_new_announcement_button( $args = array() ){
519 if ( ! fep_current_user_can( 'access_message' ) )
520 return '';
521
522 $args = wp_parse_args( $args, array(
523 'show_bracket' => '1',
524 'hide_if_zero' => '1',
525 'ajax' => '1',
526 'class' => 'fep-font-red',
527 ) );
528
529 $new = fep_get_new_announcement_number();
530
531 if( empty( $args['ajax'] ) ){
532 if( ! $new && $args['hide_if_zero'] ){
533 return '';
534 }
535 $ret = '';
536
537 if( $args['show_bracket'] ){
538 $ret .= '(';
539 }
540 $ret .= '<span class="' . $args['class'] . '">' . $new . '</span>';
541 if( $args['show_bracket'] ){
542 $ret .= ')';
543 }
544
545 return $ret;
546 }
547
548 wp_enqueue_script( 'fep-notification-script' );
549
550 $args['class'] = $args['class'] . ' fep_unread_announcement_count';
551
552 if( $args['hide_if_zero'] ){
553 $args['class'] = $args['class'] . ' fep_unread_announcement_count_hide_if_zero';
554 }
555
556 $ret = '';
557
558 if( $args['show_bracket'] && $args['hide_if_zero'] && ! $new ){
559 $ret .= '<span class="fep_unread_announcement_count_hide_if_zero fep-hide">(</span>';
560 } elseif( $args['show_bracket'] && $args['hide_if_zero'] ){
561 $ret .= '<span class="fep_unread_announcement_count_hide_if_zero">(</span>';
562 } elseif( $args['show_bracket'] ){
563 $ret .= '(';
564 }
565 if( ! $new && $args['hide_if_zero'] ){
566 $args['class'] = $args['class'] . ' fep-hide';
567 }
568 $ret .= '<span class="' . $args['class'] . '">' . $new . '</span>';
569
570 if( $args['show_bracket'] && $args['hide_if_zero'] && ! $new ){
571 $ret .= '<span class="fep_unread_announcement_count_hide_if_zero fep-hide">)</span>';
572 } elseif( $args['show_bracket'] && $args['hide_if_zero'] ){
573 $ret .= '<span class="fep_unread_announcement_count_hide_if_zero">)</span>';
574 } elseif( $args['show_bracket'] ){
575 $ret .= ')';
576 }
577
578 return $ret;
579 }
580
581 function fep_is_user_blocked( $login = '' ){
582 global $user_login;
583 if ( !$login && $user_login )
584 $login = $user_login;
585
586 if ($login){
587 $wpusers = explode(',', fep_get_option('have_permission') );
588
589 $wpusers = array_map( 'trim', $wpusers );
590
591 if( in_array( $login, $wpusers) )
592 return true;
593 } //User not logged in
594 return false;
595 }
596
597 function fep_is_user_whitelisted( $login = '' ){
598 global $user_login;
599 if ( !$login && $user_login )
600 $login = $user_login;
601
602 if ($login){
603 $wpusers = explode(',', fep_get_option('whitelist_username') );
604
605 $wpusers = array_map( 'trim', $wpusers );
606
607 if(in_array( $login, $wpusers))
608 return true;
609 } //User not logged in
610 return false;
611 }
612
613 function fep_get_userdata($data, $need = 'ID', $type = 'slug' )
614 {
615 if (!$data)
616 return '';
617
618 $type = strtolower($type);
619
620 if( 'user_nicename' == $type )
621 $type = 'slug';
622
623 if ( !in_array($type, array ('id', 'slug', 'email', 'login' )))
624 return '';
625
626 $user = get_user_by( $type , $data);
627
628 if ( $user && in_array($need, array('ID', 'user_login', 'display_name', 'user_email', 'user_nicename', 'user_registered' )))
629 return $user->$need;
630 else
631 return '';
632 }
633
634 function fep_get_user_message_count( $value = 'all', $force = false, $user_id = false )
635 {
636 return Fep_Message::init()->user_message_count( $value, $force, $user_id );
637 }
638
639 function fep_get_user_announcement_count( $value = 'all', $force = false, $user_id = false )
640 {
641 return Fep_Announcement::init()->get_user_announcement_count( $value, $force, $user_id );
642 }
643
644 function fep_get_message( $id )
645 {
646 $post = get_post( $id );
647
648 if( $post && in_array( get_post_type( $post ), array( 'fep_message', 'fep_announcement') ) ){
649 return $post;
650 } else {
651 return null;
652 }
653
654 }
655
656 function fep_get_replies( $id )
657 {
658 $args = array(
659 'post_type' => 'fep_message',
660 'post_status' => 'publish',
661 'post_parent' => $id,
662 'posts_per_page' => -1,
663 'order'=> 'ASC'
664 );
665
666 $args = apply_filters( 'fep_filter_get_replies', $args );
667
668 return new WP_Query( $args );
669 }
670
671 function fep_get_attachments( $post_id = 0, $fields = '' ) {
672
673 if( ! $post_id ) {
674 $post_id = get_the_ID();
675 }
676
677 if( ! $post_id ) {
678 return array();
679 }
680 $args = array(
681 'post_type' => 'attachment',
682 'posts_per_page' => -1,
683 'post_status' => array( 'publish', 'inherit' ),
684 'post_parent' => $post_id,
685 'fields' => $fields,
686 );
687
688 $args = apply_filters( 'fep_filter_get_attachments', $args );
689
690 return get_posts( $args );
691 }
692
693 function fep_get_message_with_replies( $id )
694 {
695
696 $args = array(
697 'post_type' => 'fep_message',
698 'post_status' => 'publish',
699 'posts_per_page' => -1,
700 'order'=> 'ASC'
701 );
702
703 if( 'threaded' == fep_get_message_view() ) {
704 $args['post_parent'] = fep_get_parent_id( $id );
705 $args['fep_include_parent'] = true;
706 } else {
707 $args['post__in'] = array( $id );
708 }
709
710 $args = apply_filters( 'fep_filter_get_message_with_replies', $args );
711
712 return new WP_Query( $args );
713 }
714
715 add_filter( 'posts_where' , 'fep_posts_where', 10, 2 );
716
717 function fep_posts_where( $where, $q ) {
718
719 global $wpdb;
720
721 if ( true === $q->get( 'fep_include_parent' ) && $q->get( 'post_parent' ) ){
722 $where .= $wpdb->prepare( " OR ( $wpdb->posts.ID = %d AND $wpdb->posts.post_status = %s )", $q->get( 'post_parent' ), $q->get( 'post_status' ) );
723 }
724
725 return $where;
726 }
727
728 function fep_get_parent_id( $id ) {
729
730 if( ! $id )
731 return 0;
732
733 do {
734 $parent = $id;
735 } while( $id = wp_get_post_parent_id( $id ) );
736 // climb up the hierarchy until we reach parent = 0
737
738 return $parent;
739
740 }
741
742 add_filter( 'the_time', 'fep_format_date', 10, 2 ) ;
743
744 function fep_format_date( $date, $d = '' )
745 {
746 global $post;
747
748 if( is_admin() || ! in_array( get_post_type(), apply_filters( 'fep_post_types_for_time', array( 'fep_message', 'fep_announcement' ) ) ) )
749 return $date;
750
751
752 if ( '0000-00-00 00:00:00' === $post->post_date ) {
753 $h_time = __( 'Unpublished', 'front-end-pm' );
754 } else {
755 $m_time = $post->post_date;
756 //$time = strtotime( $post->post_date_gmt );
757 $time = get_post_time( 'G', true, $post, false );
758
759 if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
760 if ( $t_diff < 0 ) {
761 $h_time = sprintf( __( '%s from now', 'front-end-pm' ), human_time_diff( $time ) );
762 } else {
763 $h_time = sprintf( __( '%s ago', 'front-end-pm' ), human_time_diff( $time ) );
764 }
765 } else {
766 $h_time = mysql2date( get_option( 'date_format' ). ' '.get_option( 'time_format' ), $m_time );
767 }
768 }
769
770
771 return apply_filters( 'fep_formate_date', $h_time, $date, $d );
772 }
773
774 function fep_output_filter($string, $title = false)
775 {
776 $string = stripslashes($string);
777
778 if ($title) {
779 $html = apply_filters('fep_filter_display_title', $string);
780 } else {
781 $html = apply_filters('fep_filter_display_message', $string);
782 }
783
784 return $html;
785 }
786
787 function fep_sort_by_priority( $a, $b ) {
788 if ( ! isset( $a['priority'] ) || ! isset( $b['priority'] ) || $a['priority'] === $b['priority'] ) {
789 return 0;
790 }
791 return ( $a['priority'] < $b['priority'] ) ? -1 : 1;
792 }
793
794
795 function fep_pagination( $total = null, $per_page = null, $list_class = 'fep-pagination' ) {
796
797 $total = apply_filters( 'fep_pagination_total', $total);
798
799 if( null === $per_page ) {
800 $per_page = fep_get_option('messages_page',15);
801 }
802 $per_page = apply_filters( 'fep_pagination_per_page', $per_page);
803
804 $last = ceil( absint($total) / absint($per_page) );
805
806 if( $last <= 1 )
807 return '';
808
809 //$numPgs = $total_message / fep_get_option('messages_page',50);
810 $page = ( ! empty( $_GET['feppage'] )) ? absint($_GET['feppage']) : 1;
811 $links = ( isset( $_GET['links'] ) ) ? absint($_GET['links']) : 2;
812
813 $start = ( ( $page - $links ) > 0 ) ? $page - $links : 1;
814 $end = ( ( $page + $links ) < $last ) ? $page + $links : $last;
815
816 $html = '<div class="fep-align-centre"><ul class="' . $list_class . '">';
817
818 $class = ( $page == 1 ) ? "disabled" : "";
819 $html .= '<li class="' . $class . '"><a href="' . esc_url( add_query_arg( 'feppage', ( $page - 1 ) ) ) . '">&laquo;</a></li>';
820
821 if ( $start > 1 ) {
822 $html .= '<li><a href="' . esc_url( add_query_arg( 'feppage', 1 ) ) . '">' . number_format_i18n( 1 ) . '</a></li>';
823 $html .= '<li class="disabled"><span>...</span></li>';
824 }
825
826 for ( $i = $start ; $i <= $end; $i++ ) {
827 $class = ( $page == $i ) ? "active" : "";
828 $html .= '<li class="' . $class . '"><a href="' . esc_url( add_query_arg( 'feppage', $i ) ) . '">' . number_format_i18n( $i ) . '</a></li>';
829 }
830
831 if ( $end < $last ) {
832 $html .= '<li class="disabled"><span>...</span></li>';
833 $html .= '<li><a href="' . esc_url( add_query_arg( 'feppage', $last ) ) . '">' . number_format_i18n( $last ) . '</a></li>';
834 }
835
836 $class = ( $page == $last ) ? "disabled" : "";
837 $html .= '<li class="' . $class . '"><a href="' . esc_url( add_query_arg( 'feppage', ( $page + 1 ) ) ) . '">&raquo;</a></li>';
838
839 $html .= '</ul></div>';
840
841 return $html;
842 }
843
844 function fep_is_user_admin( $user_id = 0 ){
845
846 $admin_cap = apply_filters( 'fep_admin_cap', 'manage_options' );
847 if( $user_id ){
848 return user_can( $user_id, $admin_cap );
849 }
850 return current_user_can( $admin_cap );
851 }
852
853 function fep_current_user_can( $cap, $id = false ) {
854 $can = false;
855
856 if( ! is_user_logged_in() || fep_is_user_blocked() ) {
857 return apply_filters( 'fep_current_user_can', $can, $cap, $id );
858 }
859 $no_role_access = apply_filters( 'fep_no_role_access', false, $cap, $id );
860 $roles = wp_get_current_user()->roles;
861
862 switch( $cap ) {
863 case 'access_message':
864 if( fep_is_user_whitelisted() || array_intersect( fep_get_option('userrole_access', array() ), $roles ) || ( ! $roles && $no_role_access ) ){
865 $can = true;
866 }
867 break;
868 case 'send_new_message' :
869 if( fep_is_user_whitelisted() || array_intersect( fep_get_option('userrole_new_message', array() ), $roles ) || ( ! $roles && $no_role_access ) ){
870 $can = true;
871 }
872 break;
873 case 'send_new_message_to' :
874 if( is_numeric( $id ) ){
875 // $id == user ID
876 if ( $id && $id != get_current_user_id() && fep_current_user_can('access_message') && fep_current_user_can('send_new_message') && fep_get_user_option( 'allow_messages', 1, $id ) && ! fep_is_user_blocked_for_user( $id ) ){
877 $can = true;
878 }
879 // $id == user_nicename
880 // Backward compability ( do not use )
881 } elseif ( $id && $id != fep_get_userdata( get_current_user_id(), 'user_nicename', 'id' ) && fep_current_user_can('access_message') && fep_current_user_can('send_new_message') && fep_get_user_option( 'allow_messages', 1, fep_get_userdata( $id ) ) && ! fep_is_user_blocked_for_user( fep_get_userdata( $id ) ) ){
882 $can = true;
883 }
884 break;
885 case 'send_reply' :
886 if( ! $id || ( ! in_array( get_current_user_id(), fep_get_participants( $id ) ) && ! fep_is_user_admin() ) || get_post_status ( $id ) != 'publish' ) {
887
888 } elseif( fep_is_user_whitelisted() || fep_is_user_admin() || array_intersect( fep_get_option('userrole_reply', array() ), $roles ) || ( ! $roles && $no_role_access ) ){
889 $can = true;
890 $participants = fep_get_participants( $id );
891 foreach( $participants as $participant ){
892 if( fep_is_user_blocked_for_user( $participant ) ){
893 $can = false;
894 break;
895 }
896 }
897 }
898 break;
899 case 'view_message' :
900 if( $id && ( ( in_array( get_current_user_id(), fep_get_participants( $id ) ) && get_post_status ( $id ) == 'publish' ) || fep_is_user_admin() )) {
901 $can = true;
902 }
903 break;
904 case 'delete_message' : //only for himself
905 if( $id && in_array( get_current_user_id(), fep_get_participants( $id ) ) && get_post_status ( $id ) == 'publish' ) {
906 $can = true;
907 }
908 break;
909 case 'access_directory' :
910 if( fep_is_user_admin() || fep_get_option('show_directory', 1 ) ) {
911 $can = true;
912 }
913 break;
914 case 'add_announcement' :
915 if( fep_is_user_admin() || current_user_can('create_fep_announcements') ) {
916 $can = true;
917 }
918 break;
919 case 'view_announcement' :
920 if( $id && ( ( ( array_intersect( fep_get_participant_roles( $id ), $roles ) || ( ! $roles && $no_role_access ) ) && get_post_status ( $id ) == 'publish') || fep_is_user_admin() || get_post_field( 'post_author', $id ) == get_current_user_id() ) ) {
921 $can = true;
922 }
923 break;
924 default :
925 $can = apply_filters( 'fep_current_user_can_' . $cap, $can, $cap, $id );
926 break;
927 }
928 return apply_filters( 'fep_current_user_can', $can, $cap, $id );
929 }
930
931 function fep_is_read( $parent = false, $post_id = false, $user_id = false )
932 {
933 if( ! $user_id ) {
934 $user_id = get_current_user_id();
935 }
936 if( ! $post_id ) {
937 $post_id = get_the_ID();
938 }
939 if( !$post_id || !$user_id ) {
940 return false;
941 }
942 if( $parent ) {
943 if( 'threaded' == fep_get_message_view() ){
944 return get_post_meta( fep_get_parent_id( $post_id ), '_fep_parent_read_by_'. $user_id, true );
945 } else {
946 return get_post_meta( $post_id, '_fep_parent_read_by_'. $user_id, true );
947 }
948 }
949 $read_by = get_post_meta( $post_id, '_fep_read_by', true );
950
951
952 if( is_array( $read_by ) && in_array( $user_id, $read_by ) ) {
953 return true;
954 }
955
956 return false;
957 }
958
959 function fep_make_read( $parent = false, $post_id = false, $user_id = false )
960 {
961 if( ! $post_id ) {
962 $post_id = get_the_ID();
963 }
964 if( ! $user_id ) {
965 $user_id = get_current_user_id();
966 }
967 if( !$post_id || !$user_id ) {
968 return false;
969 }
970 if( $parent ) {
971 if( 'threaded' == fep_get_message_view() ){
972 $return = add_post_meta( fep_get_parent_id( $post_id ), '_fep_parent_read_by_'. $user_id, time(), true );
973 } else {
974 $return = add_post_meta( $post_id, '_fep_parent_read_by_'. $user_id, time(), true );
975 }
976 if( $return ) {
977 delete_user_option( $user_id, '_fep_user_message_count' );
978 return true;
979 } else {
980 return false;
981 }
982 }
983 $read_by = get_post_meta( $post_id, '_fep_read_by', true );
984
985 if( ! is_array( $read_by ) ) {
986 $read_by = array();
987 }
988 if( in_array( $user_id, $read_by ) ) {
989 return false;
990 }
991 $read_by[time()] = $user_id;
992
993 return update_post_meta( $post_id, '_fep_read_by', $read_by );
994
995 }
996
997 function fep_get_the_excerpt($count = 100, $excerpt = false ){
998 if( false === $excerpt )
999 $excerpt = get_the_excerpt();
1000 $excerpt = strip_shortcodes($excerpt);
1001 $excerpt = wp_strip_all_tags($excerpt);
1002 $excerpt = substr($excerpt, 0, $count);
1003 $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
1004 $excerpt = $excerpt.' ...';
1005
1006 return apply_filters( 'fep_get_the_excerpt', $excerpt, $count);
1007 }
1008
1009 function fep_get_current_user_max_message_number()
1010 {
1011 $roles = wp_get_current_user()->roles;
1012
1013 $count_array = array();
1014
1015 if( $roles && is_array($roles) ) {
1016 foreach( $roles as $role ) {
1017 $count = fep_get_option("message_box_{$role}", 50);
1018 if( ! $count ) {
1019 return 0;
1020 }
1021 $count_array[] = $count;
1022 }
1023 }
1024 if( $count_array ) {
1025 return max($count_array);
1026 } else {
1027 return 0; //FIX ME. 0 = unlimited !!!!
1028 }
1029 }
1030
1031 function fep_wp_mail_from( $from_email ) {
1032
1033 $email = fep_get_option('from_email', get_bloginfo('admin_email'));
1034
1035 if( is_email( $email ) ) {
1036 return $email;
1037 }
1038 return $from_email;
1039
1040 }
1041
1042 function fep_wp_mail_from_name( $from_name ) {
1043
1044 $name = stripslashes( fep_get_option('from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ) );
1045
1046 if( $name ) {
1047 return $name;
1048 }
1049 return $from_name;
1050
1051 }
1052
1053 function fep_wp_mail_content_type( $content_type ) {
1054
1055 $type = fep_get_option( 'email_content_type', 'plain_text' );
1056
1057 if( 'html' == $type ) {
1058 return 'text/html';
1059 } elseif( 'plain_text' == $type ) {
1060 return 'text/plain';
1061 }
1062 return $content_type;
1063
1064 }
1065
1066 function fep_add_email_filters( $for = 'message' ){
1067
1068 //add_filter( 'wp_mail_from', 'fep_wp_mail_from', 10 );
1069 //add_filter( 'wp_mail_from_name', 'fep_wp_mail_from_name', 10 );
1070 //add_filter( 'wp_mail_content_type', 'fep_wp_mail_content_type', 10 );
1071
1072 do_action( 'fep_action_after_add_email_filters', $for );
1073 }
1074
1075 function fep_remove_email_filters( $for = 'message' ){
1076
1077 //remove_filter( 'wp_mail_from', 'fep_wp_mail_from', 10 );
1078 //remove_filter( 'wp_mail_from_name', 'fep_wp_mail_from_name', 10 );
1079 //remove_filter( 'wp_mail_content_type', 'fep_wp_mail_content_type', 10 );
1080
1081 do_action( 'fep_action_after_remove_email_filters', $for );
1082 }
1083
1084 function fep_delete_message( $message_id, $user_id = 0 ){
1085 if( 'threaded' == fep_get_message_view() ){
1086 $id = fep_get_parent_id( $message_id );
1087 } else {
1088 $id = $message_id;
1089 }
1090 $return = false;
1091
1092 if( $user_id ) {
1093 $return = add_post_meta( $id, '_fep_delete_by_'. $user_id, time(), true );
1094 } elseif( fep_current_user_can( 'delete_message', $id ) ){
1095 $return = add_post_meta( $id, '_fep_delete_by_'. get_current_user_id(), time(), true );
1096 }
1097 $should_delete_from_db = true;
1098 foreach( fep_get_participants( $id ) as $participant ) {
1099 if( ! get_post_meta( $id, '_fep_delete_by_'. $participant, true ) ) {
1100 $should_delete_from_db = false;
1101 break;
1102 }
1103
1104 }
1105 if( $should_delete_from_db && ! get_post_meta( $id, '_fep_group', true ) ) {
1106 $return = wp_trash_post( $id );
1107 }
1108 return $return;
1109 }
1110
1111 function fep_undelete_message( $message_id, $user_id = 0 ){
1112 if( 'threaded' == fep_get_message_view() ){
1113 $id = fep_get_parent_id( $message_id );
1114 } else {
1115 $id = $message_id;
1116 }
1117 $return = false;
1118
1119 if( $user_id ) {
1120 $return = delete_post_meta( $id, '_fep_delete_by_'. $user_id );
1121 } elseif( fep_current_user_can( 'delete_message', $id ) ){
1122 $return = delete_post_meta( $id, '_fep_delete_by_'. get_current_user_id() );
1123 }
1124
1125 return $return;
1126 }
1127
1128 function fep_send_message( $message = null, $override = array() )
1129 {
1130 if( null === $message ) {
1131 $message = $_POST;
1132 }
1133
1134 if( ! empty($message['fep_parent_id'] ) ) {
1135 $message['post_parent'] = absint( $message['fep_parent_id'] );
1136 $message['post_status'] = fep_get_option('reply_post_status','publish');
1137 $message['message_title'] = __('RE:', 'front-end-pm'). ' ' . wp_slash( get_post( $message['post_parent'] )->post_title );
1138 if( 'threaded' != fep_get_message_view() )
1139 $message['message_to_id'] = fep_get_participants( $message['post_parent'] );
1140 } else {
1141 $message['post_status'] = fep_get_option('parent_post_status','publish');
1142 $message['post_parent'] = 0;
1143 }
1144
1145 $message = apply_filters('fep_filter_message_before_send', $message );
1146
1147 if( empty($message['message_title']) || empty($message['message_content']) ) {
1148 return false;
1149 }
1150 // Create post array
1151 $post = array(
1152 'post_title' => $message['message_title'],
1153 'post_content' => $message['message_content'],
1154 'post_status' => $message['post_status'],
1155 'post_parent' => $message['post_parent'],
1156 'post_type' => 'fep_message'
1157 );
1158
1159 if( $override && is_array( $override ) ) {
1160 $post = wp_parse_args( $override, $post );
1161 }
1162
1163 $post = apply_filters('fep_filter_message_after_override', $post, $message );
1164
1165 // Insert the message into the database
1166 $message_id = wp_insert_post( $post );
1167
1168 if( ! $message_id || is_wp_error( $message_id ) ) {
1169 return false;
1170 }
1171 $inserted_message = get_post( $message_id );
1172
1173 do_action('fep_action_message_after_send', $message_id, $message, $inserted_message );
1174
1175 return $message_id;
1176 }
1177
1178 add_action( 'fep_action_message_after_send', 'fep_add_message_participants', 5, 3 );
1179
1180 function fep_add_message_participants( $message_id, $message, $inserted_message ){
1181
1182 if( $inserted_message->post_parent ) {
1183 if( 'threaded' == fep_get_message_view() ){
1184 if( ! in_array( $inserted_message->post_author, fep_get_participants( $inserted_message->post_parent ) )){
1185 add_post_meta( $inserted_message->post_parent, '_fep_participants', $inserted_message->post_author );
1186 fep_make_read( true, $message_id, $inserted_message->post_author );
1187 }
1188
1189 $participants = fep_get_participants( $inserted_message->post_parent );
1190
1191 foreach( $participants as $participant )
1192 {
1193 if( $participant != $inserted_message->post_author ){
1194 fep_undelete_message( $inserted_message->post_parent, $participant);
1195 delete_post_meta( $inserted_message->post_parent, '_fep_parent_read_by_'. $participant );
1196 delete_user_option( $participant, '_fep_user_message_count' );
1197 }
1198 }
1199 }
1200 }
1201 if( ! $inserted_message->post_parent || ( $inserted_message->post_parent && 'threaded' != fep_get_message_view() ) ) {
1202 if( ! empty($message['message_to_id'] ) ) { //FRONT END message_to return id of participants
1203 if( is_array( $message['message_to_id'] ) ) {
1204 foreach( $message['message_to_id'] as $participant ) {
1205
1206 if( ! in_array( $participant, fep_get_participants( $message_id ) )){
1207 add_post_meta( $message_id, '_fep_participants', $participant );
1208 delete_user_option( $participant, '_fep_user_message_count' );
1209 }
1210 }
1211 } else {
1212 if( ! in_array( $message['message_to_id'], fep_get_participants( $message_id ) )){
1213 add_post_meta( $message_id, '_fep_participants', $message['message_to_id'] );
1214 delete_user_option( $message['message_to_id'], '_fep_user_message_count' );
1215 }
1216 }
1217 }
1218 if( ! in_array( $inserted_message->post_author, fep_get_participants( $message_id ) )){
1219 add_post_meta( $message_id, '_fep_participants', $inserted_message->post_author );
1220 delete_user_option( $inserted_message->post_author, '_fep_user_message_count' );
1221 }
1222
1223 fep_make_read( true, $message_id, $inserted_message->post_author );
1224 }
1225 }
1226
1227 add_action ('transition_post_status', 'fep_send_message_transition_post_status', 10, 3);
1228
1229 function fep_send_message_transition_post_status( $new_status, $old_status, $post ){
1230 if ( 'fep_message' != $post->post_type || $old_status === $new_status ) {
1231 return;
1232 }
1233
1234 if( 'publish' == $new_status && 'threaded' == fep_get_message_view() ){
1235 if( $post->post_parent ) {
1236 update_post_meta( $post->post_parent, '_fep_last_reply_by', $post->post_author );
1237 update_post_meta( $post->post_parent, '_fep_last_reply_id', $post->ID );
1238 update_post_meta( $post->post_parent, '_fep_last_reply_time', strtotime( $post->post_date_gmt ) );
1239 } else {
1240 add_post_meta( $post->ID, '_fep_last_reply_by', $post->post_author, true );
1241 add_post_meta( $post->ID, '_fep_last_reply_id', $post->ID, true );
1242 add_post_meta( $post->ID, '_fep_last_reply_time', strtotime( $post->post_date_gmt ), true );
1243 }
1244
1245 } elseif( 'publish' == $old_status && 'threaded' == fep_get_message_view() ){
1246 if( $post->post_parent ) {
1247 $child_args = array(
1248 'post_type' => 'fep_message',
1249 'post_status' => 'publish',
1250 'posts_per_page' => 1,
1251 'post_parent' => $post->post_parent
1252 );
1253 $child = get_posts( $child_args );
1254
1255 if( $child && ! empty( $child[0] ) ){
1256 update_post_meta( $post->post_parent, '_fep_last_reply_by', $child[0]->post_author );
1257 update_post_meta( $post->post_parent, '_fep_last_reply_id', $child[0]->ID );
1258 update_post_meta( $post->post_parent, '_fep_last_reply_time', strtotime( $child[0]->post_date_gmt ) );
1259 } else {
1260 $parent_post = get_post( $post->post_parent );
1261
1262 update_post_meta( $parent_post->ID, '_fep_last_reply_by', $parent_post->post_author );
1263 update_post_meta( $parent_post->ID, '_fep_last_reply_id', $parent_post->ID );
1264 update_post_meta( $parent_post->ID, '_fep_last_reply_time', strtotime( $parent_post->post_date_gmt ) );
1265 }
1266 }
1267 }
1268 if( 'publish' == $new_status || 'publish' == $old_status ){
1269
1270 $participants = fep_get_participants( $post->ID );
1271
1272 foreach( $participants as $participant )
1273 {
1274 delete_user_option( $participant, '_fep_user_message_count' );
1275 if( $participant != $post->post_author ){
1276 delete_user_option( $participant, '_fep_notification_dismiss' );
1277 }
1278 }
1279 }
1280 }
1281
1282 function fep_add_announcement( $announcement = null, $override = array() )
1283 {
1284 if( null === $announcement ) {
1285 $announcement = $_POST;
1286 }
1287
1288 $announcement = apply_filters('fep_filter_announcement_before_added', $announcement );
1289
1290 if( empty($announcement['message_title']) || empty($announcement['message_content']) ) {
1291 return false;
1292 }
1293 // Create post array
1294 $post = array(
1295 'post_title' => $announcement['message_title'],
1296 'post_content' => $announcement['message_content'],
1297 'post_status' => 'publish',
1298 'post_type' => 'fep_announcement'
1299 );
1300
1301 if( $override && is_array( $override ) ) {
1302 $post = wp_parse_args( $override, $post );
1303 }
1304
1305 $post = apply_filters('fep_filter_announcement_after_override', $post, $announcement );
1306
1307 // Insert the message into the database
1308 $announcement_id = wp_insert_post( $post );
1309
1310 if( ! $announcement_id || is_wp_error( $announcement_id ) ) {
1311 return false;
1312 }
1313 $inserted_announcement = get_post( $announcement_id );
1314
1315 if( ! empty($announcement['announcement_roles']) && is_array($announcement['announcement_roles']) ) {
1316 foreach($announcement['announcement_roles'] as $role ) {
1317 add_post_meta( $announcement_id, '_fep_participant_roles', $role );
1318 }
1319 }
1320 add_post_meta( $announcement_id, '_fep_author', $inserted_announcement->post_author, true);
1321
1322 do_action('fep_action_announcement_after_added', $announcement_id, $announcement, $inserted_announcement );
1323
1324 return $announcement_id;
1325 }
1326
1327 function fep_backticker_encode($text) {
1328 $text = $text[1];
1329 $text = str_replace('&amp;lt;', '&lt;', $text);
1330 $text = str_replace('&amp;gt;', '&gt;', $text);
1331 $text = htmlspecialchars($text, ENT_QUOTES);
1332 $text = preg_replace("|\n+|", "\n", $text);
1333 $text = nl2br($text);
1334 $text = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $text);
1335 $text = preg_replace("/^ /", '&nbsp;', $text);
1336 $text = preg_replace("/(?<=&nbsp;| |\n) /", '&nbsp;', $text);
1337
1338 return "<code>$text</code>";
1339 }
1340
1341 function fep_backticker_display_code($text) {
1342 //$text = preg_replace_callback("|`(.*?)`|", "fep_backticker_encode", $text);
1343 $text = preg_replace_callback('!`(?:\r\n|\n|\r|)(.*?)(?:\r\n|\n|\r|)`!ims', "fep_backticker_encode", $text);
1344 $text = str_replace('<code></code>', '`', $text);
1345 return $text;
1346 }
1347
1348 function fep_backticker_code_input_filter( $message ) {
1349
1350 $message['message_content'] = fep_backticker_display_code($message['message_content']);
1351
1352 return $message;
1353 }
1354 add_filter( 'fep_filter_message_before_send', 'fep_backticker_code_input_filter', 5);
1355
1356 function fep_autosuggestion_ajax() {
1357 _deprecated_function( __FUNCTION__, '4.4', 'Fep_Ajax class' );
1358
1359 global $user_ID;
1360
1361 if( !fep_get_option('show_autosuggest', 1) && !fep_is_user_admin() )
1362 die();
1363
1364 if ( check_ajax_referer( 'fep-autosuggestion', 'token', false )) {
1365
1366 $searchq = $_POST['searchBy'];
1367
1368
1369 $args = array(
1370 'search' => "*{$searchq}*",
1371 'search_columns' => array( 'display_name' ),
1372 'exclude' => array( $user_ID ),
1373 'number' => 5,
1374 'orderby' => 'display_name',
1375 'order' => 'ASC',
1376 'fields' => array( 'display_name', 'user_nicename' )
1377 );
1378
1379 $args = apply_filters ('fep_autosuggestion_arguments', $args );
1380
1381 // The Query
1382 $user_query = new WP_User_Query( $args );
1383
1384 if(strlen($searchq)>0)
1385 {
1386 echo "<ul>";
1387 if (! empty( $user_query->results ))
1388 {
1389 foreach($user_query->results as $user)
1390 {
1391
1392 ?>
1393 <li><a href="#" onClick="fep_fill_autosuggestion('<?php echo $user->user_nicename; ?>','<?php echo $user->display_name; ?>');return false;"><?php echo $user->display_name; ?></a></li>
1394 <?php
1395
1396 }
1397 }
1398 else
1399 echo "<li>".__("No matches found", 'front-end-pm')."</li>";
1400 echo "</ul>";
1401 }
1402 }
1403 die();
1404 }
1405
1406 //add_action('wp_ajax_fep_autosuggestion_ajax','fep_autosuggestion_ajax');
1407
1408 function fep_footer_credit()
1409 {
1410 $style = '';
1411 if ( ! fep_get_option('show_branding', 1) ) {
1412 $style = " style='display: none'";
1413 }
1414 echo "<div{$style}><a href='https://www.shamimsplugins.com/products/front-end-pm-pro/' target='_blank'>Front End PM</a></div>";
1415 }
1416
1417 add_action('fep_footer_note', 'fep_footer_credit' );
1418
1419 function fep_notification()
1420 {
1421 if ( ! fep_current_user_can( 'access_message' ) )
1422 return '';
1423 if ( ! fep_get_option('show_notification', 1) )
1424 return '';
1425
1426 $unread_count = fep_get_new_message_number();
1427 $sm = sprintf(_n('%s message', '%s messages', $unread_count, 'front-end-pm'), number_format_i18n($unread_count) );
1428
1429 $show = '';
1430
1431 $unread_ann_count = fep_get_user_announcement_count( 'unread' );
1432 $sa = sprintf(_n('%s announcement', '%s announcements', $unread_ann_count, 'front-end-pm'), number_format_i18n($unread_ann_count) );
1433
1434 if ( $unread_count || $unread_ann_count ) {
1435 $show = __("You have", 'front-end-pm');
1436
1437 if ( $unread_count )
1438 $show .= "<a href='".fep_query_url('messagebox')."'> $sm</a>";
1439
1440 if ( $unread_count && $unread_ann_count )
1441 $show .= ' ' .__('and', 'front-end-pm');
1442
1443 if ( $unread_ann_count )
1444 $show .= "<a href='".fep_query_url('announcements')."'> $sa</a>";
1445
1446 $show .= ' ';
1447 $show .= __('unread', 'front-end-pm');
1448 }
1449 return apply_filters('fep_header_notification', $show);
1450 }
1451
1452
1453 function fep_notification_div() {
1454 if ( ! fep_current_user_can( 'access_message' ) )
1455 return;
1456 if ( ! fep_get_option('show_notification', 1) )
1457 return;
1458
1459 wp_enqueue_script( 'fep-notification-script' );
1460
1461 $unread_count = fep_get_new_message_number();
1462 $sm = sprintf(_n('%s message', '%s messages', $unread_count, 'front-end-pm'), number_format_i18n($unread_count) );
1463
1464 $unread_ann_count = fep_get_new_announcement_number();
1465 $sa = sprintf(_n('%s announcement', '%s announcements', $unread_ann_count, 'front-end-pm'), number_format_i18n($unread_ann_count) );
1466
1467 $class = 'fep-notification-bar';
1468 if( !$unread_count && !$unread_ann_count ){
1469 $class .= ' fep-hide';
1470 } elseif( get_user_option( '_fep_notification_dismiss' ) ){
1471 $class .= ' fep-hide';
1472 }
1473
1474 $show = '<div id="fep-notification-bar" class="'. $class . '"><p>';
1475 $show .= __("You have", 'front-end-pm');
1476
1477 $class = 'fep_unread_message_count_hide_if_zero';
1478 if( ! $unread_count )
1479 $class .= ' fep-hide';
1480
1481 $show .= '<span class="'.$class.'"> <a href="'.fep_query_url('messagebox').'"><span class="fep_unread_message_count_text">'. $sm . '</span></a></span>';
1482
1483 $class = 'fep_hide_if_anyone_zero';
1484 if( ! $unread_count || ! $unread_ann_count )
1485 $class .= ' fep-hide';
1486
1487 $show .= '<span class="'.$class.'"> ' .__('and', 'front-end-pm') . '</span>';
1488
1489 $class = 'fep_unread_announcement_count_hide_if_zero';
1490 if( ! $unread_ann_count )
1491 $class .= ' fep-hide';
1492
1493 $show .= '<span class="'.$class.'"> <a href="'.fep_query_url('announcements').'"><span class="fep_unread_announcement_count_text">'. $sa . '</span></a></span>';
1494
1495 $show .= ' ';
1496 $show .= __('unread', 'front-end-pm');
1497 $show .= '</p>';
1498 $show .= '<button aria-label="'. esc_attr( 'Dismiss notice', 'front-end-pm' ).'" class="fep-notice-dismiss">×</button>';
1499 $show .= '</div>';
1500
1501 echo apply_filters('fep_header_notification', $show);
1502 }
1503
1504
1505 add_action('wp_head', 'fep_notification_div', 99 );
1506
1507 function fep_notification_ajax() {
1508 _deprecated_function( __FUNCTION__, '4.4', 'Fep_Ajax class' );
1509
1510 if ( check_ajax_referer( 'fep-notification', 'token', false )) {
1511
1512 $notification = fep_notification();
1513 if ( $notification )
1514 echo $notification;
1515 }
1516 wp_die();
1517 }
1518
1519 //add_action('wp_ajax_fep_notification_ajax','fep_notification_ajax');
1520 //add_action('wp_ajax_nopriv_fep_notification_ajax','fep_notification_ajax');
1521
1522 function fep_auth_redirect(){
1523 if( ! fep_page_id() || ( ! is_page( fep_page_id() ) && ! is_single( fep_page_id() ) ) ) {
1524 return;
1525 }
1526
1527 do_action( 'fep_template_redirect' );
1528
1529 if( apply_filters( 'fep_using_auth_redirect', false ) ) {
1530 auth_redirect();
1531 }
1532 }
1533 add_action('template_redirect','fep_auth_redirect', 99 );
1534
1535 add_filter( 'auth_redirect_scheme', 'fep_auth_redirect_scheme' );
1536 function fep_auth_redirect_scheme( $scheme ){
1537
1538 if( is_admin() || ! fep_page_id() || ( ! is_page( fep_page_id() ) && ! is_single( fep_page_id() ) ) ) {
1539 return $scheme;
1540 }
1541
1542 return 'logged_in';
1543 }
1544
1545 add_filter( 'map_meta_cap', 'fep_map_meta_cap', 10, 4 );
1546
1547 function fep_map_meta_cap( $caps, $cap, $user_id, $args ) {
1548
1549 $our_caps = array( 'read_fep_message', 'edit_fep_message', 'delete_fep_message', 'read_fep_announcement', 'edit_fep_announcement', 'delete_fep_announcement' );
1550
1551 /* If editing, deleting, or reading a message or announcement, get the post and post type object. */
1552 if ( in_array( $cap, $our_caps ) ) {
1553 $post = get_post( $args[0] );
1554 $post_type = get_post_type_object( $post->post_type );
1555
1556 /* Set an empty array for the caps. */
1557 $caps = array();
1558 } else {
1559 return $caps;
1560 }
1561
1562 /* If editing a message or announcement, assign the required capability. */
1563 if ( 'edit_fep_message' == $cap || 'edit_fep_announcement' == $cap ) {
1564 if ( $user_id == $post->post_author )
1565 $caps[] = $post_type->cap->edit_posts;
1566 else
1567 $caps[] = $post_type->cap->edit_others_posts;
1568 }
1569
1570 /* If deleting a message or announcement, assign the required capability. */
1571 elseif ( 'delete_fep_message' == $cap || 'delete_fep_announcement' == $cap ) {
1572 if ( $user_id == $post->post_author )
1573 $caps[] = $post_type->cap->delete_posts;
1574 else
1575 $caps[] = $post_type->cap->delete_others_posts;
1576 }
1577
1578 /* If reading a private message or announcement, assign the required capability. */
1579 elseif ( 'read_fep_message' == $cap || 'read_fep_announcement' == $cap ) {
1580
1581 if ( 'private' != $post->post_status )
1582 $caps[] = 'read';
1583 elseif ( $user_id == $post->post_author )
1584 $caps[] = 'read';
1585 else
1586 $caps[] = $post_type->cap->read_private_posts;
1587 }
1588
1589 /* Return the capabilities required by the user. */
1590 return $caps;
1591 }
1592
1593 function fep_array_trim( $array )
1594 {
1595
1596 if (!is_array( $array ))
1597 return trim( $array );
1598
1599 return array_map('fep_array_trim', $array );
1600 }
1601
1602 function fep_is_pro(){
1603 return file_exists( FEP_PLUGIN_DIR. 'pro/pro-features.php' );
1604 }
1605
1606 function fep_errors(){
1607 static $errors; // Will hold global variable safely
1608 return isset($errors) ? $errors : ($errors = new WP_Error());
1609 }
1610
1611 function fep_success(){
1612 static $success; // Will hold global variable safely
1613 return isset($success) ? $success : ($success = new WP_Error());
1614 }
1615
1616 function fep_info_output(){
1617
1618 /*
1619 // If conditions are met and errors exist:
1620 if(!fep_info()->get_error_codes()) return;
1621
1622 $success = array();
1623 $info = array();
1624 $errors = array();
1625
1626 // Loop error codes and display errors
1627 foreach( fep_info()->get_error_codes() as $code ){
1628
1629 $data = fep_info()->get_error_data($code);
1630 // Display stuff here
1631 if( 'success' == $data ) {
1632 $success[] = fep_info()->get_error_message($code);
1633 } elseif( 'info' == $data ){
1634 $info[] = fep_info()->get_error_message($code);
1635 } else {
1636 $errors[] = fep_info()->get_error_message($code);
1637 }
1638 }
1639 */
1640
1641 $html = '';
1642
1643 if( fep_success()->get_error_messages() ) {
1644 $html .= '<div class="fep-success">';
1645 foreach( fep_success()->get_error_messages() as $s){
1646 $html .= esc_html($s).'<br />';
1647 }
1648 $html .= '</div>';
1649 }
1650
1651 if( fep_errors()->get_error_messages() ) {
1652 $html .= '<div class="fep-wp-error">';
1653 foreach( fep_errors()->get_error_messages() as $e){
1654 $html .= '<strong>' . __('Error', 'front-end-pm') . ': </strong>'.esc_html($e).'<br />';
1655 }
1656 $html .= '</div>';
1657 }
1658
1659 return $html;
1660
1661 }
1662
1663 function fep_locate_template( $template_names, $load = false, $require_once = true ) {
1664
1665 $locations = array();
1666 $locations[10] = trailingslashit( STYLESHEETPATH ) . 'front-end-pm/';
1667 $locations[20] = trailingslashit( TEMPLATEPATH ) . 'front-end-pm/';
1668 $locations[30] = FEP_PLUGIN_DIR . 'pro/templates/';
1669 $locations[40] = FEP_PLUGIN_DIR . 'templates/';
1670
1671 $locations = apply_filters( 'fep_template_locations', $locations );
1672
1673 // sort the $locations based on priority
1674 ksort( $locations, SORT_NUMERIC );
1675
1676 $template = '';
1677
1678 if( ! is_array( $template_names ) )
1679 $template_names = explode( ',', $template_names );
1680
1681 foreach( $template_names as $template_name ){
1682
1683 $template_name = trim( $template_name );
1684
1685 if ( empty( $template_name ) )
1686 continue;
1687
1688 if( strpos( $template_name, '../') !== false || strpos( $template_name, '..\\') !== false )
1689 continue;
1690
1691 foreach( $locations as $location ){
1692 if( file_exists( $location . $template_name ) ) {
1693 $template = $location . $template_name;
1694 break 2;
1695 }
1696 }
1697
1698 }
1699
1700 if ( ( true == $load ) && ! empty( $template ) )
1701 load_template( $template, $require_once );
1702
1703 return apply_filters( 'fep_locate_template', $template, $template_names, $load, $require_once );
1704 }
1705
1706 add_action('wp_loaded', 'fep_form_posted', 20 ); //After Email hook
1707
1708 function fep_form_posted()
1709 {
1710 $action = !empty($_POST['fep_action']) ? $_POST['fep_action'] : '';
1711
1712 if( ! $action )
1713 return;
1714
1715 if ( ! fep_current_user_can('access_message') )
1716 return;
1717
1718 $menu = Fep_Menu::init()->get_menu();
1719
1720 switch( $action ) {
1721 case has_action("fep_posted_action_{$action}"):
1722 do_action("fep_posted_action_{$action}" );
1723 break;
1724 case ( 'newmessage' == $action && ! empty( $menu['newmessage'] ) ) :
1725 case 'shortcode-newmessage' :
1726 if ( ! fep_current_user_can( 'send_new_message') ){
1727 fep_errors()->add( 'permission', __("You do not have permission to send new message!", 'front-end-pm') );
1728 break;
1729 }
1730
1731 Fep_Form::init()->validate_form_field( $action );
1732 if( count( fep_errors()->get_error_messages()) == 0 ){
1733 if( $message_id = fep_send_message() ) {
1734 $message = get_post( $message_id );
1735
1736 if( 'publish' == $message->post_status ) {
1737 fep_success()->add( 'publish', __("Message successfully sent.", 'front-end-pm') );
1738 } else {
1739 fep_success()->add( 'pending', __("Message successfully sent and waiting for admin moderation.", 'front-end-pm') );
1740 }
1741 } else {
1742 fep_errors()->add( 'undefined', __("Something wrong. Please try again.", 'front-end-pm') );
1743 }
1744 }
1745
1746 break;
1747 case 'reply' :
1748
1749 if( isset( $_GET['fep_id'] ) ){
1750 $pID = absint( $_GET['fep_id'] );
1751 } else {
1752 $pID = !empty($_GET['id']) ? absint($_GET['id']) : 0;
1753 }
1754 $parent_id = fep_get_parent_id( $pID );
1755
1756 if ( ! fep_current_user_can( 'send_reply', $parent_id ) ){
1757 fep_errors()->add( 'permission', __("You do not have permission to send reply to this message!", 'front-end-pm') );
1758 break;
1759 }
1760
1761 Fep_Form::init()->validate_form_field( 'reply' );
1762 if( count( fep_errors()->get_error_messages()) == 0 ){
1763 if( $message_id = fep_send_message() ) {
1764 $message = get_post( $message_id );
1765
1766 if( 'publish' == $message->post_status ) {
1767 fep_success()->add( 'publish', __("Message successfully sent.", 'front-end-pm') );
1768 } else {
1769 fep_success()->add( 'pending', __("Message successfully sent and waiting for admin moderation.", 'front-end-pm') );
1770 }
1771 } else {
1772 fep_errors()->add( 'undefined', __("Something wrong. Please try again.", 'front-end-pm') );
1773 }
1774 }
1775
1776 break;
1777 case 'bulk_action' :
1778 case 'announcement_bulk_action' :
1779 case 'directory_bulk_action' :
1780 $posted_bulk_action = ! empty($_POST['fep-bulk-action']) ? $_POST['fep-bulk-action'] : '';
1781 if( ! $posted_bulk_action )
1782 break;
1783
1784 $token = ! empty($_POST['token']) ? $_POST['token'] : '';
1785
1786 if ( ! fep_verify_nonce( $token, $action ) ) {
1787 fep_errors()->add( 'token', __("Invalid Token. Please try again!", 'front-end-pm') );
1788 break;
1789 }
1790
1791 do_action( "fep_posted_bulk_{$action}", $posted_bulk_action );
1792
1793 break;
1794 /*
1795 // See Fep_User_Settings Class
1796 case ( 'settings' == $action && ! empty( $menu['settings'] ) ) :
1797
1798 add_action ('fep_action_form_validated', 'fep_user_settings_save', 10, 2);
1799
1800 Fep_Form::init()->validate_form_field( 'settings' );
1801
1802 if( count( fep_errors()->get_error_messages()) == 0 ){
1803 fep_success()->add( 'saved', __("Settings successfully saved.", 'front-end-pm') );
1804 }
1805
1806 break;
1807 */
1808 default:
1809 do_action( "fep_posted_action" );
1810 break;
1811
1812 }
1813
1814 if( defined( 'DOING_AJAX' ) && DOING_AJAX ){
1815 $response = array();
1816 if( count( fep_errors()->get_error_messages()) > 0){
1817 $response['fep_return'] = 'error';
1818 } elseif( count( fep_success()->get_error_messages()) > 0 ){
1819 $response['fep_return'] = 'success';
1820 } else {
1821 $response['fep_return'] = '';
1822 }
1823 $response['info'] = fep_info_output();
1824
1825 wp_send_json( $response );
1826 } elseif( !empty( $_POST['fep_redirect'] ) ){
1827 wp_safe_redirect( $_POST['fep_redirect'] );
1828 exit;
1829 }
1830 }
1831
1832 function fep_user_settings_save( $where, $fields )
1833 {
1834 if( 'settings' != $where )
1835 return;
1836
1837 _deprecated_function( __FUNCTION__, '5.3', 'Fep_User_Settings Class' );
1838
1839 if( !$fields || ! is_array( $fields ) )
1840 return;
1841
1842 $settings = array();
1843
1844 foreach( $fields as $field ) {
1845 $settings[$field['name']] = $field['posted-value'];
1846 }
1847 $settings = apply_filters('fep_filter_user_settings_before_save', $settings );
1848
1849 update_user_option( get_current_user_id(), 'FEP_user_options', $settings);
1850 }
1851
1852 function fep_get_participants( $message_id ){
1853 if( empty( $message_id ) || ! is_numeric( $message_id ) )
1854 return array();
1855
1856 if( 'threaded' == fep_get_message_view() ) {
1857 $message_id = fep_get_parent_id( $message_id );
1858 }
1859 $participants = get_post_meta( $message_id, '_fep_participants' );
1860
1861 if( ! $participants )
1862 $participants = get_post_meta( $message_id, '_participants' );
1863
1864 return $participants;
1865 }
1866
1867 function fep_get_participant_roles( $announcement_id ){
1868 if( empty( $announcement_id ) || ! is_numeric( $announcement_id ) )
1869 return array();
1870
1871 $roles = get_post_meta( $announcement_id, '_fep_participant_roles' );
1872
1873 if( ! $roles )
1874 $roles = get_post_meta( $announcement_id, '_participant_roles' );
1875
1876 return $roles;
1877 }
1878
1879 function fep_get_message_view(){
1880 $message_view = fep_get_option('message_view','threaded');
1881 $message_view = apply_filters( 'fep_get_message_view', $message_view );
1882
1883 if( ! $message_view || ! in_array( $message_view, array( 'threaded', 'individual' ) ) )
1884 $message_view = 'threaded';
1885
1886 return $message_view;
1887 }
1888
1889 function fep_get_blocked_users_for_user( $userid = '' ){
1890 $return = array();
1891
1892 if( $blocked_users = fep_get_user_option( 'blocked_users', '', $userid ) ){
1893 $blocked_users = explode( ',', $blocked_users );
1894 $return = array_filter( array_map( 'absint', $blocked_users ) );
1895 }
1896 return apply_filters( 'fep_get_blocked_users_for_user', $return, $userid );
1897 }
1898
1899 function fep_is_user_blocked_for_user( $userid, $check_id = '' ){
1900 $blocked_users = fep_get_blocked_users_for_user( $userid );
1901
1902 if( ! $check_id )
1903 $check_id = get_current_user_id();
1904
1905 if( in_array( $check_id, $blocked_users ) )
1906 return true;
1907
1908 return false;
1909 }
1910
1911 function fep_block_users_for_user( $user_ids, $userid ='' ){
1912
1913 if( is_numeric( $user_ids ) )
1914 $user_ids = array( $user_ids );
1915
1916 if( ! $user_ids || ! is_array( $user_ids ) )
1917 return 0;
1918
1919 $blocked_users = fep_get_blocked_users_for_user( $userid = '' );
1920 $need_block = array_diff( $user_ids, $blocked_users );
1921
1922 if( $need_block ){
1923 $blocked_users = array_unique( array_merge( $blocked_users, $need_block ) );
1924 fep_update_user_option( 'blocked_users', implode( ',', $blocked_users ) );
1925 }
1926 return count( $need_block );
1927 }
1928
1929 function fep_unblock_users_for_user( $user_ids, $userid ='' ){
1930 if( is_numeric( $user_ids ) )
1931 $user_ids = array( $user_ids );
1932
1933 if( ! $user_ids || ! is_array( $user_ids ) )
1934 return 0;
1935
1936 $blocked_users = fep_get_blocked_users_for_user( $userid = '' );
1937 $need_unblock = array_intersect( $blocked_users, $user_ids );
1938
1939 if( $need_unblock ){
1940 $blocked_users = array_unique( array_diff( $blocked_users, $need_unblock ) );
1941 fep_update_user_option( 'blocked_users', implode( ',', $blocked_users ) );
1942 }
1943 return count( $need_unblock );
1944 }
1945
1946 function fep_sanitize_html_class( $class ){
1947 if ( $class ){
1948 if( ! is_array( $class ) )
1949 $class = explode( ' ', $class );
1950 $class = array_map( 'sanitize_html_class', $class );
1951 $class = implode( ' ', array_filter( $class ) );
1952 }
1953 if( ! is_string( $class ) )
1954 $class = '';
1955
1956 return $class;
1957 }
1958
1959 add_filter( 'document_title_parts', 'fep_show_unread_count_in_title', 999 );
1960
1961 function fep_show_unread_count_in_title( $title ){
1962 if( fep_get_option( 'show_unread_count_in_title', 1 ) && fep_current_user_can( 'access_message' ) ){
1963 wp_enqueue_script( 'fep-notification-script' );
1964
1965 if( $count = fep_get_new_message_number() ){
1966 $count = number_format_i18n( $count );
1967 $title['title'] = "($count) " . $title['title'];
1968 }
1969 }
1970 return $title;
1971 }
1972
1973 add_filter( 'pre_get_document_title', 'fep_pre_get_document_title', 999 );
1974
1975 function fep_pre_get_document_title( $title ){
1976
1977 if( ! empty( $title ) && fep_get_option( 'show_unread_count_in_title', 1 ) && fep_current_user_can( 'access_message' ) ){
1978 wp_enqueue_script( 'fep-notification-script' );
1979
1980 if( $count = fep_get_new_message_number() ){
1981 $count = number_format_i18n( $count );
1982 $title = "($count) " . $title;
1983 }
1984 }
1985 return $title;
1986 }
1987
1988