/cf-sprout-invoice.php

  1. <?php 
  2. /** 
  3. * Plugin Name: Caldera Forms - Sprout Invoices Integration 
  4. * Plugin URI:  
  5. * Description: Sprout Invoices Form Processor 
  6. * Version: 1.0.0 
  7. * Author: David Cramer 
  8. * Author URI: https://profiles.wordpress.org/desertsnowman 
  9. * License: GPL-2.0+ 
  10. * License URI: http://www.gnu.org/licenses/gpl-2.0.txt 
  11. */ 
  12.  
  13.  
  14. // Register Processor Filter 
  15. add_filter('caldera_forms_get_form_processors', 'cf_si_register_processor'); 
  16.  
  17. // Meta Viewer Filter for viewing meta data for ana entry 
  18. add_filter('caldera_forms_get_entry_meta_sprout_invoice', 'cf_si_meta_data', 10, 2); 
  19.  
  20. // Capture invoice ID in entry meta 
  21. add_action('si_cloned_post', 'cf_si_add_invoice_meta', 10, 4); 
  22.  
  23. /** 
  24. * Function runs on cloning to create an invoice then adds the Invoice ID to the entries metablock 
  25. * This will allow the Invoice history to show up in the original entry modal. 
  26. */ 
  27. function cf_si_add_invoice_meta($new_post_id, $post_id, $new_post_type, $new_post_status) { 
  28. if($new_post_type === 'sa_invoice') { 
  29.  
  30. global $wpdb; 
  31. // I dont have a method to get entry by meta. I should add one. 
  32. $entry_meta_data = $wpdb->get_row($wpdb->prepare("SELECT `meta`.*, `entry`.* FROM `" . $wpdb->prefix ."cf_form_entry_meta` AS `meta` LEFT JOIN `" . $wpdb->prefix ."cf_form_entries` AS `entry` ON (`meta`.`entry_id` = `entry`.`id`) WHERE `meta_key` = 'estimate_id' AND `meta_value` = %d", $post_id), ARRAY_A); 
  33. if(!empty($entry_meta_data)) { 
  34. $new_meta = array( 
  35. 'entry_id' => $entry_meta_data['entry_id'],  
  36. 'process_id' => $entry_meta_data['process_id'],  
  37. 'meta_key' => 'invoice_id',  
  38. 'meta_value' => $new_post_id 
  39. ); 
  40. $wpdb->insert($wpdb->prefix ."cf_form_entry_meta", $new_meta); 
  41.  
  42.  
  43.  
  44. /** 
  45. * Addes the processor to the forms prosessors registry 
  46. */ 
  47. function cf_si_register_processor($processors) { 
  48.  
  49. // Estimate creation 
  50. $processors['sprout_invoice'] = array( 
  51. "name" => __('Sprout Invoices', 'cf-sprout'),  
  52. "description" => __("Create an estimate from a form submission.", 'cf-sprout'),  
  53. "icon" => plugin_dir_url(__FILE__) . "img/sproutapps.png",  
  54. "author" => "David Cramer",  
  55. "author_url" => "https://profiles.wordpress.org/desertsnowman",  
  56. "processor" => 'cf_si_process_form_estimate',  
  57. "template" => plugin_dir_path(__FILE__) . "config.php",  
  58. "single" => true,  
  59. "meta_template" => plugin_dir_path(__FILE__) . "meta.php",  
  60. "styles" => array( 
  61. plugin_dir_url(__FILE__) . 'css/meta_style.css' 
  62. ),  
  63. "magic_tags" => array( 
  64. "estimate_id" 
  65. ); 
  66.  
  67. // Just a Client Creation 
  68. $processors['sprout_invoice_client'] = array( 
  69. "name" => __('Sprout Client', 'cf-sprout'),  
  70. "description" => __("Create a Sprout Invoces Client from a form submission.", 'cf-sprout'),  
  71. "icon" => plugin_dir_url(__FILE__) . "img/sproutapps.png",  
  72. "author" => "David Cramer",  
  73. "author_url" => "https://profiles.wordpress.org/desertsnowman",  
  74. "processor" => 'cf_si_process_form_client',  
  75. "template" => plugin_dir_path(__FILE__) . "config-client.php",  
  76. "single" => true 
  77. ); 
  78.  
  79. return $processors; 
  80.  
  81.  
  82. /** 
  83. * Function runs on form process to create an estimate. 
  84. * This will create the Estimate and Client and Return the Estimate ID which is then a magic tag. 
  85. */ 
  86. function cf_si_process_form_estimate($config, $form) { 
  87. global $transdata; 
  88.  
  89. foreach ($config as $key => &$value) { 
  90. $value = Caldera_Forms::do_magic_tags($value); 
  91. // Set Values 
  92. $subject = $config['subject']; 
  93. $requirements = $config['requirements']; 
  94. $email = $config['email']; 
  95. $client_name = $config['client_name']; 
  96. $full_name = $config['first_name'] . ' ' . $config['first_name']; 
  97. $website = $config['website']; 
  98. $contact_street = $config['street_address']; 
  99. $contact_city = $config['city']; 
  100. $contact_zone = $config['state']; 
  101. $contact_postal_code = $config['postal_code']; 
  102. $contact_country = $config['country']; 
  103.  
  104. // build arguments for estimate 
  105. $estimate_args = array( 
  106. 'subject' => $subject,  
  107. 'requirements' => $requirements,  
  108. 'fields' => array(),  
  109. 'history_link' => Caldera_Forms::do_magic_tags( $config['history_text'] ),  
  110. ); 
  111.  
  112. // Maybe create the estimate? 
  113. $estimate = @cf_si_maybe_create_estimate( $estimate_args, $config['processor_id'] ); 
  114.  
  115. if ( is_a( $estimate, 'SI_Estimate' ) ) { 
  116. // build the client arguments if it is an estimate 
  117. $client_args = array( 
  118. 'email' => $email,  
  119. 'client_name' => $client_name,  
  120. 'full_name' => $full_name,  
  121. 'website' => $website,  
  122. 'contact_street' => $contact_street,  
  123. 'contact_city' => $contact_city,  
  124. 'contact_zone' => $contact_zone,  
  125. 'contact_postal_code' => $contact_postal_code,  
  126. 'contact_country' => $contact_country 
  127. ); 
  128. // maybe create a client? 
  129. $client = cf_si_maybe_create_client( $estimate, $client_args ); 
  130.  
  131. // return meta data for entry and magic tag values for the rest of the process 
  132. return array('estimate_id' => $transdata['estimate'][$config['processor_id']]); 
  133.  
  134.  
  135. /** 
  136. * Function runs on form process to create an estimate. 
  137. * This will create the Estimate and Client and Return the Estimate ID which is then a magic tag. 
  138. */ 
  139. function cf_si_process_form_client($config, $form) { 
  140. global $transdata; 
  141.  
  142. foreach ($config as $key => &$value) { 
  143. $value = Caldera_Forms::do_magic_tags($value); 
  144. // Set Values 
  145. $email = $config['email']; 
  146. $client_name = $config['client_name']; 
  147. $full_name = $config['first_name'] . ' ' . $config['first_name']; 
  148. $website = $config['website']; 
  149. $contact_street = $config['street_address']; 
  150. $contact_city = $config['city']; 
  151. $contact_zone = $config['state']; 
  152. $contact_postal_code = $config['postal_code']; 
  153. $contact_country = $config['country']; 
  154.  
  155. // build the client arguments 
  156. $client_args = array( 
  157. 'email' => $email,  
  158. 'client_name' => $client_name,  
  159. 'full_name' => $full_name,  
  160. 'website' => $website,  
  161. 'contact_street' => $contact_street,  
  162. 'contact_city' => $contact_city,  
  163. 'contact_zone' => $contact_zone,  
  164. 'contact_postal_code' => $contact_postal_code,  
  165. 'contact_country' => $contact_country 
  166. ); 
  167. // maybe create a client? 
  168. cf_si_maybe_create_client( null, $client_args ); 
  169.  
  170.  
  171. /** 
  172. * Function attempts to create the estimate and return the estimate object 
  173. */ 
  174. function cf_si_maybe_create_estimate( $args = array(), $process_id ) { 
  175. global $transdata; 
  176.  
  177. $defaults = array( 
  178. 'subject' => sprintf( SI_Estimate_Submissions::__('New Estimate: %s'), date( get_option( 'date_format' ).' @ '.get_option( 'time_format' ), current_time( 'timestamp' ) ) ),  
  179. 'requirements' => SI_Estimate_Submissions::__('No requirements submitted. Check to make sure the "requirements" field is required.'),  
  180. 'fields' => $_REQUEST,  
  181. ); 
  182. $parsed_args = wp_parse_args( $args, $defaults ); 
  183.  
  184. // Create estimate 
  185. $estimate_id = SI_Estimate::create_estimate( $parsed_args ); 
  186. $estimate = SI_Estimate::get_instance( $estimate_id ); 
  187.  
  188. // capture ID and add it to the process transient 
  189. $transdata['estimate'][$process_id] = $estimate_id; 
  190.  
  191. // End, don't use estimate_submitted since a notification will be fired. 
  192. do_action( 'estimate_submitted_from_adv_form', $estimate, $parsed_args ); 
  193.  
  194. // History 
  195. do_action( 'si_new_record',  
  196. sprintf( si__('%s.'), $parsed_args['history_link'] ),  
  197. SI_Estimate_Submissions::SUBMISSION_UPDATE,  
  198. $estimate_id,  
  199. sprintf( si__('%s.'), $parsed_args['history_link'] ),  
  200. 0,  
  201. FALSE ); 
  202.  
  203. return $estimate; 
  204.  
  205.  
  206. function cf_si_maybe_create_client( SI_Estimate $estimate = null, $args = array() ) { 
  207. $client_id = ( isset( $args['client_id'] ) && get_post_type( $args['client_id'] ) == SI_Client::POST_TYPE ) ? $args['client_id'] : 0; 
  208. $user_id = get_current_user_id(); 
  209.  
  210. // check to see if the user exists by email 
  211. if ( isset( $args['email'] ) && $args['email'] != '' ) { 
  212. if ( $user = get_user_by('email', $args['email'] ) ) { 
  213. $user_id = $user->ID; 
  214.  
  215. // Check to see if the user is assigned to a client already 
  216. if ( !$client_id ) { 
  217. $client_ids = SI_Client::get_clients_by_user( $user_id ); 
  218. if ( !empty( $client_ids ) ) { 
  219. $client_id = array_pop( $client_ids ); 
  220.  
  221. // Create a user for the submission if an email is provided. 
  222. if ( !$user_id ) { 
  223. // email is critical 
  224. if ( isset( $args['email'] ) && $args['email'] != '' ) { 
  225. $user_args = array( 
  226. 'user_login' => SI_Estimate_Submissions::esc__($args['email']),  
  227. 'display_name' => isset( $args['client_name'] ) ? SI_Estimate_Submissions::esc__($args['client_name']) : SI_Estimate_Submissions::esc__($args['email']),  
  228. 'user_pass' => wp_generate_password(), // random password 
  229. 'user_email' => isset( $args['email'] ) ? SI_Estimate_Submissions::esc__($args['email']) : '',  
  230. 'first_name' => si_split_full_name( SI_Estimate_Submissions::esc__($args['full_name']), 'first' ),  
  231. 'last_name' => si_split_full_name( SI_Estimate_Submissions::esc__($args['full_name']), 'last' ),  
  232. 'user_url' => isset( $args['website'] ) ? SI_Estimate_Submissions::esc__($args['website']) : '' 
  233. ); 
  234. $user_id = SI_Clients::create_user( $user_args ); 
  235.  
  236.  
  237. // create the client based on what's submitted. 
  238. if ( !$client_id ) { 
  239. $address = array( 
  240. 'street' => isset( $args['contact_street'] ) ?SI_Estimate_Submissions::esc__( $args['contact_street']) : '',  
  241. 'city' => isset( $args['contact_city'] ) ? SI_Estimate_Submissions::esc__($args['contact_city']) : '',  
  242. 'zone' => isset( $args['contact_zone'] ) ? SI_Estimate_Submissions::esc__($args['contact_zone']) : '',  
  243. 'postal_code' => isset( $args['contact_postal_code'] ) ? SI_Estimate_Submissions::esc__($args['contact_postal_code']) : '',  
  244. 'country' => isset( $args['contact_country'] ) ? SI_Estimate_Submissions::esc__($args['contact_country']) : '',  
  245. ); 
  246.  
  247. $args = array( 
  248. 'company_name' => isset( $args['client_name'] ) ? SI_Estimate_Submissions::esc__($args['client_name']) : '',  
  249. 'website' => isset( $args['website'] ) ? SI_Estimate_Submissions::esc__($args['website']) : '',  
  250. 'address' => $address,  
  251. 'user_id' => $user_id 
  252. ); 
  253.  
  254. $client_id = SI_Client::new_client( $args ); 
  255. // History 
  256. if( null !== $estimate ) { 
  257. do_action( 'si_new_record',  
  258. sprintf( 'Client Created & Assigned: %s', get_the_title( $client_id ) ),  
  259. SI_Estimate_Submissions::SUBMISSION_UPDATE,  
  260. $estimate->get_id(),  
  261. sprintf( 'Client Created & Assigned: %s', get_the_title( $client_id ) ),  
  262. 0,  
  263. FALSE ); 
  264.  
  265. // Set the estimates client 
  266. if( null !== $estimate ) { 
  267. $estimate->set_client_id( $client_id ); 
  268.  
  269.  
  270. /** 
  271. * Function formats the meta data for viewing in the item modal 
  272. * This is just a copy of the history portion of the template. 
  273. */ 
  274. function cf_si_meta_data($meta, $form) { 
  275. $estimate = get_post( $meta['meta_value'] ); 
  276. if( empty( $estimate ) ) { 
  277. $meta['error'] = __('Estimate invalid or has been deleted'); 
  278. return $meta; 
  279. if($meta['meta_key'] == 'estimate_id') { 
  280. $meta['title'] = __('Estimate', 'cf-sprout'); 
  281. }else if($meta['meta_key'] == 'invoice_id') { 
  282. $meta['title'] = __('Invoice', 'cf-sprout'); 
  283.  
  284. $meta['view_link'] = get_permalink( $estimate->ID ); 
  285. $meta['edit_link'] = 'post.php?post=' . (int) $estimate->ID .'&action=edit'; 
  286.  
  287. ob_start(); 
  288. ?> 
  289. <div id="doc_history"> 
  290. <?php foreach ( si_doc_history_records( $meta['meta_value'] ) as $item_id => $data ): ?> 
  291. <dt> 
  292. <span class="history_status <?php echo $data['status_type'] ?>"><?php echo $data['type']; ?></span><br/> 
  293. <span class="history_date"><?php echo date( get_option( 'date_format' ).' @ '.get_option( 'time_format' ), strtotime( $data['post_date'] ) ) ?></span> 
  294. </dt> 
  295.  
  296. <dd> 
  297. <?php if ( $data['status_type'] == SI_Notifications::RECORD ): ?> 
  298. <p> 
  299. <?php echo $update_title ?> 
  300. <br/><a href="#TB_inline?width=600&height=380&inlineId=notification_message_<?php echo $item_id ?>" id="show_notification_tb_link_<?php echo $item_id ?>" class="thickbox tooltip notification_message" title="<?php si_e('View Message') ?>"><?php si_e('View Message') ?></a> 
  301. </p> 
  302. <div id="notification_message_<?php echo $item_id ?>" class="cloak"> 
  303. <?php echo apply_filters( 'the_content', $data['content'] ) ?> 
  304. </div> 
  305. <?php elseif ( $data['status_type'] == SI_Invoices::VIEWED_STATUS_UPDATE ) : ?> 
  306. <p> 
  307. <?php echo $data['update_title'] ?> 
  308. </p> 
  309. <?php else: ?> 
  310. <?php echo apply_filters( 'the_content', $data['content'] ) ?> 
  311. <?php endif ?> 
  312.  
  313. </dd> 
  314. <?php endforeach ?> 
  315. </div><!-- #doc_history --> 
  316. <?php 
  317. $meta['html'] = ob_get_clean(); 
  318.  
  319. return $meta; 
.