From db77ffea092f600ca749122a75628911764561f2 Mon Sep 17 00:00:00 2001 From: Bunty Date: Sun, 3 Nov 2024 15:37:06 +0530 Subject: [PATCH 1/2] Add filter and script for pro plugin --- app/main/class-comment-mention.php | 2 + src/js/tribute-config.js | 110 +++++++++++++++++++---------- 2 files changed, 75 insertions(+), 37 deletions(-) diff --git a/app/main/class-comment-mention.php b/app/main/class-comment-mention.php index d004726..2f15f78 100644 --- a/app/main/class-comment-mention.php +++ b/app/main/class-comment-mention.php @@ -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 ); diff --git a/src/js/tribute-config.js b/src/js/tribute-config.js index 03f5e5d..c47a210 100644 --- a/src/js/tribute-config.js +++ b/src/js/tribute-config.js @@ -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 ); + } } -}); +); From 83a53cc5f071e319fb9f7784eaf4a93edf16d1bb Mon Sep 17 00:00:00 2001 From: Bunty Date: Sun, 3 Nov 2024 15:48:04 +0530 Subject: [PATCH 2/2] version updated to v1.7.11 --- README.md | 11 ++++++++--- comment-mention.php | 4 ++-- readme.txt | 11 ++++++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7ce1e56..1946161 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. diff --git a/comment-mention.php b/comment-mention.php index 962ca92..877cfd5 100644 --- a/comment-mention.php +++ b/comment-mention.php @@ -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 */ @@ -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' ) ) { /** diff --git a/readme.txt b/readme.txt index 0ed66a4..23e0237 100644 --- a/readme.txt +++ b/readme.txt @@ -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 @@ -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 @@ -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.