MP_Addon_CalculatorsCallRail_Webhook

Class MP_Addon_CalculatorsCallRail_Webhook.


Source Source

File: calculator-callrail-add-on/inc/MP_Addon_CalculatorsCallRail_Webhook.php

class MP_Addon_CalculatorsCallRail_Webhook {


	/**
	 * Get webhook from callrail.
	 * @access public
	 * @since  1.0.0
	 */
	public function get_webhook() {

		$array = array();
		if ( isset( $_SERVER['CONTENT_TYPE'] ) ) {
			if ( $_SERVER['CONTENT_TYPE'] == 'application/json' ) {
				$array = json_decode( json_encode( json_decode( file_get_contents( 'php://input' ), true ) ), 1 );
			} else {
				$array = array_merge( $_POST, $_GET );
			}
		} else {
			$array = array_merge( $_POST, $_GET );
		}

		$array = $this->sanitize_callrail( $array );
		$this->set_callrail_data( $array );
	}


	/**
	 * Sanitize callrail data.
	 * @access private
	 *
	 * @param array $array callrail data
	 *
	 * @return array sanitize array
	 * @since  1.0.0
	 */
	private function sanitize_callrail( $array ) {

		if ( is_array( $array ) ) {
			return array_map( array( $this, 'sanitize_callrail' ), $array );
		} else {
			return is_scalar( $array ) ? sanitize_text_field( $array ) : $array;
		}
	}


	/**
	 * Chech callrail data with exists leads.
	 * If leads exists to update leads, or add new callrail post type.
	 * @access protected
	 *
	 * @param array $data callrail data
	 *
	 * @since  1.0.0
	 */
	protected function set_callrail_data( $data ) {

		if ( isset( $data['resource_id'] ) && $data['resource_id'] ) {

			$callrail_data = $this->prepare_callrail_data( $data );

			$call_data = $this->prepare_call_data( $data );

			$callrail_post = $this->set_callrail( $callrail_data, $call_data );

			$lead = array(
				'name'  => $callrail_data['formatted_customer_name'],
				'phone' => $callrail_data['customer_phone_number'],
			);

			$posts = $this->get_lead( $lead );

			if ( ! empty( $posts ) ) {
				$this->update_callrail_metadata( $posts, $callrail_post );
			}
		}

	}


	/**
	 * Prepare callrail data for safe.
	 * @access protected
	 *
	 * @param array $data {
	 *                    Call rail from webhook.
	 *
	 *  @type string "answered",
	 *  @type string "business_phone_number",
	 *  @type string "call_type",
	 *  @type string "company_id",
	 *  @type string "company_name",
	 *  @type string "company_time_zone",
	 *  @type string "created_at",
	 *  @type string "customer_city",
	 *  @type string "customer_country",
	 *  @type string "customer_name",
	 *  @type string "customer_phone_number",
	 *  @type string "customer_state",
	 *  @type string "device_type",
	 *  @type string "direction",
	 *  @type string "duration",
	 *  @type bool "first_call",
	 *  @type string "formatted_call_type",
	 *  @type string "formatted_customer_location",
	 *  @type string "formatted_business_phone_number",
	 *  @type string "formatted_customer_name",
	 *  @type int "prior_calls",
	 *  @type string "formatted_customer_name_or_phone_number",
	 *  @type string "formatted_customer_phone_number",
	 *  @type string "formatted_duration",
	 *  @type string "formatted_tracking_phone_number",
	 *  @type string "formatted_tracking_source",
	 *  @type string "formatted_value",
	 *  @type int "good_lead_call_id",
	 *  @type string "good_lead_call_time",
	 *  @type int "id",
	 *  @type string "lead_status",
	 *  @type string "note",
	 *  @type string "recording",
	 *  @type int "recording_duration",
	 *  @type string "source",
	 *  @type string "source_name",
	 *  @type string "start_time",
	 *  @type array "tags",
	 *  @type int "total_calls",
	 *  @type string "tracking_phone_number",
	 *  @type string "transcription",
	 *  @type string "value": "",
	 *  @type bool "voicemail",
	 *  @type array "waveforms",
	 *  @type int "tracker_id",
	 *  @type string "keywords": "",
	 *  @type string "medium",
	 *  @type string "referring_url",
	 *  @type string "landing_page_url",
	 *  @type string "last_requested_url",
	 *  @type string "referrer_domain",
	 *  @type array "conversational_transcript",
	 *  @type string "utm_source",
	 *  @type string "utm_medium",
	 *  @type string "utm_term",
	 *  @type string "utm_content",
	 *  @type string "utm_campaign",
	 *  @type string "utma",
	 *  @type string "utmb",
	 *  @type string "utmc",
	 *  @type string "utmv",
	 *  @type string "utmz",
	 *  @type string "ga",
	 *  @type string "fbclid",
	 *  @type string "gclid",
	 *  @type array "integration_data",
	 *  @type string "keywords_spotted",
	 *  @type string "recording_player",
	 *  @type array "speaker_percent",
	 *  @type array "call_highlights",
	 *  @type string "agent_email",
	 *  @type string "campaign",
	 *  @type string "msclkid",
	 *  @type string "keypad_entries",
	 *  @type string "recording_redirect",
	 *  @type string "callercity",
	 *  @type string "callercountry",
	 *  @type string "callername",
	 *  @type string "callernum",
	 *  @type string "callerstate",
	 *  @type string "callsource",
	 *  @type array "custom",
	 *  @type string "datetime",
	 *  @type string "destinationnum",
	 *  @type string "ip",
	 *  @type string "kissmetrics_id",
	 *  @type string "landingpage",
	 *  @type string "referrer",
	 *  @type string "referrermedium",
	 *  @type int "score",
	 *  @type string "tag": "",
	 *  @type string "trackingnum",
	 *  @type string "timestamp",
	 *  @type int "person_id",
	 *  @type string "person_resource_id",
	 *  @type string "resource_id",
	 *  @type string "company_resource_id",
	 *  @type string "tracker_resource_id"
	 *  }
	 * @return array 'prepare call rail data' {
	 *     Call rail data.
	 *  @type string 'company_name'
	 *  @type string 'business_phone_number'
	 *  @type string 'formatted_customer_name'
	 *  @type string 'customer_phone_number'
	 *  @type string 'customer_country'
	 *  @type string 'formatted_customer_location'
	 *  @type string 'ip'
	 *  @type string 'lead_status'
	 *  @type string 'total_calls'
	 *  @type string 'tracking_phone_number'
	 *  @type string 'tracker_id'
	 *  @type string 'person_id'
	 *  @type string 'campaign'
	 *  @type string 'utm_source'
	 *  @type string 'utm_medium'
	 *  @type string 'utm_term'
	 *  @type string 'utm_content'
	 *  @type string 'utm_campaign'
	 *  @type string 'msclkid'
	 *  @type string 'fbclid'
	 *  @type string 'gclid'
	 *  }
	 * @since  1.0.0
	 */
	protected function prepare_callrail_data( $data ) {

		$callrail_data = array(
			//general
			'company_name'                => isset( $data['company_name'] ) ? $data['company_name'] : '',
			'business_phone_number'       => isset( $data['business_phone_number'] ) ? $data['business_phone_number'] : '',
			//lead
			'formatted_customer_name'     => isset( $data['formatted_customer_name'] ) ? $data['formatted_customer_name'] : '',
			'customer_phone_number'       => isset( $data['customer_phone_number'] ) ? $data['customer_phone_number'] : '',
			'customer_country'            => isset( $data['customer_country'] ) ? $data['customer_country'] : '',
			'formatted_customer_location' => isset( $data['formatted_customer_location'] ) ? $data['formatted_customer_location'] : '',
			'ip'                          => isset( $data['ip'] ) ? $data['ip'] : '',
			'lead_status'                 => isset( $data['lead_status'] ) ? $data['lead_status'] : '',
			'total_calls'                 => isset( $data['total_calls'] ) ? $data['total_calls'] : '',
			'tracking_phone_number'       => isset( $data['tracking_phone_number'] ) ? $data['tracking_phone_number'] : '',
			'tracker_id'                  => isset( $data['tracker_id'] ) ? $data['tracker_id'] : '',
			'person_id'                   => isset( $data['person_id'] ) ? $data['person_id'] : '',
			//utm
			'campaign'                    => isset( $data['campaign'] ) ? $data['campaign'] : '',
			'utm_source'                  => isset( $data['utm_source'] ) ? $data['utm_source'] : '',
			'utm_medium'                  => isset( $data['utm_medium'] ) ? $data['utm_medium'] : '',
			'utm_term'                    => isset( $data['utm_term'] ) ? $data['utm_term'] : '',
			'utm_content'                 => isset( $data['utm_content'] ) ? $data['utm_content'] : '',
			'utm_campaign'                => isset( $data['utm_campaign'] ) ? $data['utm_campaign'] : '',
			'msclkid'                     => isset( $data['msclkid'] ) ? $data['msclkid'] : '',
			'fbclid'                      => isset( $data['fbclid'] ) ? $data['fbclid'] : '',
			'gclid'                       => isset( $data['gclid'] ) ? $data['gclid'] : '',
		);

		return $callrail_data;
	}


	/**
	 * Prepare call data.
	 * @access protected
	 *
	 * @param array $data call rail data {
	 *                    Call rail from webhook.
	 *
	 *  @type string "answered",
	 *  @type string "business_phone_number",
	 *  @type string "call_type",
	 *  @type string "company_id",
	 *  @type string "company_name",
	 *  @type string "company_time_zone",
	 *  @type string "created_at",
	 *  @type string "customer_city",
	 *  @type string "customer_country",
	 *  @type string "customer_name",
	 *  @type string "customer_phone_number",
	 *  @type string "customer_state",
	 *  @type string "device_type",
	 *  @type string "direction",
	 *  @type string "duration",
	 *  @type bool "first_call",
	 *  @type string "formatted_call_type",
	 *  @type string "formatted_customer_location",
	 *  @type string "formatted_business_phone_number",
	 *  @type string "formatted_customer_name",
	 *  @type int "prior_calls",
	 *  @type string "formatted_customer_name_or_phone_number",
	 *  @type string "formatted_customer_phone_number",
	 *  @type string "formatted_duration",
	 *  @type string "formatted_tracking_phone_number",
	 *  @type string "formatted_tracking_source",
	 *  @type string "formatted_value",
	 *  @type int "good_lead_call_id",
	 *  @type string "good_lead_call_time",
	 *  @type int "id",
	 *  @type string "lead_status",
	 *  @type string "note",
	 *  @type string "recording",
	 *  @type int "recording_duration",
	 *  @type string "source",
	 *  @type string "source_name",
	 *  @type string "start_time",
	 *  @type array "tags",
	 *  @type int "total_calls",
	 *  @type string "tracking_phone_number",
	 *  @type string "transcription",
	 *  @type string "value": "",
	 *  @type bool "voicemail",
	 *  @type array "waveforms",
	 *  @type int "tracker_id",
	 *  @type string "keywords": "",
	 *  @type string "medium",
	 *  @type string "referring_url",
	 *  @type string "landing_page_url",
	 *  @type string "last_requested_url",
	 *  @type string "referrer_domain",
	 *  @type array "conversational_transcript",
	 *  @type string "utm_source",
	 *  @type string "utm_medium",
	 *  @type string "utm_term",
	 *  @type string "utm_content",
	 *  @type string "utm_campaign",
	 *  @type string "utma",
	 *  @type string "utmb",
	 *  @type string "utmc",
	 *  @type string "utmv",
	 *  @type string "utmz",
	 *  @type string "ga",
	 *  @type string "fbclid",
	 *  @type string "gclid",
	 *  @type array "integration_data",
	 *  @type string "keywords_spotted",
	 *  @type string "recording_player",
	 *  @type array "speaker_percent",
	 *  @type array "call_highlights",
	 *  @type string "agent_email",
	 *  @type string "campaign",
	 *  @type string "msclkid",
	 *  @type string "keypad_entries",
	 *  @type string "recording_redirect",
	 *  @type string "callercity",
	 *  @type string "callercountry",
	 *  @type string "callername",
	 *  @type string "callernum",
	 *  @type string "callerstate",
	 *  @type string "callsource",
	 *  @type array "custom",
	 *  @type string "datetime",
	 *  @type string "destinationnum",
	 *  @type string "ip",
	 *  @type string "kissmetrics_id",
	 *  @type string "landingpage",
	 *  @type string "referrer",
	 *  @type string "referrermedium",
	 *  @type int "score",
	 *  @type string "tag": "",
	 *  @type string "trackingnum",
	 *  @type string "timestamp",
	 *  @type int "person_id",
	 *  @type string "person_resource_id",
	 *  @type string "resource_id",
	 *  @type string "company_resource_id",
	 *  @type string "tracker_resource_id"
	 *  }
	 *  @return array $call {
	 *      Call data.
	 *  @type string 'start_time'
	 *  @type string 'customer_phone_number'
	 *  @type string 'formatted_call_type'
	 *  @type string 'recording'
	 *  @type string 'formatted_duration'
	 *  @type string 'conversational_transcript'
	 *  @type string 'agent_email'
	 *  @type string 'device_type'
	 *  @type string 'call_highlights'
	 *  @type string 'resource_id'
	 *  @type string 'id'
	 *  @type string 'note'
	 *  @type string 'formatted_tracking_source'
	 *  @type string 'source_name'
	 *  @type string 'tags'
	 *  @type string 'keywords'
	 *  @type string 'referring_url'
	 *  @type string 'landing_page_url'
	 *  @type string 'last_requested_url'
	 *  @type string 'referrer_domain'
	 *  @type string 'keywords_spotted'
	 *  @type string 'keypad_entries'
	 *  }
	 * @since  1.0.0
	 */
	protected function prepare_call_data( $data ) {

		$call = array(
			'start_time'                => isset( $data['start_time'] ) ? $data['start_time'] : '',
			'customer_phone_number'     => isset( $data['customer_phone_number'] ) ? $data['customer_phone_number'] : '',
			'formatted_call_type'       => isset( $data['formatted_call_type'] ) ? $data['formatted_call_type'] : '',
			'recording'                 => isset( $data['recording'] ) ? $data['recording'] : '',
			'formatted_duration'        => isset( $data['formatted_duration'] ) ? $data['formatted_duration'] : '',
			'conversational_transcript' => isset( $data['conversational_transcript'] ) ? $data['conversational_transcript'] : '',
			'agent_email'               => isset( $data['agent_email'] ) ? $data['agent_email'] : '',
			'device_type'               => isset( $data['device_type'] ) ? $data['device_type'] : '',
			'call_highlights'           => isset( $data['call_highlights'] ) ? $data['call_highlights'] : '',
			'resource_id'               => isset( $data['resource_id'] ) ? $data['resource_id'] : '',
			'id'                        => isset( $data['id'] ) ? $data['id'] : '',
			'note'                      => isset( $data['note'] ) ? $data['note'] : '',
			'formatted_tracking_source' => isset( $data['formatted_tracking_source'] ) ? $data['formatted_tracking_source'] : '',
			'source_name'               => isset( $data['source_name'] ) ? $data['source_name'] : '',
			'tags'                      => isset( $data['tags'] ) ? $data['tags'] : '',
			'keywords'                  => isset( $data['keywords'] ) ? $data['keywords'] : '',
			'referring_url'             => isset( $data['referring_url'] ) ? $data['referring_url'] : '',
			'landing_page_url'          => isset( $data['landing_page_url'] ) ? $data['landing_page_url'] : '',
			'last_requested_url'        => isset( $data['last_requested_url'] ) ? $data['last_requested_url'] : '',
			'referrer_domain'           => isset( $data['referrer_domain'] ) ? $data['referrer_domain'] : '',
			'keywords_spotted'          => isset( $data['keywords_spotted'] ) ? $data['keywords_spotted'] : '',
			'keypad_entries'            => isset( $data['keypad_entries'] ) ? $data['keypad_entries'] : '',
		);

		return $call;
	}


	/**
	 * Get lead.
	 * @access protected
	 * @sincec 1.0.0
	 *
	 * @param array $lead {
	 *                    Lead.
	 *
	 *  @type string 'name' lead name
	 *  @type string 'phone' lead phone
	 * }
	 * @return array Lead IDs
	 */
	protected function get_lead( $lead ) {

		$phone = 0 === stripos( $lead['phone'], '+' ) ? $lead['phone'] : '+' . $lead['phone'];

		$args = array(
			'posts_per_page' => - 1,
			'post_type'      => array( MortgagePlatform::$lead, MortgagePlatform::$abandoned_lead ),
			'meta_query'     => array(
				'relation' => 'AND',
				array(
					'key'   => MortgagePlatform::$prefix . 'lead_phone',
					'value' => $phone,
				),
			),
		);

		$leads = new \WP_Query( $args );


		$posts = array();
		if ( $leads->have_posts() ) {
			while ( $leads->have_posts() ) {
				$leads->the_post();

				array_push( $posts, $leads->post->ID );
			}
		}

		wp_reset_postdata();

		return $posts;

	}

	/**
	 * Set callrail.
	 * @access protected
	 *
	 * @param array  $data    {
	 *                        Call rail data.
	 *
	 *  @type string 'company_name'
	 *  @type string 'business_phone_number'
	 *  @type string 'formatted_customer_name'
	 *  @type string 'customer_phone_number'
	 *  @type string 'customer_country'
	 *  @type string 'formatted_customer_location'
	 *  @type string 'ip'
	 *  @type string 'lead_status'
	 *  @type string 'total_calls'
	 *  @type string 'tracking_phone_number'
	 *  @type string 'tracker_id'
	 *  @type string 'person_id'
	 *  @type string 'campaign'
	 *  @type string 'utm_source'
	 *  @type string 'utm_medium'
	 *  @type string 'utm_term'
	 *  @type string 'utm_content'
	 *  @type string 'utm_campaign'
	 *  @type string 'msclkid'
	 *  @type string 'fbclid'
	 *  @type string 'gclid'
	 *  }
	 *
	 * @param array  $call    {
	 *                        Call data.
	 *
	 *  @type string 'start_time'
	 *  @type string 'customer_phone_number'
	 *  @type string 'formatted_call_type'
	 *  @type string 'recording'
	 *  @type string 'formatted_duration'
	 *  @type string 'conversational_transcript'
	 *  @type string 'agent_email'
	 *  @type string 'device_type'
	 *  @type string 'call_highlights'
	 *  @type string 'resource_id'
	 *  @type string 'id'
	 *  @type string 'note'
	 *  @type string 'formatted_tracking_source'
	 *  @type string 'source_name'
	 *  @type string 'tags'
	 *  @type string 'keywords'
	 *  @type string 'referring_url'
	 *  @type string 'landing_page_url'
	 *  @type string 'last_requested_url'
	 *  @type string 'referrer_domain'
	 *  @type string 'keywords_spotted'
	 *  @type string 'keypad_entries'
	 * }
	 *
	 * @param string $post_id Lead ID
	 *
	 * @return array|string $post callrail post IDs
	 * @since  1.0.0
	 */
	protected function set_callrail( $data, $call, $post_id = '' ) {

		$callrail = $this->get_callrail( $data );

		if ( ! empty( $callrail ) ) {
			foreach ( $callrail as $callrail_id ) {
				$post[] = $this->update_callrail( $callrail_id, $data, $call, $post_id );
			}
		} else {
			$post = $this->add_callrail( $data, $call, $post_id );
		}

		return $post;

	}

	/**
	 * Add callrail post.
	 * @access protected
	 *
	 * @param array  $data {
	 *                     Call Rail data.
	 *
	 *  @type string 'company_name'
	 *  @type string 'business_phone_number'
	 *  @type string 'formatted_customer_name'
	 *  @type string 'customer_phone_number'
	 *  @type string 'customer_country'
	 *  @type string 'formatted_customer_location'
	 *  @type string 'ip'
	 *  @type string 'lead_status'
	 *  @type string 'total_calls'
	 *  @type string 'tracking_phone_number'
	 *  @type string 'tracker_id'
	 *  @type string 'person_id'
	 *  @type string 'campaign'
	 *  @type string 'utm_source'
	 *  @type string 'utm_medium'
	 *  @type string 'utm_term'
	 *  @type string 'utm_content'
	 *  @type string 'utm_campaign'
	 *  @type string 'msclkid'
	 *  @type string 'fbclid'
	 *  @type string 'gclid'
	 * }
	 *
	 * @param array  $call {
	 *                     Call data.
	 *
	 *  @type string 'start_time'
	 *  @type string 'customer_phone_number'
	 *  @type string 'formatted_call_type'
	 *  @type string 'recording'
	 *  @type string 'formatted_duration'
	 *  @type string 'conversational_transcript'
	 *  @type string 'agent_email'
	 *  @type string 'device_type'
	 *  @type string 'call_highlights'
	 *  @type string 'resource_id'
	 *  @type string 'id'
	 *  @type string 'note'
	 *  @type string 'formatted_tracking_source'
	 *  @type string 'source_name'
	 *  @type string 'tags'
	 *  @type string 'keywords'
	 *  @type string 'referring_url'
	 *  @type string 'landing_page_url'
	 *  @type string 'last_requested_url'
	 *  @type string 'referrer_domain'
	 *  @type string 'keywords_spotted'
	 *  @type string 'keypad_entries'
	 * }
	 *
	 * @param string $lead Lead ID
	 *
	 * @return int|bool The post ID on success. The value 0 or false on failure.
	 * @since  1.0.0
	 */
	protected function add_callrail( $data, $call, $lead = '' ) {

		$post_data = array(
			'post_title'  => $data['formatted_customer_name'],
			'post_type'   => MP_Addon_CalculatorsCallRail::$callrail,
			'post_status' => 'publish',
		);

		$post_id = wp_insert_post( wp_slash( $post_data ) );

		if ( $post_id ) {
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail', $data );
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_calls', $call, true );
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_person_id', $data['person_id'] );
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_customer_phone', $data['customer_phone_number'] );
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_total_calls', $data['total_calls'] );
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_location', $data['formatted_customer_location'] );
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_tracking_phone', $data['tracking_phone_number'] );
		}

		return $post_id;
	}

	/**
	 * Update callrail post.
	 * @access protected
	 *
	 * @param int    $callrail_id Callrail post ID
	 * @param array  $data        {
	 *                            Call Rail data.
	 *
	 *  @type string 'company_name'
	 *  @type string 'business_phone_number'
	 *  @type string 'formatted_customer_name'
	 *  @type string 'customer_phone_number'
	 *  @type string 'customer_country'
	 *  @type string 'formatted_customer_location'
	 *  @type string 'ip'
	 *  @type string 'lead_status'
	 *  @type string 'total_calls'
	 *  @type string 'tracking_phone_number'
	 *  @type string 'tracker_id'
	 *  @type string 'person_id'
	 *  @type string 'campaign'
	 *  @type string 'utm_source'
	 *  @type string 'utm_medium'
	 *  @type string 'utm_term'
	 *  @type string 'utm_content'
	 *  @type string 'utm_campaign'
	 *  @type string 'msclkid'
	 *  @type string 'fbclid'
	 *  @type string 'gclid'
	 * }
	 *
	 * @param array  $call        {
	 *                            Call data.
	 *
	 *  @type string 'start_time'
	 *  @type string 'customer_phone_number'
	 *  @type string 'formatted_call_type'
	 *  @type string 'recording'
	 *  @type string 'formatted_duration'
	 *  @type string 'conversational_transcript'
	 *  @type string 'agent_email'
	 *  @type string 'device_type'
	 *  @type string 'call_highlights'
	 *  @type string 'resource_id'
	 *  @type string 'id'
	 *  @type string 'note'
	 *  @type string 'formatted_tracking_source'
	 *  @type string 'source_name'
	 *  @type string 'tags'
	 *  @type string 'keywords'
	 *  @type string 'referring_url'
	 *  @type string 'landing_page_url'
	 *  @type string 'last_requested_url'
	 *  @type string 'referrer_domain'
	 *  @type string 'keywords_spotted'
	 *  @type string 'keypad_entries'
	 * }
	 *
	 * @param string $lead        Lead ID
	 *
	 * @return int|bool The post ID on success. The value 0 or false on failure.
	 * @since  1.0.0
	 */
	protected function update_callrail( $callrail_id, $data, $call, $lead ) {

		$post_data = array(
			'ID'          => $callrail_id,
			'post_type'   => MP_Addon_CalculatorsCallRail::$callrail,
			'post_title'  => $data['formatted_customer_name'],
			'post_status' => 'publish',
		);

		$post_id = wp_update_post( wp_slash( $post_data ) );

		if ( $post_id ) {
			update_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail', $data );
			update_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_person_id', $data['person_id'] );
			update_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_customer_phone', $data['customer_phone_number'] );
			update_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_total_calls', $data['total_calls'] );
			update_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_location', $data['formatted_customer_location'] );
			update_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_tracking_phone', $data['tracking_phone_number'] );

			$this->add_calls( $post_id, $call );
		}

		return $post_id;
	}


	/**
	 * Add call to calls metabox.
	 * @access protected
	 *
	 * @param int   $post_id  callrail post ID
	 * @param array $new_call {
	 *                        Call data.
	 *
	 *  @type string 'start_time'
	 *  @type string 'customer_phone_number'
	 *  @type string 'formatted_call_type'
	 *  @type string 'recording'
	 *  @type string 'formatted_duration'
	 *  @type string 'conversational_transcript'
	 *  @type string 'agent_email'
	 *  @type string 'device_type'
	 *  @type string 'call_highlights'
	 *  @type string 'resource_id'
	 *  @type string 'id'
	 *  @type string 'note'
	 *  @type string 'formatted_tracking_source'
	 *  @type string 'source_name'
	 *  @type string 'tags'
	 *  @type string 'keywords'
	 *  @type string 'referring_url'
	 *  @type string 'landing_page_url'
	 *  @type string 'last_requested_url'
	 *  @type string 'referrer_domain'
	 *  @type string 'keywords_spotted'
	 *  @type string 'keypad_entries'
	 * }
	 * @since  1.0.0
	 */
	protected function add_calls( $post_id, $new_call ) {

		$calls = get_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_calls', false );
		$id    = false;

		foreach ( $calls as $call ) {
			if ( $call['resource_id'] === $new_call['resource_id'] ) {
				update_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_calls', $new_call, $call );
				$id = true;
			}
		}
		if ( ! $id ) {
			add_post_meta( $post_id, MortgagePlatform::$prefix . 'callrail_calls', $new_call, false );
		}
	}


	/**
	 * Get callrail posts.
	 * @access protected
	 *
	 * @param array $data  {
	 *                     Call Rail data.
	 *
	 *  @type string 'company_name'
	 *  @type string 'business_phone_number'
	 *  @type string 'formatted_customer_name'
	 *  @type string 'customer_phone_number'
	 *  @type string 'customer_country'
	 *  @type string 'formatted_customer_location'
	 *  @type string 'ip'
	 *  @type string 'lead_status'
	 *  @type string 'total_calls'
	 *  @type string 'tracking_phone_number'
	 *  @type string 'tracker_id'
	 *  @type string 'person_id'
	 *  @type string 'campaign'
	 *  @type string 'utm_source'
	 *  @type string 'utm_medium'
	 *  @type string 'utm_term'
	 *  @type string 'utm_content'
	 *  @type string 'utm_campaign'
	 *  @type string 'msclkid'
	 *  @type string 'fbclid'
	 *  @type string 'gclid'
	 * }
	 * @return array $posts Callrail posts
	 * @since  1.0.0
	 */
	protected function get_callrail( $data ) {

		$phone = $data['customer_phone_number'];

		$args = array(
			'posts_per_page' => - 1,
			'post_type'      => MP_Addon_CalculatorsCallRail::$callrail,
			'meta_query'     => array(
				'relation' => 'AND',
				array(
					'key'   => MortgagePlatform::$prefix . 'callrail_person_id',
					'value' => $data['person_id'],
				),
			),
		);

		$callrail = new \WP_Query( $args );


		$posts = array();

		if ( $callrail->have_posts() ) {
			while ( $callrail->have_posts() ) {
				$callrail->the_post();

				$customer_phone_number = get_post_meta( $callrail->post->ID, MortgagePlatform::$prefix . 'callrail', true )['customer_phone_number'];
				if ( $phone == $customer_phone_number ) {
					array_push( $posts, $callrail->post->ID );
				}
			}
		}

		wp_reset_postdata();

		return $posts;
	}


	/**
	 * Update callrail metadata.
	 * @access protected
	 *
	 * @param array        $posts         Lead Post Id
	 * @param string|array $callrail_post callrails Post ID
	 *
	 * @since  1.0.0
	 */
	protected function update_callrail_metadata( $posts, $callrail_post ) {
		foreach ( $posts as $post_id ) {
			update_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_callrail', $callrail_post );

			foreach ( $callrail_post as $call_post ) {
				update_post_meta( $call_post, MortgagePlatform::$prefix . 'callrail_lead', $post_id );
			}
		}

	}

}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.