1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Instagram PHP |
---|
5 | * @author Galen Grover <galenjr@gmail.com> |
---|
6 | * @license http://opensource.org/licenses/mit-license.php The MIT License |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | include_once(INSTAGRAM_ROOT.'/Comment.php'); |
---|
11 | include_once(INSTAGRAM_ROOT.'/CurrentUser.php'); |
---|
12 | include_once(INSTAGRAM_ROOT.'/Media.php'); |
---|
13 | |
---|
14 | |
---|
15 | /** |
---|
16 | * Helper class |
---|
17 | * |
---|
18 | * Example: |
---|
19 | * |
---|
20 | * $tags_closure = function($m){ |
---|
21 | * return sprintf( '<a href="?example=tag.php&tag=%s">%s</a>', $m[1], $m[0] ); |
---|
22 | * }; |
---|
23 | * |
---|
24 | * $mentions_closure = function($m){ |
---|
25 | * return sprintf( '<a href="?example=user.php&user=%s">%s</a>', $m[1], $m[0] ); |
---|
26 | * }; |
---|
27 | * |
---|
28 | * echo \Instagram\Helper::parseTagsAndMentions( $media->getCaption(), $tags_closure, $mentions_closure ) |
---|
29 | */ |
---|
30 | class Instagram_Helper { |
---|
31 | |
---|
32 | /** |
---|
33 | * Parse mentions in a string |
---|
34 | * |
---|
35 | * Finds mentions in a string (@mention) and applies a callback function each one |
---|
36 | * |
---|
37 | * @param string $text Text to parse |
---|
38 | * @param Closure $callback Function to apply to each mention |
---|
39 | * @return string Returns the text after the callback have been applied to each mention |
---|
40 | * @access public |
---|
41 | */ |
---|
42 | public static function parseMentions( $text, Closure $callback ) { |
---|
43 | return preg_replace_callback( '~@(.+?)(?=\b)~', $callback, $text ); |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Parse tags in a string |
---|
48 | * |
---|
49 | * Finds tags in a string (#username) and applies a callback function each one |
---|
50 | * |
---|
51 | * @param string $text Text to parse |
---|
52 | * @param Closure $callback Function to apply to each tag |
---|
53 | * @return string Returns the text after the callback have been applied to each tag |
---|
54 | * @access public |
---|
55 | */ |
---|
56 | public static function parseTags( $text, Closure $callback ) { |
---|
57 | return preg_replace_callback( '~#(.+?)(?=\b)~', $callback, $text ); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Parse mentions and tags in a string |
---|
62 | * |
---|
63 | * Finds mentions and tags in a string (@mention, #tag) and applies a callback function each one |
---|
64 | * |
---|
65 | * @param string $text Text to parse |
---|
66 | * @param Closure $tags_callback Function to apply to each tag |
---|
67 | * @param Closure $mentions_callback Function to apply to each mention |
---|
68 | * @return string Returns the text after the callbacks have been applied to tags and mentions |
---|
69 | * @access public |
---|
70 | */ |
---|
71 | public static function parseTagsAndMentions( $text, Closure $tags_callback, Closure $mentions_callback ) { |
---|
72 | $text = self::parseTags( $text, $tags_callback ); |
---|
73 | $text = self::parseMentions( $text, $mentions_callback ); |
---|
74 | return $text; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Is the comment deletable |
---|
79 | * |
---|
80 | * Checks if a comment is deletable by checking if the current user posted the comment |
---|
81 | * of if the comment was added to one of the current user's media |
---|
82 | * |
---|
83 | * @param Instagram_Comment $Instagram_Comment The comment |
---|
84 | * @param Instagram_Media $Instagram_Media The media the comment was added to |
---|
85 | * @param Instagram_CurrentUser $current_user Current user |
---|
86 | * @access public |
---|
87 | */ |
---|
88 | public function commentIsDeletable( Instagram_Comment $comment, Instagram_Media $media, Instagram_CurrentUser $current_user ) { |
---|
89 | return |
---|
90 | $comment->getUser()->getId() == $current_user->getId() || |
---|
91 | $media->getUser()->getId() == $current_user->getId(); |
---|
92 | } |
---|
93 | |
---|
94 | } |
---|
95 | ?> |
---|