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(); // Filters found mentions. Passes original found mentions and content as args. return apply_filters( 'p2_found_mentions', $matches[1], $matches[1], $content ); } function filter_mentions( $mentions ) { $this->load_users(); return array_intersect( $mentions, $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 = "@$name"; $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 ( ! is_super_admin() && 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 ); } } ?>