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. * * @return array An array of user objects indexed by user ID. */ function load_users() { // Cache the user information. if ( ! empty( $this->users ) ) return $this->users; $users = get_users(); foreach ( $users as $user ) { $this->users[ $user->ID ] = $user; $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 ); } static function wp_html_split( $input ) { static $regex; if ( ! isset( $regex ) && function_exists( 'get_html_split_regex' ) ) $regex = get_html_split_regex(); if ( ! isset( $regex ) ) { $comments = '!' // Start of comment, after the <. . '(?:' // Unroll the loop: Consume everything until --> is found. . '-(?!->)' // Dash not followed by end of comment. . '[^\-]*+' // Consume non-dashes. . ')*+' // Loop possessively. . '(?:-->)?'; // End of comment. If not found, match all input. $cdata = '!\[CDATA\[' // Start of comment, after the <. . '[^\]]*+' // Consume non-]. . '(?:' // Unroll the loop: Consume everything until ]]> is found. . '](?!]>)' // One ] not followed by end of comment. . '[^\]]*+' // Consume non-]. . ')*+' // Loop possessively. . '(?:]]>)?'; // End of comment. If not found, match all input. $escaped = '(?=' // Is the element escaped? . '!--' . '|' . '!\[CDATA\[' . ')' . '(?(?=!-)' // If yes, which type? . $comments . '|' . $cdata . ')'; $regex = '/(' // Capture the entire match. . '<' // Find start of element. . '(?' // Conditional expression follows. . $escaped // Find end of escaped element. . '|' // ... else ... . '[^>]*>?' // Find end of normal element. . ')' . ')/'; } return preg_split( $regex, $input, -1, PREG_SPLIT_DELIM_CAPTURE ); } function mention_link_callback( $matches ) { global $current_user; $name = $matches[1]; if ( !in_array( $name, $this->names ) ) return $matches[0]; $slug = P2_MENTIONS_SLUG; $search = is_search() ? substr( get_search_query( false ), 1 ) : ''; $classes = 'mention'; // If we're searching for this name, highlight it. if ( $name === $search ) $classes .= ' mention-highlight'; if ( is_user_logged_in() && $name === $current_user->user_login ) $classes .= ' mention-current-user'; $url = get_term_link( $name, P2_MENTIONS_TAXONOMY ); $url = apply_filters( 'p2_mention_url', $url, $name ); if ( is_wp_error( $url ) || ! $url ) { return $matches[0]; } $replacement = "@$name"; return apply_filters( 'p2_mention_link', $replacement, $name ); } /** * 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 ) { global $current_user; if ( !preg_match( $this->mentions_regex, $content ) ) return $content; $this->load_users(); $textarr = self::wp_html_split( $content ); foreach( $textarr as &$element ) { if ( '' == $element || '<' === $element[0] || false === strpos( $element, '@' ) ) { continue; } $element = preg_replace_callback( $this->mentions_regex, array( $this, 'mention_link_callback' ), $element ); } return implode( $textarr ); } /** * 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' => ( isset( $user->user_nicename ) ? $user->user_nicename : $user->display_name ), 'gravatar' => get_avatar( $user->user_email, 32 ), ); } return apply_filters( 'p2_user_suggestion', $js_users ); } }