MP_Addon_Letter_Callback::json_send_letter()

Send letter to lead from the lead page.

Contents


Source Source

File: calculator-letter-add-on/inc/MP_Addon_Letter_Callback.php

	public function json_send_letter() {

		MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_json-request' );

		$post_id     = $_POST['leadId'];
		$description = $_POST['leadDescription'];
		$template    = $_POST['leadTemplate'];

		$firstname  = get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_first_name' ) ? get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_first_name', true ) : '';
		$secondname = get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_second_name' ) ? get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_second_name', true ) : '';
		$name       = $firstname . ' ' . $secondname;

		$email = get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_email' ) ? get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_email', true ) : null;
		$phone = get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_phone' ) ? get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_phone', true ) : null;

		$lead_address = get_post_meta( $post_id, MortgagePlatform::$prefix . 'address' ) ? get_post_meta( $post_id, MortgagePlatform::$prefix . 'address', true ) : null;
		$address      = $this->is_not_empty( $lead_address['address'] );
		$city         = $this->is_not_empty( $lead_address['city'] );
		$state        = $this->is_not_empty( $lead_address['state'] );
		$zip          = $this->is_not_empty( $lead_address['zip'] );
		$country      = $this->is_not_empty( $lead_address['country'] );

		if ( $address && $city ) {
			$to = array(
				'name'            => esc_attr( $name ),
				'address_line1'   => esc_attr( $address ),
				'address_line2'   => '',
				'address_city'    => esc_attr( $city ),
				'address_state'   => esc_attr( $state ),
				'address_zip'     => esc_attr( $zip ),
				'address_country' => esc_attr( $country ),
				'email'           => esc_attr( $email ),
				'phone'           => esc_attr( $phone ),
			);

			$letter   = new MP_Addon_Letters();
			$response = $letter->send_letter( $to, $description, $template );

			if ( $response ) {
				echo esc_html__( 'Letter send successfully', 'CalculatorsLetterAddon' );

				$lead_log['lead_letter_manually'] = current_time( 'Y-m-d H:i:s' ) . ' : Success';

				add_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_log', wp_slash( $lead_log ), '' );
			}

			wp_die();

		}

		echo esc_html__( 'Error', 'CalculatorsLetterAddon' );

		$lead_log['lead_letter_manually'] = current_time( 'Y-m-d H:i:s' ) . ' : Error';

		add_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_log', wp_slash( $lead_log ), '' );

		wp_die();
	}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.