5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
16 * $tags_closure = function($m){
17 * return sprintf( '<a href="?example=tag.php&tag=%s">%s</a>', $m[1], $m[0] );
20 * $mentions_closure = function($m){
21 * return sprintf( '<a href="?example=user.php&user=%s">%s</a>', $m[1], $m[0] );
24 * echo \Instagram\Helper::parseTagsAndMentions( $media->getCaption(), $tags_closure, $mentions_closure )
29 * Parse mentions in a string
31 * Finds mentions in a string (@mention) and applies a callback function each one
33 * @param string $text Text to parse
34 * @param \Closure $callback Function to apply to each mention
35 * @return string Returns the text after the callback have been applied to each mention
38 public static function parseMentions( $text, \Closure $callback ) {
39 return preg_replace_callback( '~@(.+?)(?=\b)~', $callback, $text );
43 * Parse tags in a string
45 * Finds tags in a string (#username) and applies a callback function each one
47 * @param string $text Text to parse
48 * @param \Closure $callback Function to apply to each tag
49 * @return string Returns the text after the callback have been applied to each tag
52 public static function parseTags( $text, \Closure $callback ) {
53 return preg_replace_callback( '~#(.+?)(?=\b)~', $callback, $text );
57 * Parse mentions and tags in a string
59 * Finds mentions and tags in a string (@mention, #tag) and applies a callback function each one
61 * @param string $text Text to parse
62 * @param \Closure $tags_callback Function to apply to each tag
63 * @param \Closure $mentions_callback Function to apply to each mention
64 * @return string Returns the text after the callbacks have been applied to tags and mentions
67 public static function parseTagsAndMentions( $text, \Closure $tags_callback, \Closure $mentions_callback ) {
68 $text = self::parseTags( $text, $tags_callback );
69 $text = self::parseMentions( $text, $mentions_callback );
74 * Is the comment deletable
76 * Checks if a comment is deletable by checking if the current user posted the comment
77 * or if the comment was added to one of the current user's media
79 * @param \Instagram\Comment $comment The comment
80 * @param \Instagram\Media $media The media the comment was added to
81 * @param \Instagram\CurrentUser $current_user Current user
84 public static function commentIsDeletable( \Instagram\Comment $comment, \Instagram\Media $media, \Instagram\CurrentUser $current_user ) {
86 $comment->getUser()->getId() == $current_user->getId() ||
87 $media->getUser()->getId() == $current_user->getId();