MP_Addon_API

Class MP_Addon_API.


Description Description

See also See also


Top ↑

Source Source

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

class MP_Addon_API extends \WP_REST_Controller {


	/**
	 * API key.
	 * @var string API key
	 * @since  1.0.0
	 * @access private
	 */
	private $api_key;


	/**
	 * API constructor.
	 * @since  1.0.0
	 * @access public
	 */
	public function __construct() {

		$this->api_key;

		$this->namespace = MortgagePlatform::$domain . '/v1';
		$this->rest_base = '/apikey/(?P<apikey>.+)/lead/(?P<leadID>.+)';

	}


	/**
	 * Endpoint API.
	 * @since  1.0.0
	 * @access public
	 *
	 * @return bool True on success, false on error.
	 */
	public function api() {
		return register_rest_route(
			$this->namespace,
			$this->rest_base,
			array(
				array(
					'methods'             => 'GET',
					'callback'            => array( $this, 'getLead' ),
					'permission_callback' => array( $this, 'permission' ),
					'args'                => array(
						'leadID' => array(
							'default'           => null,
							'required'          => true,
							'type'              => 'integer',
							'validate_callback' => array( $this, 'validate_lead' ),
							'sanitize_callback' => array( $this, 'sanitize_lead' ),
						),
						'apikey' => array(
							'default'  => null,
							'required' => true,
						),
					),
				),
				array(
					'methods'             => 'POST',
					'callback'            => array( $this, 'postLead' ),
					'permission_callback' => array( $this, 'permission' ),
					'args'                => array(
						'leadID' => array(
							'default'           => 'add',
							'required'          => true,
							'validate_callback' => array( $this, 'validate_lead' ),
							'sanitize_callback' => array( $this, 'sanitize_lead' ),
						),
					),
				),

			)
		);
	}


	/**
	 * Permission callback.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param \WP_REST_Request $request request
	 *
	 * @return bool
	 */
	public function permission( \WP_REST_Request $request ) {

		$found = false;
		$api = wp_cache_get('api_addon_posts', MortgagePlatform::$domain, false, $found);
		if(!$found){
			$apikey = $request->get_param( 'apikey' );

			$api = new \WP_Query(
				array(
					'post_type'      => MP_Addon_API_PostType::$mortgage_api,
					'posts_per_page' => - 1,
					'meta_key'       => MortgagePlatform::$prefix . 'api_key',
					'meta_value'     => esc_attr( $apikey ),
				)
			);

			wp_cache_set('api_addon_posts', $api, MortgagePlatform::$domain, WEEK_IN_SECONDS);
		}

		if ( $api->have_posts() ) {
			if ( $request->get_method() === 'POST' ) {
				if ( ! empty( get_post_meta( $api->post->ID, MortgagePlatform::$prefix . 'allow_post', true ) ) ) {
					return true;
				} else {
					return new \WP_Error( 'rest_forbidden', esc_html__( 'You cannot view leads.' ), array( 'status' => $this->authorization_status_code() ) );
				}
			} else {
				return true;
			}
		} else {
			return false;
		}

		wp_reset_postdata();

	}


	/**
	 * Sets up the proper HTTP status code for authorization.
	 *
	 * @access public
	 * @since 1.0.0
	 *
	 * @return int
	 */
	public function authorization_status_code() {

		$status = 401;

		return $status;
	}

	/**
	 * Sanitize leadID parameter.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param int $param                param
	 * @param \WP_REST_Request $request request
	 * @param string $key               key
	 *
	 * @return int
	 */
	public function sanitize_lead( $param, $request, $key ) {
		if ( $request->get_method() === 'GET' ) {
			return (int) $param;
		} else {
			return $param;
		}

	}

	/**
	 * Validate leadID parameter.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param int $param                param
	 * @param \WP_REST_Request $request request
	 * @param string $key               key
	 *
	 * @return bool|\WP_Error
	 */
	public function validate_lead( $param, $request, $key ) {

		if ( $request->get_method() === 'GET' ) {
			if ( get_post( $param ) && get_post_type( $param ) === MortgagePlatform::$lead ) {

				return is_numeric( $param );

			} else {
				return new \WP_Error( 'rest_no_leads', esc_html__( 'No found lead with ID ', 'CalculatorsAPIAddon' ) . $param, array( 'status' => 404 ) );
			}
		} else {
			return is_string( $param );

		}
	}


	/**
	 * API Export Lead Data.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param \WP_REST_Request $request request
	 *
	 * @return array
	 */
	public function getLead( \WP_REST_Request $request ) {

		$lead_id = $request->get_param( 'leadID' );

		$lead_data = MP_Addon_API_Callback::get_lead( $lead_id );

		$response = array(
			'code'    => 'rest_lead',
			'message' => 'OK',
			'data'    => array(
				'status' => 200,
				'result' => $lead_data,
			),
		);

		return $response;
	}


	/**
	 * API Import Lead Data.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param \WP_REST_Request $request request
	 *
	 * @return \WP_Error
	 */
	public function postLead( \WP_REST_Request $request ) {

		$data = $request->get_param( 'data' );
		$data = json_decode( $data, true );

		$lead_id = $request->get_param( 'leadID' );

		$response = MP_Addon_API_Callback::post_lead_by_api( $lead_id, $data );

		return $response;
	}


}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.