| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\modules\follows\classes; |
| 4 |
|
| 5 |
class Actions { |
| 6 |
public function __construct() { |
| 7 |
$this->init_hooks(); |
| 8 |
} |
| 9 |
|
| 10 |
private function init_hooks() { |
| 11 |
add_action( 'wpforo_action_followconfirm', [ $this, 'confirmation' ] ); |
| 12 |
add_action( 'wpforo_action_unfollow', [ $this, 'unfollow' ] ); |
| 13 |
|
| 14 |
add_action( 'wpforo_after_add_topic', [ $this, 'after_add_topic' ], 7, 2 ); |
| 15 |
add_action( 'wpforo_after_add_post', [ $this, 'after_add_post' ], 7, 2 ); |
| 16 |
|
| 17 |
add_action( 'wpforo_post_approve', [ $this, 'after_post_approve' ], 7 ); |
| 18 |
add_action( 'wpforo_after_delete_user', [ $this, 'after_delete_user' ] ); |
| 19 |
|
| 20 |
## -- AJAX ACTIONS -- ## |
| 21 |
add_action( 'wp_ajax_wpforo_follow_unfollow_user', [ $this, 'follow_unfollow_user' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
public function confirmation() { |
| 25 |
if( $confirmkey = wpfval( WPF()->GET, 'key' ) ) WPF()->sbscrb->Follows->confirm( sanitize_text_field( $confirmkey ) ); |
| 26 |
|
| 27 |
wp_safe_redirect( wpforo_url( '', 'members' ) ); |
| 28 |
exit(); |
| 29 |
} |
| 30 |
|
| 31 |
public function unfollow() { |
| 32 |
if( $confirmkey = wpfval( WPF()->GET, 'key' ) ) { |
| 33 |
if( WPF()->sbscrb->Follows->unfollow_by_key( sanitize_text_field( $confirmkey ) ) ) { |
| 34 |
WPF()->notice->add( 'Unfollow Done', 'success' ); |
| 35 |
} else { |
| 36 |
WPF()->notice->add( 'Unfollow Error', 'error' ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
wp_safe_redirect( wpforo_url( '', 'members' ) ); |
| 41 |
exit(); |
| 42 |
} |
| 43 |
|
| 44 |
public function after_add_topic( $topic, $forum ) { |
| 45 |
if( ! wpfval( $topic, 'url' ) ) $topic['url'] = wpforo_topic( $topic['topicid'], 'url' ); |
| 46 |
if( ! wpfkey( $topic, 'body' ) ) $topic['body'] = wpforo_post( $topic['first_postid'], 'body' ); |
| 47 |
$owner = wpforo_member( $topic ); |
| 48 |
|
| 49 |
$follows = WPF()->sbscrb->Follows->get_follows( [ 'itemid' => $topic['userid'], 'itemtype' => 'user', 'active' => true ] ); |
| 50 |
foreach( $follows as $follow ) { |
| 51 |
if( $follow['userid'] ) { |
| 52 |
$user = WPF()->member->get_member( $follow['userid'] ); |
| 53 |
} elseif( $follow['email'] ) { |
| 54 |
$user = [ 'groupid' => 4, 'groupids' => [ 4 ], 'display_name' => ( $follow['name'] ?: $follow['email'] ), 'user_email' => $follow['email'] ]; |
| 55 |
} else { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
if( apply_filters( 'break_wpforo_follow_send_email_after_add_topic', false, $user, $owner, $topic, $forum ) ) continue; |
| 60 |
|
| 61 |
$key = 'topicid_' . $topic['topicid'] . '_user_email_' . $user['user_email']; |
| 62 |
if( ! WPF()->ram_cache->exists( $key ) ) { |
| 63 |
|
| 64 |
if( in_array( wpfval( $user, 'status' ), [ 'banned', 'inactive' ], true ) ) continue; |
| 65 |
if( wpforo_is_users_same( $owner, $user ) ) continue; |
| 66 |
if( wpforo_is_users_same( $user ) ) continue; |
| 67 |
if( ! WPF()->topic->view_access( $topic, $user ) ) continue; |
| 68 |
|
| 69 |
$sbj_msg = WPF()->sbscrb->get_sbj_msg( 'user_follow', $forum, $topic, $owner, $user, $follow['unfollow_link'] ); |
| 70 |
wpforo_send_email( $user['user_email'], $sbj_msg['sbj'], $sbj_msg['msg'], '', 'follow', (int) $topic['topicid'] ); |
| 71 |
|
| 72 |
WPF()->ram_cache->set( $key, true ); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public function after_add_post( $post, $topic ) { |
| 78 |
$owner = wpforo_member( $post ); |
| 79 |
$follows = WPF()->sbscrb->Follows->get_follows( [ 'itemid' => $post['userid'], 'itemtype' => 'user', 'active' => true ] ); |
| 80 |
foreach( $follows as $follow ) { |
| 81 |
if( $follow['userid'] ) { |
| 82 |
$user = WPF()->member->get_member( $follow['userid'] ); |
| 83 |
} elseif( $follow['user_email'] ) { |
| 84 |
$user = [ 'groupid' => 4, 'groupids' => [ 4 ], 'display_name' => ( $follow['name'] ?: $follow['email'] ), 'user_email' => $follow['email'] ]; |
| 85 |
} else { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
if( apply_filters( 'break_wpforo_follow_send_email_after_add_post', false, $user, $owner, $post, $topic ) ) continue; |
| 90 |
|
| 91 |
$key = 'postid_' . $post['postid'] . '_user_email_' . $user['user_email']; |
| 92 |
if( ! WPF()->ram_cache->exists( $key ) ) { |
| 93 |
|
| 94 |
if( in_array( wpfval( $user, 'status' ), [ 'banned', 'inactive' ], true ) ) continue; |
| 95 |
if( wpforo_is_users_same( $owner, $user ) ) continue; |
| 96 |
if( wpforo_is_users_same( $user ) ) continue; |
| 97 |
if( ! WPF()->post->view_access( $post, $user ) ) continue; |
| 98 |
|
| 99 |
$sbj_msg = WPF()->sbscrb->get_sbj_msg( 'user_follow', $topic, $post, $owner, $user, $follow['unfollow_link'] ); |
| 100 |
wpforo_send_email( $user['user_email'], $sbj_msg['sbj'], $sbj_msg['msg'], '', 'follow', (int) $post['postid'] ); |
| 101 |
|
| 102 |
WPF()->ram_cache->set( $key, true ); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function after_post_approve( $post ) { |
| 108 |
if( ( $topicid = wpforo_bigintval( wpfval( $post, 'topicid' ) ) ) && ( $topic = WPF()->topic->get_topic( $topicid ) ) ) { |
| 109 |
$this->after_add_post( $post, $topic ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public function after_delete_user( $userid ) { |
| 114 |
WPF()->sbscrb->Follows->delete( [ 'itemid' => $userid, 'itemtype' => 'user' ] ); |
| 115 |
WPF()->sbscrb->Follows->delete( [ 'userid' => $userid ] ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
### -- AJAX ACTIONS -- ### |
| 120 |
|
| 121 |
public function follow_unfollow_user() { |
| 122 |
wpforo_verify_nonce( 'wpforo_follow_unfollow_user' ); |
| 123 |
if( WPF()->current_userid && ( $userid = wpforo_bigintval( wpfval( $_POST, 'userid' ) ) ) ) { |
| 124 |
if( WPF()->current_userid !== $userid ) { |
| 125 |
if( (int) wpfval( $_POST, 'stat' ) ) { |
| 126 |
if( WPF()->sbscrb->Follows->follow( WPF()->current_userid, $userid ) ) { |
| 127 |
wp_send_json_success( [ |
| 128 |
'stat' => 1, |
| 129 |
'phrase' => wpforo_phrase( 'Unfollow', false ), |
| 130 |
'followers_count' => WPF()->sbscrb->Follows->get_count( [ 'itemid' => $userid, 'itemtype' => 'user', 'active' => true ] ), |
| 131 |
'notice' => wpforo_phrase( 'done', false ), |
| 132 |
] ); |
| 133 |
} |
| 134 |
} else { |
| 135 |
if( WPF()->sbscrb->Follows->unfollow( WPF()->current_userid, $userid ) ) { |
| 136 |
wp_send_json_success( [ |
| 137 |
'stat' => 0, |
| 138 |
'phrase' => wpforo_phrase( 'Follow', false ), |
| 139 |
'followers_count' => WPF()->sbscrb->Follows->get_count( [ 'itemid' => $userid, 'itemtype' => 'user', 'active' => true ] ), |
| 140 |
'notice' => wpforo_phrase( 'done', false ), |
| 141 |
] ); |
| 142 |
} |
| 143 |
} |
| 144 |
} else { |
| 145 |
wp_send_json_error( [ 'notice' => wpforo_phrase( 'Self following not allowed!', false ) ] ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
wp_send_json_error( [ 'notice' => wpforo_phrase( 'You are not logged in or sent wrong data!', false ) ] ); |
| 150 |
} |
| 151 |
} |
| 152 |
|