Created
July 18, 2014 14:43
-
-
Save rendfall/9fbb5a6231a3dae9d045 to your computer and use it in GitHub Desktop.
User Manager for Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if( ! get_role('simple_user')){ | |
add_role( | |
'simple_user', | |
__( 'Użytkownik' ), | |
array( | |
'read' => true, // true allows this capability | |
) | |
); | |
} | |
class UserManager { | |
protected $errors = array(); | |
public function __construct(){ | |
// do nothing | |
} | |
protected function validateUsername($username){ | |
if(empty($username)) { | |
$this->riseError('Pole nazwa użytkowika nie może być puste.'); | |
return false; | |
} | |
return true; | |
} | |
protected function validateEmail($email){ | |
// format | |
$regex = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/"; | |
if( ! preg_match($regex, $email)) { | |
$this->riseError('Format adresu e-mail jest niepoprawny.'); | |
return false; | |
} | |
// is exists | |
if (email_exists($email)) { | |
$this->riseError('Podany e-mail jest zajęty.'); | |
return false; | |
} | |
return true; | |
} | |
protected function passMismatch($pass1, $pass2){ | |
if($pass1 && $pass2){ | |
if($pass1 == $pass2) { | |
return true; | |
} else { | |
$this->riseError('Hasła muszą być takie same.'); | |
return false; | |
} | |
} else { | |
$this->riseError('Musisz podać hasło.'); | |
return false; | |
} | |
} | |
protected function addUser($username, $password, $email){ | |
if($username && $password && $email){ | |
$role = get_role('simple_user'); | |
$newUserID = wp_create_user($username, $password, $email); | |
// $usedata = array( | |
// 'ID' => $newUserID, | |
// 'user_nicename' => '', | |
// 'user_url' => '', | |
// 'user_email' => $email, | |
// 'display_name' => '', | |
// 'nickname' => '', | |
// 'first_name' => '', | |
// 'last_name' => '', | |
// 'description' => '', | |
// 'rich_editing' => false, | |
// 'show_admin_bar_front' => false, | |
// ); wp_update_user($userdata); | |
$newUser = new WP_User($newUserID); | |
$newUser->set_role($role->name); | |
if(is_wp_error($newUserID)){ | |
return false; | |
} else { | |
return $newUserID; | |
} | |
} else { | |
return false; | |
} | |
} | |
public function registerUser($data){ | |
global $wpdb; | |
$username = $wpdb->escape($data['email']); //$wpdb->escape($data['username']); | |
$email = $wpdb->escape($data['email']); | |
$pass1 = $wpdb->escape($data['pass1']); | |
$pass2 = $wpdb->escape($data['pass2']); | |
if(false == $this->validateUsername($username)) return false; | |
if(false == $this->validateEmail($email)) return false; | |
if(false == $this->passMismatch($pass1, $pass2)) return false; | |
if($this->addUser($username, $pass1, $email)){ | |
//$from = get_option('admin_email'); | |
$from = 'noreply@domain.pl'; | |
$headers = 'From: '.$from . "\r\n"; | |
$subject = "Wiadomość ze strony domain.pl - rejestracja użytkownika"; | |
$msg = "Rejestracja nowego użytkownika.\n\nDane do logowania:\nAdres email: $username\nHasło: $pass1"; | |
wp_mail( $email, $subject, $msg, $headers ); // TODO: handle email notifications | |
wp_mail( $from, $subject, $msg, $headers ); // send copy | |
return true; //"Pomyślnie dodano użytkownika"; | |
} else { | |
return false; | |
} | |
} | |
public function loginUser($data){ | |
global $wpdb; | |
$creds = array(); | |
$creds['user_login'] = $wpdb->escape($data['log']); | |
$creds['user_password'] = $wpdb->escape($data['pwd']); | |
$creds['remember'] = true; | |
if(false == $this->validateUsername($creds['user_login'])) return false; | |
if(empty($creds['user_password'])) { | |
$this->riseError('Pole hasła nie może być puste.'); | |
return false; | |
} | |
//$user = wp_signon($creds, false); | |
$user = wp_signon(); | |
if (is_wp_error($user)){ | |
$this->riseError('Podano niewłaściwe hasło lub nazwę użytkownika.'); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
public function resetPassword($user, $newpass) { | |
do_action('password_reset', $user, $newpass); | |
wp_set_password($newpass, $user->ID); | |
wp_password_change_notification($user); // TODO: change default mail to dedicated | |
} | |
protected function getFile($file){ | |
$path = dirname(__FILE__) .'/'. $file; | |
if(file_exists($path)){ | |
include_once($path); | |
} else { | |
return false; | |
} | |
} | |
protected function riseError($text){ | |
$this->errors[] = $text; | |
} | |
public function getLastMessage(){ | |
return end($this->errors); | |
} | |
public function renderLoginForm(){ | |
$this->getFile('loginform.php'); | |
} | |
public function renderRegistrationForm(){ | |
$this->getFile('registrationform.php'); | |
} | |
static function getAllUsers($role = 'simple_user'){ | |
$args = array( | |
'number' => 0, | |
'role' => $role | |
); | |
$users = new WP_User_Query($args); | |
return $users->results; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment