当前位置: 首页>>代码示例>>PHP>>正文


PHP WP_User::add_role方法代码示例

本文整理汇总了PHP中WP_User::add_role方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_User::add_role方法的具体用法?PHP WP_User::add_role怎么用?PHP WP_User::add_role使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WP_User的用法示例。


在下文中一共展示了WP_User::add_role方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: fes_22_upgrade_vendor_permissions

/**
 * Upgrades vendor permissions
 *
 * @since 2.2
 * @return void
 */
function fes_22_upgrade_vendor_permissions()
{
    $fes_version = get_option('fes_db_version', '2.1');
    if (version_compare($fes_version, '2.2', '>=')) {
        return;
    }
    ignore_user_abort(true);
    if (!edd_is_func_disabled('set_time_limit') && !ini_get('safe_mode')) {
        set_time_limit(0);
    }
    $step = isset($_GET['step']) ? absint($_GET['step']) : 1;
    $offset = $step == 1 ? 0 : $step * 100;
    $users = new WP_User_Query(array('fields' => 'ID', 'number' => 100, 'offset' => $offset));
    $users = $users->results;
    if ($users && count($users) > 0) {
        foreach ($users as $user => $id) {
            if (user_can($id, 'fes_is_vendor') && !user_can($id, 'fes_is_admin') && !user_can($id, 'administrator') && !user_can($id, 'editor')) {
                $user = new WP_User($id);
                $user->add_role('frontend_vendor');
            }
        }
        // Keys found so upgrade them
        $step++;
        $redirect = add_query_arg(array('page' => 'fes-upgrades', 'edd_upgrade' => 'upgrade_vendor_permissions', 'step' => $step), admin_url('index.php'));
        wp_redirect($redirect);
        exit;
    } else {
        // No more keys found, update the DB version and finish up
        update_option('fes_db_version', fes_plugin_version);
        wp_redirect(admin_url('admin.php?page=fes-about'));
        exit;
    }
}
开发者ID:SelaInc,项目名称:eassignment,代码行数:39,代码来源:class-update.php

示例2:

 function bp_course_convert_customer_to_student($order_id)
 {
     $order = new WC_Order($order_id);
     if ($order->user_id > 0) {
         $user = new WP_User($order->user_id);
         $user->remove_role('customer');
         $user->add_role('student');
     }
 }
开发者ID:nikitansk,项目名称:devschool,代码行数:9,代码来源:bp-course-actions.php

示例3: ao_edd_set_customer_role

function ao_edd_set_customer_role($payment_id)
{
    $email = edd_get_payment_user_email($payment_id);
    $downloads = edd_get_payment_meta_downloads($payment_id);
    $user_id = edd_get_payment_user_id($payment_id);
    if ($user_id) {
        $user = new WP_User($user_id);
        // Add role
        $user->add_role('buyer');
    }
}
开发者ID:mintplugins,项目名称:library,代码行数:11,代码来源:change_buyer_role.php

示例4: wpec_members_save_user

function wpec_members_save_user($user_id, $length, $role)
{
    $add_user = new WP_User($user_id);
    //$members_lengths = array();
    $members_lengths = get_user_meta($user_id, '_subscription_ends', true);
    $members_starts = get_user_meta($user_id, '_subscription_starts', true);
    $future_time = mktime(date('h'), date('m'), date('s') + $length, date('m'), date('d'), date('Y'));
    $current_time = time();
    $members_lengths[$role] = $future_time;
    $members_starts[$role] = $current_time;
    $add_user->add_cap($role, true);
    //$subscription_lengths = array();
    $subscription_lengths = get_user_meta($user_id, '_subscription_length', true);
    $subscription_lengths[$role] = $length;
    // dont think we need this line...
    $add_user->add_role('subscriber');
    update_user_meta($user_id, '_subscription_ends', $members_lengths);
    update_user_meta($user_id, '_subscription_length', $subscription_lengths);
    update_user_meta($user_id, '_subscription_starts', $members_starts);
    update_user_meta($user_id, '_has_current_subscription', 'true');
}
开发者ID:picassentviu,项目名称:AMMPro,代码行数:21,代码来源:wpec_members_access_admin.php

示例5: ProcessNewUser

 static function ProcessNewUser($user_id)
 {
     if (@$_POST["dsidxpress"] != "1") {
         return;
     }
     $new_user = new WP_User($user_id);
     $new_user->add_role(dsSearchAgent_Roles::$Role_Name);
     $referring_url = $_SERVER['HTTP_REFERER'];
     $post_vars = array();
     $post_vars["propertyID"] = $_POST["propertyID"];
     $post_vars["firstName"] = $_POST["first_name"];
     $post_vars["lastName"] = $_POST["last_name"];
     $post_vars["phoneNumber"] = $_POST["phone_number"];
     $post_vars["emailAddress"] = $_POST["user_email"];
     $post_vars["scheduleYesNo"] = "";
     $post_vars["scheduleDateDay"] = "1";
     $post_vars["scheduleDateMonth"] = "1";
     $post_vars["comments"] = "";
     $post_vars["referringURL"] = $referring_url;
     //$post_vars["returnURL"] = $_POST[""];
     $apiHttpResponse = dsSearchAgent_ApiRequest::FetchData("ContactForm", $post_vars, false, 0);
     wp_set_auth_cookie($user_id, true, is_ssl());
 }
开发者ID:krisatkinson,项目名称:dsidxpress,代码行数:23,代码来源:roles.php

示例6: pw_affwp_set_role_on_registration

/**
 * Plugin Name: AffiliateWP - Affiliate role on registration
 * Plugin URI: http://affiliatewp.com
 * Description: Sets an affiliate user's role to a specific role when being added as an affiliate
 * Author: Pippin Williamson
 * Author URI: http://pippinsplugins.com
 * Version: 1.0
 */
function pw_affwp_set_role_on_registration($affiliate_id = 0)
{
    $user_id = affwp_get_affiliate_user_id($affiliate_id);
    $user = new WP_User($user_id);
    $user->add_role('affiliate');
}
开发者ID:companyjuice,项目名称:affiliatewp-code-snippet-library,代码行数:14,代码来源:affiliate-role-on-registration.php

示例7: unaffectUsersToGroup

 /**
  * D�saffecte des utilisateurs � un role
  * @param $code identifiant du role
  */
 function unaffectUsersToGroup($code)
 {
     // ROLES
     $roles = get_option('wp_user_roles', array());
     // Si le role existe
     if (isset($roles[$code])) {
         $wps_customer_mdl = new wps_customer_mdl();
         $users = $wps_customer_mdl->getUserList();
         if (!empty($users)) {
             foreach ($users as $user) {
                 $u = new WP_User($user->ID);
                 // Si l'utilisateur poss�de le role, on le retire de sa liste de droits
                 if (isset($u->roles[0]) && $u->roles[0] == $code) {
                     $u->remove_role($u->roles[0]);
                     $u->add_role('subscriber');
                 }
             }
         }
     }
 }
开发者ID:fedeB-IT-dept,项目名称:fedeB_website,代码行数:24,代码来源:wps_customer_group.php

示例8: array

	function test_user_level_update() {
		// make sure the user_level is correctly set and changed with the user's role

		// user starts as an author
		$id = $this->factory->user->create( array( 'role' => 'author' ) );
		$user = new WP_User($id);
		$this->assertTrue($user->exists(), "Problem getting user $id");

		// author = user level 2
		$this->assertEquals( 2, $user->user_level );

		// they get promoted to editor - level should get bumped to 7
		$user->set_role('editor');
		$this->assertEquals( 7, $user->user_level );

		// demoted to contributor - level is reduced to 1
		$user->set_role('contributor');
		$this->assertEquals( 1, $user->user_level );

		// if they have two roles, user_level should be the max of the two
		$user->add_role('editor');
		$this->assertEquals(array('contributor', 'editor'), $user->roles);
		$this->assertEquals( 7, $user->user_level );
	}
开发者ID:staylor,项目名称:develop.svn.wordpress.org,代码行数:24,代码来源:capabilities.php

示例9: rcp_process_registration


//.........这里部分代码省略.........
    $subscription_key = rcp_generate_subscription_key();
    $old_subscription_id = $member->get_subscription_id();
    if ($old_subscription_id) {
        update_user_meta($user_data['id'], '_rcp_old_subscription_id', $old_subscription_id);
    }
    if (!$member->is_active()) {
        update_user_meta($user_data['id'], 'rcp_subscription_level', $subscription_id);
        update_user_meta($user_data['id'], 'rcp_subscription_key', $subscription_key);
        // Ensure no pending level details are set
        delete_user_meta($user_data['id'], 'rcp_pending_subscription_level');
        delete_user_meta($user_data['id'], 'rcp_pending_subscription_key');
        $member->set_status('pending');
    } else {
        // If the member is already active, we need to set these as pending changes
        update_user_meta($user_data['id'], 'rcp_pending_subscription_level', $subscription_id);
        update_user_meta($user_data['id'], 'rcp_pending_subscription_key', $subscription_key);
        // Flag the member as having just upgraded
        update_user_meta($user_data['id'], '_rcp_just_upgraded', current_time('timestamp'));
    }
    $member->set_joined_date('', $subscription_id);
    // Calculate the expiration date for the member
    $member_expires = $member->calculate_expiration($auto_renew);
    update_user_meta($user_data['id'], 'rcp_pending_expiration_date', $member_expires);
    // remove the user's old role, if this is a new user, we need to replace the default role
    $old_role = get_option('default_role', 'subscriber');
    if ($old_subscription_id) {
        $old_level = $rcp_levels_db->get_level($old_subscription_id);
        $old_role = !empty($old_level->role) ? $old_level->role : $old_role;
    }
    $member->remove_role($old_role);
    // Set the user's role
    $role = !empty($subscription->role) ? $subscription->role : 'subscriber';
    $user = new WP_User($user_data['id']);
    $user->add_role(apply_filters('rcp_default_user_level', $role, $subscription_id));
    do_action('rcp_form_processing', $_POST, $user_data['id'], $price);
    // process a paid subscription
    if ($price > '0') {
        if (!empty($discount)) {
            $discounts = new RCP_Discounts();
            $discount_obj = $discounts->get_by('code', $discount);
            // record the usage of this discount code
            $discounts->add_to_user($user_data['id'], $discount);
            // increase the usage count for the code
            $discounts->increase_uses($discount_obj->id);
            // if the discount is 100%, log the user in and redirect to success page
            if ($full_discount) {
                $member->set_expiration_date($member_expires);
                $member->set_status('active');
                rcp_login_user_in($user_data['id'], $user_data['login']);
                wp_redirect(rcp_get_return_url($user_data['id']));
                exit;
            }
        }
        // Remove trialing status, if it exists
        delete_user_meta($user_data['id'], 'rcp_is_trialing');
        // log the new user in
        rcp_login_user_in($user_data['id'], $user_data['login']);
        $redirect = rcp_get_return_url($user_data['id']);
        $subscription_data = array('price' => rcp_get_registration()->get_total(true, false), 'discount' => rcp_get_registration()->get_total_discounts(), 'discount_code' => $discount, 'fee' => rcp_get_registration()->get_total_fees(), 'length' => $subscription->duration, 'length_unit' => strtolower($subscription->duration_unit), 'subscription_id' => $subscription->id, 'subscription_name' => $subscription->name, 'key' => $subscription_key, 'user_id' => $user_data['id'], 'user_name' => $user_data['login'], 'user_email' => $user_data['email'], 'currency' => $rcp_options['currency'], 'auto_renew' => $auto_renew, 'return_url' => $redirect, 'new_user' => $user_data['need_new'], 'post_data' => $_POST);
        // if giving the user a credit, make sure the credit does not exceed the first payment
        if ($subscription_data['fee'] < 0 && abs($subscription_data['fee']) > $subscription_data['price']) {
            $subscription_data['fee'] = -1 * $subscription_data['price'];
        }
        update_user_meta($user_data['id'], 'rcp_pending_subscription_amount', $subscription_data['price'] + $subscription_data['fee']);
        // send all of the subscription data off for processing by the gateway
        rcp_send_to_gateway($gateway, apply_filters('rcp_subscription_data', $subscription_data));
开发者ID:ramiy,项目名称:restrict-content-pro,代码行数:67,代码来源:registration-functions.php

示例10: rcp_process_registration


//.........这里部分代码省略.........
		}

		if( $user_data['need_new'] ) {

			$user_data['id'] = wp_insert_user( array(
					'user_login'		=> $user_data['login'],
					'user_pass'	 		=> $user_data['password'],
					'user_email'		=> $user_data['email'],
					'first_name'		=> $user_data['first_name'],
					'last_name'			=> $user_data['last_name'],
					'user_registered'	=> date( 'Y-m-d H:i:s' )
				)
			);
		}

		if( $user_data['id'] ) {

			if( ! rcp_is_active( $user_data['id'] ) ) {

				rcp_set_status( $user_data['id'], 'pending' );
	
			}

			// setup a unique key for this subscription
			$subscription_key = rcp_generate_subscription_key();
			update_user_meta( $user_data['id'], 'rcp_subscription_key', $subscription_key );
			update_user_meta( $user_data['id'], 'rcp_subscription_level', $subscription_id );

			rcp_set_expiration_date( $user_data['id'], $member_expires );

			// Set the user's role
			$role = ! empty( $subscription->role ) ? $subscription->role : 'subscriber';
			$user = new WP_User( $user_data['id'] );
			$user->add_role( apply_filters( 'rcp_default_user_level', $role, $subscription_id ) );

			do_action( 'rcp_form_processing', $_POST, $user_data['id'], $price );

			// process a paid subscription
			if( $price > '0' ) {

				if( ! empty( $discount ) ) {

					// record the usage of this discount code
					$discounts->add_to_user( $user_data['id'], $discount );

					// incrase the usage count for the code
					$discounts->increase_uses( $discount_obj->id );

					// if the discount is 100%, log the user in and redirect to success page
					if( $price == '0' ) {
						rcp_set_status( $user_data['id'], 'active' );
						rcp_email_subscription_status( $user_data['id'], 'active' );
						rcp_login_user_in( $user_data['id'], $user_data['login'] );
						wp_redirect( rcp_get_return_url( $user_data['id'] ) ); exit;
					}

				}

				// Determine auto renew behavior
				if( '3' == rcp_get_auto_renew_behavior() && isset( $_POST['rcp_auto_renew'] ) ) {

					$auto_renew = true;

				} elseif( '1' == rcp_get_auto_renew_behavior() ) {

					$auto_renew = true;
开发者ID:JiveDig,项目名称:Restrict-Content-Pro,代码行数:67,代码来源:registration-functions.php

示例11: wp_user_parents_add_child

/**
 * Maybe add a child from the "Children" section
 *
 * @since 0.1.0
 */
function wp_user_parents_add_child()
{
    // Bail if no signup nonce
    if (empty($_REQUEST['signup_nonce'])) {
        return;
    }
    // Bail if nonce fails
    if (!wp_verify_nonce($_REQUEST['signup_nonce'], 'wp_user_dashboard_child_signup')) {
        return;
    }
    // Bail if current user cannot have children
    if (!current_user_can('have_user_children')) {
        return;
    }
    // Sanitize fields
    $redirect = false;
    $email = sanitize_email($_REQUEST['email']);
    $firstname = !empty($_REQUEST['firstname']) ? $_REQUEST['firstname'] : '';
    $lastname = !empty($_REQUEST['lastname']) ? $_REQUEST['lastname'] : '';
    $password = !empty($_REQUEST['password']) ? $_REQUEST['password'] : wp_generate_password(12, false);
    $username = !empty($_REQUEST['username']) ? $_REQUEST['username'] : "{$firstname}-{$lastname}";
    // Names are empty
    if (empty($firstname) || empty($lastname) || strlen($firstname) < 2 || strlen($lastname) < 2) {
        $args = array('error' => 'name');
        $url = wp_get_user_dashboard_url('children');
        $redirect = add_query_arg($args, $url);
    }
    // Username exists
    if (username_exists($username) || strlen($username) < 4) {
        $args = array('error' => 'username');
        $url = wp_get_user_dashboard_url('children');
        $redirect = add_query_arg($args, $url);
    }
    // Email exists
    if (email_exists($email)) {
        $args = array('error' => 'username');
        $url = wp_get_user_dashboard_url('children');
        $redirect = add_query_arg($args, $url);
    }
    // Redirect
    if (!empty($redirect)) {
        wp_safe_redirect($redirect);
        exit;
    }
    // Requires activation
    if (is_multisite() && apply_filters('wp_join_page_requires_activation', true)) {
        wpmu_signup_user($username, $email, array('add_to_blog' => get_current_blog_id(), 'new_role' => get_option('default_role'), 'first_name' => $firstname, 'last_name' => $lastname));
    }
    // Create the user account
    $user_id = wpmu_create_user(esc_html(sanitize_key($username)), $password, $email);
    // Bail if no user ID for site
    if (empty($user_id)) {
        $args = array('error' => 'unknown');
        $url = wp_get_user_dashboard_url('children');
        $redirect = add_query_arg($args, $url);
    }
    // Get new userdata
    $user = new WP_User($user_id);
    $user->add_role('pending');
    // Get the current user ID
    $current_user_id = get_current_user_id();
    // Save fullname to usermeta
    update_user_meta($user->ID, 'first_name', $firstname);
    update_user_meta($user->ID, 'last_name', $lastname);
    add_user_meta($user->ID, 'user_parent', $current_user_id, false);
    // Do action
    do_action('wp_user_parents_added_child', $user, $current_user_id);
    // Redirect
    $args = array('success' => 'yay');
    $url = wp_get_user_dashboard_url('children');
    $redirect = add_query_arg($args, $url);
    wp_safe_redirect($redirect);
    die;
}
开发者ID:stuttter,项目名称:wp-user-parents,代码行数:79,代码来源:user-dashboard.php

示例12: isset

 function process_bulk_action()
 {
     $ids = isset($_GET['vendor']) ? $_GET['vendor'] : false;
     if (empty($ids)) {
         return;
     }
     if (!is_array($ids)) {
         $ids = array($ids);
     }
     $current_action = $_GET['action'];
     foreach ($ids as $id) {
         if ('approve_vendor' === $current_action) {
             if ($id < 2) {
                 break;
             }
             if (user_can($id, 'fes_is_admin') || user_can($id, 'frontend_vendor')) {
                 break;
             }
             if (!user_can($id, 'pending_vendor')) {
                 break;
             }
             $user = new WP_User($id);
             $user->remove_role('pending_vendor');
             $user->add_role('frontend_vendor');
             $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
             $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
             $subject = apply_filters('fes_application_approved_message_subj', __('Application Approved', 'edd_fes'), 0);
             $message = EDD_FES()->helper->get_option('fes-vendor-app-approved-email', '');
             $type = "user";
             $args['permissions'] = 'fes-vendor-app-approved-email-toggle';
             EDD_FES()->emails->send_email($user->user_email, $from_name, $from_email, $subject, $message, $type, $id, $args);
             do_action('fes_approve_vendor_admin', $id);
             if (isset($_GET['redirect']) && $_GET['redirect'] == '2') {
                 wp_redirect(admin_url('admin.php?page=fes-vendors&vendor=' . $id . '&action=edit&approved=2'));
                 exit;
             }
         }
         if ('revoke_vendor' === $current_action) {
             if ($id < 2) {
                 break;
             }
             if (!(user_can($id, 'fes_is_admin') || user_can($id, 'frontend_vendor'))) {
                 break;
             }
             $user = new WP_User($id);
             $user->remove_role('frontend_vendor');
             $user->remove_cap('fes_is_admin');
             $user->add_role('subscriber');
             // remove all their posts
             $args = array('post_type' => 'download', 'author' => $id, 'posts_per_page' => -1, 'fields' => 'ids', 'post_status' => 'any');
             $query = new WP_Query($args);
             foreach ($query->posts as $id) {
                 wp_delete_post($id, false);
             }
             $to = $user->user_email;
             $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
             $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
             $subject = apply_filters('fes_application_revoked_message_subj', __('Application Revoked', 'edd_fes'), 0);
             $message = EDD_FES()->helper->get_option('fes-vendor-app-revoked-email', '');
             $type = "user";
             $id = $id;
             $args['permissions'] = 'fes-vendor-app-revoked-email-toggle';
             EDD_FES()->emails->send_email($to, $from_name, $from_email, $subject, $message, $type, $id, $args);
             do_action('fes_revoke_vendor_admin', $id);
         }
         if ('decline_vendor' === $current_action) {
             if ($id < 2) {
                 break;
             }
             if (user_can($id, 'fes_is_admin') || user_can($id, 'frontend_vendor')) {
                 break;
             }
             if (!user_can($id, 'pending_vendor')) {
                 break;
             }
             $user = new WP_User($id);
             $user->remove_role('pending_vendor');
             $to = $user->user_email;
             $from_name = isset($edd_options['from_name']) ? $edd_options['from_name'] : get_bloginfo('name');
             $from_email = isset($edd_options['from_email']) ? $edd_options['from_email'] : get_option('admin_email');
             $subject = apply_filters('fes_application_declined_message_subj', __('Application Declined', 'edd_fes'), 0);
             $message = EDD_FES()->helper->get_option('fes-vendor-app-declined-email', '');
             $type = "user";
             $id = $id;
             $args['permissions'] = 'fes-vendor-app-declined-email-toggle';
             EDD_FES()->emails->send_email($to, $from_name, $from_email, $subject, $message, $type, $id, $args);
             do_action('fes_decline_vendor_admin', $id);
         }
         if ('suspend_vendor' === $current_action) {
             if ($id < 2) {
                 break;
             }
             if (user_can($id, 'pending_vendor')) {
                 break;
             }
             if (user_can($id, 'suspended_vendor')) {
                 break;
             }
             $user = new WP_User($id);
             $user->remove_role('frontend_vendor');
//.........这里部分代码省略.........
开发者ID:nguyenthai2010,项目名称:ngocshop,代码行数:101,代码来源:class-menu.php

示例13: foreach

 function merge_user($user_id, $merged_user_id)
 {
     $user = new WP_User($user_id);
     $merged_user = new WP_User($merged_user_id);
     $roles = $merged_user->roles;
     foreach ($roles as $role) {
         $user->add_role($role);
     }
     wp_delete_user($merged_user_id, $user_id);
     if (get_userdata($merged_user_id)) {
         echo 'Please contact the site administrator.  During the DaCDb update, a duplicate user failed to be deleted. The duplicate is  ' . $merged_user_id . ' Trying to merge to user ' . $user_id . ' Current user logged in is ' . get_current_user_id();
     }
 }
开发者ID:cshort,项目名称:rotary-dacdb,代码行数:13,代码来源:rotary-dacdb-memberdata.php

示例14: solvease_roles_capabilities_update_user_role_cap

 /**
  * update user roles and capabilities
  * @param type $user
  * @return boolean
  */
 public function solvease_roles_capabilities_update_user_role_cap($user_id)
 {
     // check if its a valid POST
     if (!isset($_POST['solvease_user_role_cap_nonce']) || !wp_verify_nonce($_POST['solvease_user_role_cap_nonce'], 'solvease_user_role_cap') || $_POST['user_id'] != $user_id) {
         return;
     }
     $user = new WP_User($user_id);
     // capabilities
     $capabilities = isset($_POST['cap']) ? array_keys($_POST['cap']) : array();
     // all user roles
     $all_roles = $this->solvease_roles_capabilities_get_roles();
     // primary roles
     $primary_role = $_POST['primary_role'];
     // secondary roles
     $secondary_roles = isset($_POST['secondary_roles']) ? array_keys($_POST['secondary_roles']) : array();
     // blank user roles
     $user->roles = array();
     // remove all user roles
     $user->remove_all_caps();
     // add primary roles
     if ($primary_role != '' && isset($all_roles[$primary_role])) {
         $user->add_role($primary_role);
     } else {
         return FALSE;
     }
     // add secondary roles
     if (!empty($secondary_roles)) {
         foreach ($secondary_roles as $secondary_role) {
             if (isset($all_roles[$secondary_role]) && $primary_role != $secondary_role) {
                 $user->add_role($secondary_role);
             }
         }
     }
     // add capabilities of user roles
     $user->update_user_level_from_caps();
     // add capabilities
     if (!empty($capabilities)) {
         foreach ($capabilities as $capability) {
             $user->add_cap($capability);
         }
     }
     return $user;
 }
开发者ID:RA2WP,项目名称:RA2WP,代码行数:48,代码来源:class-solvease-wp-roles-capabilities_cap_functionality.php

示例15: import_user

 /**
  * Import a single user
  * 
  * @param array $user
  * @return array
  */
 protected function import_user($user)
 {
     $local_user = get_user_by('login', $user['data']['user_login']);
     $local_user_object = new WP_User($local_user->ID);
     $update = !empty($local_user) ? true : false;
     if (!function_exists('wp_insert_user')) {
         include_once ABSPATH . 'wp-includes/registration.php';
     }
     // args used by wp_insert_user & wp_update_user
     // makes for an easy merge and a reminder of just what is handled at that time
     $insert_user_args = array('user_login' => null, 'user_nicename' => null, 'user_url' => null, 'user_email' => null, 'display_name' => null, 'nickname' => null, 'first_name' => null, 'last_name' => null, 'description' => null, 'rich_editing' => null, 'user_registered' => null, 'role' => null, 'use_ssl' => 0, 'admin_color' => null, 'comment_shortcuts' => null);
     foreach (_wp_get_user_contactmethods() as $contact_method => $contact_method_name) {
         $insert_user_args[$contact_method] = null;
     }
     cfd_tmp_dbg('importing_user.txt', $user, 'print');
     foreach ($insert_user_args as $key => &$arg) {
         if ($key == 'role') {
             $arg = $user['roles'][0];
         } else {
             if (!empty($user['data'][$key])) {
                 $arg = $user['data'][$key];
             }
         }
     }
     cfd_tmp_dbg('importing_user_args.txt', $insert_user_args, 'print');
     if ($update) {
         $local_userdata = get_object_vars(get_userdata($local_user->ID));
         $insert_user_args = array_merge($local_userdata, $insert_user_args);
         unset($insert_user_args['user_pass']);
         $user_id = wp_update_user($insert_user_args);
     } else {
         if (email_exists($user['data']['user_email'])) {
             $this->add_import_message('users', '__error__', sprintf(__('Email address "%s" already exists for another user', 'cf-deploy'), $user['data']['user_email']));
             return false;
         }
         // set generic password for new user
         $insert_user_args['user_password'] = time();
         $user_id = wp_insert_user($insert_user_args);
     }
     if (empty($user_id) || is_wp_error($user_id)) {
         $errstring = sprintf(__('Import failed for user "%s".', 'cf-deploy'), $user['data']['user_nicename']);
         if (is_wp_error($user_id)) {
             $errstring .= ' ' . __('Error:', 'cf-deploy') . ' ' . $user_id->get_error_message();
         }
         $this->add_import_message('users', '__error__', $errstring);
         $ret = false;
     } else {
         // Set/Update Capabilities & Roles
         $u = new WP_User($user_id);
         // set roles, remove all existing and replace with what is being brought in
         foreach ($u->roles as $role) {
             $u->remove_role($role);
         }
         foreach ($user['roles'] as $role) {
             $u->add_role($role);
         }
         // set caps, remove all existing caps before setting them anew
         $u->remove_all_caps();
         foreach ($user['caps'] as $cap => $value) {
             $u->add_cap($cap, (bool) $value);
         }
         $this->add_import_message('users', '__notice__', sprintf(__('User "%s" successfully imported.', 'cf-deploy'), $user['data']['user_login']));
         $ret = true;
     }
     $item_change['users'][$user['data']['user_login']] = 'new';
     if (!empty($local_user)) {
         $log_users = array($local_user_object);
         array_walk_recursive($log_users, array($this, 'object_to_array'));
         $item_change['users'][$user['data']['user_login']] = current($log_users);
     }
     $this->log_item_change($item_change);
     return $ret;
 }
开发者ID:niko-lgdcom,项目名称:wp-install,代码行数:79,代码来源:deploy.class.php


注:本文中的WP_User::add_role方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。