PageRenderTime 36ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/bp-templates/bp-nouveau/includes/groups/ajax.php

https://github.com/boonebgorges/BuddyPress
PHP | 485 lines | 357 code | 79 blank | 49 comment | 55 complexity | 627b7021d6ce4787bb96674d2b4bdaf8 MD5 | raw file
  1. <?php
  2. /**
  3. * Groups Ajax functions
  4. *
  5. * @since 3.0.0
  6. * @version 3.1.0
  7. */
  8. // Exit if accessed directly.
  9. defined( 'ABSPATH' ) || exit;
  10. add_action( 'admin_init', function() {
  11. $ajax_actions = array(
  12. array( 'groups_filter' => array( 'function' => 'bp_nouveau_ajax_object_template_loader', 'nopriv' => true ) ),
  13. array( 'groups_join_group' => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  14. array( 'groups_leave_group' => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  15. array( 'groups_accept_invite' => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  16. array( 'groups_reject_invite' => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  17. array( 'groups_request_membership' => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  18. array( 'groups_get_group_potential_invites' => array( 'function' => 'bp_nouveau_ajax_get_users_to_invite', 'nopriv' => false ) ),
  19. array( 'groups_send_group_invites' => array( 'function' => 'bp_nouveau_ajax_send_group_invites', 'nopriv' => false ) ),
  20. array( 'groups_delete_group_invite' => array( 'function' => 'bp_nouveau_ajax_remove_group_invite', 'nopriv' => false ) ),
  21. );
  22. foreach ( $ajax_actions as $ajax_action ) {
  23. $action = key( $ajax_action );
  24. add_action( 'wp_ajax_' . $action, $ajax_action[ $action ]['function'] );
  25. if ( ! empty( $ajax_action[ $action ]['nopriv'] ) ) {
  26. add_action( 'wp_ajax_nopriv_' . $action, $ajax_action[ $action ]['function'] );
  27. }
  28. }
  29. }, 12 );
  30. /**
  31. * Join or leave a group when clicking the "join/leave" button via a POST request.
  32. *
  33. * @since 3.0.0
  34. *
  35. * @return string HTML
  36. */
  37. function bp_nouveau_ajax_joinleave_group() {
  38. $response = array(
  39. 'feedback' => sprintf(
  40. '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  41. esc_html__( 'There was a problem performing this action. Please try again.', 'buddypress' )
  42. ),
  43. );
  44. // Bail if not a POST action.
  45. if ( ! bp_is_post_request() || empty( $_POST['action'] ) ) {
  46. wp_send_json_error( $response );
  47. }
  48. if ( empty( $_POST['nonce'] ) || empty( $_POST['item_id'] ) || ! bp_is_active( 'groups' ) ) {
  49. wp_send_json_error( $response );
  50. }
  51. // Use default nonce
  52. $nonce = $_POST['nonce'];
  53. $check = 'bp_nouveau_groups';
  54. // Use a specific one for actions needed it
  55. if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
  56. $nonce = $_POST['_wpnonce'];
  57. $check = $_POST['action'];
  58. }
  59. // Nonce check!
  60. if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
  61. wp_send_json_error( $response );
  62. }
  63. // Cast gid as integer.
  64. $group_id = (int) $_POST['item_id'];
  65. $errors = array(
  66. 'cannot' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>', esc_html__( 'You cannot join this group.', 'buddypress' ) ),
  67. 'member' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>', esc_html__( 'You are already a member of the group.', 'buddypress' ) ),
  68. );
  69. if ( groups_is_user_banned( bp_loggedin_user_id(), $group_id ) ) {
  70. $response['feedback'] = $errors['cannot'];
  71. wp_send_json_error( $response );
  72. }
  73. // Validate and get the group
  74. $group = groups_get_group( array( 'group_id' => $group_id ) );
  75. if ( empty( $group->id ) ) {
  76. wp_send_json_error( $response );
  77. }
  78. // Manage all button's possible actions here.
  79. switch ( $_POST['action'] ) {
  80. case 'groups_accept_invite':
  81. if ( ! groups_accept_invite( bp_loggedin_user_id(), $group_id ) ) {
  82. $response = array(
  83. 'feedback' => sprintf(
  84. '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  85. esc_html__( 'Group invitation could not be accepted.', 'buddypress' )
  86. ),
  87. 'type' => 'error',
  88. );
  89. } else {
  90. if ( bp_is_active( 'activity' ) ) {
  91. groups_record_activity(
  92. array(
  93. 'type' => 'joined_group',
  94. 'item_id' => $group->id,
  95. )
  96. );
  97. }
  98. // User is now a member of the group
  99. $group->is_member = '1';
  100. $response = array(
  101. 'feedback' => sprintf(
  102. '<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  103. esc_html__( 'Group invite accepted.', 'buddypress' )
  104. ),
  105. 'type' => 'success',
  106. 'is_user' => bp_is_user(),
  107. 'contents' => bp_get_group_join_button( $group ),
  108. 'is_group' => bp_is_group(),
  109. );
  110. }
  111. break;
  112. case 'groups_reject_invite':
  113. if ( ! groups_reject_invite( bp_loggedin_user_id(), $group_id ) ) {
  114. $response = array(
  115. 'feedback' => sprintf(
  116. '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  117. esc_html__( 'Group invite could not be rejected', 'buddypress' )
  118. ),
  119. 'type' => 'error',
  120. );
  121. } else {
  122. $response = array(
  123. 'feedback' => sprintf(
  124. '<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  125. esc_html__( 'Group invite rejected', 'buddypress' )
  126. ),
  127. 'type' => 'success',
  128. 'is_user' => bp_is_user(),
  129. );
  130. }
  131. break;
  132. case 'groups_join_group':
  133. if ( groups_is_user_member( bp_loggedin_user_id(), $group->id ) ) {
  134. $response = array(
  135. 'feedback' => $errors['member'],
  136. 'type' => 'error',
  137. );
  138. } elseif ( 'public' !== $group->status ) {
  139. $response = array(
  140. 'feedback' => $errors['cannot'],
  141. 'type' => 'error',
  142. );
  143. } elseif ( ! groups_join_group( $group->id ) ) {
  144. $response = array(
  145. 'feedback' => sprintf(
  146. '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  147. esc_html__( 'Error joining this group.', 'buddypress' )
  148. ),
  149. 'type' => 'error',
  150. );
  151. } else {
  152. // User is now a member of the group
  153. $group->is_member = '1';
  154. $response = array(
  155. 'contents' => bp_get_group_join_button( $group ),
  156. 'is_group' => bp_is_group(),
  157. 'type' => 'success',
  158. );
  159. }
  160. break;
  161. case 'groups_request_membership' :
  162. if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
  163. $response = array(
  164. 'feedback' => sprintf(
  165. '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  166. esc_html__( 'Error requesting membership.', 'buddypress' )
  167. ),
  168. 'type' => 'error',
  169. );
  170. } else {
  171. // Request is pending
  172. $group->is_pending = '1';
  173. $response = array(
  174. 'contents' => bp_get_group_join_button( $group ),
  175. 'is_group' => bp_is_group(),
  176. 'type' => 'success',
  177. );
  178. }
  179. break;
  180. case 'groups_leave_group' :
  181. if ( ! groups_leave_group( $group->id ) ) {
  182. $response = array(
  183. 'feedback' => sprintf(
  184. '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  185. esc_html__( 'Error leaving group.', 'buddypress' )
  186. ),
  187. 'type' => 'error',
  188. );
  189. } else {
  190. // User is no more a member of the group
  191. $group->is_member = '0';
  192. $bp = buddypress();
  193. /**
  194. * When inside the group or in the loggedin user's group memberships screen
  195. * we need to reload the page.
  196. */
  197. $bp_is_group = bp_is_group() || ( bp_is_user_groups() && bp_is_my_profile() );
  198. $response = array(
  199. 'contents' => bp_get_group_join_button( $group ),
  200. 'is_group' => $bp_is_group,
  201. 'type' => 'success',
  202. );
  203. // Reset the message if not in a Group or in a loggedin user's group memberships one!
  204. if ( ! $bp_is_group && isset( $bp->template_message ) && isset( $bp->template_message_type ) ) {
  205. unset( $bp->template_message, $bp->template_message_type );
  206. @setcookie( 'bp-message', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
  207. @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
  208. }
  209. }
  210. break;
  211. }
  212. if ( 'error' === $response['type'] ) {
  213. wp_send_json_error( $response );
  214. }
  215. wp_send_json_success( $response );
  216. }
  217. /**
  218. * @since 3.0.0
  219. */
  220. function bp_nouveau_ajax_get_users_to_invite() {
  221. $bp = buddypress();
  222. $response = array(
  223. 'feedback' => __( 'There was a problem performing this action. Please try again.', 'buddypress' ),
  224. 'type' => 'error',
  225. );
  226. if ( empty( $_POST['nonce'] ) ) {
  227. wp_send_json_error( $response );
  228. }
  229. // Use default nonce
  230. $nonce = $_POST['nonce'];
  231. $check = 'bp_nouveau_groups';
  232. // Use a specific one for actions needed it
  233. if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
  234. $nonce = $_POST['_wpnonce'];
  235. $check = $_POST['action'];
  236. }
  237. // Nonce check!
  238. if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
  239. wp_send_json_error( $response );
  240. }
  241. $request = bp_parse_args(
  242. $_POST,
  243. array(
  244. 'scope' => 'members',
  245. ),
  246. 'nouveau_ajax_get_users_to_invite'
  247. );
  248. $bp->groups->invites_scope = 'members';
  249. $message = __( 'Select members to invite by clicking the + button. Once you\'ve made your selection, use the "Send Invites" navigation item to continue.', 'buddypress' );
  250. if ( 'friends' === $request['scope'] ) {
  251. $request['user_id'] = bp_loggedin_user_id();
  252. $bp->groups->invites_scope = 'friends';
  253. $message = __( 'Select friends to invite by clicking the + button. Once you\'ve made your selection, use the "Send Invites" navigation item to continue.', 'buddypress' );
  254. }
  255. if ( 'invited' === $request['scope'] ) {
  256. if ( ! bp_group_has_invites( array( 'user_id' => 'any' ) ) ) {
  257. wp_send_json_error( array(
  258. 'feedback' => __( 'No pending group invitations found.', 'buddypress' ),
  259. 'type' => 'info',
  260. ) );
  261. }
  262. $request['is_confirmed'] = false;
  263. $bp->groups->invites_scope = 'invited';
  264. $message = __( 'You can view the group\'s pending invitations from this screen.', 'buddypress' );
  265. }
  266. $potential_invites = bp_nouveau_get_group_potential_invites( $request );
  267. if ( empty( $potential_invites->users ) ) {
  268. $error = array(
  269. 'feedback' => __( 'No members were found. Try another filter.', 'buddypress' ),
  270. 'type' => 'info',
  271. );
  272. if ( 'friends' === $bp->groups->invites_scope ) {
  273. $error = array(
  274. 'feedback' => __( 'All your friends are already members of this group, or have already received an invite to join this group, or have requested to join it.', 'buddypress' ),
  275. 'type' => 'info',
  276. );
  277. if ( 0 === (int) bp_get_total_friend_count( bp_loggedin_user_id() ) ) {
  278. $error = array(
  279. 'feedback' => __( 'You have no friends!', 'buddypress' ),
  280. 'type' => 'info',
  281. );
  282. }
  283. }
  284. unset( $bp->groups->invites_scope );
  285. wp_send_json_error( $error );
  286. }
  287. $potential_invites->users = array_map( 'bp_nouveau_prepare_group_potential_invites_for_js', array_values( $potential_invites->users ) );
  288. $potential_invites->users = array_filter( $potential_invites->users );
  289. // Set a message to explain use of the current scope
  290. $potential_invites->feedback = $message;
  291. unset( $bp->groups->invites_scope );
  292. wp_send_json_success( $potential_invites );
  293. }
  294. /**
  295. * @since 3.0.0
  296. */
  297. function bp_nouveau_ajax_send_group_invites() {
  298. $bp = buddypress();
  299. $response = array(
  300. 'feedback' => __( 'Invites could not be sent. Please try again.', 'buddypress' ),
  301. 'type' => 'error',
  302. );
  303. // Verify nonce
  304. if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_send_invites' ) ) {
  305. wp_send_json_error( $response );
  306. }
  307. $group_id = bp_get_current_group_id();
  308. if ( bp_is_group_create() && ! empty( $_POST['group_id'] ) ) {
  309. $group_id = (int) $_POST['group_id'];
  310. }
  311. if ( ! bp_groups_user_can_send_invites( $group_id ) ) {
  312. $response['feedback'] = __( 'You are not allowed to send invitations for this group.', 'buddypress' );
  313. wp_send_json_error( $response );
  314. }
  315. if ( empty( $_POST['users'] ) ) {
  316. wp_send_json_error( $response );
  317. }
  318. // For feedback
  319. $invited = array();
  320. foreach ( (array) $_POST['users'] as $user_id ) {
  321. $invited[ (int) $user_id ] = groups_invite_user(
  322. array(
  323. 'user_id' => $user_id,
  324. 'group_id' => $group_id,
  325. )
  326. );
  327. }
  328. if ( ! empty( $_POST['message'] ) ) {
  329. $bp->groups->invites_message = wp_kses( wp_unslash( $_POST['message'] ), array() );
  330. add_filter( 'groups_notification_group_invites_message', 'bp_nouveau_groups_invites_custom_message', 10, 1 );
  331. }
  332. // Send the invites.
  333. groups_send_invites( bp_loggedin_user_id(), $group_id );
  334. if ( ! empty( $_POST['message'] ) ) {
  335. unset( $bp->groups->invites_message );
  336. remove_filter( 'groups_notification_group_invites_message', 'bp_nouveau_groups_invites_custom_message', 10, 1 );
  337. }
  338. if ( array_search( false, $invited ) ) {
  339. $errors = array_keys( $invited, false );
  340. $error_count = count( $errors );
  341. $error_message = sprintf(
  342. /* translators: count of users affected */
  343. _n(
  344. 'Invitation failed for %s user.',
  345. 'Invitation failed for %s users.',
  346. $error_count, 'buddypress'
  347. ),
  348. number_format_i18n( $error_count )
  349. );
  350. wp_send_json_error(
  351. array(
  352. 'feedback' => $error_message,
  353. 'users' => $errors,
  354. 'type' => 'error',
  355. )
  356. );
  357. }
  358. wp_send_json_success(
  359. array(
  360. 'feedback' => __( 'Invitations sent.', 'buddypress' ),
  361. 'type' => 'success',
  362. )
  363. );
  364. }
  365. /**
  366. * @since 3.0.0
  367. */
  368. function bp_nouveau_ajax_remove_group_invite() {
  369. $user_id = (int) $_POST['user'];
  370. $group_id = bp_get_current_group_id();
  371. // Verify nonce
  372. if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_invite_uninvite_user' ) ) {
  373. wp_send_json_error(
  374. array(
  375. 'feedback' => __( 'Group invitation could not be removed.', 'buddypress' ),
  376. 'type' => 'error',
  377. )
  378. );
  379. }
  380. if ( BP_Groups_Member::check_for_membership_request( $user_id, $group_id ) ) {
  381. wp_send_json_error(
  382. array(
  383. 'feedback' => __( 'The member is already a member of the group.', 'buddypress' ),
  384. 'type' => 'warning',
  385. 'code' => 1,
  386. )
  387. );
  388. }
  389. // Remove the unsent invitation.
  390. if ( ! groups_uninvite_user( $user_id, $group_id ) ) {
  391. wp_send_json_error(
  392. array(
  393. 'feedback' => __( 'Group invitation could not be removed.', 'buddypress' ),
  394. 'type' => 'error',
  395. 'code' => 0,
  396. )
  397. );
  398. }
  399. wp_send_json_success(
  400. array(
  401. 'feedback' => __( 'There are no more pending invitations for the group.', 'buddypress' ),
  402. 'type' => 'info',
  403. 'has_invites' => bp_group_has_invites( array( 'user_id' => 'any' ) ),
  404. )
  405. );
  406. }