|
<?php |
|
|
|
/* |
|
Plugin Name: Custom Registration |
|
Description: Updates user rating based on number of posts. |
|
Version: 1.1 |
|
Author: Tristan Slater w/ Agbonghama Collins |
|
Author URI: http://kanso.ca |
|
*/ |
|
|
|
|
|
|
|
///////////////// |
|
// PLUGIN CORE // |
|
///////////////// |
|
function cr(&$fields, &$errors) { |
|
|
|
// Check args and replace if necessary |
|
if (!is_array($fields)) $fields = array(); |
|
if (!is_wp_error($errors)) $errors = new WP_Error; |
|
|
|
// Check for form submit |
|
if (isset($_POST['submit'])) { |
|
|
|
// Get fields from submitted form |
|
$fields = cr_get_fields(); |
|
|
|
// Validate fields and produce errors |
|
if (cr_validate($fields, $errors)) { |
|
|
|
// If successful, register user |
|
$user_id = wp_insert_user($fields); |
|
|
|
// update user metadata |
|
update_user_meta( $user_id, 'wpcf-user-phone', $fields['phone_number'] );///////insert user phone number |
|
update_user_meta( $user_id, 'wpcf-user-assigned-agent', $fields['agent'] );/////insert agent assigned |
|
update_user_meta( $user_id, 'wpcf-user-join-date', time()); /////insert account creation date |
|
|
|
// Send the new user the welcome email |
|
wp_newuser_notification($user_id,$fields['user_pass']); |
|
|
|
// And display a message |
|
echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.'; |
|
|
|
// Clear field data |
|
$fields = array(); |
|
} |
|
} |
|
|
|
// Santitize fields |
|
cr_sanitize($fields); |
|
|
|
// Generate form |
|
cr_display_form($fields, $errors); |
|
} |
|
|
|
function cr_sanitize(&$fields) { |
|
$fields['user_login'] = isset($fields['user_email']) ? sanitize_email($fields['user_email']) : ''; |
|
$fields['user_pass'] = isset($fields['user_pass']) ? esc_attr($fields['user_pass']) : ''; |
|
$fields['user_email'] = isset($fields['user_email']) ? sanitize_email($fields['user_email']) : ''; |
|
$fields['phone_number'] = isset($fields['phone_number']) ? sanitize_text_field($fields['phone_number']) : ''; |
|
$fields['first_name'] = isset($fields['first_name']) ? sanitize_text_field($fields['first_name']) : ''; |
|
$fields['last_name'] = isset($fields['last_name']) ? sanitize_text_field($fields['last_name']) : ''; |
|
$fields['agent'] = isset($fields['agent']) ? sanitize_text_field($fields['agent']) : ''; |
|
} |
|
|
|
function cr_display_form($fields = array(), $errors = null) { |
|
|
|
// Check for wp error obj and see if it has any errors |
|
if (is_wp_error($errors) && count($errors->get_error_messages()) > 0) { |
|
|
|
// Display errors |
|
?><ul><?php |
|
foreach ($errors->get_error_messages() as $key => $val) { |
|
?><li> |
|
<?php echo $val; ?> |
|
</li><?php |
|
} |
|
?></ul><?php |
|
} |
|
|
|
// Display Register form |
|
|
|
?><form action="<?php $_SERVER['REQUEST_URI'] ?>" method="post"> |
|
<div> |
|
<label for="email">Email <strong>*</strong></label> |
|
<input placeholder="Email" type="text" name="user_email" value="<?php echo (isset($fields['user_email']) ? $fields['user_email'] : '') ?>"> |
|
</div> |
|
<div> |
|
<label for="user_pass">Password</label> |
|
<input placeholder="Password" type="password" name="user_pass"> |
|
</div> |
|
<div> |
|
<label for="firstname">First Name</label> |
|
<input placeholder="First Name" type="text" name="first_name" value="<?php echo (isset($fields['first_name']) ? $fields['first_name'] : '') ?>"> |
|
</div> |
|
<div> |
|
<label for="last_name">Last Name</label> |
|
<input placeholder="Last Name" type="text" name="last_name" value="<?php echo (isset($fields['last_name']) ? $fields['last_name'] : '') ?>"> |
|
</div> |
|
<div> |
|
<label for="phone_number">Phone Number</label> |
|
<input placeholder="Phone Number" type="text" name="phone_number" value="<?php echo (isset($fields['phone_number']) ? $fields['phone_number'] : '') ?>"> |
|
</div> |
|
<div> |
|
<label for="agent">Agent</label> |
|
<select name="agent" > |
|
<option value="">Agent</option> |
|
<?php |
|
$agentData = getAgentsData(); |
|
foreach($agentData as $agent => $value): |
|
echo '<option value="'.$value[0].'">'.$value[1].'</option>'; //close your tags!! |
|
endforeach; |
|
?> |
|
|
|
</select> |
|
</div> |
|
<input type="submit" name="submit" class="btn" value="Register"> |
|
</form><?php |
|
} |
|
|
|
function cr_get_fields() { |
|
return array( |
|
'user_login' => isset($_POST['user_email']) ? $_POST['user_email'] : '', |
|
'user_pass' => isset($_POST['user_pass']) ? $_POST['user_pass'] : '', |
|
'user_email' => isset($_POST['user_email']) ? $_POST['user_email'] : '', |
|
'phone_number' => isset($_POST['phone_number']) ? $_POST['phone_number'] : '', |
|
'first_name' => isset($_POST['first_name']) ? $_POST['first_name'] : '', |
|
'last_name' => isset($_POST['last_name']) ? $_POST['last_name'] : '', |
|
'agent' => isset($_POST['agent']) ? $_POST['agent'] : '' |
|
|
|
); |
|
} |
|
|
|
function cr_validate(&$fields, &$errors) { |
|
|
|
// Make sure there is a proper wp error obj |
|
// If not, make one |
|
if (!is_wp_error($errors)) $errors = new WP_Error; |
|
|
|
// Validate form data |
|
|
|
if (empty($fields['user_email'])) { |
|
$errors->add('field', 'An email is required and we will email you the rest.'); |
|
} |
|
|
|
if (empty($fields['user_pass'])) { |
|
$fields['user_pass'] = wp_generate_password ( 12, false ); |
|
} |
|
|
|
if (!is_email($fields['user_email'])) { |
|
$errors->add('email_invalid', 'Email is not valid'); |
|
} |
|
|
|
if (email_exists($fields['user_email'])) { |
|
$errors->add('email', 'Email Already in use'); |
|
} |
|
|
|
// If errors were produced, fail |
|
if (count($errors->get_error_messages()) > 0) { |
|
return false; |
|
} |
|
|
|
// Else, success! |
|
return true; |
|
} |
|
|
|
// Redefine user notification function |
|
function wp_newuser_notification( $user_id, $plaintext_pass) { |
|
$user = new WP_User($user_id); |
|
|
|
$user_login = stripslashes($user->user_login); |
|
$user_email = stripslashes($user->user_email); |
|
|
|
$message = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\r\n\r\n"; |
|
$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; |
|
$message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n"; |
|
|
|
@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message); |
|
|
|
if ( empty($plaintext_pass) ) |
|
return; |
|
|
|
$message = __('Hi there,') . "\r\n\r\n"; |
|
$message .= sprintf(__("Welcome to %s! Here's how to log in:"), get_option('blogname')) . "\r\n\r\n"; |
|
$message .= wp_login_url() . "\r\n"; |
|
$message .= sprintf(__('Username: %s'), $user_login) . "\r\n"; |
|
$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n\r\n"; |
|
$message .= sprintf(__('If you have any problems, please contact me at %s.'), get_option('admin_email')) . "\r\n\r\n"; |
|
$message .= __('Adios!'); |
|
|
|
wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message); |
|
|
|
} |
|
|
|
// Redefine user notification function |
|
function getAgentsData() { |
|
$agentData = array(); |
|
$args=array( |
|
'posts_per_page'=>100, // Number of related posts that will be shown. |
|
'caller_get_posts'=>1, |
|
'post_type'=>'agents', |
|
); |
|
$my_query = new wp_query( $args ); |
|
if( $my_query->have_posts() ) { |
|
while( $my_query->have_posts() ) { |
|
$my_query->the_post(); |
|
$agentID = $my_query->post->ID; |
|
$agentName = types_render_field( "agent-first-name", array('output'=>'raw'))." ".types_render_field( "agent-last-name", array('output'=>'raw')); |
|
array_push($agentData,array($agentID,$agentName)); |
|
} |
|
} |
|
return $agentData; |
|
} |
|
|
|
////////////////////////////////////////////////////////////////Log user login date |
|
function loginDateWrite($user_login, $user) { |
|
$user_id = $user->ID; |
|
add_user_meta( $user_id, 'wpcf-user-last-logged-in', time()); |
|
} |
|
add_action('wp_login', 'loginDateWrite', 10, 2); |
|
|
|
//////////////////////////////////////////////////////////////// |
|
function storeCurrentURL(){ |
|
$user_id = get_current_user_id(); |
|
if ($user_id >=1){ |
|
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; |
|
add_user_meta( $user_id, 'wpcf-user-visits', $actual_link); |
|
} |
|
} |
|
add_action( 'wp_footer', 'storeCurrentURL' ); |
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////// |
|
|
|
/////////////// |
|
// SHORTCODE // |
|
/////////////// |
|
|
|
// The callback function for the [cr] shortcode |
|
function cr_cb() { |
|
$fields = array(); |
|
$errors = new WP_Error(); |
|
|
|
// Buffer output |
|
ob_start(); |
|
|
|
// Custom registration, go! |
|
cr($fields, $errors); |
|
|
|
// Return buffer |
|
return ob_get_clean(); |
|
} |
|
add_shortcode('cr', 'cr_cb'); |