Changeset 10525 for trunk/src/bp-xprofile/bp-xprofile-admin.php
- Timestamp:
- 02/05/2016 05:37:16 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/bp-xprofile-admin.php
r10434 r10525 10 10 // Exit if accessed directly. 11 11 defined( 'ABSPATH' ) || exit; 12 13 require dirname( __FILE__ ) . '/classes/class-bp-xprofile-user-admin.php'; 12 14 13 15 /** … … 671 673 } 672 674 673 if ( ! class_exists( 'BP_XProfile_User_Admin' ) ) :674 675 /**676 * Load xProfile Profile admin area.677 *678 * @since 2.0.0679 */680 class BP_XProfile_User_Admin {681 682 /**683 * Setup xProfile User Admin.684 *685 * @since 2.0.0686 *687 * @uses buddypress() to get BuddyPress main instance.688 */689 public static function register_xprofile_user_admin() {690 691 // Bail if not in admin.692 if ( ! is_admin() ) {693 return;694 }695 696 $bp = buddypress();697 698 if ( empty( $bp->profile->admin ) ) {699 $bp->profile->admin = new self;700 }701 702 return $bp->profile->admin;703 }704 705 /**706 * Constructor method.707 *708 * @since 2.0.0709 */710 public function __construct() {711 $this->setup_actions();712 }713 714 /**715 * Set admin-related actions and filters.716 *717 * @since 2.0.0718 */719 private function setup_actions() {720 // Enqueue scripts.721 add_action( 'bp_members_admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 1 );722 723 // Register the metabox in Member's community admin profile.724 add_action( 'bp_members_admin_xprofile_metabox', array( $this, 'register_metaboxes' ), 10, 3 );725 726 // Saves the profile actions for user ( avatar, profile fields ).727 add_action( 'bp_members_admin_update_user', array( $this, 'user_admin_load' ), 10, 4 );728 }729 730 /**731 * Enqueue needed scripts.732 *733 * @since 2.3.0734 *735 * @param int $screen_id Screen ID being displayed.736 */737 public function enqueue_scripts( $screen_id ) {738 if ( ( false === strpos( $screen_id, 'users_page_bp-profile-edit' )739 && false === strpos( $screen_id, 'profile_page_bp-profile-edit' ) )740 || bp_core_get_root_option( 'bp-disable-avatar-uploads' )741 || ! buddypress()->avatar->show_avatars742 || ! bp_attachments_is_wp_version_supported() ) {743 return;744 }745 746 /**747 * Get Thickbox.748 *749 * We cannot simply use add_thickbox() here as WordPress is not playing750 * nice with Thickbox width/height see https://core.trac.wordpress.org/ticket/17249751 * Using media-upload might be interesting in the future for the send to editor stuff752 * and we make sure the tb_window is wide enougth753 */754 wp_enqueue_style ( 'thickbox' );755 wp_enqueue_script( 'media-upload' );756 757 // Get Avatar Uploader.758 bp_attachments_enqueue_scripts( 'BP_Attachment_Avatar' );759 }760 761 /**762 * Register the xProfile metabox on Community Profile admin page.763 *764 * @since 2.0.0765 *766 * @param int $user_id ID of the user being edited.767 * @param string $screen_id Screen ID to load the metabox in.768 * @param object|null $stats_metabox Context and priority for the stats metabox.769 */770 public function register_metaboxes( $user_id = 0, $screen_id = '', $stats_metabox = null ) {771 772 // Set the screen ID if none was passed.773 if ( empty( $screen_id ) ) {774 $screen_id = buddypress()->members->admin->user_page;775 }776 777 // Setup a new metabox class if none was passed.778 if ( empty( $stats_metabox ) ) {779 $stats_metabox = new StdClass();780 }781 782 // Moving the Stats Metabox.783 $stats_metabox->context = 'side';784 $stats_metabox->priority = 'low';785 786 // Each Group of fields will have his own metabox.787 $profile_args = array(788 'fetch_fields' => false,789 'user_id' => $user_id,790 );791 792 if ( ! bp_is_user_spammer( $user_id ) && bp_has_profile( $profile_args ) ) {793 794 // Loop through field groups and add a metabox for each one.795 while ( bp_profile_groups() ) : bp_the_profile_group();796 add_meta_box(797 'bp_xprofile_user_admin_fields_' . sanitize_key( bp_get_the_profile_group_slug() ),798 esc_html( bp_get_the_profile_group_name() ),799 array( $this, 'user_admin_profile_metaboxes' ),800 $screen_id,801 'normal',802 'core',803 array( 'profile_group_id' => absint( bp_get_the_profile_group_id() ) )804 );805 endwhile;806 807 // If member is already a spammer, show a generic metabox.808 } else {809 add_meta_box(810 'bp_xprofile_user_admin_empty_profile',811 _x( 'User marked as a spammer', 'xprofile user-admin edit screen', 'buddypress' ),812 array( $this, 'user_admin_spammer_metabox' ),813 $screen_id,814 'normal',815 'core'816 );817 }818 819 if ( buddypress()->avatar->show_avatars ) {820 // Avatar Metabox.821 add_meta_box(822 'bp_xprofile_user_admin_avatar',823 _x( 'Profile Photo', 'xprofile user-admin edit screen', 'buddypress' ),824 array( $this, 'user_admin_avatar_metabox' ),825 $screen_id,826 'side',827 'low'828 );829 }830 }831 832 /**833 * Save the profile fields in Members community profile page.834 *835 * Loaded before the page is rendered, this function is processing form836 * requests.837 *838 * @since 2.0.0839 *840 * @param string $doaction Action being run.841 * @param int $user_id ID for the user whose profile is being saved.842 * @param array $request Request being made.843 * @param string $redirect_to Where to redirect user to.844 */845 public function user_admin_load( $doaction = '', $user_id = 0, $request = array(), $redirect_to = '' ) {846 847 // Eventually delete avatar.848 if ( 'delete_avatar' === $doaction ) {849 850 check_admin_referer( 'delete_avatar' );851 852 $redirect_to = remove_query_arg( '_wpnonce', $redirect_to );853 854 if ( bp_core_delete_existing_avatar( array( 'item_id' => $user_id ) ) ) {855 $redirect_to = add_query_arg( 'updated', 'avatar', $redirect_to );856 } else {857 $redirect_to = add_query_arg( 'error', 'avatar', $redirect_to );858 }859 860 bp_core_redirect( $redirect_to );861 862 // Update profile fields.863 } elseif ( isset( $_POST['field_ids'] ) ) {864 865 // Check the nonce.866 check_admin_referer( 'edit-bp-profile_' . $user_id );867 868 // Check we have field ID's.869 if ( empty( $_POST['field_ids'] ) ) {870 $redirect_to = add_query_arg( 'error', '1', $redirect_to );871 bp_core_redirect( $redirect_to );872 }873 874 /**875 * Unlike front-end edit-fields screens, the wp-admin/profile876 * displays all groups of fields on a single page, so the list of877 * field ids is an array gathering for each group of fields a878 * distinct comma separated list of ids.879 *880 * As a result, before using the wp_parse_id_list() function, we881 * must ensure that these ids are "merged" into a single comma882 * separated list.883 */884 $merge_ids = join( ',', $_POST['field_ids'] );885 886 // Explode the posted field IDs into an array so we know which fields have been submitted.887 $posted_field_ids = wp_parse_id_list( $merge_ids );888 $is_required = array();889 890 // Loop through the posted fields formatting any datebox values then validate the field.891 foreach ( (array) $posted_field_ids as $field_id ) {892 if ( ! isset( $_POST['field_' . $field_id ] ) ) {893 if ( ! empty( $_POST['field_' . $field_id . '_day'] ) && ! empty( $_POST['field_' . $field_id . '_month'] ) && ! empty( $_POST['field_' . $field_id . '_year'] ) ) {894 895 // Concatenate the values.896 $date_value = $_POST['field_' . $field_id . '_day'] . ' ' . $_POST['field_' . $field_id . '_month'] . ' ' . $_POST['field_' . $field_id . '_year'];897 898 // Turn the concatenated value into a timestamp.899 $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $date_value ) );900 }901 }902 903 $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ) && ! bp_current_user_can( 'bp_moderate' );904 if ( $is_required[ $field_id ] && empty( $_POST['field_' . $field_id ] ) ) {905 $redirect_to = add_query_arg( 'error', '2', $redirect_to );906 bp_core_redirect( $redirect_to );907 }908 }909 910 // Set the errors var.911 $errors = false;912 913 // Now we've checked for required fields, let's save the values.914 foreach ( (array) $posted_field_ids as $field_id ) {915 916 // Certain types of fields (checkboxes, multiselects) may come917 // through empty. Save them as an empty array so that they don't918 // get overwritten by the default on the next edit.919 $value = isset( $_POST['field_' . $field_id] ) ? $_POST['field_' . $field_id] : '';920 921 if ( ! xprofile_set_field_data( $field_id, $user_id, $value, $is_required[ $field_id ] ) ) {922 $errors = true;923 } else {924 925 /**926 * Fires after the saving of each profile field, if successful.927 *928 * @since 1.1.0929 *930 * @param int $field_id ID of the field being updated.931 * @param string $value Value that was saved to the field.932 */933 do_action( 'xprofile_profile_field_data_updated', $field_id, $value );934 }935 936 // Save the visibility level.937 $visibility_level = ! empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public';938 xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level );939 }940 941 /**942 * Fires after all of the profile fields have been saved.943 *944 * @since 1.0.0945 *946 * @param int $user_id ID of the user whose data is being saved.947 * @param array $posted_field_ids IDs of the fields that were submitted.948 * @param bool $errors Whether or not errors occurred during saving.949 */950 do_action( 'xprofile_updated_profile', $user_id, $posted_field_ids, $errors );951 952 // Set the feedback messages.953 if ( ! empty( $errors ) ) {954 $redirect_to = add_query_arg( 'error', '3', $redirect_to );955 } else {956 $redirect_to = add_query_arg( 'updated', '1', $redirect_to );957 }958 959 bp_core_redirect( $redirect_to );960 }961 }962 963 /**964 * Render the xprofile metabox for Community Profile screen.965 *966 * @since 2.0.0967 *968 * @param WP_User|null $user The WP_User object for the user being edited.969 * @param array $args Aray of arguments for metaboxes.970 */971 public function user_admin_profile_metaboxes( $user = null, $args = array() ) {972 973 // Bail if no user ID.974 if ( empty( $user->ID ) ) {975 return;976 }977 978 $r = bp_parse_args( $args['args'], array(979 'profile_group_id' => 0,980 'user_id' => $user->ID981 ), 'bp_xprofile_user_admin_profile_loop_args' );982 983 // We really need these args.984 if ( empty( $r['profile_group_id'] ) || empty( $r['user_id'] ) ) {985 return;986 }987 988 // Bail if no profile fields are available.989 if ( ! bp_has_profile( $r ) ) {990 return;991 }992 993 // Loop through profile groups & fields.994 while ( bp_profile_groups() ) : bp_the_profile_group(); ?>995 996 <input type="hidden" name="field_ids[]" id="<?php echo esc_attr( 'field_ids_' . bp_get_the_profile_group_slug() ); ?>" value="<?php echo esc_attr( bp_get_the_profile_group_field_ids() ); ?>" />997 998 <?php if ( bp_get_the_profile_group_description() ) : ?>999 1000 <p class="description"><?php bp_the_profile_group_description(); ?></p>1001 1002 <?php endif; ?>1003 1004 <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?>1005 1006 <div<?php bp_field_css_class( 'bp-profile-field' ); ?>>1007 1008 <?php1009 1010 $field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );1011 $field_type->edit_field_html( array( 'user_id' => $r['user_id'] ) );1012 1013 if ( bp_get_the_profile_field_description() ) : ?>1014 1015 <p class="description"><?php bp_the_profile_field_description(); ?></p>1016 1017 <?php endif;1018 1019 /**1020 * Fires before display of visibility form elements for profile metaboxes.1021 *1022 * @since 1.7.01023 */1024 do_action( 'bp_custom_profile_edit_fields_pre_visibility' );1025 1026 $can_change_visibility = bp_current_user_can( 'bp_xprofile_change_field_visibility' ); ?>1027 1028 <p class="field-visibility-settings-<?php echo $can_change_visibility ? 'toggle' : 'notoggle'; ?>" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id(); ?>">1029 1030 <?php1031 printf(1032 __( 'This field can be seen by: %s', 'buddypress' ),1033 '<span class="current-visibility-level">' . bp_get_the_profile_field_visibility_level_label() . '</span>'1034 );1035 ?>1036 1037 <?php if ( $can_change_visibility ) : ?>1038 1039 <a href="#" class="button visibility-toggle-link"><?php esc_html_e( 'Change', 'buddypress' ); ?></a>1040 1041 <?php endif; ?>1042 </p>1043 1044 <?php if ( $can_change_visibility ) : ?>1045 1046 <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>">1047 <fieldset>1048 <legend><?php _e( 'Who can see this field?', 'buddypress' ); ?></legend>1049 1050 <?php bp_profile_visibility_radio_buttons(); ?>1051 1052 </fieldset>1053 <a class="button field-visibility-settings-close" href="#"><?php esc_html_e( 'Close', 'buddypress' ); ?></a>1054 </div>1055 1056 <?php endif; ?>1057 1058 <?php1059 1060 /**1061 * Fires at end of custom profile field items on your xprofile screen tab.1062 *1063 * @since 1.1.01064 */1065 do_action( 'bp_custom_profile_edit_fields' ); ?>1066 1067 </div>1068 1069 <?php endwhile; // End bp_profile_fields(). ?>1070 1071 <?php endwhile; // End bp_profile_groups.1072 }1073 1074 /**1075 * Render the fallback metabox in case a user has been marked as a spammer.1076 *1077 * @since 2.0.01078 *1079 * @param WP_User|null $user The WP_User object for the user being edited.1080 */1081 public function user_admin_spammer_metabox( $user = null ) {1082 ?>1083 <p><?php printf( __( '%s has been marked as a spammer. All BuddyPress data associated with the user has been removed', 'buddypress' ), esc_html( bp_core_get_user_displayname( $user->ID ) ) ) ;?></p>1084 <?php1085 }1086 1087 /**1088 * Render the Avatar metabox to moderate inappropriate images.1089 *1090 * @since 2.0.01091 *1092 * @param WP_User|null $user The WP_User object for the user being edited.1093 */1094 public function user_admin_avatar_metabox( $user = null ) {1095 1096 if ( empty( $user->ID ) ) {1097 return;1098 } ?>1099 1100 <div class="avatar">1101 1102 <?php echo bp_core_fetch_avatar( array(1103 'item_id' => $user->ID,1104 'object' => 'user',1105 'type' => 'full',1106 'title' => $user->display_name1107 ) ); ?>1108 1109 <?php if ( bp_get_user_has_avatar( $user->ID ) ) :1110 1111 $query_args = array(1112 'user_id' => $user->ID,1113 'action' => 'delete_avatar'1114 );1115 1116 if ( ! empty( $_REQUEST['wp_http_referer'] ) ) {1117 $query_args['wp_http_referer'] = urlencode( wp_unslash( $_REQUEST['wp_http_referer'] ) );1118 }1119 1120 $community_url = add_query_arg( $query_args, buddypress()->members->admin->edit_profile_url );1121 $delete_link = wp_nonce_url( $community_url, 'delete_avatar' ); ?>1122 1123 <a href="<?php echo esc_url( $delete_link ); ?>" title="<?php esc_attr_e( 'Delete Profile Photo', 'buddypress' ); ?>" class="bp-xprofile-avatar-user-admin"><?php esc_html_e( 'Delete Profile Photo', 'buddypress' ); ?></a>1124 1125 <?php endif;1126 1127 // Load the Avatar UI templates if user avatar uploads are enabled and current WordPress version is supported.1128 if ( ! bp_core_get_root_option( 'bp-disable-avatar-uploads' ) && bp_attachments_is_wp_version_supported() ) : ?>1129 <a href="#TB_inline?width=800px&height=400px&inlineId=bp-xprofile-avatar-editor" title="<?php esc_attr_e( 'Edit Profile Photo', 'buddypress' );?>" class="thickbox bp-xprofile-avatar-user-edit"><?php esc_html_e( 'Edit Profile Photo', 'buddypress' ); ?></a>1130 <div id="bp-xprofile-avatar-editor" style="display:none;">1131 <?php bp_attachments_get_template_part( 'avatars/index' ); ?>1132 </div>1133 <?php endif; ?>1134 1135 </div>1136 <?php1137 }1138 1139 }1140 endif; // End class_exists check.1141 1142 675 // Load the xprofile user admin. 1143 676 add_action( 'bp_init', array( 'BP_XProfile_User_Admin', 'register_xprofile_user_admin' ), 11 );
Note: See TracChangeset
for help on using the changeset viewer.