Skip to content

Commit

Permalink
Merge pull request #9 from deenison/develop
Browse files Browse the repository at this point in the history
v1.3.0 - Minor Release
  • Loading branch information
deenison authored Apr 24, 2018
2 parents d662c67 + 2a47f82 commit e0cd969
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 116 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
"bugs": {
"url": "https://github.com/deenison/WP-Lorem-User-Generator/issues"
},
"v": "1.1.0"
"v": "1.3.0"
}
172 changes: 112 additions & 60 deletions src/LoremUserGenerator/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function renderPluginPage()
public static function renderSettingsPage()
{
View::render('page-settings', array(
'defaultUserRole' => get_option('lorem-user-generator:default_user_role'),
'defaultUserRole' => Helper::getDefaultUserRole(),
'usersRoles' => Helper::getUsersRoles()
));
}
Expand All @@ -65,7 +65,7 @@ public static function generateRandomUsers()
|| !DOING_AJAX
|| empty($_GET)
|| !isset($_GET['nonce'])
|| !wp_verify_nonce($_GET['nonce'], LUG_SLUG . '.generate_users')
|| !check_ajax_referer(LUG_SLUG . '.generate_users', 'nonce', false)
) {
throw new \Exception(__('Invalid request.', 'lorem-user-generator'));
}
Expand Down Expand Up @@ -111,22 +111,50 @@ public static function generateRandomUsers()
unset($iso, $nationalities, $nationalitiesPool);
}

$role = isset($_GET['role']) ? trim($_GET['role']) : '';
$usersRoles = Helper::getUsersRoles();
if (!isset($usersRoles[$role])) {
throw new Exception(__('Invalid user role.', 'lorem-user-generator'));
}

$data = Helper::fetchDataFromApi($params);
$data = isset($data->results) ? $data->results : array();

$dataHTML = "";

if (count($data) > 0) {
$usersRoles = Helper::getUsersRoles();
$defaultUserRole = get_option('lorem-user-generator:default_user_role');

foreach ($data as $userIndex => $user) {
$dataHTML .= View::render('result-row', array(
'user' => $user,
'userIndex' => $userIndex,
'usersRoles' => $usersRoles,
'defaultUserRole' => $defaultUserRole
), true);
$skipUsersReview = isset($_GET['skip_review']) && is_numeric($_GET['skip_review']) ? (bool)$_GET['skip_review'] : false;

if ($skipUsersReview) {
foreach ($data as $userIndex => $user) {
$userData = array(
'fname' => $user->name->first,
'lname' => $user->name->last,
'email' => $user->email,
'uname' => $user->login->username,
'pwd' => $user->login->password,
'role' => $role
);

$userId = self::insertUserToDb($userData, $usersRoles);

$dataHTML .= View::render('result-row', array(
'user' => $user,
'userIndex' => $userIndex,
'usersRoles' => $usersRoles,
'defaultUserRole' => $role,
'userId' => $userId
), true);
}
} else {
foreach ($data as $userIndex => $user) {
$dataHTML .= View::render('result-row', array(
'user' => $user,
'userIndex' => $userIndex,
'usersRoles' => $usersRoles,
'defaultUserRole' => $role
), true);
}
}
}

Expand Down Expand Up @@ -166,68 +194,92 @@ public static function addUser()
}

$userIndex = isset($_POST['i']) && is_numeric($_POST['i']) ? (int)$_POST['i'] : -1;
if (!wp_verify_nonce($_POST['nonce'], LUG_SLUG . ':nonce.add_user:' . $userIndex)) {
if (!check_ajax_referer(LUG_SLUG . ':nonce.add_user:' . $userIndex, 'nonce', false)) {
throw new \Exception(__('Invalid nonce.', 'lorem-user-generator'));
}

$missingParamErrorString = _x('Missing "%s" parameter.', '%s: parameter name', 'lorem-user-generator');

$userFirstName = isset($_POST['user_fname']) ? trim((string)$_POST['user_fname']) : "";
if (empty($userFirstName)) {
throw new \Exception(sprintf($missingParamErrorString, 'user_fname'));
$userData = array(
'fname' => isset($_POST['user_fname']) ? trim((string)$_POST['user_fname']) : '',
'lname' => isset($_POST['user_lname']) ? trim((string)$_POST['user_lname']) : '',
'email' => isset($_POST['user_email']) ? trim((string)$_POST['user_email']) : '',
'uname' => isset($_POST['user_uname']) ? trim((string)$_POST['user_uname']) : '',
'pwd' => isset($_POST['user_pwd']) ? trim((string)$_POST['user_pwd']) : '',
'role' => isset($_POST['user_role']) ? trim((string)$_POST['user_role']) : ''
);

$userId = self::insertUserToDb($userData);
if (is_string($userId)) {
throw new Exception($userId);
}

$userLastName = isset($_POST['user_lname']) ? trim((string)$_POST['user_lname']) : "";
if (empty($userLastName)) {
throw new \Exception(sprintf($missingParamErrorString, 'user_lname'));
}
$response['user_role'] = $userData['role'];
$response['user_profile_url'] = admin_url('user-edit.php?user_id=' . $userId);

$userEmail = isset($_POST['user_email']) ? trim((string)$_POST['user_email']) : "";
if (empty($userEmail)) {
throw new \Exception(sprintf($missingParamErrorString, 'user_email'));
}
$response['success'] = true;
} catch (\Exception $e) {
$response['error'] = $e->getMessage();
}

$userUsername = isset($_POST['user_uname']) ? trim((string)$_POST['user_uname']) : "";
if (empty($userUsername)) {
throw new \Exception(sprintf($missingParamErrorString, 'user_uname'));
}
wp_send_json($response);
}

$userPassword = isset($_POST['user_pwd']) ? (string)$_POST['user_pwd'] : "";
if (empty($userPassword)) {
throw new \Exception(sprintf($missingParamErrorString, 'user_pwd'));
}
/**
* Add a set of user data into DB as a new user.
*
* @since @todo
* @access private
* @static
*
* @param mixed $data Can be either plain object or array.
* @param array $userRoles Array of available user roles.
*
* @return The new user id in case of success. Error string otherwise.
*/
private static function insertUserToDb($data, $userRoles = array())
{
$data = json_decode(json_encode((array)$data));

$userRole = isset($_POST['user_role']) ? trim((string)$_POST['user_role']) : "";
$usersRoles = Helper::getUsersRoles();
if (!isset($usersRoles[$userRole])) {
throw new \Exception(sprintf(__('Invalid "%s" parameter.', 'lorem-user-generator'), 'user_role'));
}
if (empty($userRoles)) {
$userRoles = Helper::getUsersRoles();
}

$badParamErrTmpl = _x('Missing/invalid "%s" parameter.', '%s: parameter name', 'lorem-user-generator');

$userData = apply_filters('lorem-user-generator:onBeforeAddUser', array(
'user_login' => $userUsername,
'user_pass' => $userPassword,
'user_nicename' => $userUsername,
'user_email' => $userEmail,
'display_name' => $userFirstName . ' ' . $userLastName,
'nickname' => $userUsername,
'first_name' => $userFirstName,
'last_name' => $userLastName,
'role' => $userRole
));

$userId = wp_insert_user($userData);
if (is_wp_error($userId)) {
throw new \Exception($userId->get_error_message());
$userData = array();

$requiredColumns = array('fname', 'lname', 'email', 'uname', 'pwd', 'role');
foreach ($requiredColumns as $columnName) {
$columnValue = isset($data->{$columnName}) ? trim((string)$data->{$columnName}) : '';
if (empty($columnValue)) {
throw new \Exception(sprintf($badParamErrTmpl, $columnName));
}

$response['user_role'] = $userRole;
$response['user_profile_url'] = admin_url('user-edit.php?user_id=' . $userId);
$data->{$columnName} = $columnValue;
}

$response['success'] = true;
} catch (\Exception $e) {
$response['error'] = $e->getMessage();
if (!isset($userRoles[$data->role])) {
throw new \Exception(sprintf($badParamErrTmpl, 'role'));
}

wp_send_json($response);
$userData = apply_filters('lorem-user-generator:onBeforeAddUser', array(
'user_login' => $data->uname,
'user_pass' => $data->pwd,
'user_nicename' => $data->uname,
'user_email' => $data->email,
'display_name' => $data->fname . ' ' . $data->lname,
'nickname' => $data->uname,
'first_name' => $data->fname,
'last_name' => $data->lname,
'role' => $data->role
));

unset($columnName, $requiredColumns, $data);

$userId = wp_insert_user($userData);
if (is_wp_error($userId)) {
throw new \Exception($userId->get_error_message());
}

return $userId;
}
}
15 changes: 15 additions & 0 deletions src/LoremUserGenerator/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,19 @@ public static function fetchDataFromApi($customParams = array())

return apply_filters('lorem-user-generator:fetchDataFromApi', $response);
}

/**
* Retrieve the default user role's alias.
*
* @since @todo
* @static
*
* @return string
*/
public static function getDefaultUserRole()
{
$defaultUserRole = get_option('lorem-user-generator:default_user_role');

return $defaultUserRole;
}
}
4 changes: 2 additions & 2 deletions src/LoremUserGenerator/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct()
return;
}

self::$namespace = get_class(self::$instance);
self::$namespace = __NAMESPACE__ . '\Plugin';
self::$controllerNamespace = LUG_NAMESPACE . 'Controller';

$this->loadLanguageDomain();
Expand Down Expand Up @@ -148,7 +148,7 @@ public static function enqueueScripts()
if (self::canRunOnCurrentPage()) {
wp_enqueue_script('luser-select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js', array('jquery'), LUG_VERSION);
wp_enqueue_script(LUG_SLUG, LUG_BASE_URL . 'assets/js/lorem-user-generator.js', array('jquery'), LUG_VERSION);
wp_localize_script(LUG_SLUG, '$l', array(
wp_localize_script(LUG_SLUG, '$i18n', array(
'LB_GENERATING' => __('Generating...', 'lorem-user-generator'),
'MSG_RESULTS_WILL_APPEAR_HERE' => __('Results will appear here...', 'lorem-user-generator'),
'LB_GENERATE' => __('Generate', 'lorem-user-generator'),
Expand Down
Loading

0 comments on commit e0cd969

Please sign in to comment.