BP_Media_Extractor::extract_mentions( string $richtext, string $plaintext, array $extra_args = array() )

Extract @mentions tags from text.


Description Description

If the Activity component is enabled, it is used to parse @mentions. The mentioned "name" must match a user account, otherwise it is discarded.

If the Activity component is disabled, any @mentions are extracted.


Parameters Parameters

$richtext

(Required) Content to parse.

$plaintext

(Required) Sanitized version of the content.

$extra_args

(Optional) Bespoke data for a particular extractor.

Default value: array()


Top ↑

Return Return

(array)

  • 'has'
    (array) Extracted media counts. {
  • 'mentions'
    (int)
  • 'mentions'
    (array) Extracted mentions. { Array of extracted media.
  • 'name'
    (string) @mention.
  • 'user_id'
    (string) User ID. Optional, only set if Activity component enabled.

  • Top ↑

    Source Source

    File: bp-core/classes/class-bp-media-extractor.php

    	protected function extract_mentions( $richtext, $plaintext, $extra_args = array() ) {
    		$data     = array( 'has' => array( 'mentions' => 0 ), 'mentions' => array() );
    		$mentions = array();
    
    		// If the Activity component is active, use it to parse @mentions.
    		if ( bp_is_active( 'activity' ) ) {
    			$mentions = bp_activity_find_mentions( $plaintext );
    			if ( ! $mentions ) {
    				$mentions = array();
    			}
    
    		// If the Activity component is disabled, instead do a basic parse.
    		} else {
    			if ( strpos( $plaintext, '@' ) !== false ) {
    				preg_match_all( '/[@]+([A-Za-z0-9-_\.@]+)\b/', $plaintext, $matches );
    
    				if ( ! empty( $matches[1] ) ) {
    					$mentions = array_unique( array_map( 'strtolower', $matches[1] ) );
    				}
    			}
    		}
    
    		// Build results.
    		foreach ( $mentions as $user_id => $mention_name ) {
    			$mention = array( 'name' => strtolower( $mention_name ) );
    
    			// If the Activity component is active, store the User ID, too.
    			if ( bp_is_active( 'activity' ) ) {
    				$mention['user_id'] = (int) $user_id;
    			}
    
    			$data['mentions'][] = $mention;
    		}
    
    		$data['has']['mentions'] = count( $data['mentions'] );
    
    		/**
    		 * Filters @mentions extracted from text.
    		 *
    		 * @since 2.3.0
    		 *
    		 * @param array  $data       Extracted @mentions. See {@link BP_Media_Extractor::extract_mentions()} for format.
    		 * @param string $richtext   Content to parse.
    		 * @param string $plaintext  Copy of $richtext without any markup.
    		 * @param array  $extra_args Bespoke data for a particular extractor (optional).
    		 */
    		return apply_filters( 'bp_media_extractor_mentions', $data, $richtext, $plaintext, $extra_args );
    	}
    

    Top ↑

    Changelog Changelog

    Changelog
    Version Description
    2.3.0 Introduced.

    Top ↑

    User Contributed Notes User Contributed Notes

    You must log in before being able to contribute a note or feedback.