File: /home/mmickelson/theflexguys.com/baronconstruct/wp-content/themes/p2/inc/mentions.php
<?php
class P2_Mentions extends P2_Terms_In_Comments {
var $names = array();
var $users = array();
var $mentions_regex = '/\B@([\w-\.]+)\b/';
function P2_Mentions() {
p2_maybe_define( 'P2_MENTIONS_TAXONOMY', 'mentions', 'p2_mentions_taxonomy' );
p2_maybe_define( 'P2_MENTIONS_SLUG', 'mentions', 'p2_mentions_slug' );
// Hooks
add_action( 'init', array( &$this, 'init' ), 0 );
add_filter( 'the_content', array( &$this, 'mention_links' ), 5 );
add_filter( 'comment_text', array( &$this, 'mention_links' ), 5 );
parent::P2_Terms_In_Comments( P2_MENTIONS_TAXONOMY );
}
function init() {
// Register P2 user taxonomy
$taxonomy_args = apply_filters( 'p2_mentions_taxonomy_args', array(
'show_ui' => false,
'show_in_nav_menus' => false,
'rewrite' => array( 'slug' => P2_MENTIONS_SLUG ),
) );
register_taxonomy( P2_MENTIONS_TAXONOMY, 'post', $taxonomy_args );
}
/**
* Generates array of users indexed by user ID, and
* an array of user_nicenames, indexed by user ID.
*
* @compat < 3.1: Use $wpdb and get_users of blog.
*/
function load_users() {
global $wpdb; // < 3.1
// Cache the user information.
if ( ! empty( $this->users ) )
return $this->users;
if ( function_exists( 'get_users' ) ) { // >= 3.1
$users = get_users();
foreach ( $users as $user ) {
$this->users[ $user->ID ] = $user;
$this->names[ $user->ID ] = $user->user_nicename;
}
} else { // < 3.1
$users = get_users_of_blog();
$user_ids = '';
foreach ( $users as $user ) {
$this->users[ $user->ID ] = $user;
$user_ids .= $user->ID;
}
foreach ( $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($user_ids)" ) as $user ) {
$this->users[ $user->ID ]['user_nicename'] = $user->user_nicename;
$this->names[ $user->ID ] = $user->user_nicename;
}
}
return $this->users;
}
function update_post_terms( $post_id, $post ) {
return $this->find_mentions( $post->post_content );
}
function update_comment_terms( $comment_id, $comment ) {
return $this->find_mentions( $comment->comment_content );
}
function find_mentions( $content ) {
if ( ! preg_match_all( $this->mentions_regex, $content, $matches ) )
return array();
$this->load_users();
return array_intersect( $matches[1], $this->names );
}
/**
* Parses and links mentions within a string.
* Run on the_content.
*
* @param string $content The content.
* @return string The linked content.
*/
function mention_links( $content ) {
$names = $this->find_mentions( $content );
$slug = P2_MENTIONS_SLUG;
$search = is_search() ? substr( get_search_query( false ), 1 ) : '';
foreach ( $names as $name ) {
$classes = 'mention';
// If we're searching for this name, highlight it.
if ( $name === $search )
$classes .= ' mention-highlight';
$replacement = "<a href='" . esc_url( home_url( "$slug/$name/" ) ) . "' class='$classes'>@$name</a>";
$replacement = apply_filters( 'p2_mention_link', $replacement, $name );
$content = preg_replace( "/@$name\b/i", $replacement, $content );
}
return $content;
}
/**
* Generates the user information for the mentions autocomplete feature.
*
* @return array User information.
*/
function user_suggestion() {
// Membership check
$user = wp_get_current_user();
if ( function_exists( 'is_user_member_of_blog' ) && ! is_user_member_of_blog( $user->ID ) )
return;
// Capability check
if ( ! current_user_can( 'edit_posts' ) )
return;
$this->load_users();
$js_users = array();
foreach( $this->users as $user ) {
$js_users[] = array(
'name' => $user->display_name,
'username' => $user->user_nicename,
'gravatar' => get_avatar( $user->user_email, 32 ),
);
}
return apply_filters( "p2_user_suggestion", $js_users );
}
}
?>