/** * Echos out contact form * * * @todo add provision to not display fields that no longer exist * @version 1.0 * Copyright 2011 Andy Potanin, Usability Dynamics, Inc. <*****@*****.**> */ function draw_form($form_settings) { global $wp_crm, $post; extract($form_settings); $form = $wp_crm['wp_crm_contact_system_data'][$form_slug]; if (empty($form['fields'])) { return false; } WP_CRM_F::force_script_inclusion('jquery-datepicker'); WP_CRM_F::force_script_inclusion('wp_crm_profile_editor'); $wp_crm_nonce = md5(NONCE_KEY . $form_slug); $wpc_form_id = 'wpc_' . $wp_crm_nonce . '_form'; //** Load user object if passed */ if ($use_current_user == 'true') { $current_user = wp_get_current_user(); if (0 == $current_user->ID) { $user_data = false; } else { $user_data = wp_crm_get_user($current_user->ID); } } if ($require_login_for_existing_users) { //** Get array of fields that must be checked to verify if user already exists */ $check_fields = apply_filters('wp_crm_distinct_user_fields', array('user_email')); } ?> <form id="<?php echo $wpc_form_id; ?> " class="wp_crm_contact_form wp_crm_contact_form_<?php echo $form_slug; ?> "> <ul class="wp_crm_contact_form"> <li class="wp_crm_<?php echo $wp_crm_nonce; ?> _first"> <?php /* Span Prevention */ ?> <input type="hidden" name="action" value="process_crm_message" /> <input type="text" name="wp_crm_nonce" value="<?php echo $wp_crm_nonce; ?> " /> <input type="text" name="email" /> <input type="text" name="name" /> <input type="text" name="url" /> <input type="text" name="comment" /> <input type="hidden" name="wp_crm[success_message]" value="<?php echo esc_attr($success_message); ?> " /> <?php if ($user_data) { ?> <input type="hidden" name="wp_crm[user_id]" value="<?php echo $current_user->ID; ?> " /> <?php } ?> <?php /* Span Prevention */ ?> </li> <?php $tabindex = 1; foreach ($form['fields'] as $field) { $this_attribute = $wp_crm['data_structure']['attributes'][$field]; $this_attribute['autocomplete'] = 'false'; if ($user_data && $user_data[$field]) { $values = $user_data[$field]; } else { $values = false; } ?> <li class="wp_crm_form_element <?php echo $this_attribute['required'] == 'true' ? 'wp_crm_required_field' : ''; ?> wp_crm_<?php echo $field; ?> _container"> <label class="wp_crm_input_label"><?php echo $this_attribute['title']; ?> </label> <div class="wp_crm_input_wrapper"> <?php if ($display_notes && $this_attribute['input_type'] != 'text') { ?> <span class="wp_crm_attribute_note"><?php echo nl2br($this_attribute['description']); ?> </span><?php } ?> <?php echo WP_CRM_F::user_input_field($field, $values, $this_attribute, $user_data, "tabindex={$tabindex}"); ?> <?php if ($display_notes && $this_attribute['input_type'] == 'text') { ?> <span class="wp_crm_attribute_note"><?php echo nl2br($this_attribute['description']); ?> </span><?php } ?> <span class="wp_crm_error_messages"></span> </div> </li> <?php $tabindex++; } ?> <?php if ($form['message_field'] == 'on') { ?> <li class="wp_crm_form_element wp_crm_message_field "> <div class="wp_crm_input_wrapper"> <?php echo WP_CRM_F::user_input_field('message_field', false, array('input_type' => 'textarea'), $user_data, "tabindex={$tabindex}"); ?> </div> </li> <?php } ?> <li class="wp_crm_form_response"><div class="wp_crm_response_text" style="display:none;"></li> <li class="wp_crm_submit_row"> <div class="wp_crm_input_wrapper"> <input class="<?php echo md5($wp_crm_nonce . '_submit'); ?> " type="submit" value="<?php echo $submit_text; ?> " /> </div> <input type="hidden" name="form_slug" value="<?php echo md5($form_slug); ?> " /> <input type="hidden" name="associated_object" value="<?php echo $post->ID; ?> " /> </li> </ul> </form> <style type="text/css">.wp_crm_<?php echo $wp_crm_nonce; ?> _first {display:none;}</style> <script type="text/javascript"> jQuery(document).ready(function() { if(typeof wp_crm_developer_log != 'function') { function wp_crm_developer_log() {} } if(typeof _gaq != 'object') { var _gaq = false; } if(_gaq) { _gaq.push(['_trackEvent', "Contact Form", "Viewed", "<?php echo esc_attr($form['title']); ?> "]); } var this_form = jQuery("#<?php echo $wpc_form_id; ?> "); var submit_button = jQuery("input[type=submit]", this_form); var form_response_field = jQuery(".wp_crm_form_response div", this_form); var this_form_data = {}; var validation_error = false; jQuery(this_form).change(function(event) { if(this_form_data.start_form == undefined) { this_form_data.start_form = event.timeStamp; } if(_gaq && this_form_data.interaction_logged !== undefined) { _gaq.push(['_trackEvent', "Contact Form", "Interacted With", "<?php echo esc_attr($form['title']); ?> "]); this_form_data.interaction_logged = true; } }); jQuery(this_form).submit(function(event) { event.preventDefault(); submit_this_form(); }); jQuery(submit_button).click(function(event) { event.preventDefault(); submit_this_form(); }); <?php if ($require_login_for_existing_users) { foreach ($check_fields as $attribute_slug) { ?> jQuery(".wp_crm_<?php echo $attribute_slug; ?> _field", this_form).change(function() { validation_error = true; submit_this_form('system_validate', this); }); <?php } ?> <?php } ?> function submit_this_form(crm_action, trigger_object) { var validation_error = false; var form = this_form; wp_crm_developer_log('submit_this_form() initiated.'); if(typeof wp_crm_save_user_form == 'function') { /* passed form object into wp_crm_save_user_form() is not usable */ if(!wp_crm_save_user_form(jQuery(form))) { return false; } } else { wp_crm_developer_log('wp_crm_save_user_form() function does not exist.'); } jQuery("*", form).removeClass(form).removeClass("wp_crm_input_error"); jQuery("span.wp_crm_error_messages", form).removeClass(form).text(""); <?php if (isset($form_settings['js_validation_function'])) { ?> /** Custom validation */ if(!validation_error){ t = <?php echo $form_settings['js_validation_function']; ?> (form); if(!t) { validation_error = true; } } <?php } ?> if(validation_error) { jQuery(submit_button).removeAttr("disabled"); return false; } params = jQuery(this_form).serialize(); if(crm_action != 'system_validate') { jQuery(submit_button).attr("disabled", "disabled"); jQuery(form_response_field).show(); jQuery(form_response_field).removeClass('success'); jQuery(form_response_field).removeClass('failure'); jQuery(form_response_field).text("<?php _e('Processing...', 'wp_crm'); ?> "); } if(crm_action) { params = params + "&crm_action=" + crm_action; } jQuery(submit_button).attr("disabled", "disabled"); jQuery.ajax({ url: "<?php echo admin_url('admin-ajax.php'); ?> ", dataType: "json", data: params, cache: false, success: function(result) { /* Enable submit button in case it was disabled during validation */ jQuery(submit_button).removeAttr("disabled"); /* Get conflicting fields */ if(result.bad_fields !== undefined) { jQuery.each(result.bad_fields, function(field) { /* If check started by a specific object, we only update it */ if(jQuery(trigger_object).hasClass("regular-text") && jQuery(trigger_object).attr("wp_crm_slug") != field) { return; } jQuery("div.wp_crm_"+field+"_div input.regular-text:first", form).addClass("wp_crm_input_error"); jQuery("div.wp_crm_"+field+"_div span.wp_crm_error_messages", form).text(result.bad_fields[field]); }); } /* If doing only a validation, stop here */ if(crm_action == 'system_validate') { if(result.validation_passed == true) { validation_error = true; } else { validation_error = false; } return; } if(result.success == "true") { if(_gaq) { _gaq.push(['_trackEvent', "Contact Form: <?php echo esc_attr($form['title']); ?> ", "Submitted", "Total Time", (+new Date) - this_form_data.start_time]); } jQuery(form_response_field).addClass("success"); jQuery(submit_button).removeAttr("disabled"); } else { if(_gaq) { _gaq.push(['_trackEvent', "Contact Form: <?php echo esc_attr($form['title']); ?> ", "Submission Failure", result.message]); this_form_data.interaction_logged = true; } jQuery(form_response_field).addClass("failure"); jQuery(submit_button).removeAttr("disabled"); } <?php if ($js_callback_function) { ?> if(typeof <?php echo $js_callback_function; ?> == 'function') { callback_data = {}; callback_data.form = jQuery("#<?php echo $wpc_form_id; ?> "); callback_data.result = result; <?php echo $js_callback_function; ?> (callback_data); } <?php } ?> jQuery(form_response_field).text(result.message); }, error: function(result) { jQuery(form_response_field).show(); jQuery(form_response_field).addClass("failure"); jQuery(form_response_field).text("<?php _e('A server error occurred while trying to process the form.', 'wp_crm'); ?> "); jQuery(form_response_field).addClass("failure"); jQuery(submit_button).removeAttr("disabled"); if(_gaq) { _gaq.push(['_trackEvent', "Contact Form: <?php echo esc_attr($form['title']); ?> ", "Submission Failure", "Server error."]); this_form_data.interaction_logged = true; } } }); } }); </script> <?php }
/** * 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; }
/** * Render CRM fields on BP edit profile page * * @global object $profile_template * @global object $current_user * @author korotkov@UD */ static function bp_after_profile_field_content() { //** If we are going to edit profile */ if (bp_is_current_action('edit')) { global $profile_template, $current_user; //** Loop through BP fields in current group */ foreach ($profile_template->group->fields as $field_key => $field_value) { //** If field is not CRM field - skip */ if (empty($field_value->source) || $field_value->source != 'wp_crm') { continue; } //** Get current data from CRM */ $user_data = wp_crm_get_user($current_user->ID); //** Attribute title */ ob_start(); ?> <label class="control-label"><?php echo $field_value->attribute['title']; ?> </label> <?php $label = ob_get_contents(); ob_clean(); //** Attribute description */ ob_start(); ?> <p class="help-block"><?php echo $field_value->attribute['description']; ?> </p> <?php $description = ob_get_contents(); ob_clean(); //** Draw title, input and description */ ?> <div class="control-group"> <?php echo apply_filters('bp_wp_crm_profile_field_label', $label); ?> <div class="controls"> <?php echo WP_CRM_F::user_input_field($field_value->slug, $user_data[$field_value->slug], $field_value->attribute, $user_data); echo apply_filters('bp_wp_crm_profile_field_description', $description); ?> </div> </div> <?php //** Remove current custom field from BP fields list so they won't be processed by BP */ unset($profile_template->group->fields[$field_key]); } } }
/** * Generate HTML for a single row on the users.php admin panel. * */ function single_row($user_id) { global $wp_roles, $wp_crm; if (is_object($user_id)) { $user_id = $user_id->ID; } $user_object = wp_crm_get_user($user_id); $r = "<tr id='user-{$user_id}'{$style}>"; list($columns, $hidden) = $this->get_column_info(); foreach ($columns as $column_name => $column_display_name) { $class = "class=\"{$column_name} column-{$column_name}\""; $style = ''; if (in_array($column_name, $hidden)) { $style = ' style="display:none;"'; } $attributes = "{$class}{$style}"; $r .= "<td {$attributes}>"; $single_cell = $this->single_cell($column_name, $user_object, $user_id); //** Need to insert some sort of space in there to avoid DataTable error that occures when "null" is returned */ $ajax_cells[] = ' ' . $single_cell; $r .= $single_cell; $r .= "</td>"; } $r .= '</tr>'; if ($this->_args['ajax']) { return $ajax_cells; } return $r; }
/** * Get a value of a user. * * Ideally an attribute key should be passed, in which case the first value will be returned by default. * If a full meta key is passed, the value will be returned. * If a value of an option is passed, a boolean will be returned depending on if the option is enabled for the user. * * @since 0.1 * */ function wp_crm_get_value($meta_key, $user_id = false, $args = '') { global $current_user, $wp_crm; $args = wp_parse_args($args, array('return' => 'value', 'concat_char' => ', ', 'meta_key' => $meta_key, 'option_key' => '')); if (!$user_id) { $user_id = $current_user->ID; } $quantifiable_attributes = array_keys(WP_CRM_F::get_quantifiable_attributes()); //** Check if meta key exists as key and as label */ $attributes = $wp_crm['data_structure']['attributes']; $full_meta_keys = $wp_crm['data_structure']['full_meta_keys']; $meta_keys = $wp_crm['data_structure']['meta_keys']; //* If passed key is an attribute key (as intended) */ if (is_array($attributes) && in_array($meta_key, array_keys($attributes))) { $args['attribute_key'] = $meta_key; } //* If passed meta_key is actually a label name */ if (is_array($meta_keys) && in_array($meta_key, $meta_keys)) { $meta_value_flip = array_flip($meta_keys); $args['attribute_key'] = $meta_value_flip[$meta_key]; $meta_key = $args['attribute_key']; } //* If a full meta key is passed (for pre-defined values) */ if (is_array($full_meta_keys) && in_array($meta_key, array_keys($full_meta_keys))) { $args['full_meta_key'] = $meta_key; $args['attribute_key'] = $full_meta_keys[$meta_key]; } //** If all else fails, use the passed $meta_key (it may be a main user table key) */ if (empty($args['attribute_key'])) { $args['attribute_key'] = $meta_key; } //** Get the full attribute data once we have the attribute_key */ $attribute = WP_CRM_F::get_attribute($args['attribute_key']); //* Make sure we have a user object */ if (!empty($user_id) && is_numeric($user_id)) { $user_object = (array) wp_crm_get_user($user_id); } //** If we have the full meta key then we can get value easily from get_user_meta() */ if (!empty($args['full_meta_key']) && $args['full_meta_key'] != $args['attribute_key']) { $args['value'] = get_user_meta($user_id, $args['full_meta_key'], true); } else { //** Attribute has options, we return the label of the option */ if (!empty($attribute['has_options'])) { if ($attribute['input_type'] == 'text' || $attribute['input_type'] == 'textarea' || $attribute['input_type'] == 'date') { //** Try to get value by option key. */ $option_key = !empty($args['option_key']) ? $args['option_key'] : (is_array($attribute['option_keys']) ? key($attribute['option_keys']) : false); if (!empty($option_key)) { $args['value'] = get_user_meta($user_id, $args['attribute_key'] . '_option_' . $option_key, true); if (!empty($args['value'])) { $args['label'] = $meta_keys[$args['attribute_key'] . '_option_' . $option_key]; $args['return_option_label'] = true; $args['value'] .= ', ' . $args['label']; } } if (empty($args['value'])) { $args['value'] = WP_CRM_F::get_first_value(!empty($user_object[$args['attribute_key']]) ? $user_object[$args['attribute_key']] : array()); } } else { $options = WP_CRM_F::list_options($user_object, $args['attribute_key']); if (is_array($options)) { $args['value'] = implode($args['concat_char'], $options); } } } else { $args['value'] = WP_CRM_F::get_first_value(!empty($user_object[$args['attribute_key']]) ? $user_object[$args['attribute_key']] : array()); } } //** Check if this should be a boolean response */ if ((empty($args['return_option_label']) || !$args['return_option_label']) && in_array($args['attribute_key'], $quantifiable_attributes)) { if (!empty($args['value']) && $args['value'] == 'on') { $args['value'] = true; } } switch ($args['return']) { case 'value': $result = !empty($args['value']) ? $args['value'] : ''; break; case 'detail': $result = $args; break; default: $result = $args[$args['return']]; break; } return $result; }
/** * Debug user by ID */ static function user_object() { echo "CRM Object Report: \n" . print_r(wp_crm_get_user($_REQUEST["user_id"]), true) . "\nRaw Meta Report: \n" . print_r(WP_CRM_F::show_user_meta_report($_REQUEST["user_id"]), true); die; }