UM_ADDON_generate_random_users

The Ultimate Member UM ADDON generate random users class.

Defined (1)

The class is defined in the following location(s).

/addons/generate_random_users.php  
  1. class UM_ADDON_generate_random_users { 
  2.  
  3. function __construct() { 
  4.  
  5. add_action('admin_menu', array(&$this, 'admin_menu'), 1001); 
  6.  
  7. add_action('admin_init', array(&$this, 'admin_init'), 1); 
  8.  
  9. add_action('um_admin_addon_hook', array(&$this, 'um_admin_addon_hook') ); 
  10.  
  11.  
  12.  
  13. function admin_menu() { 
  14.  
  15. global $ultimatemember; 
  16. $this->addon = $ultimatemember->addons['generate_random_users']; 
  17. add_submenu_page('ultimatemember', $this->addon[0], $this->addon[0], 'manage_options', 'generate_random_users', array(&$this, 'content') ); 
  18.  
  19.  
  20. function um_admin_addon_hook( $hook ) { 
  21. global $ultimatemember; 
  22.  
  23. switch ( $hook ) { 
  24. case 'generate_random_users': 
  25. $json_url = "https://randomuser.me/api/"; 
  26.  
  27. $arr_post_header = array( ); 
  28.  
  29. if( isset( $_GET['gender'] ) ) { 
  30. $gender = $_GET['gender']; 
  31. if( $gender != 'both' ) { 
  32. $json_url = add_query_arg('gender', $gender, $json_url ); 
  33. $arr_post_header['gender'] = $gender; 
  34.  
  35. if( isset( $_GET['nationality'] ) ) { 
  36. $nationality = implode(", ", $_GET['nationality']); 
  37. if( ! empty( $nationality ) ) { 
  38. $json_url = add_query_arg('nat', $nationality, $json_url ); 
  39. $arr_post_header['nat'] = $nationality; 
  40.  
  41. if( isset( $_GET['total_users'] ) ) { 
  42. $total_users = intval( $_GET['total_users'] ); 
  43. $json_url = add_query_arg('results', $total_users, $json_url ); 
  44. $arr_post_header['results'] = $total_users; 
  45.  
  46.  
  47. $response = wp_remote_get( $json_url, array('timeout' => 120 ) ); 
  48. $json = ''; 
  49.  
  50. if( is_array($response) ) { 
  51. if( isset( $response['body'] ) && ! empty( $response['body'] ) ) { 
  52. $json = json_decode( $response['body'] ); 
  53.  
  54. if( is_wp_error( $response ) ) { 
  55. wp_die( $response->get_error_message() ); 
  56.  
  57. if( ! empty( $json ) ) { 
  58.  
  59. remove_action('um_after_new_user_register', 'um_after_new_user_register', 10, 2); 
  60.  
  61. $failed_dummies = 0; 
  62. $default_role = um_get_option("default_role"); 
  63.  
  64. foreach( $json->results as $dummy ) { 
  65.  
  66. if( isset( $_GET['password'] ) && ! empty( $_GET['password'] ) ) { 
  67. $password = $_GET['password'];  
  68. }else{ 
  69. $password = wp_generate_password( 8, false ); 
  70.  
  71. $site_url = @$_SERVER['SERVER_NAME']; 
  72. $random_generate = wp_generate_password( 4, false ); 
  73.  
  74. if( username_exists( $dummy->login->username ) ) { 
  75. $dummy->login->username = $dummy->login->username.'_'. $random_generate; 
  76.  
  77. $dummy->login->email = $dummy->login->username."_dummy@{$site_url}"; 
  78.  
  79. if( email_exists( $dummy->login->email ) ) { 
  80. $dummy->login->email = $dummy->login->username."_dummy_{$random_generate}@{$site_url}"; 
  81.  
  82. $userdata = array( 
  83. 'display_name' => ucfirst( $dummy->name->first )." ".ucfirst( $dummy->name->last ),  
  84. 'first_name' => ucfirst( $dummy->name->first ),  
  85. 'last_name' => ucfirst( $dummy->name->last ),  
  86. 'user_email' => $dummy->login->email,  
  87. 'user_login' => $dummy->login->username,  
  88. 'user_pass' => $password,  
  89. ); 
  90.  
  91. $user_id = wp_insert_user( $userdata ); 
  92.  
  93. if( is_wp_error( $user_id ) ) { 
  94. $failed_dummies++; 
  95.  
  96. $usermeta = array( 
  97. 'synced_profile_photo' => $dummy->picture->large,  
  98. 'gender' => ucfirst($dummy->gender),  
  99. 'birth_date' => date("Y/m/d", strtotime( $dummy->dob) ),  
  100. '_um_last_login' => date("Y/m/d", strtotime( $dummy->registered ) ),  
  101. 'mobile_number' => $dummy->cell,  
  102. 'phone_number' => $dummy->phone,  
  103. 'synced_gravatar_hashed_id' => md5( strtolower( trim( $dummy->email ) ) ),  
  104. 'account_status' => 'approved',  
  105. '_um_profile_dummy' => true,  
  106. 'role' => isset( $default_role ) ? $default_role: 'member' 
  107. ); 
  108.  
  109. if( isset( $_GET['add_cover_photo'] ) && $_GET['add_cover_photo'] == 1 ) { 
  110.  
  111. $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'); 
  112. $color = $rand[rand(0, 15)].$rand[rand(0, 15)].$rand[rand(0, 15)].$rand[rand(0, 15)].$rand[rand(0, 15)].$rand[rand(0, 15)]; 
  113.  
  114. $usermeta['synced_cover_photo'] = 'http://placehold.it/650x350/'.$color.'/'.$color; 
  115.  
  116.  
  117. foreach ( $usermeta as $key => $value ) { 
  118. update_user_meta( $user_id, $key, $value ); 
  119.  
  120.  
  121. wp_redirect( admin_url("admin.php?page=generate_random_users") ); exit; 
  122.  
  123. break; 
  124.  
  125. case 'remove_random_users': 
  126.  
  127. $dummies = new WP_User_Query( 
  128. array( 
  129. 'fields' => array('ID'),  
  130. 'meta_key' => '_um_profile_dummy',  
  131. 'meta_value' => true,  
  132. 'meta_compare' => '=' 
  133. ); 
  134.  
  135. if( $dummies->total_users > 0 ) { 
  136. foreach ( $dummies->get_results() as $dummy ) { 
  137.  
  138. if( isset( $dummy->ID ) ) { 
  139. wp_delete_user( $dummy->ID ); 
  140.  
  141.  
  142. delete_option('um_generated_dumies'); 
  143. wp_redirect( admin_url("admin.php?page=generate_random_users") ); 
  144. exit; 
  145.  
  146. break; 
  147.  
  148. default: 
  149.  
  150. break; 
  151.  
  152.  
  153. function admin_init() { 
  154. if ( isset( $_REQUEST['um-addon-hook'] ) ) { 
  155. $hook = $_REQUEST['um-addon-hook']; 
  156. do_action("um_admin_addon_hook", $hook ); 
  157.  
  158. function content() { 
  159.  
  160. ?> 
  161.  
  162. <div class="wrap"> 
  163.  
  164. <h2>Ultimate Member <sup style="font-size:15px"><?php echo ultimatemember_version; ?></sup></h2> 
  165.  
  166. <h3><?php echo $this->addon[0]; ?></h3> 
  167.  
  168. <?php if ( isset( $this->content ) ) {  
  169. echo $this->content; 
  170. } else { ?> 
  171.  
  172. <p>This tool allows you to add dummies as Ultimate Member users. </p> 
  173. <?php  
  174. $dummies = new WP_User_Query( 
  175. array( 
  176. 'fields' => array('ID'),  
  177. 'meta_key' => '_um_profile_dummy',  
  178. 'meta_value' => true,  
  179. 'meta_compare' => '=' 
  180. ); 
  181. ?> 
  182. <?php if( $dummies->total_users > 0 ): ?> 
  183. <form method="get"> 
  184. <p> 
  185.  <input type="submit" class="button button-secondary" value="Remove Generated Dummies (<?php echo $dummies->total_users;?>)"/> 
  186. <input type="hidden" name="um-addon-hook" value="remove_random_users"/> 
  187. <input type="hidden" name="page" value="generate_random_users"/> 
  188. </form> 
  189. <br/> 
  190. <?php endif; ?>  
  191. <form method="get"> 
  192. <label for="total_users">How many dummies? <br/><input type="text" name="total_users" value="30" /> <br/><br/> 
  193. <label for="gender">Gender:</label> <br/> 
  194. <label><input type='radio' name="gender" value="male"/> Male</label> <br/>  
  195. <label><input type='radio' name="gender" value="female"/> Female</label> <br/>  
  196. <label><input type='radio' checked="checked" name="gender" value="both"/> Both</label>  
  197. <br/><br/> 
  198. <label for="nationality">Available Nationalities:</label><br/> 
  199. <select style="width:150px" name="nationality[]" multiple> 
  200. <?php  
  201. $nationality = array( 'AU', 'BR', 'CA', 'CH', 'DE', 'DK', 'ES', 'FI', 'FR', 'GB', 'IE', 'IR', 'NL', 'NZ', 'TR', 'US' ); 
  202. foreach ($nationality as $code ) { 
  203. $value = strtolower($code); 
  204. echo "<option value='".$value."'/> ".$code."</option>"; 
  205. ?> 
  206. </select> 
  207. <br/><br/> 
  208. <label for="add_cover_photo"><input type="checkbox" name="add_cover_photo" value="1" /> 
  209. Add cover photos?</label> 
  210. <br/><small>Generates random colored cover photos</small> 
  211. <br/><br/> 
  212. <label for="password"> 
  213. Account Passwords:  
  214. <input type="password" name="password" /> 
  215. <br/><small>if you leave this blank, it will generate random strings password</small> 
  216. </label> 
  217. <br/> 
  218. <input type="submit" class="button button-primary" value="Start Generating Dummies"/> 
  219. <input type="hidden" name="um-addon-hook" value="generate_random_users"/> 
  220.  
  221. <?php } ?> 
  222. </form> 
  223.  
  224. </div><div class="clear"></div> 
  225.  
  226. <?php 
  227.