Advertisement
adsleeblythe

salesforce.php

Oct 11th, 2013
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 39.22 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WordPress-to-Lead for Salesforce CRM
  4. Plugin URI: http://www.salesforce.com/form/signup/wordpress-to-lead.jsp?d=70130000000F4Mw
  5. Description: Easily embed a contactform into your posts, pages or your sidebar, and capture the entries straight into Salesforce CRM!
  6. Author: Joost de Valk, Nick Ciske, Modern Tribe Inc.
  7. Version: 2.0.3
  8. Author URI: http://tri.be/
  9. */
  10.  
  11. if ( ! class_exists( 'Salesforce_Admin' ) ) {
  12.  
  13.     require_once('ov_plugin_tools.php');
  14.    
  15.     class Salesforce_Admin extends OV_Plugin_Admin {
  16.  
  17.         var $hook       = 'salesforce-wordpress-to-lead';
  18.         var $filename   = 'salesforce/salesforce.php';
  19.         var $longname   = 'WordPress-to-Lead for Salesforce CRM Configuration';
  20.         var $shortname  = 'Salesforce.com';
  21.         var $optionname = 'salesforce2';
  22.         var $homepage   = 'http://www.salesforce.com/wordpress/';
  23.         var $ozhicon    = 'salesforce-16x16.png';
  24.        
  25.         function Salesforce_Admin() {
  26.             add_action( 'admin_menu', array(&$this, 'register_settings_page') );
  27.             add_filter( 'plugin_action_links', array(&$this, 'add_action_link'), 10, 2 );
  28.             add_filter( 'ozh_adminmenu_icon', array(&$this, 'add_ozh_adminmenu_icon' ) );              
  29.            
  30.             add_action('admin_print_scripts', array(&$this,'config_page_scripts'));
  31.             add_action('admin_print_styles', array(&$this,'config_page_styles'));  
  32.             add_action('admin_footer', array(&$this,'warning'));
  33.            
  34.             add_action('wp_ajax_sfw2l_get_captcha', 'salesforce_captcha');
  35.             add_action('wp_ajax_nopriv_sfw2l_get_captcha', 'salesforce_captcha');
  36.  
  37.         }
  38.                
  39.         function warning() {
  40.             $options  = get_option($this->optionname);
  41.             if (!isset($options['org_id']) || empty($options['org_id']))
  42.                 echo "<div id='message' class='error'><p><strong>".__('Your WordPress-to-Lead settings are not complete.','salesforce')."</strong> ".__('You must enter your Salesforce.com Organisation ID for it to work.','salesforce')." <a href='".$this->plugin_options_url()."'>".__('Settings','salesforce')."</a></p></div>";
  43.                
  44.                 //echo 'ERROR= '.get_option('plugin_error');
  45.                
  46.         }
  47.        
  48.         function config_page() {
  49.            
  50.             $options = get_option($this->optionname);
  51.            
  52.             if ( isset($_POST['submit']) ) {
  53.                
  54.                 //die('<pre>'.print_r($_POST,true)); //DEBUG
  55.  
  56.                 if( isset( $_POST['mode'] ) && $_POST['mode'] == 'editform' ){
  57.  
  58.                     $form_id = (int) $_POST['form_id'];
  59.                    
  60.                     if(!isset($options['forms'][$form_id]))
  61.                         $options['forms'][$form_id] = salesforce_default_form();
  62.  
  63.                     //Begin Save Form Data
  64.                     $newinputs = array();
  65.                     foreach ($options['forms'][$form_id]['inputs'] as $id => $input) {
  66.                         foreach (array('show','required') as $option_name) {
  67.                             if (isset($_POST['inputs'][$id.'_'.$option_name])) {
  68.                                 $newinputs[$id][$option_name] = true;
  69.                                 unset($_POST['inputs'][$id.'_'.$option_name]);
  70.                             } else {
  71.                                 $newinputs[$id][$option_name] = false;
  72.                             }
  73.                         }
  74.                         foreach (array('type','label','value','pos') as $option_name) {
  75.                             if (isset($_POST['inputs'][$id.'_'.$option_name])) {
  76.                                 $newinputs[$id][$option_name] = $_POST['inputs'][$id.'_'.$option_name];
  77.                                 unset($_POST['inputs'][$id.'_'.$option_name]);
  78.                             }
  79.                         }  
  80.                     }
  81.                    
  82.                     //add any new fields
  83.                    
  84.                     if( isset($_POST['add_inputs']) ){
  85.                    
  86.                         foreach ($_POST['add_inputs'] as $key=>$input) {
  87.                        
  88.                             $id = $input['field_name'];
  89.                        
  90.                             if( !empty($id) ){
  91.                                 foreach (array('show','required') as $option_name) {
  92.                                     if (isset($_POST['add_inputs'][$key][$option_name])) {
  93.                                         $newinputs[$id][$option_name] = true;
  94.                                         unset($_POST['add_inputs'][$key][$option_name]);
  95.                                     } else {
  96.                                         $newinputs[$id][$option_name] = false;
  97.                                     }
  98.                                 }
  99.                                
  100.                                 foreach (array('type','label','value','pos') as $option_name) {
  101.                                     if (isset($_POST['add_inputs'][$key][$option_name])) {
  102.                                         $newinputs[$id][$option_name] = $_POST['add_inputs'][$key][$option_name];
  103.                                         unset($_POST['add_inputs'][$key][$option_name]);
  104.                                     }
  105.                                 }
  106.                             }
  107.                         }
  108.                    
  109.                     }
  110.                    
  111.                     w2l_sksort($newinputs,'pos',true);
  112.                     $options['forms'][$form_id]['inputs'] = $newinputs; //TODO
  113.                    
  114.                     foreach (array('form_name','source','returl') as $option_name) {
  115.                         if (isset($_POST[$option_name])) {
  116.                             $options['forms'][$form_id][$option_name] = $_POST[$option_name];
  117.                         }
  118.                     }
  119.                    
  120.                     //End Save Form Data
  121.                
  122.                 }elseif( isset( $_POST['mode'] ) && $_POST['mode'] == 'delete'){
  123.                
  124.                     if( isset( $_POST['form_id'] ) && $_POST['form_id'] != 1 )
  125.                         unset( $options['forms'][$_POST['form_id']] );
  126.                
  127.                 }else{
  128.                
  129.                     //Save general settings
  130.                                    
  131.                     $options  = get_option($this->optionname);
  132.                     if (!current_user_can('manage_options')) die(__('You cannot edit the WordPress-to-Lead options.', 'salesforce'));
  133.                     check_admin_referer('salesforce-udpatesettings');
  134.                    
  135.                     foreach (array('usecss','showccuser','ccadmin','captcha') as $option_name) {
  136.                         if (isset($_POST[$option_name])) {
  137.                             $options[$option_name] = true;
  138.                         } else {
  139.                             $options[$option_name] = false;
  140.                         }
  141.                     }
  142.                    
  143.                    
  144.                     foreach (array('successmsg','errormsg','sferrormsg','org_id','submitbutton','subject','ccusermsg','requiredfieldstext') as $option_name) {
  145.                         if (isset($_POST[$option_name])) {
  146.                             $options[$option_name] = $_POST[$option_name];
  147.                         }
  148.                     }
  149.                 }
  150.                
  151.                 //save changes to DB
  152.                 update_option($this->optionname, $options);
  153.            
  154.             }
  155.            
  156.             //$options = get_option($this->optionname);
  157.  
  158.             if (empty($options))
  159.                 $options = salesforce_default_settings();
  160.            
  161.             ?>
  162.             <div class="wrap">
  163.                 <a href="http://salesforce.com/"><div id="yoast-icon" style="background: url(<?php echo plugins_url('',__FILE__); ?>/salesforce-50x50.png) no-repeat;" class="icon32"><br /></div></a>
  164.                 <h2 style="line-height: 50px;"><?php echo $this->longname; ?></h2>
  165.                 <div class="postbox-container" style="width:70%;">
  166.                    
  167.                     <?php
  168.                    
  169.                     if( isset($_POST['submit']) && empty($_POST['mode']) ){
  170.                         echo '<div id="message" class="updated"><p>' . __('Configuration Saved','salesforce') . '</p></div>';
  171.                     }
  172.                    
  173.                     ?>
  174.                                        
  175.                     <div class="metabox-holder">   
  176.                         <div class="meta-box-sortables">
  177.                             <?php if (!isset($_GET['tab']) || $_GET['tab'] == 'home') { ?>
  178.                             <form action="" method="post" id="salesforce-conf">
  179.                                 <?php if (function_exists('wp_nonce_field')) { wp_nonce_field('salesforce-udpatesettings'); } ?>
  180.                                 <input type="hidden" value="<?php echo $options['version']; ?>" name="version"/>
  181.                                 <?php
  182.                                
  183.                                     //$this->postbox('options','Options','<pre>'.print_r($options,true).'</pre>'); //DEBUG
  184.                                
  185.                                     $content = $this->textinput('successmsg',__('Success message after sending message', 'salesforce') );
  186.                                     $content .= $this->textinput('errormsg',__('Error message when not all form fields are filled', 'salesforce') );
  187.                                     $content .= $this->textinput('sferrormsg',__('Error message when Salesforce.com connection fails', 'salesforce') );
  188.                                     $this->postbox('basicsettings',__('Basic Settings', 'salesforce'),$content);
  189.                                    
  190.                                     $content = $this->textinput('org_id',__('Your Salesforce.com organisation ID','salesforce'));
  191.                                     $content .= '<small>'.__('To find your Organisation ID, in your Salesforce.com account, go to Setup &raquo; Company Profile &raquo; Company Information','salesforce').'</small><br/><br/><br/>';
  192.                                     $this->postbox('sfsettings',__('Salesforce.com Settings', 'salesforce'),$content);
  193.  
  194.                                     $content = $this->checkbox('showccuser',__('Allow user to request a copy of their submission', 'salesforce') );
  195.                                     $content .= '<br/>';
  196.                                     $content .= $this->textinput('ccusermsg',__('Request a copy text', 'salesforce') );
  197.                                     $content .= $this->textinput('subject',__('Email subject', 'salesforce') );
  198.                                     $content .= '<small>'.__('Use %BLOG_NAME% to auto-insert the blog title into the subject','salesforce').'</small><br/><br/><br/>';
  199.  
  200.                                     $content .= $this->checkbox('ccadmin',__('Send blog admin an email notification', 'salesforce') );
  201.                                     $this->postbox('sfsettings',__('Email Settings', 'salesforce'),$content);
  202.  
  203.                                     $content = $this->textinput('submitbutton',__('Submit button text', 'salesforce') );
  204.                                     $content .= $this->textinput('requiredfieldstext',__('Required fields text', 'salesforce') );
  205.                                     $content .= $this->checkbox('usecss',__('Use Form CSS?', 'salesforce') );
  206.                                     $content .= '<br/><small><a href="'.$this->plugin_options_url().'&amp;tab=css">'.__('Read how to copy the CSS to your own CSS file').'</a></small><br><br>';
  207.  
  208.                                     $content .= $this->checkbox('captcha',__('Use CAPTCHA?', 'salesforce') );
  209.                                     $content .= '<br/><small><a href="http://en.wikipedia.org/wiki/CAPTCHA" target="_blank">'.__('Learn more about CAPTCHAs at Wikipedia').'</a></small>';
  210.  
  211.                                     $this->postbox('formsettings',__('Form Settings', 'salesforce'),$content);
  212.                                    
  213.                                    
  214.                                     ?>
  215.                                     <div class="submit"><input type="submit" class="button-primary" name="submit" value="<?php _e("Save WordPress-to-Lead Settings", 'salesforce'); ?>" /></div>
  216.                                     <?php
  217.                                    
  218.                                     $content = '<table border="1">';
  219.                                     $content .= '<tr><th>ID</th><th>Name</th></tr>';       
  220.                                     foreach($options['forms'] as $key=>$form){
  221.                                        
  222.                                         $content .= '<tr><td>'.$key.'</td><td><a href="'.$this->plugin_options_url().'&tab=form&id='.$key.'">'.$form['form_name'].'</a><td></tr>';
  223.                                    
  224.                                     }
  225.                                     $content .= '</table>';
  226.                                    
  227.                                     $content .= '<p><a class="button-secondary" href="'.$this->plugin_options_url().'&tab=form">'.__('Add a new form','salesforce').' &raquo;</a></p>';        
  228.  
  229.                                         $this->postbox('sfforms',__('Forms', 'salesforce'),$content);
  230.  
  231.  
  232.                                 ?>
  233.                                
  234.                             </form>
  235.                             <?php } else if ($_GET['tab'] == 'css') { ?>
  236.                             <?php echo '<p>'.salesforce_back_link($this->plugin_options_url()).'</p>'; ?>
  237.                             <p><?php echo __("If you don't want the inline styling this plugins uses, but add the CSS for the form to your own theme's CSS, you can start by just copying the proper CSS below into your CSS file. Just copy the correct text, and then you can usually find &amp; edit your CSS file",'salesforce'); ?> <a href="<?php echo admin_url('theme-editor.php'); ?>?file=<?php echo str_replace(WP_CONTENT_DIR,'',get_stylesheet_directory()); ?>/style.css&amp;theme=<?php echo urlencode(get_current_theme()); ?>&amp;dir=style"><?php echo __('here','salesforce');?></a>.</p>
  238.                             <div style="width:260px;margin:0 10px 0 0;float:left;">
  239.                                 <div id="normalcss" class="postbox">
  240.                                     <div class="handlediv" title="<?php echo __('Click to toggle','salesforce'); ?>"><br /></div>
  241.                                     <h3 class="hndle"><span><?php echo __('CSS for the normal form','salesforce'); ?></span></h3>
  242.                                     <div class="inside">
  243. <pre>form.w2llead {
  244.   text-align: left;
  245.   clear: both;
  246. }
  247. .w2llabel, .w2linput {
  248.   display: block;
  249.   width: 120px;
  250.   float: left;
  251. }
  252. .w2llabel.error {
  253.   color: #f00;
  254. }
  255. .w2llabel {
  256.   clear: left;
  257.   margin: 4px 0;
  258. }
  259. .w2linput.text {
  260.   width: 200px;
  261.   height: 18px;
  262.   margin: 4px 0;
  263. }
  264. .w2linput.textarea {
  265.   clear: both;
  266.   width: 320px;
  267.   height: 75px;
  268.   margin: 10px 0;
  269. }
  270. .w2linput.submit {
  271.   float: none;
  272.   margin: 10px 0 0 0;
  273.   clear: both;
  274.   width: 150px;
  275. }
  276. .w2linput.checkbox{
  277.   vertical-align: middle;
  278. }
  279. .w2llabel.checkbox{
  280.   clear:both;
  281. }
  282. .w2limg{
  283. display: block; clear: both;
  284. }
  285. #salesforce {
  286.   margin: 3px 0 0 0;
  287.   color: #aaa;
  288. }
  289. #salesforce a {
  290.   color: #999;
  291. }</pre>
  292. </div>
  293. </div></div>
  294. <div style="width:260px;float:left;">
  295.     <div id="widgetcss" class="postbox">
  296.         <div class="handlediv" title="<?php echo __('Click to toggle','salesforce'); ?>"><br /></div>
  297.         <h3 class="hndle"><span><?php echo __('CSS for the sidebar widget form','salesforce'); ?></span></h3>
  298.         <div class="inside">
  299. <pre>.sidebar form.w2llead {
  300.   clear: none;
  301.   text-align: left;
  302. }
  303. .sidebar .w2linput,
  304. .sidebar .w2llabel {
  305.   float: none;
  306.   display: inline;
  307. }
  308. .sidebar .w2llabel.error {
  309.   color: #f00;
  310. }
  311. .sidebar .w2llabel {
  312.   margin: 4px 0;
  313. }
  314. .sidebar .w2linput.text {
  315.   width: 160px;
  316.   height: 18px;
  317.   margin: 4px 0;
  318. }
  319. .sidebar .w2linput.textarea {
  320.   width: 160px;
  321.   height: 50px;
  322.   margin: 10px 0;
  323. }
  324. .sidebar .w2linput.submit {
  325.   margin: 10px 0 0 0;
  326. }
  327. #salesforce {
  328.   margin: 3px 0 0 0;
  329.   color: #aaa;
  330. }
  331. #salesforce a {
  332.   color: #999;
  333. }</pre>
  334. </div></div></div>
  335.  
  336.                             <?php } else if ($_GET['tab'] == 'form') { ?>
  337.  
  338.  
  339. <?php
  340. if(isset($_POST['mode']) && $_POST['mode'] == 'delete' && $form_id != 1 ){
  341.  
  342.     echo '<div id="message" class="updated"><p>' . __('Deleted Form #','salesforce') . $form_id . '</p></div>';
  343.  
  344. }else{
  345.  
  346.     if(!isset($form_id) && isset($_GET['id']))
  347.         $form_id = (int) $_GET['id'];
  348.    
  349.     if( isset($_POST['form_id']) )
  350.         $form_id = (int) $_POST['form_id'];
  351.  
  352.     if( !isset($form_id) || $form_id == 0 ){
  353.         //generate a new default form
  354.         end( $options['forms'] );
  355.         $form_id = key( $options['forms'] ) + 1;
  356.         $options['forms'][$form_id] = salesforce_default_form();
  357.     }
  358.  
  359.     //check for deleted forms
  360.     if( $form_id && !isset($options['forms'][$form_id]) ){
  361.         echo '<div id="message" class="error"><p>' . __('This form could not be found.','salesforce') . '</p></div>';
  362.     }else{
  363.  
  364.         if(isset($_POST['submit']) && $_POST['submit'])
  365.             echo '<div id="message" class="updated"><p>' . __('Form settings updated.','salesforce') . '</p></div>';
  366.     ?>
  367.  
  368.                             <form action="" method="post" id="salesforce-conf">
  369.                                 <?php if (function_exists('wp_nonce_field')) { wp_nonce_field('salesforce-udpatesettings'); } ?>
  370.                                 <input type="hidden" value="<?php echo $options['version']; ?>" name="version"/>
  371.                                 <input type="hidden" value="editform" name="mode"/>                            
  372.                                 <?php
  373.                                
  374.                                     //$this->postbox('options','Options','<pre>'.print_r($options,true).'</pre>'); //DEBUG
  375.  
  376.                                         $content = '<p>';
  377.                                         $content .= '<input type="text" name="form_name" style="width:50%;" value="'.esc_html($options['forms'][$form_id]['form_name']).'">';
  378.                                         //$content .= '<br/><small>'.__('').'</small>';
  379.                                         $content .= '</p>';
  380.                                        
  381.                                         $this->postbox('sfformtitle',__('Form Name', 'salesforce'),$content);
  382.  
  383.                                         $content = '<style type="text/css">th{text-align:left;}</style><table id="salesforce_form_editor">';
  384.                                         $content .= '<tr>'
  385.                                         .'<th width="15%">'.__('Field','salesforce').'</th>'
  386.                                         .'<th width="10%">'.__('Enable','salesforce').'</th>'
  387.                                         .'<th width="10%">'.__('Required','salesforce').'</th>'
  388.                                         .'<th width="10%">'.__('Type','salesforce').'</th>'
  389.                                         .'<th width="20%">'.__('Label','salesforce').'</th>'
  390.                                         .'<th width="20%">'.__('Value','salesforce').'</th>'
  391.                                         .'<th width="10%">'.__('Position','salesforce').'</th>'
  392.                                         .'</tr>';
  393.                                         $i = 1;
  394.                                         foreach ($options['forms'][$form_id]['inputs'] as $field => $input) {
  395.                                             if (empty($input['pos']))
  396.                                                 $input['pos'] = $i;
  397.                                             $content .= '<tr>';
  398.                                             $content .= '<th>'.$field.'</th>';
  399.                                             $content .= '<td><input type="checkbox" name="inputs['.$field.'_show]" '.checked($input['show'],true,false).'/></td>';
  400.                                             $content .= '<td><input type="checkbox" name="inputs['.$field.'_required]" '.checked($input['required'],true,false).'/></td>';
  401.                                             $content .= '<td><select name="inputs['.$field.'_type]">';
  402.                                             $content .= '<option '.selected($input['type'],'text',false).'>text</option>';
  403.                                             $content .= '<option '.selected($input['type'],'textarea',false).'>textarea</option>';
  404.                                             $content .= '<option '.selected($input['type'],'hidden',false).'>hidden</option>';
  405.                                             $content .= '</select></td>';
  406.                                             $content .= '<td><input size="20" name="inputs['.$field.'_label]" type="text" value="'.esc_html($input['label']).'"/></td>';
  407.                                            
  408.                                             $content .= '<td><input size="20" name="inputs['.$field.'_value]" type="text" value="';
  409.                                             if( isset($input['value']) ) $content .= esc_html($input['value']);
  410.                                             $content .= '"/></td>';
  411.                                             $content .= '<td><input size="2" name="inputs['.$field.'_pos]" type="text" value="'.esc_html($input['pos']).'"/></td>';
  412.                                             $content .= '</tr>';
  413.                                             $i++;
  414.                                         }
  415.                                        
  416.                                         $content .= '</table>';
  417.                                        
  418.                                         ?>
  419. <script>
  420.  
  421. var pos = <?php echo $i; ?>;
  422. var i = 1;
  423. function salesforce_add_field(){
  424.    
  425.     var row = '<tr>';
  426.     row += '<td><input type="text" size="10" name="add_inputs['+i+'][field_name]"></td>';
  427.     row += '<td><input type="checkbox" name="add_inputs['+i+'][show]"></td>';
  428.     row += '<td><input type="checkbox" name="add_inputs['+i+'][required]"></td>';
  429.     row += '<td><select name="add_inputs['+i+'][type]"><option>text</option><option>textarea</option><option>hidden</option></select></td>';
  430.     row += '<td><input type="text" name="add_inputs['+i+'][label]"></td>';
  431.     row += '<td><input type="text" name="add_inputs['+i+'][value]"></td>';
  432.     row += '<td><input type="text" size="2" name="add_inputs['+i+'][pos]" value="'+pos+'"></td>';
  433.     row += '</tr>';
  434.    
  435.     jQuery('#salesforce_form_editor tbody').append(row);
  436.    
  437.     pos++;
  438.     i++;
  439.  
  440. }
  441.  
  442. </script>
  443.                                         <?php
  444.                                        
  445.                                         $content .= '<p><a class="button-secondary" href="javascript:salesforce_add_field();">Add a field</a></p>';
  446.                                        
  447.                                         $this->postbox('sffields',__('Form Fields', 'salesforce'),$content);
  448.                                        
  449.                                         $content = '<p>';
  450.                                         $content .= '<label>'.__('Lead Source:','salesforce').'</label><br/>';
  451.                                         $content .= '<input type="text" name="source" style="width:50%;" value="'.esc_html($options['forms'][$form_id]['source']).'">';
  452.                                         $content .= '<br/><small>'.__('Lead Source to display in Salesforce.com, use %URL% to include the URL of the page containing the form').'</small>';
  453.                                         $content .= '</p>';
  454.                                        
  455.                                         $content .= '<p>';
  456.                                         $content .= '<label>'.__('Return/Thanks URL:','salesforce').'</label><br/>';
  457.                                         $content .= '<input type="text" name="returl" style="width:50%;" value="'.esc_html($options['forms'][$form_id]['returl']).'">';
  458.                                         $content .= '<br/><small>'.__('e.g.http://yoursite.com/thanks/').'</small>';
  459.                                         $content .= '</p>';
  460.  
  461.                                         $content .= '<input type="hidden" name="form_id" id="form_id" value="'.$form_id.'">';
  462.                                        
  463.                                         $this->postbox('sfformmeta',__('Form Settings', 'salesforce'),$content);
  464.                                    
  465.                                 ?>
  466.                                
  467.                                 <div class="submit"><input type="submit" class="button-primary" name="submit" value="<?php _e("Save Form", 'salesforce'); ?>" /></div>
  468.                             </form>
  469.                            
  470.                                 <?php if( !empty($_GET['id']) && $_GET['id'] != 1 ){ ?>
  471.                                 <form action="" method="post" id="salesforce-delete">
  472.                                 <?php if (function_exists('wp_nonce_field')) { wp_nonce_field('salesforce-udpatesettings'); } ?>
  473.                                     <input type="hidden" value="delete" name="mode"/>
  474.                                     <input type="hidden" value="<?php echo $form_id; ?>" name="form_id"/>
  475.                                     <input type="submit" name="submit" class="button-secondary" value="Delete this form">
  476.                                 </form>
  477.                                 <?php } ?>
  478. <?php } ?>
  479.                     <?php } ?>
  480.                    
  481.                     <?php echo '<p>'.salesforce_back_link($this->plugin_options_url()).'</p>'; ?>
  482.                    
  483.                             <?php } ?>
  484.                         </div>
  485.                     </div>
  486.                 </div>
  487.                 <div class="postbox-container" style="width:20%;">
  488.                     <div class="metabox-holder">   
  489.                         <div class="meta-box-sortables">
  490.                             <?php
  491.                                 $this->postbox('usesalesforce',__('How to Use This Plugin','salesforce'),__('<p>To use this form, copy the following shortcode into a post or page:</p><pre style="padding:5px 10px;margin:10px 0;background-color:lightyellow;">[salesforce form="X"]</pre><p>Replace X with the form number for the form you want to show.</p><p>Make sure you have entered all the correct settings on the left, including your Organisation ID.</p>','salesforce'));
  492.                                 $this->plugin_like(false);
  493.                                 $this->plugin_support();
  494.                                 // $this->news();
  495.                             ?>
  496.                         </div>
  497.                         <br/><br/><br/>
  498.                     </div>
  499.                 </div>
  500.             </div>
  501.             <?php
  502.         }
  503.     } // end class SalesForce_Admin
  504.     $salesforce = new Salesforce_Admin();
  505. }
  506.  
  507. function salesforce_default_settings() {
  508.     $options = array();
  509.     $options['version']             = '2.0';
  510.     $options['successmsg']          = __('Success!','salesforce');
  511.     $options['errormsg']            = __('There was an error, please fill all required fields.','salesforce');
  512.     $options['requiredfieldstext']  = __('These fields are required.','salesforce');
  513.     $options['sferrormsg']          = __('Failed to connect to Salesforce.com.','salesforce');
  514.     $options['submitbutton']        = __('Submit','salesforce');
  515.     $options['subject']             = __('Thank you for contacting %BLOG_NAME%','salesforce');
  516.     $options['showccuser']          = true;
  517.     $options['ccusermsg']           = __('Send me a copy','salesforce');
  518.     $options['ccadmin']             = false;
  519.     $options['captcha']             = false;
  520.  
  521.     $options['usecss']              = true;
  522.  
  523.     $options['forms'][1] = salesforce_default_form();
  524.    
  525.     update_option('salesforce2', $options);
  526.    
  527.     return $options;
  528. }
  529.  
  530. function salesforce_default_form() {
  531.  
  532.     $dform = array();
  533.    
  534.     $dform['form_name'] = 'My Lead Form '.date('Y-m-d h:i:s');
  535.     $dform['source'] = __('Lead form on ','salesforce').get_bloginfo('name');
  536.     $dform['returl'] = '';
  537.    
  538.     $dform['inputs'] = array(
  539.             'first_name'    => array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true),
  540.             'first_name'    => array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true),
  541.             'last_name'     => array('type' => 'text', 'label' => 'Last name', 'show' => true, 'required' => true),
  542.             'email'         => array('type' => 'text', 'label' => 'Email', 'show' => true, 'required' => true),
  543.             'phone'         => array('type' => 'text', 'label' => 'Phone', 'show' => true, 'required' => false),
  544.             'description'   => array('type' => 'textarea', 'label' => 'Message', 'show' => true, 'required' => true),
  545.             'title'         => array('type' => 'text', 'label' => 'Title', 'show' => false, 'required' => false),
  546.             'company'       => array('type' => 'text', 'label' => 'Company', 'show' => false, 'required' => false),
  547.             'street'        => array('type' => 'text', 'label' => 'Street', 'show' => false, 'required' => false),
  548.             'city'          => array('type' => 'text', 'label' => 'City', 'show' => false, 'required' => false),
  549.             'state'         => array('type' => 'text', 'label' => 'State', 'show' => false, 'required' => false),
  550.             'zip'           => array('type' => 'text', 'label' => 'ZIP', 'show' => false, 'required' => false),
  551.             'country'       => array('type' => 'text', 'label' => 'Country', 'show' => false, 'required' => false),
  552.             'Campaign_ID'   => array('type' => 'hidden', 'label' => 'Campaign ID', 'show' => false, 'required' => false),
  553.         );
  554.    
  555.     return $dform;
  556.  
  557. }
  558.  
  559. function salesforce_back_link($url){
  560.  
  561.     return '<a href="'.$url.'">&laquo; '.__('Back to configuration page','salesforce').'</a>';
  562.  
  563. }
  564.  
  565. /**
  566.  * Sort input array by $subkey
  567.  * Taken from: http://php.net/manual/en/function.ksort.php
  568.  */
  569. function w2l_sksort(&$array, $subkey="id", $sort_ascending=false) {
  570.  
  571.     if( !is_array( $array ) )
  572.         return $array;
  573.        
  574.     $temp_array = array();
  575.  
  576.     if (count($array))
  577.         $temp_array[key($array)] = array_shift($array);
  578.  
  579.     foreach($array as $key => $val){
  580.         $offset = 0;
  581.         $found = false;
  582.         foreach($temp_array as $tmp_key => $tmp_val)
  583.         {
  584.             if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) {
  585.                 $temp_array = array_merge(    (array)array_slice($temp_array,0,$offset),
  586.                                             array($key => $val),
  587.                                             array_slice($temp_array,$offset)
  588.                                           );
  589.                 $found = true;
  590.             }
  591.             $offset++;
  592.         }
  593.         if(!$found) $temp_array = array_merge($temp_array, array($key => $val));
  594.     }
  595.  
  596.     if ($sort_ascending) $array = array_reverse($temp_array);
  597.  
  598.     else $array = $temp_array;
  599. }
  600.  
  601.  
  602. function salesforce_captcha(){
  603.     include("lib/captcha/captcha.php");
  604.     die();
  605. }
  606.  
  607. function salesforce_form($options, $is_sidebar = false, $content = '', $form_id = 1) {
  608.    
  609.     if( !isset($options['forms'][$form_id]) )
  610.         return;
  611.    
  612.     if (!empty($content))
  613.         $content = wpautop('<strong>'.$content.'</strong>');
  614.        
  615.     if ($options['usecss'] && !$is_sidebar) {
  616.         $content .= '<style type="text/css">
  617.         form.w2llead{ text-align:left; clear:both;}
  618.         .w2llabel, .w2linput { display:block; float:left; }
  619.         .w2llabel.error { color:#f00; }
  620.         .w2llabel { clear:left; margin:4px 0; width:50%; }
  621.         .w2linput.text{ width:50%; height:18px; margin:4px 0; }
  622.         .w2linput.textarea { clear:both; width:100%; height:75px; margin:10px 0;}
  623.         .w2lsubmit{ float:none; clear:both; }
  624.         .w2linput.submit { float:none; margin: 10px 0 0 0; clear:both;}
  625.         .w2linput.checkbox{ vertical-align: middle;}
  626.         .w2llabel.checkbox{ clear:both; }
  627.         .w2limg{ display: block; clear: both; }
  628.         #salesforce{ margin:3px 0 0 0; color:#aaa; }
  629.         #salesforce a{ color:#999; }
  630.         </style>';
  631.     } elseif ($is_sidebar && $options['usecss']) {
  632.         $content .= '<style type="text/css">
  633.         .sidebar form.w2llead{ clear:none; text-align:left; }
  634.         .sidebar .w2linput, #sidebar .w2llabel{ float:none; display:inline; }
  635.         .sidebar .w2llabel.error { color:#f00; }
  636.         .sidebar .w2llabel { margin:4px 0; float:none; display:inline; }
  637.         .sidebar .w2linput.text{ width:95%; height:18px; margin:4px 0;}
  638.         .sidebar .w2linput.textarea {width:95%; height:50px; margin:10px 0;}
  639.         .sidebar .w2lsubmit{ float:none; clear:both; }
  640.         .sidebar .w2linput.submit { margin:10px 0 0 0; }
  641.         #salesforce{ margin:3px 0 0 0; color:#aaa; }
  642.         #salesforce a{ color:#999; }
  643.         </style>';
  644.     }
  645.     $sidebar = '';
  646.    
  647.     if ( $is_sidebar )
  648.         $sidebar = ' sidebar';
  649.        
  650.     $content .= "\n".'<form id="salesforce_w2l_lead_'.$form_id.str_replace(' ','_',$sidebar).'" class="w2llead'.$sidebar.'" method="post">'."\n";
  651.  
  652.     foreach ($options['forms'][$form_id]['inputs'] as $id => $input) {
  653.         if (!$input['show'])
  654.             continue;
  655.         $val    = '';
  656.         if (isset($_POST[$id])){
  657.             $val    = esc_attr(strip_tags(stripslashes($_POST[$id])));
  658.         }else{
  659.             if( isset($input['value']) ) $val   = esc_attr(strip_tags(stripslashes($input['value'])));
  660.         }
  661.  
  662.         $error  = ' ';
  663.         if (isset($input['error']) && $input['error'])
  664.             $error  = ' error ';
  665.            
  666.         if($input['type'] != 'hidden')
  667.             $content .= "\t".'<label class="w2llabel'.$error.$input['type'].'" for="sf_'.$id.'">'.esc_html(stripslashes($input['label'])).':';
  668.        
  669.         if ($input['required'] && $input['type'] != 'hidden')
  670.             $content .= ' *';
  671.        
  672.         if($input['type'] != 'hidden')
  673.             $content .= '</label>'."\n";
  674.        
  675.         if ($input['type'] == 'text') {        
  676.             $content .= "\t".'<input value="'.$val.'" id="sf_'.$id.'" class="w2linput text" name="'.$id.'" type="text"/><br/>'."\n\n";
  677.         } else if ($input['type'] == 'textarea') {
  678.             $content .= "\t".'<br/>'."\n\t".'<textarea id="sf_'.$id.'" class="w2linput textarea" name="'.$id.'">'.$val.'</textarea><br/>'."\n\n";
  679.         } else if ($input['type'] == 'hidden') {
  680.             $content .= "\t\n\t".'<input type="hidden" id="sf_'.$id.'" class="w2linput hidden" name="'.$id.'" value="'.$val.'">'."\n\n";
  681.         }
  682.     }
  683.  
  684.     //captcha
  685.    
  686.     if($options['captcha']){
  687.    
  688.         include("lib/captcha/captcha.php");
  689.         $captcha = captcha();
  690.        
  691.         //$content .=  'CODE='.$captcha['code'].'<hr>';
  692.    
  693.         $sf_hash = sha1($captcha['code'].NONCE_SALT);
  694.    
  695.         set_transient( $sf_hash, $captcha['code'], 60*15 );
  696.    
  697.         $content .=  '<label class="w2llabel">'.__('Type the text shown: *','salesforce').'</label><br>
  698.             <img class="w2limg" src="' . $captcha['image_src'] . '&hash=' . $sf_hash . '" alt="CAPTCHA image" /><br>';
  699.  
  700.         $content .=  '<input type="text" class="w2linput text" name="captcha_text" value=""><br>';
  701.         $content .=  '<input type="hidden" class="w2linput hidden" name="captcha_hash" value="'. $sf_hash .'">';
  702.    
  703.     }
  704.    
  705.     //send me a copy
  706.     if( $options['showccuser'] ){
  707.         $label = $options['ccusermsg'];
  708.         if( empty($label) ) $label = __('Send me a copy','salesforce');
  709.         $content .= "\t\n\t".'<p><label class="w2llabel checkbox"><input type="checkbox" name="w2lcc" class="w2linput checkbox" value="1"/> '.esc_html($label)."</label><p>\n";
  710.     }
  711.    
  712.     //spam honeypot
  713.     $content .= "\t".'<input type="text" name="message" class="w2linput" value="" style="display: none;"/>'."\n";
  714.  
  715.     //form id
  716.     $content .= "\t".'<input type="hidden" name="form_id" class="w2linput" value="'.$form_id.'" />'."\n";
  717.  
  718.     $submit = stripslashes($options['submitbutton']);
  719.     if (empty($submit))
  720.         $submit = "Submit";
  721.     $content .= "\t".'<div class="w2lsubmit"><input type="submit" name="w2lsubmit" class="w2linput submit" value="'.esc_attr($submit).'"/></div>'."\n";
  722.     $content .= '</form>'."\n";
  723.  
  724.     $reqtext = stripslashes($options['requiredfieldstext']);
  725.     if (!empty($reqtext))
  726.         $content .= '<p id="requiredfieldsmsg"><sup>*</sup>'.esc_html($reqtext).'</p>';
  727.     $content .= '<div id="salesforce"><small>'.__('Powered by','salesforce').' <a href="http://www.salesforce.com/">Salesforce CRM</a></small></div>';
  728.    
  729.     $content = apply_filters('salesforce_w2l_form_html', $content);
  730.    
  731.     return $content;
  732. }
  733.  
  734. function submit_salesforce_form($post, $options) {
  735.    
  736.     global $wp_version;
  737.     if (!isset($options['org_id']) || empty($options['org_id']))
  738.         return false;
  739.  
  740.     //spam honeypot
  741.     if( !empty($_POST['message']) )
  742.         return false;
  743.  
  744.     //print_r($_POST); //DEBUG
  745.    
  746.     $form_id = intval( $_POST['form_id'] );
  747.  
  748.     $post['oid']            = $options['org_id'];
  749.     $post['lead_source']    = str_replace('%URL%','['.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].']',$options['forms'][$form_id]['source']);
  750.     $post['00ND0000005cTaK'] = '['.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].']';
  751.     $post['debug']          = 0;
  752.  
  753.     // Set SSL verify to false because of server issues.
  754.     $args = array(  
  755.         'body'      => $post,
  756.         'headers'   => array(
  757.             'user-agent' => 'WordPress-to-Lead for Salesforce plugin - WordPress/'.$wp_version.'; '.get_bloginfo('url'),
  758.         ),
  759.         'sslverify' => false,  
  760.     );
  761.    
  762.     $result = wp_remote_post('https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', $args);
  763.    
  764.     if( is_wp_error($result) )
  765.         return false;
  766.    
  767.     if ($result['response']['code'] == 200){
  768.  
  769.         if( isset( $_POST['w2lcc'] ) && $_POST['w2lcc'] == 1 )
  770.             salesforce_cc_user($post, $options, $form_id);
  771.  
  772.         if( isset( $options['ccadmin'] ) && $options['ccadmin'] )
  773.             salesforce_cc_admin($post, $options, $form_id);
  774.        
  775.         return true;
  776.     }else{
  777.  
  778.         return false;
  779.     }
  780. }
  781.  
  782. function salesforce_cc_user($post, $options, $form_id = 1){
  783.    
  784.     $from_name = apply_filters('salesforce_w2l_cc_user_from_name', get_bloginfo('name'));
  785.     $from_email = apply_filters('salesforce_w2l_cc_user_from_email', get_option('admin_email'));
  786.    
  787.     $headers = 'From: '.$from_name.' <' . $from_email . ">\r\n";
  788.  
  789.     $subject = str_replace('%BLOG_NAME%', get_bloginfo('name'), $options['subject']);
  790.     if( empty($subject) ) $subject = __('Thank you for contacting','salesforce').' '.get_bloginfo('name');
  791.  
  792.     //remove hidden fields
  793.     foreach ($options['forms'][$form_id]['inputs'] as $id => $input) {
  794.         if( $input['type'] == 'hidden' )
  795.             unset( $post[$id] );
  796.     }
  797.    
  798.     unset($post['oid']);
  799.     unset($post['lead_source']);
  800.     unset($post['debug']);
  801.    
  802.     $message = '';
  803.  
  804.     //$message .= print_r( $post , true);
  805.     //$message .= print_r( $options['forms'][$form_id]['inputs'] , true);
  806.  
  807.    
  808.     //format message
  809.     foreach($post as $name=>$value){
  810.         if( !empty($value) )
  811.             $message .= $options['forms'][$form_id]['inputs'][$name]['label'].': '.$value."\r\n";
  812.     }
  813.  
  814.     wp_mail( $_POST['email'], $subject, $message, $headers );
  815.  
  816. }
  817.  
  818. function salesforce_cc_admin($post, $options, $form_id = 1){
  819.  
  820.     $from_name = apply_filters('salesforce_w2l_cc_admin_from_name', get_bloginfo('name'));
  821.     $from_email = apply_filters('salesforce_w2l_cc_admin_from_email', get_option('admin_email'));
  822.    
  823.     $headers = 'From: '.$from_name.' <' . $from_email . ">\r\n";
  824.  
  825.     $subject = __('Salesforce WP to Lead Submission','salesforce');
  826.  
  827.     $message = '';
  828.  
  829.     unset($post['oid']);
  830.     unset($post['lead_source']);
  831.     unset($post['debug']);
  832.    
  833.     //format message
  834.     foreach($post as $name=>$value){
  835.         if( !empty($value) )
  836.             $message .= $options['forms'][$form_id]['inputs'][$name]['label'].': '.$value."\r\n";
  837.     }
  838.  
  839.     $emails = array( get_option('admin_email') );
  840.  
  841.     $emails = apply_filters( 'salesforce_w2l_cc_admin_email_list', $emails );
  842.    
  843.     //print_r( $emails );
  844.    
  845.     foreach( $emails as $email ){
  846.         wp_mail( $email, $subject, $message, $headers );
  847.     }
  848.  
  849. }
  850.  
  851. function salesforce_form_shortcode($atts) {
  852.  
  853.     extract( shortcode_atts( array(
  854.         'form' => '1',
  855.         'sidebar' => false,
  856.     ), $atts ) );
  857.    
  858.     $emailerror = '';
  859.     $captchaerror = '';
  860.     $content = '';
  861.    
  862.     $form = (int) $form;
  863.     $sidebar = (bool) $sidebar;
  864.    
  865.     $options = get_option("salesforce2");
  866.     if (!is_array($options))
  867.         $options = salesforce_default_settings();
  868.  
  869.     //don't submit unless we're in the right shortcode
  870.     if( isset( $_POST['form_id'] ) ){
  871.         $form_id = intval( $_POST['form_id'] );
  872.        
  873.         if( $form_id != $form ){
  874.             $content = salesforce_form($options, $sidebar, null, $form);
  875.             return $content;
  876.            
  877.         }
  878.     }
  879.  
  880.     //this is the right form, continue
  881.     if (isset($_POST['w2lsubmit'])) {
  882.         $error = false;
  883.         $post = array();
  884.        
  885.         foreach ($options['forms'][$form]['inputs'] as $id => $input) {
  886.             if ($input['required'] && empty($_POST[$id])) {
  887.                 $options['forms'][$form]['inputs'][$id]['error'] = true;
  888.                 $error = true;
  889.             } else if ($id == 'email' && $input['required'] && !is_email($_POST[$id]) ) {
  890.                 $error = true;
  891.                 $emailerror = true;
  892.             } else {
  893.                 if( isset($_POST[$id]) ) $post[$id] = trim(strip_tags(stripslashes($_POST[$id])));
  894.             }
  895.         }
  896.        
  897.         //check captcha if enabled
  898.         if( $options['captcha'] ){
  899.            
  900.             if( $_POST['captcha_hash'] != sha1( $_POST['captcha_text'].NONCE_SALT )){
  901.                 $error = true;
  902.                 $captchaerror = true;
  903.             }
  904.            
  905.         }
  906.        
  907.         if (!$error) {
  908.             $result = submit_salesforce_form($post, $options, $form);
  909.            
  910.             //echo 'RESULT='.$result;
  911.             //if($result) echo 'true';
  912.             //if(!$result) echo 'false';
  913.                        
  914.             if (!$result){
  915.                
  916.                 $content = '<strong>'.esc_html(stripslashes($options['sferrormsg'])).'</strong>';          
  917.             }else{
  918.            
  919.                 if( !empty($options['forms'][$form]['returl']) ){
  920.                     //wp_redirect( $options['forms'][$form]['returl'] );
  921.                     //exit;
  922.                    
  923.                     ?>
  924.                     <script type="text/javascript">
  925.                    <!--
  926.                       window.location= <?php echo "'" . $options['forms'][$form]['returl'] . "'"; ?>;
  927.                    //-->
  928.                    </script>
  929.                     <?php
  930.                 }
  931.            
  932.                 $content = '<strong>'.esc_html(stripslashes($options['successmsg'])).'</strong>';
  933.             }
  934.         } else {
  935.             $errormsg = esc_html( stripslashes($options['errormsg']) ) ;
  936.             if ($emailerror)
  937.                 $errormsg .= '<br/>'.__('The email address you entered is not a valid email address.','salesforce');
  938.            
  939.             if ($captchaerror)
  940.                 $errormsg .= '<br/>'.__('The text you entered did not match the image.','salesforce');
  941.            
  942.             $content .= salesforce_form($options, $sidebar, $errormsg, $form);
  943.         }
  944.     } else {
  945.         $content = salesforce_form($options, $sidebar, null, $form);
  946.     }
  947.    
  948.     return $content;
  949. }
  950.  
  951. add_shortcode('salesforce', 'salesforce_form_shortcode');  
  952.  
  953. class Salesforce_WordPress_to_Lead_Widgets extends WP_Widget {
  954.  
  955.     function Salesforce_WordPress_to_Lead_Widgets() {
  956.         $widget_ops = array( 'classname' => 'salesforce', 'description' => __('Displays a WordPress-to-Lead for Salesforce Form','salesforce') );
  957.         $control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => 'salesforce' );
  958.         $this->WP_Widget( 'salesforce', 'Salesforce', $widget_ops, $control_ops );
  959.     }
  960.  
  961.     function widget( $args, $instance ) {
  962.         extract( $args );
  963.         echo $before_widget;
  964.         $title = apply_filters('widget_title', $instance['title'] );
  965.         if ( $title ) {
  966.             echo $before_title . $title . $after_title;
  967.         }
  968.         if ( !empty($instance['desc']) && empty($_POST) ) {
  969.             echo '<p>' . $instance['desc'] . '</p>';
  970.         }
  971.         $is_sidebar = true;
  972.         echo do_shortcode('[salesforce form="'.$instance['form'].'" sidebar="true"]');
  973.         echo $after_widget;
  974.     }
  975.  
  976.     function update( $new_instance, $old_instance ) {
  977.         $instance = $old_instance;
  978.         foreach ( array('title', 'desc', 'form') as $val ) {
  979.             $instance[$val] = strip_tags( $new_instance[$val] );
  980.         }
  981.         return $instance;
  982.     }
  983.  
  984.     function form( $instance ) {
  985.         $defaults = array(
  986.             'title' => 'Contact Us',
  987.             'desc'  => 'Contact us using the form below',
  988.             'form'  => 1,
  989.         );
  990.         $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  991.         <p>
  992.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e("Title"); ?>:</label>
  993.             <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
  994.         </p>
  995.  
  996.         <p>
  997.             <label for="<?php echo $this->get_field_id( 'desc' ); ?>"><?php _e("Introduction"); ?>:</label>
  998.             <input id="<?php echo $this->get_field_id( 'desc' ); ?>" name="<?php echo $this->get_field_name( 'desc' ); ?>" value="<?php echo $instance['desc']; ?>" style="width:90%;" />
  999.         </p>
  1000.  
  1001.         <p>
  1002.             <label for="<?php echo $this->get_field_id( 'form' ); ?>"><?php _e("Form"); ?>:</label>
  1003.             <select id="<?php echo $this->get_field_id( 'form' ); ?>" name="<?php echo $this->get_field_name( 'form' ); ?>">
  1004.                 <?php
  1005.                 $sfoptions = get_option('salesforce2');
  1006.                
  1007.                 foreach($sfoptions['forms'] as $key=>$value){
  1008.                    
  1009.                     echo '<option value="'.$key.'"';
  1010.                     if( $instance['form'] == $key)
  1011.                         echo ' selected="selected"';
  1012.                     echo '>'.$value['form_name'].'</option>';
  1013.                                        
  1014.                
  1015.                 }
  1016.                 ?>
  1017.             </select>
  1018.         </p>
  1019.  
  1020.  
  1021.     <?php
  1022.     }
  1023. }
  1024.  
  1025. function salesforce_widget_func() {
  1026.     register_widget( 'Salesforce_WordPress_to_Lead_Widgets' );
  1027. }
  1028. add_action( 'widgets_init', 'salesforce_widget_func' );
  1029.  
  1030. function salesforce_activate(){
  1031.  
  1032.     $options = get_option('salesforce2');
  1033.  
  1034.     if( $options['version'] == '2.0' )
  1035.         return;
  1036.  
  1037.     $oldoptions = get_option('salesforce');
  1038.    
  1039.     if( !empty($oldoptions) && $oldoptions['version'] != '2.0' ){
  1040.  
  1041.         $options = salesforce_default_settings();
  1042.        
  1043.         //migrate existing data
  1044.         $options['successmsg']          = $oldoptions['successmsg'];
  1045.         $options['errormsg']            = $oldoptions['errormsg'];
  1046.         $options['requiredfieldstext']  = $oldoptions['requiredfieldstext'];
  1047.         $options['sferrormsg']          = $oldoptions['sferrormsg'];
  1048.         $options['source']              = $oldoptions['source'];
  1049.         $options['submitbutton']        = $oldoptions['submitbutton'];
  1050.    
  1051.         $options['usecss']              = $oldoptions['usecss'];
  1052.  
  1053.         $options['ccusermsg']           = false; //default to off for upgrades
  1054.  
  1055.         $options['org_id']              = $oldoptions['org_id'];
  1056.  
  1057.         //copy existing form input data
  1058.         if( is_array($oldoptions['inputs']) )
  1059.             foreach($oldoptions['inputs'] as $key=>$val){
  1060.                 $newinputs[$key] = $val;
  1061.             }
  1062.  
  1063.         //sort merged inputs
  1064.         w2l_sksort($newinputs,'pos',true);
  1065.        
  1066.         //save merged and sorted inputs
  1067.         $options['forms'][1]['inputs'] = $newinputs;
  1068.  
  1069.         //source is now saved per form
  1070.         $options['forms'][1]['source']  = $oldoptions['source'];
  1071.  
  1072.         update_option('salesforce2', $options);
  1073.         //$options = get_option('salesforce');
  1074.  
  1075.     }
  1076.    
  1077.     if( empty($oldoptions) ){
  1078.         salesforce_default_settings();
  1079.     }
  1080.  
  1081. }
  1082.  
  1083. /*
  1084. //Save Activation Error to DB for review
  1085. add_action('activated_plugin','save_error');
  1086. function save_error(){
  1087.     update_option('plugin_error',  ob_get_contents());
  1088. }
  1089. */
  1090.  
  1091. register_activation_hook( __FILE__, 'salesforce_activate' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement