Skip to content

Commit

Permalink
Merge pull request #36 from BhargavBhandari90/mention-by-name
Browse files Browse the repository at this point in the history
Add filter and script for pro plugin
  • Loading branch information
BhargavBhandari90 authored Nov 3, 2024
2 parents daffe84 + 83a53cc commit cde582b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 45 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Comment Mention #
**Contributors:** [bhargavbhandari90](https://profiles.wordpress.org/bhargavbhandari90/), [biliplugins](https://profiles.wordpress.org/biliplugins/), [hackkzy404](https://profiles.wordpress.org/hackkzy404/)
**Donate link:** https://www.paypal.me/BnB90/50
**Tags:** comments, mention, email, user, bbpress
**Tags:** comments, mention, email, user, bbpress, buntywp
**Requires at least:** 4.6
**Tested up to:** 6.4.3
**Stable tag:** 1.7.10
**Tested up to:** 6.6.2
**Stable tag:** 1.7.11
**Requires PHP:** 5.6
**License:** GPLv2 or later
**License URI:** https://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -31,6 +31,7 @@ https://www.youtube.com/watch?v=Nz47aKJhsKQ
* Search by Display name while mention.
* Enable mentioning on Page comment.
* Added option to Turn off Email notification.
* Mention by First Name & Last Name.
* Go to wp-admin –> Comment Mention
* And you will see options to enable pro features https://prnt.sc/r5W2X4utYe3v

Expand Down Expand Up @@ -62,6 +63,10 @@ e.g.

## Changelog ##

### 1.7.11 ###
* Hook added : `cmt_mntn_comment_pre_content`.
* Updated mention script for pro feature.

### 1.7.10 ###
* Fix bbPress reply link.

Expand Down
2 changes: 2 additions & 0 deletions app/main/class-comment-mention.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public function cmt_mntn_at_name_filter( $content ) {
return $content;
}

$content = apply_filters( 'cmt_mntn_comment_pre_content', $content );

// Try to find mentions.
$usernames = $this->cmt_mntn_find_mentions( $content );

Expand Down
4 changes: 2 additions & 2 deletions comment-mention.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Author URI: https://bhargavb.com
* Text Domain: comment-mention
* Domain Path: /languages
* Version: 1.7.10
* Version: 1.7.11
*
* @package Comment_Mention
*/
Expand All @@ -21,7 +21,7 @@
/**
* The version of the plugin.
*/
define( 'CMT_MNTN_VERSION', '1.7.10' );
define( 'CMT_MNTN_VERSION', '1.7.11' );
}
if ( ! defined( 'CMT_MNTN_PATH' ) ) {
/**
Expand Down
11 changes: 8 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
=== Comment Mention ===
Contributors: bhargavbhandari90, biliplugins, hackkzy404
Donate link: https://www.paypal.me/BnB90/50
Tags: comments, mention, email, user, bbpress
Tags: comments, mention, email, user, bbpress, buntywp
Requires at least: 4.6
Tested up to: 6.4.3
Stable tag: 1.7.10
Tested up to: 6.6.2
Stable tag: 1.7.11
Requires PHP: 5.6
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -31,6 +31,7 @@ https://www.youtube.com/watch?v=Nz47aKJhsKQ
* Search by Display name while mention.
* Enable mentioning on Page comment.
* Added option to Turn off Email notification.
* Mention by First Name & Last Name.
* Go to wp-admin –> Comment Mention
* And you will see options to enable pro features https://prnt.sc/r5W2X4utYe3v

Expand Down Expand Up @@ -62,6 +63,10 @@ e.g.

== Changelog ==

= 1.7.11 =
* Hook added : `cmt_mntn_comment_pre_content`.
* Updated mention script for pro feature.

= 1.7.10 =
* Fix bbPress reply link.

Expand Down
110 changes: 73 additions & 37 deletions src/js/tribute-config.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,79 @@
jQuery(function ($) {
let getUsers;
let tribute = new Tribute({
values: function (search, cb) {
getUsernames(search, (users) => cb(users));
},
lookup: function (users, mentionText) {
if (users.key.includes(mentionText)) {
return users.key + " (" + users.name + ")";
} else if (users.name.includes(mentionText)) {
return users.name + " (" + users.key + ")";
} else if (users.user_nicename.includes(mentionText)) {
return users.user_nicename + " (" + users.name + ")";
jQuery(
function ($) {
const mentionMap = {};
let tribute = new Tribute(
{
values: function (search, cb) {
let data = {
action: "cmt_mntn_get_users",
term: search,
};

$.ajax(
{
url: Comment_Mention.ajaxurl, // Replace with your API endpoint
method: 'GET',
data: data, // Send current text as the search term
dataType: 'json',
success: function(response) {
// Format the response for Tribute.js
// const results = response.data;
const results = response.data.map(
function(item) {
mentionMap[item.key] = item.name;
return item;
}
);
cb( results ); // Pass formatted results to Tribute.js
},
error: function(xhr, status, error) {
console.error( "Error fetching data:", error );
cb( [] ); // Pass empty array on error
}
}
);
},
lookup: function (users, mentionText) {
if (users.key.includes( mentionText )) {
return users.key + " (" + users.name + ")";
} else if (users.name.includes( mentionText )) {
return users.name + " (" + users.key + ")";
} else if (users.user_nicename.includes( mentionText )) {
return users.user_nicename + " (" + users.name + ")";
}
},
selectTemplate: function(item) {
if ( 'undefined' !== typeof( Comment_Mention.mention_by_fullname ) && '1' === Comment_Mention.mention_by_fullname ) {
return '@' + item.original.name; // Use `name` in textarea1
} else {
return '@' + item.original.key; // Use `name` in textarea1
}
}
}
},
});
);

tribute.attach( $( "#commentform textarea" ) );
tribute.attach( $( ".bbp-topic-form form textarea" ) );
tribute.attach( $( ".bbp-reply-form form textarea" ) );

tribute.attach($("#commentform textarea"));
tribute.attach($(".bbp-topic-form form textarea"));
tribute.attach($(".bbp-reply-form form textarea"));
if ( $( '#main_comment' ).length > 0 ) {
$( '#comment' ).on( 'tribute-replaced', cmt_mntn_sync_usernames );
$( '#comment' ).on( 'input', cmt_mntn_sync_usernames );
}

function getUsernames(search, cb) {
let data = {
action: "cmt_mntn_get_users",
term: search,
};
function cmt_mntn_sync_usernames() {

getUsers = $.ajax({
url: Comment_Mention.ajaxurl,
data: data,
method: "GET",
beforeSend: function () {
if (getUsers != null) {
getUsers.abort();
let content = $( '#comment' ).val();

Object.keys( mentionMap ).forEach(
name => {
const nameMention = '@' + mentionMap[name]; // Mention format in textarea1 (name)
const usernameMention = '@' + name; // Mention format in textarea2 (username)
content = content.split( nameMention ).join( usernameMention );
}
},
success: function (response) {
var usernames = response.data;
cb(usernames);
},
});
);

$( '#main_comment' ).val( content );
}
}
});
);

0 comments on commit cde582b

Please sign in to comment.