/** * Replaced notification variables with actual values * * @param array $notification_data * @param array $replace_with * @since 0.21 * */ static function replace_notification_values($notification_data = false, $replace_with = false) { if (!is_array($replace_with)) { return; } $notification_keys = array_keys($notification_data); foreach ($replace_with as $key => $value) { if (is_array($value)) { $value = WP_CRM_F::get_first_value($value); } foreach ($notification_data as $n_key => $n_value) { $notification_data[$n_key] = str_replace('[' . $key . ']', $value, $n_value); } } return $notification_data; }
/** * Replaced notification variables with actual values * * @param array $notification_data * @param array $replace_with * @since 0.21 * */ static function replace_notification_values($notification_data = false, $replace_with = false) { if (!is_array($replace_with)) { return; } $associated_object = !empty($_REQUEST['associated_object']) ? get_post($_REQUEST['associated_object']) : false; $notification_keys = array_keys($notification_data); $replace_with['post_id'] = !empty($associated_object) ? $associated_object->ID : ''; $replace_with['post_title'] = !empty($associated_object) ? $associated_object->post_title : ''; $replace_with['post_link'] = !empty($associated_object) ? get_permalink($associated_object->ID) : ''; $replace_with = apply_filters('wp_crm_notification_replace_values', $replace_with); foreach ($replace_with as $key => $value) { if (is_array($value)) { $value = WP_CRM_F::get_first_value($value); } foreach ($notification_data as $n_key => $n_value) { $notification_data[$n_key] = str_replace('[' . $key . ']', $value, $n_value); } } return $notification_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; }
/** * Render custom attributes metabox * @global array $wp_crm * @param array $post * @param array $metabox * @return null * @author korotkov@ud * @todo Maybe we can use crm_page_wp_crm_add_new::primary_information function for this because they are similar */ static function custom_group_metabox($post, $metabox) { global $wp_crm; if (empty($metabox['args']['fields']) && !is_array($metabox['args']['fields'])) { return; } $user_role = WP_CRM_F::get_first_value($post['role']); ?> <table class="form-table"> <?php if (!empty($wp_crm['data_structure']) && is_array($wp_crm['data_structure']['attributes'])) { ?> <?php foreach ($metabox['args']['fields'] as $slug => $attribute) { $row_classes = array(); $row_classes[] = @$attribute['has_options'] ? 'wp_crm_has_options' : 'wp_crm_no_options'; $row_classes[] = @$attribute['required'] == 'true' ? 'wp_crm_required_field' : ''; $row_classes[] = @$attribute['primary'] == 'true' ? 'primary' : 'not_primary'; $row_classes[] = !empty($wp_crm['hidden_attributes'][$user_role]) && is_array($wp_crm['hidden_attributes'][$user_role]) && in_array($slug, $wp_crm['hidden_attributes'][$user_role]) ? 'hidden' : ''; $row_classes[] = 'wp_crm_user_entry_row'; $row_classes[] = "wp_crm_{$slug}_row"; $continue = apply_filters("wp_crm_before_{$slug}_frontend", array('continue' => false, 'values' => $post[$slug], 'attribute' => $attribute, 'args' => $metabox['args'])); if ($continue['continue']) { continue; } ?> <tr meta_key="<?php echo esc_attr($slug); ?> " wp_crm_input_type="<?php echo esc_attr($attribute['input_type']); ?> " class="<?php echo implode(' ', $row_classes); ?> "> <th> <?php if (@$attribute['input_type'] != 'checkbox' || isset($attribute['options'])) { ?> <?php ob_start(); ?> <label for="wp_crm_<?php echo $slug; ?> _field"> <?php echo $attribute['title']; ?> </label> <div class="wp_crm_description"><?php echo $attribute['description']; ?> </div> <?php $label = ob_get_contents(); ob_end_clean(); ?> <?php echo apply_filters('wp_crm_user_input_label', $label, $slug, $attribute, $post); ?> <?php } ?> </th> <td class="wp_crm_user_data_row" wp_crm_attribute="<?php echo $slug; ?> "> <div class="blank_slate hidden" show_attribute="<?php echo $slug; ?> "><?php echo !empty($attribute['blank_message']) ? $attribute['blank_message'] : "Add {$attribute['title']}"; ?> </div> <?php echo WP_CRM_F::user_input_field($slug, $post[$slug], $attribute, $post); ?> <?php if (isset($attribute['allow_multiple']) && $attribute['allow_multiple'] == 'true') { ?> <div class="add_another"><?php _('Add Another'); ?> </div> <?php } ?> </td> </tr> <?php do_action("wp_crm_after_{$slug}", array('values' => !empty($values) ? $values : false, 'attribute' => $attribute, 'user_object' => !empty($user_object) ? $user_object : false, 'args' => !empty($args) ? $args : array())); } ?> <?php } ?> </table> <?php }
function primary_information($user_object) { global $wp_crm; $user_role = WP_CRM_F::get_first_value($user_object['role']); ?> <table class="form-table"> <?php if (!empty($wp_crm['data_structure']) && is_array($wp_crm['data_structure']['attributes'])) { ?> <?php foreach ($wp_crm['data_structure']['attributes'] as $slug => $attribute) { $row_classes = array(); $row_classes[] = @$attribute['has_options'] ? 'wp_crm_has_options' : 'wp_crm_no_options'; $row_classes[] = @$attribute['required'] == 'true' ? 'wp_crm_required_field' : ''; $row_classes[] = @$attribute['primary'] == 'true' ? 'primary' : 'not_primary'; $row_classes[] = is_array($wp_crm['hidden_attributes'][$user_role]) && in_array($slug, $wp_crm['hidden_attributes'][$user_role]) ? 'hidden' : ''; $row_classes[] = 'wp_crm_user_entry_row'; $row_classes[] = "wp_crm_{$slug}_row"; ?> <tr meta_key="<?php echo esc_attr($slug); ?> " wp_crm_input_type="<?php echo esc_attr($attribute['input_type']); ?> " class="<?php echo implode(' ', $row_classes); ?> "> <th> <?php if (@$attribute['input_type'] != 'checkbox' || isset($attribute['options'])) { ?> <?php ob_start(); ?> <label for="wp_crm_<?php echo $slug; ?> _field"> <?php echo $attribute['title']; ?> </label> <div class="wp_crm_description"><?php echo $attribute['description']; ?> </div> <?php $label = ob_get_contents(); ob_end_clean(); ?> <?php echo apply_filters('wp_crm_user_input_label', $label, $slug, $attribute, $user_object); ?> <?php } ?> </th> <td class="wp_crm_user_data_row" wp_crm_attribute="<?php echo $slug; ?> "> <div class="blank_slate hidden" show_attribute="<?php echo $slug; ?> "><?php echo !empty($attribute['blank_message']) ? $attribute['blank_message'] : "Add {$attribute['title']}"; ?> </div> <?php echo WP_CRM_F::user_input_field($slug, $user_object[$slug], $attribute, $user_object); ?> <?php if (isset($attribute['allow_multiple']) && $attribute['allow_multiple'] == 'true') { ?> <div class="add_another"><?php _('Add Another'); ?> </div> <?php } ?> </td> </tr> <?php } ?> <?php } ?> </table> <?php }
/** * * @global type $wp_crm * @param type $user_object */ static function primary_information($user_object) { global $wp_crm; $user_role = WP_CRM_F::get_first_value(!empty($user_object['role']) ? $user_object['role'] : array()); ?> <table class="form-table"> <?php if (isset($_GET['user_id'])) { $user_id = $_GET['user_id']; $user_info = get_user_meta($user_id); $stripe_id = $user_info['stripe_customer_id'][0]; ?> <h2>Order History</h2> <tr> <td><strong>Order No.</strong></td> <td><strong>Order Date</strong></td> <td><strong>Status</strong></td> <td><strong>Tracking #</strong></td> </tr> <?php $boxes = get_user_boxes($user_id); foreach ($boxes as $box) { ?> <tr> <td><?php echo $box['order_id']; ?> </td> <td><?php echo $box['date_requested']; ?> </td> <td><?php echo $box['ship_status']; ?> </td> <td><?php if ($box['tracking_id'] == '') { if ($box['ship_status'] == 'Awaiting Shipment') { echo 'N/A'; } else { $_SESSION['order_id'] = $box['order_id']; $_SESSION['user_id'] = $user_id; $_SESSION['date_requested'] = $box['date_requested']; ?> <a href="/process-shipments.php" class="button add-new-h2"><?php _e('Sync Shipment', 'wp_crm'); ?> </a> <?php } } else { echo $box['tracking_id']; } ?> </td> </tr> <?php } } ?> </table> <table class="form-table"> <?php if (!empty($wp_crm['data_structure']) && is_array($wp_crm['data_structure']['attributes'])) { ?> <?php foreach (apply_filters('wp_crm_primary_information_attributes', $wp_crm['data_structure']['attributes']) as $slug => $attribute) { /* we already have an Actions box to change user pass, so we can just skip it here */ if ($slug == 'user_pass') { continue; } $row_classes = array(); $row_classes[] = @$attribute['has_options'] ? 'wp_crm_has_options' : 'wp_crm_no_options'; $row_classes[] = @$attribute['required'] == 'true' ? 'wp_crm_required_field' : ''; $row_classes[] = @$attribute['primary'] == 'true' ? 'primary' : 'not_primary'; $row_classes[] = !empty($wp_crm['hidden_attributes'][$user_role]) && is_array($wp_crm['hidden_attributes'][$user_role]) && in_array($slug, $wp_crm['hidden_attributes'][$user_role]) ? 'hidden' : ''; $row_classes[] = 'wp_crm_user_entry_row'; $row_classes[] = "wp_crm_{$slug}_row"; ?> <tr meta_key="<?php echo esc_attr($slug); ?> " wp_crm_input_type="<?php echo esc_attr($attribute['input_type']); ?> " class="<?php echo implode(' ', $row_classes); ?> "> <th> <?php ob_start(); ?> <label for="wp_crm_<?php echo $slug; ?> _field"> <?php echo $attribute['title']; ?> </label> <div class="wp_crm_description"><?php echo !empty($attribute['description']) ? $attribute['description'] : ''; ?> </div> <?php $label = ob_get_contents(); ob_end_clean(); ?> <?php echo apply_filters('wp_crm_user_input_label', $label, $slug, $attribute, $user_object); ?> </th> <td class="wp_crm_user_data_row" wp_crm_attribute="<?php echo $slug; ?> "> <div class="blank_slate hidden" show_attribute="<?php echo $slug; ?> "><?php echo !empty($attribute['blank_message']) ? $attribute['blank_message'] : "Add {$attribute['title']}"; ?> </div> <?php echo WP_CRM_F::user_input_field($slug, !empty($user_object[$slug]) ? $user_object[$slug] : '', $attribute, $user_object); ?> <?php if (isset($attribute['allow_multiple']) && $attribute['allow_multiple'] == 'true') { ?> <div class="add_another"><?php _('Add Another'); ?> </div> <?php } ?> </td> </tr> <?php } ?> <?php } ?> </table> <!--<table class="form-table"> <?php if (isset($_GET['user_id'])) { $user_id = $_GET['user_id']; $user_info = get_user_meta($user_id); $stripe_id = $user_info['stripe_customer_id'][0]; ?> <h2>Order History</h2> <tr> <td><strong>Order No.</strong></td> <td><strong>Order Date</strong></td> <td><strong>Status</strong></td> <td><strong>Tracking #</strong></td> </tr> <?php $boxes = get_user_boxes($user_id); foreach ($boxes as $box) { ?> <tr> <td><?php echo $box['order_id']; ?> </td> <td><?php echo $box['date_requested']; ?> </td> <td><?php echo $box['ship_status']; ?> </td> <td><?php echo $box['tracking_id']; ?> </td> </tr> <?php } } ?> </table>--> <?php }
/** * Saves Buddypress profile data. * * @uses WP_CRM_Core::wp_crm_save_user_data() * @param array $data. Request (POST,GET) * @author peshkov@UD */ static function bp_save_profile_data($data) { global $bp; if (empty($data['bp']) || empty($data['user_id'])) { return; } //* Set necessary variables */ $user_id = $data['user_id']; $user_data = $data['wp_crm']['user_data']; $data = $data['bp']; $errors = false; $posted_field_ids = array(); $is_required = array(); //* Set xprofile full name from display_name */ $display_name = WP_CRM_F::get_first_value($user_data['display_name']); if (!empty($display_name)) { $fullname_field_name = bp_xprofile_fullname_field_name(); $fullname_field_id = xprofile_get_field_id_from_name($fullname_field_name); $data["field_{$fullname_field_id}"] = $display_name; } //* Get all posted field ids */ foreach ($data as $name => $value) { $field_id = str_replace(array('field_', '_day', '_month', '_year'), '', $name); array_push($posted_field_ids, $field_id); } $posted_field_ids = array_unique($posted_field_ids); //* Validate the field */ foreach ($posted_field_ids as $field_id) { if (!isset($data['field_' . $field_id])) { if (!empty($data['field_' . $field_id . '_day']) && !empty($data['field_' . $field_id . '_month']) && !empty($data['field_' . $field_id . '_year'])) { /* Concatenate the values */ $date_value = $data['field_' . $field_id . '_day'] . ' ' . $data['field_' . $field_id . '_month'] . ' ' . $data['field_' . $field_id . '_year']; /* Turn the concatenated value into a timestamp */ $data['field_' . $field_id] = date('Y-m-d H:i:s', strtotime($date_value)); } } $is_required[$field_id] = xprofile_check_is_required_field($field_id); if ($is_required[$field_id] && empty($data['field_' . $field_id])) { $errors = true; } } //** There are errors */ if ($errors) { WP_CRM_F::add_message(__('Please make sure you fill in all required Buddypress fields in this profile field group before saving.', ud_get_wp_crm()->domain), 'bad'); //** No errors */ } else { //** Now we've checked for required fields, lets save the values. */ foreach ($posted_field_ids as $field_id) { //** Certain types of fields (checkboxes, multiselects) may come through empty. */ //** Save them as an empty array so that they don't get overwritten by the default on the next edit. */ if (empty($data['field_' . $field_id])) { $value = array(); } else { $value = $data['field_' . $field_id]; } if (!xprofile_set_field_data($field_id, $user_id, $value, $is_required[$field_id])) { $errors = true; } else { do_action('xprofile_profile_field_data_updated', $field_id, $value); } } //** Set the feedback message if we have error */ if ($errors) { WP_CRM_F::add_message(__('There was a problem updating some of Buddypress profile information, please try again.', ud_get_wp_crm()->domain), 'bad'); } } }
/** * Format company on overview page in the main_view cell * * @todo add link to filter down by company * @since 0.1 */ static function wp_crm_display_company($current, $user_id, $user_object, $scope) { if ($scope == 'main_view') { return (WP_CRM_F::get_first_value($user_object['title']) ? WP_CRM_F::get_first_value($user_object['title']) . ' at ' : '') . '<a href="">' . WP_CRM_F::get_first_value($user_object['company']) . '</a>'; } return $current; }
/** * Tries to determine what the main display value of the user should be * Cycles through in attribute order to find first with value * * @since 0.1 * */ static function get_primary_display_value($user_object) { global $wp_crm; if (!empty($user_object) && is_numeric($user_object)) { $user_object = wp_crm_get_user($user_object); } if ($primary_user_attribute = $wp_crm['configuration']['primary_user_attribute']) { $primary_user_attribute = WP_CRM_F::get_first_value($user_object[$primary_user_attribute]); if (!empty($primary_user_attribute)) { $return = $primary_user_attribute; } } //** If unable to get value from primary user attribute, grab the first from attribute list */ if (!$return && !empty($wp_crm['data_structure']) && is_array($wp_crm['data_structure']['attributes'])) { $attribute_keys = array_keys($wp_crm['data_structure']['attributes']); foreach ($attribute_keys as $key) { if ($return = WP_CRM_F::get_first_value($user_object[$key])) { break; } } } //** Default to user_login */ if (!$return || is_array($return)) { $return = WP_CRM_F::get_first_value($user_object['user_login']); } //** Return values */ if ($return) { return $return; } return false; }