Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: Someone Update Say My Name Plugin

<?php
/*
* Plugin Name: Say My Name
* Plugin Description: Sends a notification email if someone mentions your name in a post (based on Notify Post by Thomas Klaiber).
* Author: <a href="http://www.ellequadro.net">Matteo Crippa</a>, updated for bbPress 1.0 by Kawauso
* Version: 0.2
*/

function say_my_name ( $post_id, $args ) {

global $bbdb, $topic;

if ( isset( $args[ 'post_id' ] ) && false !== $args[ 'post_id' ] ) // Don't run on edits
return;

$post = strtolower( $args[ 'post_text' ] );

$all_users = $bbdb->get_results( "SELECT * FROM $bbdb->users WHERE user_status=0" );

foreach( $all_users as $userdata ) {

if( !is_smn( $userdata->ID ) )
continue;

/* $notify = false;
*/ $display_name = strtolower( $userdata->display_name );

/* if( strpos( $display_name, ' ' ) === false ) { // Use word-by-word searching if the display name is one word
if( !isset( $words ) )
$words = explode( ' ', $post ); // Only create the word list if necessary

foreach ( $words as $word ) {

if( $display_name == $word || "@$display_name" == $word ) {

$notify = true;
break;

}

}
}

else*/ if( strpos( " $post", " $display_name" ) !== false || strpos( " $post", " @$display_name" ) !== false) // Always require a leading space
/* $notify = true;

if( $notify ) */{

$message = __( "Someone called you on: %1$s nn%2$s " );
@mail( $userdata->user_email, bb_get_option( 'name' ) . ':' . __( 'Notification' ), sprintf( $message, $topic->topic_title, get_topic_link( $topic_id ) ), 'From: ' . bb_get_option( 'admin_email' ) );

}

}

}
add_action( 'bb_insert_post', 'say_my_name', 1, 2 );

function smn_profile() {

if( !bb_is_user_logged_in() )
return;

global $user_id;

if( is_smn( $user_id ) )
$checked = ' checked="checked"';
else
$checked = '';

?>
</fieldset>
<fieldset>
<legend>Say My Name Notification</legend>
<p><?php _e('If you want to get an email when someone call your name in a new post.')?></p>
<table width="100%">
<tr>
<th width="21%" scope="row"><?php _e('Activate')?>:</th>
<td width="79%"><input name="smn" id="smn" type="checkbox" value="1"<?php echo $checked?> /></td>
</tr>
</table>
<?php
}
add_action( 'extra_profile_info', 'smn_profile' );

function smn_edit() {
global $user_id;

if( $_POST[ 'smn' ] )
bb_update_usermeta( $user_id, "smn", true );
else
bb_update_usermeta( $user_id, "smn", false );

}
add_action( 'profile_edited', 'smn_edit' );

function is_smn ( $user_id ) {
$user = bb_get_user( $user_id );
if ( $user->smn )
return true;
else
return false;
}
?>

Take out the /* */ around the commented out parts of the code if you want to re-enable the foreach() loop method for single word display names. I couldn’t find much of a difference in speed, but then again it might be quite different on a live server. You can also just delete all that code too if you want to make the file smaller :P I’ll upload this to the Extend section someday, along with the other plugin I keep meaning to finish and upload.

Edit: Added support for @ :P

This should be 100% case insensitive, but I’ve not tested with anything unicode. It’ll always look for a space before the name to avoid false positives, but if it’s at the very start of the post, it’ll still be detected.

Skip to toolbar