Changeset 7764 for trunk/bp-xprofile/bp-xprofile-admin.php
- Timestamp:
- 01/29/2014 05:01:17 PM (12 years ago)
- File:
-
- 1 edited
-
trunk/bp-xprofile/bp-xprofile-admin.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-xprofile/bp-xprofile-admin.php
r7271 r7764 467 467 <?php 468 468 } 469 470 if ( ! class_exists( 'BP_XProfile_User_Admin' ) ) : 471 /** 472 * Load xProfile Profile admin area. 473 * 474 * @package BuddyPress 475 * @subpackage xProfileAdministration 476 * 477 * @since BuddyPress (2.0.0) 478 */ 479 class BP_XProfile_User_Admin { 480 481 /** 482 * Setup xProfile User Admin. 483 * 484 * @access public 485 * @since BuddyPress (2.0.0) 486 * 487 * @uses buddypress() to get BuddyPress main instance 488 */ 489 public static function register_xprofile_user_admin() { 490 if( ! is_admin() ) 491 return; 492 493 $bp = buddypress(); 494 495 if( empty( $bp->profile->admin ) ) { 496 $bp->profile->admin = new self; 497 } 498 499 return $bp->profile->admin; 500 } 501 502 /** 503 * Constructor method. 504 * 505 * @access public 506 * @since BuddyPress (2.0.0) 507 */ 508 public function __construct() { 509 $this->setup_actions(); 510 } 511 512 /** 513 * Set admin-related actions and filters. 514 * 515 * @access private 516 * @since BuddyPress (2.0.0) 517 */ 518 private function setup_actions() { 519 520 /** Actions ***************************************************/ 521 522 // Register the metabox in Member's community admin profile 523 add_action( 'bp_members_admin_xprofile_metabox', array( $this, 'register_metaboxes' ), 10, 3 ); 524 525 // Saves the profile actions for user ( avatar, profile fields ) 526 add_action( 'bp_members_admin_update_user', array( $this, 'user_admin_load' ), 10, 4 ); 527 528 } 529 530 /** 531 * Register the xProfile metabox on Community Profile admin page. 532 * 533 * @access public 534 * @since BuddyPress (2.0.0) 535 * 536 * @param int $user_id ID of the user being edited. 537 * @param string $screen_id Screen ID to load the metabox in. 538 * @param object $stats_metabox Context and priority for the stats metabox. 539 */ 540 public function register_metaboxes( $user_id = 0, $screen_id = '', $stats_metabox = null ) { 541 542 if ( empty( $screen_id ) ) { 543 $screen_id = buddypress()->members->admin->user_page; 544 } 545 546 if ( empty( $stats_metabox ) ) { 547 $stats_metabox = new StdClass(); 548 } 549 550 // Moving the Stats Metabox 551 $stats_metabox->context = 'side'; 552 $stats_metabox->priority = 'low'; 553 554 // Each Group of fields will have his own metabox 555 if ( false == bp_is_user_spammer( $user_id ) && bp_has_profile( array( 'fetch_fields' => false ) ) ) { 556 while ( bp_profile_groups() ) : bp_the_profile_group(); 557 add_meta_box( 'bp_xprofile_user_admin_fields_' . sanitize_key( bp_get_the_profile_group_slug() ), esc_html( bp_get_the_profile_group_name() ), array( &$this, 'user_admin_profile_metaboxes' ), $screen_id, 'normal', 'core', array( 'profile_group_id' => absint( bp_get_the_profile_group_id() ) ) ); 558 endwhile; 559 560 // if a user has been mark as a spammer, his BuddyPress datas are removed ! 561 } else { 562 add_meta_box( 'bp_xprofile_user_admin_empty_profile', _x( 'User marked as a spammer', 'xprofile user-admin edit screen', 'buddypress' ), array( &$this, 'user_admin_spammer_metabox' ), $screen_id, 'normal', 'core' ); 563 } 564 565 // Avatar Metabox 566 add_meta_box( 'bp_xprofile_user_admin_avatar', _x( 'Avatar', 'xprofile user-admin edit screen', 'buddypress' ), array( &$this, 'user_admin_avatar_metabox' ), $screen_id, 'side', 'low' ); 567 568 } 569 570 /** 571 * Save the profile fields in Members community profile page. 572 * 573 * Loaded before the page is rendered, this function is processing form 574 * requests. 575 * 576 * @access public 577 * @since BuddyPress (2.0.0) 578 */ 579 public function user_admin_load( $doaction = '', $user_id = 0, $request = array(), $redirect_to = '' ) { 580 581 // Eventually delete avatar 582 if ( 'delete_avatar' == $doaction ) { 583 584 check_admin_referer( 'delete_avatar' ); 585 586 $redirect_to = remove_query_arg( '_wpnonce', $redirect_to ); 587 588 if ( bp_core_delete_existing_avatar( array( 'item_id' => $user_id ) ) ) { 589 $redirect_to = add_query_arg( 'updated', 'avatar', $redirect_to ); 590 } else { 591 $redirect_to = add_query_arg( 'error', 'avatar', $redirect_to ); 592 } 593 594 bp_core_redirect( $redirect_to ); 595 596 // Update profile fields 597 } else { 598 // Check to see if any new information has been submitted 599 if ( isset( $_POST['field_ids'] ) ) { 600 601 // Check the nonce 602 check_admin_referer( 'edit-bp-profile_' . $user_id ); 603 604 // Check we have field ID's 605 if ( empty( $_POST['field_ids'] ) ) { 606 $redirect_to = add_query_arg( 'error', '1', $redirect_to ); 607 bp_core_redirect( $redirect_to ); 608 } 609 610 $merge_ids = ''; 611 foreach ( $_POST['field_ids'] as $ids ) { 612 $merge_ids .= $ids . ','; 613 } 614 615 // Explode the posted field IDs into an array so we know which 616 // fields have been submitted 617 $posted_field_ids = array_filter( wp_parse_id_list( $merge_ids ) ); 618 $is_required = array(); 619 620 // Loop through the posted fields formatting any datebox values 621 // then validate the field 622 foreach ( (array) $posted_field_ids as $field_id ) { 623 if ( ! isset( $_POST['field_' . $field_id] ) ) { 624 if ( ! empty( $_POST['field_' . $field_id . '_day'] ) && ! empty( $_POST['field_' . $field_id . '_month'] ) && ! empty( $_POST['field_' . $field_id . '_year'] ) ) { 625 // Concatenate the values 626 $date_value = $_POST['field_' . $field_id . '_day'] . ' ' . $_POST['field_' . $field_id . '_month'] . ' ' . $_POST['field_' . $field_id . '_year']; 627 628 // Turn the concatenated value into a timestamp 629 $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $date_value ) ); 630 } 631 } 632 633 $is_required[ $field_id ] = xprofile_check_is_required_field( $field_id ); 634 if ( $is_required[ $field_id ] && empty( $_POST['field_' . $field_id] ) ) { 635 $redirect_to = add_query_arg( 'error', '2', $redirect_to ); 636 bp_core_redirect( $redirect_to ); 637 } 638 } 639 640 // Set the errors var 641 $errors = false; 642 643 // Now we've checked for required fields, lets save the values. 644 foreach ( (array) $posted_field_ids as $field_id ) { 645 646 // 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. 647 if ( empty( $_POST['field_' . $field_id] ) ) { 648 $value = array(); 649 } else { 650 $value = $_POST['field_' . $field_id]; 651 } 652 653 if ( ! xprofile_set_field_data( $field_id, $user_id, $value, $is_required[ $field_id ] ) ) { 654 $errors = true; 655 } else { 656 do_action( 'xprofile_profile_field_data_updated', $field_id, $value ); 657 } 658 659 // Save the visibility level 660 $visibility_level = ! empty( $_POST['field_' . $field_id . '_visibility'] ) ? $_POST['field_' . $field_id . '_visibility'] : 'public'; 661 xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level ); 662 } 663 664 do_action( 'xprofile_updated_profile', $user_id, $posted_field_ids, $errors ); 665 666 // Set the feedback messages 667 if ( ! empty( $errors ) ) { 668 $redirect_to = add_query_arg( 'error', '3', $redirect_to ); 669 } else { 670 $redirect_to = add_query_arg( 'updated', '1', $redirect_to ); 671 } 672 673 bp_core_redirect( $redirect_to ); 674 } 675 } 676 } 677 678 /** 679 * Render the xprofile metabox for Community Profile screen. 680 * 681 * @access public 682 * @since BuddyPress (2.0.0) 683 * 684 * @param WP_User $user The WP_User object for the user being edited. 685 */ 686 public function user_admin_profile_metaboxes( $user = null, $args = array() ) { 687 688 if ( empty( $user->ID ) ) { 689 return; 690 } 691 692 $r = bp_parse_args( $args['args'], array( 693 'profile_group_id' => 0, 694 'user_id' => $user->ID 695 ), 'bp_xprofile_user_admin_profile_loop_args' ); 696 697 // We really need these args 698 if ( empty( $r['profile_group_id'] ) || empty( $r['user_id'] ) ) { 699 return; 700 } 701 702 if ( bp_has_profile( $r ) ) : 703 704 while ( bp_profile_groups() ) : bp_the_profile_group(); ?> 705 706 <p class="description"><?php bp_the_profile_group_description(); ?></p> 707 708 <table class="form-table"> 709 <tbody> 710 711 <?php while ( bp_profile_fields() ) : bp_the_profile_field(); ?> 712 713 <tr> 714 715 <?php if ( 'textbox' === bp_get_the_profile_field_type() ) : ?> 716 717 <th><label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></label></th> 718 <td class="admin-field-<?php bp_the_profile_field_type();?>"> 719 <input type="text" name="<?php bp_the_profile_field_input_name(); ?>" id="<?php bp_the_profile_field_input_name(); ?>" value="<?php bp_the_profile_field_edit_value(); ?>" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>/> 720 <span class="description"><?php bp_the_profile_field_description(); ?></span> 721 </td> 722 723 <?php endif; ?> 724 725 <?php if ( 'textarea' === bp_get_the_profile_field_type() ) : ?> 726 727 <th><label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></label></th> 728 <td class="admin-field-<?php bp_the_profile_field_type();?>"> 729 <textarea rows="5" cols="40" name="<?php bp_the_profile_field_input_name(); ?>" id="<?php bp_the_profile_field_input_name(); ?>" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>><?php bp_the_profile_field_edit_value(); ?></textarea> 730 <p class="description"><?php bp_the_profile_field_description(); ?></p> 731 </td> 732 733 <?php endif; ?> 734 735 <?php if ( 'selectbox' === bp_get_the_profile_field_type() ) : ?> 736 737 <th><label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></label></th> 738 <td class="admin-field-<?php bp_the_profile_field_type();?>"> 739 <select name="<?php bp_the_profile_field_input_name(); ?>" id="<?php bp_the_profile_field_input_name(); ?>" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>> 740 <?php bp_the_profile_field_options( array( 'user_id' => $r['user_id'], ) ); ?> 741 </select> 742 <span class="description"><?php bp_the_profile_field_description(); ?></span> 743 </td> 744 745 <?php endif; ?> 746 747 <?php if ( 'multiselectbox' === bp_get_the_profile_field_type() ) : ?> 748 749 <th><label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></label></th> 750 <td class="admin-field-<?php bp_the_profile_field_type();?>"> 751 <select name="<?php bp_the_profile_field_input_name(); ?>" id="<?php bp_the_profile_field_input_name(); ?>" multiple="multiple" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>> 752 753 <?php bp_the_profile_field_options( array( 'user_id' => $r['user_id'], ) ); ?> 754 755 </select> 756 757 758 <?php if ( !bp_get_the_profile_field_is_required() ) : ?> 759 760 <p><a class="clear-value" href="javascript:clear( '<?php bp_the_profile_field_input_name(); ?>' );"><?php _e( 'Clear', 'buddypress' ); ?></a></p> 761 762 <?php endif; ?> 763 <p class="description"><?php bp_the_profile_field_description(); ?></p> 764 </td> 765 766 <?php endif; ?> 767 768 <?php if ( 'radio' === bp_get_the_profile_field_type() ) : ?> 769 770 <th> 771 <span class="label"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></span> 772 </th> 773 <td class="admin-field-<?php bp_the_profile_field_type();?>"> 774 <fieldset> 775 <legend class="screen-reader-text"><span><?php bp_the_profile_field_name(); ?></span></legend> 776 <?php bp_the_profile_field_options( array( 'user_id' => $r['user_id'], ) ); ?> 777 </fieldset> 778 779 <?php if ( !bp_get_the_profile_field_is_required() ) : ?> 780 781 <p><a class="clear-value" href="javascript:clear( '<?php bp_the_profile_field_input_name(); ?>' );"><?php _e( 'Clear', 'buddypress' ); ?></a></p> 782 783 <?php endif; ?> 784 <p class="description"><?php bp_the_profile_field_description(); ?></p> 785 </td> 786 787 <?php endif; ?> 788 789 <?php if ( 'checkbox' === bp_get_the_profile_field_type() ) : ?> 790 791 <th> 792 <span class="label"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></span> 793 </th> 794 <td class="admin-field-<?php bp_the_profile_field_type();?>"> 795 <?php bp_the_profile_field_options( array( 'user_id' => $r['user_id'], ) ); ?> 796 <p class="description"><?php bp_the_profile_field_description(); ?></p> 797 </td> 798 799 <?php endif; ?> 800 801 <?php if ( 'datebox' === bp_get_the_profile_field_type() ) : ?> 802 803 <th> 804 <label for="<?php bp_the_profile_field_input_name(); ?>_day"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php _e( '(required)', 'buddypress' ); ?><?php endif; ?></label> 805 </th> 806 <td class="admin-field-<?php bp_the_profile_field_type();?>"> 807 <select name="<?php bp_the_profile_field_input_name(); ?>_day" id="<?php bp_the_profile_field_input_name(); ?>_day" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>> 808 809 <?php bp_the_profile_field_options( array( 'user_id' => $r['user_id'], 'type' => 'day', ) ); ?> 810 811 </select> 812 813 <select name="<?php bp_the_profile_field_input_name(); ?>_month" id="<?php bp_the_profile_field_input_name(); ?>_month" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>> 814 815 <?php bp_the_profile_field_options( array( 'user_id' => $r['user_id'], 'type' => 'month', ) ); ?> 816 817 </select> 818 819 <select name="<?php bp_the_profile_field_input_name(); ?>_year" id="<?php bp_the_profile_field_input_name(); ?>_year" <?php if ( bp_get_the_profile_field_is_required() ) : ?>aria-required="true"<?php endif; ?>> 820 821 <?php bp_the_profile_field_options( array( 'user_id' => $r['user_id'], 'type' => 'year', ) ); ?> 822 823 </select> 824 <p class="description"><?php bp_the_profile_field_description(); ?></p> 825 </td> 826 827 <?php endif; ?> 828 829 </tr> 830 831 <tr class="admin-field-visibility-tr"> 832 <td class="admin-field-visibility-td"> </td> 833 <td class="admin-field-visibility-td"> 834 835 <?php do_action( 'bp_custom_profile_edit_fields_pre_visibility' ); ?> 836 837 <?php if ( bp_current_user_can( 'bp_xprofile_change_field_visibility' ) ) : ?> 838 <p class="description field-visibility-settings-toggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>"> 839 <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> <a href="#" class="visibility-toggle-link"><?php _e( 'Change', 'buddypress' ); ?></a> 840 </p> 841 842 <div class="field-visibility-settings" id="field-visibility-settings-<?php bp_the_profile_field_id() ?>"> 843 <fieldset> 844 <legend><?php esc_html_e( 'Who can see this field?', 'buddypress' ) ?></legend> 845 846 <?php bp_profile_visibility_radio_buttons() ?> 847 848 </fieldset> 849 <a class="field-visibility-settings-close" href="#"><?php esc_html_e( 'Close', 'buddypress' ) ?></a> 850 </div> 851 <?php else : ?> 852 <div class="field-visibility-settings-notoggle" id="field-visibility-settings-toggle-<?php bp_the_profile_field_id() ?>"> 853 <?php printf( __( 'This field can be seen by: <span class="current-visibility-level">%s</span>', 'buddypress' ), bp_get_the_profile_field_visibility_level_label() ) ?> 854 </div> 855 <?php endif ?> 856 857 </td> 858 </tr> 859 860 <?php endwhile; ?> 861 </tbody> 862 863 </table> 864 <input type="hidden" name="field_ids[]" id="field_ids_<?php bp_the_profile_group_slug(); ?>" value="<?php bp_the_profile_group_field_ids(); ?>" /> 865 <?php endwhile; 866 endif; 867 } 868 869 /** 870 * Render the fallback metabox in case a user has been marked as a spammer. 871 * 872 * @access public 873 * @since BuddyPress (2.0.0) 874 * 875 * @param WP_User $user The WP_User object for the user being edited. 876 */ 877 public function user_admin_spammer_metabox( $user = null ) { 878 ?> 879 <p><?php printf( __( '%s has been marked as a spammer, this user's BuddyPress datas were removed', 'buddypress' ), esc_html( bp_core_get_user_displayname( $user->ID ) ) ) ;?></p> 880 <?php 881 } 882 883 /** 884 * Render the Avatar metabox to moderate inappropriate images. 885 * 886 * @access public 887 * @since BuddyPress (2.0.0) 888 * 889 * @param WP_User $user The WP_User object for the user being edited. 890 */ 891 public function user_admin_avatar_metabox( $user = null ) { 892 893 if ( empty( $user->ID ) ) { 894 return; 895 } 896 897 $args = array( 898 'item_id' => $user->ID, 899 'object' => 'user', 900 'type' => 'full', 901 'title' => $user->display_name 902 ); 903 904 ?> 905 906 <div class="avatar"> 907 908 <?php echo bp_core_fetch_avatar( $args ); ?> 909 910 <?php if ( bp_get_user_has_avatar( $user->ID ) ) : 911 912 $query_args = array( 913 'user_id' => $user->ID, 914 'action' => 'delete_avatar' 915 ); 916 917 if ( ! empty( $_REQUEST['wp_http_referer'] ) ) 918 $query_args['wp_http_referer'] = urlencode( wp_unslash( $_REQUEST['wp_http_referer'] ) ); 919 920 $community_url = add_query_arg( $query_args, buddypress()->members->admin->edit_profile_url ); 921 $delete_link = wp_nonce_url( $community_url, 'delete_avatar' ); ?> 922 923 <a href="<?php echo esc_url( $delete_link ); ?>" title="<?php esc_attr_e( 'Delete Avatar', 'buddypress' ); ?>" class="bp-xprofile-avatar-user-admin"><?php esc_html_e( 'Delete Avatar', 'buddypress' ); ?></a></li> 924 925 <?php endif; ?> 926 927 </div> 928 <?php 929 } 930 931 } 932 endif; // class_exists check 933 934 // Load the xprofile user admin 935 add_action( 'bp_init', array( 'BP_XProfile_User_Admin', 'register_xprofile_user_admin' ), 11 );
Note: See TracChangeset
for help on using the changeset viewer.