????
| Current Path : /home/degesdxb/public_html/wp-content/plugins/mphb-pricelabs/includes/ |
| Current File : //home/degesdxb/public_html/wp-content/plugins/mphb-pricelabs/includes/pricelabs-api.php |
<?php
namespace MPHB\Addons\MPHB_Pricelabs;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* API doc: https://documenter.getpostman.com/view/507656/SVSEurQC
* https://app.swaggerhub.com/apis-docs/Customer_API/customer_api/1.0.0-oas3
*/
class Pricelabs_API {
private const BASE_PRICELABS_API_URL = 'https://api.pricelabs.co/v1/';
private const LISTING_ID_PMS_DIVIDER = '#PMS#';
private string $pricelabs_api_key;
private array $listings = array();
public function __construct( string $pricelabs_api_key ) {
$this->pricelabs_api_key = $pricelabs_api_key;
}
/**
* @return array [ listing_id_and_pms (string) => listing_name (string) ]
* @throws Exception if something goes wrong
*/
public function get_listings(): array {
if ( empty( $this->listings ) ) {
$response = wp_remote_get(
add_query_arg(
array(
'skip_hidden' => false,
'only_syncing_listings' => false,
),
self::BASE_PRICELABS_API_URL . 'listings'
),
array(
'headers' => array(
'Content-Type' => 'application/json',
'X-API-Key' => $this->pricelabs_api_key,
),
'timeout' => 15,
)
);
if ( is_wp_error( $response ) ) {
throw new \Exception( $response->get_error_message() );
}
$result = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $result['error'] ) ) {
if ( is_array( $result['error'] ) && ! empty( $result['error']['message'] ) ) {
throw new \Exception( $result['error']['message'] );
} elseif ( is_string( $result['error'] ) ) {
throw new \Exception( $result['error'] );
} else {
throw new \Exception( var_export( $result['error'], true ) );
}
}
if ( isset( $result['listings'] ) ) {
foreach ( $result['listings'] as $listing_data ) {
$this->listings[ $listing_data['id'] . self::LISTING_ID_PMS_DIVIDER . $listing_data['pms'] ] = $listing_data['name'];
}
}
}
return $this->listings;
}
/**
* @return array [ 'listing_id' => $listingId (string), 'pms_name' => $property_managment_system_name (string) ]
* @throws Exception if something goes wrong
*/
public function get_listing_id_and_pms_name( string $listing_id_and_pms_name ): array {
$listingInfo = explode( self::LISTING_ID_PMS_DIVIDER, $listing_id_and_pms_name );
if ( 2 !== count( $listingInfo ) ) {
throw new \Exception( 'Listing identifier: ' . $listing_id_and_pms_name . ' is invalid.' );
}
return array(
'listing_id' => $listingInfo[0],
'pms_name' => $listingInfo[1],
);
}
/**
* @return array [ date (Y-m-d) => price (float) ]
* @throws Exception if something goes wrong
*/
public function get_listing_prices( string $listing_id_and_pms_name, \DateTime $dateFrom, \DateTime $dateTo ): array {
$listingInfo = $this->get_listing_id_and_pms_name( $listing_id_and_pms_name );
$response = wp_remote_post(
self::BASE_PRICELABS_API_URL . 'listing_prices',
array(
'headers' => array(
'Content-Type' => 'application/json',
'X-API-Key' => $this->pricelabs_api_key,
),
'timeout' => 15,
'body' => json_encode(
array(
'listings' => array(
array(
'id' => $listingInfo[ 'listing_id' ],
'pms' => $listingInfo[ 'pms_name' ],
'dateFrom' => $dateFrom->format( 'Y-m-d' ),
'dateTo' => $dateTo->format( 'Y-m-d' ),
// unknown parameter from https://app.swaggerhub.com/apis-docs/Customer_API/customer_api/1.0.0-oas3#/Prices/pricesForListings
// 'reason' => false,
),
),
)
),
)
);
if ( is_wp_error( $response ) ) {
throw new \Exception( $response->get_error_message() );
}
$result = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $result['error'] ) ) {
$error_message = 'Unknown error in PriceLabs API.';
if ( isset( $result['error']['message'] ) ) {
$error_message = $result['error']['message'];
} elseif ( isset( $result['desc'] ) ) {
$error_message = $result['error'] . ': ' . $result['desc'];
} elseif ( is_string( $result['error'] ) ) {
$error_message = $result['error'];
}
throw new \Exception( $error_message );
}
$listing_prices = array();
if ( is_array( $result ) ) {
if ( ! isset( $result[0]['currency'] ) ) {
throw new \Exception( 'Could not determine currency of PriceLabs listing [ ' .
$listingInfo[ 'listing_id' ] . ': ' . $listingInfo[ 'pms_name' ] . ' ]' );
}
if ( MPHB()->settings()->currency()->getCurrencyCode() !== $result[0]['currency'] ) {
throw new \Exception(
'Could not import listing [ ' . $listingInfo[ 'listing_id' ] .
': ' . $listingInfo[ 'pms_name' ] .
' ] because its currency "' . $result[0]['currency'] . '" does not match the currency in the plugin settings.'
);
}
foreach ( $result[0]['data'] as $date_data ) {
$listing_prices[ $date_data['date'] ] = $date_data['price'];
}
}
return $listing_prices;
}
}