/** * Wrapper function to send notification with WP-CRM or without one * @param mixed $args['user'] * @param sting $args['trigger_action'] * @param sting $args['data'] aka $notification_data * @param sting $args['crm_log_message'] * @param sting $args['subject'] using in email notification * @param sting $args['message'] using in email notification * @uses self::replace_data() * @uses wp_crm_send_notification() * @return boolean false if notification was not sent successfully * @autor odokienko@UD */ public static function send_notification($args = array()) { $args = wp_parse_args($args, array('ignore_wp_crm' => false, 'user' => false, 'trigger_action' => false, 'data' => array(), 'message' => '', 'subject' => '', 'crm_log_message' => '')); if (is_numeric($args['user'])) { $args['user'] = get_user_by('id', $args['user']); } elseif (filter_var($args['user'], FILTER_VALIDATE_EMAIL)) { $args['user'] = get_user_by('email', $args['user']); } elseif (is_string($args['user'])) { $args['user'] = get_user_by('login', $args['user']); } if (!is_object($args['user']) || empty($args['user']->data->user_email)) { return false; } if (function_exists('wp_crm_send_notification') && empty($args['ignore_wp_crm'])) { if (!empty($args['crm_log_message'])) { wp_crm_add_to_user_log($args['user']->ID, self::replace_data($args['crm_log_message'], $args['data'])); } if (!empty($args['trigger_action']) && is_callable('WP_CRM_N', 'get_trigger_action_notification')) { $notifications = WP_CRM_N::get_trigger_action_notification($args['trigger_action']); if (!empty($notifications)) { return wp_crm_send_notification($args['trigger_action'], $args['data']); } } } if (empty($args['message'])) { return false; } return wp_mail($args['user']->data->user_email, self::replace_data($args['subject'], $args['data']), self::replace_data($args['message'], $args['data'])); }
/** * Processes contact form via ajax request. * * @todo add security precautions to filter out potential SQL injections or bad data (such as account escalation) * @version 1.0 * Copyright 2011 Andy Potanin, Usability Dynamics, Inc. <*****@*****.**> */ function process_crm_message() { global $wp_crm; //** Server seems to return nothing somethines, adding space in beginning seems to solve */ /** This needs to be removed - it causes a warning when the header items are set later in the code, when then causes the form NOT to work echo ' '; */ //** watch for spam */ if (!empty($_REQUEST['comment']) || !empty($_REQUEST['email']) || !empty($_REQUEST['name']) || !empty($_REQUEST['url'])) { die(json_encode(array('success' => 'false', 'message' => __('If you see this message, WP-CRM through you were a robot. Please contact admin if you do not think are you one.', 'wp_crm')))); } $data = $_REQUEST['wp_crm']; $crm_action = $_REQUEST['crm_action']; if (empty($data)) { die; } //** Some other security */ if (isset($data['user_data']['user_id'])) { //** Fail - user_id will never be passed in this manner unless somebody is screwing around */ die(json_encode(array('success' => 'false', 'message' => __('Form could not be submitted.', 'wp_crm')))); } $md5_form_slug = $_REQUEST['form_slug']; $associated_object = $_REQUEST['associated_object']; foreach ($wp_crm['wp_crm_contact_system_data'] as $form_slug => $form_data) { if ($md5_form_slug == md5($form_slug)) { $confirmed_form_slug = $form_slug; $confirmed_form_data = $form_data; continue; } } if (!$confirmed_form_slug) { die; } if (isset($data['user_id'])) { //** User ID was passsed. Verify that current user is logged in */ $current_user = wp_get_current_user(); if (0 == $current_user->ID || $data['user_id'] != $current_user->ID) { //** User ID not found, or passed doesn't match. Either way, fail with ambigous messages. die(json_encode(array('success' => 'false', 'message' => __('Form could not be submitted.', 'wp_crm')))); } else { //** We have User ID, we are updating an existing profile */ $data['user_data']['user_id']['default'][] = $current_user->ID; } } //** Get required fields */ foreach ($wp_crm['data_structure']['attributes'] as $field_slug => $field_data) { if ($field_data['required']) { $required_fields[] = $field_slug; } } $check_fields = apply_filters('wp_crm_distinct_user_fields', array('user_email')); //** Do not check any fields if nothing to check */ foreach ($data['user_data'] as $field_slug => $field_data) { foreach ($field_data as $value) { $value = WP_CRM_F::get_first_value($value); //** Check for completion */ if ($wp_crm['data_structure']['attributes'][$field_slug]['required']) { $error = apply_filters('wp_crm_contact_form_data_validation', false, array('field' => $field_slug, 'value' => $value)); if ($error) { $bad_fields[$field_slug] = $error; continue; } if (empty($value)) { $bad_fields[$field_slug] = sprintf(__('%1s cannot be empty.', 'wp_crm'), $wp_crm['data_structure']['attributes'][$field_slug]['title']); } } //** Check for data conlicts */ if (is_array($check_fields) && in_array($field_slug, $check_fields)) { //** Current field needs to be checked to avoid conflict */ if ($conflict_user_id = WP_CRM_F::check_data_field($field_slug, $value)) { if ($data['user_data']['user_id']['default'][0] != $conflict_user_id) { $bad_fields[$field_slug] = sprintf(__('This %1s belongs to a registered user, please login.', 'wp_crm'), $wp_crm['data_structure']['attributes'][$field_slug]['title']); } } } } } //** If this is a validation request, we check to make sure everything is good */ if ($crm_action == 'system_validate') { if ($bad_fields) { die(json_encode(array('success' => true, 'validation_passed' => false, 'bad_fields' => $bad_fields))); } else { die(json_encode(array('success' => true, 'validation_passed' => true))); } } if ($bad_fields) { die(json_encode(array('success' => 'false', 'bad_fields' => $bad_fields, 'message' => __('Form could not be submitted. Please make sure you have entered your information properly.', 'wp_crm')))); } $user_data = @wp_crm_save_user_data($data['user_data'], 'default_role=' . $wp_crm['configuration']['new_contact_role'] . '&use_global_messages=false&match_login=true&no_redirect=true&return_detail=true'); if (!$user_data) { if ($confirmed_form_data['message_field'] == 'on') { //** If contact form includes a message, notify that message could not be sent */ die(json_encode(array('success' => 'false', 'message' => __('Message could not be sent. Please make sure you have entered your information properly.', 'wp_crm')))); } else { //** If contact form DOES NOT include a message, notify that it could not be submitted */ die(json_encode(array('success' => 'false', 'message' => __('Form could not be submitted. Please make sure you have entered your information properly.', 'wp_crm')))); } } else { $user_id = $user_data['user_id']; if ($user_data['new_user']) { //** Log in DB that this account was created automatically via contact form */ update_user_meta($user_id, 'wpc_cm_generated_account', true); } } $message = WP_CRM_F::get_first_value($_REQUEST['wp_crm']['user_data']['message_field']); if ($confirmed_form_data['notify_with_blank_message'] != 'on' && empty($message)) { //** No message submitted */ } else { if (empty($message)) { $message = __(' -- No message. -- ', 'wp_crm'); } //** Message is submitted. Do stuff. */ $message_id = class_contact_messages::insert_message($user_id, $message, $confirmed_form_slug); $associated_object = !empty($associated_object) ? $associated_object : false; if ($associated_object) { class_contact_messages::insert_message_meta($message_id, 'associated_object', $associated_object); } //** Build default notification arguments */ foreach ($wp_crm['data_structure']['attributes'] as $attribute => $attribute_data) { $notification_info[$attribute] = wp_crm_get_value($attribute, $user_id); } $notification_info['message_content'] = stripslashes($message); $notification_info['trigger_action'] = $confirmed_form_data['title']; $notification_info['profile_link'] = admin_url("admin.php?page=wp_crm_add_new&user_id={$user_id}"); /** Add extra filters */ $maybe_notification_info = apply_filters('wp_crm_notification_info', $notification_info, $associated_object); //** Make sure our array wasn't overwritten by a poorly written hooked in function, it shuold never be blank */ if (!empty($maybe_notification_info) || !is_array($maybe_notification_info)) { $notification_info = $maybe_notification_info; } //** Pass the trigger and array of notification arguments to sender function */ wp_crm_send_notification($confirmed_form_slug, $notification_info); } $result = array('success' => 'true', 'message' => $data['success_message']); if (current_user_can('manage_options')) { $result['user_id'] = $user_id; } echo json_encode($result); die; }
/** * Send notification on new_user_registration's ( new user is registered ) event * * @author peshkov@UD * @since 0.35.2 */ static function maybe_send_user_register_notification($user_id) { $action = 'new_user_registration'; if (!is_callable('WP_CRM_N', 'get_trigger_action_notification')) { include_once WP_CRM_Path . '/core/notification.php'; } $notifications = WP_CRM_N::get_trigger_action_notification($action); if (!empty($notifications)) { $userdata = get_userdata($user_id); if (!empty($userdata)) { wp_crm_send_notification($action, array('user_id' => $userdata->ID, 'user_login' => $userdata->user_login, 'user_email' => $userdata->user_email, 'user_url' => $userdata->user_url, 'display_name' => $userdata->display_name)); } } return $user_id; }
/** * Handle "quick actions" via ajax * * Return json instructions on next action. User by several JS functions. * * @since 0.1 * */ static function quick_action($array = false) { global $wpdb; $action = !empty($_REQUEST['wp_crm_quick_action']) ? $_REQUEST['wp_crm_quick_action'] : false; $object_id = !empty($_REQUEST['object_id']) ? $_REQUEST['object_id'] : false; switch ($action) { case 'reset_user_password': $user_password = wp_generate_password(12, false); if ($object_id && $wpdb->update($wpdb->users, array('user_pass' => wp_hash_password($user_password)), array('ID' => $object_id))) { $user_data = get_userdata($object_id); $user_login = $user_data->user_login; $user_email = $user_data->user_email; $reset_key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM {$wpdb->users} WHERE user_login = %s", $user_login)); if (empty($reset_key)) { $key = wp_generate_password(20, false); $wpdb->update($wpdb->users, array('user_activation_key' => $reset_key), array('user_login' => $user_login)); } wp_crm_add_to_user_log($object_id, __('Password reset. A random password has been generated for user by system.', 'wp_crm')); $args['user_login'] = $user_login; $args['user_email'] = $user_email; $args['user_password'] = $user_password; $args['reset_key'] = $reset_key; $args['reset_url'] = network_site_url("wp-login.php?action=rp&key={$reset_key}&login="******"DELETE FROM {$wpdb->crm_log} WHERE id = {$object_id}")) { $return['success'] = 'true'; $return['message'] = __('Message deleted.', 'wp_crm'); $return['action'] = 'hide_element'; } break; case 'trash_message_and_user': if (current_user_can('delete_users')) { $user_id = $wpdb->get_var("SELECT object_id FROM {$wpdb->crm_log} WHERE id = {$object_id} AND object_type = 'user' "); if ($user_id) { wp_delete_user($user_id); } $return['success'] = 'true'; $return['message'] = __('Sender trashed.', 'wp_crm'); $return['action'] = 'hide_element'; } break; default: $return = apply_filters('wp_crm_quick_action', array('action' => $action, 'object_id' => $object_id)); break; } if (is_array($return)) { return json_encode($return); } else { return false; } }
/** * Sends notification to invoice creator * * @global array $wpi_settings * * @param array $invoice * * @author korotkov@UD * * @refactoring odokienko@UD */ function wp_invoice_send_creator_notification($invoice, $notification_data) { global $wpi_settings; $headers = array("From: {$notification_data['business_name']} <{$notification_data['from']}>\r\n", "Content-Type: text/html"); $subject = sprintf(__("Invoice #%s has been paid", WPI), $notification_data['invoice_id']); $message = sprintf(__("Hello %1s,<br><br>%2s has paid invoice #%3s.<br><br>%4s<br>Total payments: %5s %6s of %7s %8s.<br><br>You can overview invoice status and payment history by clicking this link:<br>%9s<br><br>User information:<br><br>ID: %10s<br>Name: %11s<br>Email: %12s<br><br>--------------------<br>%13s", WPI), $notification_data['creator_name'], $notification_data['user_name'], $notification_data['invoice_id'], $notification_data['invoice_title'], $notification_data['default_currency_code'], $notification_data['total_payments'], $notification_data['default_currency_code'], $notification_data['total'], $notification_data['permalink'], $notification_data['user_id'], $notification_data['user_name'], $notification_data['user_email'], $notification_data['site']); if (function_exists('wp_crm_send_notification') && !empty($wpi_settings['use_wp_crm_to_send_notifications']) && $wpi_settings['use_wp_crm_to_send_notifications'] == 'true') { wp_crm_send_notification('wpi_send_invoice_creator_email', $notification_data); //** Add message to user activity stream */ wp_crm_add_to_user_log($notification_data['creator_id'], sprintf(__("WP-Invoice: Message with subject '%1s' was sent", WPI), $subject), false, array('attribute' => 'wpi_notification')); } else { $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8'); $subject = html_entity_decode($subject, ENT_QUOTES, 'UTF-8'); wp_mail("{$notification_data['creator_name']} <{$notification_data['creator_email']}>", $subject, $message, implode("\r\n", (array) $headers) . "\r\n"); } }