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')."
'.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(); ?>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"; $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' );