| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\modules\reactions\classes; |
| 4 |
|
| 5 |
use wpforo\modules\reactions\Reactions; |
| 6 |
|
| 7 |
class Template { |
| 8 |
public function __construct() { |
| 9 |
$this->init_hooks(); |
| 10 |
} |
| 11 |
|
| 12 |
private function init_hooks() { |
| 13 |
add_action( 'wpforo_post_footer_start', function( $post ) { |
| 14 |
echo $this->like_button( $post ); |
| 15 |
} ); |
| 16 |
add_action( 'wpforo_post_content_end', function( $post ) { |
| 17 |
echo $this->like_button( $post ); |
| 18 |
} ); |
| 19 |
add_action( 'wpforo_post_bottom_start', function( $post, $topic, $forum, $layout ) { |
| 20 |
if( $layout === 1 ) echo $this->like_button( $post ) . ' '; |
| 21 |
}, 1, 4 ); |
| 22 |
add_action( 'wpforo_post_bottom_start', function( $post, $topic, $forum, $layout ) { |
| 23 |
if( $layout === 1 ) echo '<div class="reacted-users">' . $this->likers( $post['postid'] ) . '</div>'; |
| 24 |
}, 2, 4 ); |
| 25 |
add_action( 'wpforo_post_bottom_end', function( $post ) { |
| 26 |
echo '<div class="reacted-users">' . $this->likers( $post['postid'] ) . '</div>'; |
| 27 |
} ); |
| 28 |
add_action( 'wpforo_post_footer_bottom_start', function( $post ) { |
| 29 |
echo '<div class="reacted-users">' . $this->likers( $post['postid'] ) . '</div>'; |
| 30 |
} ); |
| 31 |
} |
| 32 |
|
| 33 |
private function get_button_by_type( $type ) { |
| 34 |
$types = Reactions::get_types(); |
| 35 |
if( $_type = wpfval( $types, $type ) ) return $_type['icon']; |
| 36 |
|
| 37 |
return '<i class="far fa-thumbs-up"></i>'; |
| 38 |
} |
| 39 |
|
| 40 |
public function like_button( $post, $userid = 0 ): string { |
| 41 |
if( ! WPF()->current_userid || wpforo_is_owner( $post['userid'], $post['email'] ) || ! WPF()->perm->forum_can( 'l', (int) wpfval( $post, 'forumid' ) ) ) return ''; |
| 42 |
$reaction = WPF()->reaction->get_user_reaction( $post['postid'], $userid ); |
| 43 |
$type = wpfval( $reaction, 'type' ); |
| 44 |
$all = []; |
| 45 |
foreach( array_reverse( Reactions::get_types() ) as $key => $_type ) { |
| 46 |
$all[ $key ] = sprintf( |
| 47 |
'<span class="%1$s wpf-react-%2$s" data-type="%2$s">%3$s</span>', |
| 48 |
( $type !== $key ? 'wpf-react' : '' ), |
| 49 |
$key, |
| 50 |
$_type['icon'] |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
return sprintf( |
| 55 |
'<div class="wpf-reaction-wrap"><div class="wpforo-reaction wpf-popover" aria-haspopup="true" data-currentstate="%1$s"> |
| 56 |
<span class="wpf-current-reaction %2$s" data-type="%1$s">%3$s</span> |
| 57 |
<div class="wpf-popover-content">%4$s</div> |
| 58 |
</div></div>', |
| 59 |
$type, |
| 60 |
( ! $type ? 'wpf-react wpf-unreacted' : 'wpf-unreact wpf-react-' . $type ), |
| 61 |
$this->get_button_by_type( $type ), |
| 62 |
implode( '', $all ) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
public function likers( $postid ): string { |
| 67 |
if( ! $postid ) return ''; |
| 68 |
|
| 69 |
$likers = apply_filters( 'wpforo_reactions_likers_start', null, $postid ); |
| 70 |
if( ! is_null( $likers ) ) return (string) $likers; |
| 71 |
|
| 72 |
$l_count = wpforo_post( $postid, 'likes_count' ); |
| 73 |
$l_usernames = wpforo_post( $postid, 'likers_usernames' ); |
| 74 |
$return = ''; |
| 75 |
|
| 76 |
if( $l_count && $l_usernames ) { |
| 77 |
if( $l_usernames[0]['userid'] == WPF()->current_userid ) $l_usernames[0]['dname'] = wpforo_phrase( 'You', false ); |
| 78 |
if( $l_count === 1 ) { |
| 79 |
$return = sprintf( |
| 80 |
wpforo_phrase( '%s reacted', false ), |
| 81 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[0]['userid'] ), '', 30, '', false, $l_usernames[0]['dname'] ) |
| 82 |
); |
| 83 |
} elseif( $l_count == 2 ) { |
| 84 |
$return = sprintf( |
| 85 |
wpforo_phrase( '%s and %s reacted', false ), |
| 86 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[0]['userid'] ), '', 30, '', false, $l_usernames[0]['dname'] ), |
| 87 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[1]['userid'] ), '', 30, '', false, $l_usernames[1]['dname'] ) |
| 88 |
); |
| 89 |
} elseif( $l_count === 3 ) { |
| 90 |
$return = sprintf( |
| 91 |
wpforo_phrase( '%s, %s and %s reacted', false ), |
| 92 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[0]['userid'] ), '', 30, '', false, $l_usernames[0]['dname'] ), |
| 93 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[1]['userid'] ), '', 30, '', false, $l_usernames[1]['dname'] ), |
| 94 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[2]['userid'] ), '', 30, '', false, $l_usernames[2]['dname'] ) |
| 95 |
); |
| 96 |
} elseif( $l_count >= 4 ) { |
| 97 |
$return = sprintf( |
| 98 |
wpforo_phrase( '%s, %s, %s and %d people reacted', false ), |
| 99 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[0]['userid'] ), '', 30, '', false, $l_usernames[0]['dname'] ), |
| 100 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[1]['userid'] ), '', 30, '', false, $l_usernames[1]['dname'] ), |
| 101 |
wpforo_member_link( WPF()->member->get_member( $l_usernames[2]['userid'] ), '', 30, '', false, $l_usernames[2]['dname'] ), |
| 102 |
( $l_count - 3 ) |
| 103 |
); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return (string) apply_filters( 'wpforo_reactions_likers_end', $return, $postid, $l_count, $l_usernames ); |
| 108 |
} |
| 109 |
} |
| 110 |
|