/** * @inheritDoc */ public function setTitle($title, $pageTitle = NULL) { if (!$pageTitle) { $pageTitle = $title; } // FIXME: Why is this global? global $civicrm_wp_title; $civicrm_wp_title = $title; // yes, set page title, depending on context $context = civi_wp()->civicrm_context_get(); switch ($context) { case 'admin': case 'shortcode': $template = CRM_Core_Smarty::singleton(); $template->assign('pageTitle', $pageTitle); } }
/** * Create WordPress user from contacts * * @access private * * @param array $rows The contacts data array * @return void */ private function createUsers($rows) { // set debug flag when testing $debug = false; // init debug arrays $users = array(); $messages = array(); // extend PHP's execution time ini_set('max_execution_time', 300); // get default role only once $default_role = get_option('default_role'); // get Civi config object $config = CRM_Core_Config::singleton(); // code for redirect grabbed from CRM_Contact_Form_Task_Delete::postProcess() $urlParams = 'force=1'; $urlString = "civicrm/contact/search/advanced"; // let WordPress plugins know what we're about to do do_action('civicrm_wp_profile_sync_user_add_pre'); // disable Civi's own register hook remove_action('user_register', array(civi_wp(), 'update_user')); remove_action('profile_update', array($this, 'update_user')); // process data foreach ($rows as $row) { // skip if no email if (empty($row['email'])) { continue; } // skip if email is not valid if (!is_email($row['email'])) { continue; } // skip if email already exists if (email_exists($row['email'])) { $messages[] = $row['email'] . ' already exists'; continue; } // filter names $first_name = $this->filterName($row['first_name']); $middle_name = $this->filterName($row['middle_name']); $last_name = $this->filterName($row['last_name']); // lots of first names are simply initials - if so, use both first and middle names if (strlen($first_name) == 1) { $first_name .= $middle_name; } // lets only take a maximum of 8 letters of the last name $last_name = substr($last_name, 0, 8); // concatenate first and last names $uname = $first_name . $last_name; // construct a likely username $uname = sanitize_user($uname); // skip if username not valid if (!validate_username($uname)) { $messages[] = 'username ' . $uname . ' is not valid'; continue; } // does this username already exist? if (username_exists($uname)) { $messages[] = 'username ' . $uname . ' already exists'; $messages[] = $row; // let's try adding in the middle name $uname = $first_name . $middle_name . $last_name; // construct a likely username $uname = sanitize_user($uname); // skip if username not valid if (!validate_username($uname)) { $messages[] = 'username ' . $uname . ' is not valid'; continue; } // skip if this username already exists if (username_exists($uname)) { $messages[] = 'extra username ' . $uname . ' already exists'; continue; } else { $messages[] = 'extra username ' . $uname . ' does not exist - we could add it'; } } /** * We cannot create WP user using CRM_Core_BAO_CMSUser::create() because it * will attempt to log the user in and notify them of their new account. We * have to find another means to do this. * * In the meantime, what follows is cloned from the CiviCRM process for * creating WordPress users and modified accordingly. */ // create an arbitrary password $password = substr(md5(uniqid(microtime())), 0, 8); // populate user data $user_data = array('ID' => '', 'user_login' => $uname, 'user_email' => $row['email'], 'user_pass' => $password, 'nickname' => $uname, 'role' => $default_role, 'first_name' => $row['first_name'], 'last_name' => $row['last_name']); // skip if debugging if (!$debug) { // add the user $user_id = wp_insert_user($user_data); // if contact doesn't already exist create UF Match if ($user_id !== FALSE && isset($row['id'])) { $transaction = new CRM_Core_Transaction(); // create the UF Match record $ufmatch = new CRM_Core_DAO_UFMatch(); $ufmatch->domain_id = CRM_Core_Config::domainID(); $ufmatch->uf_id = $user_id; $ufmatch->contact_id = $row['id']; $ufmatch->uf_name = $row['mail']; if (!$ufmatch->find(TRUE)) { $ufmatch->save(); $ufmatch->free(); $transaction->commit(); } } } else { // add to debug array $users[] = $user_data; } } // if debugging die now if ($debug) { trigger_error(print_r(array('method' => 'createUsers', 'messages' => $messages, 'count' => count($messages), 'users' => $users), true), E_USER_ERROR); die; } // re-enable Civi's register hook add_action('user_register', array(civi_wp(), 'update_user')); // let WordPress plugins know what we've done do_action('civicrm_wp_profile_sync_user_add_post'); // set a message CRM_Core_Session::setStatus('', ts('Users Added to WordPress'), 'success'); // redirect? CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams)); }
/** * Get the Registration link for an EO Event. * * @since 0.2.2 * * @param int $post_id The numeric ID of the WP post * @return string $url The raw URL to the CiviCRM Registration page */ function civicrm_event_organiser_get_register_url($post_id = null) { // init return $url = ''; // bail if no CiviCRM init function if (!function_exists('civi_wp')) { return $url; } // try and init CiviCRM if (!civi_wp()->initialize()) { return $url; } // need the post ID $post_id = absint(empty($post_id) ? get_the_ID() : $post_id); // bail if not present if (empty($post_id)) { return $url; } // get plugin reference $plugin = civicrm_eo(); // get CiviEvents $civi_events = $plugin->db->get_civi_event_ids_by_eo_event_id($post_id); // sanity check if (empty($civi_events)) { return $url; } // get the first CiviEvent, though any would do as they all have the same value $civi_event = $plugin->civi->get_event_by_id(array_shift($civi_events)); // get link for the registration page $url = $plugin->civi->get_registration_link($civi_event); /** * Filter registration URL before returning. * * @param string $url The raw URL to the CiviCRM Registration page * @param string $link The HTML link to the CiviCRM Registration page * @param string $text The text content of the link * @param int $post_id The numeric ID of the WP post */ return apply_filters('civicrm_event_organiser_register_url', $url, $civi_event, $post_id); }
/** * Instance constructor * * @return object $this The object instance */ function __construct() { // store reference to Civi object $this->civi = civi_wp(); return $this; }
/** * Test if CiviCRM plugin is active * * @since 0.1 * * @return bool */ public function is_active() { // bail if no CiviCRM init function if (!function_exists('civi_wp')) { return false; } // try and init CiviCRM return civi_wp()->initialize(); }
/** * Function to get the contact type * @param string $default contact type * @return $ctype contact type */ function civicrm_get_ctype($default = NULL) { return civi_wp()->get_civicrm_contact_type($default); }
/** * Update a WP user when a CiviCRM contact is updated * * @param string $op The type of database operation * @param string $objectName The type of object * @param integer $objectId The ID of the object * @param object $objectRef The object * @return void */ public function civi_contact_updated($op, $objectName, $objectId, $objectRef) { // target our operation if ($op != 'edit') { return; } // target our object type if ($objectName != 'Individual') { return; } $this->_debug(array('function' => 'civi_contact_updated', 'op' => $op, 'objectName' => $objectName, 'objectId' => $objectId, 'objectRef' => $objectRef)); // check if we have a contact email if (!isset($objectRef->email[0]->email)) { // no, init CiviCRM to get WP user ID if (!civi_wp()->initialize()) { return; } // make sure Civi file is included require_once 'CRM/Core/BAO/UFMatch.php'; // search using Civi's logic $user_id = CRM_Core_BAO_UFMatch::getUFId($objectId); // kick out if we didn't get one if (empty($user_id)) { return; } } else { // yes, use it to get WP user $user = get_user_by('email', $objectRef->email[0]->email); // bail if not a WP user if (!$user instanceof WP_User) { return; } // assign ID $user_id = $user->ID; } // update first name update_user_meta($user_id, 'first_name', $objectRef->first_name); // update last name update_user_meta($user_id, 'last_name', $objectRef->last_name); // compatibility with BP XProfile WordPress User Sync plugin if (defined('BP_XPROFILE_WP_USER_SYNC_VERSION')) { // access object global $bp_xprofile_wordpress_user_sync; // call the relevant sync method $bp_xprofile_wordpress_user_sync->intercept_wp_user_update($user_id); } // avoid getting WP user unless we're debugging if (CIVICRM_WP_PROFILE_SYNC_DEBUG) { // for debugging, let get WP user $user = new WP_User($user_id); $this->_debug(array('user' => $user, 'first_name' => $user->first_name, 'last_name' => $user->last_name)); } }
function event_calendar_shortcode_includes() { global $post; // don't parse content when there's no post object, eg on 404 pages. if (!is_object($post)) { return; } // check for existence of shortcode in content. if (preg_match('/\\[event_calendar/', $post->post_content)) { if (!civicrm_initialize()) { return; } // do we have functionality provided by plugin version 4.6+ present? $civi = civi_wp(); if (method_exists($civi, 'front_end_page_load')) { // add core resources for front end add_action('wp', array($civi, 'front_end_page_load'), 100); } else { // add CiviCRM core resources. CRM_Core_Resources::singleton()->addCoreResources(); $config = CRM_Core_Config::singleton(); $config->userFrameworkFrontend = $front_end; } } }