cf_si_maybe_create_client

The Caldera Forms - Sprout Invoices Integration cf si maybe create client function.

Description

cf_si_maybe_create_client( (constant) SI_Estimate $estimate = null, (array) $args = array() ); 

Parameters (2)

0. $estimate — Optional. (constant) => SI_Estimate $estimate = null
The estimate.
1. $args — Optional. (array) => array()
The args.

Usage

  1. if ( !function_exists( 'cf_si_maybe_create_client' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'caldera-forms-sprout-invoices-integration/cf-sprout-invoice.php'; 
  3.  
  4. // The estimate. 
  5. $estimate = SI_Estimate $estimate = null; 
  6.  
  7. // The args. 
  8. $args = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = cf_si_maybe_create_client($estimate, $args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/cf-sprout-invoice.php  
  1. function cf_si_maybe_create_client( SI_Estimate $estimate = null, $args = array() ) { 
  2. $client_id = ( isset( $args['client_id'] ) && get_post_type( $args['client_id'] ) == SI_Client::POST_TYPE ) ? $args['client_id'] : 0; 
  3. $user_id = get_current_user_id(); 
  4.  
  5. // check to see if the user exists by email 
  6. if ( isset( $args['email'] ) && $args['email'] != '' ) { 
  7. if ( $user = get_user_by('email', $args['email'] ) ) { 
  8. $user_id = $user->ID; 
  9.  
  10. // Check to see if the user is assigned to a client already 
  11. if ( !$client_id ) { 
  12. $client_ids = SI_Client::get_clients_by_user( $user_id ); 
  13. if ( !empty( $client_ids ) ) { 
  14. $client_id = array_pop( $client_ids ); 
  15.  
  16. // Create a user for the submission if an email is provided. 
  17. if ( !$user_id ) { 
  18. // email is critical 
  19. if ( isset( $args['email'] ) && $args['email'] != '' ) { 
  20. $user_args = array( 
  21. 'user_login' => SI_Estimate_Submissions::esc__($args['email']),  
  22. 'display_name' => isset( $args['client_name'] ) ? SI_Estimate_Submissions::esc__($args['client_name']) : SI_Estimate_Submissions::esc__($args['email']),  
  23. 'user_pass' => wp_generate_password(), // random password 
  24. 'user_email' => isset( $args['email'] ) ? SI_Estimate_Submissions::esc__($args['email']) : '',  
  25. 'first_name' => si_split_full_name( SI_Estimate_Submissions::esc__($args['full_name']), 'first' ),  
  26. 'last_name' => si_split_full_name( SI_Estimate_Submissions::esc__($args['full_name']), 'last' ),  
  27. 'user_url' => isset( $args['website'] ) ? SI_Estimate_Submissions::esc__($args['website']) : '' 
  28. ); 
  29. $user_id = SI_Clients::create_user( $user_args ); 
  30.  
  31.  
  32. // create the client based on what's submitted. 
  33. if ( !$client_id ) { 
  34. $address = array( 
  35. 'street' => isset( $args['contact_street'] ) ?SI_Estimate_Submissions::esc__( $args['contact_street']) : '',  
  36. 'city' => isset( $args['contact_city'] ) ? SI_Estimate_Submissions::esc__($args['contact_city']) : '',  
  37. 'zone' => isset( $args['contact_zone'] ) ? SI_Estimate_Submissions::esc__($args['contact_zone']) : '',  
  38. 'postal_code' => isset( $args['contact_postal_code'] ) ? SI_Estimate_Submissions::esc__($args['contact_postal_code']) : '',  
  39. 'country' => isset( $args['contact_country'] ) ? SI_Estimate_Submissions::esc__($args['contact_country']) : '',  
  40. ); 
  41.  
  42. $args = array( 
  43. 'company_name' => isset( $args['client_name'] ) ? SI_Estimate_Submissions::esc__($args['client_name']) : '',  
  44. 'website' => isset( $args['website'] ) ? SI_Estimate_Submissions::esc__($args['website']) : '',  
  45. 'address' => $address,  
  46. 'user_id' => $user_id 
  47. ); 
  48.  
  49. $client_id = SI_Client::new_client( $args ); 
  50. // History 
  51. if( null !== $estimate ) { 
  52. do_action( 'si_new_record',  
  53. sprintf( 'Client Created & Assigned: %s', get_the_title( $client_id ) ),  
  54. SI_Estimate_Submissions::SUBMISSION_UPDATE,  
  55. $estimate->get_id(),  
  56. sprintf( 'Client Created & Assigned: %s', get_the_title( $client_id ) ),  
  57. 0,  
  58. FALSE ); 
  59.  
  60. // Set the estimates client 
  61. if( null !== $estimate ) { 
  62. $estimate->set_client_id( $client_id );