Advertisement
amereservant

Sharing WPMU Blog Registrations Plugin

Jul 23rd, 2011
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.38 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WPMU Blog Registrations
  4. Plugin URI: http://uglyrobot.com
  5. Description: Allows people to register for an individual blog using the normal registration process. Compatible with plugins like Register Plus. You can also control whether users can adjust their own registration settings.
  6. Version: 1.0
  7. Author: Aaron Edwards
  8. Author URI: mailto:aaron@uglyrobot.com
  9. License: GPL (see http://www.gnu.org/copyleft/gpl.html)
  10.  
  11. Instructions: Drop into wp-content/mu-plugins/ and visit the "Blog Registrations" menu item under Site Admin.
  12.  
  13. Contact me at aaron@uglyrobot.com for custom modifications or any other WPMU/Wordpress development work.
  14. */
  15.  
  16. //declare hooks
  17. add_action( 'login_form_register', 'mp_br_register_page' );
  18. add_action( 'admin_menu', 'mp_br_add_admin_menu' ); //site admin menu
  19. add_action( 'admin_menu', 'mp_br_add_menu' ); //user admin menu
  20. add_action( 'wpmu_new_blog','mp_br_new_blog' ); //auto activation hook
  21.  
  22.  
  23. function mp_br_register_page() {
  24.   global $blog_id, $_POST;
  25.   switch_to_blog($blog_id);
  26.  
  27.   if ( $blog_id == 1 ) {
  28.         wp_redirect('wp-signup.php');
  29.         exit();
  30.     }
  31.  
  32.     if ( !get_blog_option($blog_id,'users_can_register') ) {
  33.         wp_redirect('wp-login.php?registration=disabled');
  34.         exit();
  35.     }
  36.  
  37.     $user_login = '';
  38.     $user_email = '';
  39.     if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
  40.         require_once( ABSPATH . WPINC . '/registration.php');
  41.  
  42.         $user_login = $_POST['user_login'];
  43.         $user_email = $_POST['user_email'];
  44.         $errors = register_new_user($user_login, $user_email);
  45.         if ( !is_wp_error($errors) ) {
  46.             wp_redirect('wp-login.php?checkemail=registered');
  47.             exit();
  48.         }
  49.     }
  50.  
  51.     login_header(__('Registration Form'), '<p class="message register">' . __('Register For This Site') . '</p>', $errors);
  52. ?>
  53.  
  54. <form name="registerform" id="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
  55.     <p>
  56.         <label><?php _e('Username') ?><br />
  57.         <input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" tabindex="10" /></label>
  58.     </p>
  59.     <p>
  60.         <label><?php _e('E-mail') ?><br />
  61.         <input type="text" name="user_email" id="user_email" class="input" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" tabindex="20" /></label>
  62.     </p>
  63. <?php do_action('register_form'); ?>
  64.     <p id="reg_passmail"><?php _e('A password will be e-mailed to you.') ?></p>
  65.     <br class="clear" />
  66.     <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="<?php esc_attr_e('Register'); ?>" tabindex="100" /></p>
  67. </form>
  68.  
  69. <p id="nav">
  70. <a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in') ?></a> |
  71. <a href="<?php echo site_url('wp-login.php?action=lostpassword', 'login') ?>" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a>
  72. </p>
  73.  
  74. </div>
  75.  
  76. <p id="backtoblog"><a href="<?php bloginfo('url'); ?>/" title="<?php _e('Are you lost?') ?>"><?php printf(__('&larr; Back to %s'), get_bloginfo('title', 'display' )); ?></a></p>
  77.  
  78. <script type="text/javascript">
  79. try{document.getElementById('user_login').focus();}catch(e){}
  80. </script>
  81. </body>
  82. </html>
  83. <?php
  84.   exit();
  85. }
  86.  
  87.  
  88. function mp_br_add_admin_menu() {
  89.   if (!is_site_admin()) return;
  90.  
  91.     add_submenu_page('wpmu-admin.php', 'Blog Registrations', 'Blog Registrations', '10', 'blog-registrations', 'mp_br_page' );
  92. }
  93.  
  94.  
  95. function mp_br_page() {
  96.     if (!is_site_admin()) return;
  97.    
  98.     //fix wpmu wonkiness
  99.     remove_filter('option_users_can_register', 'users_can_register_signup_filter');
  100.   $registration = get_site_option('registration');
  101.  
  102.   //setup default options on first load
  103.   if (!get_site_option('mp_br_options')) {
  104.     update_site_option('mp_br_options', array('default_role'=>'subscriber', 'user_control'=>1));
  105.    
  106.     global $wpdb, $site_id;
  107.    
  108.     $query = "SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = $site_id AND blog_id != 1";
  109.     $blog_list = $wpdb->get_results( $query, ARRAY_A );
  110.     foreach ( (array) $blog_list as $blog) {
  111.       update_blog_option( $blog['blog_id'], 'users_can_register', 1 );
  112.     }
  113.   }
  114.  
  115.   if (isset($_POST['Submit'])) {
  116.     if (in_array($_POST['default_role'], array('administrator','editor','author','contributor','subscriber'))) {
  117.    
  118.       $control = ($_POST['user_control']==1) ? 1 : 0;
  119.       update_site_option('mp_br_options', array('default_role'=>$_POST['default_role'], 'user_control'=>$control));
  120.      
  121.       global $wpdb, $site_id;
  122.      
  123.       $query = "SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = $site_id AND blog_id != 1";
  124.       $blog_list = $wpdb->get_results( $query, ARRAY_A );
  125.       $count = 0;
  126.       foreach ( (array) $blog_list as $blog) {
  127.         update_blog_option( $blog['blog_id'], 'users_can_register', 1 );
  128.         update_blog_option( $blog['blog_id'], 'default_role', $_POST['default_role'] );
  129.         $count++;
  130.       }
  131.       $saved = true;
  132.     }
  133.   }
  134.  
  135.   $options = get_site_option('mp_br_options');
  136. ?>
  137. <div class='wrap'>
  138. <?php
  139. if ($saved)
  140.   echo '<div id="message" class="updated fade"><p>Settings Saved For All '.$count.' Blogs!</p></div>';
  141.  
  142. if ( $registration == 'none' || $registration == 'blog' )
  143.   echo '<div id="message" class="error"><p>You need to <a href="wpmu-options.php#registration1">enable user registrations</a>!</p></div>';
  144. ?>
  145. <h2><?php _e('Single Blog Registrations') ?></h2>
  146. <div class="donate-message" style="border:1px gray solid;margin:10px;padding:10px;">
  147.   <iframe src="http://media.missionsplace.com/plugin_iframe.html" width="100%" height="100%">
  148.     <table>
  149.      <tr>
  150.        <td>If you find this plugin useful, please send me a small donation in honor of the time I put into it. Thanks!</td>
  151.        <td>
  152.         <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  153.         <input type="hidden" name="cmd" value="_s-xclick">
  154.         <input type="hidden" name="hosted_button_id" value="5935926">
  155.         <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  156.         <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
  157.         </form>
  158.        </td>
  159.      </tr>
  160.      <tr>
  161.        <td>I provide support and answer feature requests for this plugin exclusively through the forums at <a href="http://premium.wpmudev.org/?ref=prayhumbly-3442">WPMU Dev Premium</a>. If you are not a member, signup using this button and you can post your questions there.</td>
  162.        <td><a href='http://premium.wpmudev.org/?ref=prayhumbly-3442'><img border="0" src='http://premium.wpmudev.org/banners/180x60-banner.png' alt='180x60-banner.png' title='Check Out WPMU DEV Premium' /></a></td>
  163.      </tr>
  164.     </table>
  165.   </iframe>
  166. </div>
  167.  
  168. <p>These are sitewide settings that upon saving will be applied to all existing and future new blogs. Does not update the main site blog (blog_id=1) for security reasons. Note that the default when installing this plugin is for user registrations to be opened and at a subscriber level.</p>
  169. <p class="error">Be warned, as changing these will overwrite every blog's individual settings!</p>
  170. <form method="post" action="">
  171. <table class="form-table">
  172. <tr valign="top">
  173. <th scope="row"><?php _e('Enable User Control') ?></th>
  174. <td> <fieldset><legend class="screen-reader-text"><span><?php _e('Enable User Control') ?></span></legend><label for="user_control">
  175. <input name="user_control" type="checkbox" id="user_control" value="1" <?php checked('1', $options['user_control']); ?> />
  176. <?php _e('Users can update registration settings') ?></label>
  177. </fieldset></td>
  178. </tr>
  179. <tr valign="top">
  180. <th scope="row"><label for="default_role"><?php _e('New User Default Role') ?></label></th>
  181. <td>
  182. <select name="default_role" id="default_role"><?php wp_dropdown_roles( $options['default_role'] ); ?></select>
  183. </td>
  184. </tr>
  185. </table>
  186. <p class="submit">
  187. <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
  188. </p>
  189. </form>
  190.  
  191. </div>
  192. <?php
  193. } //end mp_br_page()
  194.  
  195.  
  196.  
  197. function mp_br_add_menu() {
  198.   global $blog_id;
  199.   //hide menu from main blog
  200.   if ($blog_id==1)
  201.     return;
  202.  
  203.   $options = get_site_option('mp_br_options');
  204.   if ($options['user_control'])
  205.     add_submenu_page('users.php', 'User Registrations', 'User Registrations', 'edit_users', 'user-registrations', 'mp_br_user_page' );
  206. }
  207.  
  208. //page displayed to users if enabled
  209. function mp_br_user_page() {
  210.  
  211.   //temporarily deal with WPMU filter wonkiness
  212.     remove_filter('option_users_can_register', 'users_can_register_signup_filter');
  213.   if (isset($_POST['Submit'])) {
  214.     if (!check_admin_referer('mp_br_user_page')) die("Security Problem");
  215.    
  216.     if (in_array($_POST['default_role'], array('administrator','editor','author','contributor','subscriber'))) {
  217.       $control = ($_POST['users_can_register']==1) ? 1 : 0;
  218.       update_option( 'users_can_register', $control );
  219.       update_option( 'default_role', $_POST['default_role'] );
  220.       $saved = true;
  221.     }
  222.   }
  223. ?>
  224. <div class='wrap'>
  225. <?php
  226. if ($saved)
  227.   echo '<div id="message" class="updated fade"><p>Settings Saved.</p></div>';
  228. ?>
  229. <h2><?php _e('User Registrations') ?></h2>
  230. <form method="post" action="">
  231. <table class="form-table">
  232. <tr valign="top">
  233. <th scope="row"><?php _e('Membership') ?></th>
  234. <td> <fieldset><legend class="screen-reader-text"><span><?php _e('Membership') ?></span></legend><label for="users_can_register">
  235. <input name="users_can_register" type="checkbox" id="users_can_register" value="1" <?php checked('1', get_option('users_can_register')); ?> />
  236. <?php _e('Anyone can register') ?></label>
  237. </fieldset></td>
  238. </tr>
  239. <tr valign="top">
  240. <th scope="row"><label for="default_role"><?php _e('New User Default Role') ?></label></th>
  241. <td>
  242. <select name="default_role" id="default_role"><?php wp_dropdown_roles( get_option('default_role') ); ?></select>
  243. </td>
  244. </tr>
  245. </table>
  246. <?php wp_nonce_field('mp_br_user_page'); ?>
  247. <p class="submit">
  248. <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
  249. </p>
  250. </form>
  251.  
  252. </div>
  253. <?php
  254. } //end mp_br_user_page()
  255.  
  256. //saves defaults on new blog creation
  257. function mp_br_new_blog($blog_id) {
  258.   //fix wpmu wonkiness
  259.     remove_filter('option_users_can_register', 'users_can_register_signup_filter');
  260.    
  261.   update_blog_option( $blog_id, 'users_can_register', 1 );
  262.   $options = get_site_option('mp_br_options');
  263.   update_blog_option( $blog_id, 'default_role', $options['default_role'] );
  264. }
  265. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement