« '.__('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; } add_action('wp_footer','salesforce_da_js'); // Add Daddy Analytics JS tracking to all pages function salesforce_da_js( ){ $options = get_option("salesforce2"); if( isset( $options['da_token'] ) && isset( $options['da_url'] ) && isset( $options['da_site'] ) ){ $da_token = $options['da_token']; $da_url = $options['da_url']; $da_site = $options['da_site']; echo "\n\n".' '."\n\n"; } } 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']) { wp_enqueue_style( 'sfwp2lcss', plugins_url('/assets/css/sfwp2l.css', __FILE__) ); } $custom_css = '/salesforce-wordpress-to-lead/custom.css'; if( file_exists( get_stylesheet_directory() . $custom_css ) ) wp_enqueue_style( 'sfwp2lcsscustom', get_stylesheet_directory_uri() . $custom_css ); $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']))); } if($input['type'] != 'hidden' && $input['type'] != 'current_date') { $content .= '
'; } $error = ' '; if (isset($input['error']) && $input['error']) { $error = ' error '; } if($input['type'] != 'hidden' && $input['type'] != 'current_date') { if ($options['wpcf7css']) { $content .= '

'; } if ($input['type'] == 'checkbox') { $content .= "\t\n\t".''."\n\n"; } if (!empty($input['label'])) { $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'] == 'current_date') { $content .= "\t\n\t".''."\n\n"; } else if ($input['type'] == 'html'){ $content .= stripslashes($input['opts'])."\n\n"; } else if ($input['type'] == 'select') { $content .= "\t\n\t".''."\n\n"; } if($input['type'] != 'hidden' && $input['type'] != 'current_date') { if ($options['wpcf7css']) { $content .= '

'; } $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"; //daddy analytics if( isset( $options['da_token'] ) && isset( $options['da_url'] ) ){ $da_token = $options['da_token']; $da_url = $options['da_url']; $content .= "\t".''."\n"; $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'])) { error_log( "Salesforce: No organisation ID set." ); return false; } //spam honeypot if( !empty($_POST['message']) ) { error_log( "Salesforce: No message set." ); return false; } //print_r($_POST); //DEBUG $form_id = absint( $_POST['form_id'] ); $post['oid'] = $options['org_id']; // web to lead $post['orgid'] = $options['org_id']; // web to case if (!empty($options['forms'][$form_id]['source'])) { $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, ); if( $options['forms'][$form_id]['type'] == 'case' ){ $url = 'https://www.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8'; }else{ $url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8'; } $url = apply_filters( 'salesforce_w2l_api_url', $url ); $result = wp_remote_post( $url, $args ); if( is_wp_error($result) ) { error_log( "Salesforce HTTP error: " . print_r( $result, true ) ); 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{ error_log( "Salesforce response error: " . print_r( $result, true ) ); 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']); if (!empty($options['forms'][$form_id]['source'])) { 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($name) && !empty($value) ) $label = $options['forms'][$form_id]['inputs'][$name]['label']; if( trim( $label ) != '' ) $message .= $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"; if (get_option('email_sender') != '') { $headers .= 'Sender: '.get_option('email_sender')."\r\n"; } $headers .= 'Reply-to: '.$from_name.' <' . $from_email . ">\r\n"; if( $options['forms'][$form_id]['type'] == 'case' ){ $subject = __('Salesforce Web to Case Submission','salesforce'); }else{ $subject = __('Salesforce Web to Lead Submission','salesforce'); } $message = ''; unset($post['oid']); if (!empty($options['forms'][$form_id]['source'])) { unset($post['lead_source']); } unset($post['debug']); //format message foreach($post as $name=>$value){ if( !empty($value) ){ $label = $options['forms'][$form_id]['inputs'][$name]['label']; if( trim( $label ) != '' ) $message .= $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) { $val = trim( $_POST[$id] ); if ($input['required'] && !$val ) { $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]))); } } //pass daddy analytics fields if( isset( $options['da_token'] ) && isset( $options['da_url'] ) ){ $da_token = $options['da_token']; $da_url = $options['da_url']; $post[$da_token] = $_POST[$da_token]; $post[$da_url] = $_POST[$da_url]; } //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'); function salesforce_activate(){ $options = get_option('salesforce2'); if( $options['version'] == '2.0' ) return; $oldoptions = get_option('salesforce'); if( !empty($oldoptions) && $oldoptions['version'] != '2.0' ){ $options = salesforce_default_settings(); //migrate existing data $options['successmsg'] = $oldoptions['successmsg']; $options['errormsg'] = $oldoptions['errormsg']; $options['requiredfieldstext'] = $oldoptions['requiredfieldstext']; $options['sferrormsg'] = $oldoptions['sferrormsg']; $options['source'] = $oldoptions['source']; $options['submitbutton'] = $oldoptions['submitbutton']; $options['usecss'] = $oldoptions['usecss']; $options['wpcf7css'] = $oldoptions['wpcf7css']; //$options['hide_salesforce_link'] = $oldoptions['hide_salesforce_link']; $options['ccusermsg'] = false; //default to off for upgrades $options['org_id'] = $oldoptions['org_id']; //copy existing form input data if( is_array($oldoptions['inputs']) ) foreach($oldoptions['inputs'] as $key=>$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' );