| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\modules\mentioning\classes; |
| 4 |
|
| 5 |
class Actions { |
| 6 |
public function __construct() { |
| 7 |
$this->init_hooks(); |
| 8 |
} |
| 9 |
|
| 10 |
public function init_hooks() { |
| 11 |
add_filter( 'wpforo_body_text_filter', [ $this, 'mentioned_code_to_link' ] ); |
| 12 |
add_action( 'wpforo_after_add_topic', [ $this, 'send_mail_to_mentioned_users' ], 5, 2 ); |
| 13 |
add_action( 'wpforo_after_add_post', [ $this, 'send_mail_to_mentioned_users' ], 5, 2 ); |
| 14 |
|
| 15 |
#### -- AJAX ACTIONS -- ### |
| 16 |
add_action( 'wp_ajax_wpforo_mute_mentions', [ $this, 'mute' ] ); |
| 17 |
} |
| 18 |
|
| 19 |
public function mentioned_code_to_link( $text ) { |
| 20 |
return preg_replace_callback( |
| 21 |
'#@([^\s\0<>\[\]!,.()\'\"|?@]+)($|[\s\0<>\[\]!,.()\'\"|?@])#iu', |
| 22 |
function( $match ) { |
| 23 |
$return = $match[0]; |
| 24 |
if( $user = WPF()->member->get_member( $match[1] ) ) { |
| 25 |
$return = wpforo_member_link( $user, '', 30, '', false, '@' . $match[1] ) . $match[2]; |
| 26 |
} |
| 27 |
|
| 28 |
return $return; |
| 29 |
}, |
| 30 |
(string) $text |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
public function send_mail_to_mentioned_users( $item, $pitem ) { |
| 35 |
if( preg_match_all( '#@([^\s\0<>\[\]!,.()\'\"|?@]+)(?:$|[\s\0<>\[\]!,.()\'\"|?@])#iu', (string) $item['body'], $matches, PREG_SET_ORDER ) ) { |
| 36 |
$topic = wpfkey( $item, 'first_postid' ) ? $item : $pitem; |
| 37 |
$owner = wpforo_member( $item ); |
| 38 |
foreach( $matches as $match ) { |
| 39 |
$user = WPF()->member->get_member( $match[1] ); |
| 40 |
if( ! empty( $user['user_email'] ) && ! $user['is_mention_muted'] ) { |
| 41 |
if( apply_filters( 'break_wpforo_mention_send_email', false, $user, $owner, $item, $pitem ) ) continue; |
| 42 |
if( in_array( wpfval($user, 'status'), ['banned', 'inactive'], true ) ) continue; |
| 43 |
if( wpforo_is_users_same( $owner, $user ) ) continue; |
| 44 |
if( wpforo_is_users_same( $user ) ) continue; |
| 45 |
if( wpfkey( $item, 'first_postid' ) ){ |
| 46 |
if( ! WPF()->topic->view_access( $item, $user ) ) continue; |
| 47 |
}else{ |
| 48 |
if( ! WPF()->post->view_access( $item, $user ) ) continue; |
| 49 |
} |
| 50 |
//Adding notification if user doesn't mention yourself |
| 51 |
if( apply_filters( 'wpforo_user_mentioning_notification', true ) ) { |
| 52 |
$notify = false; |
| 53 |
//If current post is a reply check the parent post |
| 54 |
if( wpfval( $item, 'parentid' ) ) { |
| 55 |
$parent_post = wpforo_post( $item['parentid'] ); |
| 56 |
//Parent post author has already received a "new_reply" notification, |
| 57 |
//so the mentioning is not neccessary. It only adds a new notification |
| 58 |
//if the parent post author is not the mentioned user. |
| 59 |
if( ! empty( $parent_post ) && $user['userid'] != wpfval( $parent_post, 'userid' ) ) $notify = true; |
| 60 |
} else { |
| 61 |
//This post is probably a first topic post or a single post (not a reply) |
| 62 |
//The topic author has already received a "new_reply" notification, |
| 63 |
//so the mentioning is not neccessary. It only adds a new notification |
| 64 |
//if the topic author is not the mentioned user. |
| 65 |
if( ! empty( $topic ) && $user['userid'] != wpfval( $topic, 'userid' ) ) $notify = true; |
| 66 |
} |
| 67 |
if( $notify ) { |
| 68 |
$args = [ |
| 69 |
'itemid' => ( wpfval( $item, 'postid' ) ? $item['postid'] : $item['first_postid'] ), |
| 70 |
'userid' => $user['userid'], |
| 71 |
'content' => ( wpfval( $item, 'body' ) ? $item['body'] : $item['title'] ), |
| 72 |
'permalink' => ( wpfval( $item, 'posturl' ) ? $item['posturl'] : ( wpfval($item, 'topicurl' ) ?: WPF()->topic->get_url( $item ) ) ), |
| 73 |
]; |
| 74 |
WPF()->activity->add_notification( 'new_mention', $args ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
//Sending Email Notification |
| 79 |
if( wpforo_setting( 'subscriptions', 'user_mention_notify' ) ) { |
| 80 |
|
| 81 |
if( wpfkey( $item, 'first_postid' ) ){ |
| 82 |
$key = 'topicid_' . $item['topicid']; |
| 83 |
}else{ |
| 84 |
$key = 'postid_' . $item['postid']; |
| 85 |
} |
| 86 |
$key .= '_user_email_' . $user['user_email']; |
| 87 |
|
| 88 |
if( ! WPF()->ram_cache->exists( $key ) ){ |
| 89 |
|
| 90 |
$sbj_msg = WPF()->sbscrb->get_sbj_msg( 'user_mention', $pitem, $item, $owner, $user, '' ); |
| 91 |
wpforo_send_email( $user['user_email'], $sbj_msg['sbj'], $sbj_msg['msg'], '', 'mention', (int) $item['postid'] ); |
| 92 |
|
| 93 |
WPF()->ram_cache->set( $key, true ); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Mute Mentions, do not send mention emails |
| 105 |
*/ |
| 106 |
public function mute() { |
| 107 |
wpforo_verify_nonce( 'wpforo_mute_mentions' ); |
| 108 |
if( WPF()->current_userid && WPF()->current_object['user_is_same_current_user'] ) { |
| 109 |
$is_mention_muted = ! (int) wpfval( $_POST, 'currentstate' ); |
| 110 |
$r = WPF()->member->update_profile_field( WPF()->current_userid, 'is_mention_muted', $is_mention_muted ); |
| 111 |
if( $r !== false ) wp_send_json_success( [ |
| 112 |
'ico' => WPF()->sbscrb->Mentioning->Template->get_currentstate_ico( $is_mention_muted ), |
| 113 |
'currentstate' => (int) $is_mention_muted, |
| 114 |
] ); |
| 115 |
} |
| 116 |
wp_send_json_error(); |
| 117 |
} |
| 118 |
} |
| 119 |
|