zkoprek

Untitled

Feb 15th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.45 KB | None | 0 0
  1. <?php
  2. /*
  3.   Plugin Name: WPWA User Manager
  4.   Plugin URI:
  5.   Description: User management module for the portfolio management application.
  6.   Author: Rakhitha Nimesh
  7.   Version: 1.0
  8.   Author URI: http://www.innovativephp.com/
  9. */
  10.  
  11. class WPWA_User_Manager {
  12.  
  13.     public function __construct() {
  14.         // Creates all the user types
  15.         register_activation_hook( __FILE__ , array( $this, 'add_application_user_roles' ) );
  16.         // Remove unused user roles
  17.         register_activation_hook( __FILE__, array( $this, 'remove_application_user_roles' ) );
  18.         // Create custom capabilities for user roles
  19.         register_activation_hook( __FILE__, array( $this, 'add_application_user_capabilities' ) );
  20.  
  21.         register_activation_hook( __FILE__, array( $this, 'flush_application_rewrite_rules' ) );
  22.  
  23.         add_action( 'template_redirect', array( $this, 'front_controller' ) );
  24.  
  25.         add_action( 'init', array( $this, 'manage_user_routes' ) );
  26.  
  27.         //add_action('wpwa_register_user', array($this, 'validate_user'));
  28.         add_action( 'wpwa_register_user', array( $this, 'register_user' ) );
  29.         add_action( 'wpwa_login_user', array( $this, 'login_user' ) );
  30.         add_action( 'wpwa_activate_user', array( $this, 'activate_user' ) );
  31.         add_filter( 'authenticate', array( $this, 'authenticate_user' ), 30, 3 );
  32.  
  33.         add_filter( 'query_vars', array( $this, 'manage_user_routes_query_vars' ) );
  34.  
  35.         add_action( 'wp_enqueue_scripts', array( $this, 'generate_styles' ) );
  36.     }
  37.  
  38. //    /*
  39. //     * Add extra validation on user registration
  40. //     *
  41. //     * @param  -
  42. //     * @return void
  43. //     */
  44. //    public function validate_user() {
  45. //        remove_action('wpwa_register_user', array($this, 'register_user'));
  46. //    }
  47.  
  48.     /*
  49.      * Add new user roles to application on activation
  50.      *
  51.      * @param  -
  52.      * @return void
  53.     */
  54.  
  55.     public function add_application_user_roles() {
  56.         add_role( 'follower', 'Follower', array( 'read' => true ) );
  57.         add_role( 'developer', 'Developer', array( 'read' => true ) );
  58.         add_role( 'member', 'Member', array( 'read' => true ) );
  59.     }
  60.  
  61.     /*
  62.      * Remove existing user roles from application on activation
  63.      *
  64.      * @param  -
  65.      * @return void
  66.     */
  67.  
  68.     public function remove_application_user_roles() {
  69.         remove_role( 'author' );
  70.         remove_role( 'editor' );
  71.         remove_role( 'contributor' );
  72.         remove_role( 'subscriber' );
  73.     }
  74.  
  75.     /*
  76.      * Add capabilities to user roles on activation
  77.      *
  78.      * @param  -
  79.      * @return void
  80.     */
  81.  
  82.     public function add_application_user_capabilities() {
  83.         $role = get_role( 'follower' );
  84.         $role->add_cap( 'follow_developer_activities' );
  85.     }
  86.  
  87.     /*
  88.      * Activate user account using the link
  89.      *
  90.      * @param  -
  91.      * @return void
  92.     */
  93.  
  94.     public function activate_user() {
  95.  
  96.         $activation_code = isset( $_GET['activation_code'] ) ? $_GET['activation_code'] : '';
  97.         $message = '';
  98.  
  99.         // Get activation record for the user
  100.         $user_query = new WP_User_Query(
  101.                 array(
  102.                         'meta_key' => 'activation_code',
  103.                         'meta_value' => $activation_code
  104.                 )
  105.         );
  106.  
  107.         $users = $user_query->get_results();
  108.  
  109.         // Check and update activation status
  110.         if ( !empty($users) ) {
  111.             $user_id = $users[0]->ID;
  112.             update_user_meta( $user_id, 'activation_status', 'active' );
  113.             $message = 'Account activated successfully. ';
  114.         } else {
  115.             $message = 'Invalid Activation Code';
  116.         }
  117.  
  118.         include dirname(__FILE__) . '/templates/info.php';
  119.         exit;
  120.     }
  121.  
  122.     /*
  123.      * Log the user into the system
  124.      *
  125.      * @param  -
  126.      * @return void
  127.     */
  128.     public function login_user() {
  129.         if ( $_POST ) {
  130.  
  131.             $errors = array();
  132.  
  133.             $username = isset ( $_POST['username'] ) ? $_POST['username'] : '';
  134.             $password = isset ( $_POST['password'] ) ? $_POST['password'] : '';
  135.            
  136.             if ( empty( $username ) )
  137.                 array_push( $errors, 'Please enter a username.' );
  138.  
  139.             if ( empty( $password ) )
  140.                 array_push( $errors, 'Please enter password.' );
  141.  
  142.             if(count($errors) > 0){
  143.                 include dirname(__FILE__) . '/templates/login.php';
  144.                 exit;
  145.             }
  146.  
  147.             $credentials = array();
  148.            
  149.             $credentials['user_login']      = $username;
  150.             $credentials['user_login']      = sanitize_user( $credentials['user_login'] );
  151.             $credentials['user_password']   = $password;
  152.             $credentials['remember']        = false;
  153.  
  154.             $user = wp_signon( $credentials, false );
  155.             if ( is_wp_error( $user ) )
  156.                 array_push( $errors, $user->get_error_message() );
  157.             else
  158.                 wp_redirect( home_url() );
  159.         }
  160.  
  161.         if ( !is_user_logged_in() ) {
  162.             include dirname(__FILE__) . '/templates/login.php';
  163.         } else {
  164.             wp_redirect( home_url() );
  165.         }
  166.         exit;
  167.     }
  168.  
  169.     /*
  170.      * Execute extra validations in user authentication
  171.      *
  172.      * @param  object  User
  173.      * @param  string  Username of the authenticated user
  174.      * @param  string  Password of the authenticated user
  175.      * @return object  User Object or Error Object
  176.     */
  177.     public function authenticate_user( $user, $username, $password ) {
  178.         if ( !in_array( 'administrator', (array) $user->roles ) ) {
  179.             $active_status = '';
  180.             $active_status = get_user_meta( $user->data->ID, 'activation_status', true );
  181.  
  182.             if ( 'inactive' == $active_status ) {
  183.                 $user = new WP_Error( 'denied', __('<strong>ERROR</strong>: Please activate your account.' ) );
  184.             }
  185.         }
  186.         return $user;
  187.     }
  188.  
  189.     /*
  190.      * Register new application user from frontend
  191.      *
  192.      * @param  -
  193.      * @return void
  194.     */
  195.  
  196.     public function register_user() {
  197.         if ( $_POST ) {
  198.  
  199.             $errors = array();
  200.  
  201.             $user_login = ( isset ( $_POST['user'] ) ? $_POST['user'] : '' );
  202.             $user_email = ( isset ( $_POST['email'] ) ? $_POST['email'] : '' );
  203.             $user_type  = ( isset ( $_POST['user_type'] ) ? $_POST['user_type'] : '' );
  204.  
  205.             // Validating user data
  206.             if ( empty( $user_login ) )
  207.                 array_push( $errors, 'Please enter a username.' );
  208.  
  209.             if ( empty( $user_email ) )
  210.                 array_push( $errors, 'Please enter e-mail.' );
  211.  
  212.             if ( empty( $user_type ) )
  213.                 array_push( $errors, 'Please enter user type.' );
  214.  
  215.  
  216.             $sanitized_user_login = sanitize_user( $user_login );
  217.  
  218.             if ( !empty($user_email) && !is_email( $user_email ) )
  219.                 array_push( $errors, 'Please enter valid email.');
  220.             elseif ( email_exists( $user_email ) )
  221.                 array_push( $errors, 'User with this email already registered.' );
  222.  
  223.             if ( empty( $sanitized_user_login ) || !validate_username( $user_login ) )
  224.                 array_push( $errors, 'Invalid username.' );
  225.             elseif ( username_exists( $sanitized_user_login ) )
  226.                 array_push( $errors, 'Username already exists.' );
  227.  
  228.             if ( empty( $errors ) ) {
  229.                 $user_pass  = wp_generate_password();
  230.                 $user_id    = wp_insert_user( array('user_login' => $sanitized_user_login,
  231.                                                         'user_email' => $user_email,
  232.                                                         'role' => $user_type,
  233.                                                         'user_pass' => $user_pass)
  234.                                             );
  235.  
  236.  
  237.                 if ( !$user_id ) {
  238.                     array_push( $errors, 'Registration failed.' );
  239.                 } else {
  240.                     $activation_code = $this->random_string();
  241.  
  242.                     update_user_meta( $user_id, 'activation_code', $activation_code );
  243.                     update_user_meta( $user_id, 'activation_status', 'inactive' );
  244.                     wp_new_user_notification( $user_id, $user_pass, $activation_code );
  245.  
  246.                     $success_message = "Registration completed successfully. Please check your email for activation link.";
  247.                 }
  248.  
  249.                 if ( !is_user_logged_in() ) {
  250.                     include dirname(__FILE__) . '/templates/login.php';
  251.                     exit;
  252.                 }
  253.             }
  254.         }
  255.         if ( !is_user_logged_in() ) {
  256.             include dirname(__FILE__) . '/templates/register.php';
  257.             exit;
  258.         }
  259.     }
  260.  
  261.     /*
  262.      * Front controller for handling custom routing
  263.      *
  264.      * @param  -
  265.      * @return void
  266.     */
  267.  
  268.     public function front_controller() {
  269.         global $wp_query;
  270.         $control_action = isset ( $wp_query->query_vars['control_action'] ) ? $wp_query->query_vars['control_action'] : ''; ;
  271.         switch ( $control_action ) {
  272.             case 'register':
  273.                 do_action( 'wpwa_register_user' );
  274.                 break;
  275.  
  276.             case 'login':
  277.                 do_action( 'wpwa_login_user' );
  278.                 break;
  279.  
  280.             case 'activate':
  281.                 do_action( 'wpwa_activate_user' );
  282.                 break;
  283.         }
  284.     }
  285.  
  286.     /*
  287.      * Add custom routinng rules
  288.      *
  289.      * @param  -
  290.      * @return void
  291.     */
  292.  
  293.     public function manage_user_routes() {
  294.         add_rewrite_rule( '^user/([^/]+)/?', 'index.php?control_action=$matches[1]', 'top' );
  295.     }
  296.  
  297.     /*
  298.      * Flush and rest application rewrite rules on activation
  299.      *
  300.      * @param  -
  301.      * @return void
  302.     */
  303.  
  304.     public function flush_application_rewrite_rules() {
  305.         $this->manage_user_routes();
  306.         flush_rewrite_rules();
  307.     }
  308.  
  309.     /*
  310.      * Add custom query variables to WordPress
  311.      *
  312.      * @param  array  List of built-in query variables of WordPress
  313.      * @return void
  314.     */
  315.  
  316.     public function manage_user_routes_query_vars( $query_vars ) {
  317.         $query_vars[] = 'control_action';
  318.         return $query_vars;
  319.     }
  320.  
  321.     /*
  322.      * Generate random string for activation code
  323.      *
  324.      * @param  -
  325.      * @return string
  326.     */
  327.     public function random_string() {
  328.         $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  329.         $randstr = '';
  330.         for ( $i = 0; $i < 15; $i++ ) {
  331.             $randstr .= $characters[rand(0, strlen( $characters ))];
  332.         }
  333.         return $randstr;
  334.     }
  335.  
  336.     /*
  337.      * Include neccessary styles for the plugin
  338.      *
  339.      * @param  -  
  340.      * @return void
  341.     */
  342.     public function generate_styles() {
  343.         wp_register_style( 'user_styles', plugins_url( 'css/style.css', __FILE__ ) );
  344.         wp_enqueue_style( 'user_styles' );
  345.     }
  346.  
  347. }
  348.  
  349. $user_manege = new WPWA_User_Manager();
  350.  
  351.  
  352.  
  353.  
  354.  
  355. /*
  356.  *  Overriden version of wp_new_user_notification function
  357.  *  for sending activation code
  358. */
  359.  
  360. if ( !function_exists( 'wp_new_user_notification' ) ) {
  361.  
  362.     function wp_new_user_notification($user_id, $plaintext_pass = '', $activate_code = '') {
  363.  
  364.         $user = new WP_User($user_id);
  365.  
  366.         $user_login = stripslashes($user->user_login);
  367.         $user_email = stripslashes($user->user_email);
  368.  
  369.         $message = sprintf(__('New user registration on %s:'), get_option('blogname')) . '\r\n\r\n';
  370.         $message .= sprintf(__('Username: %s'), $user_login) . '\r\n\r\n';
  371.         $message .= sprintf(__('E-mail: %s'), $user_email) . '\r\n';
  372.  
  373.         @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
  374.  
  375.         if (empty($plaintext_pass))
  376.             return;
  377.  
  378.         $activate_link = site_url() . "/user/activate/?activation_code=$activate_code";
  379.  
  380.         $message = __('Hi there,') . '\r\n\r\n';
  381.         $message .= sprintf(__('Welcome to %s! Please activate your account using the link:'), get_option('blogname')) . '\r\n\r\n';
  382.         $message .= sprintf(__('<a href="%s">%s</a>'), $activate_link, $activate_link) . '\r\n';
  383.         $message .= sprintf(__('Username: %s'), $user_login) . '\r\n';
  384.         $message .= sprintf(__('Password: %s'), $plaintext_pass) . '\r\n\r\n';
  385.  
  386.         wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
  387.     }
  388.  
  389. }
Add Comment
Please, Sign In to add comment