Skip to content

Commit cde582b

Browse files
Merge pull request #36 from BhargavBhandari90/mention-by-name
Add filter and script for pro plugin
2 parents daffe84 + 83a53cc commit cde582b

File tree

5 files changed

+93
-45
lines changed

5 files changed

+93
-45
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Comment Mention #
22
**Contributors:** [bhargavbhandari90](https://profiles.wordpress.org/bhargavbhandari90/), [biliplugins](https://profiles.wordpress.org/biliplugins/), [hackkzy404](https://profiles.wordpress.org/hackkzy404/)
33
**Donate link:** https://www.paypal.me/BnB90/50
4-
**Tags:** comments, mention, email, user, bbpress
4+
**Tags:** comments, mention, email, user, bbpress, buntywp
55
**Requires at least:** 4.6
6-
**Tested up to:** 6.4.3
7-
**Stable tag:** 1.7.10
6+
**Tested up to:** 6.6.2
7+
**Stable tag:** 1.7.11
88
**Requires PHP:** 5.6
99
**License:** GPLv2 or later
1010
**License URI:** https://www.gnu.org/licenses/gpl-2.0.html
@@ -31,6 +31,7 @@ https://www.youtube.com/watch?v=Nz47aKJhsKQ
3131
* Search by Display name while mention.
3232
* Enable mentioning on Page comment.
3333
* Added option to Turn off Email notification.
34+
* Mention by First Name & Last Name.
3435
* Go to wp-admin –> Comment Mention
3536
* And you will see options to enable pro features https://prnt.sc/r5W2X4utYe3v
3637

@@ -62,6 +63,10 @@ e.g.
6263

6364
## Changelog ##
6465

66+
### 1.7.11 ###
67+
* Hook added : `cmt_mntn_comment_pre_content`.
68+
* Updated mention script for pro feature.
69+
6570
### 1.7.10 ###
6671
* Fix bbPress reply link.
6772

app/main/class-comment-mention.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ public function cmt_mntn_at_name_filter( $content ) {
169169
return $content;
170170
}
171171

172+
$content = apply_filters( 'cmt_mntn_comment_pre_content', $content );
173+
172174
// Try to find mentions.
173175
$usernames = $this->cmt_mntn_find_mentions( $content );
174176

comment-mention.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Author URI: https://bhargavb.com
77
* Text Domain: comment-mention
88
* Domain Path: /languages
9-
* Version: 1.7.10
9+
* Version: 1.7.11
1010
*
1111
* @package Comment_Mention
1212
*/
@@ -21,7 +21,7 @@
2121
/**
2222
* The version of the plugin.
2323
*/
24-
define( 'CMT_MNTN_VERSION', '1.7.10' );
24+
define( 'CMT_MNTN_VERSION', '1.7.11' );
2525
}
2626
if ( ! defined( 'CMT_MNTN_PATH' ) ) {
2727
/**

readme.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== Comment Mention ===
22
Contributors: bhargavbhandari90, biliplugins, hackkzy404
33
Donate link: https://www.paypal.me/BnB90/50
4-
Tags: comments, mention, email, user, bbpress
4+
Tags: comments, mention, email, user, bbpress, buntywp
55
Requires at least: 4.6
6-
Tested up to: 6.4.3
7-
Stable tag: 1.7.10
6+
Tested up to: 6.6.2
7+
Stable tag: 1.7.11
88
Requires PHP: 5.6
99
License: GPLv2 or later
1010
License URI: https://www.gnu.org/licenses/gpl-2.0.html
@@ -31,6 +31,7 @@ https://www.youtube.com/watch?v=Nz47aKJhsKQ
3131
* Search by Display name while mention.
3232
* Enable mentioning on Page comment.
3333
* Added option to Turn off Email notification.
34+
* Mention by First Name & Last Name.
3435
* Go to wp-admin –> Comment Mention
3536
* And you will see options to enable pro features https://prnt.sc/r5W2X4utYe3v
3637

@@ -62,6 +63,10 @@ e.g.
6263

6364
== Changelog ==
6465

66+
= 1.7.11 =
67+
* Hook added : `cmt_mntn_comment_pre_content`.
68+
* Updated mention script for pro feature.
69+
6570
= 1.7.10 =
6671
* Fix bbPress reply link.
6772

src/js/tribute-config.js

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,79 @@
1-
jQuery(function ($) {
2-
let getUsers;
3-
let tribute = new Tribute({
4-
values: function (search, cb) {
5-
getUsernames(search, (users) => cb(users));
6-
},
7-
lookup: function (users, mentionText) {
8-
if (users.key.includes(mentionText)) {
9-
return users.key + " (" + users.name + ")";
10-
} else if (users.name.includes(mentionText)) {
11-
return users.name + " (" + users.key + ")";
12-
} else if (users.user_nicename.includes(mentionText)) {
13-
return users.user_nicename + " (" + users.name + ")";
1+
jQuery(
2+
function ($) {
3+
const mentionMap = {};
4+
let tribute = new Tribute(
5+
{
6+
values: function (search, cb) {
7+
let data = {
8+
action: "cmt_mntn_get_users",
9+
term: search,
10+
};
11+
12+
$.ajax(
13+
{
14+
url: Comment_Mention.ajaxurl, // Replace with your API endpoint
15+
method: 'GET',
16+
data: data, // Send current text as the search term
17+
dataType: 'json',
18+
success: function(response) {
19+
// Format the response for Tribute.js
20+
// const results = response.data;
21+
const results = response.data.map(
22+
function(item) {
23+
mentionMap[item.key] = item.name;
24+
return item;
25+
}
26+
);
27+
cb( results ); // Pass formatted results to Tribute.js
28+
},
29+
error: function(xhr, status, error) {
30+
console.error( "Error fetching data:", error );
31+
cb( [] ); // Pass empty array on error
32+
}
33+
}
34+
);
35+
},
36+
lookup: function (users, mentionText) {
37+
if (users.key.includes( mentionText )) {
38+
return users.key + " (" + users.name + ")";
39+
} else if (users.name.includes( mentionText )) {
40+
return users.name + " (" + users.key + ")";
41+
} else if (users.user_nicename.includes( mentionText )) {
42+
return users.user_nicename + " (" + users.name + ")";
43+
}
44+
},
45+
selectTemplate: function(item) {
46+
if ( 'undefined' !== typeof( Comment_Mention.mention_by_fullname ) && '1' === Comment_Mention.mention_by_fullname ) {
47+
return '@' + item.original.name; // Use `name` in textarea1
48+
} else {
49+
return '@' + item.original.key; // Use `name` in textarea1
50+
}
51+
}
1452
}
15-
},
16-
});
53+
);
54+
55+
tribute.attach( $( "#commentform textarea" ) );
56+
tribute.attach( $( ".bbp-topic-form form textarea" ) );
57+
tribute.attach( $( ".bbp-reply-form form textarea" ) );
1758

18-
tribute.attach($("#commentform textarea"));
19-
tribute.attach($(".bbp-topic-form form textarea"));
20-
tribute.attach($(".bbp-reply-form form textarea"));
59+
if ( $( '#main_comment' ).length > 0 ) {
60+
$( '#comment' ).on( 'tribute-replaced', cmt_mntn_sync_usernames );
61+
$( '#comment' ).on( 'input', cmt_mntn_sync_usernames );
62+
}
2163

22-
function getUsernames(search, cb) {
23-
let data = {
24-
action: "cmt_mntn_get_users",
25-
term: search,
26-
};
64+
function cmt_mntn_sync_usernames() {
2765

28-
getUsers = $.ajax({
29-
url: Comment_Mention.ajaxurl,
30-
data: data,
31-
method: "GET",
32-
beforeSend: function () {
33-
if (getUsers != null) {
34-
getUsers.abort();
66+
let content = $( '#comment' ).val();
67+
68+
Object.keys( mentionMap ).forEach(
69+
name => {
70+
const nameMention = '@' + mentionMap[name]; // Mention format in textarea1 (name)
71+
const usernameMention = '@' + name; // Mention format in textarea2 (username)
72+
content = content.split( nameMention ).join( usernameMention );
3573
}
36-
},
37-
success: function (response) {
38-
var usernames = response.data;
39-
cb(usernames);
40-
},
41-
});
74+
);
75+
76+
$( '#main_comment' ).val( content );
77+
}
4278
}
43-
});
79+
);

0 commit comments

Comments
 (0)