PageRenderTime 935ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/salesforce.php

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