/** * Hooked into the estimate submission form. Create a client * if one already doesn't exist. * @param SI_Estimate $estimate * @param array $parsed_args * @return */ public static function create_client_from_submission(SI_Estimate $estimate, $parsed_args = array()) { $client_id = isset($_REQUEST['client_id']) && get_post_type($_REQUEST['client_id']) == SI_Client::POST_TYPE ? $_REQUEST['client_id'] : 0; $user_id = get_current_user_id(); // check to see if the user exists by email if (isset($_REQUEST['sa_estimate_email']) && $_REQUEST['sa_estimate_email'] != '') { if ($user = get_user_by('email', $_REQUEST['sa_estimate_email'])) { $user_id = $user->ID; } } // Check to see if the user is assigned to a client already if (!$client_id) { $client_ids = SI_Client::get_clients_by_user($user_id); if (!empty($client_ids)) { $client_id = array_pop($client_ids); } } // Create a user for the submission if an email is provided. if (!$user_id) { // email is critical if (isset($_REQUEST['sa_estimate_email']) && $_REQUEST['sa_estimate_email'] != '') { $user_args = array('user_login' => esc_html($_REQUEST['sa_estimate_email'], 'sprout-invoices'), 'display_name' => isset($_REQUEST['sa_estimate_client_name']) ? esc_html($_REQUEST['sa_estimate_client_name'], 'sprout-invoices') : esc_html($_REQUEST['sa_estimate_email'], 'sprout-invoices'), 'user_pass' => wp_generate_password(), 'user_email' => isset($_REQUEST['sa_estimate_email']) ? esc_html($_REQUEST['sa_estimate_email'], 'sprout-invoices') : '', 'first_name' => si_split_full_name(esc_html($_REQUEST['sa_estimate_name'], 'sprout-invoices'), 'first'), 'last_name' => si_split_full_name(esc_html($_REQUEST['sa_estimate_name'], 'sprout-invoices'), 'last'), 'user_url' => isset($_REQUEST['sa_estimate_website']) ? esc_html($_REQUEST['sa_estimate_website'], 'sprout-invoices') : ''); $user_id = self::create_user($user_args); } } // create the client based on what's submitted. if (!$client_id) { $address = array('street' => isset($_REQUEST['sa_contact_street']) ? esc_html($_REQUEST['sa_contact_street'], 'sprout-invoices') : '', 'city' => isset($_REQUEST['sa_contact_city']) ? esc_html($_REQUEST['sa_contact_city'], 'sprout-invoices') : '', 'zone' => isset($_REQUEST['sa_contact_zone']) ? esc_html($_REQUEST['sa_contact_zone'], 'sprout-invoices') : '', 'postal_code' => isset($_REQUEST['sa_contact_postal_code']) ? esc_html($_REQUEST['sa_contact_postal_code'], 'sprout-invoices') : '', 'country' => isset($_REQUEST['sa_contact_country']) ? esc_html($_REQUEST['sa_contact_country'], 'sprout-invoices') : ''); $args = array('company_name' => isset($_REQUEST['sa_estimate_client_name']) ? esc_html($_REQUEST['sa_estimate_client_name'], 'sprout-invoices') : '', 'website' => isset($_REQUEST['sa_estimate_website']) ? esc_html($_REQUEST['sa_estimate_website'], 'sprout-invoices') : '', 'address' => $address, 'user_id' => $user_id); $client_id = SI_Client::new_client($args); } // Set the estimates client $estimate->set_client_id($client_id); }
/** * Maybe create a client from submission * @param SI_Estimate $estimate * @param array $args * email - required * * client_id - if client_id is passed than just assign estimate * * client_name - required * * full_name - * * website * * contact_street * * contact_city * * contact_zone * * contact_postal_code * * contact_country * */ public static function maybe_create_client(SI_Estimate $estimate, $args = array()) { $args = apply_filters('si_afi_maybe_create_client', $args); $client_id = isset($args['client_id']) && get_post_type($args['client_id']) == SI_Client::POST_TYPE ? $args['client_id'] : 0; $user_id = get_current_user_id(); // check to see if the user exists by email if (isset($args['email']) && $args['email'] != '') { if ($user = get_user_by('email', $args['email'])) { $user_id = $user->ID; } } // Check to see if the user is assigned to a client already if (!$client_id) { $client_ids = SI_Client::get_clients_by_user($user_id); if (!empty($client_ids)) { $client_id = array_pop($client_ids); } } // Create a user for the submission if an email is provided. if (!$user_id) { // email is critical if (isset($args['email']) && $args['email'] != '') { $user_args = array('user_login' => self::esc__($args['email']), 'display_name' => isset($args['client_name']) ? self::esc__($args['client_name']) : self::esc__($args['email']), 'user_pass' => wp_generate_password(), 'user_email' => isset($args['email']) ? self::esc__($args['email']) : '', 'first_name' => si_split_full_name(self::esc__($args['full_name']), 'first'), 'last_name' => si_split_full_name(self::esc__($args['full_name']), 'last'), 'user_url' => isset($args['website']) ? self::esc__($args['website']) : ''); $user_id = SI_Clients::create_user($user_args); } } // create the client based on what's submitted. if (!$client_id) { $address = array('street' => isset($args['contact_street']) ? self::esc__($args['contact_street']) : '', 'city' => isset($args['contact_city']) ? self::esc__($args['contact_city']) : '', 'zone' => isset($args['contact_zone']) ? self::esc__($args['contact_zone']) : '', 'postal_code' => isset($args['contact_postal_code']) ? self::esc__($args['contact_postal_code']) : '', 'country' => isset($args['contact_country']) ? self::esc__($args['contact_country']) : ''); $args = array('company_name' => isset($args['client_name']) ? self::esc__($args['client_name']) : '', 'website' => isset($args['website']) ? self::esc__($args['website']) : '', 'address' => $address, 'user_id' => $user_id); $client_id = SI_Client::new_client($args); // History do_action('si_new_record', sprintf('Client Created & Assigned: %s', get_the_title($client_id)), self::SUBMISSION_UPDATE, $estimate->get_id(), sprintf('Client Created & Assigned: %s', get_the_title($client_id)), 0, false); } // Set the estimates client $estimate->set_client_id($client_id); }