PluginProbe
wpForo Forum / 3.0.9
wpForo Forum v3.0.9
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / modules / reactions / classes / Actions.php

Actions.php in wpForo Forum 3.0.9, at modules/reactions/classes/Actions.php

184 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace wpforo\modules\reactions\classes;
4
5 use wpforo\modules\reactions\Reactions;
6
7 class Actions {
8 public function __construct() {
9 $this->init_hooks();
10 }
11
12 private function init_hooks() {
13 add_action( 'wpforo_after_delete_user', [ $this, 'after_delete_user' ], 10, 2 );
14 add_action( 'wpforo_after_delete_post', [ $this, 'after_delete_post' ] );
15 add_filter( 'wpforo_member_get_points_other_points', [ $this, 'calc_member_points' ], 10, 2 );
16
17 #### -- AJAX ACTIONS -- ###
18 add_action( 'wp_ajax_wpforo_vote_ajax', [ $this, 'vote' ] );
19
20 add_action( 'wp_ajax_wpforo_react', [ $this, 'react' ] );
21 add_action( 'wp_ajax_wpforo_unreact', [ $this, 'unreact' ] );
22 }
23
24 public function after_delete_user( $userid, $reassign ) {
25 if( $reassign ) WPF()->reaction->edit_for_all_active_boards( [ 'post_userid' => $reassign ], [ 'post_userid' => $userid ] );
26 WPF()->reaction->delete_for_all_active_boards( [ 'userid' => $userid ] );
27 }
28
29 public function after_delete_post( $post ) {
30 WPF()->reaction->delete( [ 'postid' => $post['postid'] ] );
31 }
32
33 public function calc_member_points( $other_points, $member ) {
34 $reaction_types = Reactions::get_types();
35 $rpoints = 0;
36 foreach( wpfval( $member, 'reactions_in' ) as $key => $point ) {
37 if( ! ( $point = intval( $point ) ) || in_array( $key, [ 'up', 'down' ] ) ) continue;
38 if( $type = wpfval( $reaction_types, $key ) ) $rpoints += ( $point * floatval( $type['reaction'] ) );
39 }
40
41 return $other_points + $rpoints;
42 }
43
44 public function vote() {
45 wpforo_verify_nonce( 'wpforo_vote_ajax' );
46 if( ! WPF()->current_userid ) {
47 WPF()->notice->add( wpforo_get_login_or_register_notice_text() );
48 wp_send_json_error( [ 'notice' => WPF()->notice->get_notices() ] );
49 }
50
51 if( ! $postid = wpforo_bigintval( wpfval( $_POST, 'postid' ) ) ) {
52 WPF()->notice->add( 'Wrong post data', 'error' );
53 wp_send_json_error( [ 'notice' => WPF()->notice->get_notices() ] );
54 }
55
56 $votestatus = wpfval( $_POST, 'votestatus' );
57 if( $votestatus === 'clear' ) {
58 $votes = WPF()->post->clear_user_post_votes( $postid );
59 WPF()->notice->add( 'You have removed your vote', 'success' );
60 wp_send_json_success( [ 'votes' => $votes, 'notice' => WPF()->notice->get_notices() ] );
61 } else {
62 $reaction = $votestatus === 'down' ? - 1 : 1;
63
64 if( WPF()->post->has_user_voted_post( $postid, $reaction ) ) {
65 WPF()->notice->add( 'You are already voted this post' );
66 wp_send_json_error( [ 'notice' => WPF()->notice->get_notices() ] );
67 } else {
68 WPF()->post->clear_user_post_votes( $postid );
69 }
70
71 if( false !== ( $votes = WPF()->post->vote_post( $postid, $reaction ) ) ) {
72 wp_send_json_success( [ 'votes' => $votes, 'notice' => WPF()->notice->get_notices() ] );
73 }
74 }
75
76 WPF()->notice->add( 'Wrong post data', 'error' );
77 wp_send_json_error( [ 'notice' => WPF()->notice->get_notices() ] );
78 }
79
80 public function react() {
81 wpforo_verify_nonce( 'wpforo_react' );
82 if( ( $userid = WPF()->current_userid ) && ( $postid = wpfval( $_POST, 'postid' ) ) ) {
83 $types = Reactions::get_types();
84 if( ! ( $type = wpfval( $_POST, 'type' ) ) || ! wpfkey( $types, $type ) ) $type = 'up';
85
86 $is_reacted = WPF()->reaction->is_reacted( $postid, $userid, $type );
87 if( ! $is_reacted ) {
88 $post = wpforo_post( $postid );
89 $_type = wpfval( $types, $type );
90 $reaction = [
91 'postid' => $postid,
92 'post_userid' => wpforo_bigintval( wpfval( $post, 'userid' ) ),
93 'userid' => $userid,
94 'type' => $type,
95 'reaction' => (int) wpfval( $_type, 'reaction' ),
96 ];
97 WPF()->reaction->delete(
98 [
99 'postid' => $postid,
100 'userid' => $userid,
101 ]
102 );
103 if( WPF()->reaction->add( $reaction ) ) {
104 wpforo_clean_cache( 'post-soft', $postid );
105
106 do_action( 'wpforo_react_post', $reaction, $post );
107
108 ##############################
109 if( $type === 'up' ) {
110 /*
111 * @deprecated 2.3.4
112 */
113 do_action( 'wpforo_undo_dislike', $post, WPF()->current_userid );
114 /*
115 * @deprecated 2.3.4
116 */
117 do_action( 'wpforo_like', $post, WPF()->current_userid );
118 } elseif( $type === 'down' ) {
119 /*
120 * @deprecated 2.3.4
121 */
122 do_action( 'wpforo_undo_like', $post, WPF()->current_userid );
123 /*
124 * @deprecated 2.3.4
125 */
126 do_action( 'wpforo_dislike', $post, WPF()->current_userid );
127 } else {
128 //unknown like type
129 /*
130 * @deprecated 2.3.4
131 */
132 do_action( 'wpforo_like', $post, WPF()->current_userid );
133 }
134 #######################
135
136 wp_send_json_success(
137 [
138 'like_button' => WPF()->reaction->Template->like_button( $post, $userid ),
139 'likers' => WPF()->reaction->Template->likers( $postid ),
140 ]
141 );
142 }
143 }
144 }
145
146 wp_send_json_error();
147 }
148
149 public function unreact() {
150 wpforo_verify_nonce( 'wpforo_unreact' );
151 if( ( $userid = WPF()->current_userid ) && ( $postid = wpfval( $_POST, 'postid' ) ) ) {
152 $post = wpforo_post( $postid );
153 $args = [
154 'postid' => $postid,
155 'userid' => $userid,
156 ];
157 WPF()->reaction->delete( $args );
158 wpforo_clean_cache( 'post-soft', $postid );
159
160 do_action( 'wpforo_unreact_post', $args );
161
162 #################
163 /*
164 * @deprecated 2.3.4
165 */
166 do_action( 'wpforo_undo_like', $post, WPF()->current_userid );
167 /*
168 * @deprecated 2.3.4
169 */
170 do_action( 'wpforo_undo_dislike', $post, WPF()->current_userid );
171 #################
172
173 wp_send_json_success(
174 [
175 'like_button' => WPF()->reaction->Template->like_button( $post, $userid ),
176 'likers' => WPF()->reaction->Template->likers( $postid ),
177 ]
178 );
179 }
180
181 wp_send_json_error();
182 }
183 }
184