| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Transfer Ownership |
| 4 | Plugin URI: http://wordpress.org/ |
| 5 | Description: This provides a UI for the owner of a blog to transfer ownership to a different administrator. |
| 6 | Author: Alex Shiels |
| 7 | Version: 1.0 |
| 8 | Author URI: http://thresholdstate.com/ |
| 9 | */ |
| 10 | |
| 11 | class transfer_ownership { |
| 12 | |
| 13 | var $errors = array(); |
| 14 | |
| 15 | function _show_errors() { |
| 16 | if ( $this->errors ) { |
| 17 | echo '<div id="message" class="error">'; |
| 18 | foreach ($this->errors as $err) { |
| 19 | echo "<p>{$err}</p>"; |
| 20 | } |
| 21 | echo '</div>'; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | function transfer_ownership() { |
| 26 | $this->reallydeleteblog = false; |
| 27 | add_action('admin_menu', array(&$this, 'admin_menu')); |
| 28 | add_action('admin_footer', array(&$this, 'admin_footer')); |
| 29 | } |
| 30 | |
| 31 | function admin_footer() { |
| 32 | global $wpdb; |
| 33 | |
| 34 | if( $this->reallydeleteblog == true ) { |
| 35 | wpmu_delete_blog( $wpdb->blogid ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | function admin_menu() { |
| 40 | add_submenu_page('users.php', __('Transfer Ownership'), __('Transfer Ownership'), 'transfer_ownership', 'transfer-ownership', array(&$this, 'plugin_content')); |
| 41 | } |
| 42 | |
| 43 | function plugin_content() { |
| 44 | if ( !empty($_POST['confirm']) ) |
| 45 | return $this->confirm_post(); |
| 46 | elseif ( !empty($_POST['transfer']) ) |
| 47 | return $this->transfer_post(); |
| 48 | else |
| 49 | return $this->transfer_form(); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | function transfer_form() { |
| 54 | global $current_user; |
| 55 | |
| 56 | $blogurl = get_option('home'); |
| 57 | $blogname = get_bloginfo('name'); |
| 58 | |
| 59 | // find all the administrator users |
| 60 | $users = get_users_of_blog(); |
| 61 | $administrators = array(); |
| 62 | foreach ($users as $user) { |
| 63 | $_user = new WP_User($user->user_id); |
| 64 | if ( $_user->has_cap('administrator') ) |
| 65 | $administrators[] = $user; |
| 66 | } |
| 67 | |
| 68 | if ( count($administrators) < 2 ) { ?> |
| 69 | <div class="wrap"> |
| 70 | <h2><?php _e('Transfer Ownership'); ?></h2> |
| 71 | <p><?php printf(__('You are currently the owner of <a href="%s">%s</a>. You may use this page to transfer ownership of this blog to someone else. First you need to make that person an Administrator.'), $blogurl); ?></p> |
| 72 | </div><?php |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | ?> |
| 77 | <div class="wrap"> |
| 78 | <?php $this->_show_errors(); ?> |
| 79 | <h2><?php _e('Transfer Ownership'); ?></h2> |
| 80 | <p><?php printf(__('You are currently the owner of <a href="%s">%s</a>. You may use this page to transfer ownership of this blog to another administrator.'), $blogurl, $blogname); ?></p> |
| 81 | <p><strong><?php _e('This is a permanent change that cannot be undone.'); ?></strong></p> |
| 82 | |
| 83 | <form method="POST" action=""> |
| 84 | <h3><?php _e('New Owner:'); ?></h3> |
| 85 | <table class="widefat"> |
| 86 | <tbody> |
| 87 | <tr class="thead"> |
| 88 | <th scope="col" class="check-column"></th> |
| 89 | <th><?php _e('Username') ?></th> |
| 90 | <th><?php _e('Name') ?></th> |
| 91 | <th><?php _e('E-mail') ?></th> |
| 92 | </tr> |
| 93 | </tbody> |
| 94 | <tbody id="users" class="list:user user-list"> |
| 95 | |
| 96 | <?php |
| 97 | foreach ( $administrators as $candidate ) { |
| 98 | echo "<tr>"; |
| 99 | $checked = ''; |
| 100 | if ( $candidate->user_id == $current_user->ID ) |
| 101 | $checked = " checked='checked'"; |
| 102 | echo "<th scope='row' class='check-column'><input type='radio' name='new-owner' id='new-owner-{$candidate->user_id}' value='{$candidate->user_id}'{$checked} /></th>\n"; |
| 103 | echo "<td><label for='new-owner-{$candidate->user_id}'>{$candidate->user_login}</label></td>"; |
| 104 | echo "<td>{$candidate->first_name} {$candidate->last_name}</td>"; |
| 105 | echo "<td>{$candidate->user_email}</td>"; |
| 106 | echo "</tr>\n"; |
| 107 | } |
| 108 | ?> |
| 109 | </tbody> |
| 110 | </table> |
| 111 | <?php wp_nonce_field('transfer-owner') ?> |
| 112 | <p class="submit"><input type="submit" name="transfer" value="<?php _e('Transfer'); ?>" /></p> |
| 113 | </form> |
| 114 | </div> |
| 115 | <?php |
| 116 | } |
| 117 | |
| 118 | function transfer_post() { |
| 119 | check_admin_referer('transfer-owner'); |
| 120 | |
| 121 | global $current_user; |
| 122 | |
| 123 | if ( $_POST['new-owner'] == $current_user->ID ) { |
| 124 | $this->errors[] = __('You are already the owner.'); |
| 125 | return $this->transfer_form(); |
| 126 | } |
| 127 | |
| 128 | // no action, just show the confirmation form |
| 129 | return $this->confirm_form(); |
| 130 | } |
| 131 | |
| 132 | function confirm_form() { |
| 133 | $new_owner_id = intval($_POST['new-owner']); |
| 134 | $new_owner = new WP_User($new_owner_id); |
| 135 | |
| 136 | ?> |
| 137 | <div class="wrap"> |
| 138 | <?php $this->_show_errors(); ?> |
| 139 | <h2><?php _e('Confirm Transfer of Ownership'); ?></h2> |
| 140 | <p><?php printf( __('Please confirm that you would like to transfer ownership of the blog <a href="%s">%s</a> to %s (%s).'), get_option('home'), get_bloginfo('name'), $new_owner->user_login, $new_owner->user_email); ?> |
| 141 | </p> |
| 142 | <p><?php _e('You must enter your password to complete the transfer.'); ?></p> |
| 143 | <form method="POST" action=""> |
| 144 | <p><label for="confirm-password"><?php _e('Password:'); ?></label> |
| 145 | <input type="password" name="password" id="confirm-password" /> |
| 146 | <input type="hidden" name="new-owner" value="<?php echo $new_owner_id; ?>" /> |
| 147 | <?php wp_nonce_field('confirm-owner') ?> |
| 148 | </p> |
| 149 | <p class="submit"><input type="submit" name="confirm" value="<?php _e('Confirm'); ?>" /></p> |
| 150 | </form> |
| 151 | |
| 152 | </div> |
| 153 | <?php |
| 154 | } |
| 155 | |
| 156 | function confirm_post() { |
| 157 | check_admin_referer('confirm-owner'); |
| 158 | if ( !current_user_can('transfer_ownership') ) |
| 159 | wp_die(__('You can’t transfer ownership.')); |
| 160 | |
| 161 | global $current_user; |
| 162 | $auth = wp_authenticate($current_user->user_login, $_POST['password']); |
| 163 | if ( is_wp_error($auth) ) { |
| 164 | $this->errors[] = $auth->get_error_message(); |
| 165 | return $this->confirm_form(); |
| 166 | } |
| 167 | |
| 168 | $new_owner = new WP_User( intval($_POST['new-owner']) ); |
| 169 | |
| 170 | // make sure the new owner is valid |
| 171 | if ( empty($new_owner->ID) or !$new_owner->has_cap('administrator') ) { |
| 172 | $this->errors[] = __('Invalid user selected'); |
| 173 | return $this->transfer_form(); |
| 174 | } |
| 175 | |
| 176 | // transfer ownership |
| 177 | $new_owner->add_cap('owner'); |
| 178 | $current_user->remove_cap('owner'); |
| 179 | |
| 180 | ?> |
| 181 | <div id="message" class="updated fade"> |
| 182 | <p><?php printf( __('%s is the new owner of <a href="%s">%s</a>.'), $new_owner->user_login, get_option('home'), get_bloginfo('name') ); ?></p> |
| 183 | </div> |
| 184 | <? |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | $transfer_ownership = new transfer_ownership(); |
| 189 | |
| 190 | ?> |
| 191 | No newline at end of file |