This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Edit profile patch

By: a guest on Mar 11th, 2014  |  syntax: PHP  |  size: 7.60 KB  |  views: 320  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /**
  4.  * BuddyPress XProfile Screens
  5.  *
  6.  * Screen functions are the controllers of BuddyPress. They will execute when
  7.  * their specific URL is caught. They will first save or manipulate data using
  8.  * business functions, then pass on the user to a template file.
  9.  *
  10.  * @package BuddyPress
  11.  * @subpackage XProfileScreens
  12.  */
  13.  
  14. // Exit if accessed directly
  15. if ( !defined( 'ABSPATH' ) ) exit;
  16.  
  17. /**
  18.  * Handles the display of the profile page by loading the correct template file.
  19.  *
  20.  * @package BuddyPress XProfile
  21.  * @uses bp_core_load_template() Looks for and loads a template file within the current member theme (folder/filename)
  22.  */
  23. function xprofile_screen_display_profile() {
  24.         $new = isset( $_GET['new'] ) ? $_GET['new'] : '';
  25.  
  26.         do_action( 'xprofile_screen_display_profile', $new );
  27.         bp_core_load_template( apply_filters( 'xprofile_template_display_profile', 'members/single/home' ) );
  28. }
  29.  
  30. /**
  31.  * Handles the display of the profile edit page by loading the correct template file.
  32.  * Also checks to make sure this can only be accessed for the logged in users profile.
  33.  *
  34.  * @package BuddyPress XProfile
  35.  * @uses bp_is_my_profile() Checks to make sure the current user being viewed equals the logged in user
  36.  * @uses bp_core_load_template() Looks for and loads a template file within the current member theme (folder/filename)
  37.  */
  38. function xprofile_screen_edit_profile() {
  39.  
  40.         if ( !bp_is_my_profile() && !bp_current_user_can( 'bp_moderate' ) )
  41.                 return false;
  42.  
  43.         $bp = buddypress();
  44.  
  45.         // Make sure a group is set.
  46.         /*
  47.         if ( !bp_action_variable( 1 ) )
  48.             bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/1' ) );
  49.  
  50.         // Check the field group exists
  51.         if ( !bp_is_action_variable( 'group' ) || !xprofile_get_field_group( bp_action_variable( 1 ) ) ) {
  52.                 bp_do_404();
  53.                 return;
  54.         }
  55.        
  56.          *
  57.          */
  58.  
  59.         // No errors
  60.         $errors = false;
  61.  
  62.         // Check to see if any new information has been submitted
  63.         if ( isset( $_POST['field_ids'] ) ) {
  64.  
  65.                 // Check the nonce
  66.                 check_admin_referer( 'bp_xprofile_edit' );
  67.  
  68.                 // Check we have field ID's
  69.                 if ( empty( $_POST['field_ids'] ) )
  70.                         bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/' . bp_action_variable( 1 ) ) );
  71.  
  72.                 // Explode the posted field IDs into an array so we know which
  73.                 // fields have been submitted
  74.                 $posted_field_ids = explode( ',', $_POST['field_ids'] );
  75.                 $is_required      = array();
  76.  
  77.                 // Loop through the posted fields formatting any datebox values
  78.                 // then validate the field
  79.                 foreach ( (array) $posted_field_ids as $field_id ) {
  80.                         if ( !isset( $_POST['field_' . $field_id] ) ) {
  81.  
  82.                                 if ( !empty( $_POST['field_' . $field_id . '_day'] ) && !empty( $_POST['field_' . $field_id . '_month'] ) && !empty( $_POST['field_' . $field_id . '_year'] ) ) {
  83.                                         // Concatenate the values
  84.                                         $date_value =   $_POST['field_' . $field_id . '_day'] . ' ' . $_POST['field_' . $field_id . '_month'] . ' ' . $_POST['field_' . $field_id . '_year'];
  85.  
  86.                                         // Turn the concatenated value into a timestamp
  87.                                         $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $date_value ) );
  88.                                 }
  89.  
  90.                         }
  91.  
  92.                         $is_required[$field_id] = xprofile_check_is_required_field( $field_id );
  93.                         if ( $is_required[$field_id] && empty( $_POST['field_' . $field_id] ) ) {
  94.                                 $errors = true;
  95.                         }
  96.                 }
  97.  
  98.                 // There are errors
  99.                 if ( !empty( $errors ) ) {
  100.                         bp_core_add_message( __( 'Please make sure you fill in all required fields in this profile field group before saving.', 'buddypress' ), 'error' );
  101.  
  102.                 // No errors
  103.                 } else {
  104.  
  105.                         // Reset the errors var
  106.                         $errors = false;
  107.  
  108.                         // Now we've checked for required fields, lets save the values.
  109.                         foreach ( (array) $posted_field_ids as $field_id ) {
  110.  
  111.                                 // 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.
  112.                                 if ( empty( $_POST['field_' . $field_id] ) ) {
  113.                                         $value = array();
  114.                                 } else {
  115.                                         $value = $_POST['field_' . $field_id];
  116.                                 }
  117.  
  118.                                 if ( !xprofile_set_field_data( $field_id, bp_displayed_user_id(), $value, $is_required[$field_id] ) ) {
  119.                                         $errors = true;
  120.                                 } else {
  121.                                         do_action( 'xprofile_profile_field_data_updated', $field_id, $value );
  122.                                 }
  123.  
  124.                                 // Save the visibility level
  125.                                 $visibility_level = !empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
  126.                                 xprofile_set_field_visibility_level( $field_id, bp_displayed_user_id(), $visibility_level );
  127.                         }
  128.  
  129.                         do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors );
  130.  
  131.                         // Set the feedback messages
  132.                         if ( !empty( $errors ) ) {
  133.                                 bp_core_add_message( __( 'There was a problem updating some of your profile information, please try again.', 'buddypress' ), 'error' );
  134.                         } else {
  135.                                 bp_core_add_message( __( 'Changes saved.', 'buddypress' ) );
  136.                         }
  137.  
  138.                         // Redirect back to the edit screen to display the updates and message
  139.                         #bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/' . bp_action_variable( 1 ) ) );
  140.                        bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/' ) );
  141.                 }
  142.         }
  143.  
  144.         do_action( 'xprofile_screen_edit_profile' );
  145.         bp_core_load_template( apply_filters( 'xprofile_template_edit_profile', 'members/single/home' ) );
  146. }
  147.  
  148. /**
  149.  * Handles the uploading and cropping of a user avatar. Displays the change avatar page.
  150.  *
  151.  * @package BuddyPress XProfile
  152.  * @uses bp_is_my_profile() Checks to make sure the current user being viewed equals the logged in user
  153.  * @uses bp_core_load_template() Looks for and loads a template file within the current member theme (folder/filename)
  154.  */
  155. function xprofile_screen_change_avatar() {
  156.  
  157.         // Bail if not the correct screen
  158.         if ( !bp_is_my_profile() && !bp_current_user_can( 'bp_moderate' ) )
  159.                 return false;
  160.  
  161.         // Bail if there are action variables
  162.         if ( bp_action_variables() ) {
  163.                 bp_do_404();
  164.                 return;
  165.         }
  166.  
  167.         $bp = buddypress();
  168.  
  169.         if ( ! isset( $bp->avatar_admin ) )
  170.                 $bp->avatar_admin = new stdClass();
  171.  
  172.         $bp->avatar_admin->step = 'upload-image';
  173.  
  174.         if ( !empty( $_FILES ) ) {
  175.  
  176.                 // Check the nonce
  177.                 check_admin_referer( 'bp_avatar_upload' );
  178.  
  179.                 // Pass the file to the avatar upload handler
  180.                 if ( bp_core_avatar_handle_upload( $_FILES, 'xprofile_avatar_upload_dir' ) ) {
  181.                         $bp->avatar_admin->step = 'crop-image';
  182.  
  183.                         // Make sure we include the jQuery jCrop file for image cropping
  184.                         add_action( 'wp_print_scripts', 'bp_core_add_jquery_cropper' );
  185.                 }
  186.         }
  187.  
  188.         // If the image cropping is done, crop the image and save a full/thumb version
  189.         if ( isset( $_POST['avatar-crop-submit'] ) ) {
  190.  
  191.                 // Check the nonce
  192.                 check_admin_referer( 'bp_avatar_cropstore' );
  193.  
  194.                 $args = array(
  195.                         'item_id'       => bp_displayed_user_id(),
  196.                         'original_file' => $_POST['image_src'],
  197.                         'crop_x'        => $_POST['x'],
  198.                         'crop_y'        => $_POST['y'],
  199.                         'crop_w'        => $_POST['w'],
  200.                         'crop_h'        => $_POST['h']
  201.                 );
  202.  
  203.                 if ( ! bp_core_avatar_handle_crop( $args ) ) {
  204.                         bp_core_add_message( __( 'There was a problem cropping your avatar.', 'buddypress' ), 'error' );
  205.                 } else {
  206.                         do_action( 'xprofile_avatar_uploaded' );
  207.                         bp_core_add_message( __( 'Your new avatar was uploaded successfully.', 'buddypress' ) );
  208.                         bp_core_redirect( bp_loggedin_user_domain() );
  209.                 }
  210.         }
  211.  
  212.         do_action( 'xprofile_screen_change_avatar' );
  213.  
  214.         bp_core_load_template( apply_filters( 'xprofile_template_change_avatar', 'members/single/home' ) );
  215. }
clone this paste RAW Paste Data