/** * Use this method to prevent excluding something that was not configured by FakerPress * * @param array|int|\WP_User $user The ID for the user or the Object * @return bool */ public static function delete($user) { if (is_array($user)) { $deleted = array(); foreach ($user as $id) { $id = $id instanceof \WP_User ? $id->ID : $id; if (!is_numeric($id)) { continue; } $deleted[$id] = self::delete($id); } return $deleted; } if (is_numeric($user)) { $user = new \WP_User($user); } if (!$user instanceof \WP_User || !$user->exists()) { return false; } $flag = (bool) get_user_meta($user->ID, self::$flag, true); if (true !== $flag) { return false; } return wp_delete_user($user->ID, get_current_user_id()); }
function test_delete_user() { $user_id = $this->factory->user->create(array('role' => 'author')); $user = new WP_User($user_id); $post = array('post_author' => $user_id, 'post_status' => 'publish', 'post_content' => rand_str(), 'post_title' => rand_str(), 'post_type' => 'post'); // insert a post and make sure the ID is ok $post_id = wp_insert_post($post); $this->assertTrue(is_numeric($post_id)); $this->assertTrue($post_id > 0); $post = get_post($post_id); $this->assertEquals($post_id, $post->ID); $post = array('post_author' => $user_id, 'post_status' => 'publish', 'post_content' => rand_str(), 'post_title' => rand_str(), 'post_type' => 'nav_menu_item'); // insert a post and make sure the ID is ok $nav_id = wp_insert_post($post); $this->assertTrue(is_numeric($nav_id)); $this->assertTrue($nav_id > 0); $post = get_post($nav_id); $this->assertEquals($nav_id, $post->ID); wp_delete_user($user_id); $user = new WP_User($user_id); if (is_multisite()) { $this->assertTrue($user->exists()); } else { $this->assertFalse($user->exists()); } $this->assertNotNull(get_post($post_id)); $this->assertEquals('trash', get_post($post_id)->post_status); // nav_menu_item is delete_with_user = false so the nav post should remain published. $this->assertNotNull(get_post($nav_id)); $this->assertEquals('publish', get_post($nav_id)->post_status); wp_delete_post($nav_id, true); $this->assertNull(get_post($nav_id)); wp_delete_post($post_id, true); $this->assertNull(get_post($post_id)); }
/** * @inheritdoc */ public function init($path) { $postId = filter_input(INPUT_GET, 'postid', FILTER_SANITIZE_NUMBER_INT); $post = $postId ? get_post($postId) : false; $allowed = apply_filters(Launcher::SLUG . '_post_types', ['post']); if (!$post instanceof WP_Post || $this->meta->has($post->ID, $post->post_author) || !in_array($post->post_type, $allowed, true)) { return false; } $this->post = $post; $this->author = new WP_User($post->post_author); $this->path = $path; $mail = $this->author && $this->author->exists() ? $this->author->get('user_email') : false; return $mail && filter_var($mail, FILTER_VALIDATE_EMAIL); }
/** * User Meta Shortcode handler * Retrieve the value of a property or meta key from the users and usermeta tables. * usage: [user_meta user_id=1 key="first_name" size="50" wpautop="on" pre="Pre Label " post="Post Label "] * @param array $atts * @param string $content * @return stirng */ function user_meta_shortcode_handler($atts, $content = null) { if (!isset($atts['user_id'])) { $user = wp_get_current_user(); $atts['user_id'] = $user->ID; } if (!isset($atts['size'])) { $atts['size'] = '50'; } $user = new WP_User($atts['user_id']); if (!$user->exists()) { return; } if ($atts['key'] == 'avatar') { return $atts['pre'] . get_avatar($user->ID, $atts['size']) . $atts['post']; } if ($user->has_prop($atts['key'])) { if ($atts['wpautop'] == 'on') { $value = wpautop($user->get($atts['key'])); } else { $value = $user->get($atts['key']); } } if (!empty($value)) { return $atts['pre'] . $value . $atts['post']; } return; }
/** * @ticket BP7243 */ public function test_friendship_should_create_default_initiator_and_friend() { $f = $this->factory->friendship->create_and_get(); $u1 = new WP_User($f->initiator_user_id); $u2 = new WP_User($f->friend_user_id); $this->assertTrue($u1->exists()); $this->assertTrue($u2->exists()); }
function set_as_user_role($role) { // create user $user_id = $this->factory->user->create(array('role' => $role)); $user = new WP_User($user_id); $this->assertTrue($user->exists(), 'Problem getting user ' . $user_id); // log in as user wp_set_current_user($user_id); $this->{$user_id} = $user_id; $this->assertTrue(current_user_can($role)); }
function get_edit_user_link($user_id = null) { if (!$user_id) { $user_id = get_current_user_id(); } if (empty($user_id) || !current_user_can('edit_user', $user_id)) { return ''; } $user = new WP_User($user_id); if (!$user->exists()) { return ''; } if (get_current_user_id() == $user->ID) { $link = get_edit_profile_url($user->ID); } else { $link = add_query_arg('user_id', $user->ID, self_admin_url('user-edit.php')); } return apply_filters('get_edit_user_link', $link, $user->ID); }
/** * Delete a user from the network and remove from all sites. * * @since 3.0.0 * * @todo Merge with wp_delete_user() ? * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $id The user ID. * @return bool True if the user was deleted, otherwise false. */ function wpmu_delete_user($id) { global $wpdb; if (!is_numeric($id)) { return false; } $id = (int) $id; $user = new WP_User($id); if (!$user->exists()) { return false; } // Global super-administrators are protected, and cannot be deleted. $_super_admins = get_super_admins(); if (in_array($user->user_login, $_super_admins, true)) { return false; } /** * Fires before a user is deleted from the network. * * @since MU * * @param int $id ID of the user about to be deleted from the network. */ do_action('wpmu_delete_user', $id); $blogs = get_blogs_of_user($id); if (!empty($blogs)) { foreach ($blogs as $blog) { switch_to_blog($blog->userblog_id); remove_user_from_blog($id, $blog->userblog_id); $post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_author = %d", $id)); foreach ((array) $post_ids as $post_id) { wp_delete_post($post_id); } // Clean links $link_ids = $wpdb->get_col($wpdb->prepare("SELECT link_id FROM {$wpdb->links} WHERE link_owner = %d", $id)); if ($link_ids) { foreach ($link_ids as $link_id) { wp_delete_link($link_id); } } restore_current_blog(); } } $meta = $wpdb->get_col($wpdb->prepare("SELECT umeta_id FROM {$wpdb->usermeta} WHERE user_id = %d", $id)); foreach ($meta as $mid) { delete_metadata_by_mid('user', $mid); } $wpdb->delete($wpdb->users, array('ID' => $id)); clean_user_cache($user); /** This action is documented in wp-admin/includes/user.php */ do_action('deleted_user', $id); return true; }
function xt_new_tixian($tixiandata) { $tixiandata['user_id'] = (int) $tixiandata['user_id']; $user = new WP_User($tixiandata['user_id']); $account_field = XT_USER_ALIPAY; $account = $user->{$account_field}; $account_name_field = XT_USER_ALIPAY_NAME; $account_name = $user->{$account_name_field}; if ($user->exists() && !empty($account) && !empty($account_name)) { global $wpdb; $old = $wpdb->get_row('SELECT * FROM ' . XT_TABLE_TIXIAN . ' WHERE user_id=' . $user->ID . ' AND status=0'); //审核中 if (!empty($old)) { //累加到未审核的 return $wpdb->update(XT_TABLE_TIXIAN, array('cash' => (double) $old->cash + (double) $tixiandata['cash'], 'jifen' => intval($old->jifen) + intval($tixiandata['jifen'])), array('id' => $old->id)); } else { $cashback = (int) xt_fanxian_cashback(); if ((double) $tixiandata['cash'] + intval($tixiandata['jifen']) * 100 < $cashback) { return 0; } $tixiandata['status'] = 0; $tixiandata['cash'] = (double) $tixiandata['cash']; $tixiandata['jifen'] = intval($tixiandata['jifen']); $tixiandata['freeze'] = 0; $tixiandata['freeze_jifen'] = 0; $tixiandata['account'] = $account; $tixiandata['account_name'] = $account_name; $tixiandata['content'] = isset($tixiandata['content']) ? $tixiandata['content'] : ''; $tixiandata['create_time'] = current_time('mysql'); $tixiandata['update_time'] = current_time('mysql'); return xt_insert_tixian($tixiandata); } } }
/** * @return array */ public static function ajax_deleteAdminUser_callback() { /** @var wpdb $wpdb */ global $wpdb; $issueID = absint(!empty($_POST['issueID']) ? $_POST['issueID'] : 0); $wfIssues = new wfIssues(); $issue = $wfIssues->getIssueByID($issueID); if (!$issue) { return array('errorMsg' => "We could not find that issue in our database."); } $data = $issue['data']; if (empty($data['userID'])) { return array('errorMsg' => "We could not find that user in the database."); } $user = new WP_User($data['userID']); if (!$user->exists()) { return array('errorMsg' => "We could not find that user in the database."); } $userLogin = $user->user_login; if (is_multisite() && strcasecmp($user->user_email, get_site_option('admin_email')) === 0) { return array('errorMsg' => "This user's email is the network admin email. It will need to be changed before deleting this user."); } if (is_multisite()) { revoke_super_admin($data['userID']); } wp_delete_user($data['userID']); if (is_multisite()) { $wpdb->delete($wpdb->users, array('ID' => $data['userID'])); } $wfIssues->deleteIssue($issueID); return array('ok' => 1, 'user_login' => $userLogin); }
/** * Whether a particular user has capability or role. * * @since 3.1.0 * * @param int|object $user User ID or object. * @param string $capability Capability or role name. * @return bool */ function user_can( $user, $capability ) { if ( ! is_object( $user ) ) $user = new WP_User( $user ); if ( ! $user || ! $user->exists() ) return false; $args = array_slice( func_get_args(), 2 ); $args = array_merge( array( $capability ), $args ); return call_user_func_array( array( &$user, 'has_cap' ), $args ); }
function test_revoked_super_admin_is_deleted() { if (isset($GLOBALS['super_admins'])) { $old_global = $GLOBALS['super_admins']; unset($GLOBALS['super_admins']); } $user_id = $this->factory->user->create(); grant_super_admin($user_id); revoke_super_admin($user_id); wpmu_delete_user($user_id); $user = new WP_User($user_id); $this->assertFalse($user->exists(), 'WP_User->exists'); if (isset($old_global)) { $GLOBALS['super_admins'] = $old_global; } }
/** * Sanitize new settings field before saving * * @param array|string $input Passed input values to sanitize * @return array|string Sanitized input fields */ public function sanitize_options($input) { $new_input['caps'] = empty($input['caps']) ? 0 : 1; $new_input['only'] = empty($input['only']) ? 0 : 1; $user = new WP_User($input['default']); $new_input['default'] = $user->exists() ? $input['default'] : ''; return $new_input; }
function verify_xml_rpc_signature() { if ($this->xmlrpc_verification) { return $this->xmlrpc_verification; } // It's not for us if (!isset($_GET['token']) || empty($_GET['signature'])) { return false; } @(list($token_key, $version, $user_id) = explode(':', $_GET['token'])); if (empty($token_key) || empty($version) || strval(JETPACK__API_VERSION) !== $version) { return false; } if ('0' === $user_id) { $token_type = 'blog'; $user_id = 0; } else { $token_type = 'user'; if (empty($user_id) || !ctype_digit($user_id)) { return false; } $user_id = (int) $user_id; $user = new WP_User($user_id); if (!$user || !$user->exists()) { return false; } } $token = Jetpack_Data::get_access_token($user_id); if (!$token) { return false; } if (0 !== strpos($token->secret, "{$token_key}.")) { return false; } require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; $jetpack_signature = new Jetpack_Signature($token->secret, (int) Jetpack_Options::get_option('time_diff')); if (isset($_POST['_jetpack_is_multipart'])) { $post_data = $_POST; $file_hashes = array(); foreach ($post_data as $post_data_key => $post_data_value) { if (0 !== strpos($post_data_key, '_jetpack_file_hmac_')) { continue; } $post_data_key = substr($post_data_key, strlen('_jetpack_file_hmac_')); $file_hashes[$post_data_key] = $post_data_value; } foreach ($file_hashes as $post_data_key => $post_data_value) { unset($post_data["_jetpack_file_hmac_{$post_data_key}"]); $post_data[$post_data_key] = $post_data_value; } ksort($post_data); $body = http_build_query(stripslashes_deep($post_data)); } elseif (is_null($this->HTTP_RAW_POST_DATA)) { $body = file_get_contents('php://input'); } else { $body = null; } $signature = $jetpack_signature->sign_current_request(array('body' => is_null($body) ? $this->HTTP_RAW_POST_DATA : $body)); if (!$signature) { return false; } else { if (is_wp_error($signature)) { return $signature; } else { if ($signature !== $_GET['signature']) { return false; } } } $timestamp = (int) $_GET['timestamp']; $nonce = stripslashes((string) $_GET['nonce']); if (!$this->add_nonce($timestamp, $nonce)) { return false; } $this->xmlrpc_verification = array('type' => $token_type, 'user_id' => $token->external_user_id); return $this->xmlrpc_verification; }
function test_page_meta_caps() { // simple tests for some common meta capabilities // Make our author $author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) ); // make a page $page = $this->factory->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) ); // the author of the page $this->assertTrue($author->exists(), "Problem getting user " . $author->ID); // add some other users $admin = new WP_User( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); $author_2 = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) ); $editor = new WP_User( $this->factory->user->create( array( 'role' => 'editor' ) ) ); $contributor = new WP_User( $this->factory->user->create( array( 'role' => 'contributor' ) ) ); // administrators, editors and the post owner can edit it $this->assertTrue($admin->has_cap('edit_page', $page)); $this->assertTrue($editor->has_cap('edit_page', $page)); // other authors and contributors can't $this->assertFalse($author->has_cap('edit_page', $page)); $this->assertFalse($author_2->has_cap('edit_page', $page)); $this->assertFalse($contributor->has_cap('edit_page', $page)); // administrators, editors and the post owner can delete it $this->assertTrue($admin->has_cap('delete_page', $page)); $this->assertTrue($editor->has_cap('delete_page', $page)); // other authors and contributors can't $this->assertFalse($author->has_cap('delete_page', $page)); $this->assertFalse($author_2->has_cap('delete_page', $page)); $this->assertFalse($contributor->has_cap('delete_page', $page)); }
function test_exists() { $user_id = $this->factory->user->create( array( 'role' => 'author' ) ); $user = new WP_User( $user_id ); $this->assertTrue( $user->exists() ); $user = new WP_User( 123456789 ); $this->assertFalse( $user->exists() ); $user = new WP_User( 0 ); $this->assertFalse( $user->exists() ); }
function test_exists() { $user = new WP_User(self::$author_id); $this->assertTrue($user->exists()); $user = new WP_User(123456789); $this->assertFalse($user->exists()); $user = new WP_User(0); $this->assertFalse($user->exists()); }
/** * Get the names of the roles that a user belongs to. * * "Why not just read the $user->roles array directly?", you may ask. Because some popular plugins have a really * nasty bug where they inadvertently remove entries from that array. Specifically, they retrieve the first user * role like this: * * $roleName = array_shift($currentUser->roles); * * What some plugin developers fail to realize is that, in addition to returning the first entry, array_shift() * also *removes* it from the array. As a result, $user->roles is now missing one of the user's roles. This bug * doesn't cause major problems only because most plugins check capabilities and don't care about roles as such. * AME needs to know to determine menu permissions for different roles. * * Known buggy plugins: * - W3 Total Cache 0.9.4.1 * * The current workaround is to cache the role list before it can get corrupted by other plugins. This approach * has its own risks (cache invalidation is hard), but it should be reasonably safe assuming that everyone uses * only standard WP APIs to modify user roles (e.g. @see WP_User::add_role ). * * @param WP_User $user * @return array */ public function get_user_roles($user) { if (empty($user)) { return array(); } if (!$user->exists()) { return $user->roles; } if (!isset($this->cached_user_roles[$user->ID])) { //Note: In rare cases, WP_User::$roles can be false. For AME it's more convenient to have an empty list. $this->cached_user_roles[$user->ID] = !empty($user->roles) ? $user->roles : array(); } return $this->cached_user_roles[$user->ID]; }
/** * In WPCF_Roles::add_caps() we're adding extra capabilities to superadmins. * * When the superadmin status is revoked, we need to take those caps back, otherwise we might create a security * issue. * * This is a temporary workaround for types-768 until a better solution is provided. * * @param int|WP_User $user ID of the user or a WP_User instance that is currently being edited. * @since 2.1 */ public function clean_the_mess_in_nonadmin_user_caps($user) { if (!$user instanceof WP_User) { $user = new WP_User($user); if (!$user->exists()) { return; } } // True if the user is network (super) admin. Also returns True if network mode is disabled and the user is an admin. $is_superadmin = is_super_admin($user->ID); if (!$is_superadmin) { // We'll remove the extra Types capabilities. If the user has a role that adds those capabilities, nothing // should change for them. $wpcf_capabilities = array_keys(self::wpcf_get_capabilities()); foreach ($wpcf_capabilities as $capability) { $user->remove_cap($capability); } } }
private function do_login($uid) { $user = new \WP_User($uid); if (!$user->exists()) { return; } if (is_user_logged_in() && $this->opts('override_logins') === 'no') { return; } wp_set_current_user($uid, $user->user_login); wp_set_auth_cookie($uid); do_action('wp_login', $user->user_login); }
/** * Remove user and optionally reassign posts and links to another user. * * If the $reassign parameter is not assigned to a User ID, then all posts will * be deleted of that user. The action 'delete_user' that is passed the User ID * being deleted will be run after the posts are either reassigned or deleted. * The user meta will also be deleted that are for that User ID. * * @since 2.0.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $id User ID. * @param int $reassign Optional. Reassign posts and links to new User ID. * @return bool True when finished. */ function wp_delete_user($id, $reassign = null) { global $wpdb; if (!is_numeric($id)) { return false; } $id = (int) $id; $user = new WP_User($id); if (!$user->exists()) { return false; } // Normalize $reassign to null or a user ID. 'novalue' was an older default. if ('novalue' === $reassign) { $reassign = null; } elseif (null !== $reassign) { $reassign = (int) $reassign; } /** * Fires immediately before a user is deleted from the database. * * @since 2.0.0 * * @param int $id ID of the user to delete. * @param int|null $reassign ID of the user to reassign posts and links to. * Default null, for no reassignment. */ do_action('delete_user', $id, $reassign); if (null === $reassign) { $post_types_to_delete = array(); foreach (get_post_types(array(), 'objects') as $post_type) { if ($post_type->delete_with_user) { $post_types_to_delete[] = $post_type->name; } elseif (null === $post_type->delete_with_user && post_type_supports($post_type->name, 'author')) { $post_types_to_delete[] = $post_type->name; } } /** * Filter the list of post types to delete with a user. * * @since 3.4.0 * * @param array $post_types_to_delete Post types to delete. * @param int $id User ID. */ $post_types_to_delete = apply_filters('post_types_to_delete_with_user', $post_types_to_delete, $id); $post_types_to_delete = implode("', '", $post_types_to_delete); $post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_author = %d AND post_type IN ('{$post_types_to_delete}')", $id)); if ($post_ids) { foreach ($post_ids as $post_id) { wp_delete_post($post_id); } } // Clean links $link_ids = $wpdb->get_col($wpdb->prepare("SELECT link_id FROM {$wpdb->links} WHERE link_owner = %d", $id)); if ($link_ids) { foreach ($link_ids as $link_id) { wp_delete_link($link_id); } } } else { $post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_author = %d", $id)); $wpdb->update($wpdb->posts, array('post_author' => $reassign), array('post_author' => $id)); if (!empty($post_ids)) { foreach ($post_ids as $post_id) { clean_post_cache($post_id); } } $link_ids = $wpdb->get_col($wpdb->prepare("SELECT link_id FROM {$wpdb->links} WHERE link_owner = %d", $id)); $wpdb->update($wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id)); if (!empty($link_ids)) { foreach ($link_ids as $link_id) { clean_bookmark_cache($link_id); } } } // FINALLY, delete user if (is_multisite()) { remove_user_from_blog($id, get_current_blog_id()); } else { $meta = $wpdb->get_col($wpdb->prepare("SELECT umeta_id FROM {$wpdb->usermeta} WHERE user_id = %d", $id)); foreach ($meta as $mid) { delete_metadata_by_mid('user', $mid); } $wpdb->delete($wpdb->users, array('ID' => $id)); } clean_user_cache($user); /** * Fires immediately after a user is deleted from the database. * * @since 2.9.0 * * @param int $id ID of the deleted user. * @param int|null $reassign ID of the user to reassign posts and links to. * Default null, for no reassignment. */ do_action('deleted_user', $id, $reassign); return true; }
private function UserExists() { return $this->user != null && $this->user->exists(); }
function test_is_user_member_of_blog() { global $wpdb; $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); $old_current = get_current_user_id(); wp_set_current_user( $user1_id ); $this->assertTrue( is_user_member_of_blog() ); $this->assertTrue( is_user_member_of_blog( 0, 0 ) ); $this->assertTrue( is_user_member_of_blog( 0, $wpdb->blogid ) ); $this->assertTrue( is_user_member_of_blog( $user1_id ) ); $this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) ); $blog_ids = $this->factory->blog->create_many( 5 ); foreach ( $blog_ids as $blog_id ) { $this->assertInternalType( 'int', $blog_id ); $this->assertTrue( is_user_member_of_blog( $user1_id, $blog_id ) ); $this->assertTrue( remove_user_from_blog( $user1_id, $blog_id ) ); $this->assertFalse( is_user_member_of_blog( $user1_id, $blog_id ) ); } wpmu_delete_user( $user1_id ); $user = new WP_User( $user1_id ); $this->assertFalse( $user->exists(), 'WP_User->exists' ); $this->assertFalse( is_user_member_of_blog( $user1_id ), 'is_user_member_of_blog' ); wp_set_current_user( $old_current ); }
?> <tr class="refund <?php echo !empty($class) ? $class : ''; ?> " data-order_refund_id="<?php echo $refund->id; ?> "> <td class="check-column"></td> <td class="thumb"><div></div></td> <td class="name"> <?php echo esc_attr__('Refund', 'woocommerce') . ' #' . absint($refund->id) . ' - ' . esc_attr(date_i18n(get_option('date_format') . ', ' . get_option('time_format'), strtotime($refund->post->post_date))); if ($who_refunded->exists()) { echo ' ' . esc_attr_x('by', 'Ex: Refund - $date >by< $username', 'woocommerce') . ' ' . '<abbr class="refund_by" title="' . esc_attr__('ID: ', 'woocommerce') . absint($who_refunded->ID) . '">' . esc_attr($who_refunded->display_name) . '</abbr>'; } ?> <?php if ($refund->get_refund_reason()) { ?> <p class="description"><?php echo esc_html($refund->get_refund_reason()); ?> </p> <?php } ?> <input type="hidden" class="order_refund_id" name="order_refund_id[]" value="<?php echo esc_attr($refund->id);
/** * Clean all user caches * * @since 3.0.0 * * @param WP_User|int $user User object or ID to be cleaned from the cache */ function clean_user_cache($user) { if (is_numeric($user)) { $user = new WP_User($user); } if (!$user->exists()) { return; } wp_cache_delete($user->ID, 'users'); wp_cache_delete($user->user_login, 'userlogins'); wp_cache_delete($user->user_email, 'useremail'); wp_cache_delete($user->user_nicename, 'userslugs'); }
/** * Filter for `get_user_option` queries. For Custom Field mapping. * * @param bool|string $what_wp_says The current field value, if WordPress has found one. Otherwise, FALSE. * @param string $option_name Field slug/name being queried * @param WP_User $user WP_User object related to the `get_user_option` query * * @return mixed */ public static function do_filter_get_user_option($what_wp_says, $option_name, $user) { if ($what_wp_says !== FALSE) { return $what_wp_says; } if (!$user instanceof WP_User || !$user->exists()) { return $what_wp_says; } if (!($user_fields = get_user_option("s2member_custom_fields", $user->ID))) { return $what_wp_says; } if (isset($user_fields[$option_name])) { return $user_fields[$option_name]; } if (stripos($option_name, 's2_') === 0 && ($real_option_name = preg_replace('/^s2_/i', '', $option_name))) { if (isset($user_fields[$real_option_name])) { return $user_fields[$real_option_name]; } } return $what_wp_says; }
/** * Logs access capability times. * * @package s2Member\CCAPS * @since 140514 * * @attaches-to ``add_action('added_user_meta')`` * @attaches-to ``add_action('updated_user_meta')`` * @attaches-to ``add_action('deleted_user_meta')`` (indirectly) * * @param integer $meta_id Meta row ID in database. * @param integer $object_id User ID. * @param string $meta_key Meta key. * @param mixed $meta_value Meta value. */ public static function log_access_cap_times($meta_id, $object_id, $meta_key, $meta_value) { $wpdb = $GLOBALS['wpdb']; /** @var $wpdb \wpdb For IDEs. */ if (strpos($meta_key, 'capabilities') === FALSE || $meta_key !== $wpdb->get_blog_prefix() . 'capabilities') { return; } // Not updating caps. $user_id = (int) $object_id; $user = new WP_User($user_id); if (!$user->ID || !$user->exists()) { return; } // Not a valid user. $caps['prev'] = !empty(self::$prev_caps_by_user[$user_id]) ? self::$prev_caps_by_user[$user_id] : array(); self::$prev_caps_by_user = array(); // Reset this in case `get_user_caps_before_update()` doesn't run somehow. $caps['now'] = is_array($meta_value) ? $meta_value : array(); $role_objects = $GLOBALS['wp_roles']->role_objects; foreach ($caps as &$_caps_prev_now) { foreach (array_intersect(array_keys($_caps_prev_now), array_keys($role_objects)) as $_role) { if ($_caps_prev_now[$_role]) { // If the cap (i.e., the role) is enabled; merge its caps. $_caps_prev_now = array_merge($_caps_prev_now, $role_objects[$_role]->capabilities); } } $_s2_caps_prev_now = array(); foreach ($_caps_prev_now as $_cap => $_enabled) { if (strpos($_cap, 'access_s2member_') === 0) { $_s2_caps_prev_now[substr($_cap, 16)] = $_enabled; } } $_caps_prev_now = $_s2_caps_prev_now; } unset($_s2_caps_prev_now, $_caps_prev_now, $_role, $_cap, $_enabled); $ac_times = get_user_option('s2member_access_cap_times', $user_id); if (!is_array($ac_times)) { $ac_times = array(); } if (!isset(self::$log_time)) { self::$log_time = (double) time(); } foreach ($caps['prev'] as $_cap => $_was_enabled) { if ($_was_enabled && empty($caps['now'][$_cap])) { $ac_times[number_format(self::$log_time += 0.0001, 4, '.', '')] = '-' . $_cap; } } unset($_cap, $_was_enabled); foreach ($caps['now'] as $_cap => $_now_enabled) { if ($_now_enabled && empty($caps['prev'][$_cap])) { $ac_times[number_format(self::$log_time += 0.0001, 4, '.', '')] = $_cap; } } unset($_cap, $_now_enabled); update_user_option($user_id, 's2member_access_cap_times', $ac_times); }
/** * Remove user and optionally reassign posts and links to another user. * * If the $reassign parameter is not assigned to an User ID, then all posts will * be deleted of that user. The action 'delete_user' that is passed the User ID * being deleted will be run after the posts are either reassigned or deleted. * The user meta will also be deleted that are for that User ID. * * @since 2.0.0 * * @param int $id User ID. * @param int $reassign Optional. Reassign posts and links to new User ID. * @return bool True when finished. */ function wp_delete_user($id, $reassign = 'novalue') { global $wpdb; $id = (int) $id; $user = new WP_User($id); if (!$user->exists()) { return false; } // allow for transaction statement do_action('delete_user', $id); if ('novalue' === $reassign || null === $reassign) { $post_types_to_delete = array(); foreach (get_post_types(array(), 'objects') as $post_type) { if ($post_type->delete_with_user) { $post_types_to_delete[] = $post_type->name; } elseif (null === $post_type->delete_with_user && post_type_supports($post_type->name, 'author')) { $post_types_to_delete[] = $post_type->name; } } $post_types_to_delete = apply_filters('post_types_to_delete_with_user', $post_types_to_delete, $id); $post_types_to_delete = implode("', '", $post_types_to_delete); $post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_author = %d AND post_type IN ('{$post_types_to_delete}')", $id)); if ($post_ids) { foreach ($post_ids as $post_id) { wp_delete_post($post_id); } } // Clean links $link_ids = $wpdb->get_col($wpdb->prepare("SELECT link_id FROM {$wpdb->links} WHERE link_owner = %d", $id)); if ($link_ids) { foreach ($link_ids as $link_id) { wp_delete_link($link_id); } } } else { $reassign = (int) $reassign; $post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_author = %d", $id)); $wpdb->update($wpdb->posts, array('post_author' => $reassign), array('post_author' => $id)); if (!empty($post_ids)) { foreach ($post_ids as $post_id) { clean_post_cache($post_id); } } $link_ids = $wpdb->get_col($wpdb->prepare("SELECT link_id FROM {$wpdb->links} WHERE link_owner = %d", $id)); $wpdb->update($wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id)); if (!empty($link_ids)) { foreach ($link_ids as $link_id) { clean_bookmark_cache($link_id); } } } // FINALLY, delete user if (is_multisite()) { remove_user_from_blog($id, get_current_blog_id()); } else { $meta = $wpdb->get_col($wpdb->prepare("SELECT umeta_id FROM {$wpdb->usermeta} WHERE user_id = %d", $id)); foreach ($meta as $mid) { delete_metadata_by_mid('user', $mid); } $wpdb->delete($wpdb->users, array('ID' => $id)); } clean_user_cache($user); // allow for commit transaction do_action('deleted_user', $id); return true; }
/** * Clean all user caches * * @since 3.0.0 * @since 4.4.0 'clean_user_cache' action was added. * * @param WP_User|int $user User object or ID to be cleaned from the cache */ function clean_user_cache($user) { if (is_numeric($user)) { $user = new WP_User($user); } if (!$user->exists()) { return; } wp_cache_delete($user->ID, 'users'); wp_cache_delete($user->user_login, 'userlogins'); wp_cache_delete($user->user_email, 'useremail'); wp_cache_delete($user->user_nicename, 'userslugs'); /** * Fires immediately after the given user's cache is cleaned. * * @since 4.4.0 * * @param int $user_id User ID. * @param WP_User $user User object. */ do_action('clean_user_cache', $user->ID, $user); }
/** * Sets our custom comment cookies on comment submission * * @param object $comment the comment object * @param WP_User $user the WordPress user who submitted the comment */ public function set_comment_cookies($comment, $user) { if ($user->exists()) { return; } $comment_cookie_lifetime = apply_filters('comment_cookie_lifetime', 30000000); $secure = 'https' === parse_url(home_url(), PHP_URL_SCHEME); // Set the name cookie setcookie(self::COMMENT_NAME_COOKIE, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure); // Set the email cookie setcookie(self::COMMENT_EMAIL_COOKIE, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure); // Set the URL cookie setcookie(self::COMMENT_URL_COOKIE, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure); }