/bp-xprofile/bp-xprofile-screens.php

  1. <?php 
  2. /** 
  3. * BuddyPress XProfile Screens. 
  4. * 
  5. * Screen functions are the controllers of BuddyPress. They will execute when 
  6. * their specific URL is caught. They will first save or manipulate data using 
  7. * business functions, then pass on the user to a template file. 
  8. * 
  9. * @package BuddyPress 
  10. * @subpackage XProfileScreens 
  11. * @since 1.5.0 
  12. */ 
  13.  
  14. // Exit if accessed directly. 
  15. defined( 'ABSPATH' ) || exit; 
  16.  
  17. /** 
  18. * Handles the display of the profile page by loading the correct template file. 
  19. * 
  20. * @since 1.0.0 
  21. * 
  22. */ 
  23. function xprofile_screen_display_profile() { 
  24. $new = isset( $_GET['new'] ) ? $_GET['new'] : ''; 
  25.  
  26. /** 
  27. * Fires right before the loading of the XProfile screen template file. 
  28. * 
  29. * @since 1.0.0 
  30. * 
  31. * @param string $new $_GET parameter holding the "new" parameter. 
  32. */ 
  33. do_action( 'xprofile_screen_display_profile', $new ); 
  34.  
  35. /** 
  36. * Filters the template to load for the XProfile screen. 
  37. * 
  38. * @since 1.0.0 
  39. * 
  40. * @param string $template Path to the XProfile template to load. 
  41. */ 
  42. bp_core_load_template( apply_filters( 'xprofile_template_display_profile', 'members/single/home' ) ); 
  43.  
  44. /** 
  45. * Handles the display of the profile edit page by loading the correct template file. 
  46. * Also checks to make sure this can only be accessed for the logged in users profile. 
  47. * 
  48. * @since 1.0.0 
  49. * 
  50. */ 
  51. function xprofile_screen_edit_profile() { 
  52.  
  53. if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) { 
  54. return false; 
  55.  
  56. // Make sure a group is set. 
  57. if ( ! bp_action_variable( 1 ) ) { 
  58. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/1' ) ); 
  59.  
  60. // Check the field group exists. 
  61. if ( ! bp_is_action_variable( 'group' ) || ! xprofile_get_field_group( bp_action_variable( 1 ) ) ) { 
  62. bp_do_404(); 
  63. return; 
  64.  
  65. // No errors. 
  66. $errors = false; 
  67.  
  68. // Check to see if any new information has been submitted. 
  69. if ( isset( $_POST['field_ids'] ) ) { 
  70.  
  71. // Check the nonce. 
  72. check_admin_referer( 'bp_xprofile_edit' ); 
  73.  
  74. // Check we have field ID's. 
  75. if ( empty( $_POST['field_ids'] ) ) { 
  76. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . bp_action_variable( 1 ) ) ); 
  77.  
  78. // Explode the posted field IDs into an array so we know which 
  79. // fields have been submitted. 
  80. $posted_field_ids = wp_parse_id_list( $_POST['field_ids'] ); 
  81. $is_required = array(); 
  82.  
  83. // Loop through the posted fields formatting any datebox values then validate the field. 
  84. foreach ( (array) $posted_field_ids as $field_id ) { 
  85. bp_xprofile_maybe_format_datebox_post_data( $field_id ); 
  86.  
  87. $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ) && ! bp_current_user_can( 'bp_moderate' ); 
  88. if ( $is_required[$field_id] && empty( $_POST['field_' . $field_id] ) ) { 
  89. $errors = true; 
  90.  
  91. // There are errors. 
  92. if ( !empty( $errors ) ) { 
  93. bp_core_add_message( __( 'Please make sure you fill in all required fields in this profile field group before saving.', 'buddypress' ), 'error' ); 
  94.  
  95. // No errors. 
  96. } else { 
  97.  
  98. // Reset the errors var. 
  99. $errors = false; 
  100.  
  101. // Now we've checked for required fields, lets save the values. 
  102. $old_values = $new_values = array(); 
  103. foreach ( (array) $posted_field_ids as $field_id ) { 
  104.  
  105. // Certain types of fields (checkboxes, multiselects) may come through empty. Save them as an empty array so that they don't get overwritten by the default on the next edit. 
  106. $value = isset( $_POST['field_' . $field_id] ) ? $_POST['field_' . $field_id] : ''; 
  107.  
  108. $visibility_level = !empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public'; 
  109.  
  110. // Save the old and new values. They will be 
  111. // passed to the filter and used to determine 
  112. // whether an activity item should be posted. 
  113. $old_values[ $field_id ] = array( 
  114. 'value' => xprofile_get_field_data( $field_id, bp_displayed_user_id() ),  
  115. 'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),  
  116. ); 
  117.  
  118. // Update the field data and visibility level. 
  119. xprofile_set_field_visibility_level( $field_id, bp_displayed_user_id(), $visibility_level ); 
  120. $field_updated = xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[ $field_id ] ); 
  121. $value = xprofile_get_field_data( $field_id, bp_displayed_user_id() ); 
  122.  
  123. $new_values[ $field_id ] = array( 
  124. 'value' => $value,  
  125. 'visibility' => xprofile_get_field_visibility_level( $field_id, bp_displayed_user_id() ),  
  126. ); 
  127.  
  128. if ( ! $field_updated ) { 
  129. $errors = true; 
  130. } else { 
  131.  
  132. /** 
  133. * Fires on each iteration of an XProfile field being saved with no error. 
  134. * 
  135. * @since 1.1.0 
  136. * 
  137. * @param int $field_id ID of the field that was saved. 
  138. * @param string $value Value that was saved to the field. 
  139. */ 
  140. do_action( 'xprofile_profile_field_data_updated', $field_id, $value ); 
  141.  
  142. /** 
  143. * Fires after all XProfile fields have been saved for the current profile. 
  144. * 
  145. * @since 1.0.0 
  146. * 
  147. * @param int $value Displayed user ID. 
  148. * @param array $posted_field_ids Array of field IDs that were edited. 
  149. * @param bool $errors Whether or not any errors occurred. 
  150. * @param array $old_values Array of original values before updated. 
  151. * @param array $new_values Array of newly saved values after update. 
  152. */ 
  153. do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values ); 
  154.  
  155. // Set the feedback messages. 
  156. if ( !empty( $errors ) ) { 
  157. bp_core_add_message( __( 'There was a problem updating some of your profile information. Please try again.', 'buddypress' ), 'error' ); 
  158. } else { 
  159. bp_core_add_message( __( 'Changes saved.', 'buddypress' ) ); 
  160.  
  161. // Redirect back to the edit screen to display the updates and message. 
  162. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . bp_get_profile_slug() . '/edit/group/' . bp_action_variable( 1 ) ) ); 
  163.  
  164. /** 
  165. * Fires right before the loading of the XProfile edit screen template file. 
  166. * 
  167. * @since 1.0.0 
  168. */ 
  169. do_action( 'xprofile_screen_edit_profile' ); 
  170.  
  171. /** 
  172. * Filters the template to load for the XProfile edit screen. 
  173. * 
  174. * @since 1.0.0 
  175. * 
  176. * @param string $template Path to the XProfile edit template to load. 
  177. */ 
  178. bp_core_load_template( apply_filters( 'xprofile_template_edit_profile', 'members/single/home' ) ); 
  179.  
  180. /** 
  181. * Handles the uploading and cropping of a user avatar. Displays the change avatar page. 
  182. * 
  183. * @since 1.0.0 
  184. * 
  185. */ 
  186. function xprofile_screen_change_avatar() { 
  187.  
  188. // Bail if not the correct screen. 
  189. if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) { 
  190. return false; 
  191.  
  192. // Bail if there are action variables. 
  193. if ( bp_action_variables() ) { 
  194. bp_do_404(); 
  195. return; 
  196.  
  197. $bp = buddypress(); 
  198.  
  199. if ( ! isset( $bp->avatar_admin ) ) { 
  200. $bp->avatar_admin = new stdClass(); 
  201.  
  202. $bp->avatar_admin->step = 'upload-image'; 
  203.  
  204. if ( !empty( $_FILES ) ) { 
  205.  
  206. // Check the nonce. 
  207. check_admin_referer( 'bp_avatar_upload' ); 
  208.  
  209. // Pass the file to the avatar upload handler. 
  210. if ( bp_core_avatar_handle_upload( $_FILES, 'xprofile_avatar_upload_dir' ) ) { 
  211. $bp->avatar_admin->step = 'crop-image'; 
  212.  
  213. // Make sure we include the jQuery jCrop file for image cropping. 
  214. add_action( 'wp_print_scripts', 'bp_core_add_jquery_cropper' ); 
  215.  
  216. // If the image cropping is done, crop the image and save a full/thumb version. 
  217. if ( isset( $_POST['avatar-crop-submit'] ) ) { 
  218.  
  219. // Check the nonce. 
  220. check_admin_referer( 'bp_avatar_cropstore' ); 
  221.  
  222. $args = array( 
  223. 'item_id' => bp_displayed_user_id(),  
  224. 'original_file' => $_POST['image_src'],  
  225. 'crop_x' => $_POST['x'],  
  226. 'crop_y' => $_POST['y'],  
  227. 'crop_w' => $_POST['w'],  
  228. 'crop_h' => $_POST['h'] 
  229. ); 
  230.  
  231. if ( ! bp_core_avatar_handle_crop( $args ) ) { 
  232. bp_core_add_message( __( 'There was a problem cropping your profile photo.', 'buddypress' ), 'error' ); 
  233. } else { 
  234.  
  235. /** 
  236. * Fires right before the redirect, after processing a new avatar. 
  237. * 
  238. * @since 1.1.0 
  239. * @since 2.3.4 Add two new parameters to inform about the user id and 
  240. * about the way the avatar was set (eg: 'crop' or 'camera'). 
  241. * 
  242. * @param string $item_id Inform about the user id the avatar was set for. 
  243. * @param string $value Inform about the way the avatar was set ('crop'). 
  244. */ 
  245. do_action( 'xprofile_avatar_uploaded', (int) $args['item_id'], 'crop' ); 
  246. bp_core_add_message( __( 'Your new profile photo was uploaded successfully.', 'buddypress' ) ); 
  247. bp_core_redirect( bp_displayed_user_domain() ); 
  248.  
  249. /** 
  250. * Fires right before the loading of the XProfile change avatar screen template file. 
  251. * 
  252. * @since 1.0.0 
  253. */ 
  254. do_action( 'xprofile_screen_change_avatar' ); 
  255.  
  256. /** 
  257. * Filters the template to load for the XProfile change avatar screen. 
  258. * 
  259. * @since 1.0.0 
  260. * 
  261. * @param string $template Path to the XProfile change avatar template to load. 
  262. */ 
  263. bp_core_load_template( apply_filters( 'xprofile_template_change_avatar', 'members/single/home' ) ); 
  264.  
  265. /** 
  266. * Displays the change cover image page. 
  267. * 
  268. * @since 2.4.0 
  269. */ 
  270. function xprofile_screen_change_cover_image() { 
  271.  
  272. // Bail if not the correct screen. 
  273. if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) { 
  274. return false; 
  275.  
  276. /** 
  277. * Fires right before the loading of the XProfile change cover image screen template file. 
  278. * 
  279. * @since 2.4.0 
  280. */ 
  281. do_action( 'xprofile_screen_change_cover_image' ); 
  282.  
  283. /** 
  284. * Filters the template to load for the XProfile cover image screen. 
  285. * 
  286. * @since 2.4.0 
  287. * 
  288. * @param string $template Path to the XProfile cover image template to load. 
  289. */ 
  290. bp_core_load_template( apply_filters( 'xprofile_template_cover_image', 'members/single/home' ) ); 
  291.  
  292. /** 
  293. * Show the xprofile settings template. 
  294. * 
  295. * @since 2.0.0 
  296. */ 
  297. function bp_xprofile_screen_settings() { 
  298.  
  299. // Redirect if no privacy settings page is accessible. 
  300. if ( bp_action_variables() || ! bp_is_active( 'xprofile' ) ) { 
  301. bp_do_404(); 
  302. return; 
  303.  
  304. /** 
  305. * Filters the template to load for the XProfile settings screen. 
  306. * 
  307. * @since 2.0.0 
  308. * 
  309. * @param string $template Path to the XProfile change avatar template to load. 
  310. */ 
  311. bp_core_load_template( apply_filters( 'bp_settings_screen_xprofile', '/members/single/settings/profile' ) ); 
.