PluginProbe
Front End PM / 7.3
Front End PM v7.3
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.3, at functions.php

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