/** function to check user's membership record while login and logout **/ function civi_member_sync_check() { civicrm_wp_initialize(); global $wpdb; global $current_user, $currentUserID, $currentUserEmail; //get username in post while login if (!empty($_POST['log'])) { $username = $_POST['log']; /*$userDetails = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE user_login =%s", $username ) ); $currentUserID = $userDetails[0]->ID;*/ $current_user = get_user_by('login', $username); $currentUserID = $current_user->ID; $currentUserEmail = $current_user->user_email; //getting current logged in user's role //$current_user_role = new WP_User( $currentUserID ); $current_user_role = $current_user->roles[0]; //getting user's civi contact id and checkmembership details if ($current_user_role != 'administrator') { require_once 'CRM/Core/Config.php'; $config = CRM_Core_Config::singleton(); require_once 'api/api.php'; $params = array('version' => '3', 'page' => 'CiviCRM', 'q' => 'civicrm/ajax/rest', 'sequential' => '1', 'uf_name' => $currentUserEmail); $contactDetails = civicrm_api("UFMatch", "get", $params); $contactID = $contactDetails['values'][0]['contact_id']; if (!empty($contactID)) { $member = CrmSync::member_check($contactID, $currentUserID, $current_user_role); } } } return true; }
/** function to check membership record and assign wordpress role based on the membership status * input params * #Wordpress UserID and * #User Role **/ function member_check($userID, $userRoles) { if (in_array('administrator', $userRoles)) { return; } civicrm_wp_initialize(); try { $contactID = civicrm_api3('UFMatch', 'getvalue', array('sequential' => 1, 'return' => 'contact_id', 'uf_id' => $userID)); } catch (CiviCRM_API3_Exception $ex) { return; } $memberships = civicrm_api3('Membership', 'get', array('sequential' => 1, 'contact_id' => $contactID)); if ($memberships['count'] == 0) { return; } foreach ($memberships['values'] as $membership) { $syncRules = role_sync_rules($membership['membership_type_id']); if (!$syncRules) { continue; } $activeStatuses = unserialize($syncRules->current_rule); $expiredStatuses = unserialize($syncRules->expiry_rule); if (in_array($membership['status_id'], $activeStatuses)) { $newRole = strtolower($syncRules->wp_role); if ($newRole == $current_user_role) { continue; } $user = new WP_User($userID); $user->set_role($newRole); } elseif (in_array($membership['status_id'], $expiredStatuses)) { $newRole = strtolower($syncRules->expire_wp_role); if ($newRole) { $user = new WP_User($userID); $user->set_role($newRole); } } } }
<div id="icon-edit-pages" class="icon32"></div> <div class="wrap"> <h2 id="add-new-user">Manual Synchronize</h2> <?php civicrm_wp_initialize(); ?> <table class="form-table"> <td> <span>Manual Synchronization:</span> <br/> <?php $sync_confirm_url = get_site_url() . "/wp-admin/admin.php?&action=confirm&page=civi_member_sync/manual_sync.php"; ?> <?php $sync_import_url = get_site_url() . "/wp-admin/admin.php?&action=import&page=civi_member_sync/manual_sync.php"; ?> <input class="button-primary" type="submit" value="Synchronize CiviMember Membership Types to WordPress Roles now" onclick="window.location.href='<?php echo $sync_confirm_url; ?> '"/> <input class="button-primary" type="submit" value="Import CiviMember Members To Wordpress User List" onclick="window.location.href='<?php echo $sync_import_url; ?> '"/> </td> </tr> </table> </div>
function _woocommerce_civicrm_add_update_contact($cid, $order) { if (!civicrm_wp_initialize()) { return; } $action = 'create'; //try { $contact = array(); if ($cid != 0) { try { $params = array('contact_id' => $cid, 'return' => array('id', 'source', 'first_name', 'last_name')); $contact = civicrm_api3('contact', 'getsingle', $params); } catch (CiviCRM_Exception $e) { CRM_Core_Error::debug_log_message('Not able to find contact'); return FALSE; } } // Create contact // Prepare array to update contact via civi API. $email = ''; $fname = ''; $lname = ''; $cid = ''; if (!empty($order->shipping_email)) { $email = $order->shipping_email; $fname = $order->shipping_first_name; $lname = $order->shipping_last_name; } else { $email = $order->billing_email; $fname = $order->billing_first_name; $lname = $order->billing_last_name; } // Try to get contact Id using dedupe $contact['first_name'] = $fname; $contact['last_name'] = $lname; $contact['email'] = $email; $dedupeParams = CRM_Dedupe_Finder::formatParams($contact, 'Individual'); $dedupeParams['check_permission'] = FALSE; $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised'); if ($ids) { $cid = $ids['0']; $action = 'update'; } $contact['sort_name'] = "{$lname}, {$fname}"; $contact['display_name'] = "{$fname} {$lname}"; if (!$cid) { $contact['contact_type'] = 'Individual'; } if (isset($contact['contact_subtype'])) { unset($contact['contact_subtype']); } if (empty($contact['source'])) { $contact['source'] = 'Woocommerce purchase'; } //CRM_Core_Error::debug_var('Contact', $contact); // Create contact or update existing contact. //if ($contact_status == 'new' && !$contact_info['values'][$cid]['first_name'] && !$contact_info['values'][$cid]['last_name']) { try { $result = civicrm_api3('contact', 'create', $contact); $cid = $result['id']; $name = trim($contact['display_name']); $name = !empty($name) ? $contact['display_name'] : $cid; $admin_url = get_admin_url(); $contact_url = "<a href='" . $admin_url . "admin.php?page=CiviCRM&q=civicrm/contact/view&reset=1&cid=" . $cid . "'>View</a>"; // Add order note if ($action == 'update') { $note = 'CiviCRM Contact Updated - ' . $contact_url; } else { $note = 'Created new CiviCRM Contact - ' . $contact_url; } $order->add_order_note($note); } catch (Exception $e) { CRM_Core_Error::debug_log_message('Not able to create/update contact'); return FALSE; } //} //$loc_types = $GLOBALS["WooCommerceCiviCRMLocationTypes"]; $woocommerce_billing = $loc_types['woocommerce-billing']; $woocommerce_shipping = $loc_types['woocommerce-shipping']; $civicrm_billing = get_option('woocommerce_civicrm_billing_location_type_id', 5); $civicrm_shipping = get_option('woocommerce_civicrm_shipping_location_type_id', 1); try { $existing_addresses = civicrm_api3('address', 'get', array('contact_id' => $cid)); $existing_addresses = $existing_addresses['values']; $existing_phones = civicrm_api3('phone', 'get', array('contact_id' => $cid)); $existing_phones = $existing_phones['values']; $shipping_address = $billing_address = array('contact_id' => $cid); $address_types = array('billing', 'shipping'); foreach ($address_types as $address_type) { // Process Phone $phone_exists = FALSE; if (!empty($order->{$address_type . '_phone'})) { $phone = array('phone_type_id' => 1, 'location_type_id' => ${'civicrm_' . $address_type}, 'phone' => $order->{$address_type . '_phone'}, 'contact_id' => $cid); foreach ($existing_phones as $existing_phone) { if ($existing_phone['location_type_id'] == ${'civicrm_' . $address_type}) { $phone['id'] = $existing_phone['id']; } if ($existing_phone['phone'] == $phone['phone']) { $phone_exists = TRUE; } } if (!$phone_exists) { civicrm_api3('phone', 'create', $phone); $note = "Created new CiviCRM Phone of type {$address_type}: {$phone['phone']}"; $order->add_order_note($note); } } // Process Address $address_exists = FALSE; if (!empty($order->{$address_type . '_address_1'}) && !empty($order->{$address_type . '_postcode'})) { // Get country id $country_id = _woocommerce_civicrm_get_country_id($order->{$address_type . '_country'}); $address = array('location_type_id' => ${'civicrm_' . $address_type}, 'city' => $order->{$address_type . '_city'}, 'postal_code' => $order->{$address_type . '_postcode'}, 'name' => $order->{$address_type . '_company'}, 'street_address' => $order->{$address_type . '_address_1'}, 'supplemental_address_1' => $order->{$address_type . '_address_2'}, 'country' => $country_id, 'contact_id' => $cid); foreach ($existing_addresses as $existing) { if ($existing['location_type_id'] == ${'civicrm_' . $address_type}) { $address['id'] = $existing['id']; } elseif ($existing['street_address'] == $address['street_address'] && CRM_Utils_Array::value('supplemental_address_1', $existing) == CRM_Utils_Array::value('supplemental_address_1', $address) && $existing['city'] == $address['city'] && $existing['postal_code'] == $address['postal_code']) { $address_exists = TRUE; } } if (!$address_exists) { civicrm_api3('address', 'create', $address); $note = "Created new CiviCRM Address of type {$address_type}: {$address['street_address']}"; $order->add_order_note($note); } } } } catch (CiviCRM_Exception $e) { CRM_Core_Error::debug_log_message('Not able to add/update address or phone'); } return $cid; //} catch (Exception $e) { // CRM_Core_Error::debug_log_message('Not able to process contact'); // return FALSE; //} }
/** * @return string * @throws CiviCRM_API3_Exception */ static function import_civi_members_to_wordpress() { civicrm_wp_initialize(); $return_message = ''; $account_creation_messages = ''; $member_types_to_sync = array(); $rules = self::get_civi_sync_rules(); $array_counter = 0; foreach ($rules as $key => $value) { $member_types_to_sync[$array_counter] = $value->civi_mem_type; $array_counter += 1; } //have them pull from the CiviSync rules $result_current_members = civicrm_api3('Membership', 'get', array('sequential' => 1, 'return' => array("contact_id"), 'membership_type_id' => array('IN' => $member_types_to_sync))); $current_member_array = array(); foreach ($result_current_members['values'] as $key => $values) { $current_member_array[] = $values['contact_id']; } $result_contacts = civicrm_api3('Contact', 'get', array('sequential' => 1, 'return' => array("first_name", "last_name", "id"), 'id' => array('IN' => $current_member_array))); if (isset($result_contacts)) { foreach ($result_contacts['values'] as $key => $values) { $member_array[$values['id']] = array('email' => '', 'first_name' => $values['first_name'], 'last_name' => $values['last_name']); } } $result_member_emails = civicrm_api3('Email', 'get', array('return' => array("email", "contact_id"), 'contact_id' => array('IN' => $current_member_array), 'is_primary' => 1)); if (isset($result_member_emails)) { foreach ($result_member_emails['values'] as $key => $values) { $member_array[$values['contact_id']]['email'] = $values['email']; } } if (isset($member_array)) { $new_account_count = 0; //TODO: make this more efficient by filtering out the emails that already exist in Wordpress. foreach ($member_array as $key => $values) { //extract the alias part of the email for the new user's username $email = $values['email']; $parts = explode("@", $email); $username = $parts[0]; if (null == username_exists($username) and !empty($email)) { $password = wp_generate_password(12, true); $user_id = wp_create_user($username, $password, $email); if (is_wp_error($user_id)) { echo 'Error creating user ' . $username . ' / ' . $email . ':<br>' . $user_id->get_error_message(); echo '<br>'; } else { $account_creation_messages = $account_creation_messages . 'Created account for user ' . $username . ' ID: ' . $user_id . '. <br>'; wp_update_user(array('ID' => $user_id, 'nickname' => $email, 'first_name' => $values['first_name'], 'last_name' => $values['last_name'])); //Update CiviCRM UFMatch record so that the new Wordpress user is appropriately connected to their Contact record in CiviCrm $user = get_user_by('id', $user_id); /*TODO: this line is not working. Need to research*/ CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $user->ID, $user->email, 'WordPress'); // Email the user wp_mail($email, 'Welcome ' . $username . '!', ' Your Password: '******'Welcome ' . $username . '!', ' Your Password: '******' sent to ' . $email . '<br>'; $new_account_count += 1; } } } $return_message = $return_message . 'Total new accounts created: ' . $new_account_count . '<br>' . $account_creation_messages; } return $return_message; }