MP_Addon_API_Callback

Class MP_Addon_Callback.


Source Source

File: calculator-API-add-on/inc/MP_Addon_API_Callback.php

class MP_Addon_API_Callback {

	/**
	 * Import Lead Data by API.
	 * @since  1.0.0
	 * @access public
	 * @static
	 *
	 * @param int|string $id lead id
	 * @param array $data        {
	 *                           Post parameter.
	 *
	 *  @type array $data         {
	 *  @type array 'lead' {
	 *  @type string 'lead_first_name' first name
	 *  @type string 'lead_second_name' second name
	 *  @type string 'lead_phone' phone
	 *  @type string 'lead_email' email
	 *  @type string 'lead_zipcode' zipcode
	 *  @type string 'lead_yearofbirth' year of birth
	 *  @type string 'lead_investment_amount' investment amount
	 *  }
	 * }
	 * @return \WP_Error
	 */
	public static function post_lead_by_api( $id, $data ) {

		$lead_id = $id;

		if ( ! $lead_id ) {
			wp_send_json_error();
		}

		if ( $data['lead'] ) {
			$lead               = $data['lead'];
			$lead_first_name    = self::sanitize_data( $lead['lead_first_name'] );
			$lead_second_name   = self::sanitize_data( $lead['lead_second_name'] );
			$lead_phone         = self::sanitize_data( $lead['lead_phone'] );
			$lead_email         = self::sanitize_data( $lead['lead_email'] );
			$lead_zipcode       = self::sanitize_data( $lead['lead_zipcode'] );
			$lead_yearofbirth   = self::sanitize_data( $lead['lead_yearofbirth'] );
			$lead_invest_amount = self::sanitize_data( $lead['lead_investment_amount'] );
		}

		$client_name = $lead_first_name . ' ' . $lead_second_name;

		$post_data = self::prepare_post_data( $lead_id, $client_name );

		if ( ! post_data ) {
			return new \WP_Error( 'rest_no_leads', esc_html__( 'No found lead with ID ', 'CalculatorsAPIAddon' ) . $lead_id, array( 'status' => 404 ) );
		}

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

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

		update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_first_name', wp_slash( $lead_first_name ), '' );
		update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_second_name', wp_slash( $lead_second_name ), '' );
		update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_phone', wp_slash( $lead_phone ), '' );
		update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_email', wp_slash( $lead_email ), '' );
		update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_zipcode', wp_slash( $lead_zipcode ), '' );
		update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_yearofbirth', wp_slash( $lead_yearofbirth ), '' );
		update_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_investment_amount', wp_slash( $lead_invest_amount ), '' );

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

		wp_send_json_success();
	}


	/**
	 * Prepare lead data.
	 * @since  1.0.0
	 * @access protected
	 * @static
	 *
	 * @param int|string $lead_id lead id
	 * @param string $client_name First name and second name of the Lead
	 *
	 * @return array|bool
	 */
	protected static function prepare_post_data( $lead_id, $client_name ) {

		$post_data = false;

		if ( 'add' === $lead_id || 'new' === $lead_id ) {

			$post_data = array(
				'post_title'  => wp_strip_all_tags( $client_name ),
				'post_status' => 'pending',
				'post_author' => 1,
				'post_type'   => MortgagePlatform::$lead,
			);

		} elseif ( get_post_type( $lead_id ) === MortgagePlatform::$lead ) {

			$post_data = array(
				'post_title' => wp_strip_all_tags( $client_name ),
				'ID'         => $lead_id,
				'post_type'  => MortgagePlatform::$lead,
			);

		}

		return $post_data;
	}


	/**
	 * Sanitize lead data.
	 *
	 * @since 1.0.0
	 * @access protected
	 * @static
	 *
	 * @param string $data lead data
	 *
	 * @return string
	 */
	protected static function sanitize_data( $data ) {
		if ( ! $data ) {
			return '';
		}
		return sanitize_text_field( $data );
	}


	/**
	 * Get Lead metadata form DataBase by Lead ID.
	 * @since  1.0.0
	 * @access public
	 * @static
	 *
	 * @param int|string $lead_id lead id
	 *
	 * @return array $lead_data {
	 *               Lead data.
	 *  @type string 'lead_id' lead id
	 *  @type array 'lead' {
	 *      @type string 'lead_first_name' first name
	 *      @type string 'lead_second_name' second name
	 *      @type string 'lead_phone' phone
	 *      @type string 'lead_email' email
	 *      @type string 'lead_zipcode' zipcode
	 *      @type string 'lead_yearofbirth' year of birth
	 *      @type string 'lead_investment_amount' investment amount
	 *  }
	 *  @type array 'searches' searches
	 *  @type array 'utm' utm
	 *  @type array 'ip' ip
	 *  }
	 */
	public static function get_lead( $lead_id ) {

		$lead_data             = array();
		$lead_data['lead_id']  = $lead_id;
		$lead_data['lead']     = array();
		$lead_data['searches'] = array();
		$lead_data['utm']      = array();
		$lead_data['ip']       = array();

		if ( get_post_type( $lead_id ) === MortgagePlatform::$lead ) {

			$lead_data['lead']['lead_first_name']        = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_first_name', true );
			$lead_data['lead']['lead_second_name']       = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_second_name', true );
			$lead_data['lead']['lead_phone']             = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_phone', true );
			$lead_data['lead']['lead_email']             = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_email', true );
			$lead_data['lead']['lead_zipcode']           = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_zipcode', true );
			$lead_data['lead']['lead_yearofbirth']       = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_yearofbirth', true );
			$lead_data['lead']['lead_investment_amount'] = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_investment_amount', true );

			$lead_data['searches'] = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead_searches' );
			$lead_data['utm']      = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'utm' )[0];
			$lead_data['ip']       = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'lead' )[0];

		}
		if ( get_post_type( $lead_id ) === MortgagePlatform::$abandoned_lead ) {

			$lead_data['lead']['lead_first_name']        = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_first_name', true );
			$lead_data['lead']['lead_second_name']       = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_second_name', true );
			$lead_data['lead']['lead_phone']             = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_phone', true );
			$lead_data['lead']['lead_email']             = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_email', true );
			$lead_data['lead']['lead_zipcode']           = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_zipcode', true );
			$lead_data['lead']['lead_yearofbirth']       = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_yearofbirth', true );
			$lead_data['lead']['lead_investment_amount'] = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_investment_amount', true );

			$lead_data['searches'] = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead_searches' );
			$lead_data['utm']      = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'utm' )[0];
			$lead_data['ip']       = get_post_meta( $lead_id, MortgagePlatform::$prefix . 'abandoned_lead' )[0];

		}

		return $lead_data;
	}


}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.