optionname); if (!isset($options['org_id']) || empty($options['org_id'])) echo "

".__('Your WordPress-to-Lead settings are not complete.','salesforce')." ".__('You must enter your Salesforce.com Organisation ID for it to work.','salesforce')." ".__('Settings','salesforce')."

"; //echo 'ERROR= '.get_option('plugin_error'); } function config_page() { $options = get_option($this->optionname); if ( isset($_POST['submit']) ) { //die('
'.print_r($_POST,true)); //DEBUG

				if( isset( $_POST['mode'] ) && $_POST['mode'] == 'editform' ){

					$form_id = (int) $_POST['form_id'];
					
					if(!isset($options['forms'][$form_id]))
						$options['forms'][$form_id] = salesforce_default_form();

					//Begin Save Form Data
					$newinputs = array();
					foreach ($options['forms'][$form_id]['inputs'] as $id => $input) {
						foreach (array('show','required') as $option_name) {
							if (isset($_POST['inputs'][$id.'_'.$option_name])) {
								$newinputs[$id][$option_name] = true;
								unset($_POST['inputs'][$id.'_'.$option_name]);
							} else {
								$newinputs[$id][$option_name] = false;
							}
						}
						foreach (array('type','label','value','pos','opts') as $option_name) {
							if (isset($_POST['inputs'][$id.'_'.$option_name])) {
								$newinputs[$id][$option_name] = $_POST['inputs'][$id.'_'.$option_name];
								unset($_POST['inputs'][$id.'_'.$option_name]);
							}
						}	
					}
					
					//add any new fields
					
					if( isset($_POST['add_inputs']) ){
					
						foreach ($_POST['add_inputs'] as $key=>$input) {
						
							$id = $input['field_name'];
						
							if( !empty($id) ){
								foreach (array('show','required') as $option_name) {
									if (isset($_POST['add_inputs'][$key][$option_name])) {
										$newinputs[$id][$option_name] = true;
										unset($_POST['add_inputs'][$key][$option_name]);
									} else {
										$newinputs[$id][$option_name] = false;
									}
								}
								
								foreach (array('type','label','value','pos','opts') as $option_name) {
									if (isset($_POST['add_inputs'][$key][$option_name])) {
										$newinputs[$id][$option_name] = $_POST['add_inputs'][$key][$option_name];
										unset($_POST['add_inputs'][$key][$option_name]);
									}
								}
							}
						}
					
					}
					
					w2l_sksort($newinputs,'pos',true);
					$options['forms'][$form_id]['inputs'] = $newinputs; //TODO
					
					foreach (array('form_name','source','returl') as $option_name) {
						if (isset($_POST[$option_name])) {
							$options['forms'][$form_id][$option_name] = $_POST[$option_name];
						}
					}
					
					//End Save Form Data
				
				}elseif( isset( $_POST['mode'] ) && $_POST['mode'] == 'delete'){
				
					if( isset( $_POST['form_id'] ) && $_POST['form_id'] != 1 )
						unset( $options['forms'][$_POST['form_id']] );
				
				}elseif( isset( $_POST['mode'] ) && $_POST['mode'] == 'clone'){
				
					if( isset( $_POST['form_id'] ) && $_POST['form_id'] != 1 ) {
						$new_id = max(array_keys($options['forms'])) + 1;
						$options['forms'][$new_id] = $options['forms'][$_POST['form_id']];
					}
				
				}else{
				
					//Save general settings
									
					$options  = get_option($this->optionname);
					if (!current_user_can('manage_options')) die(__('You cannot edit the WordPress-to-Lead options.', 'salesforce'));
					check_admin_referer('salesforce-udpatesettings');
					
					foreach (array('usecss','showccuser','ccadmin','captcha','wpcf7css','hide_salesforce_link') as $option_name) {
						if (isset($_POST[$option_name])) {
							$options[$option_name] = true;
						} else {
							$options[$option_name] = false;
						}
					}
					
					
			        foreach (array('successmsg','errormsg','sferrormsg','org_id','submitbutton','subject','ccusermsg','requiredfieldstext') as $option_name) {
						if (isset($_POST[$option_name])) {
							$options[$option_name] = $_POST[$option_name];
						}
					}
				}
				
				//save changes to DB
				update_option($this->optionname, $options);
			
			}
			
			//$options = get_option($this->optionname);

			if (empty($options))
				$options = salesforce_default_settings();
			
			?>
			

longname; ?>

' . __('Configuration Saved','salesforce') . '

'; } ?>
postbox('options','Options','
'.print_r($options,true).'
'); //DEBUG $content = $this->textinput('successmsg',__('Success message after sending message', 'salesforce') ); $content .= $this->textinput('errormsg',__('Error message when not all form fields are filled', 'salesforce') ); $content .= $this->textinput('sferrormsg',__('Error message when Salesforce.com connection fails', 'salesforce') ); $this->postbox('basicsettings',__('Basic Settings', 'salesforce'),$content); $content = $this->textinput('org_id',__('Your Salesforce.com organisation ID','salesforce')); $content .= ''.__('To find your Organisation ID, in your Salesforce.com account, go to Setup » Company Profile » Company Information','salesforce').'


'; $this->postbox('sfsettings',__('Salesforce.com Settings', 'salesforce'),$content); $content = $this->checkbox('showccuser',__('Allow user to request a copy of their submission', 'salesforce') ); $content .= '
'; $content .= $this->textinput('ccusermsg',__('Request a copy text', 'salesforce') ); $content .= $this->textinput('subject',__('Email subject', 'salesforce') ); $content .= ''.__('Use %BLOG_NAME% to auto-insert the blog title into the subject','salesforce').'


'; $content .= $this->checkbox('ccadmin',__('Send blog admin an email notification', 'salesforce') ); $this->postbox('sfsettings',__('Email Settings', 'salesforce'),$content); $content = $this->textinput('submitbutton',__('Submit button text', 'salesforce') ); $content .= $this->textinput('requiredfieldstext',__('Required fields text', 'salesforce') ); $content .= $this->checkbox('usecss',__('Use Form CSS?', 'salesforce') ); $content .= $this->checkbox('wpcf7css',__('Use WPCF7 CSS integration?', 'salesforce') ); $content .= $this->checkbox('hide_salesforce_link',__('Hide salesforce link on form?', 'salesforce') ); $content .= '
'.__('Read how to copy the CSS to your own CSS file').'

'; $content .= $this->checkbox('captcha',__('Use CAPTCHA?', 'salesforce') ); $content .= '
'.__('Learn more about CAPTCHAs at Wikipedia').''; $this->postbox('formsettings',__('Form Settings', 'salesforce'),$content); ?>
" />
'; $content .= 'IDName'; foreach($options['forms'] as $key=>$form){ $content .= ''.$key.''.$form['form_name'].''; } $content .= ''; $content .= '

'.__('Add a new form','salesforce').' »

'; $this->postbox('sfforms',__('Forms', 'salesforce'),$content); ?>
'.salesforce_back_link($this->plugin_options_url()).'

'; ?>

.


form.w2llead {
  text-align: left;
  clear: both;
}
.w2llabel, .w2linput {
  display: block;
  width: 120px;
  float: left;
}
.w2llabel.error {
  color: #f00;
}
.w2llabel {
  clear: left;
  margin: 4px 0;
}
.w2linput.text {
  width: 200px;
  height: 18px;
  margin: 4px 0;
}
.w2linput.textarea {
  clear: both;
  width: 320px;
  height: 75px;
  margin: 10px 0;
}
.w2linput.submit {
  float: none;
  margin: 10px 0 0 0;
  clear: both;
  width: 150px;
}
.w2linput.checkbox{
  vertical-align: middle;
}
.w2llabel.checkbox{
  clear:both;
}
.w2linput.select{
  clear:both;
}
.w2limg{ 
display: block; clear: both; 
}
#salesforce {
  margin: 3px 0 0 0;
  color: #aaa;
}
#salesforce a {
  color: #999;
}

.sidebar form.w2llead {
  clear: none;
  text-align: left;
}
.sidebar .w2linput, 
.sidebar .w2llabel {
  float: none;
  display: inline;
}
.sidebar .w2llabel.error {
  color: #f00;
}
.sidebar .w2llabel {
  margin: 4px 0;
}
.sidebar .w2linput.text {
  width: 160px;
  height: 18px;
  margin: 4px 0;
}
.sidebar .w2linput.textarea {
  width: 160px;
  height: 50px;
  margin: 10px 0;
}
.sidebar .w2linput.submit {
  margin: 10px 0 0 0;
}
#salesforce {
  margin: 3px 0 0 0;
  color: #aaa;
}
#salesforce a {
  color: #999;
}

' . __('Deleted Form #','salesforce') . $form_id . '

'; } else if(isset($_POST['mode']) && $_POST['mode'] == 'clone' && $form_id != 1 ) { echo '

' . __('Cloned Form #','salesforce') . $form_id . '

'; }else{ if(!isset($form_id) && isset($_GET['id'])) $form_id = (int) $_GET['id']; if( isset($_POST['form_id']) ) $form_id = (int) $_POST['form_id']; if( !isset($form_id) || $form_id == 0 ){ //generate a new default form end( $options['forms'] ); $form_id = key( $options['forms'] ) + 1; $options['forms'][$form_id] = salesforce_default_form(); } //check for deleted forms if( $form_id && !isset($options['forms'][$form_id]) ){ echo '

' . __('This form could not be found.','salesforce') . '

'; }else{ if(isset($_POST['submit']) && $_POST['submit']) echo '

' . __('Form settings updated.','salesforce') . '

'; ?>
postbox('options','Options','
'.print_r($options,true).'
'); //DEBUG $content = '

'; $content .= ''; //$content .= '
'.__('').''; $content .= '

'; $this->postbox('sfformtitle',__('Form Name', 'salesforce'),$content); $content = ''; $content .= '' .'' .'' .'' .'' .'' .'' .'' .'' .''; $i = 1; foreach ($options['forms'][$form_id]['inputs'] as $field => $input) { if (empty($input['pos'])) $input['pos'] = $i; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $i++; } $content .= '
'.__('Field','salesforce').''.__('Enable','salesforce').''.__('Required','salesforce').''.__('Type','salesforce').''.__('Label','salesforce').''.__('Value','salesforce').''.__('Options','salesforce').''.__('Position','salesforce').'
'.$field.'
'; ?> Add a field

'; $this->postbox('sffields',__('Form Fields', 'salesforce'),$content); $content = '

'; $content .= '
'; $content .= ''; $content .= '
'.__('Lead Source to display in Salesforce.com, use %URL% to include the URL of the page containing the form').''; $content .= '

'; $content .= '

'; $content .= '
'; $content .= ''; $content .= '
'.__('e.g.http://yoursite.com/thanks/').''; $content .= '

'; $content .= ''; $this->postbox('sfformmeta',__('Form Settings', 'salesforce'),$content); ?>
" />
'.salesforce_back_link($this->plugin_options_url()).'

'; ?>
postbox('usesalesforce',__('How to Use This Plugin','salesforce'),__('

To use this form, copy the following shortcode into a post or page:

[salesforce form="X"]

Replace X with the form number for the form you want to show.

Make sure you have entered all the correct settings on the left, including your Organisation ID.

','salesforce')); $this->plugin_like(false); $this->plugin_support(); // $this->news(); ?>



array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true), 'first_name' => array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true), 'last_name' => array('type' => 'text', 'label' => 'Last name', 'show' => true, 'required' => true), 'email' => array('type' => 'text', 'label' => 'Email', 'show' => true, 'required' => true), 'phone' => array('type' => 'text', 'label' => 'Phone', 'show' => true, 'required' => false), 'description' => array('type' => 'textarea', 'label' => 'Message', 'show' => true, 'required' => true), 'title' => array('type' => 'text', 'label' => 'Title', 'show' => false, 'required' => false), 'company' => array('type' => 'text', 'label' => 'Company', 'show' => false, 'required' => false), 'street' => array('type' => 'text', 'label' => 'Street', 'show' => false, 'required' => false), 'city' => array('type' => 'text', 'label' => 'City', 'show' => false, 'required' => false), 'state' => array('type' => 'text', 'label' => 'State', 'show' => false, 'required' => false), 'zip' => array('type' => 'text', 'label' => 'ZIP', 'show' => false, 'required' => false), 'country' => array('type' => 'text', 'label' => 'Country', 'show' => false, 'required' => false), 'Campaign_ID' => array('type' => 'hidden', 'label' => 'Campaign ID', 'show' => false, 'required' => false), ); return $dform; } function salesforce_back_link($url){ return '« '.__('Back to configuration page','salesforce').''; } /** * Sort input array by $subkey * Taken from: http://php.net/manual/en/function.ksort.php */ function w2l_sksort(&$array, $subkey="id", $sort_ascending=false) { if( !is_array( $array ) ) return $array; $temp_array = array(); if (count($array)) $temp_array[key($array)] = array_shift($array); foreach($array as $key => $val){ $offset = 0; $found = false; foreach($temp_array as $tmp_key => $tmp_val) { if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) { $temp_array = array_merge( (array)array_slice($temp_array,0,$offset), array($key => $val), array_slice($temp_array,$offset) ); $found = true; } $offset++; } if(!$found) $temp_array = array_merge($temp_array, array($key => $val)); } if ($sort_ascending) $array = array_reverse($temp_array); else $array = $temp_array; } function salesforce_captcha(){ include("lib/captcha/captcha.php"); die(); } function salesforce_form($options, $is_sidebar = false, $content = '', $form_id = 1) { if( !isset($options['forms'][$form_id]) ) return; if (!empty($content)) $content = wpautop(''.$content.''); if ($options['usecss'] && !$is_sidebar) { $content .= ''; } elseif ($is_sidebar && $options['usecss']) { $content .= ''; } $sidebar = ''; if ( $is_sidebar ) $sidebar = ' sidebar'; if ( $options['wpcf7css'] ) { $content .= '
'; } $content .= "\n".'
'."\n"; foreach ($options['forms'][$form_id]['inputs'] as $id => $input) { if (!$input['show']) continue; $val = ''; if (isset($_POST[$id])){ $val = esc_attr(strip_tags(stripslashes($_POST[$id]))); }else{ if( isset($input['value']) ) $val = esc_attr(strip_tags(stripslashes($input['value']))); } $error = ' '; if (isset($input['error']) && $input['error']) $error = ' error '; if($input['type'] != 'hidden') { if ($options['wpcf7css']) { $content .= '

'; } if ($input['type'] == 'checkbox') { $content .= "\t\n\t".''."\n\n"; } $content .= "\t".''."\n"; if ($options['wpcf7css']) { $content .= ''; } } if ($input['type'] == 'text') { $content .= "\t".'
'."\n\n"; } else if ($input['type'] == 'textarea') { $content .= "\t".( !$options['wpcf7css'] ? '
' : '' )."\n\t".'
'."\n\n"; } else if ($input['type'] == 'hidden') { $content .= "\t\n\t".''."\n\n"; } else if ($input['type'] == 'select') { $content .= "\t\n\t".'
'."\n\n"; } if($input['type'] != 'hidden') { if ($options['wpcf7css']) { $content .= '

'; } } } //captcha if($options['captcha']){ include("lib/captcha/captcha.php"); $captcha = captcha(); //$content .= 'CODE='.$captcha['code'].'
'; $sf_hash = sha1($captcha['code'].NONCE_SALT); set_transient( $sf_hash, $captcha['code'], 60*15 ); $content .= '
CAPTCHA image
'; $content .= '
'; $content .= ''; } //send me a copy if( $options['showccuser'] ){ $label = $options['ccusermsg']; if( empty($label) ) $label = __('Send me a copy','salesforce'); $content .= "\t\n\t".'

\n"; } //spam honeypot $content .= "\t".''."\n"; //form id $content .= "\t".''."\n"; $submit = stripslashes($options['submitbutton']); if (empty($submit)) $submit = "Submit"; $content .= "\t"; if ($options['wpcf7css']) { $content .= '

'; } else { $content .= '

'; } $content .= ''."\n"; if ($options['wpcf7css']) { $content .= '

'; } else { $content .= '
'; } $content .= '
'."\n"; $reqtext = stripslashes($options['requiredfieldstext']); if (!empty($reqtext)) $content .= '

*'.esc_html($reqtext).'

'; if (!$options['hide_salesforce_link']) { $content .= '
'.__('Powered by','salesforce').' Salesforce CRM
'; } if ( $options['wpcf7css'] ) { $content .= '
'; } $content = apply_filters('salesforce_w2l_form_html', $content); return $content; } function submit_salesforce_form($post, $options) { global $wp_version; if (!isset($options['org_id']) || empty($options['org_id'])) return false; //spam honeypot if( !empty($_POST['message']) ) return false; //print_r($_POST); //DEBUG $form_id = intval( $_POST['form_id'] ); $post['oid'] = $options['org_id']; $post['lead_source'] = str_replace('%URL%','['.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].']',$options['forms'][$form_id]['source']); $post['debug'] = 0; // Set SSL verify to false because of server issues. $args = array( 'body' => $post, 'headers' => array( 'user-agent' => 'WordPress-to-Lead for Salesforce plugin - WordPress/'.$wp_version.'; '.get_bloginfo('url'), ), 'sslverify' => false, ); $result = wp_remote_post('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', $args); if( is_wp_error($result) ) return false; if ($result['response']['code'] == 200){ if( isset( $_POST['w2lcc'] ) && $_POST['w2lcc'] == 1 ) salesforce_cc_user($post, $options, $form_id); if( isset( $options['ccadmin'] ) && $options['ccadmin'] ) salesforce_cc_admin($post, $options, $form_id); return true; }else{ return false; } } function salesforce_cc_user($post, $options, $form_id = 1){ $from_name = apply_filters('salesforce_w2l_cc_user_from_name', get_bloginfo('name')); $from_email = apply_filters('salesforce_w2l_cc_user_from_email', get_option('admin_email')); $headers = 'From: '.$from_name.' <' . $from_email . ">\r\n"; $subject = str_replace('%BLOG_NAME%', get_bloginfo('name'), $options['subject']); if( empty($subject) ) $subject = __('Thank you for contacting','salesforce').' '.get_bloginfo('name'); //remove hidden fields foreach ($options['forms'][$form_id]['inputs'] as $id => $input) { if( $input['type'] == 'hidden' ) unset( $post[$id] ); } unset($post['oid']); unset($post['lead_source']); unset($post['debug']); $message = ''; //$message .= print_r( $post , true); //$message .= print_r( $options['forms'][$form_id]['inputs'] , true); //format message foreach($post as $name=>$value){ if( !empty($value) ) $message .= $options['forms'][$form_id]['inputs'][$name]['label'].': '.$value."\r\n"; } wp_mail( $_POST['email'], $subject, $message, $headers ); } function salesforce_cc_admin($post, $options, $form_id = 1){ $from_name = apply_filters('salesforce_w2l_cc_admin_from_name', get_bloginfo('name')); $from_email = apply_filters('salesforce_w2l_cc_admin_from_email', get_option('admin_email')); $headers = 'From: '.$from_name.' <' . $from_email . ">\r\n"; $subject = __('Salesforce WP to Lead Submission','salesforce'); $message = ''; unset($post['oid']); unset($post['lead_source']); unset($post['debug']); //format message foreach($post as $name=>$value){ if( !empty($value) ) $message .= $options['forms'][$form_id]['inputs'][$name]['label'].': '.$value."\r\n"; } $emails = array( get_option('admin_email') ); $emails = apply_filters( 'salesforce_w2l_cc_admin_email_list', $emails ); //print_r( $emails ); foreach( $emails as $email ){ wp_mail( $email, $subject, $message, $headers ); } } function salesforce_form_shortcode($atts) { extract( shortcode_atts( array( 'form' => '1', 'sidebar' => false, ), $atts ) ); $emailerror = ''; $captchaerror = ''; $content = ''; $form = (int) $form; $sidebar = (bool) $sidebar; $options = get_option("salesforce2"); if (!is_array($options)) $options = salesforce_default_settings(); //don't submit unless we're in the right shortcode if( isset( $_POST['form_id'] ) ){ $form_id = intval( $_POST['form_id'] ); if( $form_id != $form ){ $content = salesforce_form($options, $sidebar, null, $form); return $content; } } //this is the right form, continue if (isset($_POST['w2lsubmit'])) { $error = false; $post = array(); foreach ($options['forms'][$form]['inputs'] as $id => $input) { if ($input['required'] && empty($_POST[$id])) { $options['forms'][$form]['inputs'][$id]['error'] = true; $error = true; } else if ($id == 'email' && $input['required'] && !is_email($_POST[$id]) ) { $error = true; $emailerror = true; } else { if( isset($_POST[$id]) ) $post[$id] = trim(strip_tags(stripslashes($_POST[$id]))); } } //check captcha if enabled if( $options['captcha'] ){ if( $_POST['captcha_hash'] != sha1( $_POST['captcha_text'].NONCE_SALT )){ $error = true; $captchaerror = true; } } if (!$error) { $result = submit_salesforce_form($post, $options, $form); //echo 'RESULT='.$result; //if($result) echo 'true'; //if(!$result) echo 'false'; if (!$result){ $content = ''.esc_html(stripslashes($options['sferrormsg'])).''; }else{ if( !empty($options['forms'][$form]['returl']) ){ //wp_redirect( $options['forms'][$form]['returl'] ); //exit; ?> '.esc_html(stripslashes($options['successmsg'])).''; } } else { $errormsg = esc_html( stripslashes($options['errormsg']) ) ; if ($emailerror) $errormsg .= '
'.__('The email address you entered is not a valid email address.','salesforce'); if ($captchaerror) $errormsg .= '
'.__('The text you entered did not match the image.','salesforce'); $content .= salesforce_form($options, $sidebar, $errormsg, $form); } } else { $content = salesforce_form($options, $sidebar, null, $form); } return $content; } add_shortcode('salesforce', 'salesforce_form_shortcode'); class Salesforce_WordPress_to_Lead_Widgets extends WP_Widget { function Salesforce_WordPress_to_Lead_Widgets() { $widget_ops = array( 'classname' => 'salesforce', 'description' => __('Displays a WordPress-to-Lead for Salesforce Form','salesforce') ); $control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'salesforce' ); $this->WP_Widget( 'salesforce', 'Salesforce', $widget_ops, $control_ops ); } function widget( $args, $instance ) { extract( $args ); echo $before_widget; $title = apply_filters('widget_title', $instance['title'] ); if ( $title ) { echo $before_title . $title . $after_title; } if ( !empty($instance['desc']) && empty($_POST) ) { echo '

' . $instance['desc'] . '

'; } $is_sidebar = true; echo do_shortcode('[salesforce form="'.$instance['form'].'" sidebar="true"]'); echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; foreach ( array('title', 'desc', 'form') as $val ) { $instance[$val] = strip_tags( $new_instance[$val] ); } return $instance; } function form( $instance ) { $defaults = array( 'title' => 'Contact Us', 'desc' => 'Contact us using the form below', 'form' => 1, ); $instance = wp_parse_args( (array) $instance, $defaults ); ?>

$val){ $newinputs[$key] = $val; } //sort merged inputs w2l_sksort($newinputs,'pos',true); //save merged and sorted inputs $options['forms'][1]['inputs'] = $newinputs; //source is now saved per form $options['forms'][1]['source'] = $oldoptions['source']; update_option('salesforce2', $options); //$options = get_option('salesforce'); } if( empty($oldoptions) ){ salesforce_default_settings(); } } /* //Save Activation Error to DB for review add_action('activated_plugin','save_error'); function save_error(){ update_option('plugin_error', ob_get_contents()); } */ register_activation_hook( __FILE__, 'salesforce_activate' );